separate remote from daemon
This commit is contained in:
		
							parent
							
								
									a0d7f8c816
								
							
						
					
					
						commit
						f8c8701edc
					
				
							
								
								
									
										25
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										25
									
								
								README.md
									
									
									
									
									
								
							| @ -34,12 +34,12 @@ Examples | ||||
| As a user service | ||||
| 
 | ||||
| ```bash | ||||
| telebitd --config ~/.config/telebit/telebit.yml & | ||||
| telebitd --config ~/.config/telebit/telebitd.yml & | ||||
| ``` | ||||
| 
 | ||||
| As a system service | ||||
| ```bash | ||||
| sudo telebitd --config ~/.config/telebit/telebit.yml | ||||
| sudo telebitd --config ~/.config/telebit/telebitd.yml | ||||
| ``` | ||||
| 
 | ||||
| Example output: | ||||
| @ -101,9 +101,7 @@ curl -fsSL https://get.telebit.cloud/ > get.sh; bash get.sh | ||||
| </small> | ||||
| --> | ||||
| 
 | ||||
| Of course, feel free to inspect the install script before you run it: `curl -fsSL https://get.telebit.cloud` | ||||
| 
 | ||||
| This will | ||||
| What does the installer do? | ||||
| 
 | ||||
|   * install Telebit Remote to `/opt/telebit` | ||||
|   * symlink the executables to `/usr/local/bin` for convenience | ||||
| @ -112,6 +110,11 @@ This will | ||||
|   * create the appropriate system launcher file | ||||
|     * `/etc/systemd/system/telebit.service` | ||||
|     * `/Library/LaunchDaemons/cloud.telebit.remote.plist` | ||||
|   * create local user config | ||||
|     * `~/.config/telebit/telebit.yml` | ||||
|     * `~/.local/share/telebit` | ||||
| 
 | ||||
| Of course, feel free to inspect it before you run it: `curl -fsSL https://get.telebit.cloud` | ||||
| 
 | ||||
| **You can customize the installation**: | ||||
| 
 | ||||
