Compare commits

..

41 Commits

Author SHA1 Message Date
2c20a1fd5a New release 2016-08-06 18:58:00 +10:00
915f72de35 Add changes for next release 2016-08-06 18:52:26 +10:00
1ffc3f52a1 Merge pull request #108 from vieira/pf-ipv6
IPv6 support for OSX and BSDs
2016-07-29 07:57:35 +10:00
8520ea2787 Use == instead of is to compare with AF_INET 2016-07-27 23:18:25 +00:00
6a394deaf2 Fixes missing comma from tuple in pf tests 2016-07-27 23:06:36 +00:00
83d5c59a57 Tests for IPv6 on pf 2016-07-27 22:17:02 +00:00
1cfd9eb9d7 Be more specific and consistent in some pf rules 2016-07-27 22:15:47 +00:00
f8d58fa4f0 IPv6 support for BSD and OSX
Adds IPv6 support for OpenBSD and OSX.
2016-07-24 22:04:29 +00:00
d2d5a37541 AF_INET6 is different between BSDs and Linux
AF_INET is the same constant on Linux and BSD but AF_INET6
is different. As the client and server can be running on
different platforms we can not just set the socket family
to what comes in the wire.
2016-07-24 22:02:17 +00:00
e9be2deea0 Exclude the IP where sshuttle is really listening
We were always excluding 127.0.0.1/8 but sshuttle might be listening on
other IP, e.g., ::1 for IPv6 or any other defined with -l
2016-07-24 21:58:20 +00:00
22b1b54bfd Add pytest-runner support 2016-07-10 11:26:32 +10:00
a43c668dde Fixes type mismatch between str and bytes
Should fix issue #104.
2016-07-09 22:49:12 +00:00
e0dfb95596 Fix OpenBSD pf test failure 2016-06-17 17:18:43 +08:00
5d28ce8272 Merge pull request #1 from vieira/patch-1
Add <forward_subnets> to divert rule in OpenBSD
2016-06-17 08:25:59 +08:00
f876c5db5e Add <forward_subnets> to divert rule in OpenBSD
Fixes bug where all traffic routed to loopback would end up being diverted to the same port.
2016-06-16 22:34:19 +01:00
2e1beefc9a Hack pf to enable multiple instances in Mac OS X 10.10 and above 2016-06-16 12:31:02 +08:00
5a20783baa tweak docs to match @vieira's changes 2016-05-02 21:40:53 -07:00
495b3c39ea Seed hosts without auto hosts
A possible implementation for the change requested in #94, so that seed
hosts can be used without auto hosts. In this scenario only the
specified hosts (or ips) will be looked up (or rev looked up).
2016-05-03 00:18:32 +00:00
f3cbc5018a Fix PEP8 issues 2016-04-30 18:08:46 +10:00
e73e797f33 Update files list 2016-04-30 18:05:47 +10:00
1d64879613 Fix tests 2016-04-23 13:19:06 +10:00
8fad282bfd Ensure locale is set to C for external commands
Otherwise the output can vary and confuse our attempts to parse it.

Fixes: 93
2016-04-23 12:53:45 +10:00
1dda9dd621 Add ENETUNREACH to NET_ERRS
We shouldn't come up with a fatal error because of a ENETUNREACH when
trying to contact the DNS server. Although this error shouldn't happen
either.

Fixes #89.
2016-04-20 15:18:59 +10:00
74e308a29f Don't mix tab and spaces in shell script
Sometime ago I was in python mode and incorrectly indented a line of the
shell script with spaces instead of tabs. Shame on me. This should bring
things back to their natural order.
2016-04-20 15:17:07 +10:00
516ff7bc4a Correctly obtains the python executable to use
Previously the sshuttle shell script would pass the python to use as the
first argument of the command. The new run script no longer does this.
Instead we can obtain the python being used via sys.executable.
Fixes #88.
2016-04-20 15:15:44 +10:00
89c5b57019 Attempt readthedocs workaround
readthedocs alters docs/conf.py which in turn means python_scm detects a
version and incorrectly adjusts the version number. Here we try to work
around this problem.

We do this by renaming the docs/conf.py file and copying it back to
docs/conf.py when setup.py is invoked. This way, hopefully, scm won't
see the changes to docs/conf.py

References:
http://stackoverflow.com/questions/35811267/readthedocs-and-setuptools-scm-version-wrong/36386177
https://github.com/pypa/setuptools_scm/issues/84
2016-04-18 11:44:05 +10:00
a8b288338b Release version 0.78.0 2016-04-08 12:01:37 +10:00
6fd02933dd Revert "Test for RTD"
This reverts commit 7ea2d973c7.
2016-04-08 12:00:45 +10:00
7ea2d973c7 Test for RTD 2016-04-06 11:28:41 +10:00
8ec5d1a5ac Update changes
In preparation for new release.
2016-04-05 21:14:50 +10:00
4241381d82 Backward compatibility with Python 2.4 (server)
It is often the case that the user has no administrative control over
the server that is being used. As such it is important to support as
many versions as possible, at least on the remote server end. These
fixes will allow sshuttle to be used with servers that have only
python 2.4 or python 2.6 installed while hopefully not breaking the
compatibility with 2.7 and 3.5.
2016-04-03 13:14:02 +10:00
6e15e69029 Support multiple subnet files (multiple -s options)
When passing multiple subnet files, e.g., by using -s/--subnets
multiple times or by using it together with subnets passed as positional
arguments append the content from all sources instead of only using the
subnets from the last source. This makes the behaviour of -s/--subnets
consistent with -x/--exclude.
2016-03-31 11:46:12 +11:00
8fa45885cc Remove --server option
As @brianmay observed in #82 this option is no longer used and can be
dropped.
2016-03-28 22:01:54 +00:00
b8160c4a37 Fix pep8 issues 2016-03-22 13:19:32 +11:00
05bacf6fd6 Use argparse for command line options
Fixes the kind of problems reported on #75 but does break the command
line "API" (hopefully).
2016-03-22 13:12:59 +11:00
dea3f21943 Write more server tests 2016-03-16 18:24:43 +11:00
d522d1e1bd Split client/server tests
This allows disabling all client tests using a conftest.py file, if for
example #56 gets merged and the server supports more python versions
then the server.

The server side tests are very incomplete.
2016-03-16 17:40:48 +11:00
3541e4bdfe Fix shell quoting
Due to nested shells, we need to have multiple layers of quoting. Yuck.

Closes #80
2016-03-16 16:38:22 +11:00
efdb9b8f94 If 3.5 not available, try to fallback to 2.7
In situations where 2.7 is available and some unsupported 3.x is the
system's default we should probably fallback to 2.7 instead of the
default (that might be e.g. 3.4). This might fix #78.
2016-03-16 16:16:53 +11:00
7875d1b97a Explicitly call /bin/sh for compatibility with non POSIX shells.
The fish shell doesn’t support ‘||’ and requires a ‘—python python’
workaround.  This change explicitly calls /bin/sh for the remote shell
commands.
2016-03-08 15:30:59 -08:00
2b0d0065c7 Don't force IPv6 if IPv6 name servers
Just because we may have found IPv6 DNS servers from /etc/resolv.conf
doesn't mean we should force IPv6 support.

Instead we should disable the IPv6 DNS servers if IPv6 is disabled.

Note: this will also result in any IPv6 servers specified on the command
line being silently ignored too.

Specifying an IPv6 subnet will still require IPv6 support.

Closes #74
2016-03-08 18:49:47 +11:00
30 changed files with 807 additions and 560 deletions

11
.gitignore vendored
View File

@ -1,4 +1,13 @@
sshuttle/version.py /sshuttle/version.py
/docs/conf.py
/tmp/
/.cache/
/.eggs/
/.tox/
/build/
/dist/
/sshuttle.egg-info/
/docs/_build/
*.pyc *.pyc
*~ *~
*.8 *.8

View File

@ -1,6 +1,8 @@
language: python language: python
python: python:
- 2.6
- 2.7 - 2.7
- 3.4
- 3.5 - 3.5
- pypy - pypy

View File

@ -1,3 +1,22 @@
Release 0.78.1 (6th August, 2016)
=================================
* Fix readthedocs versioning.
* Don't crash on ENETUNREACH.
* Various bug fixes.
* Improvements to BSD and OSX support.
Release 0.78.0 (Apr 8, 2016)
============================
* Don't force IPv6 if IPv6 nameservers supplied. Fixes #74.
* Call /bin/sh as users shell may not be POSIX compliant. Fixes #77.
* Use argparse for command line processing. Fixes #75.
* Remove useless --server option.
* Support multiple -s (subnet) options. Fixes #86.
* Make server parts work with old versions of Python. Fixes #81.
Release 0.77.2 (Mar 7, 2016) Release 0.77.2 (Mar 7, 2016)
============================ ============================

View File

@ -6,8 +6,10 @@ include LICENSE
include run include run
include tox.ini include tox.ini
exclude sshuttle/version.py exclude sshuttle/version.py
exclude docs/conf.py
recursive-include docs *.bat recursive-include docs *.bat
recursive-include docs *.py recursive-include docs *.py
recursive-include docs *.rst recursive-include docs *.rst
recursive-include docs Makefile recursive-include docs Makefile
recursive-include sshuttle *.py recursive-include sshuttle *.py
recursive-exclude docs/_build *

10
conftest.py Normal file
View File

@ -0,0 +1,10 @@
import sys
if sys.version_info >= (3, 0):
good_python = sys.version_info >= (3, 5)
else:
good_python = sys.version_info >= (2, 7)
collect_ignore = []
if not good_python:
collect_ignore.append("sshuttle/tests/client")

View File

@ -15,6 +15,7 @@
# import sys # import sys
# import os # import os
from setuptools_scm import get_version
# If extensions (or modules to document with autodoc) are in another directory, # If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the # add these directories to sys.path here. If the directory is relative to the
@ -54,7 +55,6 @@ copyright = '2016, Brian May'
# built documents. # built documents.
# #
# The short X.Y version. # The short X.Y version.
from setuptools_scm import get_version
version = get_version(root="..") version = get_version(root="..")
# The full version, including alpha/beta/rc tags. # The full version, including alpha/beta/rc tags.
release = version release = version

View File

@ -136,6 +136,10 @@ Options
if you use this option to give it a few names to start if you use this option to give it a few names to start
from. from.
If this option is used *without* :option:`--auto-hosts`,
then the listed hostnames will be scanned and added, but
no further hostnames will be added.
.. option:: --no-latency-control .. option:: --no-latency-control
Sacrifice latency to improve bandwidth benchmarks. ssh Sacrifice latency to improve bandwidth benchmarks. ssh

View File

@ -62,6 +62,7 @@ cmd.exe with Administrator access. See :doc:`windows` for more information.
Server side Requirements Server side Requirements
------------------------ ------------------------
Server requirements are more relaxed, however it is recommended that you use
Python 2.7 or Python 3.5. Python 2.7 or Python 3.5.

2
run
View File

@ -1,6 +1,8 @@
#!/bin/sh #!/bin/sh
if python3.5 -V 2>/dev/null; then if python3.5 -V 2>/dev/null; then
exec python3.5 -m "sshuttle" "$@" exec python3.5 -m "sshuttle" "$@"
elif python2.7 -V 2>/dev/null; then
exec python2.7 -m "sshuttle" "$@"
else else
exec python -m "sshuttle" "$@" exec python -m "sshuttle" "$@"
fi fi

2
setup.cfg Normal file
View File

@ -0,0 +1,2 @@
[aliases]
test=pytest

View File

@ -18,7 +18,12 @@
# along with python-tldap If not, see <http://www.gnu.org/licenses/>. # along with python-tldap If not, see <http://www.gnu.org/licenses/>.
from setuptools import setup, find_packages from setuptools import setup, find_packages
import shutil
with open("./docs/conf.orig.py", "r") as src:
with open("./docs/conf.py", "w") as dst:
dst.write("# FILE COPIED FROM conf.orig.py; DO NOT CHANGE\n")
shutil.copyfileobj(src, dst)
def version_scheme(version): def version_scheme(version):
from setuptools_scm.version import guess_next_dev_version from setuptools_scm.version import guess_next_dev_version
@ -31,7 +36,7 @@ setup(
'write_to': "sshuttle/version.py", 'write_to': "sshuttle/version.py",
'version_scheme': version_scheme, 'version_scheme': version_scheme,
}, },
setup_requires=['setuptools_scm'], setup_requires=['setuptools_scm', 'pytest-runner'],
# version=version, # version=version,
url='https://github.com/sshuttle/sshuttle', url='https://github.com/sshuttle/sshuttle',
author='Brian May', author='Brian May',

