2010-05-08 02:02:04 +02:00
|
|
|
import re, struct, socket, select, subprocess
|
2010-05-05 05:21:16 +02:00
|
|
|
if not globals().get('skip_imports'):
|
|
|
|
import ssnet, helpers
|
|
|
|
from ssnet import SockWrapper, Handler, Proxy, Mux, MuxWrapper
|
|
|
|
from helpers import *
|
2010-05-02 06:52:06 +02:00
|
|
|
|
|
|
|
|
2010-05-08 02:02:04 +02:00
|
|
|
def _ipmatch(ipstr):
|
|
|
|
if ipstr == 'default':
|
|
|
|
ipstr = '0.0.0.0/0'
|
|
|
|
m = re.match(r'^(\d+(\.\d+(\.\d+(\.\d+)?)?)?)(?:/(\d+))?$', ipstr)
|
|
|
|
if m:
|
|
|
|
g = m.groups()
|
|
|
|
ips = g[0]
|
|
|
|
width = int(g[4] or 32)
|
|
|
|
if g[1] == None:
|
|
|
|
ips += '.0.0.0'
|
|
|
|
width = min(width, 8)
|
|
|
|
elif g[2] == None:
|
|
|
|
ips += '.0.0'
|
|
|
|
width = min(width, 16)
|
|
|
|
elif g[3] == None:
|
|
|
|
ips += '.0'
|
|
|
|
width = min(width, 24)
|
|
|
|
return (struct.unpack('!I', socket.inet_aton(ips))[0], width)
|
|
|
|
|
|
|
|
|
|
|
|
def _ipstr(ip, width):
|
|
|
|
if width >= 32:
|
|
|
|
return ip
|
|
|
|
else:
|
|
|
|
return "%s/%d" % (ip, width)
|
|
|
|
|
|
|
|
|
|
|
|
def _maskbits(netmask):
|
|
|
|
if not netmask:
|
|
|
|
return 32
|
|
|
|
for i in range(32):
|
|
|
|
if netmask[0] & (1<<i):
|
|
|
|
return 32-i
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
def _list_routes():
|
|
|
|
argv = ['netstat', '-rn']
|
|
|
|
p = subprocess.Popen(argv, stdout=subprocess.PIPE)
|
|
|
|
routes = []
|
|
|
|
for line in p.stdout:
|
|
|
|
cols = re.split(r'\s+', line)
|
|
|
|
ipw = _ipmatch(cols[0])
|
|
|
|
if not ipw:
|
|
|
|
continue # some lines won't be parseable; never mind
|
|
|
|
maskw = _ipmatch(cols[2]) # linux only
|
|
|
|
mask = _maskbits(maskw) # returns 32 if maskw is null
|
|
|
|
width = min(ipw[1], mask)
|
|
|
|
ip = ipw[0] & (((1<<width)-1) << (32-width))
|
|
|
|
routes.append((socket.inet_ntoa(struct.pack('!I', ip)), width))
|
|
|
|
rv = p.wait()
|
|
|
|
if rv != 0:
|
|
|
|
raise Fatal('%r returned %d' % (argv, rv))
|
|
|
|
return routes
|
|
|
|
|
|
|
|
|
|
|
|
def list_routes():
|
|
|
|
for (ip,width) in _list_routes():
|
|
|
|
if not ip.startswith('0.') and not ip.startswith('127.'):
|
|
|
|
yield (ip,width)
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-05-02 06:52:06 +02:00
|
|
|
def main():
|
2010-05-02 08:23:42 +02:00
|
|
|
if helpers.verbose >= 1:
|
|
|
|
helpers.logprefix = ' s: '
|
|
|
|
else:
|
|
|
|
helpers.logprefix = 'server: '
|
2010-05-08 02:02:04 +02:00
|
|
|
|
|
|
|
routes = list(list_routes())
|
|
|
|
debug1('available routes:\n')
|
|
|
|
for r in routes:
|
|
|
|
debug1(' %s/%d\n' % r)
|
2010-05-05 05:21:16 +02:00
|
|
|
|
|
|
|
# synchronization header
|
|
|
|
sys.stdout.write('SSHUTTLE0001')
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
2010-05-02 06:52:06 +02:00
|
|
|
handlers = []
|
|
|
|
mux = Mux(socket.fromfd(sys.stdin.fileno(),
|
|
|
|
socket.AF_INET, socket.SOCK_STREAM),
|
|
|
|
socket.fromfd(sys.stdout.fileno(),
|
|
|
|
socket.AF_INET, socket.SOCK_STREAM))
|
|
|
|
handlers.append(mux)
|
2010-05-08 02:02:04 +02:00
|
|
|
routepkt = ''.join('%s,%d\n' % r
|
|
|
|
for r in routes)
|
|
|
|
mux.send(0, ssnet.CMD_ROUTES, routepkt)
|
2010-05-02 06:52:06 +02:00
|
|
|
|
|
|
|
def new_channel(channel, data):
|
|
|
|
(dstip,dstport) = data.split(',', 1)
|
|
|
|
dstport = int(dstport)
|
|
|
|
outwrap = ssnet.connect_dst(dstip,dstport)
|
|
|
|
handlers.append(Proxy(MuxWrapper(mux, channel), outwrap))
|
|
|
|
|
|
|
|
mux.new_channel = new_channel
|
|
|
|
|
|
|
|
while mux.ok:
|
|
|
|
r = set()
|
|
|
|
w = set()
|
|
|
|
x = set()
|
|
|
|
handlers = filter(lambda s: s.ok, handlers)
|
|
|
|
for s in handlers:
|
|
|
|
s.pre_select(r,w,x)
|
2010-05-02 12:17:43 +02:00
|
|
|
debug2('Waiting: %d[%d,%d,%d] (fullness=%d/%d)...\n'
|
|
|
|
% (len(handlers), len(r), len(w), len(x),
|
|
|
|
mux.fullness, mux.too_full))
|
2010-05-02 06:52:06 +02:00
|
|
|
(r,w,x) = select.select(r,w,x)
|
2010-05-02 07:18:55 +02:00
|
|
|
#log('r=%r w=%r x=%r\n' % (r,w,x))
|
2010-05-02 06:52:06 +02:00
|
|
|
ready = set(r) | set(w) | set(x)
|
|
|
|
for s in handlers:
|
2010-05-02 12:17:43 +02:00
|
|
|
#debug2('check: %r: %r\n' % (s, s.socks & ready))
|
2010-05-02 06:52:06 +02:00
|
|
|
if s.socks & ready:
|
|
|
|
s.callback()
|
2010-05-02 11:06:51 +02:00
|
|
|
mux.check_fullness()
|
2010-05-02 11:39:17 +02:00
|
|
|
mux.callback()
|