Commit | Line | Data |
---|---|---|
b4cc75dc FT |
1 | import sys, os |
2 | import subprocess, socket, fcntl, select | |
3 | ||
4 | class sshsocket(object): | |
5 | def __init__(self, host, path, user = None, port = None): | |
6 | args = ["ssh"] | |
7 | if user is not None: | |
8 | args += ["-u", str(user)] | |
9 | if port is not None: | |
10 | args += ["-p", str(int(port))] | |
11 | args += [host] | |
16e7fd3d | 12 | args += ["python3", "-m", "pdm.sshsock", path] |
b4cc75dc FT |
13 | self.proc = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, close_fds=True) |
14 | fcntl.fcntl(self.proc.stdout, fcntl.F_SETFL, fcntl.fcntl(self.proc.stdout, fcntl.F_GETFL) | os.O_NONBLOCK) | |
34a3ccd6 | 15 | head = self.recv(5) |
98ecc02e | 16 | if head != b"SSOCK": |
34a3ccd6 FT |
17 | raise socket.error("unexpected reply from %s: %r" % (host, head)) |
18 | head = self.recv(1) | |
98ecc02e FT |
19 | if head == b"+": |
20 | buf = b"" | |
e3250828 FT |
21 | while True: |
22 | r = self.recv(1) | |
98ecc02e | 23 | if r == b"": |
e3250828 | 24 | raise socket.error("unexpected EOF in SSH socket stream") |
98ecc02e | 25 | elif r == b"\n": |
e3250828 FT |
26 | break |
27 | buf += r | |
34a3ccd6 | 28 | return |
98ecc02e FT |
29 | elif head == b"-": |
30 | buf = b"" | |
34a3ccd6 FT |
31 | while True: |
32 | r = self.recv(1) | |
98ecc02e | 33 | if r in {b"\n", b""}: |
34a3ccd6 FT |
34 | break |
35 | buf += r | |
98ecc02e | 36 | raise socket.error(buf.decode("utf-8")) |
34a3ccd6 FT |
37 | else: |
38 | raise socket.error("unexpected reply from %s: %r" % (host, head)) | |
b4cc75dc FT |
39 | |
40 | def close(self): | |
41 | if self.proc is not None: | |
42 | self.proc.stdin.close() | |
43 | self.proc.stdout.close() | |
44 | self.proc.wait() | |
45 | self.proc = None | |
46 | ||
47 | def send(self, data, flags = 0): | |
48 | self.proc.stdin.write(data) | |
49 | return len(data) | |
50 | ||
51 | def recv(self, buflen, flags = 0): | |
52 | if (flags & socket.MSG_DONTWAIT) == 0: | |
53 | select.select([self.proc.stdout], [], []) | |
54 | return self.proc.stdout.read(buflen) | |
55 | ||
56 | def fileno(self): | |
57 | return self.proc.stdout.fileno() | |
58 | ||
59 | def __del__(self): | |
60 | self.close() | |
61 | ||
62 | def cli(): | |
16e7fd3d | 63 | fcntl.fcntl(sys.stdin.buffer, fcntl.F_SETFL, fcntl.fcntl(sys.stdin.buffer, fcntl.F_GETFL) | os.O_NONBLOCK) |
b4cc75dc FT |
64 | sk = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
65 | try: | |
34a3ccd6 FT |
66 | try: |
67 | sk.connect(sys.argv[1]) | |
68 | except socket.error as err: | |
69 | sys.stdout.write("SSOCK-connect: %s\n" % err) | |
1a8ebe55 | 70 | sys.stdout.flush() |
34a3ccd6 FT |
71 | return |
72 | sys.stdout.write("SSOCK+\n") | |
1a8ebe55 | 73 | sys.stdout.flush() |
16e7fd3d FT |
74 | buf1 = b"" |
75 | buf2 = b"" | |
b4cc75dc FT |
76 | while True: |
77 | wfd = [] | |
78 | if buf1: wfd.append(sk) | |
16e7fd3d FT |
79 | if buf2: wfd.append(sys.stdout.buffer) |
80 | rfd, wfd, efd = select.select([sk, sys.stdin.buffer], wfd, []) | |
b4cc75dc FT |
81 | if sk in rfd: |
82 | ret = sk.recv(65536) | |
16e7fd3d | 83 | if ret == b"": |
b4cc75dc FT |
84 | break |
85 | else: | |
86 | buf2 += ret | |
16e7fd3d FT |
87 | if sys.stdin.buffer in rfd: |
88 | ret = sys.stdin.buffer.read() | |
89 | if ret == b"": | |
b4cc75dc FT |
90 | break |
91 | else: | |
92 | buf1 = ret | |
93 | if sk in wfd: | |
94 | ret = sk.send(buf1) | |
95 | buf1 = buf1[ret:] | |
16e7fd3d FT |
96 | if sys.stdout.buffer in wfd: |
97 | sys.stdout.buffer.write(buf2) | |
98 | sys.stdout.buffer.flush() | |
99 | buf2 = b"" | |
b4cc75dc FT |
100 | finally: |
101 | sk.close() | |
102 | ||
103 | if __name__ == "__main__": | |
104 | cli() |