diff options
author | Brian May <brian@linuxpenguins.xyz> | 2015-11-15 16:45:26 +1100 |
---|---|---|
committer | Brian May <brian@linuxpenguins.xyz> | 2015-11-15 16:45:26 +1100 |
commit | d4f10b232a031004905fa1705829a2060764c407 (patch) | |
tree | 7bfca19f6057c53adda2b050e7df3fabd3adcdf2 /sshuttle/helpers.py | |
parent | 41b8ad4c9797c58e11cd3c562a77bbce907d1c9e (diff) |
Restructure code
* Make compatible with setuptools.
* Load modules via ssh into separate modules, not the one name space.
Diffstat (limited to 'sshuttle/helpers.py')
-rw-r--r-- | sshuttle/helpers.py | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/sshuttle/helpers.py b/sshuttle/helpers.py new file mode 100644 index 0000000..6d8c9a3 --- /dev/null +++ b/sshuttle/helpers.py @@ -0,0 +1,95 @@ +import sys +import socket +import errno + +logprefix = '' +verbose = 0 + + +def log(s): + try: + sys.stdout.flush() + sys.stderr.write(logprefix + s) + sys.stderr.flush() + except IOError: + # this could happen if stderr gets forcibly disconnected, eg. because + # our tty closes. That sucks, but it's no reason to abort the program. + pass + + +def debug1(s): + if verbose >= 1: + log(s) + + +def debug2(s): + if verbose >= 2: + log(s) + + +def debug3(s): + if verbose >= 3: + log(s) + + +class Fatal(Exception): + pass + + +def list_contains_any(l, sub): + for i in sub: + if i in l: + return True + return False + + +def resolvconf_nameservers(): + l = [] + for line in open('/etc/resolv.conf'): + words = line.lower().split() + if len(words) >= 2 and words[0] == 'nameserver': + l.append(family_ip_tuple(words[1])) + return l + + +def resolvconf_random_nameserver(): + l = resolvconf_nameservers() + if l: + if len(l) > 1: + # don't import this unless we really need it + import random + random.shuffle(l) + return l[0] + else: + return (socket.AF_INET, '127.0.0.1') + + +def islocal(ip, family): + sock = socket.socket(family) + try: + try: + sock.bind((ip, 0)) + except socket.error, e: + if e.args[0] == errno.EADDRNOTAVAIL: + return False # not a local IP + else: + raise + finally: + sock.close() + return True # it's a local IP, or there would have been an error + + +def family_ip_tuple(ip): + if ':' in ip: + return (socket.AF_INET6, ip) + else: + return (socket.AF_INET, ip) + + +def family_to_string(family): + if family == socket.AF_INET6: + return "AF_INET6" + elif family == socket.AF_INET: + return "AF_INET" + else: + return str(family) |