MAJOR: Updates for Authenticated Web UI and CLI #30
							
								
								
									
										100
									
								
								bin/telebitd.js
									
									
									
									
									
								
							
							
						
						
									
										100
									
								
								bin/telebitd.js
									
									
									
									
									
								
							| @ -386,6 +386,7 @@ function handleApi(req, res) { | |||||||
|   function getConfigOnly() { |   function getConfigOnly() { | ||||||
|     var resp = JSON.parse(JSON.stringify(state.config)); |     var resp = JSON.parse(JSON.stringify(state.config)); | ||||||
|     resp.version = pkg.version; |     resp.version = pkg.version; | ||||||
|  |     resp._otp = state.otp; | ||||||
|     res.setHeader('Content-Type', 'application/json'); |     res.setHeader('Content-Type', 'application/json'); | ||||||
|     res.end(JSON.stringify(resp)); |     res.end(JSON.stringify(resp)); | ||||||
|   } |   } | ||||||
| @ -416,50 +417,63 @@ function handleApi(req, res) { | |||||||
|       res.end('{"error":{"message":"module \'init\' needs more arguments"}}'); |       res.end('{"error":{"message":"module \'init\' needs more arguments"}}'); | ||||||
|       return; |       return; | ||||||
|     } |     } | ||||||
|     if (!Array.isArray(opts.body)) { |  | ||||||
|       // TODO
 |  | ||||||
|       res.statusCode = 500; |  | ||||||
|       res.end('{"error":{"message":"[internal error (our fault)] module \'init\' expected an array"}}'); |  | ||||||
|       return; |  | ||||||
|     } |  | ||||||
|     // relay, email, agree_tos, servernames, ports
 |  | ||||||
|     //
 |  | ||||||
|     opts.body.forEach(function (opt) { |  | ||||||
|       var parts = opt.split(/:/); |  | ||||||
|       if ('true' === parts[1]) { |  | ||||||
|         parts[1] = true; |  | ||||||
|       } else if ('false' === parts[1]) { |  | ||||||
|         parts[1] = false; |  | ||||||
|       } else if ('null' === parts[1]) { |  | ||||||
|         parts[1] = null; |  | ||||||
|       } else if ('undefined' === parts[1]) { |  | ||||||
|         parts[1] = undefined; |  | ||||||
|       } |  | ||||||
|       conf[parts[0]] = parts[1]; |  | ||||||
|     }); |  | ||||||
| 
 | 
 | ||||||
|     // TODO camelCase query
 |     if (Array.isArray(opts.body)) { | ||||||
|     state.config.email = conf.email || state.config.email || ''; |       // relay, email, agree_tos, servernames, ports
 | ||||||
|     if ('undefined' !== typeof conf.agreeTos |       //
 | ||||||
|       || 'undefined' !== typeof conf.agreeTos ) { |       opts.body.forEach(function (opt) { | ||||||
|       state.config.agreeTos = conf.agreeTos || conf.agree_tos; |         var parts = opt.split(/:/); | ||||||
|  |         if ('true' === parts[1]) { | ||||||
|  |           parts[1] = true; | ||||||
|  |         } else if ('false' === parts[1]) { | ||||||
|  |           parts[1] = false; | ||||||
|  |         } else if ('null' === parts[1]) { | ||||||
|  |           parts[1] = null; | ||||||
|  |         } else if ('undefined' === parts[1]) { | ||||||
|  |           parts[1] = undefined; | ||||||
|  |         } | ||||||
|  |         conf[parts[0]] = parts[1]; | ||||||
|  |       }); | ||||||
|  |     } else { | ||||||
|  |       conf = opts.body; | ||||||
|     } |     } | ||||||
|     state.otp = conf._otp; // this should only be done on the client side
 | 
 | ||||||
|     state.config.relay = conf.relay || state.config.relay || ''; |     conf = camelCopy(conf); | ||||||
|  | 
 | ||||||
|  |     // TODO deep merge
 | ||||||
|  |     // greenlock config
 | ||||||
