MAJOR: Updates for Authenticated Web UI and CLI #30
| @ -588,7 +588,8 @@ function handleApi(req, res) { | |||||||
|   function getStatus() { |   function getStatus() { | ||||||
|     res.setHeader('Content-Type', 'application/json'); |     res.setHeader('Content-Type', 'application/json'); | ||||||
|     res.end(JSON.stringify( |     res.end(JSON.stringify( | ||||||
|       { status: (state.config.disable ? 'disabled' : 'enabled') |       { module: 'status' | ||||||
|  |       , status: (state.config.disable ? 'disabled' : 'enabled') | ||||||
|       , ready: ((state.config.relay && (state.config.token || state.config.agreeTos)) ? true : false) |       , ready: ((state.config.relay && (state.config.token || state.config.agreeTos)) ? true : false) | ||||||
|       , active: !!myRemote |       , active: !!myRemote | ||||||
|       , connected: 'maybe (todo)' |       , connected: 'maybe (todo)' | ||||||
| @ -697,15 +698,30 @@ function serveControlsHelper() { | |||||||
|   , readableAll: true |   , readableAll: true | ||||||
|   , exclusive: false |   , exclusive: false | ||||||
|   }; |   }; | ||||||
|  |   if (!state.config.ipc) { | ||||||
|  |     state.config.ipc = {}; | ||||||
|  |   } | ||||||
|  |   if (!state.config.ipc.path) { | ||||||
|  |     state.config.ipc.path = path.dirname(state._ipc.path); | ||||||
|  |   } | ||||||
|  |   require('mkdirp').sync(state.config.ipc.path); | ||||||
|  |   if (!state.config.ipc.type) { | ||||||
|  |     state.config.ipc.type = 'port'; | ||||||
|  |   } | ||||||
|  |   var portFile = path.join(state.config.ipc.path, 'telebit.port'); | ||||||
|  |   if (fs.existsSync(portFile)) { | ||||||
|  |     state._ipc.port = parseInt(fs.readFileSync(portFile, 'utf8').trim(), 10); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|   if ('socket' === state._ipc.type) { |   if ('socket' === state._ipc.type) { | ||||||
|     require('mkdirp').sync(path.dirname(state._ipc.path)); |     require('mkdirp').sync(path.dirname(state._ipc.path)); | ||||||
|   } |   } | ||||||
|   // https://nodejs.org/api/net.html#net_server_listen_options_callback
 |   // https://nodejs.org/api/net.html#net_server_listen_options_callback
 | ||||||
|   // path is ignore if port is defined
 |   // path is ignore if port is defined
 | ||||||
|   // https://git.coolaj86.com/coolaj86/telebit.js/issues/23#issuecomment-326
 |   // https://git.coolaj86.com/coolaj86/telebit.js/issues/23#issuecomment-326
 | ||||||
|   if (state._ipc.port) { |   if ('port' === state.config.ipc.type) { | ||||||
|     serverOpts.host = 'localhost'; |     serverOpts.host = 'localhost'; | ||||||
|     serverOpts.port = state._ipc.port; |     serverOpts.port = state._ipc.port || 0; | ||||||
|   } else { |   } else { | ||||||
|     serverOpts.path = state._ipc.path; |     serverOpts.path = state._ipc.path; | ||||||
|   } |   } | ||||||
| @ -718,6 +734,21 @@ function serveControlsHelper() { | |||||||
|     //console.log(this.address());
 |     //console.log(this.address());
 | ||||||
|     console.info("[info] Listening for commands on", address); |     console.info("[info] Listening for commands on", address); | ||||||
|   }); |   }); | ||||||
|  |   controlServer.on('error', function (err) { | ||||||
|  |     if ('EADDRINUSE' === err.code) { | ||||||
|  |       try { | ||||||
|  |         fs.unlinkSync(portFile); | ||||||
|  |       } catch(e) { | ||||||
|  |         // nada
 | ||||||
|  |       } | ||||||
|  |       setTimeout(function () { | ||||||
|  |         console.log("trying again"); | ||||||
|  |         serveControlsHelper(); | ||||||
|  |       }, 1000); | ||||||
|  |       return; | ||||||
|  |     } | ||||||
|  |     console.error('failed to start c&c server:', err); | ||||||
|  |   }); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function serveControls() { | function serveControls() { | ||||||
|  | |||||||
							
								
								
									
										60
									
								
								lib/admin/index.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								lib/admin/index.html
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,60 @@ | |||||||
|  | <!DOCTYPE html> | ||||||
|  | <html> | ||||||
|  | <head> | ||||||
|  |   <title>Telebit Admin</title> | ||||||
|  | </head> | ||||||
|  | <body> | ||||||
|  |   <div class="v-app"> | ||||||
|  |     <h1>Telebit Admin</h1> | ||||||
|  | 
 | ||||||
|  |     <section> | ||||||
|  |       <h2>GET /api/config</h2> | ||||||
|  |       <pre><code>{{ config }}</code></pre> | ||||||
|  |     </section> | ||||||
|  | 
 | ||||||
|  |     <section> | ||||||
|  |       <h2>GET /api/status</h2> | ||||||
|  |       <pre><code>{{ status }}</code></pre> | ||||||
|  |     </section> | ||||||
|  | 
 | ||||||
|  |     <section> | ||||||
|  |       <h2>POST /api/init</h2> | ||||||
|  |       <form v-on:submit.stop.prevent="initialize"> | ||||||
|  | 
 | ||||||
|  |         <label for="-email">Email:</label> | ||||||
|  |         <input id="-email" v-model="init.email" type="text" placeholder="john@example.com"> | ||||||
|  |         <br> | ||||||
|  | 
 | ||||||
|  |         <label for="-teletos"><input id="-teletos" v-model="init.teletos" type="checkbox"> | ||||||
|  |           Accept Telebit Terms of Service</label> | ||||||
|  |         <br> | ||||||
|  | 
 | ||||||
|  |         <label for="-letos"><input id="-letos" v-model="init.letos" type="checkbox"> | ||||||
|  |           Accept Let's Encrypt Terms of Service</label> | ||||||
|  |         <br> | ||||||
|  | 
 | ||||||
|  |       </form> | ||||||
|  |       <pre><code>{{ init }}</code></pre> | ||||||
|  |     </section> | ||||||
|  | 
 | ||||||
|  |     <section> | ||||||
|  |       <h2>POST /api/http</h2> | ||||||
|  |       <pre><code>{{ http }}</code></pre> | ||||||
|  |     </section> | ||||||
|  | 
 | ||||||
|  |     <section> | ||||||
|  |       <h2>POST /api/tcp</h2> | ||||||
|  |       <pre><code>{{ tcp }}</code></pre> | ||||||
|  |     </section> | ||||||
|  | 
 | ||||||
|  |     <section> | ||||||
|  |       <h2>POST /api/ssh</h2> | ||||||
|  |       <pre><code>{{ ssh }}</code></pre> | ||||||
|  |     </section> | ||||||
|  | 
 | ||||||
|  |   </div> | ||||||
|  | 
 | ||||||
|  |   <script src="js/vue.js"></script> | ||||||
|  |   <script src="js/app.js"></script> | ||||||
|  | </body> | ||||||
|  | </html> | ||||||
							
								
								
									
										50
									
								
								lib/admin/js/app.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								lib/admin/js/app.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,50 @@ | |||||||
|  | ;(function () { | ||||||
|  | 'use strict'; | ||||||
|  | 
 | ||||||
|  | console.log("hello"); | ||||||
|  | 
 | ||||||
|  | var Vue = window.Vue; | ||||||
|  | var api = {}; | ||||||
|  | 
 | ||||||
|  | api.config = function apiConfig() { | ||||||
|  |   return window.fetch("/api/config", { method: "GET" }).then(function (resp) { | ||||||
|  |     return resp.json().then(function (json) { | ||||||
|  |       appData.config = json; | ||||||
|  |       return json; | ||||||
|  |     }); | ||||||
|  |   }); | ||||||
|  | }; | ||||||
|  | api.status = function apiStatus() { | ||||||
|  |   return window.fetch("/api/status", { method: "GET" }).then(function (resp) { | ||||||
|  |     return resp.json().then(function (json) { | ||||||
|  |       appData.status = json; | ||||||
|  |       return json; | ||||||
|  |     }); | ||||||
|  |   }); | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | var appData = { | ||||||
|  |   config: null | ||||||
|  | , status: null | ||||||
|  | , init: {} | ||||||
|  | , http: null | ||||||
|  | , tcp: null | ||||||
|  | , ssh: null | ||||||
|  | }; | ||||||
|  | var appMethods = { | ||||||
|  |   initialize: function () { | ||||||
|  |     console.log("call initialize"); | ||||||
|  |   } | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | new Vue({ | ||||||
|  |   el: ".v-app" | ||||||
|  | , data: appData | ||||||
|  | , methods: appMethods | ||||||
|  | }); | ||||||
|  | 
 | ||||||
|  | api.config(); | ||||||
|  | api.status(); | ||||||
|  | 
 | ||||||
|  | window.api = api; | ||||||
|  | }()); | ||||||
							
								
								
									
										10947
									
								
								lib/admin/js/vue.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10947
									
								
								lib/admin/js/vue.js
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user