View File

@ -0,0 +1,4 @@
try:
from sshuttle.version import version as __version__
except ImportError:
__version__ = "unknown"

View File

@ -4,19 +4,20 @@ import imp
z = zlib.decompressobj() z = zlib.decompressobj()
while 1: while 1:
name = stdin.readline().strip() name = sys.stdin.readline().strip()
if name: if name:
name = name.decode("ASCII") name = name.decode("ASCII")
nbytes = int(stdin.readline()) nbytes = int(sys.stdin.readline())
if verbosity >= 2: if verbosity >= 2:
sys.stderr.write('server: assembling %r (%d bytes)\n' sys.stderr.write('server: assembling %r (%d bytes)\n'
% (name, nbytes)) % (name, nbytes))
content = z.decompress(stdin.read(nbytes)) content = z.decompress(sys.stdin.read(nbytes))
module = imp.new_module(name) module = imp.new_module(name)
parent, _, parent_name = name.rpartition(".") parents = name.rsplit(".", 1)
if parent != "": if len(parents) == 2:
parent, parent_name = parents
setattr(sys.modules[parent], parent_name, module) setattr(sys.modules[parent], parent_name, module)
code = compile(content, name, "exec") code = compile(content, name, "exec")
@ -33,4 +34,4 @@ sshuttle.helpers.verbose = verbosity
import sshuttle.cmdline_options as options import sshuttle.cmdline_options as options
from sshuttle.server import main from sshuttle.server import main
main(options.latency_control) main(options.latency_control, options.auto_hosts)

View File