|  |     if (!state.config.greenlock) { state.config.greenlock = {}; } | ||||||
|  |     if (conf.greenlock) { | ||||||
|  |       if ('undefined' !== typeof conf.greenlock.agree) { | ||||||
|  |         state.config.greenlock.agree = conf.greenlock.agree; | ||||||
|  |       } | ||||||
|  |       if (conf.greenlock.server) { state.config.greenlock.server = conf.greenlock.server; } | ||||||
|  |       if (conf.greenlock.version) { state.config.greenlock.version = conf.greenlock.version; } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     // main config
 | ||||||
|  |     if (conf.email) { state.config.email = conf.email; } | ||||||
|  |     if (conf.relay) { state.config.relay = conf.relay; } | ||||||
|  |     if (conf.token) { state.config.token = conf.token; } | ||||||
|  |     if (conf.secret) { state.config.secret = conf.secret; } | ||||||
|  |     if ('undefined' !== typeof conf.agreeTos) { | ||||||
|  |       state.config.agreeTos = conf.agreeTos; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     // to state
 | ||||||
|  |     if (conf.pretoken) { state.pretoken = conf.pretoken; } | ||||||
|  |     if (conf._otp) { | ||||||
|  |       state.otp = conf._otp; // TODO should this only be done on the client side?
 | ||||||
|  |       delete conf._otp; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     console.log(); |     console.log(); | ||||||
|     console.log('conf.token', typeof conf.token, conf.token); |     console.log('conf.token', typeof conf.token, conf.token); | ||||||
|     console.log('state.config.token', typeof state.config.token, state.config.token); |     console.log('state.config.token', typeof state.config.token, state.config.token); | ||||||
|     state.config.token = conf.token || state.config.token || null; | 
 | ||||||
|     state.config.secret = conf.secret || state.config.secret || null; |     if (state.secret) { console.log('state.secret'); state.token = common.signToken(state); } | ||||||
|     state.pretoken = conf.pretoken || state.config.pretoken || null; |     if (!state.token) { console.log('!state.token'); state.token = conf._token; } | ||||||
|     if (state.secret) { | 
 | ||||||
|       console.log('state.secret'); |  | ||||||
|       state.token = common.signToken(state); |  | ||||||
|     } |  | ||||||
|     if (!state.token) { |  | ||||||
|       console.log('!state.token'); |  | ||||||
|       state.token = conf._token; |  | ||||||
|     } |  | ||||||
|     console.log(); |     console.log(); | ||||||
|     console.log('JSON.stringify(conf)'); |     console.log('JSON.stringify(conf)'); | ||||||
|     console.log(JSON.stringify(conf)); |     console.log(JSON.stringify(conf)); | ||||||
| @ -630,6 +644,10 @@ function handleApi(req, res) { | |||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   function route() { |   function route() { | ||||||
|  |     if (/\b(relay)\b/.test(opts.pathname)) { | ||||||
|  |       controllers.relay(req, res, opts); | ||||||
|  |       return; | ||||||
|  |     } | ||||||
|     if (/\b(config)\b/.test(opts.pathname) && /get/i.test(req.method)) { |     if (/\b(config)\b/.test(opts.pathname) && /get/i.test(req.method)) { | ||||||
|       getConfigOnly(); |       getConfigOnly(); | ||||||
|       return; |       return; | ||||||
| @ -684,10 +702,6 @@ function handleApi(req, res) { | |||||||
|       listSuccess(); |       listSuccess(); | ||||||
|       return; |       return; | ||||||
|     } |     } | ||||||
|     if (/relay/.test(opts.pathname)) { |  | ||||||
|       controllers.relay(req, res, opts); |  | ||||||
|       return; |  | ||||||
|     } |  | ||||||
| 
 | 
 | ||||||
|     res.setHeader('Content-Type', 'application/json'); |     res.setHeader('Content-Type', 'application/json'); | ||||||
|     res.end(JSON.stringify({"error":{"message":"unrecognized rpc"}})); |     res.end(JSON.stringify({"error":{"message":"unrecognized rpc"}})); | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user