Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 88 lines (68 sloc) 1.925 kb
e67208a @brianmay helpers.py: errno is used by this module, but not imported.
brianmay authored
1 import sys, os, socket, errno
a818105 @apenwarr client now listens on a socket and gets SO_ORIGINAL_DST correctly.
authored
2
915a96b @apenwarr We now have a server that works... some of the time.
authored
3 logprefix = ''
2dd328a @apenwarr Add a -v (and -vv) flag and decrease default message verbosity.
authored
4 verbose = 0
915a96b @apenwarr We now have a server that works... some of the time.
authored
5
a818105 @apenwarr client now listens on a socket and gets SO_ORIGINAL_DST correctly.
authored
6 def log(s):
3a25f70 @apenwarr log(): don't abort if we fail to write to stderr.
authored
7 try:
8 sys.stdout.flush()
9 sys.stderr.write(logprefix + s)
10 sys.stderr.flush()
11 except IOError:
12 # this could happen if stderr gets forcibly disconnected, eg. because
13 # our tty closes. That sucks, but it's no reason to abort the program.
14 pass
2dd328a @apenwarr Add a -v (and -vv) flag and decrease default message verbosity.
authored
15
16 def debug1(s):
17 if verbose >= 1:
18 log(s)
19
20 def debug2(s):
21 if verbose >= 2:
22 log(s)
81c89ce @apenwarr Don't bother with a backtrace when we produce certain fatal errors.
authored
23
a2ea5ab @apenwarr Add 'sshuttle --hostwatch' subcommand.
authored
24 def debug3(s):
25 if verbose >= 3:
26 log(s)
27
81c89ce @apenwarr Don't bother with a backtrace when we produce certain fatal errors.
authored
28
29 class Fatal(Exception):
30 pass
c403a83 @apenwarr Don't use set() since it's not in python 2.3.
authored
31
32
bd20841 @apenwarr firewall.py: clean up repeated calls to ssubprocess.call().
authored
33 EXITCODE_NEEDS_REBOOT = 111
34 class FatalNeedsReboot(Fatal):
35 pass
36
37
c403a83 @apenwarr Don't use set() since it's not in python 2.3.
authored
38 def list_contains_any(l, sub):
39 for i in sub:
40 if i in l:
41 return True
42 return False
4c5185d @apenwarr dns: extract 'nameserver' lines from /etc/resolv.conf
authored
43
44
45 def resolvconf_nameservers():
46 l = []
47 for line in open('/etc/resolv.conf'):
48 words = line.lower().split()
49 if len(words) >= 2 and words[0] == 'nameserver':
50 l.append(words[1])
51 return l
52
53
54 def resolvconf_random_nameserver():
55 l = resolvconf_nameservers()
56 if l:
57 if len(l) > 1:
58 # don't import this unless we really need it
59 import random
60 random.shuffle(l)
61 return l[0]
62 else:
63 return '127.0.0.1'
64
7f3c522 @apenwarr Move client._islocal() to helpers.islocal() in preparation for sharing.
authored
65
66 def islocal(ip):
67 sock = socket.socket()
68 try:
69 try:
70 sock.bind((ip, 0))
71 except socket.error, e:
72 if e.args[0] == errno.EADDRNOTAVAIL:
73 return False # not a local IP
74 else:
75 raise
76 finally:
77 sock.close()
78 return True # it's a local IP, or there would have been an error
79
80
432e98c @apenwarr auto-hosts: don't add hosts that aren't being routed by sshuttle.
authored
81 def shl(n, bits):
82 # we use our own implementation of left-shift because
83 # results may be different between older and newer versions
84 # of python for numbers like 1<<32. We use long() because
85 # int(2**32) doesn't work in older python, which has limited
86 # int sizes.
87 return n * long(2**bits)
Something went wrong with that request. Please try again.