@ -400,7 +400,7 @@ def ondns(listener, method, mux, handlers):
def _main(tcp_listener, udp_listener, fw, ssh_cmd, remotename, def _main(tcp_listener, udp_listener, fw, ssh_cmd, remotename,
python, latency_control, python, latency_control,
dns_listener, seed_hosts, auto_nets, daemon): dns_listener, seed_hosts, auto_hosts, auto_nets, daemon):
debug1('Starting client with Python version %s\n' debug1('Starting client with Python version %s\n'
% platform.python_version()) % platform.python_version())
@ -418,7 +418,8 @@ def _main(tcp_listener, udp_listener, fw, ssh_cmd, remotename,
(serverproc, serversock) = ssh.connect( (serverproc, serversock) = ssh.connect(
ssh_cmd, remotename, python, ssh_cmd, remotename, python,
stderr=ssyslog._p and ssyslog._p.stdin, stderr=ssyslog._p and ssyslog._p.stdin,
options=dict(latency_control=latency_control)) options=dict(latency_control=latency_control,
auto_hosts=auto_hosts))
except socket.error as e: except socket.error as e:
if e.args[0] == errno.EPIPE: if e.args[0] == errno.EPIPE:
raise Fatal("failed to establish ssh session (1)") raise Fatal("failed to establish ssh session (1)")
@ -514,7 +515,7 @@ def _main(tcp_listener, udp_listener, fw, ssh_cmd, remotename,
def main(listenip_v6, listenip_v4, def main(listenip_v6, listenip_v4,
ssh_cmd, remotename, python, latency_control, dns, nslist, ssh_cmd, remotename, python, latency_control, dns, nslist,
method_name, seed_hosts, auto_nets, method_name, seed_hosts, auto_hosts, auto_nets,
subnets_include, subnets_exclude, daemon, pidfile): subnets_include, subnets_exclude, daemon, pidfile):
if daemon: if daemon:
@ -547,11 +548,16 @@ def main(listenip_v6, listenip_v4,
else: else:
listenip_v6 = None listenip_v6 = None
required.ipv6 = len(subnets_v6) > 0 or len(nslist_v6) > 0 \ required.ipv6 = len(subnets_v6) > 0 or listenip_v6 is not None
or listenip_v6 is not None required.ipv4 = len(subnets_v4) > 0 or listenip_v4 is not None
required.udp = avail.udp required.udp = avail.udp
required.dns = len(nslist) > 0 required.dns = len(nslist) > 0
# if IPv6 not supported, ignore IPv6 DNS servers
if not required.ipv6:
nslist_v6 = []
nslist = nslist_v4
fw.method.assert_features(required) fw.method.assert_features(required)
if required.ipv6 and listenip_v6 is None: if required.ipv6 and listenip_v6 is None:
@ -566,6 +572,14 @@ def main(listenip_v6, listenip_v4,
if listenip_v4 == "auto": if listenip_v4 == "auto":
listenip_v4 = ('127.0.0.1', 0) listenip_v4 = ('127.0.0.1', 0)
if required.ipv4 and \
not any(listenip_v4[0] == sex[1] for sex in subnets_v4):
subnets_exclude.append((socket.AF_INET, listenip_v4[0], 32))
if required.ipv6 and \
not any(listenip_v6[0] == sex[1] for sex in subnets_v6):
subnets_exclude.append((socket.AF_INET6, listenip_v6[0], 128))
if listenip_v6 and listenip_v6[1] and listenip_v4 and listenip_v4[1]: if listenip_v6 and listenip_v6[1] and listenip_v4 and listenip_v4[1]:
# if both ports given, no need to search for a spare port # if both ports given, no need to search for a spare port
ports = [0, ] ports = [0, ]
@ -709,7 +723,7 @@ def main(listenip_v6, listenip_v4,
try: try:
return _main(tcp_listener, udp_listener, fw, ssh_cmd, remotename, return _main(tcp_listener, udp_listener, fw, ssh_cmd, remotename,
python, latency_control, dns_listener, python, latency_control, dns_listener,
seed_hosts, auto_nets, daemon) seed_hosts, auto_hosts, auto_nets, daemon)
finally: finally:
try: try:
if daemon: if daemon:

View File

@ -1,199 +1,46 @@
import sys
import re import re
import socket
import sshuttle.helpers as helpers import sshuttle.helpers as helpers
import sshuttle.options as options
import sshuttle.client as client import sshuttle.client as client
import sshuttle.firewall as firewall import sshuttle.firewall as firewall
import sshuttle.hostwatch as hostwatch import sshuttle.hostwatch as hostwatch
import sshuttle.ssyslog as ssyslog import sshuttle.ssyslog as ssyslog
from sshuttle.options import parser, parse_ipport6, parse_ipport4
from sshuttle.helpers import family_ip_tuple, log, Fatal from sshuttle.helpers import family_ip_tuple, log, Fatal
# 1.2.3.4/5 or just 1.2.3.4
def parse_subnet4(s):
m = re.match(r'(\d+)(?:\.(\d+)\.(\d+)\.(\d+))?(?:/(\d+))?$', s)
if not m:
raise Fatal('%r is not a valid IP subnet format' % s)
(a, b, c, d, width) = m.groups()
(a, b, c, d) = (int(a or 0), int(b or 0), int(c or 0), int(d or 0))
if width is None:
width = 32
else:
width = int(width)
if a > 255 or b > 255 or c > 255 or d > 255:
raise Fatal('%d.%d.%d.%d has numbers > 255' % (a, b, c, d))
if width > 32:
raise Fatal('*/%d is greater than the maximum of 32' % width)
return(socket.AF_INET, '%d.%d.%d.%d' % (a, b, c, d), width)
# 1:2::3/64 or just 1:2::3
def parse_subnet6(s):
m = re.match(r'(?:([a-fA-F\d:]+))?(?:/(\d+))?$', s)
if not m:
raise Fatal('%r is not a valid IP subnet format' % s)
(net, width) = m.groups()
if width is None:
width = 128
else:
width = int(width)
if width > 128:
raise Fatal('*/%d is greater than the maximum of 128' % width)
return(socket.AF_INET6, net, width)
# Subnet file, supporting empty lines and hash-started comment lines
def parse_subnet_file(s):
try:
handle = open(s, 'r')
except OSError:
raise Fatal('Unable to open subnet file: %s' % s)
raw_config_lines = handle.readlines()
config_lines = []
for line_no, line in enumerate(raw_config_lines):
line = line.strip()
if len(line) == 0:
continue
if line[0] == '#':
continue
config_lines.append(line)
return config_lines
# list of:
# 1.2.3.4/5 or just 1.2.3.4
# 1:2::3/64 or just 1:2::3
def parse_subnets(subnets_str):
subnets = []
for s in subnets_str:
if ':' in s:
subnet = parse_subnet6(s)
else:
subnet = parse_subnet4(s)
subnets.append(subnet)
return subnets
# 1.2.3.4:567 or just 1.2.3.4 or just 567
def parse_ipport4(s):
s = str(s)
m = re.match(r'(?:(\d+)\.(\d+)\.(\d+)\.(\d+))?(?::)?(?:(\d+))?$', s)
if not m:
raise Fatal('%r is not a valid IP:port format' % s)
(a, b, c, d, port) = m.groups()
(a, b, c, d, port) = (int(a or 0), int(b or 0), int(c or 0), int(d or 0),
int(port or 0))
if a > 255 or b > 255 or c > 255 or d > 255:
raise Fatal('%d.%d.%d.%d has numbers > 255' % (a, b, c, d))
if port > 65535:
raise Fatal('*:%d is greater than the maximum of 65535' % port)
if a is None:
a = b = c = d = 0
return ('%d.%d.%d.%d' % (a, b, c, d), port)
# [1:2::3]:456 or [1:2::3] or 456
def parse_ipport6(s):
s = str(s)
m = re.match(r'(?:\[([^]]*)])?(?::)?(?:(\d+))?$', s)
if not m:
raise Fatal('%s is not a valid IP:port format' % s)
(ip, port) = m.groups()
(ip, port) = (ip or '::', int(port or 0))
return (ip, port)
def parse_list(list):
return re.split(r'[\s,]+', list.strip()) if list else []
optspec = """
sshuttle [-l [ip:]port] [-r [username@]sshserver[:port]] <subnets...>
sshuttle --firewall <port> <subnets...>
sshuttle --hostwatch
--
l,listen= transproxy to this ip address and port number
H,auto-hosts scan for remote hostnames and update local /etc/hosts
N,auto-nets automatically determine subnets to route
dns capture local DNS requests and forward to the remote DNS server
ns-hosts= capture and forward remote DNS requests to the following servers
method= auto, nat, tproxy or pf
python= path to python interpreter on the remote server
r,remote= ssh hostname (and optional username) of remote sshuttle server
x,exclude= exclude this subnet (can be used more than once)
X,exclude-from= exclude the subnets in a file (whitespace separated)
v,verbose increase debug message verbosity
V,version print the sshuttle version number and exit
e,ssh-cmd= the command to use to connect to the remote [ssh]
seed-hosts= with -H, use these hostnames for initial scan (comma-separated)
no-latency-control sacrifice latency to improve bandwidth benchmarks
wrap= restart counting channel numbers after this number (for testing)
disable-ipv6 disables ipv6 support
D,daemon run in the background as a daemon
s,subnets= file where the subnets are stored, instead of on the command line
syslog send log messages to syslog (default if you use --daemon)
pidfile= pidfile name (only if using --daemon) [./sshuttle.pid]
server (internal use only)
firewall (internal use only)
hostwatch (internal use only)
"""
def main(): def main():
o = options.Options(optspec) opt = parser.parse_args()
(opt, flags, extra) = o.parse(sys.argv[1:])
if opt.version:
from sshuttle.version import version
print(version)
return 0
if opt.daemon: if opt.daemon:
opt.syslog = 1 opt.syslog = 1
if opt.wrap: if opt.wrap:
import sshuttle.ssnet as ssnet import sshuttle.ssnet as ssnet
ssnet.MAX_CHANNEL = int(opt.wrap) ssnet.MAX_CHANNEL = opt.wrap
helpers.verbose = opt.verbose or 0 helpers.verbose = opt.verbose
try: try:
if opt.firewall: if opt.firewall:
if len(extra) != 0: if opt.subnets:
o.fatal('exactly zero arguments expected') parser.error('exactly zero arguments expected')
return firewall.main(opt.method, opt.syslog) return firewall.main(opt.method, opt.syslog)
elif opt.hostwatch: elif opt.hostwatch:
return hostwatch.hw_main(extra) return hostwatch.hw_main(opt.subnets)
else: else:
if len(extra) < 1 and not opt.auto_nets and not opt.subnets: includes = opt.subnets
o.fatal('at least one subnet, subnet file, or -N expected') excludes = opt.exclude
includes = extra if not includes and not opt.auto_nets:
excludes = ['127.0.0.0/8'] parser.error('at least one subnet, subnet file, '
for k, v in flags: 'or -N expected')
if k in ('-x', '--exclude'):
excludes.append(v)
if k in ('-X', '--exclude-from'):
excludes += open(v).read().split()
remotename = opt.remote remotename = opt.remote
if remotename == '' or remotename == '-': if remotename == '' or remotename == '-':
remotename = None remotename = None
nslist = [family_ip_tuple(ns) for ns in parse_list(opt.ns_hosts)] nslist = [family_ip_tuple(ns) for ns in opt.ns_hosts]
if opt.seed_hosts and not opt.auto_hosts:
o.fatal('--seed-hosts only works if you also use -H')
if opt.seed_hosts: if opt.seed_hosts:
sh = re.split(r'[\s,]+', (opt.seed_hosts or "").strip()) sh = re.split(r'[\s,]+', (opt.seed_hosts or "").strip())
elif opt.auto_hosts: elif opt.auto_hosts:
sh = [] sh = []
else: else:
sh = None sh = None
if opt.subnets:
includes = parse_subnet_file(opt.subnets)
if not opt.method:
method_name = "auto"
elif opt.method in ["auto", "nat", "tproxy", "pf"]:
method_name = opt.method
else:
o.fatal("method_name %s not supported" % opt.method)
if opt.listen: if opt.listen:
ipport_v6 = None ipport_v6 = None
ipport_v4 = None ipport_v4 = None
@ -218,11 +65,12 @@ def main():
opt.latency_control, opt.latency_control,
opt.dns, opt.dns,
nslist, nslist,
method_name, opt.method,
sh, sh,
opt.auto_hosts,
opt.auto_nets, opt.auto_nets,
parse_subnets(includes), includes,
parse_subnets(excludes), excludes,
opt.daemon, opt.pidfile) opt.daemon, opt.pidfile)
if return_code == 0: if return_code == 0:

View File

@ -5,6 +5,17 @@ import errno
logprefix = '' logprefix = ''
verbose = 0 verbose = 0
if sys.version_info[0] == 3:
binary_type = bytes
def b(s):
return s.encode("ASCII")
else:
binary_type = str
def b(s):
return s
def log(s): def log(s):
global logprefix global logprefix
@ -70,7 +81,8 @@ def islocal(ip, family):
try: try:
try: try:
sock.bind((ip, 0)) sock.bind((ip, 0))
except socket.error as e: except socket.error:
_, e = sys.exc_info()[:2]
if e.args[0] == errno.EADDRNOTAVAIL: if e.args[0] == errno.EADDRNOTAVAIL:
return False # not a local IP return False # not a local IP
else: else:

View File

@ -22,7 +22,8 @@ hostnames = {}
queue = {} queue = {}
try: try:
null = open('/dev/null', 'wb') null = open('/dev/null', 'wb')
except IOError as e: except IOError:
_, e = sys.exc_info()[:2]
log('warning: %s\n' % e) log('warning: %s\n' % e)
null = os.popen("sh -c 'while read x; do :; done'", 'wb', 4096) null = os.popen("sh -c 'while read x; do :; done'", 'wb', 4096)
@ -38,7 +39,7 @@ def write_host_cache():
for name, ip in sorted(hostnames.items()): for name, ip in sorted(hostnames.items()):
f.write(('%s,%s\n' % (name, ip)).encode("ASCII")) f.write(('%s,%s\n' % (name, ip)).encode("ASCII"))
f.close() f.close()
os.chmod(tmpname, 0o600) os.chmod(tmpname, 384) # 600 in octal, 'rw-------'
os.rename(tmpname, CACHEFILE) os.rename(tmpname, CACHEFILE)
finally: finally:
try: try:
@ -50,7 +51,8 @@ def write_host_cache():
def read_host_cache(): def read_host_cache():
try: try:
f = open(CACHEFILE) f = open(CACHEFILE)
except IOError as e: except IOError:
_, e = sys.exc_info()[:2]
if e.errno == errno.ENOENT: if e.errno == errno.ENOENT:
return return
else: else:
@ -68,8 +70,8 @@ def read_host_cache():
def found_host(hostname, ip): def found_host(hostname, ip):
hostname = re.sub(r'\..*', '', hostname) hostname = re.sub(r'\..*', '', hostname)
hostname = re.sub(r'[^-\w]', '_', hostname) hostname = re.sub(r'[^-\w]', '_', hostname)
if (ip.startswith('127.') or ip.startswith('255.') if (ip.startswith('127.') or ip.startswith('255.') or
or hostname == 'localhost'): hostname == 'localhost'):
return return
oldip = hostnames.get(hostname) oldip = hostnames.get(hostname)
if oldip != ip: if oldip != ip:
@ -124,7 +126,8 @@ def _check_netstat():
p = ssubprocess.Popen(argv, stdout=ssubprocess.PIPE, stderr=null) p = ssubprocess.Popen(argv, stdout=ssubprocess.PIPE, stderr=null)
content = p.stdout.read().decode("ASCII") content = p.stdout.read().decode("ASCII")
p.wait() p.wait()
except OSError as e: except OSError:
_, e = sys.exc_info()[:2]
log('%r failed: %r\n' % (argv, e)) log('%r failed: %r\n' % (argv, e))
return return
@ -144,7 +147,8 @@ def _check_smb(hostname):
p = ssubprocess.Popen(argv, stdout=ssubprocess.PIPE, stderr=null) p = ssubprocess.Popen(argv, stdout=ssubprocess.PIPE, stderr=null)
lines = p.stdout.readlines() lines = p.stdout.readlines()
p.wait() p.wait()
except OSError as e: except OSError:
_, e = sys.exc_info()[:2]
log('%r failed: %r\n' % (argv, e)) log('%r failed: %r\n' % (argv, e))
_smb_ok = False _smb_ok = False
return return
@ -201,7 +205,8 @@ def _check_nmb(hostname, is_workgroup, is_master):
p = ssubprocess.Popen(argv, stdout=ssubprocess.PIPE, stderr=null) p = ssubprocess.Popen(argv, stdout=ssubprocess.PIPE, stderr=null)
lines = p.stdout.readlines() lines = p.stdout.readlines()
rv = p.wait() rv = p.wait()
except OSError as e: except OSError:
_, e = sys.exc_info()[:2]
log('%r failed: %r\n' % (argv, e)) log('%r failed: %r\n' % (argv, e))
_nmb_ok = False _nmb_ok = False
return return
@ -250,7 +255,7 @@ def _stdin_still_ok(timeout):
return True return True
def hw_main(seed_hosts): def hw_main(seed_hosts, auto_hosts):
if helpers.verbose >= 2: if helpers.verbose >= 2:
helpers.logprefix = 'HH: ' helpers.logprefix = 'HH: '
else: else:
@ -259,17 +264,18 @@ def hw_main(seed_hosts):
debug1('Starting hostwatch with Python version %s\n' debug1('Starting hostwatch with Python version %s\n'
% platform.python_version()) % platform.python_version())
read_host_cache()
_enqueue(_check_etc_hosts)
_enqueue(_check_netstat)
check_host('localhost')
check_host(socket.gethostname())
check_workgroup('workgroup')
check_workgroup('-')
for h in seed_hosts: for h in seed_hosts:
check_host(h) check_host(h)
if auto_hosts:
read_host_cache()
_enqueue(_check_etc_hosts)
_enqueue(_check_netstat)
check_host('localhost')
check_host(socket.gethostname())
check_workgroup('workgroup')
check_workgroup('-')
while 1: while 1:
now = time.time() now = time.time()
for t, last_polled in list(queue.items()): for t, last_polled in list(queue.items()):

View File

@ -1,3 +1,4 @@
import os
import socket import socket
import subprocess as ssubprocess import subprocess as ssubprocess
from sshuttle.helpers import log, debug1, Fatal, family_to_string from sshuttle.helpers import log, debug1, Fatal, family_to_string
@ -18,7 +19,11 @@ def ipt_chain_exists(family, table, name):
else: else:
raise Exception('Unsupported family "%s"' % family_to_string(family)) raise Exception('Unsupported family "%s"' % family_to_string(family))
argv = [cmd, '-t', table, '-nL'] argv = [cmd, '-t', table, '-nL']
p = ssubprocess.Popen(argv, stdout=ssubprocess.PIPE) env = {
'PATH': os.environ['PATH'],
'LC_ALL': "C",
}
p = ssubprocess.Popen(argv, stdout=ssubprocess.PIPE, env=env)
for line in p.stdout: for line in p.stdout:
if line.startswith(b'Chain %s ' % name.encode("ASCII")): if line.startswith(b'Chain %s ' % name.encode("ASCII")):
return True return True
@ -35,7 +40,11 @@ def ipt(family, table, *args):
else: else:
raise Exception('Unsupported family "%s"' % family_to_string(family)) raise Exception('Unsupported family "%s"' % family_to_string(family))
debug1('>> %s\n' % ' '.join(argv)) debug1('>> %s\n' % ' '.join(argv))
rv = ssubprocess.call(argv) env = {
'PATH': os.environ['PATH'],
'LC_ALL': "C",
}
rv = ssubprocess.call(argv, env=env)
if rv: if rv:
raise Fatal('%r returned %d' % (argv, rv)) raise Fatal('%r returned %d' % (argv, rv))

View File

@ -11,7 +11,7 @@ from sshuttle.helpers import debug1, debug2, debug3, Fatal, family_to_string
from sshuttle.methods import BaseMethod from sshuttle.methods import BaseMethod
_pf_context = {'started_by_sshuttle': False, 'Xtoken': None} _pf_context = {'started_by_sshuttle': False, 'Xtoken': []}
_pf_fd = None _pf_fd = None
@ -59,7 +59,8 @@ class Generic(object):
pfctl('-e') pfctl('-e')
_pf_context['started_by_sshuttle'] = True _pf_context['started_by_sshuttle'] = True
def disable(self): def disable(self, anchor):
pfctl('-a %s -F all' % anchor)
if _pf_context['started_by_sshuttle']: if _pf_context['started_by_sshuttle']:
pfctl('-d') pfctl('-d')
@ -96,12 +97,12 @@ class Generic(object):
def _get_natlook_port(self, xport): def _get_natlook_port(self, xport):
return xport return xport
def add_anchors(self, status=None): def add_anchors(self, anchor, status=None):
if status is None: if status is None:
status = pfctl('-s all')[0] status = pfctl('-s all')[0]
self.status = status self.status = status
if b'\nanchor "sshuttle"' not in status: if ('\nanchor "%s"' % anchor).encode('ASCII') not in status:
self._add_anchor_rule(self.PF_PASS, b"sshuttle") self._add_anchor_rule(self.PF_PASS, anchor.encode('ASCII'))
def _add_anchor_rule(self, type, name, pr=None): def _add_anchor_rule(self, type, name, pr=None):
if pr is None: if pr is None:
@ -120,10 +121,20 @@ class Generic(object):
'I', self.PF_CHANGE_ADD_TAIL), 4) # action = PF_CHANGE_ADD_TAIL 'I', self.PF_CHANGE_ADD_TAIL), 4) # action = PF_CHANGE_ADD_TAIL
ioctl(pf_get_dev(), pf.DIOCCHANGERULE, pr) ioctl(pf_get_dev(), pf.DIOCCHANGERULE, pr)
def add_rules(self, rules): def _inet_version(self, family):
return b'inet' if family == socket.AF_INET else b'inet6'
def _lo_addr(self, family):
return b'127.0.0.1' if family == socket.AF_INET else b'::1'
def add_rules(self, anchor, rules):
assert isinstance(rules, bytes) assert isinstance(rules, bytes)
debug3("rules:\n" + rules.decode("ASCII")) debug3("rules:\n" + rules.decode("ASCII"))
pfctl('-a sshuttle -f /dev/stdin', rules) pfctl('-a %s -f /dev/stdin' % anchor, rules)
def has_skip_loopback(self):
return b'skip' in pfctl('-s Interfaces -i lo -v')[0]
class FreeBsd(Generic): class FreeBsd(Generic):
@ -153,11 +164,11 @@ class FreeBsd(Generic):
def __init__(self): def __init__(self):
super(FreeBsd, self).__init__() super(FreeBsd, self).__init__()
def add_anchors(self): def add_anchors(self, anchor):
status = pfctl('-s all')[0] status = pfctl('-s all')[0]
if b'\nrdr-anchor "sshuttle"' not in status: if ('\nrdr-anchor "%s"' % anchor).encode('ASCII') not in status:
self._add_anchor_rule(self.PF_RDR, b'sshuttle') self._add_anchor_rule(self.PF_RDR, anchor.encode('ASCII'))
super(FreeBsd, self).add_anchors(status=status) super(FreeBsd, self).add_anchors(anchor, status=status)
def _add_anchor_rule(self, type, name): def _add_anchor_rule(self, type, name):
pr = self.pfioc_rule() pr = self.pfioc_rule()
@ -168,17 +179,20 @@ class FreeBsd(Generic):
memmove(addressof(pr) + self.POOL_TICKET_OFFSET, ppa[4:8], 4) memmove(addressof(pr) + self.POOL_TICKET_OFFSET, ppa[4:8], 4)
super(FreeBsd, self)._add_anchor_rule(type, name, pr=pr) super(FreeBsd, self)._add_anchor_rule(type, name, pr=pr)
def add_rules(self, includes, port, dnsport, nslist): def add_rules(self, anchor, includes, port, dnsport, nslist, family):
inet_version = self._inet_version(family)
lo_addr = self._lo_addr(family)
tables = [ tables = [
b'table <forward_subnets> {%s}' % b','.join(includes) b'table <forward_subnets> {%s}' % b','.join(includes)
] ]
translating_rules = [ translating_rules = [
b'rdr pass on lo0 proto tcp ' b'rdr pass on lo0 %s proto tcp to <forward_subnets> '
b'to <forward_subnets> -> 127.0.0.1 port %r' % port b'-> %s port %r' % (inet_version, lo_addr, port)
] ]
filtering_rules = [ filtering_rules = [
b'pass out route-to lo0 inet proto tcp ' b'pass out route-to lo0 %s proto tcp '
b'to <forward_subnets> keep state' b'to <forward_subnets> keep state' % inet_version
] ]
if len(nslist) > 0: if len(nslist) > 0:
@ -186,16 +200,16 @@ class FreeBsd(Generic):
b'table <dns_servers> {%s}' % b'table <dns_servers> {%s}' %
b','.join([ns[1].encode("ASCII") for ns in nslist])) b','.join([ns[1].encode("ASCII") for ns in nslist]))
translating_rules.append( translating_rules.append(
b'rdr pass on lo0 proto udp to ' b'rdr pass on lo0 %s proto udp to <dns_servers> '
b'<dns_servers> port 53 -> 127.0.0.1 port %r' % dnsport) b'port 53 -> %s port %r' % (inet_version, lo_addr, dnsport))
filtering_rules.append( filtering_rules.append(
b'pass out route-to lo0 inet proto udp to ' b'pass out route-to lo0 %s proto udp to '
b'<dns_servers> port 53 keep state') b'<dns_servers> port 53 keep state' % inet_version)
rules = b'\n'.join(tables + translating_rules + filtering_rules) \ rules = b'\n'.join(tables + translating_rules + filtering_rules) \
+ b'\n' + b'\n'
super(FreeBsd, self).add_rules(rules) super(FreeBsd, self).add_rules(anchor, rules)
class OpenBsd(Generic): class OpenBsd(Generic):
@ -225,24 +239,28 @@ class OpenBsd(Generic):
self.pfioc_natlook = pfioc_natlook self.pfioc_natlook = pfioc_natlook
super(OpenBsd, self).__init__() super(OpenBsd, self).__init__()
def add_anchors(self): def add_anchors(self, anchor):
# before adding anchors and rules we must override the skip lo # before adding anchors and rules we must override the skip lo
# that comes by default in openbsd pf.conf so the rules we will add, # that comes by default in openbsd pf.conf so the rules we will add,
# which rely on translating/filtering packets on lo, can work # which rely on translating/filtering packets on lo, can work
pfctl('-f /dev/stdin', b'match on lo\n') if self.has_skip_loopback():
super(OpenBsd, self).add_anchors() pfctl('-f /dev/stdin', b'match on lo\n')
super(OpenBsd, self).add_anchors(anchor)
def add_rules(self, anchor, includes, port, dnsport, nslist, family):
inet_version = self._inet_version(family)
lo_addr = self._lo_addr(family)
def add_rules(self, includes, port, dnsport, nslist):
tables = [ tables = [
b'table <forward_subnets> {%s}' % b','.join(includes) b'table <forward_subnets> {%s}' % b','.join(includes)
] ]
translating_rules = [ translating_rules = [
b'pass in on lo0 inet proto tcp ' b'pass in on lo0 %s proto tcp to <forward_subnets> '
b'divert-to 127.0.0.1 port %r' % port b'divert-to %s port %r' % (inet_version, lo_addr, port)
] ]
filtering_rules = [ filtering_rules = [
b'pass out inet proto tcp ' b'pass out %s proto tcp to <forward_subnets> '
b'to <forward_subnets> route-to lo0 keep state' b'route-to lo0 keep state' % inet_version
] ]
if len(nslist) > 0: if len(nslist) > 0:
@ -250,16 +268,16 @@ class OpenBsd(Generic):
b'table <dns_servers> {%s}' % b'table <dns_servers> {%s}' %
b','.join([ns[1].encode("ASCII") for ns in nslist])) b','.join([ns[1].encode("ASCII") for ns in nslist]))
translating_rules.append( translating_rules.append(
b'pass in on lo0 inet proto udp to <dns_servers>' b'pass in on lo0 %s proto udp to <dns_servers> port 53 '
b'port 53 rdr-to 127.0.0.1 port %r' % dnsport) b'rdr-to %s port %r' % (inet_version, lo_addr, dnsport))
filtering_rules.append( filtering_rules.append(
b'pass out inet proto udp to ' b'pass out %s proto udp to <dns_servers> port 53 '
b'<dns_servers> port 53 route-to lo0 keep state') b'route-to lo0 keep state' % inet_version)
rules = b'\n'.join(tables + translating_rules + filtering_rules) \ rules = b'\n'.join(tables + translating_rules + filtering_rules) \
+ b'\n' + b'\n'
super(OpenBsd, self).add_rules(rules) super(OpenBsd, self).add_rules(anchor, rules)
class Darwin(FreeBsd): class Darwin(FreeBsd):
@ -292,19 +310,20 @@ class Darwin(FreeBsd):
def enable(self): def enable(self):
o = pfctl('-E') o = pfctl('-E')
_pf_context['Xtoken'] = \ _pf_context['Xtoken'].append(re.search(b'Token : (.+)', o[1]).group(1))
re.search(b'Token : (.+)', o[1]).group(1)
def disable(self): def disable(self, anchor):
if _pf_context['Xtoken'] is not None: pfctl('-a %s -F all' % anchor)
pfctl('-X %s' % _pf_context['Xtoken'].decode("ASCII")) if _pf_context['Xtoken']:
pfctl('-X %s' % _pf_context['Xtoken'].pop().decode("ASCII"))
def add_anchors(self): def add_anchors(self, anchor):
# before adding anchors and rules we must override the skip lo # before adding anchors and rules we must override the skip lo
# that in some cases ends up in the chain so the rules we will add, # that in some cases ends up in the chain so the rules we will add,
# which rely on translating/filtering packets on lo, can work # which rely on translating/filtering packets on lo, can work
pfctl('-f /dev/stdin', b'pass on lo\n') if self.has_skip_loopback():
super(Darwin, self).add_anchors() pfctl('-f /dev/stdin', b'pass on lo\n')
super(Darwin, self).add_anchors(anchor)
def _add_natlook_ports(self, pnl, src_port, dst_port): def _add_natlook_ports(self, pnl, src_port, dst_port):
pnl.sxport.port = socket.htons(src_port) pnl.sxport.port = socket.htons(src_port)
@ -326,9 +345,14 @@ def pfctl(args, stdin=None):
argv = ['pfctl'] + list(args.split(" ")) argv = ['pfctl'] + list(args.split(" "))
debug1('>> %s\n' % ' '.join(argv)) debug1('>> %s\n' % ' '.join(argv))
env = {
'PATH': os.environ['PATH'],
'LC_ALL': "C",
}
p = ssubprocess.Popen(argv, stdin=ssubprocess.PIPE, p = ssubprocess.Popen(argv, stdin=ssubprocess.PIPE,
stdout=ssubprocess.PIPE, stdout=ssubprocess.PIPE,
stderr=ssubprocess.PIPE) stderr=ssubprocess.PIPE,
env=env)
o = p.communicate(stdin) o = p.communicate(stdin)
if p.returncode: if p.returncode:
raise Fatal('%r returned %d' % (argv, p.returncode)) raise Fatal('%r returned %d' % (argv, p.returncode))
@ -344,9 +368,17 @@ def pf_get_dev():
return _pf_fd return _pf_fd
def pf_get_anchor(family, port):
return 'sshuttle%s-%d' % ('' if family == socket.AF_INET else '6', port)
class Method(BaseMethod): class Method(BaseMethod):
def get_supported_features(self):
result = super(Method, self).get_supported_features()
result.ipv6 = True
return result
def get_tcp_dstip(self, sock): def get_tcp_dstip(self, sock):
pfile = self.firewall.pfile pfile = self.firewall.pfile
@ -372,7 +404,7 @@ class Method(BaseMethod):
translating_rules = [] translating_rules = []
filtering_rules = [] filtering_rules = []
if family != socket.AF_INET: if family not in [socket.AF_INET, socket.AF_INET6]:
raise Exception( raise Exception(
'Address family "%s" unsupported by pf method_name' 'Address family "%s" unsupported by pf method_name'
% family_to_string(family)) % family_to_string(family))
@ -391,20 +423,20 @@ class Method(BaseMethod):
snet.encode("ASCII"), snet.encode("ASCII"),
swidth)) swidth))
pf.add_anchors() anchor = pf_get_anchor(family, port)
pf.add_rules(includes, port, dnsport, nslist) pf.add_anchors(anchor)
pf.add_rules(anchor, includes, port, dnsport, nslist, family)
pf.enable() pf.enable()
def restore_firewall(self, port, family, udp): def restore_firewall(self, port, family, udp):
if family != socket.AF_INET: if family not in [socket.AF_INET, socket.AF_INET6]:
raise Exception( raise Exception(
'Address family "%s" unsupported by pf method_name' 'Address family "%s" unsupported by pf method_name'
% family_to_string(family)) % family_to_string(family))
if udp: if udp:
raise Exception("UDP not supported by pf method_name") raise Exception("UDP not supported by pf method_name")
pfctl('-a sshuttle -F all') pf.disable(pf_get_anchor(family, port))
pf.disable()
def firewall_command(self, line): def firewall_command(self, line):
if line.startswith('QUERY_PF_NAT '): if line.startswith('QUERY_PF_NAT '):

