mirror of
https://github.com/sshuttle/sshuttle.git
synced 2025-01-18 20:08:14 +01:00
Don't bother with a backtrace when we produce certain fatal errors.
We'll introduce a new "Fatal" exception for this purpose, and throw it when we just want to print a user message and abort immediately.
This commit is contained in:
parent
2dd328ada4
commit
81c89ce9be
20
client.py
20
client.py
@ -20,27 +20,31 @@ def iptables_setup(port, subnets):
|
|||||||
['--iptables', str(port)] + subnets_str)
|
['--iptables', str(port)] + subnets_str)
|
||||||
rv = subprocess.call(argv)
|
rv = subprocess.call(argv)
|
||||||
if rv != 0:
|
if rv != 0:
|
||||||
raise Exception('%r returned %d' % (argv, rv))
|
raise Fatal('%r returned %d' % (argv, rv))
|
||||||
|
|
||||||
|
|
||||||
def _main(listener, listenport, use_server, remotename, subnets):
|
def _main(listener, listenport, use_server, remotename, subnets):
|
||||||
handlers = []
|
handlers = []
|
||||||
if use_server:
|
if use_server:
|
||||||
helpers.logprefix = 'c : '
|
if helpers.verbose >= 1:
|
||||||
|
helpers.logprefix = 'c : '
|
||||||
|
else:
|
||||||
|
helpers.logprefix = 'client: '
|
||||||
(serverproc, serversock) = ssh.connect(remotename)
|
(serverproc, serversock) = ssh.connect(remotename)
|
||||||
mux = Mux(serversock, serversock)
|
mux = Mux(serversock, serversock)
|
||||||
handlers.append(mux)
|
handlers.append(mux)
|
||||||
|
|
||||||
expected = 'SSHUTTLE0001'
|
expected = 'SSHUTTLE0001'
|
||||||
initstring = serversock.recv(len(expected))
|
initstring = serversock.recv(len(expected))
|
||||||
if initstring != expected:
|
|
||||||
raise Exception('expected server init string %r; got %r'
|
|
||||||
% (expected, initstring))
|
|
||||||
|
|
||||||
rv = serverproc.poll()
|
rv = serverproc.poll()
|
||||||
if rv:
|
if rv:
|
||||||
raise Exception('server died with error code %d' % rv)
|
raise Fatal('server died with error code %d' % rv)
|
||||||
|
|
||||||
|
if initstring != expected:
|
||||||
|
raise Fatal('expected server init string %r; got %r'
|
||||||
|
% (expected, initstring))
|
||||||
|
|
||||||
# we definitely want to do this *after* starting ssh, or we might end
|
# we definitely want to do this *after* starting ssh, or we might end
|
||||||
# up intercepting the ssh connection!
|
# up intercepting the ssh connection!
|
||||||
iptables_setup(listenport, subnets)
|
iptables_setup(listenport, subnets)
|
||||||
@ -67,7 +71,7 @@ def _main(listener, listenport, use_server, remotename, subnets):
|
|||||||
if use_server:
|
if use_server:
|
||||||
rv = serverproc.poll()
|
rv = serverproc.poll()
|
||||||
if rv:
|
if rv:
|
||||||
raise Exception('server died with error code %d' % rv)
|
raise Fatal('server died with error code %d' % rv)
|
||||||
|
|
||||||
r = set()
|
r = set()
|
||||||
w = set()
|
w = set()
|
||||||
|
@ -15,3 +15,7 @@ def debug1(s):
|
|||||||
def debug2(s):
|
def debug2(s):
|
||||||
if verbose >= 2:
|
if verbose >= 2:
|
||||||
log(s)
|
log(s)
|
||||||
|
|
||||||
|
|
||||||
|
class Fatal(Exception):
|
||||||
|
pass
|
||||||
|
54
main.py
54
main.py
@ -1,6 +1,7 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
import sys, os, re
|
import sys, os, re
|
||||||
import helpers, options, client, server, iptables
|
import helpers, options, client, server, iptables
|
||||||
|
from helpers import *
|
||||||
|
|
||||||
|
|
||||||
# list of:
|
# list of:
|
||||||
@ -10,7 +11,7 @@ def parse_subnets(subnets_str):
|
|||||||
for s in subnets_str:
|
for s in subnets_str:
|
||||||
m = re.match(r'(\d+)(?:\.(\d+)\.(\d+)\.(\d+))?(?:/(\d+))?$', s)
|
m = re.match(r'(\d+)(?:\.(\d+)\.(\d+)\.(\d+))?(?:/(\d+))?$', s)
|
||||||
if not m:
|
if not m:
|
||||||
raise Exception('%r is not a valid IP subnet format' % s)
|
raise Fatal('%r is not a valid IP subnet format' % s)
|
||||||
(a,b,c,d,width) = m.groups()
|
(a,b,c,d,width) = m.groups()
|
||||||
(a,b,c,d) = (int(a or 0), int(b or 0), int(c or 0), int(d or 0))
|
(a,b,c,d) = (int(a or 0), int(b or 0), int(c or 0), int(d or 0))
|
||||||
if width == None:
|
if width == None:
|
||||||
@ -18,9 +19,9 @@ def parse_subnets(subnets_str):
|
|||||||
else:
|
else:
|
||||||
width = int(width)
|
width = int(width)
|
||||||
if a > 255 or b > 255 or c > 255 or d > 255:
|
if a > 255 or b > 255 or c > 255 or d > 255:
|
||||||
raise Exception('%d.%d.%d.%d has numbers > 255' % (a,b,c,d))
|
raise Fatal('%d.%d.%d.%d has numbers > 255' % (a,b,c,d))
|
||||||
if width > 32:
|
if width > 32:
|
||||||
raise Exception('*/%d is greater than the maximum of 32' % width)
|
raise Fatal('*/%d is greater than the maximum of 32' % width)
|
||||||
subnets.append(('%d.%d.%d.%d' % (a,b,c,d), width))
|
subnets.append(('%d.%d.%d.%d' % (a,b,c,d), width))
|
||||||
return subnets
|
return subnets
|
||||||
|
|
||||||
@ -30,14 +31,14 @@ def parse_ipport(s):
|
|||||||
s = str(s)
|
s = str(s)
|
||||||
m = re.match(r'(?:(\d+)\.(\d+)\.(\d+)\.(\d+))?(?::)?(?:(\d+))?$', s)
|
m = re.match(r'(?:(\d+)\.(\d+)\.(\d+)\.(\d+))?(?::)?(?:(\d+))?$', s)
|
||||||
if not m:
|
if not m:
|
||||||
raise Exception('%r is not a valid IP:port format' % s)
|
raise Fatal('%r is not a valid IP:port format' % s)
|
||||||
(a,b,c,d,port) = m.groups()
|
(a,b,c,d,port) = m.groups()
|
||||||
(a,b,c,d,port) = (int(a or 0), int(b or 0), int(c or 0), int(d or 0),
|
(a,b,c,d,port) = (int(a or 0), int(b or 0), int(c or 0), int(d or 0),
|
||||||
int(port or 0))
|
int(port or 0))
|
||||||
if a > 255 or b > 255 or c > 255 or d > 255:
|
if a > 255 or b > 255 or c > 255 or d > 255:
|
||||||
raise Exception('%d.%d.%d.%d has numbers > 255' % (a,b,c,d))
|
raise Fatal('%d.%d.%d.%d has numbers > 255' % (a,b,c,d))
|
||||||
if port > 65535:
|
if port > 65535:
|
||||||
raise Exception('*:%d is greater than the maximum of 65535' % port)
|
raise Fatal('*:%d is greater than the maximum of 65535' % port)
|
||||||
if a == None:
|
if a == None:
|
||||||
a = b = c = d = 0
|
a = b = c = d = 0
|
||||||
return ('%d.%d.%d.%d' % (a,b,c,d), port)
|
return ('%d.%d.%d.%d' % (a,b,c,d), port)
|
||||||
@ -60,20 +61,27 @@ o = options.Options('sshuttle', optspec)
|
|||||||
|
|
||||||
helpers.verbose = opt.verbose
|
helpers.verbose = opt.verbose
|
||||||
|
|
||||||
if opt.server:
|
try:
|
||||||
sys.exit(server.main())
|
if opt.server:
|
||||||
elif opt.iptables:
|
sys.exit(server.main())
|
||||||
if len(extra) < 1:
|
elif opt.iptables:
|
||||||
o.fatal('at least one argument expected')
|
if len(extra) < 1:
|
||||||
sys.exit(iptables.main(int(extra[0]),
|
o.fatal('at least one argument expected')
|
||||||
parse_subnets(extra[1:])))
|
sys.exit(iptables.main(int(extra[0]),
|
||||||
else:
|
parse_subnets(extra[1:])))
|
||||||
if len(extra) < 1:
|
else:
|
||||||
o.fatal('at least one subnet expected')
|
if len(extra) < 1:
|
||||||
remotename = opt.remote
|
o.fatal('at least one subnet expected')
|
||||||
if remotename == '' or remotename == '-':
|
remotename = opt.remote
|
||||||
remotename = None
|
if remotename == '' or remotename == '-':
|
||||||
sys.exit(client.main(parse_ipport(opt.listen or '0.0.0.0:0'),
|
remotename = None
|
||||||
not opt.noserver,
|
sys.exit(client.main(parse_ipport(opt.listen or '0.0.0.0:0'),
|
||||||
remotename,
|
not opt.noserver,
|
||||||
parse_subnets(extra)))
|
remotename,
|
||||||
|
parse_subnets(extra)))
|
||||||
|
except Fatal, e:
|
||||||
|
log('fatal: %s\n' % e)
|
||||||
|
sys.exit(99)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
log('\nKeyboard interrupt: exiting.\n')
|
||||||
|
sys.exit(1)
|
||||||
|
@ -8,8 +8,11 @@ def main():
|
|||||||
# synchronization header
|
# synchronization header
|
||||||
sys.stdout.write('SSHUTTLE0001')
|
sys.stdout.write('SSHUTTLE0001')
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
|
|
||||||
helpers.logprefix = ' s: '
|
if helpers.verbose >= 1:
|
||||||
|
helpers.logprefix = ' s: '
|
||||||
|
else:
|
||||||
|
helpers.logprefix = 'server: '
|
||||||
handlers = []
|
handlers = []
|
||||||
mux = Mux(socket.fromfd(sys.stdin.fileno(),
|
mux = Mux(socket.fromfd(sys.stdin.fileno(),
|
||||||
socket.AF_INET, socket.SOCK_STREAM),
|
socket.AF_INET, socket.SOCK_STREAM),
|
||||||
|
5
ssh.py
5
ssh.py
@ -19,10 +19,9 @@ def connect(rhost):
|
|||||||
# stuff here.
|
# stuff here.
|
||||||
escapedir = re.sub(r'([^\w/])', r'\\\\\\\1', nicedir)
|
escapedir = re.sub(r'([^\w/])', r'\\\\\\\1', nicedir)
|
||||||
cmd = r"""
|
cmd = r"""
|
||||||
sh -c PATH=%s:'$PATH exec sshuttle --server'
|
sh -c PATH=%s:'$PATH exec sshuttle --server%s'
|
||||||
""" % (escapedir,)
|
""" % (escapedir, ' -v' * (helpers.verbose or 0))
|
||||||
argv = ['ssh', rhost, '--', cmd.strip()]
|
argv = ['ssh', rhost, '--', cmd.strip()]
|
||||||
print repr(argv)
|
|
||||||
(s1,s2) = socket.socketpair()
|
(s1,s2) = socket.socketpair()
|
||||||
def setup():
|
def setup():
|
||||||
# runs in the child process
|
# runs in the child process
|
||||||
|
Loading…
Reference in New Issue
Block a user