summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRoger <wenrui@gmail.com>2010-12-05 20:05:35 +0800
committerRoger <wenrui@gmail.com>2010-12-12 12:08:54 +0800
commit2ef3a301fbf04074ab0f679a60db8207e7dcdc89 (patch)
tree4f0c1c0fe8f6363ef59bfc6d491ef8ca739fa25c
parent41fd0348eb8f27d9c47d0b51c49fbf92d769ab5b (diff)
run in background (daemon) and option
-rw-r--r--client.py49
-rw-r--r--helpers.py12
-rwxr-xr-xmain.py6
3 files changed, 58 insertions, 9 deletions
diff --git a/client.py b/client.py
index 0301ca0..46f38b5 100644
--- a/client.py
+++ b/client.py
@@ -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()
diff --git a/helpers.py b/helpers.py
index 18871a2..a5b152b 100644
--- a/helpers.py
+++ b/helpers.py
@@ -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.
diff --git a/main.py b/main.py
index eab4e5b..7a7a6a4 100755
--- a/main.py
+++ b/main.py
@@ -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)