View File

@ -1,215 +1,306 @@
"""Command-line options parser.
With the help of an options spec string, easily parse command-line options.
"""
import sys
import os
import textwrap
import getopt
import re import re
import struct import socket
from argparse import ArgumentParser, Action, ArgumentTypeError as Fatal
from sshuttle import __version__
class OptDict: # 1.2.3.4/5 or just 1.2.3.4
def parse_subnet4(s):
def __init__(self): m = re.match(r'(\d+)(?:\.(\d+)\.(\d+)\.(\d+))?(?:/(\d+))?$', s)
self._opts = {} if not m:
raise Fatal('%r is not a valid IP subnet format' % s)
def __setitem__(self, k, v): (a, b, c, d, width) = m.groups()
if k.startswith('no-') or k.startswith('no_'): (a, b, c, d) = (int(a or 0), int(b or 0), int(c or 0), int(d or 0))
k = k[3:] if width is None:
v = not v width = 32
self._opts[k] = v else:
width = int(width)
def __getitem__(self, k): if a > 255 or b > 255 or c > 255 or d > 255:
if k.startswith('no-') or k.startswith('no_'): raise Fatal('%d.%d.%d.%d has numbers > 255' % (a, b, c, d))
return not self._opts[k[3:]] if width > 32:
return self._opts[k] raise Fatal('*/%d is greater than the maximum of 32' % width)
return(socket.AF_INET, '%d.%d.%d.%d' % (a, b, c, d), width)
def __getattr__(self, k):
return self[k]
def _default_onabort(msg): # 1:2::3/64 or just 1:2::3
sys.exit(97) def parse_subnet6(s):
m = re.match(r'(?:([a-fA-F\d:]+))?(?:/(\d+))?$', s)
if not m:
raise Fatal('%r is not a valid IP subnet format' % s)
(net, width) = m.groups()
if width is None:
width = 128
else:
width = int(width)
if width > 128:
raise Fatal('*/%d is greater than the maximum of 128' % width)
return(socket.AF_INET6, net, width)
def _intify(v): # Subnet file, supporting empty lines and hash-started comment lines
def parse_subnet_file(s):
try: try:
vv = int(v or '') handle = open(s, 'r')
if str(vv) == v: except OSError:
return vv raise Fatal('Unable to open subnet file: %s' % s)
except ValueError:
pass raw_config_lines = handle.readlines()
return v subnets = []
for line_no, line in enumerate(raw_config_lines):
line = line.strip()
if len(line) == 0:
continue
if line[0] == '#':
continue
subnets.append(parse_subnet(line))
return subnets
def _atoi(v): # 1.2.3.4/5 or just 1.2.3.4
try: # 1:2::3/64 or just 1:2::3
return int(v or 0) def parse_subnet(subnet_str):
except ValueError: if ':' in subnet_str:
return 0 return parse_subnet6(subnet_str)
else:
return parse_subnet4(subnet_str)
def _remove_negative_kv(k, v): # 1.2.3.4:567 or just 1.2.3.4 or just 567
if k.startswith('no-') or k.startswith('no_'): def parse_ipport4(s):
return k[3:], not v s = str(s)
return k, v m = re.match(r'(?:(\d+)\.(\d+)\.(\d+)\.(\d+))?(?::)?(?:(\d+))?$', s)
if not m:
raise Fatal('%r is not a valid IP:port format' % s)
(a, b, c, d, port) = m.groups()
(a, b, c, d, port) = (int(a or 0), int(b or 0), int(c or 0), int(d or 0),
int(port or 0))
if a > 255 or b > 255 or c > 255 or d > 255:
raise Fatal('%d.%d.%d.%d has numbers > 255' % (a, b, c, d))
if port > 65535:
raise Fatal('*:%d is greater than the maximum of 65535' % port)
if a is None:
a = b = c = d = 0
return ('%d.%d.%d.%d' % (a, b, c, d), port)
def _remove_negative_k(k): # [1:2::3]:456 or [1:2::3] or 456
return _remove_negative_kv(k, None)[0] def parse_ipport6(s):
s = str(s)
m = re.match(r'(?:\[([^]]*)])?(?::)?(?:(\d+))?$', s)
if not m:
raise Fatal('%s is not a valid IP:port format' % s)
(ip, port) = m.groups()
(ip, port) = (ip or '::', int(port or 0))
return (ip, port)
def _tty_width(): def parse_list(list):
if not hasattr(sys.stderr, "fileno"): return re.split(r'[\s,]+', list.strip()) if list else []
return _atoi(os.environ.get('WIDTH')) or 70
s = struct.pack("HHHH", 0, 0, 0, 0)
try:
import fcntl
import termios
s = fcntl.ioctl(sys.stderr.fileno(), termios.TIOCGWINSZ, s)
except (IOError, ImportError):
return _atoi(os.environ.get('WIDTH')) or 70
(ysize, xsize, ypix, xpix) = struct.unpack('HHHH', s)
return xsize or 70
class Options: class Concat(Action):
def __init__(self, option_strings, dest, nargs=None, **kwargs):
if nargs is not None:
raise ValueError("nargs not supported")
super(Concat, self).__init__(option_strings, dest, **kwargs)
"""Option parser. def __call__(self, parser, namespace, values, option_string=None):
When constructed, two strings are mandatory. The first one is the command curr_value = getattr(namespace, self.dest, [])
name showed before error messages. The second one is a string called an setattr(namespace, self.dest, curr_value + values)
optspec that specifies the synopsis and option flags and their description.
For more information about optspecs, consult the bup-options(1) man page.
Two optional arguments specify an alternative parsing function and an
alternative behaviour on abort (after having output the usage string).
By default, the parser function is getopt.gnu_getopt, and the abort parser = ArgumentParser(
behaviour is to exit the program. prog="sshuttle",
usage="%(prog)s [-l [ip:]port] [-r [user@]sshserver[:port]] <subnets...>"
)
parser.add_argument(
"subnets",
metavar="IP/MASK [IP/MASK...]",
nargs="*",
type=parse_subnet,
help="""
capture and forward traffic to these subnets (whitespace separated)
""" """
)
def __init__(self, optspec, optfunc=getopt.gnu_getopt, parser.add_argument(
onabort=_default_onabort): "-l", "--listen",
self.optspec = optspec metavar="[IP:]PORT",
self._onabort = onabort help="""
self.optfunc = optfunc transproxy to this ip address and port number
self._aliases = {} """
self._shortopts = 'h?' )
self._longopts = ['help'] parser.add_argument(
self._hasparms = {} "-H", "--auto-hosts",
self._defaults = {} action="store_true",
self._usagestr = self._gen_usage() help="""
continuously scan for remote hostnames and update local /etc/hosts as they are found
def _gen_usage(self): """
out = [] )
lines = self.optspec.strip().split('\n') parser.add_argument(
lines.reverse() "-N", "--auto-nets",
first_syn = True action="store_true",
while lines: help="""
l = lines.pop() automatically determine subnets to route
if l == '--': """
break )
out.append('%s: %s\n' % (first_syn and 'usage' or ' or', l)) parser.add_argument(
first_syn = False "--dns",
out.append('\n') action="store_true",
last_was_option = False help="""
while lines: capture local DNS requests and forward to the remote DNS server
l = lines.pop() """
if l.startswith(' '): )
out.append('%s%s\n' % (last_was_option and '\n' or '', parser.add_argument(
l.lstrip())) "--ns-hosts",
last_was_option = False metavar="IP[,IP]",
elif l: default=[],
(flags, extra) = l.split(' ', 1) type=parse_list,
extra = extra.strip() help="""
if flags.endswith('='): capture and forward DNS requests made to the following servers
flags = flags[:-1] """
has_parm = 1 )
else: parser.add_argument(
has_parm = 0 "--method",
g = re.search(r'\[([^\]]*)\]$', extra) choices=["auto", "nat", "tproxy", "pf"],
if g: metavar="TYPE",
defval = g.group(1) default="auto",
else: help="""
defval = None %(choices)s
flagl = flags.split(',') """
flagl_nice = [] )
for _f in flagl: parser.add_argument(
f, dvi = _remove_negative_kv(_f, _intify(defval)) "--python",
self._aliases[f] = _remove_negative_k(flagl[0]) metavar="PATH",
self._hasparms[f] = has_parm help="""
self._defaults[f] = dvi path to python interpreter on the remote server
if len(f) == 1: """
self._shortopts += f + (has_parm and ':' or '') )
flagl_nice.append('-' + f) parser.add_argument(
else: "-r", "--remote",
f_nice = re.sub(r'\W', '_', f) metavar="[USERNAME@]ADDR[:PORT]",
self._aliases[f_nice] = _remove_negative_k(flagl[0]) help="""
self._longopts.append(f + (has_parm and '=' or '')) ssh hostname (and optional username) of remote %(prog)s server
self._longopts.append('no-' + f) """
flagl_nice.append('--' + _f) )
flags_nice = ', '.join(flagl_nice) parser.add_argument(
if has_parm: "-x", "--exclude",
flags_nice += ' ...' metavar="IP/MASK",
prefix = ' %-20s ' % flags_nice action="append",
argtext = '\n'.join(textwrap.wrap(extra, width=_tty_width(), default=[],
initial_indent=prefix, type=parse_subnet,
subsequent_indent=' ' * 28)) help="""
out.append(argtext + '\n') exclude this subnet (can be used more than once)
last_was_option = True """
else: )
out.append('\n') parser.add_argument(
last_was_option = False "-X", "--exclude-from",
return ''.join(out).rstrip() + '\n' metavar="PATH",
action=Concat,
def usage(self, msg=""): dest="exclude",
"""Print usage string to stderr and abort.""" type=parse_subnet_file,
sys.stderr.write(self._usagestr) help="""
e = self._onabort and self._onabort(msg) or None exclude the subnets in a file (whitespace separated)
if e: """
raise e )
parser.add_argument(
def fatal(self, s): "-v", "--verbose",
"""Print an error message to stderr and abort with usage string.""" action="count",
msg = 'error: %s\n' % s default=0,
sys.stderr.write(msg) help="""
return self.usage(msg) increase debug message verbosity
"""
def parse(self, args): )
"""Parse a list of arguments and return (options, flags, extra). parser.add_argument(
"-V", "--version",
In the returned tuple, "options" is an OptDict with known options, action="version",
"flags" is a list of option flags that were used on the command-line, version=__version__,
and "extra" is a list of positional arguments. help="""
""" print the %(prog)s version number and exit
try: """
(flags, extra) = self.optfunc( )
args, self._shortopts, self._longopts) parser.add_argument(
except getopt.GetoptError as e: "-e", "--ssh-cmd",
self.fatal(e) metavar="CMD",
default="ssh",
opt = OptDict() help="""
the command to use to connect to the remote [%(default)s]
for k, v in self._defaults.items(): """
k = self._aliases[k] )
opt[k] = v parser.add_argument(
"--seed-hosts",
for (k, v) in flags: metavar="HOSTNAME[,HOSTNAME]",
k = k.lstrip('-') default=[],
if k in ('h', '?', 'help'): help="""
self.usage() comma-separated list of hostnames for initial scan (may be used with or without --auto-hosts)
if k.startswith('no-'): """
k = self._aliases[k[3:]] )
v = 0 parser.add_argument(
else: "--no-latency-control",
k = self._aliases[k] action="store_false",
if not self._hasparms[k]: dest="latency_control",
assert(v == '') help="""
v = (opt._opts.get(k) or 0) + 1 sacrifice latency to improve bandwidth benchmarks
else: """
v = _intify(v) )
opt[k] = v parser.add_argument(
for (f1, f2) in self._aliases.items(): "--wrap",
opt[f1] = opt._opts.get(f2) metavar="NUM",
return (opt, flags, extra) type=int,
help="""
restart counting channel numbers after this number (for testing)
"""
)
parser.add_argument(
"--disable-ipv6",
action="store_true",
help="""
disable IPv6 support
"""
)
parser.add_argument(
"-D", "--daemon",
action="store_true",
help="""
run in the background as a daemon
"""
)
parser.add_argument(
"-s", "--subnets",
metavar="PATH",
action=Concat,
dest="subnets",
type=parse_subnet_file,
help="""
file where the subnets are stored, instead of on the command line
"""
)
parser.add_argument(
"--syslog",
action="store_true",
help="""
send log messages to syslog (default if you use --daemon)
"""
)
parser.add_argument(
"--pidfile",
metavar="PATH",
default="./sshuttle.pid",
help="""
pidfile name (only if using --daemon) [%(default)s]
"""
)
parser.add_argument(
"--firewall",
action="store_true",
help="""
(internal use only)
"""
)
parser.add_argument(
"--hostwatch",
action="store_true",
help="""
(internal use only)
"""
)

