new stuff appears to work
This commit is contained in:
		
							parent
							
								
									b97239b252
								
							
						
					
					
						commit
						5178da0330
					
				| @ -27,28 +27,38 @@ function help() { | ||||
|   console.info(''); | ||||
|   console.info('Telebit Remote v' + pkg.version); | ||||
|   console.info(''); | ||||
|   console.info('Usage:'); | ||||
|   console.info('Daemon Usage:'); | ||||
|   console.info(''); | ||||
|   console.info('\tsudo telebit daemon --config <path>'); | ||||
|   console.info('\tex: sudo telebit daemon --config /opt/telebit/etc/telebit.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 --config ~/.config/telebit/telebit.yml status'); | ||||
|   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 status'); | ||||
|   console.info('\ttelebit enable'); | ||||
|   console.info('\ttelebit disable'); | ||||
|   console.info('\ttelebit list                            # list rules for servernames and ports'); | ||||
|   console.info(''); | ||||
|   console.info('\ttelebit list'); | ||||
|   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 3000'); | ||||
|   console.info('\ttelebit tcp 5050'); | ||||
|   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 http default'); | ||||
|   console.info('\ttelebit tcp default'); | ||||
|   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 http /path/to/module'); | ||||
|   console.info('\ttelebit tcp /path/to/module'); | ||||
|   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(''); | ||||
| @ -187,7 +197,7 @@ require('fs').readFile(confpath, 'utf8', function (err, text) { | ||||
|       } | ||||
| 
 | ||||
|       if (/enable/.test(opts.path)) { | ||||
|         state.config.disable = undefined; | ||||
|         delete state.config.disable;// = undefined;
 | ||||
|         if (!tun) { tun = rawTunnel(); } | ||||
|         fs.writeFile(confpath, require('js-yaml').safeDump(snakeCopy(state.config)), function () { | ||||
|           if (err) { | ||||
| @ -269,16 +279,18 @@ require('fs').readFile(confpath, 'utf8', function (err, text) { | ||||
|           return; | ||||
|         } | ||||
| 
 | ||||
|         // portnum
 | ||||
|         if (opts.body[1]) { | ||||
|           if (!state.servernames[opts.body[1]]) { | ||||
|           if (!state.ports[opts.body[1]]) { | ||||
|             res.statusCode = 400; | ||||
|             res.end('{"error":{"message":"bad servername \'' + opts.body[1] + '\'"'); | ||||
|             return; | ||||
|           } | ||||
|           state.servernames[opts.body[1]].handler = opts.body[0]; | ||||
|           // forward-to port-or-module
 | ||||
|           state.ports[opts.body[1]].handler = opts.body[0]; | ||||
|         } else { | ||||
|           Object.keys(state.servernames).forEach(function (key) { | ||||
|             state.servernames[key].handler = opts.body[0]; | ||||
|           Object.keys(state.ports).forEach(function (key) { | ||||
|             state.ports[key].handler = opts.body[0]; | ||||
|           }); | ||||
|         } | ||||
|         res.end('{"success":true}'); | ||||
| @ -332,7 +344,12 @@ require('fs').readFile(confpath, 'utf8', function (err, text) { | ||||
|     return true; | ||||
|   } | ||||
| 
 | ||||
|   serveControls(); | ||||
|   if (-1 !== argv.indexOf('daemon')) { | ||||
|     serveControls(); | ||||
|     return; | ||||
|   } | ||||
| 
 | ||||
|   help(); | ||||
| }); | ||||
| 
 | ||||
| function connectTunnel() { | ||||
|  | ||||
| @ -8,14 +8,33 @@ | ||||
|     <script>document.body.hidden = true;</script> | ||||
| 
 | ||||
|     <h1>Welcome Home <!-- as in 127.0.0.1, y'know ;) --></h1> | ||||
|     <p>Go ahead and bookmark this page. It's yours now.</p> | ||||
| 
 | ||||
|     <h2>You've claimed <span class="js-servername">{{servername}}</span></h2> | ||||
|     <p>Here's same ways you can use it:</p> | ||||
|     <pre><code>telebit http 3000</code></pre> | ||||
|     <div> | ||||
|       <h2>You've claimed <span class="js-servername">{{servername}}</span></h2> | ||||
|       <p>Here's some ways you can use it:</p> | ||||
|       <pre><code> | ||||
| telebit http 3000                # forward all https traffic to localhost:3000 | ||||
| telebit http /path/to/module     # handle incoming https traffic with a node module | ||||
| telebit http none                # remove all https handlers</code></pre> | ||||
|     </div> | ||||
|     <p>You can <em>always</em> use this port for <strong>SSH over HTTPS</strong>, even while you're using it for something else:</p> | ||||
|     <pre><code> | ||||
| ssh -o ProxyCommand='openssl s_client -connect %h:443 -quiet' <span class="js-servername">{{servername}}</span></code></pre> | ||||
| 
 | ||||
| 
 | ||||
|     <div class="js-port" hidden> | ||||
|       <h2>You've claimed port <span class="js-serviceport">{{serviceport}}</span></h2> | ||||
|       <p>Here's some ways you can use it:</p> | ||||
|       <pre><code> | ||||
| telebit tcp 3000                 # forward all tcp traffic to localhost:3000 | ||||
| telebit tcp /path/to/module      # handle incoming tcp traffic with a node module | ||||
| telebit tcp none                 # remove all tcp handlers</code></pre> | ||||
|     </div> | ||||
|     <p>You can <em>always</em> use this port for <strong>SSH</strong>, even while you're using it for something else:</p> | ||||
|     <pre><code> | ||||
| ssh <span class="js-servername">{{servername}}</span> -p <span class="js-serviceport">{{serviceport}}</span></code></pre> | ||||
| 
 | ||||
|     <h2>You've claimed <span class="js-serviceport">{{serviceport}}</span></h2> | ||||
|     <p>Here's same ways you can use it:</p> | ||||
|     <pre><code>#telebit tcp 3000</code></pre> | ||||
| 
 | ||||
|     <script src="js/app.js"></script> | ||||
|   </body> | ||||
|  | ||||
| @ -3,7 +3,8 @@ | ||||
| 
 | ||||
| document.body.hidden = false; | ||||
| 
 | ||||
| var hash = window.location.hash.slice(1); | ||||
| var hash = window.location.hash.substr(1); | ||||
| var query = window.location.search; | ||||
| 
 | ||||
| function parseQuery(search) { | ||||
|     var args = search.substring(1).split('&'); | ||||
| @ -31,6 +32,18 @@ function parseQuery(search) { | ||||
|     return argsParsed; | ||||
| } | ||||
| 
 | ||||
| document.querySelectorAll('.js-servername').forEach(function ($el) { | ||||
|   $el.innerText = window.location.host; | ||||
| }); | ||||
| 
 | ||||
| console.log(parseQuery(hash)); | ||||
| console.log(parseQuery(query)); | ||||
| var port = parseQuery(hash).serviceport || parseQuery(query).serviceport; | ||||
| if (port) { | ||||
|   document.querySelector('.js-port').hidden = false; | ||||
|   document.querySelectorAll('.js-serviceport').forEach(function ($el) { | ||||
|     $el.innerText = port; | ||||
|   }); | ||||
| } | ||||
| 
 | ||||
| }()); | ||||
|  | ||||
| @ -141,7 +141,14 @@ module.exports.assign = function (state, tun, cb) { | ||||
| 
 | ||||
|       cb(null, conn); | ||||
| 
 | ||||
|       other.write("[Telebit Echo Server] v1.0\nPlease edit your config file to port forward somewhere more useful.\n\n"); | ||||
|       other.write("\n" + | ||||
|       [ "[Telebit Echo Server] v1.0" | ||||
|       , "To configure tcp run the following:" | ||||
|       , "\ttelebit tcp <port number or module name>" | ||||
|       , "\tex: telebit tcp 5050" | ||||
|       , "\tex: telebit tcp /path/to/module" | ||||
|       , "\tex: telebit tcp none" | ||||
|       ].join("\n") + "\n\n"); | ||||
|     }); | ||||
|     //if (tun.data) { conn.write(tun.data); }
 | ||||
|     return conn; | ||||
| @ -178,10 +185,50 @@ module.exports.assign = function (state, tun, cb) { | ||||
|     }); | ||||
|   } | ||||
| 
 | ||||
|   function invokeTcpHandler(conf, socket, tun, id, cb) { | ||||
|     var conn; | ||||
|     if (parseInt(conf.handler, 10)) { | ||||
|       conn = getNetConn(conf.handler); | ||||
|       cb(null, conn); | ||||
|       return conn; | ||||
|     } | ||||
| 
 | ||||
|     var handle = tun.port; | ||||
|     var handler; | ||||
|     var handlerpath = conf.handler; | ||||
|     var homedir = os.homedir(); | ||||
|     var localshare = path.join(homedir, '.local/share/telebit/apps'); | ||||
| 
 | ||||
|     if (/^~/.test(handlerpath)) { | ||||
|       handlerpath = path.join(homedir, handlerpath.replace(/^~(\/?)/, '')); | ||||
|     } | ||||
| 
 | ||||
|     try { | ||||
|       handler = require(handlerpath); | ||||
|       console.info("Handling '" + handle + ":" + id + "' with '" + handlerpath + "'"); | ||||
|     } catch(e1) { | ||||
|       try { | ||||
|         handler = require(path.join(localshare, handlerpath)); | ||||
|         console.info("Handling '" + handle + ":" + id + "' with '" + handlerpath + "'"); | ||||
|       } catch(e2) { | ||||
|         console.error("Failed to load '" + handlerpath + "':", e1.message); | ||||
|         console.error("Failed to load '" + path.join(localshare, handlerpath) + "':", e2.message); | ||||
|         console.warn("Using default handler for '" + handle + ":" + id + "'"); | ||||
|         echoTcp(cb); | ||||
|       } | ||||
|     } | ||||
|     var socketPair = require('socket-pair'); | ||||
|     conn = socketPair.create(function (err, other) { | ||||
|       handler(other, tun, id); | ||||
|       cb(null, conn); | ||||
|     }); | ||||
|     return conn; | ||||
|   } | ||||
|   function invokeHandler(conf, tlsSocket, tun, id) { | ||||
|     var conn; | ||||
|     if (parseInt(conf.handler, 10)) { | ||||
|       // TODO http-proxy with proper headers and ws support
 | ||||
|       var conn = getNetConn(conf.handler); | ||||
|       conn = getNetConn(conf.handler); | ||||
|       console.info("Port-Forwarding '" + (tun.name || tun.serviceport) + "' to '" + conf.handler + "'"); | ||||
|       conn.pipe(tlsSocket); | ||||
|       tlsSocket.pipe(conn); | ||||
| @ -190,7 +237,6 @@ module.exports.assign = function (state, tun, cb) { | ||||
|     var handle = tun.name || tun.port; | ||||
|     var handler; | ||||
|     var handlerpath = conf.handler; | ||||
|     var path = require('path'); | ||||
|     var homedir = os.homedir(); | ||||
|     var localshare = path.join(homedir, '.local/share/telebit/apps'); | ||||
| 
 | ||||
| @ -340,16 +386,17 @@ module.exports.assign = function (state, tun, cb) { | ||||
|     trySsh(tun, function (err, conn) { | ||||
|       if (conn) { cb(null, conn); return; } | ||||
|       // TODO add TCP handlers
 | ||||
|       console.log('Using echo server for tcp'); | ||||
|       var conf = state.ports[tun.serviceport]; | ||||
|       if (!conf || !conf.handler || 'none' === conf.handler) { | ||||
|         console.log('Using echo server for tcp'); | ||||
|         echoTcp(cb); | ||||
|         return; | ||||
|       } | ||||
| 
 | ||||
|       var Packer = require('proxy-packer'); | ||||
|       //var addr = Packer.socketToAddr(conn);
 | ||||
|       var id = Packer.addrToId(tun); | ||||
|       invokeHandler(conf, conn, tun, id); | ||||
|       invokeTcpHandler(conf, conn, tun, id, cb); | ||||
|     }); | ||||
|     return; | ||||
|   } | ||||
|  | ||||
| @ -8,6 +8,7 @@ | ||||
| 	<array> | ||||
| 		<string>/opt/telebit/bin/node</string> | ||||
| 		<string>/opt/telebit/bin/telebit.js</string> | ||||
| 		<string>daemon</string> | ||||
| 		<string>--config</string> | ||||
|     <string>/opt/telebit/etc/telebit.yml</string> | ||||
| 	</array> | ||||
|  | ||||
| @ -24,7 +24,7 @@ Group=telebit | ||||
| 
 | ||||
| WorkingDirectory=/opt/telebit | ||||
| # custom directory cannot be set and will be the place where this exists, not the working directory | ||||
| ExecStart=/opt/telebit/bin/node /opt/telebit/bin/telebit.js --config /opt/telebit/etc/telebit.yml | ||||
| ExecStart=/opt/telebit/bin/node /opt/telebit/bin/telebit.js daemon --config /opt/telebit/etc/telebit.yml | ||||
| ExecReload=/bin/kill -USR1 $MAINPID | ||||
| 
 | ||||
| # Limit the number of file descriptors and processes; see `man systemd.exec` for more limit settings. | ||||
|  | ||||
| @ -320,7 +320,7 @@ $sudo_cmd chown -R $my_user "$TELEBIT_PATH" # "/etc/$my_app" | ||||
| 
 | ||||
| # ~/.config/systemd/user/ | ||||
| # %h/.config/telebit/telebit.yml | ||||
| echo "### Adding $my_app is a system service" | ||||
| echo "### Adding $my_app as a system service" | ||||
| # TODO detect with type -p | ||||
| my_system_launcher="" | ||||
| if [ -d "/Library/LaunchDaemons" ]; then | ||||
| @ -406,7 +406,7 @@ elif [ "launchd" == "$my_system_launcher" ]; then | ||||
|   echo "Or disabled the service and start manually:" | ||||
|   echo "" | ||||
|   echo "    ${sudo_cmde}launchctl unload -w $my_root/$my_app_launchd_service" | ||||
|   echo "    $my_app --config $TELEBIT_PATH/etc/$my_app.yml" | ||||
|   echo "    $my_app daemon --config $TELEBIT_PATH/etc/$my_app.yml" | ||||
| 
 | ||||
| else | ||||
| 
 | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user