2010-05-02 06:52:06 +02:00
|
|
|
import struct, socket, select
|
|
|
|
import ssnet, helpers
|
|
|
|
from ssnet import SockWrapper, Handler, Proxy, Mux, MuxWrapper
|
|
|
|
from helpers import *
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
# synchronization header
|
|
|
|
sys.stdout.write('SSHUTTLE0001')
|
|
|
|
sys.stdout.flush()
|
2010-05-02 08:23:42 +02:00
|
|
|
|
|
|
|
if helpers.verbose >= 1:
|
|
|
|
helpers.logprefix = ' s: '
|
|
|
|
else:
|
|
|
|
helpers.logprefix = 'server: '
|
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)
|
|
|
|
|
|
|
|
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()
|