Skip to content

Commit 3899e2e

Browse files
committed
dns: Move resolvconf_nameservers() call from firewall.py to client.py
This adds a dns_hosts command-line option, which is passed internally to the firewall, containing a comma-separated list of nameservers to target when creating firewall rules.
1 parent 9ce2fa0 commit 3899e2e

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

client.py

Lines changed: 7 additions & 2 deletions
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)],
@@ -381,11 +384,13 @@ def main(listenip, ssh_cmd, remotename, python, latency_control, dns,
381384
dnsip = dnslistener.getsockname()
382385
debug1('DNS listening on %r.\n' % (dnsip,))
383386
dnsport = dnsip[1]
387+
dns_hosts = resolvconf_nameservers()
384388
else:
385389
dnsport = 0
386390
dnslistener = None
391+
dns_hosts = []
387392

388-
fw = FirewallClient(listenip[1], subnets_include, subnets_exclude, dnsport)
393+
fw = FirewallClient(listenip[1], subnets_include, subnets_exclude, dnsport, dns_hosts)
389394

390395
try:
391396
return _main(listener, fw, ssh_cmd, remotename,

firewall.py

Lines changed: 5 additions & 7 deletions
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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def parse_ipport(s):
6767
V,version print sshuttle's version number
6868
syslog send log messages to syslog (default if you use --daemon)
6969
pidfile= pidfile name (only if using --daemon) [./sshuttle.pid]
70+
dns-hosts= (internal use only)
7071
server (internal use only)
7172
firewall (internal use only)
7273
hostwatch (internal use only)
@@ -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:

0 commit comments

Comments
 (0)