summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorScott Kuhl <kuhl@mtu.edu>2020-12-29 12:58:44 -0500
committerBrian May <brian@linuxpenguins.xyz>2021-01-01 19:32:48 +1100
commit7fc33c00201b483f75d3ca9817001fc095f23d2f (patch)
tree333f7ff3541a5757a3ee426bff23d1a78927187f
parent563f41478a2f5381ca6fa5da57d8dd9927182404 (diff)
Refactor debug, log and Fatal messages.
This commit rewrites the log() function so that it will append a newline at the end of the message if none is present. It doesn't make sense to print a log message without a newline since the next log message (which will write a prefix) expects to be starting at the beginning of a line. Although it isn't strictly necessary, this commit also removes any newlines at the ends of messages. If I missed any, including the newline at the end of the message will continue to work as it did before. Previously, some calls were missing the newline at the end even though including it was necessary for subsequent messages to appear correctly. This code also cleans up some redundant prefixes. The log() method will prepend the prefix and the different processes should set their prefix as soon as they start. Multiline messages are still supported (although the prefix for the additional lines was changed to match the length of the prefix used for the first line).
-rw-r--r--sshuttle/client.py100
-rw-r--r--sshuttle/cmdline.py4
-rw-r--r--sshuttle/firewall.py70
-rw-r--r--sshuttle/helpers.py14
-rw-r--r--sshuttle/hostwatch.py45
-rw-r--r--sshuttle/linux.py16
-rw-r--r--sshuttle/methods/__init__.py8
-rw-r--r--sshuttle/methods/ipfw.py26
-rw-r--r--sshuttle/methods/nat.py2
-rw-r--r--sshuttle/methods/nft.py2
-rw-r--r--sshuttle/methods/pf.py4
-rw-r--r--sshuttle/methods/tproxy.py6
-rw-r--r--sshuttle/sdnotify.py4
-rw-r--r--sshuttle/server.py60
-rw-r--r--sshuttle/ssh.py2
-rw-r--r--sshuttle/ssnet.py54
-rw-r--r--sshuttle/sudoers.py4
-rw-r--r--tests/client/test_helpers.py18
18 files changed, 218 insertions, 221 deletions
diff --git a/sshuttle/client.py b/sshuttle/client.py
index bb9939f..d67d0a6 100644
--- a/sshuttle/client.py
+++ b/sshuttle/client.py
@@ -41,7 +41,7 @@ _extra_fd = os.open(os.devnull, os.O_RDONLY)
def got_signal(signum, frame):
- log('exiting on signal %d\n' % signum)
+ log('exiting on signal %d' % signum)
sys.exit(1)
@@ -57,7 +57,7 @@ def check_daemon(pidfile):
if e.errno == errno.ENOENT:
return # no pidfile, ok
else:
- raise Fatal("c : can't read %s: %s" % (_pidname, e))
+ raise Fatal("can't read %s: %s" % (_pidname, e))
if not oldpid:
os.unlink(_pidname)
return # invalid pidfile, ok
@@ -177,12 +177,12 @@ class MultiListener:
assert(self.bind_called)
if self.v6:
listenip = self.v6.getsockname()
- debug1('%s listening on %r.\n' % (what, listenip))
- debug2('%s listening with %r.\n' % (what, self.v6))
+ debug1('%s listening on %r.' % (what, listenip))
+ debug2('%s listening with %r.' % (what, self.v6))
if self.v4:
listenip = self.v4.getsockname()
- debug1('%s listening on %r.\n' % (what, listenip))
- debug2('%s listening with %r.\n' % (what, self.v4))
+ debug1('%s listening on %r.' % (what, listenip))
+ debug2('%s listening with %r.' % (what, self.v4))
class FirewallClient:
@@ -233,7 +233,7 @@ class FirewallClient:
# No env: Talking to `FirewallClient.start`, which has no i18n.
break
except OSError as e:
- log('Spawning firewall manager: %r\n' % argv)
+ log('Spawning firewall manager: %r' % argv)
raise Fatal(e)
self.argv = argv
s1.close()
@@ -326,23 +326,23 @@ def expire_connections(now, mux):
remove = []
for chan, timeout in dnsreqs.items():
if timeout < now:
- debug3('expiring dnsreqs channel=%d\n' % chan)
+ debug3('expiring dnsreqs channel=%d' % chan)
remove.append(chan)
del mux.channels[chan]
for chan in remove:
del dnsreqs[chan]
- debug3('Remaining DNS requests: %d\n' % len(dnsreqs))
+ debug3('Remaining DNS requests: %d' % len(dnsreqs))
remove = []
for peer, (chan, timeout) in udp_by_src.items():
if timeout < now:
- debug3('expiring UDP channel channel=%d peer=%r\n' % (chan, peer))
+ debug3('expiring UDP channel channel=%d peer=%r' % (chan, peer))
mux.send(chan, ssnet.CMD_UDP_CLOSE, b'')
remove.append(peer)
del mux.channels[chan]
for peer in remove:
del udp_by_src[peer]
- debug3('Remaining UDP channels: %d\n' % len(udp_by_src))
+ debug3('Remaining UDP channels: %d' % len(udp_by_src))
def onaccept_tcp(listener, method, mux, handlers):
@@ -351,7 +351,7 @@ def onaccept_tcp(listener, method, mux, handlers):
sock, srcip = listener.accept()
except socket.error as e:
if e.args[0] in [errno.EMFILE, errno.ENFILE]:
- debug1('Rejected incoming connection: too many open files!\n')
+ debug1('Rejected incoming connection: too many open files!')
# free up an fd so we can eat the connection
os.close(_extra_fd)
try:
@@ -364,15 +364,15 @@ def onaccept_tcp(listener, method, mux, handlers):
raise
dstip = method.get_tcp_dstip(sock)
- debug1('Accept TCP: %s:%r -> %s:%r.\n' % (srcip[0], srcip[1],
- dstip[0], dstip[1]))
+ debug1('Accept TCP: %s:%r -> %s:%r.' % (srcip[0], srcip[1],
+ dstip[0], dstip[1]))
if dstip[1] == sock.getsockname()[1] and islocal(dstip[0], sock.family):
- debug1("-- ignored: that's my address!\n")
+ debug1("-- ignored: that's my address!")
sock.close()
return
chan = mux.next_channel()
if not chan:
- log('warning: too many open channels. Discarded connection.\n')
+ log('warning: too many open channels. Discarded connection.')
sock.close()
return
mux.send(chan, ssnet.CMD_TCP_CONNECT, b'%d,%s,%d' %
@@ -385,7 +385,7 @@ def onaccept_tcp(listener, method, mux, handlers):
def udp_done(chan, data, method, sock, dstip):
(src, srcport, data) = data.split(b",", 2)
srcip = (src, int(srcport))
- debug3('doing send from %r to %r\n' % (srcip, dstip,))
+ debug3('doing send from %r to %r' % (srcip, dstip,))
method.send_udp(sock, srcip, dstip, data)
@@ -395,7 +395,7 @@ def onaccept_udp(listener, method, mux, handlers):
if t is None:
return
srcip, dstip, data = t
- debug1('Accept UDP: %r -> %r.\n' % (srcip, dstip,))
+ debug1('Accept UDP: %r -> %r.' % (srcip, dstip,))
if srcip in udp_by_src:
chan, _ = udp_by_src[srcip]
else:
@@ -412,7 +412,7 @@ def onaccept_udp(listener, method, mux, handlers):
def dns_done(chan, data, method, sock, srcip, dstip, mux):
- debug3('dns_done: channel=%d src=%r dst=%r\n' % (chan, srcip, dstip))
+ debug3('dns_done: channel=%d src=%r dst=%r' % (chan, srcip, dstip))
del mux.channels[chan]
del dnsreqs[chan]
method.send_udp(sock, srcip, dstip, data)
@@ -427,9 +427,9 @@ def ondns(listener, method, mux, handlers):
# dstip is None if we are using a method where we can't determine
# the destination IP of the DNS request that we captured from the client.
if dstip is None:
- debug1('DNS request from %r: %d bytes\n' % (srcip, len(data)))
+ debug1('DNS request from %r: %d bytes' % (srcip, len(data)))
else:
- debug1('DNS request from %r to %r: %d bytes\n' %
+ debug1('DNS request from %r to %r: %d bytes' %
(srcip, dstip, len(data)))
chan = mux.next_channel()
dnsreqs[chan] = now + 30
@@ -445,13 +445,13 @@ def _main(tcp_listener, udp_listener, fw, ssh_cmd, remotename,
to_nameserver):
helpers.logprefix = 'c : '
- debug1('Starting client with Python version %s\n'
+ debug1('Starting client with Python version %s'
% platform.python_version())
method = fw.method
handlers = []
- debug1('Connecting to server...\n')
+ debug1('Connecting to server...')
try:
(serverproc, serversock) = ssh.connect(
@@ -463,7 +463,7 @@ def _main(tcp_listener, udp_listener, fw, ssh_cmd, remotename,
auto_nets=auto_nets))
except socket.error as e:
if e.args[0] == errno.EPIPE:
- raise Fatal("c : failed to establish ssh session (1)")
+ raise Fatal("failed to establish ssh session (1)")
else:
raise
mux = Mux(serversock.makefile("rb"), serversock.makefile("wb"))
@@ -481,22 +481,22 @@ def _main(tcp_listener, udp_listener, fw, ssh_cmd, remotename,
initstring = serversock.recv(len(expected))
except socket.error as e:
if e.args[0] == errno.ECONNRESET:
- raise Fatal("c : failed to establish ssh session (2)")
+ raise Fatal("failed to establish ssh session (2)")
else:
raise
rv = serverproc.poll()
if rv:
- raise Fatal('c : server died with error code %d' % rv)
+ raise Fatal('server died with error code %d' % rv)
if initstring != expected:
- raise Fatal('c : expected server init string %r; got %r'
+ raise Fatal('expected server init string %r; got %r'
% (expected, initstring))
- log('Connected to server.\n')
+ log('Connected to server.')
sys.stdout.flush()
if daemon:
daemonize()
- log('daemonizing (%s).\n' % _pidname)
+ log('daemonizing (%s).' % _pidname)
def onroutes(routestr):
if auto_nets:
@@ -508,11 +508,11 @@ def _main(tcp_listener, udp_listener, fw, ssh_cmd, remotename,
width = int(width)
ip = ip.decode("ASCII")
if family == socket.AF_INET6 and tcp_listener.v6 is None:
- debug2("Ignored auto net %d/%s/%d\n" % (family, ip, width))
+ debug2("Ignored auto net %d/%s/%d" % (family, ip, width))
if family == socket.AF_INET and tcp_listener.v4 is None:
- debug2("Ignored auto net %d/%s/%d\n" % (family, ip, width))
+ debug2("Ignored auto net %d/%s/%d" % (family, ip, width))
else:
- debug2("Adding auto net %d/%s/%d\n" % (family, ip, width))
+ debug2("Adding auto net %d/%s/%d" % (family, ip, width))
fw.auto_nets.append((family, ip, width, 0, 0))
# we definitely want to do this *after* starting ssh, or we might end
@@ -532,7 +532,7 @@ def _main(tcp_listener, udp_listener, fw, ssh_cmd, remotename,
sdnotify.send(sdnotify.ready(), sdnotify.status('Connected'))
def onhostlist(hostlist):
- debug2('got host list: %r\n' % hostlist)
+ debug2('got host list: %r' % hostlist)
for line in hostlist.strip().split():
if line:
name, ip = line.split(b',', 1)
@@ -548,7 +548,7 @@ def _main(tcp_listener, udp_listener, fw, ssh_cmd, remotename,
dns_listener.add_handler(handlers, ondns, method, mux)
if seed_hosts is not None:
- debug1('seed_hosts: %r\n' % seed_hosts)
+ debug1('seed_hosts: %r' % seed_hosts)
mux.send(0, ssnet.CMD_HOST_REQ, str.encode('\n'.join(seed_hosts)))
def check_ssh_alive():
@@ -588,9 +588,9 @@ def main(listenip_v6, listenip_v4,
try:
check_daemon(pidfile)
except Fatal as e:
- log("%s\n" % e)
+ log("%s" % e)
return 5
- debug1('Starting sshuttle proxy (version %s).\n' % __version__)
+ debug1('Starting sshuttle proxy (version %s).' % __version__)
helpers.logprefix = 'c : '
fw = FirewallClient(method_name, sudo_pythonpath)
@@ -643,14 +643,14 @@ def main(listenip_v6, listenip_v4,
# "auto" when listen address is unspecified.
# The user specified address if provided by user
if listenip_v6 is None:
- debug1("IPv6 disabled by --disable-ipv6\n")
+ debug1("IPv6 disabled by --disable-ipv6")
if listenip_v6 == "auto":
if avail.ipv6:
- debug1("IPv6 enabled: Using default IPv6 listen address ::1\n")
+ debug1("IPv6 enabled: Using default IPv6 listen address ::1")
listenip_v6 = ('::1', 0)
else:
debug1("IPv6 disabled since it isn't supported by method "
- "%s.\n" % fw.method.name)
+ "%s." % fw.method.name)
listenip_v6 = None
# Make final decision about enabling IPv6:
@@ -722,9 +722,9 @@ def main(listenip_v6, listenip_v4,
msg += "(available)"
else:
msg += "(not available with %s method)" % fw.method.name
- debug1(msg + "\n")
+ debug1(msg)
- debug1("Method: %s\n" % fw.method.name)
+ debug1("Method: %s" % fw.method.name)
feature_status("IPv4", required.ipv4, avail.ipv4)
feature_status("IPv6", required.ipv6, avail.ipv6)
feature_status("UDP ", required.udp, avail.udp)
@@ -744,20 +744,20 @@ def main(listenip_v6, listenip_v4,
# because we do that below when we have identified the ports to
# listen on.
debug1("Subnets to forward through remote host (type, IP, cidr mask "
- "width, startPort, endPort):\n")
+ "width, startPort, endPort):")
for i in subnets_include:
- debug1(" "+str(i)+"\n")
+ debug1(" "+str(i))
if auto_nets:
debug1("NOTE: Additional subnets to forward may be added below by "
- "--auto-nets.\n")
- debug1("Subnets to exclude from forwarding:\n")
+ "--auto-nets.")
+ debug1("Subnets to exclude from forwarding:")
for i in subnets_exclude:
- debug1(" "+str(i)+"\n")
+ debug1(" "+str(i))
if required.dns:
debug1("DNS requests normally directed at these servers will be "
- "redirected to remote:\n")
+ "redirected to remote:")
for i in nslist:
- debug1(" "+str(i)+"\n")
+ debug1(" "+str(i))
if listenip_v6 and listenip_v6[1] and listenip_v4 and listenip_v4[1]:
# if both ports given, no need to search for a spare port
@@ -775,7 +775,7 @@ def main(listenip_v6, listenip_v4,
redirectport_v4 = 0
bound = False
for port in ports:
- debug2('Trying to bind redirector on port %d\n' % port)
+ debug2('Trying to bind redirector on port %d' % port)
tcp_listener = MultiListener()
if required.udp:
@@ -830,7 +830,7 @@ def main(listenip_v6, listenip_v4,
# search for spare port for DNS
ports = range(12300, 9000, -1)
for port in ports:
- debug2('Trying to bind DNS redirector on port %d\n' % port)
+ debug2('Trying to bind DNS redirector on port %d' % port)
if port in used_ports:
continue
diff --git a/sshuttle/cmdline.py b/sshuttle/cmdline.py
index 0e7beeb..70685c1 100644
--- a/sshuttle/cmdline.py
+++ b/sshuttle/cmdline.py
@@ -117,9 +117,9 @@ def main():
return return_code
except Fatal as e:
- log('fatal: %s\n' % e)
+ log('fatal: %s' % e)
return 99
except KeyboardInterrupt:
log('\n')
- log('Keyboard interrupt: exiting.\n')
+ log('Keyboard interrupt: exiting.')
return 1
diff --git a/sshuttle/firewall.py b/sshuttle/firewall.py
index 5a3b0e7..ce767b1 100644
--- a/sshuttle/firewall.py
+++ b/sshuttle/firewall.py
@@ -51,15 +51,14 @@ def rewrite_etc_hosts(hostmap, port):
def restore_etc_hosts(hostmap, port):
# Only restore if we added hosts to /etc/hosts previously.
if len(hostmap) > 0:
- debug2('undoing /etc/hosts changes.\n')
+ debug2('undoing /etc/hosts changes.')
rewrite_etc_hosts({}, port)
# Isolate function that needs to be replaced for tests
def setup_daemon():
if os.getuid() != 0:
- raise Fatal('fw: '
- 'You must be root (or enable su/sudo) to set the firewall')
+ raise Fatal('You must be root (or enable su/sudo) to set the firewall')
# don't disappear if our controlling terminal or stdout/stderr
# disappears; we still have to clean up.
@@ -99,10 +98,10 @@ def subnet_weight(s):
# supercede it in the transproxy list, at least, so the leftover rules
# are hopefully harmless.
def main(method_name, syslog):
+ helpers.logprefix = 'fw: '
stdin, stdout = setup_daemon()
hostmap = {}
- helpers.logprefix = 'fw: '
- debug1('Starting firewall with Python version %s\n'
+ debug1('Starting firewall with Python version %s'
% platform.python_version())
if method_name == "auto":
@@ -119,7 +118,7 @@ def main(method_name, syslog):
"Check that the appropriate programs are in your "
"PATH." % method_name)
- debug1('ready method name %s.\n' % method.name)
+ debug1('ready method name %s.' % method.name)
stdout.write('READY %s\n' % method.name)
stdout.flush()
@@ -136,14 +135,14 @@ def main(method_name, syslog):
while 1:
line = stdin.readline(128)
if not line:
- raise Fatal('fw: expected route but got %r' % line)
+ raise Fatal('expected route but got %r' % line)
elif line.startswith("NSLIST\n"):
break
try:
(family, width, exclude, ip, fport, lport) = \
line.strip().split(',', 5)
except BaseException:
- raise Fatal('fw: expected route or NSLIST but got %r' % line)
+ raise Fatal('expected route or NSLIST but got %r' % line)
subnets.append((
int(family),
int(width),
@@ -151,31 +150,31 @@ def main(method_name, syslog):
ip,
int(fport),
int(lport)))
- debug2('Got subnets: %r\n' % subnets)
+ debug2('Got subnets: %r' % subnets)
nslist = []
if line != 'NSLIST\n':
- raise Fatal('fw: expected NSLIST but got %r' % line)
+ raise Fatal('expected NSLIST but got %r' % line)
while 1:
line = stdin.readline(128)
if not line:
- raise Fatal('fw: expected nslist but got %r' % line)
+ raise Fatal('expected nslist but got %r' % line)
elif line.startswith("PORTS "):
break
try:
(family, ip) = line.strip().split(',', 1)
except BaseException:
- raise Fatal('fw: expected nslist or PORTS but got %r' % line)
+ raise Fatal('expected nslist or PORTS but got %r' % line)
nslist.append((int(family), ip))
- debug2('Got partial nslist: %r\n' % nslist)
- debug2('Got nslist: %r\n' % nslist)
+ debug2('Got partial nslist: %r' % nslist)
+ debug2('Got nslist: %r' % nslist)
if not line.startswith('PORTS '):
- raise Fatal('fw: expected PORTS but got %r' % line)
+ raise Fatal('expected PORTS but got %r' % line)
_, _, ports = line.partition(" ")
ports = ports.split(",")
if len(ports) != 4:
- raise Fatal('fw: expected 4 ports but got %d' % len(ports))
+ raise Fatal('expected 4 ports but got %d' % len(ports))
port_v6 = int(ports[0])
port_v4 = int(ports[1])
dnsport_v6 = int(ports[2])
@@ -190,21 +189,21 @@ def main(method_name, syslog):
assert(dnsport_v4 >= 0)
assert(dnsport_v4 <= 65535)
- debug2('Got ports: %d,%d,%d,%d\n'
+ debug2('Got ports: %d,%d,%d,%d'
% (port_v6, port_v4, dnsport_v6, dnsport_v4))
line = stdin.readline(128)
if not line:
- raise Fatal('fw: expected GO but got %r' % line)
+ raise Fatal('expected GO but got %r' % line)
elif not line.startswith("GO "):
- raise Fatal('fw: expected GO but got %r' % line)
+ raise Fatal('expected GO but got %r' % line)
_, _, args = line.partition(" ")
udp, user = args.strip().split(" ", 1)
udp = bool(int(udp))
if user == '-':
user = None
- debug2('Got udp: %r, user: %r\n' % (udp, user))
+ debug2('Got udp: %r, user: %r' % (udp, user))
subnets_v6 = [i for i in subnets if i[0] == socket.AF_INET6]
nslist_v6 = [i for i in nslist if i[0] == socket.AF_INET6]
@@ -212,17 +211,17 @@ def main(method_name, syslog):
nslist_v4 = [i for i in nslist if i[0] == socket.AF_INET]
try:
- debug1('setting up.\n')
+ debug1('setting up.')
if subnets_v6 or nslist_v6:
- debug2('setting up IPv6.\n')
+ debug2('setting up IPv6.')
method.setup_firewall(
port_v6, dnsport_v6, nslist_v6,
socket.AF_INET6, subnets_v6, udp,
user)
if subnets_v4 or nslist_v4:
- debug2('setting up IPv4.\n')
+ debug2('setting up IPv4.')
method.setup_firewall(
port_v4, dnsport_v4, nslist_v4,
socket.AF_INET, subnets_v4, udp,
@@ -245,40 +244,38 @@ def main(method_name, syslog):
if line.startswith('HOST '):
(name, ip) = line[5:].strip().split(',', 1)
hostmap[name] = ip
- debug2('setting up /etc/hosts.\n')
+ debug2('setting up /etc/hosts.')
rewrite_etc_hosts(hostmap, port_v6 or port_v4)
elif line:
if not method.firewall_command(line):
- raise Fatal('fw: expected command, got %r' % line)
+ raise Fatal('expected command, got %r' % line)
else:
break
finally:
try:
- debug1('undoing changes.\n')
+ debug1('undoing changes.')
except BaseException:
debug2('An error occurred, ignoring it.')
try:
if subnets_v6 or nslist_v6:
- debug2('undoing IPv6 changes.\n')
+ debug2('undoing IPv6 changes.')
method.restore_firewall(port_v6, socket.AF_INET6, udp, user)
except BaseException:
try:
- debug1("Error trying to undo IPv6 firewall.\n")
- for line in traceback.format_exc().splitlines():
- debug1("---> %s\n" % line)
+ debug1("Error trying to undo IPv6 firewall.")
+ debug1(traceback.format_exc())
except BaseException:
debug2('An error occurred, ignoring it.')
try:
if subnets_v4 or nslist_v4:
- debug2('undoing IPv4 changes.\n')
+ debug2('undoing IPv4 changes.')
method.restore_firewall(port_v4, socket.AF_INET, udp, user)
except BaseException:
try:
- debug1("Error trying to undo IPv4 firewall.\n")
- for line in traceback.format_exc().splitlines():
- debug1("---> %s\n" % line)
+ debug1("Error trying to undo IPv4 firewall.")
+ debug1(traceback.format_exc())
except BaseException:
debug2('An error occurred, ignoring it.')
@@ -287,8 +284,7 @@ def main(method_name, syslog):
restore_etc_hosts(hostmap, port_v6 or port_v4)
except BaseException:
try:
- debug1("Error trying to undo /etc/hosts changes.\n")
- for line in traceback.format_exc().splitlines():
- debug1("---> %s\n" % line)
+ debug1("Error trying to undo /etc/hosts changes.")
+ debug1(traceback.format_exc())
except BaseException:
debug2('An error occurred, ignoring it.')
diff --git a/sshuttle/helpers.py b/sshuttle/helpers.py
index 9f51586..372feb3 100644
--- a/sshuttle/helpers.py
+++ b/sshuttle/helpers.py
@@ -15,12 +15,16 @@ def log(s):
global logprefix
try:
sys.stdout.flush()
+ # Put newline at end of string if line doesn't have one.
+ if not s.endswith("\n"):
+ s = s+"\n"
+ # Allow multi-line messages
if s.find("\n") != -1:
prefix = logprefix
s = s.rstrip("\n")
for line in s.split("\n"):
sys.stderr.write(prefix + line + "\n")
- prefix = "---> "
+ prefix = " "
else:
sys.stderr.write(logprefix + s)
sys.stderr.flush()
@@ -91,11 +95,11 @@ def resolvconf_nameservers(systemd_resolved):
words = line.lower().split()
if len(words) >= 2 and words[0] == 'nameserver':
this_file_nsservers.append(family_ip_tuple(words[1]))
- debug2("Found DNS servers in %s: %s\n" %
+ debug2("Found DNS servers in %s: %s" %
(f, [n[1] for n in this_file_nsservers]))
nsservers += this_file_nsservers
except OSError as e:
- debug3("Failed to read %s when looking for DNS servers: %s\n" %
+ debug3("Failed to read %s when looking for DNS servers: %s" %
(f, e.strerror))
return nsservers
@@ -215,7 +219,7 @@ def which(file, mode=os.F_OK | os.X_OK):
path = get_path()
rv = _which(file, mode, path)
if rv:
- debug2("which() found '%s' at %s\n" % (file, rv))
+ debug2("which() found '%s' at %s" % (file, rv))
else:
- debug2("which() could not find '%s' in %s\n" % (file, path))
+ debug2("which() could not find '%s' in %s" % (file, path))
return rv
diff --git a/sshuttle/hostwatch.py b/sshuttle/hostwatch.py
index d40eaf4..683b6a7 100644
--- a/sshuttle/hostwatch.py
+++ b/sshuttle/hostwatch.py
@@ -24,7 +24,7 @@ try:
null = open(os.devnull, 'wb')
except IOError:
_, e = sys.exc_info()[:2]
- log('warning: %s\n' % e)
+ log('warning: %s' % e)
null = os.popen("sh -c 'while read x; do :; done'", 'wb', 4096)
@@ -80,13 +80,13 @@ def found_host(name, ip):
oldip = hostnames.get(name)
if oldip != ip:
hostnames[name] = ip
- debug1('Found: %s: %s\n' % (name, ip))
+ debug1('Found: %s: %s' % (name, ip))
sys.stdout.write('%s,%s\n' % (name, ip))
write_host_cache()
def _check_etc_hosts():
- debug2(' > hosts\n')
+ debug2(' > hosts')
for line in open('/etc/hosts'):
line = re.sub(r'#.*', '', line)
words = line.strip().split()
@@ -95,17 +95,17 @@ def _check_etc_hosts():
ip = words[0]
names = words[1:]
if _is_ip(ip):
- debug3('< %s %r\n' % (ip, names))
+ debug3('< %s %r' % (ip, names))
for n in names:
check_host(n)
found_host(n, ip)
def _check_revdns(ip):
- debug2(' > rev: %s\n' % ip)
+ debug2(' > rev: %s' % ip)
try:
r = socket.gethostbyaddr(ip)
- debug3('< %s\n' % r[0])
+ debug3('< %s' % r[0])
check_host(r[0])
found_host(r[0], ip)
except (socket.herror, UnicodeError):
@@ -113,10 +113,10 @@ def _check_revdns(ip):
def _check_dns(hostname):
- debug2(' > dns: %s\n' % hostname)
+ debug2(' > dns: %s' % hostname)
try:
ip = socket.gethostbyname(hostname)
- debug3('< %s\n' % ip)
+ debug3('< %s' % ip)
check_host(ip)
found_host(hostname, ip)
except (socket.gaierror, UnicodeError):
@@ -124,7 +124,7 @@ def _check_dns(hostname):
def _check_netstat():
- debug2(' > netstat\n')
+ debug2(' > netstat')
argv = ['netstat', '-n']
try:
p = ssubprocess.Popen(argv, stdout=ssubprocess.PIPE, stderr=null,
@@ -133,11 +133,11 @@ def _check_netstat():
p.wait()
except OSError:
_, e = sys.exc_info()[:2]
- log('%r failed: %r\n' % (argv, e))
+ log('%r failed: %r' % (argv, e))
return
for ip in re.findall(r'\d+\.\d+\.\d+\.\d+', content):
- debug3('< %s\n' % ip)
+ debug3('< %s' % ip)
check_host(ip)
@@ -146,7 +146,7 @@ def _check_smb(hostname):
global _smb_ok
if not _smb_ok:
return
- debug2(' > smb: %s\n' % hostname)
+ debug2(' > smb: %s' % hostname)
argv = ['smbclient', '-U', '%', '-L', hostname]
try:
p = ssubprocess.Popen(argv, stdout=ssubprocess.PIPE, stderr=null,
@@ -155,7 +155,7 @@ def _check_smb(hostname):
p.wait()
except OSError:
_, e = sys.exc_info()[:2]
- log('%r failed: %r\n' % (argv, e))
+ log('%r failed: %r' % (argv, e))
_smb_ok = False
return
@@ -178,7 +178,7 @@ def _check_smb(hostname):
break
words = line.split()
hostname = words[0].lower()
- debug3('< %s\n' % hostname)
+ debug3('< %s' % hostname)
check_host(hostname)
# workgroup list section:
@@ -192,7 +192,7 @@ def _check_smb(hostname):
break
words = line.split()
(workgroup, hostname) = (words[0].lower(), words[1].lower())
- debug3('< group(%s) -> %s\n' % (workgroup, hostname))
+ debug3('< group(%s) -> %s' % (workgroup, hostname))
check_host(hostname)
check_workgroup(workgroup)
@@ -205,7 +205,7 @@ def _check_nmb(hostname, is_workgroup, is_master):
global _nmb_ok
if not _nmb_ok:
return
- debug2(' > n%d%d: %s\n' % (is_workgroup, is_master, hostname))
+ debug2(' > n%d%d: %s' % (is_workgroup, is_master, hostname))
argv = ['nmblookup'] + ['-M'] * is_master + ['--', hostname]
try:
p = ssubprocess.Popen(argv, stdout=ssubprocess.PIPE, stderr=null,
@@ -214,18 +214,18 @@ def _check_nmb(hostname, is_workgroup, is_master):
rv = p.wait()
except OSError:
_, e = sys.exc_info()[:2]
- log('%r failed: %r\n' % (argv, e))
+ log('%r failed: %r' % (argv, e))
_nmb_ok = False
return
if rv:
- log('%r returned %d\n' % (argv, rv))
+ log('%r returned %d' % (argv, rv))
return
for line in lines:
m = re.match(r'(\d+\.\d+\.\d+\.\d+) (\w+)<\w\w>\n', line)
if m:
g = m.groups()
(ip, name) = (g[0], g[1].lower())
- debug3('< %s -> %s\n' % (name, ip))
+ debug3('< %s -> %s' % (name, ip))
if is_workgroup:
_enqueue(_check_smb, ip)
else:
@@ -263,12 +263,9 @@ def _stdin_still_ok(timeout):
def hw_main(seed_hosts, auto_hosts):
- if helpers.verbose >= 2:
- helpers.logprefix = 'HH: '
- else:
- helpers.logprefix = 'hostwatch: '
+ helpers.logprefix = 'HH: '
- debug1('Starting hostwatch with Python version %s\n'
+ debug1('Starting hostwatch with Python version %s'
% platform.python_version())
for h in seed_hosts:
diff --git a/sshuttle/linux.py b/sshuttle/linux.py
index 5f91df6..d893ec7 100644
--- a/sshuttle/linux.py
+++ b/sshuttle/linux.py
@@ -7,7 +7,7 @@ def nonfatal(func, *args):
try:
func(*args)
except Fatal as e:
- log('fw: error: %s\n' % e)
+ log('error: %s' % e)
def ipt_chain_exists(family, table, name):
@@ -24,7 +24,7 @@ def ipt_chain_exists(family, table, name):
if line.startswith('Chain %s ' % name):
return True
except ssubprocess.CalledProcessError as e:
- raise Fatal('fw: %r returned %d' % (argv, e.returncode))
+ raise Fatal('%r returned %d' % (argv, e.returncode))
def ipt(family, table, *args):
@@ -34,10 +34,10 @@ def ipt(family, table, *args):
argv = ['iptables', '-t', table] + list(args)
else:
raise Exception('Unsupported family "%s"' % family_to_string(family))
- debug1('%s\n' % ' '.join(argv))
+ debug1('%s' % ' '.join(argv))
rv = ssubprocess.call(argv, env=get_env())
if rv:
- raise Fatal('fw: %r returned %d' % (argv, rv))
+ raise Fatal('%r returned %d' % (argv, rv))
def nft(family, table, action, *args):
@@ -45,10 +45,10 @@ def nft(family, table, action, *args):
argv = ['nft', action, 'inet', table] + list(args)
else:
raise Exception('Unsupported family "%s"' % family_to_string(family))
- debug1('%s\n' % ' '.join(argv))
+ debug1('%s' % ' '.join(argv))
rv = ssubprocess.call(argv, env=get_env())
if rv:
- raise Fatal('fw: %r returned %d' % (argv, rv))
+ raise Fatal('%r returned %d' % (argv, rv))
_no_ttl_module = False
@@ -66,8 +66,8 @@ def ipt_ttl(family, *args):
except Fatal:
ipt(family, *args)
# we only get here if the non-ttl attempt succeeds
- log('fw: WARNING: your iptables is missing '
- 'the ttl module.\n')
+ log('WARNING: your iptables is missing '
+ 'the ttl module.')
_no_ttl_module = True
else:
ipt(family, *args)
diff --git a/sshuttle/methods/__init__.py b/sshuttle/methods/__init__.py
index a8fe938..1882c3a 100644
--- a/sshuttle/methods/__init__.py
+++ b/sshuttle/methods/__init__.py
@@ -66,7 +66,7 @@ class BaseMethod(object):
@staticmethod
def recv_udp(udp_listener, bufsize):
- debug3('Accept UDP using recvfrom.\n')
+ debug3('Accept UDP using recvfrom.')
data, srcip = udp_listener.recvfrom(bufsize)
return (srcip, None, data)
@@ -87,7 +87,7 @@ class BaseMethod(object):
for key in ["udp", "dns", "ipv6", "ipv4", "user"]:
if getattr(features, key) and not getattr(avail, key):
raise Fatal(
- "Featu