| 
									
										
										
										
											2018-06-11 12:56:16 -06:00
										 |  |  | #!/usr/bin/env node
 | 
					
						
							|  |  |  | (function () { | 
					
						
							|  |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  | var PromiseA; | 
					
						
							|  |  |  | try { | 
					
						
							|  |  |  |   PromiseA = require('bluebird'); | 
					
						
							|  |  |  | } catch(e) { | 
					
						
							|  |  |  |   PromiseA = global.Promise; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-11 12:56:16 -06:00
										 |  |  | var pkg = require('../package.json'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var url = require('url'); | 
					
						
							|  |  |  | var path = require('path'); | 
					
						
							| 
									
										
										
										
											2018-06-11 14:52:01 -06:00
										 |  |  | var os = require('os'); | 
					
						
							| 
									
										
										
										
											2018-06-20 09:07:35 +00:00
										 |  |  | var fs = require('fs'); | 
					
						
							| 
									
										
										
										
											2018-10-20 16:46:53 -06:00
										 |  |  | var urequest = require('@coolaj86/urequest'); | 
					
						
							|  |  |  | var urequestAsync = require('util').promisify(urequest); | 
					
						
							| 
									
										
										
										
											2018-06-11 14:52:01 -06:00
										 |  |  | var common = require('../lib/cli-common.js'); | 
					
						
							| 
									
										
										
										
											2018-06-11 12:56:16 -06:00
										 |  |  | var http = require('http'); | 
					
						
							| 
									
										
										
										
											2018-09-12 03:33:38 -06:00
										 |  |  | var TOML = require('toml'); | 
					
						
							| 
									
										
										
										
											2018-06-11 12:56:16 -06:00
										 |  |  | var YAML = require('js-yaml'); | 
					
						
							| 
									
										
										
										
											2018-06-11 14:52:01 -06:00
										 |  |  | var recase = require('recase').create({}); | 
					
						
							|  |  |  | var camelCopy = recase.camelCopy.bind(recase); | 
					
						
							|  |  |  | var snakeCopy = recase.snakeCopy.bind(recase); | 
					
						
							| 
									
										
										
										
											2018-09-12 03:33:38 -06:00
										 |  |  | var TPLS = TOML.parse(fs.readFileSync(path.join(__dirname, "../lib/en-us.toml"), 'utf8')); | 
					
						
							| 
									
										
										
										
											2018-10-15 23:08:27 -06:00
										 |  |  | var startTime = Date.now(); | 
					
						
							|  |  |  | var connectTimes = []; | 
					
						
							| 
									
										
										
										
											2018-10-22 22:36:46 -06:00
										 |  |  | var isConnected = false; | 
					
						
							| 
									
										
										
										
											2019-02-28 00:00:23 -07:00
										 |  |  | var eggspress = require('../lib/eggspress.js'); | 
					
						
							| 
									
										
										
										
											2018-09-12 03:33:38 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-25 01:14:54 -06:00
										 |  |  | var TelebitRemote = require('../lib/daemon/index.js').TelebitRemote; | 
					
						
							| 
									
										
										
										
											2018-09-05 01:18:12 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-06 03:39:21 -06:00
										 |  |  | var state = { homedir: os.homedir(), servernames: {}, ports: {}, keepAlive: { state: false } }; | 
					
						
							| 
									
										
										
										
											2018-06-11 12:56:16 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | var argv = process.argv.slice(2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var confIndex = argv.indexOf('--config'); | 
					
						
							|  |  |  | var confpath; | 
					
						
							|  |  |  | var confargs; | 
					
						
							|  |  |  | if (-1 === confIndex) { | 
					
						
							|  |  |  |   confIndex = argv.indexOf('-c'); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | if (-1 !== confIndex) { | 
					
						
							|  |  |  |   confargs = argv.splice(confIndex, 2); | 
					
						
							|  |  |  |   confpath = confargs[1]; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-28 20:35:58 -06:00
										 |  |  | var cancelUpdater = require('../lib/updater')(pkg); | 
					
						
							| 
									
										
										
										
											2018-06-14 05:24:28 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-11 12:56:16 -06:00
										 |  |  | function help() { | 
					
						
							| 
									
										
										
										
											2018-09-12 03:33:38 -06:00
										 |  |  |   console.info(TPLS.daemon.help.main.replace(/{version}/g, pkg.version)); | 
					
						
							| 
									
										
										
										
											2018-06-11 12:56:16 -06:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-14 02:42:41 -06:00
										 |  |  | var verstr = [ pkg.name + ' daemon v' + pkg.version ]; | 
					
						
							| 
									
										
										
										
											2018-06-11 12:56:16 -06:00
										 |  |  | if (-1 === confIndex) { | 
					
						
							| 
									
										
										
										
											2018-06-12 04:36:37 -06:00
										 |  |  |   // We have two possible valid paths if no --config is given (i.e. run from an npm-only install)
 | 
					
						
							|  |  |  |   //   * {install}/etc/telebitd.yml
 | 
					
						
							|  |  |  |   //   * ~/.config/telebit/telebitd.yml
 | 
					
						
							|  |  |  |   // We'll asume the later since the installers include --config in the system launcher script
 | 
					
						
							| 
									
										
										
										
											2018-06-28 20:35:58 -06:00
										 |  |  |   confpath = common.DEFAULT_CONFIG_PATH; | 
					
						
							| 
									
										
										
										
											2018-06-14 02:39:34 -06:00
										 |  |  |   verstr.push('(--config "' + confpath + '")'); | 
					
						
							| 
									
										
										
										
											2018-06-11 12:56:16 -06:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if (-1 !== argv.indexOf('-h') || -1 !== argv.indexOf('--help')) { | 
					
						
							|  |  |  |   help(); | 
					
						
							| 
									
										
										
										
											2018-06-11 14:52:01 -06:00
										 |  |  |   process.exit(0); | 
					
						
							| 
									
										
										
										
											2018-06-11 12:56:16 -06:00
										 |  |  | } | 
					
						
							|  |  |  | if (!confpath || /^--/.test(confpath)) { | 
					
						
							|  |  |  |   help(); | 
					
						
							| 
									
										
										
										
											2018-06-11 14:52:01 -06:00
										 |  |  |   process.exit(1); | 
					
						
							| 
									
										
										
										
											2018-06-11 12:56:16 -06:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | state._confpath = confpath; | 
					
						
							|  |  |  | var tokenpath = path.join(path.dirname(state._confpath), 'access_token.txt'); | 
					
						
							| 
									
										
										
										
											2018-06-11 12:56:16 -06:00
										 |  |  | var token; | 
					
						
							|  |  |  | try { | 
					
						
							| 
									
										
										
										
											2018-06-28 20:35:58 -06:00
										 |  |  |   token = fs.readFileSync(tokenpath, 'ascii').trim(); | 
					
						
							| 
									
										
										
										
											2018-09-13 23:42:04 -06:00
										 |  |  |   //console.log('[DEBUG] access_token', typeof token, token);
 | 
					
						
							| 
									
										
										
										
											2018-06-11 12:56:16 -06:00
										 |  |  | } catch(e) { | 
					
						
							|  |  |  |   // ignore
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | var controlServer; | 
					
						
							| 
									
										
										
										
											2018-09-03 22:56:52 -06:00
										 |  |  | var myRemote; | 
					
						
							| 
									
										
										
										
											2018-06-16 01:11:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-30 19:34:15 -06:00
										 |  |  | function getServername(servernames, sub) { | 
					
						
							|  |  |  |   if (state.servernames[sub]) { | 
					
						
							|  |  |  |     return sub; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   var names = Object.keys(servernames).map(function (servername) { | 
					
						
							|  |  |  |     if ('*.' === servername.slice(0,2)) { | 
					
						
							|  |  |  |       return servername; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return '*.' + servername; | 
					
						
							|  |  |  |   }).sort(function (a, b) { | 
					
						
							|  |  |  |     return b.length - a.length; | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return names.filter(function (pattern) { | 
					
						
							|  |  |  |     // '.example.com' = '*.example.com'.split(1)
 | 
					
						
							|  |  |  |     var subPiece = pattern.slice(1); | 
					
						
							|  |  |  |     // '.com' = 'sub.example.com'.slice(-4)
 | 
					
						
							|  |  |  |     // '.example.com' = 'sub.example.com'.slice(-12)
 | 
					
						
							|  |  |  |     if (subPiece === sub.slice(-subPiece.length)) { | 
					
						
							|  |  |  |       return subPiece; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   })[0]; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | function saveConfig(cb) { | 
					
						
							|  |  |  |   fs.writeFile(confpath, YAML.safeDump(snakeCopy(state.config)), cb); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | var controllers = {}; | 
					
						
							| 
									
										
										
										
											2019-02-28 00:00:23 -07:00
										 |  |  | controllers.http = function (req, res) { | 
					
						
							| 
									
										
										
										
											2018-06-30 19:34:15 -06:00
										 |  |  |   function getAppname(pathname) { | 
					
						
							|  |  |  |     // port number
 | 
					
						
							|  |  |  |     if (String(pathname) === String(parseInt(pathname, 10))) { | 
					
						
							|  |  |  |       return String(pathname); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     var paths = pathname.split(/[\\\/\:]/); | 
					
						
							|  |  |  |     // rid trailing slash(es)
 | 
					
						
							|  |  |  |     while (!paths[paths.length -1]) { | 
					
						
							|  |  |  |       paths.pop(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     var name = paths.pop(); | 
					
						
							|  |  |  |     name = path.basename(name, path.extname(name)); | 
					
						
							|  |  |  |     name = name.replace(/\./, '-').replace(/-+/, '-'); | 
					
						
							|  |  |  |     return name; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-11-01 03:11:47 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |   function assign(target, handler, indexes) { | 
					
						
							|  |  |  |     target.handler = handler; | 
					
						
							|  |  |  |     if (indexes) { | 
					
						
							|  |  |  |       target.indexes = true; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       delete target.indexes; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-28 00:00:23 -07:00
										 |  |  |   if (!req.body) { | 
					
						
							| 
									
										
										
										
											2018-06-30 19:34:15 -06:00
										 |  |  |     res.statusCode = 422; | 
					
						
							|  |  |  |     res.setHeader('Content-Type', 'application/json'); | 
					
						
							| 
									
										
										
										
											2018-11-02 10:31:31 -06:00
										 |  |  |     res.end(JSON.stringify({"error":{"message":"module \'http\' needs some arguments"}})); | 
					
						
							| 
									
										
										
										
											2018-06-30 19:34:15 -06:00
										 |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-11-01 03:11:47 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-30 19:34:15 -06:00
										 |  |  |   var active = true; | 
					
						
							| 
									
										
										
										
											2019-02-28 00:00:23 -07:00
										 |  |  |   var portOrPath = req.body.handler || req.body[0]; | 
					
						
							|  |  |  |   var subdomain = req.body.name || req.body[1]; | 
					
						
							|  |  |  |   var indexes = req.body.indexes; | 
					
						
							| 
									
										
										
										
											2018-06-30 19:34:15 -06:00
										 |  |  |   var remoteHost; | 
					
						
							| 
									
										
										
										
											2018-07-27 22:30:58 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-02 10:31:31 -06:00
										 |  |  |   if (!portOrPath) { | 
					
						
							| 
									
										
										
										
											2018-11-01 03:11:47 -06:00
										 |  |  |     res.statusCode = 422; | 
					
						
							|  |  |  |     res.setHeader('Content-Type', 'application/json'); | 
					
						
							| 
									
										
										
										
											2018-11-02 10:31:31 -06:00
										 |  |  |     res.end(JSON.stringify({ error: { message: "module 'http' needs port or path" } })); | 
					
						
							| 
									
										
										
										
											2018-11-01 03:11:47 -06:00
										 |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   var appname = getAppname(portOrPath); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-27 22:30:58 -06:00
										 |  |  |   // Assign an FQDN to brief subdomains
 | 
					
						
							|  |  |  |   // ex: foo => foo.rando.telebit.cloud
 | 
					
						
							|  |  |  |   if (subdomain && !/\./.test(subdomain)) { | 
					
						
							|  |  |  |     Object.keys(state.servernames).some(function (key) { | 
					
						
							|  |  |  |       if (state.servernames[key].wildcard) { | 
					
						
							|  |  |  |         subdomain += '.' + key; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-27 23:10:36 -06:00
										 |  |  |   if ('none' === portOrPath || 'none' === subdomain) { | 
					
						
							|  |  |  |     // ~/telebit http none                  // turn off all
 | 
					
						
							|  |  |  |     // ~/telebit http none none             // (same as above)
 | 
					
						
							|  |  |  |     // ~/telebit http 3000 none             // turn off this handler
 | 
					
						
							|  |  |  |     // ~/telebit http none sub.example.com  // turn off this subdomain
 | 
					
						
							|  |  |  |     // ~/telebit http none sub              // TODO
 | 
					
						
							|  |  |  |     Object.keys(state.servernames).forEach(function (key) { | 
					
						
							|  |  |  |       if ('none' === portOrPath && 'none' === subdomain) { | 
					
						
							|  |  |  |         delete state.servernames[key].handler; | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       if (state.servernames[key].handler === portOrPath) { | 
					
						
							|  |  |  |         delete state.servernames[key].handler; | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       if (!subdomain || key === subdomain) { | 
					
						
							|  |  |  |         if (state.servernames[key].sub) { | 
					
						
							|  |  |  |           delete state.servernames[key]; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |           delete state.servernames[key].handler; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2018-11-01 03:11:47 -06:00
										 |  |  |     if (state.servernames[subdomain]) { | 
					
						
							|  |  |  |       // TODO remove all non-essential keys
 | 
					
						
							|  |  |  |       delete state.servernames[subdomain].handler; | 
					
						
							|  |  |  |       if (state.servernames[subdomain].sub) { | 
					
						
							|  |  |  |         delete state.servernames[subdomain]; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-07-27 22:30:58 -06:00
										 |  |  |     remoteHost = 'none'; | 
					
						
							| 
									
										
										
										
											2018-07-27 23:10:36 -06:00
										 |  |  |   } else if (subdomain && 'none' !== subdomain) { | 
					
						
							| 
									
										
										
										
											2018-07-27 22:30:58 -06:00
										 |  |  |     // use a subdomain with this handler
 | 
					
						
							| 
									
										
										
										
											2018-06-30 19:34:15 -06:00
										 |  |  |     var handlerName = getServername(state.servernames, subdomain); | 
					
						
							|  |  |  |     if (!handlerName) { | 
					
						
							|  |  |  |       active = false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (!state.servernames[subdomain]) { | 
					
						
							| 
									
										
										
										
											2018-07-27 23:10:36 -06:00
										 |  |  |       state.servernames[subdomain] = { sub: true }; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if ('none' === portOrPath) { | 
					
						
							|  |  |  |       delete state.servernames[subdomain].handler; | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2018-11-01 03:11:47 -06:00
										 |  |  |       assign(state.servernames[subdomain], portOrPath, indexes); | 
					
						
							| 
									
										
										
										
											2018-06-30 19:34:15 -06:00
										 |  |  |     } | 
					
						
							|  |  |  |     remoteHost = subdomain; | 
					
						
							|  |  |  |   } else { | 
					
						
							| 
									
										
										
										
											2018-07-27 22:30:58 -06:00
										 |  |  |     // just replace the default domain
 | 
					
						
							| 
									
										
										
										
											2018-06-30 19:34:15 -06:00
										 |  |  |     if (!Object.keys(state.servernames).sort(function (a, b) { | 
					
						
							|  |  |  |       return b.length - a.length; | 
					
						
							|  |  |  |     }).some(function (key) { | 
					
						
							|  |  |  |       if (state.servernames[key].handler === appname) { | 
					
						
							|  |  |  |         // example.com.handler: 3000 // already set
 | 
					
						
							|  |  |  |         remoteHost = key; | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       if (state.servernames[key].wildcard) { | 
					
						
							| 
									
										
										
										
											2018-07-27 22:30:58 -06:00
										 |  |  |         //var prefix = appname + '.' + key;
 | 
					
						
							|  |  |  |         var prefix = key; | 
					
						
							|  |  |  |         if (!state.servernames[prefix]) { | 
					
						
							| 
									
										
										
										
											2018-07-27 23:10:36 -06:00
										 |  |  |           state.servernames[prefix] = { sub: undefined }; | 
					
						
							| 
									
										
										
										
											2018-06-30 19:34:15 -06:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-11-01 03:11:47 -06:00
										 |  |  |         assign(state.servernames[prefix], portOrPath, indexes); | 
					
						
							| 
									
										
										
										
											2018-07-27 22:30:58 -06:00
										 |  |  |         remoteHost = prefix; | 
					
						
							| 
									
										
										
										
											2018-06-30 19:34:15 -06:00
										 |  |  |         return true; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     })) { | 
					
						
							|  |  |  |       Object.keys(state.servernames).some(function (key) { | 
					
						
							| 
									
										
										
										
											2018-07-27 22:30:58 -06:00
										 |  |  |         //var prefix = appname + '.' + key;
 | 
					
						
							|  |  |  |         var prefix = key; | 
					
						
							| 
									
										
										
										
											2018-11-01 03:11:47 -06:00
										 |  |  |         assign(state.servernames[key], portOrPath, indexes); | 
					
						
							| 
									
										
										
										
											2018-07-27 22:30:58 -06:00
										 |  |  |         remoteHost = prefix; | 
					
						
							| 
									
										
										
										
											2018-06-30 19:34:15 -06:00
										 |  |  |         return true; | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   state.config.servernames = state.servernames; | 
					
						
							|  |  |  |   saveConfig(function (err) { | 
					
						
							|  |  |  |     res.setHeader('Content-Type', 'application/json'); | 
					
						
							|  |  |  |     res.end(JSON.stringify({ | 
					
						
							|  |  |  |       success: true | 
					
						
							|  |  |  |     , active: active | 
					
						
							|  |  |  |     , remote: remoteHost | 
					
						
							|  |  |  |     , local: portOrPath | 
					
						
							|  |  |  |     , saved: !err | 
					
						
							|  |  |  |     , module: 'http' | 
					
						
							|  |  |  |     })); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2019-02-28 00:00:23 -07:00
										 |  |  | controllers.tcp = function (req, res) { | 
					
						
							|  |  |  |   if (!req.body) { | 
					
						
							| 
									
										
										
										
											2018-06-30 19:34:15 -06:00
										 |  |  |     res.statusCode = 422; | 
					
						
							|  |  |  |     res.setHeader('Content-Type', 'application/json'); | 
					
						
							|  |  |  |     res.end(JSON.stringify({ error: { message: "module 'tcp' needs more arguments" } })); | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   var active; | 
					
						
							| 
									
										
										
										
											2019-02-28 00:00:23 -07:00
										 |  |  |   var remotePort = req.body[1]; | 
					
						
							|  |  |  |   var portOrPath = req.body[0]; | 
					
						
							| 
									
										
										
										
											2018-06-30 19:34:15 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // portnum
 | 
					
						
							|  |  |  |   if (remotePort) { | 
					
						
							|  |  |  |     if (!state.ports[remotePort]) { | 
					
						
							|  |  |  |       active = false; | 
					
						
							| 
									
										
										
										
											2018-09-25 02:18:38 -06:00
										 |  |  |     } else { | 
					
						
							|  |  |  |       // forward-to port-or-module
 | 
					
						
							|  |  |  |       // TODO with the connect event bug fixed, we should now be able to send files over tcp
 | 
					
						
							|  |  |  |       state.ports[remotePort].handler = portOrPath; | 
					
						
							| 
									
										
										
										
											2018-06-30 19:34:15 -06:00
										 |  |  |     } | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     if (!Object.keys(state.ports).some(function (key) { | 
					
						
							|  |  |  |       if (!state.ports[key].handler) { | 
					
						
							|  |  |  |         state.ports[key].handler = portOrPath; | 
					
						
							|  |  |  |         remotePort = key; | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     })) { | 
					
						
							|  |  |  |       Object.keys(state.ports).some(function (key) { | 
					
						
							|  |  |  |         state.ports[key].handler = portOrPath; | 
					
						
							|  |  |  |         remotePort = key; | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   state.config.ports = state.ports; | 
					
						
							|  |  |  |   saveConfig(function (err) { | 
					
						
							|  |  |  |     res.setHeader('Content-Type', 'application/json'); | 
					
						
							|  |  |  |     res.end(JSON.stringify({ | 
					
						
							|  |  |  |       success: true | 
					
						
							|  |  |  |     , active: active | 
					
						
							|  |  |  |     , remote: remotePort | 
					
						
							|  |  |  |     , local: portOrPath | 
					
						
							|  |  |  |     , saved: !err | 
					
						
							|  |  |  |     , module: 'tcp' | 
					
						
							|  |  |  |     })); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2019-02-28 00:00:23 -07:00
										 |  |  | controllers.ssh = function (req, res) { | 
					
						
							|  |  |  |   if (!req.body) { | 
					
						
							| 
									
										
										
										
											2018-06-30 19:34:15 -06:00
										 |  |  |     res.statusCode = 422; | 
					
						
							|  |  |  |     res.setHeader('Content-Type', 'application/json'); | 
					
						
							|  |  |  |     res.end(JSON.stringify({"error":{"message":"module 'ssh' needs more arguments"}})); | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function sshSuccess() { | 
					
						
							|  |  |  |     //state.config.sshAuto = state.sshAuto;
 | 
					
						
							|  |  |  |     saveConfig(function (err) { | 
					
						
							| 
									
										
										
										
											2018-09-25 01:39:05 -06:00
										 |  |  |       var local = state.config.sshAuto; | 
					
						
							|  |  |  |       if (false !== local && !local) { | 
					
						
							|  |  |  |         local = 22; | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2018-06-30 19:34:15 -06:00
										 |  |  |       res.setHeader('Content-Type', 'application/json'); | 
					
						
							|  |  |  |       res.end(JSON.stringify({ | 
					
						
							|  |  |  |         success: true | 
					
						
							|  |  |  |       , active: true | 
					
						
							|  |  |  |       , remote: Object.keys(state.config.ports)[0] | 
					
						
							| 
									
										
										
										
											2018-09-25 01:39:05 -06:00
										 |  |  |       , local: local | 
					
						
							| 
									
										
										
										
											2018-06-30 19:34:15 -06:00
										 |  |  |       , saved: !err | 
					
						
							|  |  |  |       , module: 'ssh' | 
					
						
							|  |  |  |       })); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-28 00:00:23 -07:00
										 |  |  |   var rawSshAuto = req.body.port || req.body[0]; | 
					
						
							| 
									
										
										
										
											2018-10-23 00:44:59 -06:00
										 |  |  |   var sshAuto = rawSshAuto; | 
					
						
							|  |  |  |   if (-1 !== [ -1, 'false', 'none', 'off', 'disable' ].indexOf(sshAuto)) { | 
					
						
							| 
									
										
										
										
											2018-06-30 19:34:15 -06:00
										 |  |  |     state.config.sshAuto = false; | 
					
						
							|  |  |  |     sshSuccess(); | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (-1 !== [ 'true', 'auto', 'on', 'enable' ].indexOf(sshAuto)) { | 
					
						
							|  |  |  |     state.config.sshAuto = 22; | 
					
						
							|  |  |  |     sshSuccess(); | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   sshAuto = parseInt(sshAuto, 10); | 
					
						
							|  |  |  |   if (!sshAuto || sshAuto <= 0 || sshAuto > 65535) { | 
					
						
							|  |  |  |     res.statusCode = 400; | 
					
						
							|  |  |  |     res.setHeader('Content-Type', 'application/json'); | 
					
						
							| 
									
										
										
										
											2018-10-23 00:44:59 -06:00
										 |  |  |     res.end(JSON.stringify({ error: { message: "bad ssh_auto option '" + rawSshAuto + "'" } })); | 
					
						
							| 
									
										
										
										
											2018-06-30 19:34:15 -06:00
										 |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   state.config.sshAuto = sshAuto; | 
					
						
							|  |  |  |   sshSuccess(); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2019-02-28 00:00:23 -07:00
										 |  |  | controllers.relay = function (req, res) { | 
					
						
							|  |  |  |   if (!req.body) { | 
					
						
							| 
									
										
										
										
											2018-10-20 16:46:53 -06:00
										 |  |  |     res.statusCode = 422; | 
					
						
							|  |  |  |     res.setHeader('Content-Type', 'application/json'); | 
					
						
							|  |  |  |     res.end(JSON.stringify({"error":{"message":"module \'relay\' needs more arguments"}})); | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-28 00:00:23 -07:00
										 |  |  |   return urequestAsync(req.body).then(function (resp) { | 
					
						
							| 
									
										
										
										
											2018-10-20 16:46:53 -06:00
										 |  |  |     res.setHeader('Content-Type', 'application/json'); | 
					
						
							| 
									
										
										
										
											2018-10-21 03:32:04 -06:00
										 |  |  |     resp = resp.toJSON(); | 
					
						
							| 
									
										
										
										
											2018-10-20 16:46:53 -06:00
										 |  |  |     res.end(JSON.stringify(resp)); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2018-06-12 04:36:37 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-28 00:00:23 -07:00
										 |  |  | function handleApi() { | 
					
						
							|  |  |  |   var app = eggspress(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   app.use('/', function (req, res, next) { | 
					
						
							|  |  |  |     var opts = url.parse(req.url, true); | 
					
						
							|  |  |  |     if (false && opts.query._body) { | 
					
						
							|  |  |  |       try { | 
					
						
							|  |  |  |         req.body = JSON.parse(decodeURIComponent(opts.query._body, true)); | 
					
						
							|  |  |  |       } catch(e) { | 
					
						
							|  |  |  |         res.statusCode = 500; | 
					
						
							|  |  |  |         res.end('{"error":{"message":"?_body={{bad_format}}"}}'); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     var hasLength = req.headers['content-length'] > 0; | 
					
						
							|  |  |  |     if (!hasLength && !req.headers['content-type']) { | 
					
						
							|  |  |  |       next(); | 
					
						
							| 
									
										
										
										
											2018-10-15 21:02:57 -06:00
										 |  |  |       return; | 
					
						
							| 
									
										
										
										
											2018-06-11 14:52:01 -06:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-06-11 12:56:16 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-28 00:00:23 -07:00
										 |  |  |     var body = ''; | 
					
						
							|  |  |  |     req.on('readable', function () { | 
					
						
							|  |  |  |       var data; | 
					
						
							|  |  |  |       while (true) { | 
					
						
							|  |  |  |         data = req.read(); | 
					
						
							|  |  |  |         if (!data) { break; } | 
					
						
							|  |  |  |         body += data.toString(); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     req.on('end', function () { | 
					
						
							|  |  |  |       try { | 
					
						
							|  |  |  |         req.body = JSON.parse(body); | 
					
						
							|  |  |  |       } catch(e) { | 
					
						
							|  |  |  |         res.statusCode = 400; | 
					
						
							|  |  |  |         res.end('{"error":{"message":"POST body is not valid json"}}'); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       next(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function listSuccess(req, res) { | 
					
						
							| 
									
										
										
										
											2018-10-15 21:02:57 -06:00
										 |  |  |     var dumpy = { | 
					
						
							|  |  |  |       servernames: state.servernames | 
					
						
							|  |  |  |     , ports: state.ports | 
					
						
							|  |  |  |     , ssh: state.config.sshAuto || 'disabled' | 
					
						
							|  |  |  |     , code: 'CONFIG' | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     if (state.otp) { | 
					
						
							|  |  |  |       dumpy.device_pair_code = state.otp; | 
					
						
							| 
									
										
										
										
											2018-06-27 22:58:43 -06:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 21:02:57 -06:00
										 |  |  |     if (state._can_pair && state.config.email && !state.token) { | 
					
						
							|  |  |  |       dumpy.code = "AWAIT_AUTH"; | 
					
						
							|  |  |  |       dumpy.message = "Please run 'telebit init' to authenticate."; | 
					
						
							| 
									
										
										
										
											2018-06-28 20:35:58 -06:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-09-03 22:56:52 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 21:02:57 -06:00
										 |  |  |     res.end(JSON.stringify(dumpy)); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-06-12 04:36:37 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-28 00:00:23 -07:00
										 |  |  |   function getConfigOnly(req, res) { | 
					
						
							| 
									
										
										
										
											2018-10-15 21:02:57 -06:00
										 |  |  |     var resp = JSON.parse(JSON.stringify(state.config)); | 
					
						
							|  |  |  |     resp.version = pkg.version; | 
					
						
							| 
									
										
										
										
											2018-10-22 00:13:03 -06:00
										 |  |  |     resp._otp = state.otp; | 
					
						
							| 
									
										
										
										
											2018-10-15 21:02:57 -06:00
										 |  |  |     res.setHeader('Content-Type', 'application/json'); | 
					
						
							|  |  |  |     res.end(JSON.stringify(resp)); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-06-13 12:34:45 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 21:02:57 -06:00
										 |  |  |   //
 | 
					
						
							|  |  |  |   // without proper config
 | 
					
						
							|  |  |  |   //
 | 
					
						
							| 
									
										
										
										
											2019-02-28 00:00:23 -07:00
										 |  |  |   function saveAndReport(req, res) { | 
					
						
							| 
									
										
										
										
											2018-10-15 21:02:57 -06:00
										 |  |  |     console.log('[DEBUG] saveAndReport config write', confpath); | 
					
						
							|  |  |  |     console.log(YAML.safeDump(snakeCopy(state.config))); | 
					
						
							|  |  |  |     fs.writeFile(confpath, YAML.safeDump(snakeCopy(state.config)), function (err) { | 
					
						
							|  |  |  |       if (err) { | 
					
						
							|  |  |  |         res.statusCode = 500; | 
					
						
							| 
									
										
										
										
											2018-06-28 20:35:58 -06:00
										 |  |  |         res.setHeader('Content-Type', 'application/json'); | 
					
						
							| 
									
										
										
										
											2018-10-15 21:02:57 -06:00
										 |  |  |         res.end('{"error":{"message":"Could not save config file after init: ' + err.message.replace(/"/g, "'") | 
					
						
							|  |  |  |           + '.\nPerhaps check that the file exists and your user has permissions to write it?"}}'); | 
					
						
							| 
									
										
										
										
											2018-06-12 04:36:37 -06:00
										 |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 21:02:57 -06:00
										 |  |  |       listSuccess(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-28 00:00:23 -07:00
										 |  |  |     function initOrConfig(req, res) { | 
					
						
							|  |  |  |       var conf = {}; | 
					
						
							|  |  |  |       if (!req.body) { | 
					
						
							|  |  |  |         res.statusCode = 422; | 
					
						
							|  |  |  |         res.end('{"error":{"message":"module \'init\' needs more arguments"}}'); | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2018-10-15 21:02:57 -06:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2018-10-22 00:13:03 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-28 00:00:23 -07:00
										 |  |  |       if (Array.isArray(req.body)) { | 
					
						
							|  |  |  |         // relay, email, agree_tos, servernames, ports
 | 
					
						
							|  |  |  |         //
 | 
					
						
							|  |  |  |         req.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]; | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         conf = req.body; | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2018-06-13 14:48:29 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-28 00:00:23 -07:00
										 |  |  |       conf = camelCopy(conf); | 
					
						
							| 
									
										
										
										
											2018-10-22 00:13:03 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-28 00:00:23 -07:00
										 |  |  |       // 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; | 
					
						
							| 
									
										
										
										
											2018-06-11 12:56:16 -06:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-02-28 00:00:23 -07:00
										 |  |  |         if (conf.greenlock.server) { state.config.greenlock.server = conf.greenlock.server; } | 
					
						
							|  |  |  |         if (conf.greenlock.version) { state.config.greenlock.version = conf.greenlock.version; } | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-28 00:00:23 -07:00
										 |  |  |       // 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; | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-28 00:00:23 -07:00
										 |  |  |       // 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; | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2018-06-11 14:52:01 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-28 00:00:23 -07:00
										 |  |  |       console.log(); | 
					
						
							|  |  |  |       console.log('conf.token', typeof conf.token, conf.token); | 
					
						
							|  |  |  |       console.log('state.config.token', typeof state.config.token, state.config.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('JSON.stringify(conf)'); | 
					
						
							|  |  |  |       console.log(JSON.stringify(conf)); | 
					
						
							|  |  |  |       console.log(); | 
					
						
							|  |  |  |       console.log('JSON.stringify(state)'); | 
					
						
							|  |  |  |       console.log(JSON.stringify(state)); | 
					
						
							|  |  |  |       console.log(); | 
					
						
							|  |  |  |       if ('undefined' !== typeof conf.newsletter) { | 
					
						
							|  |  |  |         state.config.newsletter = conf.newsletter; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       if ('undefined' !== typeof conf.communityMember | 
					
						
							|  |  |  |         || 'undefined' !== typeof conf.community_member) { | 
					
						
							|  |  |  |         state.config.communityMember = conf.communityMember || conf.community_member; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       if ('undefined' !== typeof conf.telemetry) { | 
					
						
							|  |  |  |         state.config.telemetry = conf.telemetry; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       if (conf._servernames) { | 
					
						
							|  |  |  |         (conf._servernames||'').split(/,/g).forEach(function (key) { | 
					
						
							|  |  |  |           if (!state.config.servernames[key]) { | 
					
						
							|  |  |  |             state.config.servernames[key] = { sub: undefined }; | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       if (conf._ports) { | 
					
						
							|  |  |  |         (conf._ports||'').split(/,/g).forEach(function (key) { | 
					
						
							|  |  |  |           if (!state.config.ports[key]) { | 
					
						
							|  |  |  |             state.config.ports[key] = {}; | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2018-06-11 12:56:16 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-28 00:00:23 -07:00
										 |  |  |       if (!state.config.relay || !state.config.email || !state.config.agreeTos) { | 
					
						
							|  |  |  |         console.warn('missing config'); | 
					
						
							|  |  |  |         res.statusCode = 400; | 
					
						
							| 
									
										
										
										
											2018-06-11 14:52:01 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 21:02:57 -06:00
										 |  |  |         res.setHeader('Content-Type', 'application/json'); | 
					
						
							|  |  |  |         res.end(JSON.stringify({ | 
					
						
							| 
									
										
										
										
											2019-02-28 00:00:23 -07:00
										 |  |  |           error: { | 
					
						
							|  |  |  |             code: "E_INIT" | 
					
						
							|  |  |  |           , message: "Missing important config file params" | 
					
						
							|  |  |  |           , _params: JSON.stringify(conf) | 
					
						
							|  |  |  |           , _config: JSON.stringify(state.config) | 
					
						
							|  |  |  |           , _body: JSON.stringify(req.body) | 
					
						
							|  |  |  |           } | 
					
						
							| 
									
										
										
										
											2018-10-15 21:02:57 -06:00
										 |  |  |         })); | 
					
						
							| 
									
										
										
										
											2018-09-25 02:18:38 -06:00
										 |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2018-10-15 21:02:57 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-28 00:00:23 -07:00
										 |  |  |       // init also means enable
 | 
					
						
							|  |  |  |       delete state.config.disable; | 
					
						
							|  |  |  |       safeStartTelebitRemote(true).then(saveAndReport).catch(handleError); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-10-15 21:02:57 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-28 00:00:23 -07:00
										 |  |  |     function restart(req, res) { | 
					
						
							|  |  |  |       console.info("[telebitd.js] server closing..."); | 
					
						
							|  |  |  |       state.keepAlive.state = false; | 
					
						
							| 
									
										
										
										
											2018-10-15 21:02:57 -06:00
										 |  |  |       if (myRemote) { | 
					
						
							| 
									
										
										
										
											2019-02-28 00:00:23 -07:00
										 |  |  |         myRemote.end(); | 
					
						
							|  |  |  |         myRemote.on('end', respondAndClose); | 
					
						
							|  |  |  |         // failsafe
 | 
					
						
							|  |  |  |         setTimeout(function () { | 
					
						
							|  |  |  |           console.info("[telebitd.js] closing too slowly, force quit"); | 
					
						
							|  |  |  |           respondAndClose(); | 
					
						
							|  |  |  |         }, 5 * 1000); | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         respondAndClose(); | 
					
						
							| 
									
										
										
										
											2018-09-25 02:18:38 -06:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2018-10-15 21:02:57 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-28 00:00:23 -07:00
										 |  |  |       function respondAndClose() { | 
					
						
							|  |  |  |         res.setHeader('Content-Type', 'application/json'); | 
					
						
							|  |  |  |         res.end(JSON.stringify({ success: true })); | 
					
						
							|  |  |  |         controlServer.close(function () { | 
					
						
							|  |  |  |           console.info("[telebitd.js] server closed"); | 
					
						
							|  |  |  |           setTimeout(function () { | 
					
						
							|  |  |  |             // system daemon will restart the process
 | 
					
						
							|  |  |  |             process.exit(22); // use non-success exit code
 | 
					
						
							|  |  |  |           }, 100); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-09-25 02:18:38 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-28 00:00:23 -07:00
										 |  |  |     function mustHaveValidConfig(req, res, next) { | 
					
						
							|  |  |  |       //
 | 
					
						
							|  |  |  |       // Check for proper config
 | 
					
						
							|  |  |  |       //
 | 
					
						
							|  |  |  |       if (state.config.relay && state.config.email && state.config.agreeTos) { | 
					
						
							|  |  |  |         next(); | 
					
						
							| 
									
										
										
										
											2018-10-15 21:02:57 -06:00
										 |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-28 00:00:23 -07:00
										 |  |  |       res.statusCode = 400; | 
					
						
							|  |  |  |       res.setHeader('Content-Type', 'application/json'); | 
					
						
							|  |  |  |       res.end(JSON.stringify({ | 
					
						
							|  |  |  |         error: { code: "E_CONFIG", message: "Invalid config file. Please run 'telebit init'" } | 
					
						
							|  |  |  |       })); | 
					
						
							| 
									
										
										
										
											2018-10-15 21:02:57 -06:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-02-28 00:00:23 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     function saveAndCommit(req, res) { | 
					
						
							|  |  |  |       state.config.servernames = state.servernames; | 
					
						
							|  |  |  |       state.config.ports = state.ports; | 
					
						
							|  |  |  |       fs.writeFile(confpath, YAML.safeDump(snakeCopy(state.config)), function (err) { | 
					
						
							|  |  |  |         if (err) { | 
					
						
							|  |  |  |           res.statusCode = 500; | 
					
						
							|  |  |  |           res.setHeader('Content-Type', 'application/json'); | 
					
						
							|  |  |  |           res.end(JSON.stringify({ | 
					
						
							|  |  |  |             "error":{"message":"Could not save config file. Perhaps you're not running as root?"} | 
					
						
							|  |  |  |           })); | 
					
						
							|  |  |  |           return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         listSuccess(); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2018-10-15 21:02:57 -06:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-02-28 00:00:23 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     function handleError(err, req, res) { | 
					
						
							|  |  |  |       res.statusCode = 500; | 
					
						
							|  |  |  |       res.setHeader('Content-Type', 'application/json'); | 
					
						
							|  |  |  |       res.end(JSON.stringify({ | 
					
						
							|  |  |  |         error: { message: err.message, code: err.code } | 
					
						
							|  |  |  |       })); | 
					
						
							| 
									
										
										
										
											2018-10-15 21:02:57 -06:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-02-28 00:00:23 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     function enable(req, res) { | 
					
						
							|  |  |  |       delete state.config.disable;// = undefined;
 | 
					
						
							|  |  |  |       state.keepAlive.state = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       fs.writeFile(confpath, YAML.safeDump(snakeCopy(state.config)), function (err) { | 
					
						
							|  |  |  |         if (err) { | 
					
						
							|  |  |  |           err.message = "Could not save config file. Perhaps you're user doesn't have permission?"; | 
					
						
							|  |  |  |           handleError(err, req, res); | 
					
						
							|  |  |  |           return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         // TODO XXX myRemote.active
 | 
					
						
							|  |  |  |         if (myRemote) { | 
					
						
							|  |  |  |           listSuccess(req, res); | 
					
						
							|  |  |  |           return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         safeStartTelebitRemote(true).then(listSuccess).catch(function () { | 
					
						
							|  |  |  |           handleError(err, req, res); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2018-10-15 21:02:57 -06:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-02-28 00:00:23 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     function disable(req, res) { | 
					
						
							|  |  |  |       state.config.disable = true; | 
					
						
							|  |  |  |       state.keepAlive.state = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (myRemote) { myRemote.end(); myRemote = null; } | 
					
						
							|  |  |  |       fs.writeFile(confpath, YAML.safeDump(snakeCopy(state.config)), function (err) { | 
					
						
							|  |  |  |         res.setHeader('Content-Type', 'application/json'); | 
					
						
							|  |  |  |         if (err) { | 
					
						
							|  |  |  |           err.message = "Could not save config file. Perhaps you're user doesn't have permission?"; | 
					
						
							|  |  |  |           handleError(err); | 
					
						
							|  |  |  |           return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         res.end('{"success":true}'); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2018-10-15 21:02:57 -06:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-02-28 00:00:23 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     function getStatus(req, res) { | 
					
						
							|  |  |  |       var now = Date.now(); | 
					
						
							|  |  |  |       res.setHeader('Content-Type', 'application/json'); | 
					
						
							|  |  |  |       require('../lib/ssh.js').checkSecurity().then(function (ssh) { | 
					
						
							|  |  |  |         res.end(JSON.stringify( | 
					
						
							|  |  |  |           { module: 'status' | 
					
						
							|  |  |  |           , version: pkg.version | 
					
						
							|  |  |  |           , port: (state.config.ipc && state.config.ipc.port || state._ipc.port || undefined) | 
					
						
							|  |  |  |           , enabled: !state.config.disable | 
					
						
							|  |  |  |           , active: !!myRemote | 
					
						
							|  |  |  |           , initialized: (state.config.relay && state.config.token && state.config.agreeTos) ? true : false | 
					
						
							|  |  |  |           , connected: isConnected | 
					
						
							|  |  |  |           //, proctime: Math.round(process.uptime() * 1000)
 | 
					
						
							|  |  |  |           , uptime: now - startTime | 
					
						
							|  |  |  |           , runtime: isConnected && connectTimes.length && (now - connectTimes[0]) || 0 | 
					
						
							|  |  |  |           , reconnects: connectTimes.length | 
					
						
							|  |  |  |           , servernames: state.servernames | 
					
						
							|  |  |  |           , ssh: state.config.sshAuto | 
					
						
							|  |  |  |           , ssh_permit_root_login: ssh.permit_root_login | 
					
						
							|  |  |  |           , ssh_password_authentication: ssh.password_authentication | 
					
						
							|  |  |  |           , ssh_requests_password: ssh.requests_password | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         )); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2018-06-11 12:56:16 -06:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-06-11 14:52:01 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-28 00:00:23 -07:00
										 |  |  |   // TODO turn strings into regexes to match beginnings
 | 
					
						
							|  |  |  |   app.use(/\b(relay)\b/, controllers.relay); | 
					
						
							|  |  |  |   app.get(/\b(config)\b/, getConfigOnly); | 
					
						
							|  |  |  |   app.use(/\b(init|config)\b/, initOrConfig); | 
					
						
							|  |  |  |   app.use(/\b(restart)\b/, restart); | 
					
						
							| 
									
										
										
										
											2018-10-15 21:02:57 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-28 00:00:23 -07:00
										 |  |  |   // Position is important with eggspress
 | 
					
						
							|  |  |  |   // This should stay here, right before the other methods
 | 
					
						
							|  |  |  |   app.use('/', mustHaveValidConfig); | 
					
						
							| 
									
										
										
										
											2018-10-15 21:02:57 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-28 00:00:23 -07:00
										 |  |  |   //
 | 
					
						
							|  |  |  |   // With proper config
 | 
					
						
							|  |  |  |   //
 | 
					
						
							|  |  |  |   app.use(/\b(http)\b/, controllers.http); | 
					
						
							|  |  |  |   app.use(/\b(tcp)\b/, controllers.tcp); | 
					
						
							|  |  |  |   app.use(/\b(save|commit)\b/, saveAndCommit); | 
					
						
							|  |  |  |   app.use(/\b(ssh)\b/, controllers.ssh); | 
					
						
							|  |  |  |   app.use(/\b(enable)\b/, enable); | 
					
						
							|  |  |  |   app.use(/\b(disable)\b/, disable); | 
					
						
							|  |  |  |   app.use(/\b(status)\b/, getStatus); | 
					
						
							|  |  |  |   app.use(/\b(list)\b/, listSuccess); | 
					
						
							|  |  |  |   app.use('/', function (req, res) { | 
					
						
							|  |  |  |     res.setHeader('Content-Type', 'application/json'); | 
					
						
							|  |  |  |     res.end(JSON.stringify({"error":{"message":"unrecognized rpc"}})); | 
					
						
							| 
									
										
										
										
											2018-10-15 21:02:57 -06:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2019-02-28 00:00:23 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return app; | 
					
						
							| 
									
										
										
										
											2018-10-15 21:02:57 -06:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-02-28 00:00:23 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 21:02:57 -06:00
										 |  |  | function serveControlsHelper() { | 
					
						
							| 
									
										
										
										
											2019-02-28 00:00:23 -07:00
										 |  |  |   var app = eggspress(); | 
					
						
							|  |  |  |   var serveStatic = require('serve-static')(path.join(__dirname, '../lib/admin/')); | 
					
						
							|  |  |  |   var apiHandler = handleApi(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   app.use('/rpc/', apiHandler); | 
					
						
							|  |  |  |   app.use('/api/', apiHandler); | 
					
						
							|  |  |  |   app.use('/', serveStatic); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   controlServer = http.createServer(app); | 
					
						
							| 
									
										
										
										
											2018-09-03 22:56:52 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-14 01:26:32 -06:00
										 |  |  |   if (fs.existsSync(state._ipc.path)) { | 
					
						
							|  |  |  |     fs.unlinkSync(state._ipc.path); | 
					
						
							| 
									
										
										
										
											2018-06-11 12:56:16 -06:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-06-11 14:52:01 -06:00
										 |  |  |   // mask is so that processes owned by other users
 | 
					
						
							|  |  |  |   // can speak to this process, which is probably root-owned
 | 
					
						
							|  |  |  |   var oldUmask = process.umask(0x0000); | 
					
						
							| 
									
										
										
										
											2018-07-03 04:37:41 -06:00
										 |  |  |   var serverOpts = { | 
					
						
							|  |  |  |     writableAll: true | 
					
						
							|  |  |  |   , readableAll: true | 
					
						
							|  |  |  |   , exclusive: false | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2018-10-15 20:37:07 -06:00
										 |  |  |   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); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-02 20:30:18 -06:00
										 |  |  |   if ('socket' === state._ipc.type) { | 
					
						
							|  |  |  |     require('mkdirp').sync(path.dirname(state._ipc.path)); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-07-03 04:40:21 -06:00
										 |  |  |   // https://nodejs.org/api/net.html#net_server_listen_options_callback
 | 
					
						
							|  |  |  |   // path is ignore if port is defined
 | 
					
						
							| 
									
										
										
										
											2018-07-03 04:53:24 -06:00
										 |  |  |   // https://git.coolaj86.com/coolaj86/telebit.js/issues/23#issuecomment-326
 | 
					
						
							| 
									
										
										
										
											2018-10-15 20:37:07 -06:00
										 |  |  |   if ('port' === state.config.ipc.type) { | 
					
						
							| 
									
										
										
										
											2018-07-03 04:37:41 -06:00
										 |  |  |     serverOpts.host = 'localhost'; | 
					
						
							| 
									
										
										
										
											2018-10-15 20:37:07 -06:00
										 |  |  |     serverOpts.port = state._ipc.port || 0; | 
					
						
							| 
									
										
										
										
											2018-07-03 04:37:41 -06:00
										 |  |  |   } else { | 
					
						
							|  |  |  |     serverOpts.path = state._ipc.path; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   controlServer.listen(serverOpts, function () { | 
					
						
							| 
									
										
										
										
											2018-06-11 14:52:01 -06:00
										 |  |  |     process.umask(oldUmask); | 
					
						
							| 
									
										
										
										
											2018-06-29 15:53:55 -06:00
										 |  |  |     var address = this.address(); | 
					
						
							|  |  |  |     if (address.port) { | 
					
						
							|  |  |  |       common.setPort(state.config, address.port); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-06-28 20:35:58 -06:00
										 |  |  |     //console.log(this.address());
 | 
					
						
							| 
									
										
										
										
											2018-07-03 04:12:53 -06:00
										 |  |  |     console.info("[info] Listening for commands on", address); | 
					
						
							| 
									
										
										
										
											2018-06-11 14:52:01 -06:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2018-10-15 20:37:07 -06:00
										 |  |  |   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); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2018-06-11 14:52:01 -06:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-06-11 12:56:16 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-16 01:11:02 +00:00
										 |  |  | function serveControls() { | 
					
						
							| 
									
										
										
										
											2018-09-05 01:18:12 -06:00
										 |  |  |   serveControlsHelper(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-28 20:35:58 -06:00
										 |  |  |   if (state.config.disable) { | 
					
						
							|  |  |  |     console.info("[info] starting disabled"); | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  |   // This will remain in a disconnect state and wait for an init
 | 
					
						
							| 
									
										
										
										
											2018-09-03 22:56:52 -06:00
										 |  |  |   if (!(state.config.relay && (state.config.token || state.config.pretoken))) { | 
					
						
							| 
									
										
										
										
											2018-06-28 20:35:58 -06:00
										 |  |  |     console.info("[info] waiting for init/authentication (missing relay and/or token)"); | 
					
						
							| 
									
										
										
										
											2018-09-03 22:56:52 -06:00
										 |  |  |     return; | 
					
						
							| 
									
										
										
										
											2018-06-16 01:11:02 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-06-28 20:35:58 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-03 22:56:52 -06:00
										 |  |  |   console.info("[info] connecting with stored token"); | 
					
						
							| 
									
										
										
										
											2018-09-16 00:16:43 -06:00
										 |  |  |   return safeStartTelebitRemote().catch(function (err) { | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  |     // ignore, it'll keep looping anyway
 | 
					
						
							| 
									
										
										
										
											2018-09-16 00:16:43 -06:00
										 |  |  |     console.warn("[debug] error that (supposedly) shouldn't matter:"); | 
					
						
							|  |  |  |     console.warn(err); | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2018-06-16 01:11:02 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-11 14:52:01 -06:00
										 |  |  | function parseConfig(err, text) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function run() { | 
					
						
							| 
									
										
										
										
											2018-06-14 02:39:34 -06:00
										 |  |  |     if (!state.config) { | 
					
						
							|  |  |  |       state.config = {}; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-06-25 23:37:51 -06:00
										 |  |  |     common._init( | 
					
						
							| 
									
										
										
										
											2018-06-29 04:15:23 -06:00
										 |  |  |       state.config.root || path.join(os.homedir(), '.local/share/telebit') // || path.join(__dirname, '..')
 | 
					
						
							| 
									
										
										
										
											2018-06-28 20:35:58 -06:00
										 |  |  |     , (state.config.root && path.join(state.config.root, 'etc')) || path.resolve(common.DEFAULT_CONFIG_PATH, '..') | 
					
						
							| 
									
										
										
										
											2018-06-25 23:37:51 -06:00
										 |  |  |     ); | 
					
						
							| 
									
										
										
										
											2018-06-14 02:39:34 -06:00
										 |  |  |     state._ipc = common.pipename(state.config, true); | 
					
						
							|  |  |  |     console.info(''); | 
					
						
							|  |  |  |     console.info(verstr.join(' ')); | 
					
						
							|  |  |  |     if (!state.config.sock) { | 
					
						
							|  |  |  |       console.info('(' + state._ipc.comment + ': "' + state._ipc.path + '")'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     console.info(''); | 
					
						
							|  |  |  |     state.token = state.token || state.config.token || token; | 
					
						
							| 
									
										
										
										
											2018-06-28 20:35:58 -06:00
										 |  |  |     state.pretoken = state.pretoken || state.config.pretoken; | 
					
						
							| 
									
										
										
										
											2018-06-14 02:39:34 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-11 14:52:01 -06:00
										 |  |  |     state._confpath = confpath; | 
					
						
							|  |  |  |     if (!state.config.servernames) { | 
					
						
							|  |  |  |       state.config.servernames = {}; | 
					
						
							| 
									
										
										
										
											2018-06-11 12:56:16 -06:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-06-11 14:52:01 -06:00
										 |  |  |     if (!state.config.ports) { | 
					
						
							|  |  |  |       state.config.ports = {}; | 
					
						
							| 
									
										
										
										
											2018-06-11 12:56:16 -06:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-06-11 14:52:01 -06:00
										 |  |  |     state.servernames = JSON.parse(JSON.stringify(state.config.servernames)); | 
					
						
							|  |  |  |     state.ports = JSON.parse(JSON.stringify(state.config.ports)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     serveControls(); | 
					
						
							| 
									
										
										
										
											2018-06-11 12:56:16 -06:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-11 14:52:01 -06:00
										 |  |  |   try { | 
					
						
							| 
									
										
										
										
											2018-06-14 02:39:34 -06:00
										 |  |  |     state.config = JSON.parse(text || '{}'); | 
					
						
							| 
									
										
										
										
											2018-06-11 14:52:01 -06:00
										 |  |  |   } catch(e1) { | 
					
						
							|  |  |  |     try { | 
					
						
							| 
									
										
										
										
											2018-06-14 02:39:34 -06:00
										 |  |  |       state.config = YAML.safeLoad(text || '{}'); | 
					
						
							| 
									
										
										
										
											2018-06-11 14:52:01 -06:00
										 |  |  |     } catch(e2) { | 
					
						
							|  |  |  |       console.error(e1.message); | 
					
						
							|  |  |  |       console.error(e2.message); | 
					
						
							|  |  |  |       process.exit(1); | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-06-11 12:56:16 -06:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-14 02:39:34 -06:00
										 |  |  |   state.config = camelCopy(state.config || {}) || {}; | 
					
						
							| 
									
										
										
										
											2018-06-11 14:52:01 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |   run(); | 
					
						
							| 
									
										
										
										
											2018-06-14 02:39:34 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if ((err && 'ENOENT' === err.code) || !Object.keys(state.config).length) { | 
					
						
							|  |  |  |     if (!err || 'ENOENT' === err.code) { | 
					
						
							|  |  |  |       console.warn("Empty config file. Run 'telebit init' to configure.\n"); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       console.warn("Couldn't load config:\n\n\t" + err.message + "\n"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-06-11 14:52:01 -06:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-06-11 12:56:16 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-05 01:18:12 -06:00
										 |  |  | function approveDomains(opts, certs, cb) { | 
					
						
							|  |  |  |   // Even though it's being tunneled by a trusted source
 | 
					
						
							|  |  |  |   // we need to make sure we don't get rate-limit spammed
 | 
					
						
							|  |  |  |   // with wildcard domains
 | 
					
						
							|  |  |  |   // TODO: finish implementing dynamic dns for wildcard certs
 | 
					
						
							|  |  |  |   if (getServername(state.servernames, opts.domains[0])) { | 
					
						
							|  |  |  |     opts.email = state.greenlockConf.email || state.config.email; | 
					
						
							|  |  |  |     opts.agreeTos = state.greenlockConf.agree || state.greenlockConf.agreeTos || state.config.agreeTos; | 
					
						
							|  |  |  |     cb(null, { options: opts, certs: certs }); | 
					
						
							| 
									
										
										
										
											2018-06-21 11:01:16 +00:00
										 |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-06-11 12:56:16 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-05 01:18:12 -06:00
										 |  |  |   cb(new Error("servername not found in allowed list")); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-06-21 11:01:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  | function greenlockHelper(state) { | 
					
						
							| 
									
										
										
										
											2018-09-05 01:18:12 -06:00
										 |  |  |   // TODO Check undefined vs false for greenlock config
 | 
					
						
							|  |  |  |   state.greenlockConf = state.config.greenlock || {}; | 
					
						
							|  |  |  |   state.greenlockConfig = { | 
					
						
							|  |  |  |     version: state.greenlockConf.version || 'draft-11' | 
					
						
							|  |  |  |   , server: state.greenlockConf.server || 'https://acme-v02.api.letsencrypt.org/directory' | 
					
						
							|  |  |  |   , communityMember: state.greenlockConf.communityMember || state.config.communityMember | 
					
						
							| 
									
										
										
										
											2018-09-24 19:01:17 -06:00
										 |  |  |   , _communityPackage: 'telebit.js' | 
					
						
							| 
									
										
										
										
											2018-09-05 01:18:12 -06:00
										 |  |  |   , telemetry: state.greenlockConf.telemetry || state.config.telemetry | 
					
						
							|  |  |  |   , configDir: state.greenlockConf.configDir | 
					
						
							|  |  |  |       || (state.config.root && path.join(state.config.root, 'etc/acme')) | 
					
						
							|  |  |  |       || path.join(os.homedir(), '.config/telebit/acme') | 
					
						
							|  |  |  |   // TODO, store: require(state.greenlockConf.store.name || 'le-store-certbot').create(state.greenlockConf.store.options || {})
 | 
					
						
							|  |  |  |   , approveDomains: approveDomains | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  |   state.insecure = state.config.relay_ignore_invalid_certificates; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-06-11 12:56:16 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  | function promiseTimeout(t) { | 
					
						
							|  |  |  |   return new PromiseA(function (resolve) { | 
					
						
							|  |  |  |     setTimeout(resolve, t); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-06-28 20:35:58 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  | var promiseWss = PromiseA.promisify(function (state, fn) { | 
					
						
							|  |  |  |   return common.api.wss(state, fn); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2018-06-28 20:35:58 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  | var trPromise; | 
					
						
							| 
									
										
										
										
											2018-09-06 03:39:21 -06:00
										 |  |  | function safeStartTelebitRemote(forceOn) { | 
					
						
							|  |  |  |   // whatever is currently going will not restart
 | 
					
						
							|  |  |  |   state.keepAlive.state = false; | 
					
						
							|  |  |  |   if (trPromise && !forceOn) { return trPromise; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // if something is running, this will kill it
 | 
					
						
							|  |  |  |   // (TODO option to use known-good active instead of restarting)
 | 
					
						
							|  |  |  |   // this won't restart either
 | 
					
						
							|  |  |  |   trPromise = rawStartTelebitRemote(state.keepAlive); | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  |   trPromise.then(function () { | 
					
						
							| 
									
										
										
										
											2018-09-16 00:16:43 -06:00
										 |  |  |     console.log("[debug] success on raw start, keepAlive = true"); | 
					
						
							| 
									
										
										
										
											2018-09-12 09:55:07 -06:00
										 |  |  |     state.keepAlive.state = true; | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  |     trPromise = null; | 
					
						
							|  |  |  |   }).catch(function () { | 
					
						
							| 
									
										
										
										
											2018-09-16 00:16:43 -06:00
										 |  |  |     console.log("[debug] failure on raw start, { keepAlive = true }"); | 
					
						
							| 
									
										
										
										
											2018-09-06 03:39:21 -06:00
										 |  |  |     // this will restart
 | 
					
						
							|  |  |  |     state.keepAlive = { state: true }; | 
					
						
							|  |  |  |     trPromise = rawStartTelebitRemote(state.keepAlive); | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  |     trPromise.then(function () { | 
					
						
							| 
									
										
										
										
											2018-09-16 00:16:43 -06:00
										 |  |  |       console.log("[debug] success on 2nd start keepAlive:", state.keepAlive.state); | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  |       trPromise = null; | 
					
						
							|  |  |  |     }).catch(function () { | 
					
						
							| 
									
										
										
										
											2018-09-16 00:16:43 -06:00
										 |  |  |       console.log("[debug] failure on 2nd start. keepAlive", state.keepAlive.state); | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  |       trPromise = null; | 
					
						
							| 
									
										
										
										
											2018-09-03 22:56:52 -06:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  |   }); | 
					
						
							|  |  |  |   return trPromise; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-06 03:39:21 -06:00
										 |  |  | function rawStartTelebitRemote(keepAlive) { | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  |   var err; | 
					
						
							|  |  |  |   var exiting = false; | 
					
						
							|  |  |  |   var localRemote = myRemote; | 
					
						
							|  |  |  |   myRemote = null; | 
					
						
							| 
									
										
										
										
											2018-09-13 23:42:04 -06:00
										 |  |  |   if (localRemote) { /*console.log('DEBUG destroy() existing');*/ localRemote.destroy(); } | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |   function safeReload(delay) { | 
					
						
							|  |  |  |     if (exiting) { | 
					
						
							|  |  |  |       // return a junk promise as the prior call
 | 
					
						
							|  |  |  |       // already passed flow-control to the next promise
 | 
					
						
							|  |  |  |       // (this is a second or delayed error or close event)
 | 
					
						
							|  |  |  |       return PromiseA.resolve(); | 
					
						
							| 
									
										
										
										
											2018-09-05 01:18:12 -06:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  |     exiting = true; | 
					
						
							|  |  |  |     // TODO state.keepAlive?
 | 
					
						
							| 
									
										
										
										
											2018-09-06 03:39:21 -06:00
										 |  |  |     return promiseTimeout(delay).then(function () { | 
					
						
							|  |  |  |       return rawStartTelebitRemote(keepAlive); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2018-09-05 01:18:12 -06:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  |   if (state.config.disable) { | 
					
						
							| 
									
										
										
										
											2018-09-13 23:42:04 -06:00
										 |  |  |     //console.log('DEBUG disabled or incapable');
 | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  |     err = new Error("connecting is disabled"); | 
					
						
							|  |  |  |     err.code = 'EDISABLED'; | 
					
						
							|  |  |  |     return PromiseA.reject(err); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!(state.config.token || state.config.agreeTos)) { | 
					
						
							| 
									
										
										
										
											2018-09-13 23:42:04 -06:00
										 |  |  |     //console.log('DEBUG Must agreeTos to generate preauth');
 | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  |     err = new Error("Must either supply token (for auth) or agreeTos (for preauth)"); | 
					
						
							|  |  |  |     err.code = 'ENOAGREE'; | 
					
						
							|  |  |  |     return PromiseA.reject(err); | 
					
						
							| 
									
										
										
										
											2018-09-05 01:18:12 -06:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   state.relay = state.config.relay; | 
					
						
							|  |  |  |   if (!state.relay) { | 
					
						
							| 
									
										
										
										
											2018-09-13 23:42:04 -06:00
										 |  |  |     //console.log('DEBUG no relay');
 | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  |     err = new Error("'" + state._confpath + "' is missing 'relay'"); | 
					
						
							|  |  |  |     err.code = 'ENORELAY'; | 
					
						
							|  |  |  |     return PromiseA.reject(err); | 
					
						
							| 
									
										
										
										
											2018-09-05 01:18:12 -06:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  |   // TODO: we need some form of pre-authorization before connecting,
 | 
					
						
							|  |  |  |   // otherwise we'll get disconnected pretty quickly
 | 
					
						
							| 
									
										
										
										
											2018-09-05 01:18:12 -06:00
										 |  |  |   if (!(state.token || state.pretoken)) { | 
					
						
							| 
									
										
										
										
											2018-09-13 23:42:04 -06:00
										 |  |  |     //console.log('DEBUG no token');
 | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  |     err = new Error("no jwt token or preauthorization"); | 
					
						
							|  |  |  |     err.code = 'ENOAUTH'; | 
					
						
							|  |  |  |     return PromiseA.reject(err); | 
					
						
							| 
									
										
										
										
											2018-09-05 01:18:12 -06:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  |   return PromiseA.resolve().then(function () { | 
					
						
							| 
									
										
										
										
											2018-09-13 23:42:04 -06:00
										 |  |  |     //console.log('DEBUG rawStartTelebitRemote');
 | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     function startHelper() { | 
					
						
							| 
									
										
										
										
											2018-09-13 23:42:04 -06:00
										 |  |  |       //console.log('DEBUG startHelper');
 | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  |       greenlockHelper(state); | 
					
						
							|  |  |  |       // Saves the token
 | 
					
						
							|  |  |  |       // state.handlers.access_token({ jwt: token });
 | 
					
						
							|  |  |  |       // Adds the token to the connection
 | 
					
						
							|  |  |  |       // tun.append(token);
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-13 23:42:04 -06:00
										 |  |  |       //console.log("[DEBUG] token", typeof token, token);
 | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  |       //state.sortingHat = state.config.sortingHat;
 | 
					
						
							|  |  |  |       // { relay, config, servernames, ports, sortingHat, net, insecure, token, handlers, greenlockConfig }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       return new PromiseA(function (myResolve, myReject) { | 
					
						
							|  |  |  |         function reject(err) { | 
					
						
							|  |  |  |           if (myReject) { | 
					
						
							|  |  |  |             myReject(err); | 
					
						
							|  |  |  |             myResolve = null; | 
					
						
							|  |  |  |             myReject = null; | 
					
						
							|  |  |  |           } else { | 
					
						
							| 
									
										
										
										
											2018-09-13 23:42:04 -06:00
										 |  |  |             //console.log('DEBUG double rejection');
 | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  |           } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         function resolve(val) { | 
					
						
							| 
									
										
										
										
											2018-09-13 23:42:04 -06:00
										 |  |  |           //console.log('[DEBUG] pre-resolve');
 | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  |           if (myResolve) { | 
					
						
							|  |  |  |             myResolve(val); | 
					
						
							|  |  |  |             myResolve = null; | 
					
						
							|  |  |  |             myReject = null; | 
					
						
							|  |  |  |           } else { | 
					
						
							| 
									
										
										
										
											2018-09-13 23:42:04 -06:00
										 |  |  |             //console.log('DEBUG double resolution');
 | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  |           } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-09-05 01:18:12 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  |         function onConnect() { | 
					
						
							| 
									
										
										
										
											2018-10-22 22:36:46 -06:00
										 |  |  |           isConnected = true; | 
					
						
							| 
									
										
										
										
											2018-10-15 23:08:27 -06:00
										 |  |  |           connectTimes.unshift(Date.now()); | 
					
						
							| 
									
										
										
										
											2018-09-13 23:42:04 -06:00
										 |  |  |           console.info('[connect] relay established'); | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  |           myRemote.removeListener('error', onConnectError); | 
					
						
							| 
									
										
										
										
											2018-09-16 02:43:57 -06:00
										 |  |  |           myRemote.once('error', function (err) { | 
					
						
							|  |  |  |             console.log("[debug] Error after connect."); | 
					
						
							|  |  |  |             console.log(err); | 
					
						
							| 
									
										
										
										
											2018-09-06 03:39:21 -06:00
										 |  |  |             if (!keepAlive.state) { | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  |               reject(err); | 
					
						
							|  |  |  |               return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             retryLoop(); | 
					
						
							|  |  |  |           }); | 
					
						
							|  |  |  |           resolve(myRemote); | 
					
						
							|  |  |  |           return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-09-05 01:18:12 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  |         function onConnectError(err) { | 
					
						
							|  |  |  |           myRemote = null; | 
					
						
							| 
									
										
										
										
											2018-10-22 22:36:46 -06:00
										 |  |  |           isConnected = false; | 
					
						
							| 
									
										
										
										
											2018-09-16 02:43:57 -06:00
										 |  |  |           if (handleError(err, 'onConnectError')) { | 
					
						
							| 
									
										
										
										
											2018-09-06 03:39:21 -06:00
										 |  |  |             if (!keepAlive.state) { | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  |               reject(err); | 
					
						
							|  |  |  |               return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             safeReload(10 * 1000).then(resolve).catch(reject); | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |           } | 
					
						
							| 
									
										
										
										
											2018-09-16 02:43:57 -06:00
										 |  |  |           console.error('[Error] onConnectError: no retry (possibly bad auth):'); | 
					
						
							|  |  |  |           console.error(err); | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  |           reject(err); | 
					
						
							|  |  |  |           return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         function retryLoop() { | 
					
						
							| 
									
										
										
										
											2018-10-22 22:36:46 -06:00
										 |  |  |           isConnected = false; | 
					
						
							| 
									
										
										
										
											2018-09-13 23:42:04 -06:00
										 |  |  |           console.warn('[Warn] disconnected. Will retry?', keepAlive.state); | 
					
						
							| 
									
										
										
										
											2018-09-06 03:39:21 -06:00
										 |  |  |           if (keepAlive.state) { | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  |             safeReload(10 * 1000).then(resolve).catch(reject); | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         myRemote = TelebitRemote.createConnection({ | 
					
						
							|  |  |  |           relay: state.relay | 
					
						
							|  |  |  |         , wss: state.wss | 
					
						
							|  |  |  |         , config: state.config | 
					
						
							|  |  |  |         , otp: state.otp | 
					
						
							|  |  |  |         , sortingHat: state.config.sortingHat | 
					
						
							|  |  |  |         , net: state.net | 
					
						
							|  |  |  |         , insecure: state.insecure | 
					
						
							|  |  |  |         , token: state.token || state.pretoken // instance
 | 
					
						
							|  |  |  |         , servernames: state.servernames | 
					
						
							|  |  |  |         , ports: state.ports | 
					
						
							|  |  |  |         , handlers: state.handlers | 
					
						
							|  |  |  |         , greenlockConfig: state.greenlockConfig | 
					
						
							|  |  |  |         }, onConnect); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         myRemote.once('error', onConnectError); | 
					
						
							|  |  |  |         myRemote.once('close', retryLoop); | 
					
						
							|  |  |  |         myRemote.on('grant', state.handlers.grant); | 
					
						
							|  |  |  |         myRemote.on('access_token', state.handlers.access_token); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2018-09-05 01:18:12 -06:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  |     if (state.wss) { | 
					
						
							|  |  |  |       return startHelper(); | 
					
						
							| 
									
										
										
										
											2018-09-05 01:18:12 -06:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-16 02:43:57 -06:00
										 |  |  |     function handleError(err, prefix) { | 
					
						
							|  |  |  |       // Likely causes:
 | 
					
						
							|  |  |  |       //   * DNS lookup failed (no Internet)
 | 
					
						
							|  |  |  |       //   * Rejected (bad authn)
 | 
					
						
							|  |  |  |       if ('ENOTFOUND' === err.code) { | 
					
						
							|  |  |  |         // DNS issue, probably network is disconnected
 | 
					
						
							|  |  |  |         err.message = [ | 
					
						
							|  |  |  |           '[warn] (' + prefix + '): DNS address not found.' | 
					
						
							|  |  |  |         , '    Either the remote does not exist or local network is down or blocked.' | 
					
						
							|  |  |  |         , '    You might check wifi, eth, paywall, etc.' | 
					
						
							|  |  |  |         ].join('\n'); | 
					
						
							|  |  |  |         if (keepAlive.error !== err.code) { | 
					
						
							|  |  |  |           console.warn(err.message); | 
					
						
							|  |  |  |           keepAlive.error = err.code; | 
					
						
							|  |  |  |           console.warn("(retrying silently)"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |       } else if ('ECONNREFUSED' === err.code) { | 
					
						
							|  |  |  |         // Server issue. If it's the development server, it's probably down
 | 
					
						
							|  |  |  |         err.message = [ | 
					
						
							|  |  |  |           '[warn] onConnectError: Connection Refused.' | 
					
						
							|  |  |  |         , '    Either the remote does not exist or local network is blocking it.' | 
					
						
							|  |  |  |         , '    Is the relay service provider\'s website up? Did you make a typo?' | 
					
						
							|  |  |  |         , '    Is there a local firewall or paywall? Might the relay be otherwise blocked?' | 
					
						
							|  |  |  |         ].join('\n'); | 
					
						
							|  |  |  |         if (keepAlive.error !== err.code) { | 
					
						
							|  |  |  |           console.warn(err.message); | 
					
						
							|  |  |  |           keepAlive.error = err.code; | 
					
						
							|  |  |  |           console.warn("(retrying silently)"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  |     // get the wss url
 | 
					
						
							|  |  |  |     function retryWssLoop(err) { | 
					
						
							| 
									
										
										
										
											2018-09-06 03:39:21 -06:00
										 |  |  |       if (!keepAlive.state) { | 
					
						
							| 
									
										
										
										
											2018-09-16 02:43:57 -06:00
										 |  |  |         console.log("[debug] error getting wss url:"); | 
					
						
							|  |  |  |         console.log(err); | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  |         return PromiseA.reject(err); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2018-09-05 01:18:12 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  |       myRemote = null; | 
					
						
							| 
									
										
										
										
											2018-09-16 02:43:57 -06:00
										 |  |  |       if (handleError(err, 'retryWssLoop')) { | 
					
						
							|  |  |  |         // Always retry at this stage. It *is* a connectivity problem.
 | 
					
						
							|  |  |  |         // Since the internet is disconnected, try again and again and again.
 | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  |         return safeReload(2 * 1000); | 
					
						
							| 
									
										
										
										
											2018-09-16 02:43:57 -06:00
										 |  |  |       } else { | 
					
						
							|  |  |  |         console.error("[error] retryWssLoop (will not retry):"); | 
					
						
							|  |  |  |         console.error(err.message); | 
					
						
							|  |  |  |         return PromiseA.reject(err); | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2018-09-05 01:18:12 -06:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-16 02:43:57 -06:00
										 |  |  |     // It makes since for this to be in here because the server
 | 
					
						
							|  |  |  |     // could be restarting to force a change of the metadata
 | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  |     return promiseWss(state).then(function (wss) { | 
					
						
							|  |  |  |       state.wss = wss; | 
					
						
							| 
									
										
										
										
											2018-09-16 02:43:57 -06:00
										 |  |  |       console.log("[debug] got wss url"); | 
					
						
							|  |  |  |       keepAlive.error = null; | 
					
						
							| 
									
										
										
										
											2018-09-06 03:11:26 -06:00
										 |  |  |       return startHelper(); | 
					
						
							| 
									
										
										
										
											2018-09-16 02:43:57 -06:00
										 |  |  |     }).catch(retryWssLoop); | 
					
						
							| 
									
										
										
										
											2018-06-28 20:35:58 -06:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2018-06-11 12:56:16 -06:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-21 06:10:49 +00:00
										 |  |  | state.handlers = { | 
					
						
							|  |  |  |   grant: function (grants) { | 
					
						
							|  |  |  |     console.info(""); | 
					
						
							|  |  |  |     console.info("Connect to your device by any of the following means:"); | 
					
						
							|  |  |  |     console.info(""); | 
					
						
							|  |  |  |     grants.forEach(function (arr) { | 
					
						
							|  |  |  |       if ('https' === arr[0]) { | 
					
						
							|  |  |  |         if (!state.servernames[arr[1]]) { | 
					
						
							|  |  |  |           state.servernames[arr[1]] = {}; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-06-30 17:56:10 -06:00
										 |  |  |         state.servernames[arr[1]].wildcard = true; | 
					
						
							| 
									
										
										
										
											2018-06-21 06:10:49 +00:00
										 |  |  |       } else if ('tcp' === arr[0]) { | 
					
						
							|  |  |  |         if (!state.ports[arr[2]]) { | 
					
						
							|  |  |  |           state.ports[arr[2]] = {}; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if ('ssh+https' === arr[0]) { | 
					
						
							|  |  |  |         console.info("SSH+HTTPS"); | 
					
						
							|  |  |  |       } else if ('ssh' === arr[0]) { | 
					
						
							|  |  |  |         console.info("SSH"); | 
					
						
							|  |  |  |       } else if ('tcp' === arr[0]) { | 
					
						
							|  |  |  |         console.info("TCP"); | 
					
						
							|  |  |  |       } else if ('https' === arr[0]) { | 
					
						
							|  |  |  |         console.info("HTTPS"); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       console.info('\t' + arr[0] + '://' + arr[1] + (arr[2] ? (':' + arr[2]) : '')); | 
					
						
							|  |  |  |       if ('ssh+https' === arr[0]) { | 
					
						
							|  |  |  |         console.info("\tex: ssh -o ProxyCommand='openssl s_client -connect %h:%p -servername %h -quiet' " + arr[1] + " -p 443\n"); | 
					
						
							|  |  |  |       } else if ('ssh' === arr[0]) { | 
					
						
							|  |  |  |         console.info("\tex: ssh " + arr[1] + " -p " + arr[2] + "\n"); | 
					
						
							|  |  |  |       } else if ('tcp' === arr[0]) { | 
					
						
							|  |  |  |         console.info("\tex: netcat " + arr[1] + " " + arr[2] + "\n"); | 
					
						
							|  |  |  |       } else if ('https' === arr[0]) { | 
					
						
							|  |  |  |         console.info("\tex: curl https://" + arr[1] + "\n"); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | , access_token: function (opts) { | 
					
						
							| 
									
										
										
										
											2018-06-29 14:51:56 -06:00
										 |  |  |     if ('undefined' === opts.jwt || !opts.jwt) { | 
					
						
							|  |  |  |       console.error("Granted empty access token... ??"); | 
					
						
							|  |  |  |       console.error(JSON.stringify(opts)); | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     state.token = opts.jwt || opts.access_token; | 
					
						
							|  |  |  |     state.config.token = opts.jwt || opts.access_token; | 
					
						
							| 
									
										
										
										
											2018-06-21 06:10:49 +00:00
										 |  |  |     console.info("Updating '" + tokenpath + "' with new token:"); | 
					
						
							|  |  |  |     try { | 
					
						
							| 
									
										
										
										
											2018-06-29 15:53:55 -06:00
										 |  |  |       fs.writeFileSync(tokenpath, opts.jwt); | 
					
						
							| 
									
										
										
										
											2018-06-29 04:33:22 -06:00
										 |  |  |       fs.writeFileSync(confpath, YAML.safeDump(snakeCopy(state.config))); | 
					
						
							| 
									
										
										
										
											2018-06-21 06:10:49 +00:00
										 |  |  |     } catch (e) { | 
					
						
							|  |  |  |       console.error("Token not saved:"); | 
					
						
							|  |  |  |       console.error(e); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-21 11:01:16 +00:00
										 |  |  | function sigHandler() { | 
					
						
							| 
									
										
										
										
											2018-09-06 03:39:21 -06:00
										 |  |  |   process.removeListener('SIGINT', sigHandler); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-21 11:01:16 +00:00
										 |  |  |   console.info('Received kill signal. Attempting to exit cleanly...'); | 
					
						
							| 
									
										
										
										
											2018-09-06 03:39:21 -06:00
										 |  |  |   state.keepAlive.state = false; | 
					
						
							| 
									
										
										
										
											2018-06-21 11:01:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // We want to handle cleanup properly unless something is broken in our cleanup process
 | 
					
						
							|  |  |  |   // that prevents us from exitting, in which case we want the user to be able to send
 | 
					
						
							|  |  |  |   // the signal again and exit the way it normally would.
 | 
					
						
							| 
									
										
										
										
											2018-09-03 22:56:52 -06:00
										 |  |  |   if (myRemote) { | 
					
						
							|  |  |  |     myRemote.end(); | 
					
						
							| 
									
										
										
										
											2018-09-06 03:39:21 -06:00
										 |  |  |     myRemote = null; | 
					
						
							| 
									
										
										
										
											2018-06-21 11:01:16 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-06-28 01:53:03 -06:00
										 |  |  |   if (controlServer) { | 
					
						
							|  |  |  |     controlServer.close(); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-06-28 20:35:58 -06:00
										 |  |  |   cancelUpdater(); | 
					
						
							| 
									
										
										
										
											2018-06-21 11:01:16 +00:00
										 |  |  | } | 
					
						
							|  |  |  | // reverse 2FA otp
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | process.on('SIGINT', sigHandler); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | state.net = state.net || { | 
					
						
							|  |  |  |   createConnection: function (info, cb) { | 
					
						
							|  |  |  |     // data is the hello packet / first chunk
 | 
					
						
							|  |  |  |     // info = { data, servername, port, host, remoteFamily, remoteAddress, remotePort }
 | 
					
						
							|  |  |  |     var net = require('net'); | 
					
						
							|  |  |  |     // socket = { write, push, end, events: [ 'readable', 'data', 'error', 'end' ] };
 | 
					
						
							|  |  |  |     var socket = net.createConnection({ port: info.port, host: info.host }, cb); | 
					
						
							|  |  |  |     return socket; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-29 15:53:55 -06:00
										 |  |  | fs.readFile(confpath, 'utf8', parseConfig); | 
					
						
							| 
									
										
										
										
											2018-06-11 12:56:16 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | }()); |