diff options
author | Roger <wenrui@gmail.com> | 2010-12-05 20:05:35 +0800 |
---|---|---|
committer | Roger <wenrui@gmail.com> | 2010-12-12 12:08:54 +0800 |
commit | 2ef3a301fbf04074ab0f679a60db8207e7dcdc89 (patch) | |
tree | 4f0c1c0fe8f6363ef59bfc6d491ef8ca739fa25c | |
parent | 41fd0348eb8f27d9c47d0b51c49fbf92d769ab5b (diff) |
run in background (daemon) and option
-rw-r--r-- | client.py | 49 | ||||
-rw-r--r-- | helpers.py | 12 | ||||
-rwxr-xr-x | main.py | 6 |
3 files changed, 58 insertions, 9 deletions
@@ -4,6 +4,7 @@ import helpers, ssnet, ssh from ssnet import SockWrapper, Handler, Proxy, Mux, MuxWrapper from helpers import * +import os, sys, atexit, signal, syslog def original_dst(sock): try: @@ -97,14 +98,40 @@ class FirewallClient: if rv: raise Fatal('cleanup: %r returned %d' % (self.argv, rv)) +def exit_cleanup(): + debug1('exit cleanup\n') + os.unlink('sshuttle.pid') -def _main(listener, fw, ssh_cmd, remotename, python, seed_hosts, auto_nets): +def _main(listener, fw, ssh_cmd, remotename, python, seed_hosts, auto_nets, background): handlers = [] if helpers.verbose >= 1: helpers.logprefix = 'c : ' else: helpers.logprefix = 'client: ' debug1('connecting to server...\n') + + if background: + helpers.do_syslog = True + syslog.openlog('sshuttle') + + # we're redirecting the standard outputs here early so that + # the stderr debug message of ssh subprocess would be + # redirected properly + + # TODO: redirecting stderr of ssh to syslog + + sys.stdout.flush() + sys.stderr.flush() + si = file('/dev/null', 'r') + so = file('/dev/null', 'a+') + se = file('/dev/null', 'a+', 0) + os.dup2(si.fileno(), sys.stdin.fileno()) + os.dup2(so.fileno(), sys.stdout.fileno()) + os.dup2(se.fileno(), sys.stderr.fileno()) + si.close() + so.close() + se.close() + try: (serverproc, serversock) = ssh.connect(ssh_cmd, remotename, python) except socket.error, e: @@ -126,6 +153,22 @@ def _main(listener, fw, ssh_cmd, remotename, python, seed_hosts, auto_nets): raise Fatal('expected server init string %r; got %r' % (expected, initstring)) debug1('connected.\n') + if background: + debug1('daemonizing\n') + if os.fork(): + os._exit(0) + os.setsid() + if os.fork(): + os._exit(0) + + outfd = os.open('sshuttle.pid', + os.O_WRONLY | os.O_CREAT | os.O_EXCL) + os.write(outfd, '%i' % os.getpid()) + os.close(outfd) + + atexit.register(exit_cleanup) + # Normal exit when killed, or atexit won't work + signal.signal(signal.SIGTERM, lambda signum, stack_frame: sys.exit(1)) def onroutes(routestr): if auto_nets: @@ -182,7 +225,7 @@ def _main(listener, fw, ssh_cmd, remotename, python, seed_hosts, auto_nets): def main(listenip, ssh_cmd, remotename, python, seed_hosts, auto_nets, - subnets_include, subnets_exclude): + subnets_include, subnets_exclude, background): debug1('Starting sshuttle proxy.\n') listener = socket.socket() listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) @@ -213,6 +256,6 @@ def main(listenip, ssh_cmd, remotename, python, seed_hosts, auto_nets, try: return _main(listener, fw, ssh_cmd, remotename, - python, seed_hosts, auto_nets) + python, seed_hosts, auto_nets, background) finally: fw.done() @@ -1,13 +1,17 @@ -import sys, os +import sys, os, syslog logprefix = '' verbose = 0 +do_syslog = False def log(s): try: - sys.stdout.flush() - sys.stderr.write(logprefix + s) - sys.stderr.flush() + if do_syslog: + syslog.syslog(logprefix + s) + else: + sys.stdout.flush() + sys.stderr.write(logprefix + s) + sys.stderr.flush() except IOError: # this could happen if stderr gets forcibly disconnected, eg. because # our tty closes. That sucks, but it's no reason to abort the program. @@ -45,10 +45,11 @@ def parse_ipport(s): optspec = """ -sshuttle [-l [ip:]port] [-r [username@]sshserver[:port]] <subnets...> +sshuttle [-b] [-l [ip:]port] [-r [username@]sshserver[:port]] <subnets...> sshuttle --firewall <port> <subnets...> sshuttle --server -- +b,background run in background as daemon l,listen= transproxy to this ip address and port number [0.0.0.0:0] H,auto-hosts scan for remote hostnames and update local /etc/hosts N,auto-nets automatically determine subnets to route @@ -104,7 +105,8 @@ try: sh, opt.auto_nets, parse_subnets(includes), - parse_subnets(excludes))) + parse_subnets(excludes), + opt.background)) except Fatal, e: log('fatal: %s\n' % e) sys.exit(99) |