Skip to content

Commit bd20841

Browse files
committed
firewall.py: clean up repeated calls to ssubprocess.call().
And make sshuttle exit with a well-defined exit code (111) if it needs to reboot.
1 parent 4c1a505 commit bd20841

File tree

4 files changed

+24
-19
lines changed

4 files changed

+24
-19
lines changed

client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,9 @@ def sethostip(self, hostname, ip):
171171
def done(self):
172172
self.pfile.close()
173173
rv = self.p.wait()
174-
if rv:
174+
if rv == EXITCODE_NEEDS_REBOOT:
175+
raise FatalNeedsReboot()
176+
elif rv:
175177
raise Fatal('cleanup: %r returned %d' % (self.argv, rv))
176178

177179

firewall.py

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ def nonfatal(func, *args):
2020
log('error: %s\n' % e)
2121

2222

23+
def _call(argv):
24+
debug1('>> %s\n' % ' '.join(argv))
25+
rv = ssubprocess.call(argv)
26+
if rv:
27+
raise Fatal('%r returned %d' % (argv, rv))
28+
return rv
29+
30+
2331
def ipt_chain_exists(name):
2432
argv = ['iptables', '-t', 'nat', '-nL']
2533
p = ssubprocess.Popen(argv, stdout = ssubprocess.PIPE)
@@ -33,10 +41,7 @@ def ipt_chain_exists(name):
3341

3442
def ipt(*args):
3543
argv = ['iptables', '-t', 'nat'] + list(args)
36-
debug1('>> %s\n' % ' '.join(argv))
37-
rv = ssubprocess.call(argv)
38-
if rv:
39-
raise Fatal('%r returned %d' % (argv, rv))
44+
_call(argv)
4045

4146

4247
_no_ttl_module = False
@@ -159,15 +164,9 @@ def _defaults_write_kernel_flags(flags):
159164
flagstr = ' '.join(flags)
160165
argv = ['defaults', 'write', KERNEL_FLAGS_PATH, KERNEL_FLAGS_NAME,
161166
flagstr]
162-
debug1('>> %s\n' % ' '.join(argv))
163-
rv = ssubprocess.call(argv)
164-
if rv:
165-
raise Fatal('%r returned %d' (argv, rv))
167+
_call(argv)
166168
argv = ['plutil', '-convert', 'xml1', KERNEL_FLAGS_PATH + '.plist']
167-
debug1('>> %s\n' % ' '.join(argv))
168-
rv = ssubprocess.call(argv)
169-
if rv:
170-
raise Fatal('%r returned %d' (argv, rv))
169+
_call(argv)
171170

172171

173172

@@ -253,10 +252,7 @@ def _handle_diversion(divertsock, dnsport):
253252

254253
def ipfw(*args):
255254
argv = ['ipfw', '-q'] + list(args)
256-
debug1('>> %s\n' % ' '.join(argv))
257-
rv = ssubprocess.call(argv)
258-
if rv:
259-
raise Fatal('%r returned %d' % (argv, rv))
255+
_call(argv)
260256

261257

262258
def do_ipfw(port, dnsport, subnets):
@@ -296,8 +292,7 @@ def do_ipfw(port, dnsport, subnets):
296292
"to work around a bug in MacOS 10.7 Lion. You will need\n"
297293
"to reboot before it takes effect. You only have to\n"
298294
"do this once.\n\n")
299-
sys.exit(1)
300-
295+
sys.exit(EXITCODE_NEEDS_REBOOT)
301296

302297
ipfw('add', sport, 'check-state', 'ip',
303298
'from', 'any', 'to', 'any')

helpers.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ class Fatal(Exception):
3030
pass
3131

3232

33+
EXITCODE_NEEDS_REBOOT = 111
34+
class FatalNeedsReboot(Fatal):
35+
pass
36+
37+
3338
def list_contains_any(l, sub):
3439
for i in sub:
3540
if i in l:

main.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ def parse_ipport(s):
126126
parse_subnets(includes),
127127
parse_subnets(excludes),
128128
opt.syslog, opt.daemon, opt.pidfile))
129+
except FatalNeedsReboot, e:
130+
log('You must reboot before using sshuttle.\n')
131+
sys.exit(EXITCODE_NEEDS_REBOOT)
129132
except Fatal, e:
130133
log('fatal: %s\n' % e)
131134
sys.exit(99)

0 commit comments

Comments
 (0)