mirror of
https://github.com/sshuttle/sshuttle.git
synced 2025-01-23 22:38:53 +01:00
Rewrite binding code. DNS port may now be different from TCP port.
This commit is contained in:
parent
e7caae8126
commit
55f86a8b3f
74
client.py
74
client.py
@ -371,7 +371,7 @@ def _main(tcp_listener, fw, ssh_cmd, remotename, python, latency_control,
|
|||||||
mux.callback()
|
mux.callback()
|
||||||
|
|
||||||
|
|
||||||
def main(listenip, ssh_cmd, remotename, python, latency_control, dns,
|
def main(listenip_v4, ssh_cmd, remotename, python, latency_control, dns,
|
||||||
seed_hosts, auto_nets,
|
seed_hosts, auto_nets,
|
||||||
subnets_include, subnets_exclude, syslog, daemon, pidfile):
|
subnets_include, subnets_exclude, syslog, daemon, pidfile):
|
||||||
if syslog:
|
if syslog:
|
||||||
@ -383,44 +383,86 @@ def main(listenip, ssh_cmd, remotename, python, latency_control, dns,
|
|||||||
log("%s\n" % e)
|
log("%s\n" % e)
|
||||||
return 5
|
return 5
|
||||||
debug1('Starting sshuttle proxy.\n')
|
debug1('Starting sshuttle proxy.\n')
|
||||||
|
|
||||||
if listenip[1]:
|
if listenip_v4 and listenip_v4[1]:
|
||||||
ports = [listenip[1]]
|
# if both ports given, no need to search for a spare port
|
||||||
|
ports = [ 0, ]
|
||||||
else:
|
else:
|
||||||
|
# if at least one port missing, we have to search
|
||||||
ports = xrange(12300,9000,-1)
|
ports = xrange(12300,9000,-1)
|
||||||
|
|
||||||
|
# search for free ports and try to bind
|
||||||
last_e = None
|
last_e = None
|
||||||
|
redirectport_v4 = 0
|
||||||
bound = False
|
bound = False
|
||||||
debug2('Binding:')
|
debug2('Binding redirector:')
|
||||||
for port in ports:
|
for port in ports:
|
||||||
debug2(' %d' % port)
|
debug2(' %d' % port)
|
||||||
tcp_listener = MultiListener()
|
tcp_listener = MultiListener()
|
||||||
tcp_listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
tcp_listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||||
dns_listener = MultiListener(socket.SOCK_DGRAM)
|
|
||||||
dns_listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
if listenip_v4 and listenip_v4[1]:
|
||||||
|
lv4 = listenip_v4
|
||||||
|
redirectport_v4 = lv4[1]
|
||||||
|
elif listenip_v4:
|
||||||
|
lv4 = (listenip_v4[0],port)
|
||||||
|
redirectport_v4 = port
|
||||||
|
else:
|
||||||
|
lv4 = None
|
||||||
|
redirectport_v4 = 0
|
||||||
|
|
||||||
try:
|
try:
|
||||||
tcp_listener.bind((listenip[0], port))
|
tcp_listener.bind(lv4)
|
||||||
dns_listener.bind((listenip[0], port))
|
|
||||||
bound = True
|
bound = True
|
||||||
break
|
break
|
||||||
except socket.error, e:
|
except socket.error, e:
|
||||||
last_e = e
|
if e.errno == errno.EADDRINUSE:
|
||||||
|
last_e = e
|
||||||
|
else:
|
||||||
|
raise e
|
||||||
debug2('\n')
|
debug2('\n')
|
||||||
if not bound:
|
if not bound:
|
||||||
assert(last_e)
|
assert(last_e)
|
||||||
raise last_e
|
raise last_e
|
||||||
tcp_listener.listen(10)
|
tcp_listener.listen(10)
|
||||||
listenip = tcp_listener.v4.getsockname()
|
tcp_listener.print_listening("TCP redirector")
|
||||||
debug1('Listening on %r.\n' % (listenip,))
|
|
||||||
|
|
||||||
|
bound = False
|
||||||
if dns:
|
if dns:
|
||||||
dnsip = dns_listener.v4.getsockname()
|
# search for spare port for DNS
|
||||||
debug1('DNS listening on %r.\n' % (dnsip,))
|
debug2('Binding DNS:')
|
||||||
dnsport_v4 = dnsip[1]
|
ports = xrange(12300,9000,-1)
|
||||||
|
for port in ports:
|
||||||
|
debug2(' %d' % port)
|
||||||
|
dns_listener = MultiListener(socket.SOCK_DGRAM)
|
||||||
|
dns_listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||||
|
|
||||||
|
if listenip_v4:
|
||||||
|
lv4 = (listenip_v4[0],port)
|
||||||
|
dnsport_v4 = port
|
||||||
|
else:
|
||||||
|
lv4 = None
|
||||||
|
dnsport_v4 = 0
|
||||||
|
|
||||||
|
try:
|
||||||
|
dns_listener.bind(lv4)
|
||||||
|
bound = True
|
||||||
|
break
|
||||||
|
except socket.error, e:
|
||||||
|
if e.errno == errno.EADDRINUSE:
|
||||||
|
last_e = e
|
||||||
|
else:
|
||||||
|
raise e
|
||||||
|
debug2('\n')
|
||||||
|
dns_listener.print_listening("DNS")
|
||||||
|
if not bound:
|
||||||
|
assert(last_e)
|
||||||
|
raise last_e
|
||||||
else:
|
else:
|
||||||
dnsport_v4 = 0
|
dnsport_v4 = 0
|
||||||
dns_listener = None
|
dns_listener = None
|
||||||
|
|
||||||
fw = FirewallClient(listenip[1], subnets_include, subnets_exclude, dnsport_v4)
|
fw = FirewallClient(redirectport_v4, subnets_include, subnets_exclude, dnsport_v4)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return _main(tcp_listener, fw, ssh_cmd, remotename,
|
return _main(tcp_listener, fw, ssh_cmd, remotename,
|
||||||
|
Loading…
Reference in New Issue
Block a user