Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for iproute2 #132

Merged
merged 3 commits into from
Feb 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 37 additions & 8 deletions sshuttle/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
from sshuttle.helpers import b, log, debug1, debug2, debug3, Fatal, \
resolvconf_random_nameserver

try:
from shutil import which
except ImportError:
from distutils.spawn import find_executable as which


def _ipmatch(ipstr):
# FIXME: IPv4 only
Expand Down Expand Up @@ -60,22 +65,37 @@ def _shl(n, bits):
return n * int(2 ** bits)


def _list_routes():
def _route_netstat(line):
cols = line.split(None)
ipw = _ipmatch(cols[0])
maskw = _ipmatch(cols[2]) # linux only
mask = _maskbits(maskw) # returns 32 if maskw is null
return ipw, mask


def _route_iproute(line):
ipm = line.split(None, 1)[0]
if '/' not in ipm:
return None, None
ip, mask = ipm.split('/')
ipw = _ipmatch(ip)
return ipw, int(mask)


def _list_routes(argv, extract_route):
# FIXME: IPv4 only
argv = ['netstat', '-rn']
env = {
'PATH': os.environ['PATH'],
'LC_ALL': "C",
}
p = ssubprocess.Popen(argv, stdout=ssubprocess.PIPE, env=env)
routes = []
for line in p.stdout:
cols = re.split(r'\s+', line.decode("ASCII"))
ipw = _ipmatch(cols[0])
if not line.strip():
continue
ipw, mask = extract_route(line.decode("ASCII"))
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
continue
width = min(ipw[1], mask)
ip = ipw[0] & _shl(_shl(1, width) - 1, 32 - width)
routes.append(
Expand All @@ -84,11 +104,20 @@ def _list_routes():
if rv != 0:
log('WARNING: %r returned %d\n' % (argv, rv))
log('WARNING: That prevents --auto-nets from working.\n')

return routes


def list_routes():
for (family, ip, width) in _list_routes():
if which('ip'):
routes = _list_routes(['ip', 'route'], _route_iproute)
elif which('netstat'):
routes = _list_routes(['netstat', '-rn'], _route_netstat)
else:
log('WARNING: Neither ip nor netstat were found on the server.\n')
routes = []

for (family, ip, width) in routes:
if not ip.startswith('0.') and not ip.startswith('127.'):
yield (family, ip, width)

Expand Down
25 changes: 8 additions & 17 deletions sshuttle/tests/server/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ def test__maskbits():
sshuttle.server._maskbits(netmask)


@patch('sshuttle.server.which', side_effect=lambda x: x == 'netstat')
@patch('sshuttle.server.ssubprocess.Popen')
def test__listroutes(mock_popen):
def test_listroutes_netstat(mock_popen, mock_which):
mock_pobj = Mock()
mock_pobj.stdout = io.BytesIO(b"""
Kernel IP routing table
Expand All @@ -33,30 +34,20 @@ def test__listroutes(mock_popen):
mock_pobj.wait.return_value = 0
mock_popen.return_value = mock_pobj

routes = sshuttle.server._list_routes()
routes = sshuttle.server.list_routes()

env = {
'PATH': os.environ['PATH'],
'LC_ALL': "C",
}
assert mock_popen.mock_calls == [
call(['netstat', '-rn'], stdout=-1, env=env),
call().wait()
]
assert routes == [
(socket.AF_INET, '0.0.0.0', 0),
assert list(routes) == [
(socket.AF_INET, '192.168.1.0', 24)
]


@patch('sshuttle.server.which', side_effect=lambda x: x == 'ip')
@patch('sshuttle.server.ssubprocess.Popen')
def test_listroutes(mock_popen):
def test_listroutes_iproute(mock_popen, mock_which):
mock_pobj = Mock()
mock_pobj.stdout = io.BytesIO(b"""
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 wlan0
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 wlan0
default via 192.168.1.1 dev wlan0 proto static
192.168.1.0/24 dev wlan0 proto kernel scope link src 192.168.1.1
""")
mock_pobj.wait.return_value = 0
mock_popen.return_value = mock_pobj
Expand Down