| @ -137,9 +140,11 @@ Windows & Node.js | ||||
| 1. Install [node.js](https://nodejs.org) | ||||
| 2. Open _Node.js_ | ||||
| 2. Run the command `npm install -g telebit` | ||||
| 2. Copy the example conifg to your user folder `.config/telebit/telebit.yml` (such as `/Users/John/.config/telebit/telebit.yml`) | ||||
| 2. Change the edaim address | ||||
| 2. Run `telebit daemon --config /Users/John/.config/telebit/telebit.yml` | ||||
| 2. Copy the example daemon conifg to your user folder `.config/telebit/telebitd.yml` (such as `/Users/John/.config/telebit/telebitd.yml`) | ||||
| 2. Copy the example remote conifg to your user folder `.config/telebit/telebit.yml` (such as `/Users/John/.config/telebit/telebit.yml`) | ||||
| 2. Change the email address | ||||
| 2. Run `telebitd` | ||||
| 2. Run `telebit list` | ||||
| 
 | ||||
| **Note**: Use node.js v8.x or v10.x | ||||
| 
 | ||||
| @ -217,12 +222,12 @@ Daemon Usage | ||||
| ============ | ||||
| 
 | ||||
| ```bash | ||||
| telebit daemon --config /opt/telebit/etc/telebit.yml | ||||
| telebitd --config /opt/telebit/etc/telebitd.yml | ||||
| ``` | ||||
| 
 | ||||
| Options | ||||
| 
 | ||||
| `/opt/telebit/etc/telebit.yml:` | ||||
| `/opt/telebit/etc/telebitd.yml:` | ||||
| ``` | ||||
| email: 'jon@example.com'          # must be valid (for certificate recovery and security alerts) | ||||
| agree_tos: true                   # agree to the Telebit, Greenlock, and Let's Encrypt TOSes | ||||
|  | ||||
							
								
								
									
										190
									
								
								bin/telebit.js
									
									
									
									
									
								
							
							
						
						
									
										190
									
								
								bin/telebit.js
									
									
									
									
									
								
							| @ -1,3 +1,191 @@ | ||||
| #!/usr/bin/env node
 | ||||
| (function () { | ||||
| 'use strict'; | ||||
| 
 | ||||
| require('./telebitd.js'); | ||||
| var pkg = require('../package.json'); | ||||
| 
 | ||||
| //var url = require('url');
 | ||||
| var path = require('path'); | ||||
| var http = require('http'); | ||||
| var YAML = require('js-yaml'); | ||||
| var recase = require('recase').create({}); | ||||
| var camelCopy = recase.camelCopy.bind(recase); | ||||
| //var snakeCopy = recase.snakeCopy.bind(recase);
 | ||||
| 
 | ||||
| var common = require('../lib/cli-common.js'); | ||||
| 
 | ||||
| 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]; | ||||
| } | ||||
| 
 | ||||
| function help() { | ||||
|   console.info(''); | ||||
|   console.info('Telebit Remote v' + pkg.version); | ||||
|   console.info(''); | ||||
|   console.info('Usage:'); | ||||
|   console.info(''); | ||||
|   console.info('\ttelebit [--config <path>] <module> <module-option>'); | ||||
|   console.info(''); | ||||
|   console.info('Examples:'); | ||||
|   console.info(''); | ||||
|   console.info('\ttelebit status                          # whether enabled or disabled'); | ||||
|   console.info('\ttelebit enable                          # disallow incoming connections'); | ||||
|   console.info('\ttelebit disable                         # allow incoming connections'); | ||||
|   console.info(''); | ||||
|   console.info('\ttelebit list                            # list rules for servernames and ports'); | ||||
|   console.info(''); | ||||
|   console.info('\ttelebit http none                       # remove all https handlers'); | ||||
|   console.info('\ttelebit http 3000                       # forward all https traffic to port 3000'); | ||||
|   console.info('\ttelebit http /module/path               # load a node module to handle all https traffic'); | ||||
|   console.info(''); | ||||
|   console.info('\ttelebit http none example.com           # remove https handler from example.com'); | ||||
|   console.info('\ttelebit http 3001 example.com           # forward https traffic for example.com to port 3001'); | ||||
|   console.info('\ttelebit http /module/path example.com   # forward https traffic for example.com to port 3001'); | ||||
|   console.info(''); | ||||
|   console.info('\ttelebit tcp none                        # remove all tcp handlers'); | ||||
|   console.info('\ttelebit tcp 5050                        # forward all tcp to port 5050'); | ||||
|   console.info('\ttelebit tcp /module/path                # handle all tcp with a node module'); | ||||
|   console.info(''); | ||||
|   console.info('\ttelebit tcp none 6565                   # remove tcp handler from external port 6565'); | ||||
|   console.info('\ttelebit tcp 5050 6565                   # forward external port 6565 to local 5050'); | ||||
|   console.info('\ttelebit tcp /module/path 6565           # handle external port 6565 with a node module'); | ||||
|   console.info(''); | ||||
|   console.info('Config:'); | ||||
|   console.info(''); | ||||
|   console.info('\tSee https://git.coolaj86.com/coolaj86/telebit.js'); | ||||
|   console.info(''); | ||||
|   console.info(''); | ||||
| } | ||||
| 
 | ||||
| var verstr = '' + pkg.name + ' v' + pkg.version; | ||||
| if (-1 === confIndex) { | ||||
|   confpath = path.join(require('os').homedir(), '.config/telebit/telebit.yml'); | ||||
|   verstr += ' (--config "' + confpath + '")'; | ||||
| } | ||||
| console.info(verstr + '\n'); | ||||
| 
 | ||||
| if (-1 !== argv.indexOf('-h') || -1 !== argv.indexOf('--help')) { | ||||
|   help(); | ||||
|   process.exit(0); | ||||
| } | ||||
| if (!confpath || /^--/.test(confpath)) { | ||||
|   help(); | ||||
|   process.exit(1); | ||||
| } | ||||
| 
 | ||||
| function askForConfig() { | ||||
|   console.log("Please create a config file at '" + confpath + "' or specify --config /path/to/config"); | ||||
| } | ||||
| 
 | ||||
| function parseConfig(err, text) { | ||||
|   var config; | ||||
| 
 | ||||
|   if (err) { | ||||
|     console.error("\nCouldn't load config:\n\n\t" + err.message + "\n"); | ||||
|     if ('ENOENT' === err.code) { | ||||
|       text = 'relay: \'\''; | ||||
|     } | ||||
|     askForConfig(); | ||||
|   } | ||||
| 
 | ||||
|   try { | ||||
|     config = JSON.parse(text); | ||||
|   } catch(e1) { | ||||
|     try { | ||||
|       config = YAML.safeLoad(text); | ||||
|     } catch(e2) { | ||||
|       console.error(e1.message); | ||||
|       console.error(e2.message); | ||||
|       process.exit(1); | ||||
|       return; | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   config = camelCopy(config); | ||||
| 
 | ||||
|   function putConfig(service, args) { | ||||
|     // console.log('got it', service, args);
 | ||||
|     var req = http.get({ | ||||
|       socketPath: common.pipename(config) | ||||
|     , method: 'POST' | ||||
|     , path: '/rpc/' + service + '?_body=' + JSON.stringify(args) | ||||
|     }, function (resp) { | ||||
| 
 | ||||
|       function finish() { | ||||
|         if (200 !== resp.statusCode) { | ||||
|           console.warn("'" + service + "' may have failed." | ||||
|            + " Consider peaking at the logs either with 'journalctl -xeu telebit' or /opt/telebit/var/log/error.log"); | ||||
|           console.warn(resp.statusCode, body); | ||||
|         } else { | ||||
|           if (body) { | ||||
|             console.info('Response'); | ||||
|             console.info(body); | ||||
|           } else { | ||||
|             console.info("👌"); | ||||
|           } | ||||
|         } | ||||
|       } | ||||
| 
 | ||||
|       var body = ''; | ||||
|       if (resp.headers['content-length']) { | ||||
|         resp.on('data', function (chunk) { | ||||
|           body += chunk.toString(); | ||||
|         }); | ||||
|         resp.on('end', function () { | ||||
|           finish(); | ||||
|         }); | ||||
|       } else { | ||||
|         finish(); | ||||
|       } | ||||
|     }); | ||||
|     req.on('error', function (err) { | ||||
|       console.error('Error'); | ||||
|       console.error(err); | ||||
|       return; | ||||
|     }); | ||||
|   } | ||||
| 
 | ||||
|   // Two styles:
 | ||||
|   //     http 3000
 | ||||
|   //     http modulename
 | ||||
|   function makeRpc(key) { | ||||
|     if (key !== argv[0]) { | ||||
|       return false; | ||||
|     } | ||||
|     putConfig(argv[0], argv.slice(1)); | ||||
|     return true; | ||||
|   } | ||||
| 
 | ||||
|   if ([ 'ssh', 'http', 'tcp' ].some(function (key) { | ||||
|     if (key !== argv[0]) { | ||||
|       return false; | ||||
|     } | ||||
|     if (argv[1]) { | ||||
|       putConfig(argv[0], argv.slice(1)); | ||||
|       return true; | ||||
|     } | ||||
|     help(); | ||||
|     return true; | ||||
|   })) { | ||||
|     return true; | ||||
|   } | ||||
| 
 | ||||
|   if ([ 'status', 'enable', 'disable', 'restart', 'list', 'save' ].some(makeRpc)) { | ||||
|     return; | ||||
|   } | ||||
| 
 | ||||
|   help(); | ||||
| } | ||||
| 
 | ||||
| require('fs').readFile(confpath, 'utf8', parseConfig); | ||||
| 
 | ||||
| }()); | ||||
|  | ||||
							
								
								
									
										417
									
								
								bin/telebitd.js
									
									
									
									
									
								
							
							
						
						
									
										417
									
								
								bin/telebitd.js
									
									
									
									
									
								
							| @ -6,9 +6,14 @@ var pkg = require('../package.json'); | ||||
| 
 | ||||
| var url = require('url'); | ||||
| var path = require('path'); | ||||
| var os = require('os'); | ||||
| var common = require('../lib/cli-common.js'); | ||||
| var http = require('http'); | ||||
| var YAML = require('js-yaml'); | ||||
| var state = { servernames: {}, ports: {} }; | ||||
| var recase = require('recase').create({}); | ||||
| var camelCopy = recase.camelCopy.bind(recase); | ||||
| var snakeCopy = recase.snakeCopy.bind(recase); | ||||
| var state = { homedir: os.homedir(), servernames: {}, ports: {} }; | ||||
| 
 | ||||
| var argv = process.argv.slice(2); | ||||
| 
 | ||||
| @ -25,65 +30,37 @@ if (-1 !== confIndex) { | ||||
| 
 | ||||
| function help() { | ||||
|   console.info(''); | ||||
|   console.info('Telebit Remote v' + pkg.version); | ||||
|   console.info('Telebit Daemon v' + pkg.version); | ||||
|   console.info(''); | ||||
|   console.info('Daemon Usage:'); | ||||
|   console.info('Usage:'); | ||||
|   console.info(''); | ||||
|   console.info('\tsudo telebitd --config <path>'); | ||||
|   console.info('\tex: sudo telebitd --config /opt/telebit/etc/telebit.yml'); | ||||
|   console.info('\ttelebitd --config <path>'); | ||||
|   console.info('\tex: telebitd --config ~/.config/telebit/telebitd.yml'); | ||||
|   console.info(''); | ||||
|   console.info('Remote Usage:'); | ||||
|   console.info(''); | ||||
|   console.info('\ttelebit [--config <path>] <module> <module-option>'); | ||||
|   console.info(''); | ||||
|   console.info('Examples:'); | ||||
|   console.info(''); | ||||
|   console.info('\ttelebit status                          # whether enabled or disabled'); | ||||
|   console.info('\ttelebit enable                          # disallow incoming connections'); | ||||
|   console.info('\ttelebit disable                         # allow incoming connections'); | ||||
|   console.info(''); | ||||
|   console.info('\ttelebit list                            # list rules for servernames and ports'); | ||||
|   console.info(''); | ||||
|   console.info('\ttelebit http none                       # remove all https handlers'); | ||||
|   console.info('\ttelebit http 3000                       # forward all https traffic to port 3000'); | ||||
|   console.info('\ttelebit http /module/path               # load a node module to handle all https traffic'); | ||||
|   console.info(''); | ||||
|   console.info('\ttelebit http none example.com           # remove https handler from example.com'); | ||||
|   console.info('\ttelebit http 3001 example.com           # forward https traffic for example.com to port 3001'); | ||||
|   console.info('\ttelebit http /module/path example.com   # forward https traffic for example.com to port 3001'); | ||||
|   console.info(''); | ||||
|   console.info('\ttelebit tcp none                        # remove all tcp handlers'); | ||||
|   console.info('\ttelebit tcp 5050                        # forward all tcp to port 5050'); | ||||
|   console.info('\ttelebit tcp /module/path                # handle all tcp with a node module'); | ||||
|   console.info(''); | ||||
|   console.info('\ttelebit tcp none 6565                   # remove tcp handler from external port 6565'); | ||||
|   console.info('\ttelebit tcp 5050 6565                   # forward external port 6565 to local 5050'); | ||||
|   console.info('\ttelebit tcp /module/path 6565           # handle external port 6565 with a node module'); | ||||
|   console.info(''); | ||||
|   console.info('Config:'); | ||||
|   console.info(''); | ||||
|   console.info('\tSee https://git.coolaj86.com/coolaj86/telebit.js'); | ||||
|   console.info(''); | ||||
|   console.info(''); | ||||
|   process.exit(0); | ||||
| } | ||||
| 
 | ||||
| var verstr = '' + pkg.name + ' v' + pkg.version; | ||||
| if (-1 === confIndex) { | ||||
|   confpath = path.join(require('os').homedir(), '.config/telebit/telebit.yml'); | ||||
|   confpath = path.join(state.homedir, '.config/telebit/telebitd.yml'); | ||||
|   verstr += ' (--config "' + confpath + '")'; | ||||
| } | ||||
| console.info(verstr + '\n'); | ||||
| 
 | ||||
| if (-1 !== argv.indexOf('-h') || -1 !== argv.indexOf('--help')) { | ||||
|   help(); | ||||
|   process.exit(0); | ||||
| } | ||||
| if (!confpath || /^--/.test(confpath)) { | ||||
|   help(); | ||||
|   process.exit(1); | ||||
| } | ||||
| var defaultSockname = '/opt/telebit/var/telebit.sock'; | ||||
| var tokenfile = 'access_token.txt'; | ||||
| var tokenpath = path.join(path.dirname(confpath), tokenfile); | ||||
| var tokenpath = path.join(path.dirname(confpath), 'access_token.txt'); | ||||
| var token; | ||||
| try { | ||||
|   token = require('fs').readFileSync(tokenpath, 'ascii').trim(); | ||||
| @ -91,101 +68,10 @@ try { | ||||
|   // ignore
 | ||||
| } | ||||
| var controlServer; | ||||
| require('fs').readFile(confpath, 'utf8', function (err, text) { | ||||
|   var config; | ||||
| 
 | ||||
|   var recase = require('recase').create({}); | ||||
|   var camelCopy = recase.camelCopy.bind(recase); | ||||
|   var snakeCopy = recase.snakeCopy.bind(recase); | ||||
| 
 | ||||
|   if (err) { | ||||
|     console.error("\nCouldn't load config:\n\n\t" + err.message + "\n"); | ||||
|     process.exit(1); | ||||
|     return; | ||||
|   } | ||||
| 
 | ||||
|   try { | ||||
|     config = JSON.parse(text); | ||||
|   } catch(e1) { | ||||
|     try { | ||||
|       config = YAML.safeLoad(text); | ||||
|     } catch(e2) { | ||||
|       console.error(e1.message); | ||||
|       console.error(e2.message); | ||||
|       process.exit(1); | ||||
|       return; | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   state._confpath = confpath; | ||||
|   state.config = camelCopy(config); | ||||
|   if (state.config.token && token) { | ||||
|     console.warn(); | ||||
|     console.warn("Found two tokens:"); | ||||
|     console.warn(); | ||||
|     console.warn("\t1. " + tokenpath); | ||||
|     console.warn("\n2. " + confpath); | ||||
|     console.warn(); | ||||
|     console.warn("Choosing the first."); | ||||
|     console.warn(); | ||||
|   } | ||||
|   state.token = token; | ||||
| 
 | ||||
|   if (!state.config.servernames) { | ||||
|     state.config.servernames = {}; | ||||
|   } | ||||
|   if (!state.config.ports) { | ||||
|     state.config.ports = {}; | ||||
|   } | ||||
|   state.servernames = JSON.parse(JSON.stringify(state.config.servernames)); | ||||
|   state.ports = JSON.parse(JSON.stringify(state.config.ports)); | ||||
| 
 | ||||
|   function putConfig(service, args) { | ||||
|     // console.log('got it', service, args);
 | ||||
|     var http = require('http'); | ||||
|     var req = http.get({ | ||||
|       socketPath: state.config.sock || defaultSockname | ||||
|     , method: 'POST' | ||||
|     , path: '/rpc/' + service + '?_body=' + JSON.stringify(args) | ||||
|     }, function (resp) { | ||||
| 
 | ||||
|       function finish() { | ||||
|         if (200 !== resp.statusCode) { | ||||
|           console.warn("'" + service + "' may have failed." | ||||
|            + " Consider peaking at the logs either with 'journalctl -xeu telebit' or /opt/telebit/var/log/error.log"); | ||||
|           console.warn(resp.statusCode, body); | ||||
|         } else { | ||||
|           if (body) { | ||||
|             console.info('Response'); | ||||
|             console.info(body); | ||||
|           } else { | ||||
|             console.info("👌"); | ||||
|           } | ||||
|         } | ||||
|       } | ||||
| 
 | ||||
|       var body = ''; | ||||
|       if (resp.headers['content-length']) { | ||||
|         resp.on('data', function (chunk) { | ||||
|           body += chunk.toString(); | ||||
|         }); | ||||
|         resp.on('end', function () { | ||||
|           finish(); | ||||
|         }); | ||||
|       } else { | ||||
|         finish(); | ||||
|       } | ||||
|     }); | ||||
|     req.on('error', function (err) { | ||||
|       console.error('Error'); | ||||
|       console.error(err); | ||||
|       return; | ||||
|     }); | ||||
|   } | ||||
| 
 | ||||
|   var tun; | ||||
|   function serveControls() { | ||||
|     if (!state.config.disable) { | ||||
| var tun; | ||||
| function serveControls() { | ||||
|   if (!state.config.disable && state.config.relay && (state.config.token || state.config.agreeTos)) { | ||||
|     tun = rawTunnel(); | ||||
|   } | ||||
|   controlServer = http.createServer(function (req, res) { | ||||
| @ -219,6 +105,15 @@ require('fs').readFile(confpath, 'utf8', function (err, text) { | ||||
|       }); | ||||
|     } | ||||
| 
 | ||||
|     if (!state.config.relay || !state.config.email || !state.config.agreeTos) { | ||||
|       res.statusCode = 400; | ||||
|       res.end('{"error":{"code":"E_CONFIG","message":"Invalid config file. Please run \'telebit init\'"}}'); | ||||
|       return; | ||||
|     } | ||||
| 
 | ||||
|     //
 | ||||
|     // With proper config
 | ||||
|     //
 | ||||
|     if (/http/.test(opts.path)) { | ||||
|       if (!opts.body) { | ||||
|         res.statusCode = 422; | ||||
| @ -339,7 +234,10 @@ require('fs').readFile(confpath, 'utf8', function (err, text) { | ||||
|     } | ||||
| 
 | ||||
|     if (/status/.test(opts.path)) { | ||||
|         res.end('{"status":' + (state.config.disable ? 'disabled' : 'enabled') + '}'); | ||||
|       res.end( | ||||
|         '{"status":' + (state.config.disable ? 'disabled' : 'enabled') | ||||
|       + ',"ready":' + (state.config.relay && (state.config.token || state.config.agreeTos)) ? 'true' : 'false' | ||||
|       + '}'); | ||||
|       return; | ||||
|     } | ||||
| 
 | ||||
| @ -363,14 +261,11 @@ require('fs').readFile(confpath, 'utf8', function (err, text) { | ||||
| 
 | ||||
|     res.end('{"error":{"message":"unrecognized rpc"}}'); | ||||
|   }); | ||||
|     var pipename = (state.config.sock || defaultSockname); | ||||
|   var pipename = common.pipename(state.config); | ||||
|   var fs = require('fs'); | ||||
|   if (fs.existsSync(pipename)) { | ||||
|     fs.unlinkSync(pipename); | ||||
|   } | ||||
|     if (/^win/i.test(require('os').platform())) { | ||||
|       pipename = '\\\\?\\pipe' + pipename.replace(/\//, '\\'); | ||||
|     } | ||||
|   // mask is so that processes owned by other users
 | ||||
|   // can speak to this process, which is probably root-owned
 | ||||
|   var oldUmask = process.umask(0x0000); | ||||
| @ -382,44 +277,60 @@ require('fs').readFile(confpath, 'utf8', function (err, text) { | ||||
|   }, function () { | ||||
|     process.umask(oldUmask); | ||||
|   }); | ||||
|   } | ||||
| } | ||||
| 
 | ||||
|   // Two styles:
 | ||||
|   //     http 3000
 | ||||
|   //     http modulename
 | ||||
|   function makeRpc(key) { | ||||
|     if (key !== argv[0]) { | ||||
|       return false; | ||||
|     } | ||||
|     putConfig(argv[0], argv.slice(1)); | ||||
|     return true; | ||||
|   } | ||||
| function parseConfig(err, text) { | ||||
|   var config; | ||||
| 
 | ||||
|   if ([ 'ssh', 'http', 'tcp' ].some(function (key) { | ||||
|     if (key !== argv[0]) { | ||||
|       return false; | ||||
|   function run() { | ||||
|     state._confpath = confpath; | ||||
|     if (!state.config.servernames) { | ||||
|       state.config.servernames = {}; | ||||
|     } | ||||
|     if (argv[1]) { | ||||
|       putConfig(argv[0], argv.slice(1)); | ||||
|       return true; | ||||
|     } | ||||
|     help(); | ||||
|     return true; | ||||
|   })) { | ||||
|     return true; | ||||
|     if (!state.config.ports) { | ||||
|       state.config.ports = {}; | ||||
|     } | ||||
|     state.servernames = JSON.parse(JSON.stringify(state.config.servernames)); | ||||
|     state.ports = JSON.parse(JSON.stringify(state.config.ports)); | ||||
| 
 | ||||
|   if ([ 'status', 'enable', 'disable', 'restart', 'list', 'save' ].some(makeRpc)) { | ||||
|     return; | ||||
|   } | ||||
| 
 | ||||
|   if (-1 !== argv.indexOf('daemon')) { | ||||
|     serveControls(); | ||||
|   } | ||||
| 
 | ||||
|   if (err) { | ||||
|     console.warn("\nCouldn't load config:\n\n\t" + err.message + "\n"); | ||||
|     state.config = {}; | ||||
|     run(); | ||||
|     return; | ||||
|   } | ||||
| 
 | ||||
|   help(); | ||||
| }); | ||||
|   try { | ||||
|     config = JSON.parse(text); | ||||
|   } catch(e1) { | ||||
|     try { | ||||
|       config = YAML.safeLoad(text); | ||||
|     } catch(e2) { | ||||
|       console.error(e1.message); | ||||
|       console.error(e2.message); | ||||
|       process.exit(1); | ||||
|       return; | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   state.config = camelCopy(config); | ||||
|   if (state.config.token && token) { | ||||
|     console.warn(); | ||||
|     console.warn("Found two tokens:"); | ||||
|     console.warn(); | ||||
|     console.warn("\t1. " + tokenpath); | ||||
|     console.warn("\n2. " + confpath); | ||||
|     console.warn(); | ||||
|     console.warn("Choosing the first."); | ||||
|     console.warn(); | ||||
|   } | ||||
|   state.token = token; | ||||
| 
 | ||||
|   run(); | ||||
| } | ||||
| 
 | ||||
| function connectTunnel() { | ||||
|   function sigHandler() { | ||||
| @ -558,6 +469,10 @@ function connectTunnel() { | ||||
| } | ||||
| 
 | ||||
| function rawTunnel() { | ||||
|   if (state.config.disable || !state.config.relay || !(state.config.token || state.config.agreeTos)) { | ||||
|     return; | ||||
|   } | ||||
| 
 | ||||
|   state.relay = state.config.relay; | ||||
|   if (!state.relay) { | ||||
|     throw new Error("'" + state._confpath + "' is missing 'relay'"); | ||||
| @ -597,176 +512,6 @@ function rawTunnel() { | ||||
|   return connectTunnel(); | ||||
| } | ||||
| 
 | ||||
| /* | ||||
| var domainsMap = {}; | ||||
| var services = {}; | ||||
| 
 | ||||
| function collectDomains(val, memo) { | ||||
|   var vals = val.split(/,/g); | ||||
| 
 | ||||
|   function parseProxy(location) { | ||||
|     // john.example.com
 | ||||
|     // http:john.example.com:3000
 | ||||
|     // http://john.example.com:3000
 | ||||
|     var parts = location.split(':'); | ||||
|     if (1 === parts.length) { | ||||
|       // john.example.com -> :john.example.com:0
 | ||||
|       parts[1] = parts[0]; | ||||
| 
 | ||||
|       parts[0] = ''; | ||||
|       parts[2] = 0; | ||||
|     } | ||||
|     else if (2 === parts.length) { | ||||
|       throw new Error("invalid arguments for --domains, should use the format <domainname> or <scheme>:<domainname>:<local-port>"); | ||||
|     } | ||||
|     if (!parts[1]) { | ||||
|       throw new Error("invalid arguments for --domains, should use the format <domainname> or <scheme>:<domainname>:<local-port>"); | ||||
|     } | ||||
| 
 | ||||
|     parts[0] = parts[0].toLowerCase(); | ||||
|     parts[1] = parts[1].toLowerCase().replace(/(\/\/)?/, ''); | ||||
|     parts[2] = parseInt(parts[2], 10) || 0; | ||||
| 
 | ||||
|     memo.push({ | ||||
|       protocol: parts[0] | ||||
|     , hostname: parts[1] | ||||
|     , port: parts[2] | ||||
|     }); | ||||
|   } | ||||
| 
 | ||||
|   vals.map(function (val) { | ||||
|     return parseProxy(val); | ||||
|   }); | ||||
| 
 | ||||
|   return memo; | ||||
| } | ||||
| function collectProxies(val, memo) { | ||||
|   var vals = val.split(/,/g); | ||||
| 
 | ||||
|   function parseProxy(location) { | ||||
|     // john.example.com
 | ||||
|     // https:3443
 | ||||
|     // http:john.example.com:3000
 | ||||
|     // http://john.example.com:3000
 | ||||
|     var parts = location.split(':'); | ||||
|     var dual = false; | ||||
|     if (1 === parts.length) { | ||||
|       // john.example.com -> :john.example.com:0
 | ||||
|       parts[1] = parts[0]; | ||||
| 
 | ||||
|       parts[0] = ''; | ||||
|       parts[2] = 0; | ||||
| 
 | ||||
|       dual = true; | ||||
|     } | ||||
|     else if (2 === parts.length) { | ||||
|       // https:3443 -> https:*:3443
 | ||||
|       parts[2] = parts[1]; | ||||
| 
 | ||||
|       parts[1] = '*'; | ||||
|     } | ||||
| 
 | ||||
|     parts[0] = parts[0].toLowerCase(); | ||||
|     parts[1] = parts[1].toLowerCase().replace(/(\/\/)?/, '') || '*'; | ||||
|     parts[2] = parseInt(parts[2], 10) || 0; | ||||
|     if (!parts[2]) { | ||||
|       // TODO grab OS list of standard ports?
 | ||||
|       if (!parts[0] || 'http' === parts[0]) { | ||||
|         parts[2] = 80; | ||||
|       } | ||||
|       else if ('https' === parts[0]) { | ||||
|         parts[2] = 443; | ||||
|       } | ||||
|       else { | ||||
|         throw new Error("port must be specified - ex: tls:*:1337"); | ||||
|       } | ||||
|     } | ||||
| 
 | ||||
|     memo.push({ | ||||
|       protocol: parts[0] || 'https' | ||||
|     , hostname: parts[1] | ||||
|     , port: parts[2] || 443 | ||||
|     }); | ||||
| 
 | ||||
|     if (dual) { | ||||
|       memo.push({ | ||||
|         protocol: 'http' | ||||
|       , hostname: parts[1] | ||||
|       , port: 80 | ||||
|       }); | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   vals.map(function (val) { | ||||
|     return parseProxy(val); | ||||
|   }); | ||||
| 
 | ||||
|   return memo; | ||||
| } | ||||
| 
 | ||||
| var program = require('commander'); | ||||
| program | ||||
|   .version(pkg.version) | ||||
|   //.command('jsurl <url>')
 | ||||
|   .arguments('<url>') | ||||
|   .action(function (url) { | ||||
|     program.url = url; | ||||
|   }) | ||||
|   .option('-k --insecure', 'Allow TLS connections to a Telebit Relay without valid certs (rejectUnauthorized: false)') | ||||
|   .option('--locals <LIST>', 'comma separated list of <proto>:<port> to which matching incoming http and https should forward (reverse proxy). Ex: https:8443,smtps:8465', collectProxies, [ ]) // --reverse-proxies
 | ||||
|   .option('--domains <LIST>', 'comma separated list of domain names to set to the tunnel (to capture a specific protocol to a specific local port use the format https:example.com:1337 instead). Ex: example.com,example.net', collectDomains, [ ]) | ||||
|   .option('--device [HOSTNAME]', 'Tunnel all domains associated with this device instead of specific domainnames. Use with --locals <proto>:<port>. Ex: macbook-pro.local (the output of `hostname`)') | ||||
|   .option('--relay <URL>', 'the domain (or ip address) at which you are running Telebit Relay (the proxy)') // --proxy
 | ||||
|   .option('--secret <STRING>', 'the same secret used by the Telebit Relay (used for JWT authentication)') | ||||
|   .option('--token <STRING>', 'a pre-generated token for use with the Telebit Relay (instead of generating one with --secret)') | ||||
|   .option('--agree-tos', 'agree to the Telebit Terms of Service (requires user validation)') | ||||
|   .option('--email <EMAIL>', 'email address (or cloud address) for user validation') | ||||
|   .option('--oauth3-url <URL>', 'Cloud Authentication to use (default: https://oauth3.org)') | ||||
|   .parse(process.argv) | ||||
|   ; | ||||
| 
 | ||||
| 
 | ||||
| program.locals = (program.locals || []).concat(program.domains || []); | ||||
| program.locals.forEach(function (proxy) { | ||||
|   // Create a map from which we can derive a list of all domains we want forwarded to us.
 | ||||
|   if (proxy.hostname && proxy.hostname !== '*') { | ||||
|     domainsMap[proxy.hostname] = true; | ||||
|   } | ||||
| 
 | ||||
|   // Create a map of which port different protocols should be forwarded to, allowing for specific
 | ||||
|   // domains to go to different ports if need be (though that only works for HTTP and HTTPS).
 | ||||
|   if (proxy.protocol && proxy.port) { | ||||
|     services[proxy.protocol] = services[proxy.protocol] || {}; | ||||
| 
 | ||||
|     if (/http/.test(proxy.protocol) && proxy.hostname && proxy.hostname !== '*') { | ||||
|       services[proxy.protocol][proxy.hostname] = proxy.port; | ||||
|     } | ||||
|     else { | ||||
|       if (services[proxy.protocol]['*'] && services[proxy.protocol]['*'] !== proxy.port) { | ||||
|         console.error('cannot forward generic', proxy.protocol, 'traffic to multiple ports'); | ||||
|         process.exit(1); | ||||
|       } | ||||
|       else { | ||||
|         services[proxy.protocol]['*'] = proxy.port; | ||||
|       } | ||||
|     } | ||||
|   } | ||||
| }); | ||||
| 
 | ||||
| if (Object.keys(domainsMap).length === 0) { | ||||
|   console.error('no domains specified'); | ||||
|   process.exit(1); | ||||
|   return; | ||||
| } | ||||
| 
 | ||||
| // Make sure we have generic ports for HTTP and HTTPS
 | ||||
| services.https = services.https || {}; | ||||
| services.https['*'] = services.https['*'] || 8443; | ||||
| 
 | ||||
| services.http = services.http || {}; | ||||
| services.http['*'] = services.http['*'] || services.https['*']; | ||||
| 
 | ||||
| program.services = services; | ||||
| */ | ||||
| require('fs').readFile(confpath, 'utf8', parseConfig); | ||||
| 
 | ||||
| }()); | ||||
|  | ||||
							
								
								
									
										27
									
								
								lib/cli-common.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								lib/cli-common.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,27 @@ | ||||
| 'use strict'; | ||||
| 
 | ||||
| var common = module.exports; | ||||
| 
 | ||||
| var path = require('path'); | ||||
| var mkdirp = require('mkdirp'); | ||||
| var os = require('os'); | ||||
| var homedir = os.homedir(); | ||||
| 
 | ||||
| var localshare = '.local/share/telebit/var'; | ||||
| var localconf = '.config/telebit'; | ||||
| 
 | ||||
| common.pipename = function (config) { | ||||
|   var pipename = (config.sock || common.DEFAULT_SOCK_NAME); | ||||
|   if (/^win/i.test(os.platform())) { | ||||
|     pipename = '\\\\?\\pipe' + pipename.replace(/\//, '\\'); | ||||
|   } | ||||
|   return pipename; | ||||
| }; | ||||
| common.DEFAULT_SOCK_NAME = path.join(homedir, localshare, 'telebit.sock'); | ||||
| 
 | ||||
| try { | ||||
|   mkdirp.sync(path.join(homedir, localshare)); | ||||
|   mkdirp.sync(path.join(homedir, localconf)); | ||||
| } catch(e) { | ||||
|   console.error(e); | ||||
| } | ||||
| @ -1,6 +1,6 @@ | ||||
| { | ||||
|   "name": "telebit", | ||||
|   "version": "0.13.3", | ||||
|   "version": "0.14.0", | ||||
|   "description": "Break out of localhost. Connect to any device from anywhere over any tcp port or securely in a browser. A secure tunnel. A poor man's reverse VPN.", | ||||
|   "main": "lib/remote.js", | ||||
|   "bin": { | ||||
| @ -53,6 +53,7 @@ | ||||
|     "greenlock": "^2.2.19", | ||||
|     "js-yaml": "^3.11.0", | ||||
|     "jsonwebtoken": "^7.1.9", | ||||
|     "mkdirp": "^0.5.1", | ||||
|     "proxy-packer": "^1.4.3", | ||||
|     "recase": "^1.0.4", | ||||
|     "redirect-https": "^1.1.5", | ||||
|  | ||||
| @ -339,7 +339,7 @@ if [ -n "$my_relay" ] && [ "$my_relay" != "telebit.cloud" ]; then | ||||
| fi | ||||
| 
 | ||||
| # TODO don't create this in TMP_PATH if it exists in TELEBIT_PATH | ||||
| my_config="$TELEBIT_PATH/etc/$my_app.yml" | ||||
| my_config="$TELEBIT_PATH/etc/$my_daemon.yml" | ||||
| mkdir -p "$(dirname $my_config)" | ||||
| if [ ! -e "$my_config" ]; then | ||||
| 
 | ||||
| @ -352,6 +352,7 @@ if [ ! -e "$my_config" ]; then | ||||
|     echo "#email: jon@example.com # used for Automated HTTPS and Telebit.Cloud registrations" >> "$my_config" | ||||
|     echo "#agree_tos: true # must be enabled to use Automated HTTPS and Telebit.Cloud" >> "$my_config" | ||||
|   fi | ||||
|   echo "sock: $TELEBIT_PATH/var/telebit.sock" >> "$my_config" | ||||
| 
 | ||||
|   if [ -n "$my_relay" ]; then | ||||
|     echo "relay: $my_relay" >> "$my_config" | ||||
| @ -369,7 +370,7 @@ if [ ! -e "$my_config" ]; then | ||||
|     echo "relay: telebit.cloud # the relay server to use" >> "$my_config" | ||||
|   fi | ||||
|   #echo "dynamic_ports:\n  []" >> "$my_config" | ||||
|   cat $TELEBIT_PATH/usr/share/$my_app.tpl.yml >> "$my_config" | ||||
|   cat $TELEBIT_PATH/usr/share/$my_daemon.tpl.yml >> "$my_config" | ||||
| 
 | ||||
| fi | ||||
| 
 | ||||
| @ -385,6 +386,7 @@ mkdir -p "$(dirname $my_config)" | ||||
| if [ ! -e "$my_config" ]; then | ||||
| 
 | ||||
|   echo "cli: true" >> "$my_config" | ||||
|   echo "sock: $TELEBIT_PATH/var/telebit.sock" >> "$my_config" | ||||
| 
 | ||||
|   if [ -n "$my_email" ]; then | ||||
|     echo "email: $my_email" >> "$my_config" | ||||
| @ -403,9 +405,6 @@ if [ ! -e "$my_config" ]; then | ||||
|   else | ||||
|     echo "relay: telebit.cloud # the relay server to use" >> "$my_config" | ||||
|   fi | ||||
| 
 | ||||
|   cat $TELEBIT_PATH/usr/share/$my_app.tpl.yml >> "$my_config" | ||||
| 
 | ||||
| fi | ||||
| 
 | ||||
| #echo "${sudo_cmde}chown -R $my_user '$TELEBIT_PATH' | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user