View File

@ -12,32 +12,34 @@ import sshuttle.helpers as helpers
import sshuttle.hostwatch as hostwatch import sshuttle.hostwatch as hostwatch
import subprocess as ssubprocess import subprocess as ssubprocess
from sshuttle.ssnet import Handler, Proxy, Mux, MuxWrapper from sshuttle.ssnet import Handler, Proxy, Mux, MuxWrapper
from sshuttle.helpers import log, debug1, debug2, debug3, Fatal, \ from sshuttle.helpers import b, log, debug1, debug2, debug3, Fatal, \
resolvconf_random_nameserver resolvconf_random_nameserver
def _ipmatch(ipstr): def _ipmatch(ipstr):
if ipstr == b'default': # FIXME: IPv4 only
ipstr = b'0.0.0.0/0' if ipstr == 'default':
m = re.match(b'^(\d+(\.\d+(\.\d+(\.\d+)?)?)?)(?:/(\d+))?$', ipstr) ipstr = '0.0.0.0/0'
m = re.match('^(\d+(\.\d+(\.\d+(\.\d+)?)?)?)(?:/(\d+))?$', ipstr)
if m: if m:
g = m.groups() g = m.groups()
ips = g[0] ips = g[0]
width = int(g[4] or 32) width = int(g[4] or 32)
if g[1] is None: if g[1] is None:
ips += b'.0.0.0' ips += '.0.0.0'
width = min(width, 8) width = min(width, 8)
elif g[2] is None: elif g[2] is None:
ips += b'.0.0' ips += '.0.0'
width = min(width, 16) width = min(width, 16)
elif g[3] is None: elif g[3] is None:
ips += b'.0' ips += '.0'
width = min(width, 24) width = min(width, 24)
ips = ips.decode("ASCII") ips = ips
return (struct.unpack('!I', socket.inet_aton(ips))[0], width) return (struct.unpack('!I', socket.inet_aton(ips))[0], width)
def _ipstr(ip, width): def _ipstr(ip, width):
# FIXME: IPv4 only
if width >= 32: if width >= 32:
return ip return ip
else: else:
@ -45,6 +47,7 @@ def _ipstr(ip, width):
def _maskbits(netmask): def _maskbits(netmask):
# FIXME: IPv4 only
if not netmask: if not netmask:
return 32 return 32
for i in range(32): for i in range(32):
@ -60,10 +63,14 @@ def _shl(n, bits):
def _list_routes(): def _list_routes():
# FIXME: IPv4 only # FIXME: IPv4 only
argv = ['netstat', '-rn'] argv = ['netstat', '-rn']
p = ssubprocess.Popen(argv, stdout=ssubprocess.PIPE) env = {
'PATH': os.environ['PATH'],
'LC_ALL': "C",
}
p = ssubprocess.Popen(argv, stdout=ssubprocess.PIPE, env=env)
routes = [] routes = []
for line in p.stdout: for line in p.stdout:
cols = re.split(b'\s+', line) cols = re.split(r'\s+', line.decode("ASCII"))
ipw = _ipmatch(cols[0]) ipw = _ipmatch(cols[0])
if not ipw: if not ipw:
continue # some lines won't be parseable; never mind continue # some lines won't be parseable; never mind
@ -91,7 +98,7 @@ def _exc_dump():
return ''.join(traceback.format_exception(*exc_info)) return ''.join(traceback.format_exception(*exc_info))
def start_hostwatch(seed_hosts): def start_hostwatch(seed_hosts, auto_hosts):
s1, s2 = socket.socketpair() s1, s2 = socket.socketpair()
pid = os.fork() pid = os.fork()
if not pid: if not pid:
@ -103,7 +110,7 @@ def start_hostwatch(seed_hosts):
os.dup2(s1.fileno(), 1) os.dup2(s1.fileno(), 1)
os.dup2(s1.fileno(), 0) os.dup2(s1.fileno(), 0)
s1.close() s1.close()
rv = hostwatch.hw_main(seed_hosts) or 0 rv = hostwatch.hw_main(seed_hosts, auto_hosts) or 0
except Exception: except Exception:
log('%s\n' % _exc_dump()) log('%s\n' % _exc_dump())
rv = 98 rv = 98
@ -149,7 +156,8 @@ class DnsProxy(Handler):
try: try:
sock.send(self.request) sock.send(self.request)
self.socks.append(sock) self.socks.append(sock)
except socket.error as e: except socket.error:
_, e = sys.exc_info()[:2]
if e.args[0] in ssnet.NET_ERRS: if e.args[0] in ssnet.NET_ERRS:
# might have been spurious; try again. # might have been spurious; try again.
# Note: these errors sometimes are reported by recv(), # Note: these errors sometimes are reported by recv(),
@ -166,7 +174,8 @@ class DnsProxy(Handler):
try: try:
data = sock.recv(4096) data = sock.recv(4096)
except socket.error as e: except socket.error:
_, e = sys.exc_info()[:2]
self.socks.remove(sock) self.socks.remove(sock)
del self.peers[sock] del self.peers[sock]
@ -201,22 +210,24 @@ class UdpProxy(Handler):
debug2('UDP: sending to %r port %d\n' % dstip) debug2('UDP: sending to %r port %d\n' % dstip)
try: try:
self.sock.sendto(data, dstip) self.sock.sendto(data, dstip)
except socket.error as e: except socket.error:
_, e = sys.exc_info()[:2]
log('UDP send to %r port %d: %s\n' % (dstip[0], dstip[1], e)) log('UDP send to %r port %d: %s\n' % (dstip[0], dstip[1], e))
return return
def callback(self, sock): def callback(self, sock):
try: try:
data, peer = sock.recvfrom(4096) data, peer = sock.recvfrom(4096)
except socket.error as e: except socket.error:
_, e = sys.exc_info()[:2]
log('UDP recv from %r port %d: %s\n' % (peer[0], peer[1], e)) log('UDP recv from %r port %d: %s\n' % (peer[0], peer[1], e))
return return
debug2('UDP response: %d bytes\n' % len(data)) debug2('UDP response: %d bytes\n' % len(data))
hdr = "%s,%r," % (peer[0], peer[1]) hdr = b("%s,%r," % (peer[0], peer[1]))
self.mux.send(self.chan, ssnet.CMD_UDP_DATA, hdr + data) self.mux.send(self.chan, ssnet.CMD_UDP_DATA, hdr + data)
def main(latency_control): def main(latency_control, auto_hosts):
debug1('Starting server with Python version %s\n' debug1('Starting server with Python version %s\n'
% platform.python_version()) % platform.python_version())
@ -241,39 +252,46 @@ def main(latency_control):
socket.fromfd(sys.stdout.fileno(), socket.fromfd(sys.stdout.fileno(),
socket.AF_INET, socket.SOCK_STREAM)) socket.AF_INET, socket.SOCK_STREAM))
handlers.append(mux) handlers.append(mux)
routepkt = b'' routepkt = ''
for r in routes: for r in routes:
routepkt += b'%d,%s,%d\n' % (r[0], r[1].encode("ASCII"), r[2]) routepkt += '%d,%s,%d\n' % r
mux.send(0, ssnet.CMD_ROUTES, routepkt) mux.send(0, ssnet.CMD_ROUTES, b(routepkt))
hw = Hostwatch() hw = Hostwatch()
hw.leftover = b'' hw.leftover = b('')
def hostwatch_ready(sock): def hostwatch_ready(sock):
assert(hw.pid) assert(hw.pid)
content = hw.sock.recv(4096) content = hw.sock.recv(4096)
if content: if content:
lines = (hw.leftover + content).split(b'\n') lines = (hw.leftover + content).split(b('\n'))
if lines[-1]: if lines[-1]:
# no terminating newline: entry isn't complete yet! # no terminating newline: entry isn't complete yet!
hw.leftover = lines.pop() hw.leftover = lines.pop()
lines.append(b'') lines.append(b(''))
else: else:
hw.leftover = b'' hw.leftover = b('')
mux.send(0, ssnet.CMD_HOST_LIST, b'\n'.join(lines)) mux.send(0, ssnet.CMD_HOST_LIST, b('\n').join(lines))
else: else:
raise Fatal('hostwatch process died') raise Fatal('hostwatch process died')
def got_host_req(data): def got_host_req(data):
if not hw.pid: if not hw.pid:
(hw.pid, hw.sock) = start_hostwatch(data.strip().split()) (hw.pid, hw.sock) = start_hostwatch(
data.strip().split(), auto_hosts)
handlers.append(Handler(socks=[hw.sock], handlers.append(Handler(socks=[hw.sock],
callback=hostwatch_ready)) callback=hostwatch_ready))
mux.got_host_req = got_host_req mux.got_host_req = got_host_req
def new_channel(channel, data): def new_channel(channel, data):
(family, dstip, dstport) = data.split(b',', 2) (family, dstip, dstport) = data.decode("ASCII").split(',', 2)
family = int(family) family = int(family)
# AF_INET is the same constant on Linux and BSD but AF_INET6
# is different. As the client and server can be running on
# different platforms we can not just set the socket family
# to what comes in the wire.
if family != socket.AF_INET:
family = socket.AF_INET6
dstport = int(dstport) dstport = int(dstport)
outwrap = ssnet.connect_dst(family, dstip, dstport) outwrap = ssnet.connect_dst(family, dstip, dstport)
handlers.append(Proxy(MuxWrapper(mux, channel), outwrap)) handlers.append(Proxy(MuxWrapper(mux, channel), outwrap))
@ -293,7 +311,7 @@ def main(latency_control):
def udp_req(channel, cmd, data): def udp_req(channel, cmd, data):
debug2('Incoming UDP request channel=%d, cmd=%d\n' % (channel, cmd)) debug2('Incoming UDP request channel=%d, cmd=%d\n' % (channel, cmd))
if cmd == ssnet.CMD_UDP_DATA: if cmd == ssnet.CMD_UDP_DATA:
(dstip, dstport, data) = data.split(",", 2) (dstip, dstport, data) = data.split(b(','), 2)
dstport = int(dstport) dstport = int(dstport)
debug2('is incoming UDP data. %r %d.\n' % (dstip, dstport)) debug2('is incoming UDP data. %r %d.\n' % (dstip, dstport))
h = udphandlers[channel] h = udphandlers[channel]

