From e6f2395dacafaef086c247437bdabe8b5ab17902 Mon Sep 17 00:00:00 2001 From: Brian May Date: Sun, 15 Nov 2015 16:49:20 +1100 Subject: [PATCH] Fixup PEP8 issues. --- sshuttle/__main__.py | 1 + sshuttle/client.py | 7 ++- sshuttle/compat/ssubprocess.py | 75 +++++++++++++---------------- sshuttle/firewall.py | 86 ++++++++++++++++++++++------------ sshuttle/ssh.py | 1 + sshuttle/ui-macos/main.py | 26 +++++----- sshuttle/ui-macos/my.py | 10 ++-- 7 files changed, 113 insertions(+), 93 deletions(-) diff --git a/sshuttle/__main__.py b/sshuttle/__main__.py index 409e0d6..2c8aed6 100644 --- a/sshuttle/__main__.py +++ b/sshuttle/__main__.py @@ -105,6 +105,7 @@ def parse_ipport6(s): (ip, port) = (ip or '::', int(port or 0)) return (ip, port) + def parse_list(list): return re.split(r'[\s,]+', list.strip()) if list else [] diff --git a/sshuttle/client.py b/sshuttle/client.py index a0f6573..55be9b3 100644 --- a/sshuttle/client.py +++ b/sshuttle/client.py @@ -12,7 +12,7 @@ import ssyslog import sys from sshuttle.ssnet import SockWrapper, Handler, Proxy, Mux, MuxWrapper from sshuttle.helpers import log, debug1, debug2, debug3, Fatal, islocal, \ - resolvconf_nameservers + resolvconf_nameservers recvmsg = None try: @@ -187,11 +187,13 @@ def daemon_cleanup(): pf_command_file = None + def pf_dst(sock): peer = sock.getpeername() proxy = sock.getsockname() - argv = (sock.family, socket.IPPROTO_TCP, peer[0], peer[1], proxy[0], proxy[1]) + argv = (sock.family, socket.IPPROTO_TCP, + peer[0], peer[1], proxy[0], proxy[1]) pf_command_file.write("QUERY_PF_NAT %r,%r,%s,%r,%s,%r\n" % argv) pf_command_file.flush() line = pf_command_file.readline() @@ -202,6 +204,7 @@ def pf_dst(sock): return sock.getsockname() + def original_dst(sock): try: SO_ORIGINAL_DST = 80 diff --git a/sshuttle/compat/ssubprocess.py b/sshuttle/compat/ssubprocess.py index ee6b8da..87797e6 100644 --- a/sshuttle/compat/ssubprocess.py +++ b/sshuttle/compat/ssubprocess.py @@ -360,13 +360,17 @@ import gc import signal # Exception classes used by this module. + + class CalledProcessError(Exception): """This exception is raised when a process run by check_call() returns a non-zero exit status. The exit status will be stored in the returncode attribute.""" + def __init__(self, returncode, cmd): self.returncode = returncode self.cmd = cmd + def __str__(self): return "Command '%s' returned non-zero exit status %d" % (self.cmd, self.returncode) @@ -374,27 +378,29 @@ class CalledProcessError(Exception): if mswindows: import threading import msvcrt - if 0: # <-- change this to use pywin32 instead of the _subprocess driver + if 0: # <-- change this to use pywin32 instead of the _subprocess driver import pywintypes from win32api import GetStdHandle, STD_INPUT_HANDLE, \ - STD_OUTPUT_HANDLE, STD_ERROR_HANDLE + STD_OUTPUT_HANDLE, STD_ERROR_HANDLE from win32api import GetCurrentProcess, DuplicateHandle, \ - GetModuleFileName, GetVersion + GetModuleFileName, GetVersion from win32con import DUPLICATE_SAME_ACCESS, SW_HIDE from win32pipe import CreatePipe from win32process import CreateProcess, STARTUPINFO, \ - GetExitCodeProcess, STARTF_USESTDHANDLES, \ - STARTF_USESHOWWINDOW, CREATE_NEW_CONSOLE + GetExitCodeProcess, STARTF_USESTDHANDLES, \ + STARTF_USESHOWWINDOW, CREATE_NEW_CONSOLE from win32process import TerminateProcess from win32event import WaitForSingleObject, INFINITE, WAIT_OBJECT_0 else: from _subprocess import * + class STARTUPINFO: dwFlags = 0 hStdInput = None hStdOutput = None hStdError = None wShowWindow = 0 + class pywintypes: error = IOError else: @@ -403,7 +409,8 @@ else: import fcntl import pickle -__all__ = ["Popen", "PIPE", "STDOUT", "call", "check_call", "CalledProcessError"] +__all__ = ["Popen", "PIPE", "STDOUT", "call", + "check_call", "CalledProcessError"] try: MAXFD = os.sysconf("SC_OPEN_MAX") @@ -411,14 +418,15 @@ except: MAXFD = 256 # True/False does not exist on 2.2.0 -#try: +# try: # False -#except NameError: +# except NameError: # False = 0 # True = 1 _active = [] + def _cleanup(): for inst in _active[:]: if inst._internal_poll(_deadstate=sys.maxint) >= 0: @@ -510,7 +518,7 @@ def list2cmdline(seq): bs_buf.append(c) elif c == '"': # Double backslashes. - result.append('\\' * len(bs_buf)*2) + result.append('\\' * len(bs_buf) * 2) bs_buf = [] result.append('\\"') else: @@ -543,6 +551,7 @@ def _closerange(start, max): class Popen(object): + def __init__(self, args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=False, shell=False, @@ -634,13 +643,11 @@ class Popen(object): else: self.stderr = os.fdopen(errread, 'rb', bufsize) - def _translate_newlines(self, data): data = data.replace("\r\n", "\n") data = data.replace("\r", "\n") return data - def __del__(self, sys=sys): if not self._child_created: # We didn't get to successfully create a child process. @@ -651,7 +658,6 @@ class Popen(object): # Child is still running, keep us alive until we can wait on it. _active.append(self) - def communicate(self, input=None): """Interact with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. Wait for @@ -681,11 +687,9 @@ class Popen(object): return self._communicate(input) - def poll(self): return self._internal_poll() - if mswindows: # # Windows methods @@ -755,14 +759,12 @@ class Popen(object): c2pread, c2pwrite, errread, errwrite) - def _make_inheritable(self, handle): """Return a duplicate of handle, which is inheritable""" return DuplicateHandle(GetCurrentProcess(), handle, GetCurrentProcess(), 0, 1, DUPLICATE_SAME_ACCESS) - def _find_w9xpopen(self): """Find and return absolut path to w9xpopen.exe""" w9xpopen = os.path.join(os.path.dirname(GetModuleFileName(0)), @@ -778,7 +780,6 @@ class Popen(object): "shell or platform.") return w9xpopen - def _execute_child(self, args, executable, preexec_fn, close_fds, cwd, env, universal_newlines, startupinfo, creationflags, shell, @@ -823,13 +824,13 @@ class Popen(object): # Start the process try: hp, ht, pid, tid = CreateProcess(executable, args, - # no special security - None, None, - int(not close_fds), - creationflags, - env, - cwd, - startupinfo) + # no special security + None, None, + int(not close_fds), + creationflags, + env, + cwd, + startupinfo) except pywintypes.error, e: # Translate pywintypes.error to WindowsError, which is # a subclass of OSError. FIXME: We should really @@ -856,7 +857,6 @@ class Popen(object): if errwrite is not None: errwrite.Close() - def _internal_poll(self, _deadstate=None): """Check if child process has terminated. Returns returncode attribute.""" @@ -865,7 +865,6 @@ class Popen(object): self.returncode = GetExitCodeProcess(self._handle) return self.returncode - def wait(self): """Wait for child process to terminate. Returns returncode attribute.""" @@ -874,14 +873,12 @@ class Popen(object): self.returncode = GetExitCodeProcess(self._handle) return self.returncode - def _readerthread(self, fh, buffer): buffer.append(fh.read()) - def _communicate(self, input): - stdout = None # Return - stderr = None # Return + stdout = None # Return + stderr = None # Return if self.stdout: stdout = [] @@ -988,7 +985,6 @@ class Popen(object): c2pread, c2pwrite, errread, errwrite) - def _set_cloexec_flag(self, fd): try: cloexec_flag = fcntl.FD_CLOEXEC @@ -998,12 +994,10 @@ class Popen(object): old = fcntl.fcntl(fd, fcntl.F_GETFD) fcntl.fcntl(fd, fcntl.F_SETFD, old | cloexec_flag) - def _close_fds(self, but): _closerange(3, but) _closerange(but + 1, MAXFD) - def _execute_child(self, args, executable, preexec_fn, close_fds, cwd, env, universal_newlines, startupinfo, creationflags, shell, @@ -1109,14 +1103,13 @@ class Popen(object): os.close(errwrite) # Wait for exec to fail or succeed; possibly raising exception - data = os.read(errpipe_read, 1048576) # Exceptions limited to 1 MB + data = os.read(errpipe_read, 1048576) # Exceptions limited to 1 MB os.close(errpipe_read) if data != "": os.waitpid(self.pid, 0) child_exception = pickle.loads(data) raise child_exception - def _handle_exitstatus(self, sts): if os.WIFSIGNALED(sts): self.returncode = -os.WTERMSIG(sts) @@ -1126,7 +1119,6 @@ class Popen(object): # Should never happen raise RuntimeError("Unknown child exit status!") - def _internal_poll(self, _deadstate=None): """Check if child process has terminated. Returns returncode attribute.""" @@ -1140,7 +1132,6 @@ class Popen(object): self.returncode = _deadstate return self.returncode - def wait(self): """Wait for child process to terminate. Returns returncode attribute.""" @@ -1149,12 +1140,11 @@ class Popen(object): self._handle_exitstatus(sts) return self.returncode - def _communicate(self, input): read_set = [] write_set = [] - stdout = None # Return - stderr = None # Return + stdout = None # Return + stderr = None # Return if self.stdin: # Flush stdio buffer. This might block, if the user has @@ -1174,7 +1164,8 @@ class Popen(object): input_offset = 0 while read_set or write_set: try: - rlist, wlist, xlist = select.select(read_set, write_set, []) + rlist, wlist, xlist = select.select( + read_set, write_set, []) except select.error, e: if e.args[0] == errno.EINTR: continue @@ -1184,7 +1175,7 @@ class Popen(object): # When select has indicated that the file is writable, # we can write up to PIPE_BUF bytes without risk # blocking. POSIX defines PIPE_BUF >= 512 - chunk = input[input_offset : input_offset + 512] + chunk = input[input_offset: input_offset + 512] bytes_written = os.write(self.stdin.fileno(), chunk) input_offset += bytes_written if input_offset >= len(input): diff --git a/sshuttle/firewall.py b/sshuttle/firewall.py index 1856a15..50621d0 100644 --- a/sshuttle/firewall.py +++ b/sshuttle/firewall.py @@ -12,7 +12,7 @@ from sshuttle.helpers import log, debug1, debug3, islocal, Fatal, family_to_stri resolvconf_nameservers from fcntl import ioctl from ctypes import c_char, c_uint8, c_uint16, c_uint32, Union, Structure, \ - sizeof, addressof, memmove + sizeof, addressof, memmove # python doesn't have a definition for this @@ -465,20 +465,21 @@ def do_ipfw(port, dnsport, family, subnets, udp): return do_wait -def pfctl(args, stdin = None): +def pfctl(args, stdin=None): argv = ['pfctl'] + list(args.split(" ")) debug1('>> %s\n' % ' '.join(argv)) - p = ssubprocess.Popen(argv, stdin = ssubprocess.PIPE, - stdout = ssubprocess.PIPE, - stderr = ssubprocess.PIPE) + p = ssubprocess.Popen(argv, stdin=ssubprocess.PIPE, + stdout=ssubprocess.PIPE, + stderr=ssubprocess.PIPE) o = p.communicate(stdin) if p.returncode: raise Fatal('%r returned %d' % (argv, p.returncode)) return o -_pf_context = {'started_by_sshuttle': False, 'Xtoken':''} +_pf_context = {'started_by_sshuttle': False, 'Xtoken': ''} + def do_pf(port, dnsport, nslist, family, subnets, udp): global _pf_started_by_sshuttle @@ -487,27 +488,33 @@ def do_pf(port, dnsport, nslist, family, subnets, udp): filtering_rules = [] if subnets: - includes=[] + includes = [] # If a given subnet is both included and excluded, list the exclusion # first; the table will ignore the second, opposite definition for f, swidth, sexclude, snet \ in sorted(subnets, key=lambda s: (s[1], s[2]), reverse=True): - includes.append("%s%s/%s" % ("!" if sexclude else "", snet, swidth)) + includes.append("%s%s/%s" % + ("!" if sexclude else "", snet, swidth)) tables.append('table {%s}' % ','.join(includes)) - translating_rules.append('rdr pass on lo0 proto tcp to -> 127.0.0.1 port %r' % port) - filtering_rules.append('pass out route-to lo0 inet proto tcp to keep state') + translating_rules.append( + 'rdr pass on lo0 proto tcp to -> 127.0.0.1 port %r' % port) + filtering_rules.append( + 'pass out route-to lo0 inet proto tcp to keep state') if dnsport: - tables.append('table {%s}' % ','.join([ns[1] for ns in nslist])) - translating_rules.append('rdr pass on lo0 proto udp to port 53 -> 127.0.0.1 port %r' % dnsport) - filtering_rules.append('pass out route-to lo0 inet proto udp to port 53 keep state') + tables.append('table {%s}' % ','.join( + [ns[1] for ns in nslist])) + translating_rules.append( + 'rdr pass on lo0 proto udp to port 53 -> 127.0.0.1 port %r' % dnsport) + filtering_rules.append( + 'pass out route-to lo0 inet proto udp to port 53 keep state') rules = '\n'.join(tables + translating_rules + filtering_rules) + '\n' pf_status = pfctl('-s all')[0] if not '\nrdr-anchor "sshuttle" all\n' in pf_status: - pf_add_anchor_rule(PF_RDR, "sshuttle") + pf_add_anchor_rule(PF_RDR, "sshuttle") if not '\nanchor "sshuttle" all\n' in pf_status: pf_add_anchor_rule(PF_PASS, "sshuttle") @@ -515,7 +522,7 @@ def do_pf(port, dnsport, nslist, family, subnets, udp): if sys.platform == "darwin": o = pfctl('-E') _pf_context['Xtoken'] = re.search(r'Token : (.+)', o[1]).group(1) - elif 'INFO:\nStatus: Disabled' in pf_status: + elif 'INFO:\nStatus: Disabled' in pf_status: pfctl('-e') _pf_context['started_by_sshuttle'] = True else: @@ -578,13 +585,15 @@ def restore_etc_hosts(port): rewrite_etc_hosts(port) -# This are some classes and functions used to support pf in yosemite. +# This are some classes and functions used to support pf in yosemite. class pf_state_xport(Union): _fields_ = [("port", c_uint16), ("call_id", c_uint16), ("spi", c_uint32)] + class pf_addr(Structure): + class _pfa(Union): _fields_ = [("v4", c_uint32), # struct in_addr ("v6", c_uint32 * 4), # struct in6_addr @@ -595,6 +604,7 @@ class pf_addr(Structure): _fields_ = [("pfa", _pfa)] _anonymous_ = ("pfa",) + class pfioc_natlook(Structure): _fields_ = [("saddr", pf_addr), ("daddr", pf_addr), @@ -604,20 +614,23 @@ class pfioc_natlook(Structure): ("dxport", pf_state_xport), ("rsxport", pf_state_xport), ("rdxport", pf_state_xport), - ("af", c_uint8), # sa_family_t + ("af", c_uint8), # sa_family_t ("proto", c_uint8), ("proto_variant", c_uint8), ("direction", c_uint8)] pfioc_rule = c_char * 3104 # sizeof(struct pfioc_rule) -pfioc_pooladdr = c_char * 1136 # sizeof(struct pfioc_pooladdr) +pfioc_pooladdr = c_char * 1136 # sizeof(struct pfioc_pooladdr) MAXPATHLEN = 1024 -DIOCNATLOOK = ((0x40000000L | 0x80000000L) | ((sizeof(pfioc_natlook) & 0x1fff) << 16) | ((ord('D')) << 8) | (23)) -DIOCCHANGERULE = ((0x40000000L | 0x80000000L) | ((sizeof(pfioc_rule) & 0x1fff) << 16) | ((ord('D')) << 8) | (26)) -DIOCBEGINADDRS = ((0x40000000L | 0x80000000L) | ((sizeof(pfioc_pooladdr) & 0x1fff) << 16) | ((ord('D')) << 8) | (51)) +DIOCNATLOOK = ((0x40000000L | 0x80000000L) | ( + (sizeof(pfioc_natlook) & 0x1fff) << 16) | ((ord('D')) << 8) | (23)) +DIOCCHANGERULE = ((0x40000000L | 0x80000000L) | ( + (sizeof(pfioc_rule) & 0x1fff) << 16) | ((ord('D')) << 8) | (26)) +DIOCBEGINADDRS = ((0x40000000L | 0x80000000L) | ( + (sizeof(pfioc_pooladdr) & 0x1fff) << 16) | ((ord('D')) << 8) | (51)) PF_CHANGE_ADD_TAIL = 2 PF_CHANGE_GET_TICKET = 6 @@ -629,6 +642,7 @@ PF_OUT = 2 _pf_fd = None + def pf_get_dev(): global _pf_fd if _pf_fd == None: @@ -636,9 +650,11 @@ def pf_get_dev(): return _pf_fd + def pf_query_nat(family, proto, src_ip, src_port, dst_ip, dst_port): - [proto, family, src_port, dst_port] = [int(v) for v in [proto, family, src_port, dst_port]] - + [proto, family, src_port, dst_port] = [ + int(v) for v in [proto, family, src_port, dst_port]] + length = 4 if family == socket.AF_INET else 16 pnl = pfioc_natlook() @@ -650,12 +666,15 @@ def pf_query_nat(family, proto, src_ip, src_port, dst_ip, dst_port): memmove(addressof(pnl.daddr), socket.inet_pton(pnl.af, dst_ip), length) pnl.dxport.port = socket.htons(dst_port) - ioctl(pf_get_dev(), DIOCNATLOOK, (c_char * sizeof(pnl)).from_address(addressof(pnl))) + ioctl(pf_get_dev(), DIOCNATLOOK, (c_char * + sizeof(pnl)).from_address(addressof(pnl))) - ip = socket.inet_ntop(pnl.af, (c_char * length).from_address(addressof(pnl.rdaddr))) + ip = socket.inet_ntop( + pnl.af, (c_char * length).from_address(addressof(pnl.rdaddr))) port = socket.ntohs(pnl.rdxport.port) return (ip, port) + def pf_add_anchor_rule(type, name): ACTION_OFFSET = 0 POOL_TICKET_OFFSET = 8 @@ -667,14 +686,18 @@ def pf_add_anchor_rule(type, name): ioctl(pf_get_dev(), DIOCBEGINADDRS, ppa) - memmove(addressof(pr) + POOL_TICKET_OFFSET, ppa[4:8], 4) #pool_ticket - memmove(addressof(pr) + ANCHOR_CALL_OFFSET, name, min(MAXPATHLEN, len(name))) #anchor_call = name - memmove(addressof(pr) + RULE_ACTION_OFFSET, struct.pack('I', type), 4) #rule.action = type + memmove(addressof(pr) + POOL_TICKET_OFFSET, ppa[4:8], 4) # pool_ticket + memmove(addressof(pr) + ANCHOR_CALL_OFFSET, name, + min(MAXPATHLEN, len(name))) # anchor_call = name + memmove(addressof(pr) + RULE_ACTION_OFFSET, + struct.pack('I', type), 4) # rule.action = type - memmove(addressof(pr) + ACTION_OFFSET, struct.pack('I', PF_CHANGE_GET_TICKET), 4) #action = PF_CHANGE_GET_TICKET + memmove(addressof(pr) + ACTION_OFFSET, struct.pack('I', + PF_CHANGE_GET_TICKET), 4) # action = PF_CHANGE_GET_TICKET ioctl(pf_get_dev(), DIOCCHANGERULE, pr) - memmove(addressof(pr) + ACTION_OFFSET, struct.pack('I', PF_CHANGE_ADD_TAIL), 4) #action = PF_CHANGE_ADD_TAIL + memmove(addressof(pr) + ACTION_OFFSET, struct.pack('I', + PF_CHANGE_ADD_TAIL), 4) # action = PF_CHANGE_ADD_TAIL ioctl(pf_get_dev(), DIOCCHANGERULE, pr) @@ -707,7 +730,8 @@ def main(port_v6, port_v4, dnsport_v6, dnsport_v4, nslist, method, udp, syslog): elif program_exists('pfctl'): method = "pf" else: - raise Fatal("can't find either ipfw, iptables or pfctl; check your PATH") + raise Fatal( + "can't find either ipfw, iptables or pfctl; check your PATH") if method == "nat": do_it = do_iptables_nat diff --git a/sshuttle/ssh.py b/sshuttle/ssh.py index a0c1ad1..aea22ae 100644 --- a/sshuttle/ssh.py +++ b/sshuttle/ssh.py @@ -42,6 +42,7 @@ def readfile(name): return contents + def empackage(z, name, data=None): if not data: data = readfile(name) diff --git a/sshuttle/ui-macos/main.py b/sshuttle/ui-macos/main.py index 62fad4c..50d71ba 100644 --- a/sshuttle/ui-macos/main.py +++ b/sshuttle/ui-macos/main.py @@ -2,19 +2,19 @@ import sys import os import pty from AppKit import ( - objc, - NSApp, - NSApplicationMain, - NSAttributedString, - NSFileHandle, - NSFileHandleDataAvailableNotification, - NSImage, - NSMenu, - NSMenuItem, - NSNotificationCenter, - NSObject, - NSStatusBar, - NSVariableStatusItemLength, + objc, + NSApp, + NSApplicationMain, + NSAttributedString, + NSFileHandle, + NSFileHandleDataAvailableNotification, + NSImage, + NSMenu, + NSMenuItem, + NSNotificationCenter, + NSObject, + NSStatusBar, + NSVariableStatusItemLength, ) import my import models diff --git a/sshuttle/ui-macos/my.py b/sshuttle/ui-macos/my.py index 134d6fc..e32bc46 100644 --- a/sshuttle/ui-macos/my.py +++ b/sshuttle/ui-macos/my.py @@ -1,10 +1,10 @@ import os from AppKit import ( - NSBundle, - NSData, - NSDictionary, - NSImage, - NSUserDefaults, + NSBundle, + NSData, + NSDictionary, + NSImage, + NSUserDefaults, ) import PyObjCTools.AppHelper