Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 107 lines (93 sloc) 3.425 kb
da774f3f »
2010-10-01 import and use subprocess.py from python 2.6.
1 import sys, os, re, socket, zlib
2 import compat.ssubprocess as ssubprocess
8fde1155 »
2011-01-25 Implement the optional fullness checking a bit more like I like it.
3 import helpers
4a462258 »
2010-05-02 ssh.py: support finding sshuttle in "$HOME/.../sshuttle"
4 from helpers import *
2f3c86e9 »
2010-05-01 Initial commit.
5
8fe3592b »
2010-05-04 Don't require the remote server to have sshuttle installed.
6
7 def readfile(name):
8 basedir = os.path.dirname(os.path.abspath(sys.argv[0]))
a8b3d698 »
2010-05-12 ssh.py: try harder to find required *.py files.
9 path = [basedir] + sys.path
10 for d in path:
11 fullname = os.path.join(d, name)
12 if os.path.exists(fullname):
13 return open(fullname, 'rb').read()
14 raise Exception("can't find file %r in any of %r" % (name, path))
8fe3592b »
2010-05-04 Don't require the remote server to have sshuttle installed.
15
16
8fde1155 »
2011-01-25 Implement the optional fullness checking a bit more like I like it.
17 def empackage(z, filename, data=None):
da774f3f »
2010-10-01 import and use subprocess.py from python 2.6.
18 (path,basename) = os.path.split(filename)
8fde1155 »
2011-01-25 Implement the optional fullness checking a bit more like I like it.
19 if not data:
20 data = readfile(filename)
21 content = z.compress(data)
8fe3592b »
2010-05-04 Don't require the remote server to have sshuttle installed.
22 content += z.flush(zlib.Z_SYNC_FLUSH)
8fde1155 »
2011-01-25 Implement the optional fullness checking a bit more like I like it.
23 return '%s\n%d\n%s' % (basename, len(content), content)
8fe3592b »
2010-05-04 Don't require the remote server to have sshuttle installed.
24
25
8fde1155 »
2011-01-25 Implement the optional fullness checking a bit more like I like it.
26 def connect(ssh_cmd, rhostport, python, stderr, options):
2f3c86e9 »
2010-05-01 Initial commit.
27 main_exe = sys.argv[0]
8173925b »
2010-05-04 ssh.py: allow hostnames of the form hostname:port
28 portl = []
95c9b788 »
2010-11-19 Add support for IPv6 remote hosts.
29
30 rhostIsIPv6 = False
918725c4 »
2010-12-09 Oops, earlier ipv6 patch didn't work if no -r option is specified.
31 if (rhostport or '').count(':') > 1:
95c9b788 »
2010-11-19 Add support for IPv6 remote hosts.
32 rhostIsIPv6 = True
33 if rhostport.count(']') or rhostport.count('['):
34 result = rhostport.split(']')
35 rhost = result[0].strip('[')
36 if len(result) > 1:
37 result[1] = result[1].strip(':')
38 if result[1] is not '':
39 portl = ['-p', str(int(result[1]))]
40 else: # can't disambiguate IPv6 colons and a port number. pass the hostname through.
41 rhost = rhostport
42 else: # IPv4
43 l = (rhostport or '').split(':', 1)
44 rhost = l[0]
45 if len(l) > 1:
46 portl = ['-p', str(int(l[1]))]
8fe3592b »
2010-05-04 Don't require the remote server to have sshuttle installed.
47
2f3c86e9 »
2010-05-01 Initial commit.
48 if rhost == '-':
49 rhost = None
8fe3592b »
2010-05-04 Don't require the remote server to have sshuttle installed.
50
95c9b788 »
2010-11-19 Add support for IPv6 remote hosts.
51 ipv6flag = []
52 if rhostIsIPv6:
53 ipv6flag = ['-6']
54
8fe3592b »
2010-05-04 Don't require the remote server to have sshuttle installed.
55 z = zlib.compressobj(1)
56 content = readfile('assembler.py')
8fde1155 »
2011-01-25 Implement the optional fullness checking a bit more like I like it.
57 optdata = ''.join("%s=%r\n" % (k,v) for (k,v) in options.items())
58 content2 = (empackage(z, 'cmdline_options.py', optdata) +
59 empackage(z, 'helpers.py') +
da774f3f »
2010-10-01 import and use subprocess.py from python 2.6.
60 empackage(z, 'compat/ssubprocess.py') +
8fe3592b »
2010-05-04 Don't require the remote server to have sshuttle installed.
61 empackage(z, 'ssnet.py') +
33efa5ac »
2010-05-08 Added new --auto-hosts and --seed-hosts options to the client.
62 empackage(z, 'hostwatch.py') +
8fe3592b »
2010-05-04 Don't require the remote server to have sshuttle installed.
63 empackage(z, 'server.py') +
64 "\n")
65
66 pyscript = r"""
67 import sys;
68 skip_imports=1;
69 verbosity=%d;
70 exec compile(sys.stdin.read(%d), "assembler.py", "exec")
8fde1155 »
2011-01-25 Implement the optional fullness checking a bit more like I like it.
71 """ % (helpers.verbose or 0, len(content))
8fe3592b »
2010-05-04 Don't require the remote server to have sshuttle installed.
72 pyscript = re.sub(r'\s+', ' ', pyscript.strip())
73
74
2f3c86e9 »
2010-05-01 Initial commit.
75 if not rhost:
1ca8aa5b »
2011-02-07 server: workaround for idiotic ArchLinux renaming of python to python2.
76 # ignore the --python argument when running locally; we already know
77 # which python version works.
78 argv = [sys.argv[1], '-c', pyscript]
2f3c86e9 »
2010-05-01 Initial commit.
79 else:
32b4defa »
2010-11-09 Add a new --ssh-cmd= option to let you override the ssh command.
80 if ssh_cmd:
81 sshl = ssh_cmd.split(' ')
82 else:
83 sshl = ['ssh']
1ca8aa5b »
2011-02-07 server: workaround for idiotic ArchLinux renaming of python to python2.
84 if python:
85 pycmd = "'%s' -c '%s'" % (python, pyscript)
86 else:
87 pycmd = ("P=python2; $P -V 2>/dev/null || P=python; "
65b0390f »
2011-05-03 ssh.py: use 'exec python -c' instead of just 'python -c'.
88 "exec \"$P\" -c '%s'") % pyscript
32b4defa »
2010-11-09 Add a new --ssh-cmd= option to let you override the ssh command.
89 argv = (sshl +
90 portl +
95c9b788 »
2010-11-19 Add support for IPv6 remote hosts.
91 ipv6flag +
1ca8aa5b »
2011-02-07 server: workaround for idiotic ArchLinux renaming of python to python2.
92 [rhost, '--', pycmd])
5f0bfb5d »
2010-05-01 Basic implementation of a multiplex protocol - client side only.
93 (s1,s2) = socket.socketpair()
2f3c86e9 »
2010-05-01 Initial commit.
94 def setup():
95 # runs in the child process
5f0bfb5d »
2010-05-01 Basic implementation of a multiplex protocol - client side only.
96 s2.close()
97 s1a,s1b = os.dup(s1.fileno()), os.dup(s1.fileno())
98 s1.close()
8fe3592b »
2010-05-04 Don't require the remote server to have sshuttle installed.
99 debug2('executing: %r\n' % argv)
da774f3f »
2010-10-01 import and use subprocess.py from python 2.6.
100 p = ssubprocess.Popen(argv, stdin=s1a, stdout=s1b, preexec_fn=setup,
8a5ae1a4 »
2010-12-31 Rearrange daemonization/syslog stuff and make it more resilient.
101 close_fds=True, stderr=stderr)
5f0bfb5d »
2010-05-01 Basic implementation of a multiplex protocol - client side only.
102 os.close(s1a)
103 os.close(s1b)
8fe3592b »
2010-05-04 Don't require the remote server to have sshuttle installed.
104 s2.sendall(content)
105 s2.sendall(content2)
5f0bfb5d »
2010-05-01 Basic implementation of a multiplex protocol - client side only.
106 return p, s2
Something went wrong with that request. Please try again.