View File

@ -8,6 +8,13 @@ import subprocess as ssubprocess
import sshuttle.helpers as helpers import sshuttle.helpers as helpers
from sshuttle.helpers import debug2 from sshuttle.helpers import debug2
try:
# Python >= 3.5
from shlex import quote
except ImportError:
# Python 2.x
from pipes import quote
def readfile(name): def readfile(name):
tokens = name.split(".") tokens = name.split(".")
@ -89,17 +96,17 @@ def connect(ssh_cmd, rhostport, python, stderr, options):
b"\n") b"\n")
pyscript = r""" pyscript = r"""
import sys; import sys, os;
verbosity=%d; verbosity=%d;
stdin=getattr(sys.stdin,"buffer",sys.stdin); sys.stdin = os.fdopen(0, "rb");
exec(compile(stdin.read(%d), "assembler.py", "exec")) exec(compile(sys.stdin.read(%d), "assembler.py", "exec"))
""" % (helpers.verbose or 0, len(content)) """ % (helpers.verbose or 0, len(content))
pyscript = re.sub(r'\s+', ' ', pyscript.strip()) pyscript = re.sub(r'\s+', ' ', pyscript.strip())
if not rhost: if not rhost:
# ignore the --python argument when running locally; we already know # ignore the --python argument when running locally; we already know
# which python version works. # which python version works.
argv = [sys.argv[1], '-c', pyscript] argv = [sys.executable, '-c', pyscript]
else: else:
if ssh_cmd: if ssh_cmd:
sshl = ssh_cmd.split(' ') sshl = ssh_cmd.split(' ')
@ -109,7 +116,8 @@ def connect(ssh_cmd, rhostport, python, stderr, options):
pycmd = "'%s' -c '%s'" % (python, pyscript) pycmd = "'%s' -c '%s'" % (python, pyscript)
else: else:
pycmd = ("P=python3.5; $P -V 2>/dev/null || P=python; " pycmd = ("P=python3.5; $P -V 2>/dev/null || P=python; "
"exec \"$P\" -c '%s'") % pyscript "exec \"$P\" -c %s") % quote(pyscript)
pycmd = ("exec /bin/sh -c %s" % quote(pycmd))
argv = (sshl + argv = (sshl +
portl + portl +
[rhost, '--', pycmd]) [rhost, '--', pycmd])

View File

