mirror of
https://github.com/sshuttle/sshuttle.git
synced 2025-04-25 11:48:40 +02:00
Factor out common mainloop code between client and server.
Also improve the socket message output a bit.
This commit is contained in:
parent
b0f061e204
commit
84376284db
16
client.py
16
client.py
@ -4,6 +4,7 @@ import helpers, ssnet, ssh
|
|||||||
from ssnet import SockWrapper, Handler, Proxy, Mux, MuxWrapper
|
from ssnet import SockWrapper, Handler, Proxy, Mux, MuxWrapper
|
||||||
from helpers import *
|
from helpers import *
|
||||||
|
|
||||||
|
|
||||||
def original_dst(sock):
|
def original_dst(sock):
|
||||||
try:
|
try:
|
||||||
SO_ORIGINAL_DST = 80
|
SO_ORIGINAL_DST = 80
|
||||||
@ -176,20 +177,7 @@ def _main(listener, fw, use_server, remotename, python, seed_hosts, auto_nets):
|
|||||||
if rv:
|
if rv:
|
||||||
raise Fatal('server died with error code %d' % rv)
|
raise Fatal('server died with error code %d' % rv)
|
||||||
|
|
||||||
r = []
|
ssnet.runonce(handlers, mux)
|
||||||
w = []
|
|
||||||
x = []
|
|
||||||
handlers = filter(lambda s: s.ok, handlers)
|
|
||||||
for s in handlers:
|
|
||||||
s.pre_select(r,w,x)
|
|
||||||
debug2('Waiting: %d[%d,%d,%d]...\n'
|
|
||||||
% (len(handlers), len(r), len(w), len(x)))
|
|
||||||
(r,w,x) = select.select(r,w,x)
|
|
||||||
#log('r=%r w=%r x=%r\n' % (r,w,x))
|
|
||||||
ready = r+w+x
|
|
||||||
for s in handlers:
|
|
||||||
if list_contains_any(s.socks, ready):
|
|
||||||
s.callback()
|
|
||||||
if use_server:
|
if use_server:
|
||||||
mux.callback()
|
mux.callback()
|
||||||
mux.check_fullness()
|
mux.check_fullness()
|
||||||
|
17
server.py
17
server.py
@ -162,21 +162,6 @@ def main():
|
|||||||
if rpid:
|
if rpid:
|
||||||
raise Fatal('hostwatch exited unexpectedly: code 0x%04x\n' % rv)
|
raise Fatal('hostwatch exited unexpectedly: code 0x%04x\n' % rv)
|
||||||
|
|
||||||
r = []
|
ssnet.runonce(handlers, mux)
|
||||||
w = []
|
|
||||||
x = []
|
|
||||||
handlers = filter(lambda s: s.ok, handlers)
|
|
||||||
for s in handlers:
|
|
||||||
s.pre_select(r,w,x)
|
|
||||||
debug2('Waiting: %d[%d,%d,%d] (fullness=%d/%d)...\n'
|
|
||||||
% (len(handlers), len(r), len(w), len(x),
|
|
||||||
mux.fullness, mux.too_full))
|
|
||||||
(r,w,x) = select.select(r,w,x)
|
|
||||||
#log('r=%r w=%r x=%r\n' % (r,w,x))
|
|
||||||
ready = r+w+x
|
|
||||||
for s in handlers:
|
|
||||||
#debug2('check: %r: %r\n' % (s, s.socks & ready))
|
|
||||||
if list_contains_any(s.socks, ready):
|
|
||||||
s.callback()
|
|
||||||
mux.check_fullness()
|
mux.check_fullness()
|
||||||
mux.callback()
|
mux.callback()
|
||||||
|
31
ssnet.py
31
ssnet.py
@ -36,6 +36,17 @@ def _add(l, elem):
|
|||||||
l.append(elem)
|
l.append(elem)
|
||||||
|
|
||||||
|
|
||||||
|
def _fds(l):
|
||||||
|
out = []
|
||||||
|
for i in l:
|
||||||
|
try:
|
||||||
|
out.append(i.fileno())
|
||||||
|
except AttributeError:
|
||||||
|
out.append(i)
|
||||||
|
out.sort()
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
def _nb_clean(func, *args):
|
def _nb_clean(func, *args):
|
||||||
try:
|
try:
|
||||||
return func(*args)
|
return func(*args)
|
||||||
@ -43,6 +54,7 @@ def _nb_clean(func, *args):
|
|||||||
if e.errno not in (errno.EWOULDBLOCK, errno.EAGAIN, errno.EPIPE):
|
if e.errno not in (errno.EWOULDBLOCK, errno.EAGAIN, errno.EPIPE):
|
||||||
raise
|
raise
|
||||||
else:
|
else:
|
||||||
|
debug3('%s: err was: %s\n' % (func.__name__, e))
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
@ -429,3 +441,22 @@ def connect_dst(ip, port):
|
|||||||
return SockWrapper(outsock, outsock,
|
return SockWrapper(outsock, outsock,
|
||||||
connect_to = (ip,port),
|
connect_to = (ip,port),
|
||||||
peername = '%s:%d' % (ip,port))
|
peername = '%s:%d' % (ip,port))
|
||||||
|
|
||||||
|
|
||||||
|
def runonce(handlers, mux):
|
||||||
|
r = []
|
||||||
|
w = []
|
||||||
|
x = []
|
||||||
|
handlers = filter(lambda s: s.ok, handlers)
|
||||||
|
for s in handlers:
|
||||||
|
s.pre_select(r,w,x)
|
||||||
|
debug2('Waiting: %d r=%r w=%r x=%r (fullness=%d/%d)\n'
|
||||||
|
% (len(handlers), _fds(r), _fds(w), _fds(x),
|
||||||
|
mux.fullness, mux.too_full))
|
||||||
|
(r,w,x) = select.select(r,w,x)
|
||||||
|
debug2(' Ready: %d r=%r w=%r x=%r\n'
|
||||||
|
% (len(handlers), _fds(r), _fds(w), _fds(x)))
|
||||||
|
ready = r+w+x
|
||||||
|
for s in handlers:
|
||||||
|
if list_contains_any(s.socks, ready):
|
||||||
|
s.callback()
|
||||||
|
Loading…
Reference in New Issue
Block a user