Skip to content

Commit 0730639

Browse files
committed
Merge branch 'pull/22'
* Merging pull request apenwarr#22.
2 parents 852c307 + 0cc65cc commit 0730639

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

client.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,20 @@ def original_dst(sock):
9696

9797

9898
class FirewallClient:
99-
def __init__(self, port, subnets_include, subnets_exclude, dnsport):
99+
def __init__(self, port, subnets_include, subnets_exclude, dnsport, dns_hosts):
100100
self.port = port
101101
self.auto_nets = []
102102
self.subnets_include = subnets_include
103103
self.subnets_exclude = subnets_exclude
104104
self.dnsport = dnsport
105+
self.dns_hosts = dns_hosts
105106
argvbase = ([sys.argv[1], sys.argv[0], sys.argv[1]] +
106107
['-v'] * (helpers.verbose or 0) +
107108
['--firewall', str(port), str(dnsport)])
108109
if ssyslog._p:
109110
argvbase += ['--syslog']
111+
if dnsport:
112+
argvbase += ['--dns-hosts', ','.join(dns_hosts)]
110113
argv_tries = [
111114
['sudo', '-p', '[local sudo] Password: '] + argvbase,
112115
['su', '-c', ' '.join(argvbase)],
@@ -337,7 +340,8 @@ def onhostlist(hostlist):
337340
mux.callback()
338341

339342

340-
def main(listenip, ssh_cmd, remotename, python, latency_control, dns,
343+
def main(listenip, ssh_cmd, remotename, python, latency_control,
344+
dns, dns_hosts,
341345
seed_hosts, auto_nets,
342346
subnets_include, subnets_exclude, syslog, daemon, pidfile):
343347
if syslog:
@@ -378,15 +382,18 @@ def main(listenip, ssh_cmd, remotename, python, latency_control, dns,
378382
listenip = listener.getsockname()
379383
debug1('Listening on %r.\n' % (listenip,))
380384

381-
if dns:
385+
if dns or dns_hosts:
382386
dnsip = dnslistener.getsockname()
383387
debug1('DNS listening on %r.\n' % (dnsip,))
384388
dnsport = dnsip[1]
389+
if dns:
390+
dns_hosts += resolvconf_nameservers()
385391
else:
386392
dnsport = 0
387393
dnslistener = None
394+
dns_hosts = []
388395

389-
fw = FirewallClient(listenip[1], subnets_include, subnets_exclude, dnsport)
396+
fw = FirewallClient(listenip[1], subnets_include, subnets_exclude, dnsport, dns_hosts)
390397

391398
try:
392399
return _main(listener, fw, ssh_cmd, remotename,

firewall.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def ipt_ttl(*args):
7070
# multiple copies shouldn't have overlapping subnets, or only the most-
7171
# recently-started one will win (because we use "-I OUTPUT 1" instead of
7272
# "-A OUTPUT").
73-
def do_iptables(port, dnsport, subnets):
73+
def do_iptables(port, dnsport, nslist, subnets):
7474
chain = 'sshuttle-%s' % port
7575

7676
# basic cleanup/setup of chains
@@ -104,7 +104,6 @@ def do_iptables(port, dnsport, subnets):
104104
'--to-ports', str(port))
105105

106106
if dnsport:
107-
nslist = resolvconf_nameservers()
108107
for ip in nslist:
109108
ipt_ttl('-A', chain, '-j', 'REDIRECT',
110109
'--dest', '%s/32' % ip,
@@ -255,7 +254,7 @@ def ipfw(*args):
255254
_call(argv)
256255

257256

258-
def do_ipfw(port, dnsport, subnets):
257+
def do_ipfw(port, dnsport, nslist, subnets):
259258
sport = str(port)
260259
xsport = str(port+1)
261260

@@ -354,7 +353,6 @@ def do_ipfw(port, dnsport, subnets):
354353
IPPROTO_DIVERT)
355354
divertsock.bind(('0.0.0.0', port)) # IP field is ignored
356355

357-
nslist = resolvconf_nameservers()
358356
for ip in nslist:
359357
# relabel and then catch outgoing DNS requests
360358
ipfw('add', sport, 'divert', sport,
@@ -451,7 +449,7 @@ def ip_in_subnets(ip, subnets):
451449
# exit. In case that fails, it's not the end of the world; future runs will
452450
# supercede it in the transproxy list, at least, so the leftover rules
453451
# are hopefully harmless.
454-
def main(port, dnsport, syslog):
452+
def main(port, dnsport, nslist, syslog):
455453
assert(port > 0)
456454
assert(port <= 65535)
457455
assert(dnsport >= 0)
@@ -516,7 +514,7 @@ def main(port, dnsport, syslog):
516514
try:
517515
if line:
518516
debug1('firewall manager: starting transproxy.\n')
519-
do_wait = do_it(port, dnsport, subnets)
517+
do_wait = do_it(port, dnsport, nslist, subnets)
520518
sys.stdout.write('STARTED\n')
521519

522520
try:
@@ -546,5 +544,5 @@ def main(port, dnsport, syslog):
546544
debug1('firewall manager: undoing changes.\n')
547545
except:
548546
pass
549-
do_it(port, 0, [])
547+
do_it(port, 0, [], [])
550548
restore_etc_hosts(port)

main.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def parse_ipport(s):
5454
H,auto-hosts scan for remote hostnames and update local /etc/hosts
5555
N,auto-nets automatically determine subnets to route
5656
dns capture local DNS requests and forward to the remote DNS server
57+
dns-hosts= capture DNS requests to these servers and forward (comma-separated)
5758
python= path to python interpreter on the remote server
5859
r,remote= ssh hostname (and optional username) of remote sshuttle server
5960
x,exclude= exclude this subnet (can be used more than once)
@@ -94,7 +95,9 @@ def parse_ipport(s):
9495
elif opt.firewall:
9596
if len(extra) != 2:
9697
o.fatal('exactly two arguments expected')
97-
sys.exit(firewall.main(int(extra[0]), int(extra[1]), opt.syslog))
98+
port, dnsport = int(extra[0]), int(extra[1])
99+
nslist = re.split(r'[\s,]+', opt.dns_hosts.strip()) if dnsport else []
100+
sys.exit(firewall.main(port, dnsport, nslist, opt.syslog))
98101
elif opt.hostwatch:
99102
sys.exit(hostwatch.hw_main(extra))
100103
else:
@@ -110,6 +113,7 @@ def parse_ipport(s):
110113
remotename = opt.remote
111114
if remotename == '' or remotename == '-':
112115
remotename = None
116+
nslist = re.split(r'[\s,]+', opt.dns_hosts.strip()) if opt.dns_hosts else []
113117
if opt.seed_hosts and not opt.auto_hosts:
114118
o.fatal('--seed-hosts only works if you also use -H')
115119
if opt.seed_hosts:
@@ -124,6 +128,7 @@ def parse_ipport(s):
124128
opt.python,
125129
opt.latency_control,
126130
opt.dns,
131+
nslist,
127132
sh,
128133
opt.auto_nets,
129134
parse_subnets(includes),

0 commit comments

Comments
 (0)