@ -1,9 +1,10 @@
import sys
import struct import struct
import socket import socket
import errno import errno
import select import select
import os import os
from sshuttle.helpers import log, debug1, debug2, debug3, Fatal from sshuttle.helpers import b, binary_type, log, debug1, debug2, debug3, Fatal
MAX_CHANNEL = 65535 MAX_CHANNEL = 65535
@ -53,7 +54,8 @@ cmd_to_name = {
NET_ERRS = [errno.ECONNREFUSED, errno.ETIMEDOUT, NET_ERRS = [errno.ECONNREFUSED, errno.ETIMEDOUT,
errno.EHOSTUNREACH, errno.ENETUNREACH, errno.EHOSTUNREACH, errno.ENETUNREACH,
errno.EHOSTDOWN, errno.ENETDOWN] errno.EHOSTDOWN, errno.ENETDOWN,
errno.ENETUNREACH]
def _add(l, elem): def _add(l, elem):
@ -75,7 +77,8 @@ def _fds(l):
def _nb_clean(func, *args): def _nb_clean(func, *args):
try: try:
return func(*args) return func(*args)
except OSError as e: except OSError:
_, e = sys.exc_info()[:2]
if e.errno not in (errno.EWOULDBLOCK, errno.EAGAIN): if e.errno not in (errno.EWOULDBLOCK, errno.EAGAIN):
raise raise
else: else:
@ -88,7 +91,8 @@ def _try_peername(sock):
pn = sock.getpeername() pn = sock.getpeername()
if pn: if pn:
return '%s:%s' % (pn[0], pn[1]) return '%s:%s' % (pn[0], pn[1])
except socket.error as e: except socket.error:
_, e = sys.exc_info()[:2]
if e.args[0] not in (errno.ENOTCONN, errno.ENOTSOCK): if e.args[0] not in (errno.ENOTCONN, errno.ENOTSOCK):
raise raise
return 'unknown' return 'unknown'
@ -144,7 +148,8 @@ class SockWrapper:
self.rsock.connect(self.connect_to) self.rsock.connect(self.connect_to)
# connected successfully (Linux) # connected successfully (Linux)
self.connect_to = None self.connect_to = None
except socket.error as e: except socket.error:
_, e = sys.exc_info()[:2]
debug3('%r: connect result: %s\n' % (self, e)) debug3('%r: connect result: %s\n' % (self, e))
if e.args[0] == errno.EINVAL: if e.args[0] == errno.EINVAL:
# this is what happens when you call connect() on a socket # this is what happens when you call connect() on a socket
@ -191,7 +196,8 @@ class SockWrapper:
self.shut_write = True self.shut_write = True
try: try:
self.wsock.shutdown(SHUT_WR) self.wsock.shutdown(SHUT_WR)
except socket.error as e: except socket.error:
_, e = sys.exc_info()[:2]
self.seterr('nowrite: %s' % e) self.seterr('nowrite: %s' % e)
def too_full(self): def too_full(self):
@ -203,7 +209,8 @@ class SockWrapper:
self.wsock.setblocking(False) self.wsock.setblocking(False)
try: try:
return _nb_clean(os.write, self.wsock.fileno(), buf) return _nb_clean(os.write, self.wsock.fileno(), buf)
except OSError as e: except OSError:
_, e = sys.exc_info()[:2]
if e.errno == errno.EPIPE: if e.errno == errno.EPIPE:
debug1('%r: uwrite: got EPIPE\n' % self) debug1('%r: uwrite: got EPIPE\n' % self)
self.nowrite() self.nowrite()
@ -225,9 +232,10 @@ class SockWrapper:
self.rsock.setblocking(False) self.rsock.setblocking(False)
try: try:
return _nb_clean(os.read, self.rsock.fileno(), 65536) return _nb_clean(os.read, self.rsock.fileno(), 65536)
except OSError as e: except OSError:
_, e = sys.exc_info()[:2]
self.seterr('uread: %s' % e) self.seterr('uread: %s' % e)
return b'' # unexpected error... we'll call it EOF return b('') # unexpected error... we'll call it EOF
def fill(self): def fill(self):
if self.buf: if self.buf:
@ -235,7 +243,7 @@ class SockWrapper:
rb = self.uread() rb = self.uread()
if rb: if rb:
self.buf.append(rb) self.buf.append(rb)
if rb == b'': # empty string means EOF; None means temporarily empty if rb == b(''): # empty string means EOF; None means temporarily empty
self.noread() self.noread()
def copy_to(self, outwrap): def copy_to(self, outwrap):
@ -333,11 +341,11 @@ class Mux(Handler):
self.channels = {} self.channels = {}
self.chani = 0 self.chani = 0
self.want = 0 self.want = 0
self.inbuf = b'' self.inbuf = b('')
self.outbuf = [] self.outbuf = []
self.fullness = 0 self.fullness = 0
self.too_full = False self.too_full = False
self.send(0, CMD_PING, b'chicken') self.send(0, CMD_PING, b('chicken'))
def next_channel(self): def next_channel(self):
# channel 0 is special, so we never allocate it # channel 0 is special, so we never allocate it
@ -350,14 +358,14 @@ class Mux(Handler):
def amount_queued(self): def amount_queued(self):
total = 0 total = 0
for b in self.outbuf: for byte in self.outbuf:
total += len(b) total += len(byte)
return total return total
def check_fullness(self): def check_fullness(self):
if self.fullness > 32768: if self.fullness > 32768:
if not self.too_full: if not self.too_full:
self.send(0, CMD_PING, b'rttest') self.send(0, CMD_PING, b('rttest'))
self.too_full = True self.too_full = True
# ob = [] # ob = []
# for b in self.outbuf: # for b in self.outbuf:
@ -366,9 +374,10 @@ class Mux(Handler):
# log('outbuf: %d %r\n' % (self.amount_queued(), ob)) # log('outbuf: %d %r\n' % (self.amount_queued(), ob))
def send(self, channel, cmd, data): def send(self, channel, cmd, data):
assert isinstance(data, bytes) assert isinstance(data, binary_type)
assert len(data) <= 65535 assert len(data) <= 65535
p = struct.pack('!ccHHH', b'S', b'S', channel, cmd, len(data)) + data p = struct.pack('!ccHHH', b('S'), b('S'), channel, cmd, len(data)) \
+ data
self.outbuf.append(p) self.outbuf.append(p)
debug2(' > channel=%d cmd=%s len=%d (fullness=%d)\n' debug2(' > channel=%d cmd=%s len=%d (fullness=%d)\n'
% (channel, cmd_to_name.get(cmd, hex(cmd)), % (channel, cmd_to_name.get(cmd, hex(cmd)),
@ -434,14 +443,15 @@ class Mux(Handler):
def fill(self): def fill(self):
self.rsock.setblocking(False) self.rsock.setblocking(False)
try: try:
b = _nb_clean(os.read, self.rsock.fileno(), 32768) read = _nb_clean(os.read, self.rsock.fileno(), 32768)
except OSError as e: except OSError:
_, e = sys.exc_info()[:2]
raise Fatal('other end: %r' % e) raise Fatal('other end: %r' % e)
# log('<<< %r\n' % b) # log('<<< %r\n' % b)
if b == b'': # EOF if read == b(''): # EOF
self.ok = False self.ok = False
if b: if read:
self.inbuf += b self.inbuf += read
def handle(self): def handle(self):
self.fill() self.fill()
@ -451,8 +461,8 @@ class Mux(Handler):
if len(self.inbuf) >= (self.want or HDR_LEN): if len(self.inbuf) >= (self.want or HDR_LEN):
(s1, s2, channel, cmd, datalen) = \ (s1, s2, channel, cmd, datalen) = \
struct.unpack('!ccHHH', self.inbuf[:HDR_LEN]) struct.unpack('!ccHHH', self.inbuf[:HDR_LEN])
assert(s1 == b'S') assert(s1 == b('S'))
assert(s2 == b'S') assert(s2 == b('S'))
self.want = datalen + HDR_LEN self.want = datalen + HDR_LEN
if self.want and len(self.inbuf) >= self.want: if self.want and len(self.inbuf) >= self.want:
data = self.inbuf[HDR_LEN:self.want] data = self.inbuf[HDR_LEN:self.want]
@ -496,14 +506,14 @@ class MuxWrapper(SockWrapper):
if not self.shut_read: if not self.shut_read:
debug2('%r: done reading\n' % self) debug2('%r: done reading\n' % self)
self.shut_read = True self.shut_read = True
self.mux.send(self.channel, CMD_TCP_STOP_SENDING, b'') self.mux.send(self.channel, CMD_TCP_STOP_SENDING, b(''))
self.maybe_close() self.maybe_close()
def nowrite(self): def nowrite(self):
if not self.shut_write: if not self.shut_write:
debug2('%r: done writing\n' % self) debug2('%r: done writing\n' % self)
self.shut_write = True self.shut_write = True
self.mux.send(self.channel, CMD_TCP_EOF, b'') self.mux.send(self.channel, CMD_TCP_EOF, b(''))
self.maybe_close() self.maybe_close()
def maybe_close(self): def maybe_close(self):
@ -526,7 +536,7 @@ class MuxWrapper(SockWrapper):
def uread(self): def uread(self):
if self.shut_read: if self.shut_read:
return b'' # EOF return b('') # EOF
else: else:
return None # no data available right now return None # no data available right now
@ -548,7 +558,7 @@ def connect_dst(family, ip, port):
outsock.setsockopt(socket.SOL_IP, socket.IP_TTL, 42) outsock.setsockopt(socket.SOL_IP, socket.IP_TTL, 42)
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): def runonce(handlers, mux):

View File

@ -10,7 +10,7 @@ from sshuttle.methods.pf import FreeBsd, Darwin, OpenBsd
def test_get_supported_features(): def test_get_supported_features():
method = get_method('pf') method = get_method('pf')
features = method.get_supported_features() features = method.get_supported_features()
assert not features.ipv6 assert features.ipv6
assert not features.udp assert not features.udp
assert features.dns assert features.dns
@ -155,6 +155,8 @@ def test_firewall_command_openbsd(mock_pf_get_dev, mock_ioctl, mock_stdout):
def pfctl(args, stdin=None): def pfctl(args, stdin=None):
if args == '-s Interfaces -i lo -v':
return (b'lo0 (skip)',)
if args == '-s all': if args == '-s all':
return (b'INFO:\nStatus: Disabled\nanother mary had a little lamb\n', return (b'INFO:\nStatus: Disabled\nanother mary had a little lamb\n',
b'little lamb\n') b'little lamb\n')
@ -174,19 +176,45 @@ def test_setup_firewall_darwin(mock_pf_get_dev, mock_ioctl, mock_pfctl):
method = get_method('pf') method = get_method('pf')
assert method.name == 'pf' assert method.name == 'pf'
with pytest.raises(Exception) as excinfo: # IPV6
method.setup_firewall(
1024, 1026, method.setup_firewall(
[(10, u'2404:6800:4004:80c::33')], 1024, 1026,
10, [(10, u'2404:6800:4004:80c::33')],
[(10, 64, False, u'2404:6800:4004:80c::'), 10,
(10, 128, True, u'2404:6800:4004:80c::101f')], [(10, 64, False, u'2404:6800:4004:80c::'),
True) (10, 128, True, u'2404:6800:4004:80c::101f')],
assert str(excinfo.value) \ False)
== 'Address family "AF_INET6" unsupported by pf method_name' assert mock_ioctl.mock_calls == [
assert mock_pf_get_dev.mock_calls == [] call(mock_pf_get_dev(), 0xC4704433, ANY),
assert mock_ioctl.mock_calls == [] call(mock_pf_get_dev(), 0xCC20441A, ANY),
assert mock_pfctl.mock_calls == [] call(mock_pf_get_dev(), 0xCC20441A, ANY),
call(mock_pf_get_dev(), 0xC4704433, ANY),
call(mock_pf_get_dev(), 0xCC20441A, ANY),
call(mock_pf_get_dev(), 0xCC20441A, ANY),
]
assert mock_pfctl.mock_calls == [
call('-s Interfaces -i lo -v'),
call('-f /dev/stdin', b'pass on lo\n'),
call('-s all'),
call('-a sshuttle6-1024 -f /dev/stdin',
b'table <forward_subnets> {'
b'!2404:6800:4004:80c::101f/128,2404:6800:4004:80c::/64'
b'}\n'
b'table <dns_servers> {2404:6800:4004:80c::33}\n'
b'rdr pass on lo0 inet6 proto tcp '
b'to <forward_subnets> -> ::1 port 1024\n'
b'rdr pass on lo0 inet6 proto udp '
b'to <dns_servers> port 53 -> ::1 port 1026\n'
b'pass out route-to lo0 inet6 proto tcp '
b'to <forward_subnets> keep state\n'
b'pass out route-to lo0 inet6 proto udp '
b'to <dns_servers> port 53 keep state\n'),
call('-E'),
]
mock_pf_get_dev.reset_mock()
mock_ioctl.reset_mock()
mock_pfctl.reset_mock()
with pytest.raises(Exception) as excinfo: with pytest.raises(Exception) as excinfo:
method.setup_firewall( method.setup_firewall(
@ -215,14 +243,15 @@ def test_setup_firewall_darwin(mock_pf_get_dev, mock_ioctl, mock_pfctl):
call(mock_pf_get_dev(), 0xCC20441A, ANY), call(mock_pf_get_dev(), 0xCC20441A, ANY),
] ]
assert mock_pfctl.mock_calls == [ assert mock_pfctl.mock_calls == [
call('-s Interfaces -i lo -v'),
call('-f /dev/stdin', b'pass on lo\n'), call('-f /dev/stdin', b'pass on lo\n'),
call('-s all'), call('-s all'),
call('-a sshuttle -f /dev/stdin', call('-a sshuttle-1025 -f /dev/stdin',
b'table <forward_subnets> {!1.2.3.66/32,1.2.3.0/24}\n' b'table <forward_subnets> {!1.2.3.66/32,1.2.3.0/24}\n'
b'table <dns_servers> {1.2.3.33}\n' b'table <dns_servers> {1.2.3.33}\n'
b'rdr pass on lo0 proto tcp ' b'rdr pass on lo0 inet proto tcp '
b'to <forward_subnets> -> 127.0.0.1 port 1025\n' b'to <forward_subnets> -> 127.0.0.1 port 1025\n'
b'rdr pass on lo0 proto udp ' b'rdr pass on lo0 inet proto udp '
b'to <dns_servers> port 53 -> 127.0.0.1 port 1027\n' b'to <dns_servers> port 53 -> 127.0.0.1 port 1027\n'
b'pass out route-to lo0 inet proto tcp ' b'pass out route-to lo0 inet proto tcp '
b'to <forward_subnets> keep state\n' b'to <forward_subnets> keep state\n'
@ -237,7 +266,7 @@ def test_setup_firewall_darwin(mock_pf_get_dev, mock_ioctl, mock_pfctl):
method.restore_firewall(1025, 2, False) method.restore_firewall(1025, 2, False)
assert mock_ioctl.mock_calls == [] assert mock_ioctl.mock_calls == []
assert mock_pfctl.mock_calls == [ assert mock_pfctl.mock_calls == [
call('-a sshuttle -F all'), call('-a sshuttle-1025 -F all'),
call("-X abcdefg"), call("-X abcdefg"),
] ]
mock_pf_get_dev.reset_mock() mock_pf_get_dev.reset_mock()
@ -256,19 +285,34 @@ def test_setup_firewall_freebsd(mock_pf_get_dev, mock_ioctl, mock_pfctl):
method = get_method('pf') method = get_method('pf')
assert method.name == 'pf' assert method.name == 'pf'
with pytest.raises(Exception) as excinfo: method.setup_firewall(
method.setup_firewall( 1024, 1026,
1024, 1026, [(10, u'2404:6800:4004:80c::33')],
[(10, u'2404:6800:4004:80c::33')], 10,
10, [(10, 64, False, u'2404:6800:4004:80c::'),
[(10, 64, False, u'2404:6800:4004:80c::'), (10, 128, True, u'2404:6800:4004:80c::101f')],
(10, 128, True, u'2404:6800:4004:80c::101f')], False)
True)
assert str(excinfo.value) \ assert mock_pfctl.mock_calls == [
== 'Address family "AF_INET6" unsupported by pf method_name' call('-s all'),
assert mock_pf_get_dev.mock_calls == [] call('-a sshuttle6-1024 -f /dev/stdin',
assert mock_ioctl.mock_calls == [] b'table <forward_subnets> {'
assert mock_pfctl.mock_calls == [] b'!2404:6800:4004:80c::101f/128,2404:6800:4004:80c::/64'
b'}\n'
b'table <dns_servers> {2404:6800:4004:80c::33}\n'
b'rdr pass on lo0 inet6 proto tcp '
b'to <forward_subnets> -> ::1 port 1024\n'
b'rdr pass on lo0 inet6 proto udp '
b'to <dns_servers> port 53 -> ::1 port 1026\n'
b'pass out route-to lo0 inet6 proto tcp '
b'to <forward_subnets> keep state\n'
b'pass out route-to lo0 inet6 proto udp '
b'to <dns_servers> port 53 keep state\n'),
call('-e'),
]
mock_pf_get_dev.reset_mock()
mock_ioctl.reset_mock()
mock_pfctl.reset_mock()
with pytest.raises(Exception) as excinfo: with pytest.raises(Exception) as excinfo:
method.setup_firewall( method.setup_firewall(
@ -298,12 +342,12 @@ def test_setup_firewall_freebsd(mock_pf_get_dev, mock_ioctl, mock_pfctl):
] ]
assert mock_pfctl.mock_calls == [ assert mock_pfctl.mock_calls == [
call('-s all'), call('-s all'),
call('-a sshuttle -f /dev/stdin', call('-a sshuttle-1025 -f /dev/stdin',
b'table <forward_subnets> {!1.2.3.66/32,1.2.3.0/24}\n' b'table <forward_subnets> {!1.2.3.66/32,1.2.3.0/24}\n'
b'table <dns_servers> {1.2.3.33}\n' b'table <dns_servers> {1.2.3.33}\n'
b'rdr pass on lo0 proto tcp ' b'rdr pass on lo0 inet proto tcp '
b'to <forward_subnets> -> 127.0.0.1 port 1025\n' b'to <forward_subnets> -> 127.0.0.1 port 1025\n'
b'rdr pass on lo0 proto udp ' b'rdr pass on lo0 inet proto udp '
b'to <dns_servers> port 53 -> 127.0.0.1 port 1027\n' b'to <dns_servers> port 53 -> 127.0.0.1 port 1027\n'
b'pass out route-to lo0 inet proto tcp ' b'pass out route-to lo0 inet proto tcp '
b'to <forward_subnets> keep state\n' b'to <forward_subnets> keep state\n'
@ -318,7 +362,7 @@ def test_setup_firewall_freebsd(mock_pf_get_dev, mock_ioctl, mock_pfctl):
method.restore_firewall(1025, 2, False) method.restore_firewall(1025, 2, False)
assert mock_ioctl.mock_calls == [] assert mock_ioctl.mock_calls == []
assert mock_pfctl.mock_calls == [ assert mock_pfctl.mock_calls == [
call('-a sshuttle -F all'), call('-a sshuttle-1025 -F all'),
call("-d"), call("-d"),
] ]
mock_pf_get_dev.reset_mock() mock_pf_get_dev.reset_mock()
@ -337,20 +381,41 @@ def test_setup_firewall_openbsd(mock_pf_get_dev, mock_ioctl, mock_pfctl):
method = get_method('pf') method = get_method('pf')
assert method.name == 'pf' assert method.name == 'pf'
with pytest.raises(Exception) as excinfo: method.setup_firewall(
method.setup_firewall( 1024, 1026,
1024, 1026, [(10, u'2404:6800:4004:80c::33')],
[(10, u'2404:6800:4004:80c::33')], 10,
10, [(10, 64, False, u'2404:6800:4004:80c::'),
[(10, 64, False, u'2404:6800:4004:80c::'), (10, 128, True, u'2404:6800:4004:80c::101f')],
(10, 128, True, u'2404:6800:4004:80c::101f')], False)
True)
assert str(excinfo.value) \
== 'Address family "AF_INET6" unsupported by pf method_name'
assert mock_pf_get_dev.mock_calls == []
assert mock_ioctl.mock_calls == []
assert mock_pfctl.mock_calls == []
assert mock_ioctl.mock_calls == [
call(mock_pf_get_dev(), 0xcd48441a, ANY),
call(mock_pf_get_dev(), 0xcd48441a, ANY),
]
assert mock_pfctl.mock_calls == [
call('-s Interfaces -i lo -v'),
call('-f /dev/stdin', b'match on lo\n'),
call('-s all'),
call('-a sshuttle6-1024 -f /dev/stdin',
b'table <forward_subnets> {'
b'!2404:6800:4004:80c::101f/128,2404:6800:4004:80c::/64'
b'}\n'
b'table <dns_servers> {2404:6800:4004:80c::33}\n'
b'pass in on lo0 inet6 proto tcp to '
b'<forward_subnets> divert-to ::1 port 1024\n'
b'pass in on lo0 inet6 proto udp '
b'to <dns_servers> port 53 rdr-to ::1 port 1026\n'
b'pass out inet6 proto tcp to '
b'<forward_subnets> route-to lo0 keep state\n'
b'pass out inet6 proto udp to '
b'<dns_servers> port 53 route-to lo0 keep state\n'),
call('-e'),
]
mock_pf_get_dev.reset_mock()
mock_ioctl.reset_mock()
mock_pfctl.reset_mock()
with pytest.raises(Exception) as excinfo: with pytest.raises(Exception) as excinfo:
method.setup_firewall( method.setup_firewall(
1025, 1027, 1025, 1027,
@ -374,14 +439,15 @@ def test_setup_firewall_openbsd(mock_pf_get_dev, mock_ioctl, mock_pfctl):
call(mock_pf_get_dev(), 0xcd48441a, ANY), call(mock_pf_get_dev(), 0xcd48441a, ANY),
] ]
assert mock_pfctl.mock_calls == [ assert mock_pfctl.mock_calls == [
call('-s Interfaces -i lo -v'),
call('-f /dev/stdin', b'match on lo\n'), call('-f /dev/stdin', b'match on lo\n'),
call('-s all'), call('-s all'),
call('-a sshuttle -f /dev/stdin', call('-a sshuttle-1025 -f /dev/stdin',
b'table <forward_subnets> {!1.2.3.66/32,1.2.3.0/24}\n' b'table <forward_subnets> {!1.2.3.66/32,1.2.3.0/24}\n'
b'table <dns_servers> {1.2.3.33}\n' b'table <dns_servers> {1.2.3.33}\n'
b'pass in on lo0 inet proto tcp divert-to 127.0.0.1 port 1025\n' b'pass in on lo0 inet proto tcp to <forward_subnets> divert-to 127.0.0.1 port 1025\n'
b'pass in on lo0 inet proto udp to ' b'pass in on lo0 inet proto udp to '
b'<dns_servers>port 53 rdr-to 127.0.0.1 port 1027\n' b'<dns_servers> port 53 rdr-to 127.0.0.1 port 1027\n'
b'pass out inet proto tcp to ' b'pass out inet proto tcp to '
b'<forward_subnets> route-to lo0 keep state\n' b'<forward_subnets> route-to lo0 keep state\n'
b'pass out inet proto udp to ' b'pass out inet proto udp to '
@ -395,7 +461,7 @@ def test_setup_firewall_openbsd(mock_pf_get_dev, mock_ioctl, mock_pfctl):
method.restore_firewall(1025, 2, False) method.restore_firewall(1025, 2, False)
assert mock_ioctl.mock_calls == [] assert mock_ioctl.mock_calls == []
assert mock_pfctl.mock_calls == [ assert mock_pfctl.mock_calls == [
call('-a sshuttle -F all'), call('-a sshuttle-1025 -F all'),
call("-d"), call("-d"),
] ]
mock_pf_get_dev.reset_mock() mock_pf_get_dev.reset_mock()

View File

@ -0,0 +1,68 @@
import os
import io
import socket
import sshuttle.server
from mock import patch, Mock, call
def test__ipmatch():
assert sshuttle.server._ipmatch("1.2.3.4") is not None
assert sshuttle.server._ipmatch("::1") is None # ipv6 not supported
assert sshuttle.server._ipmatch("42 Example Street, Melbourne") is None
def test__ipstr():
assert sshuttle.server._ipstr("1.2.3.4", 24) == "1.2.3.4/24"
assert sshuttle.server._ipstr("1.2.3.4", 32) == "1.2.3.4"
def test__maskbits():
netmask = sshuttle.server._ipmatch("255.255.255.0")
sshuttle.server._maskbits(netmask)
@patch('sshuttle.server.ssubprocess.Popen')
def test__listroutes(mock_popen):
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
""")
mock_pobj.wait.return_value = 0
mock_popen.return_value = mock_pobj
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),
(socket.AF_INET, '192.168.1.0', 24)
]
@patch('sshuttle.server.ssubprocess.Popen')
def test_listroutes(mock_popen):
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
""")
mock_pobj.wait.return_value = 0
mock_popen.return_value = mock_pobj
routes = sshuttle.server.list_routes()
assert list(routes) == [
(socket.AF_INET, '192.168.1.0', 24)
]

View File

@ -1,12 +1,16 @@
[tox] [tox]
downloadcache = {toxworkdir}/cache/ downloadcache = {toxworkdir}/cache/
envlist = envlist =
py26,
py27, py27,
py34,
py35, py35,
[testenv] [testenv]
basepython = basepython =
py26: python2.6
py27: python2.7 py27: python2.7
py34: python3.4
py35: python3.5 py35: python3.5
commands = commands =
py.test py.test