forked from cizra/pycat
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproxy.py
More file actions
204 lines (181 loc) · 7.78 KB
/
proxy.py
File metadata and controls
204 lines (181 loc) · 7.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env python3
import collections
import os
import socket
import threading
from select import select
from typing import Any, Callable, TypeVar
import attrs
import mcp
import mudtelnet
SINGLE_CLIENT = False
KeyType = TypeVar('KeyType')
def deep_update(mapping: dict[KeyType, Any], *updating_mappings: dict[KeyType, Any]) -> dict[KeyType, Any]:
updated_mapping = mapping.copy()
for updating_mapping in updating_mappings:
for k, v in updating_mapping.items():
if k in updated_mapping and isinstance(updated_mapping[k], dict) and isinstance(v, dict):
updated_mapping[k] = deep_update(updated_mapping[k], v)
else:
updated_mapping[k] = v
return updated_mapping
@attrs.define(slots=False)
class Client():
fd: socket.socket = attrs.field(repr=False)
addr: tuple[str, int]
oob_out: tuple[int, int] = attrs.field(repr=False)
has_gmcp: bool | None = None
has_mcp: bool | None = None
state: dict = attrs.field(factory=dict)
supported_mcp_packages: dict = attrs.field(factory=dict)
pipe_write = None
connection = mudtelnet.TelnetConnection(app_linemode=False)
def write(self, line: bytes | str) -> None:
if isinstance(line, str):
line = line.encode('utf-8')
if self.pipe_write is None:
self.pipe_write = os.fdopen(self.oob_out[1], 'wb')
self.pipe_write.write(line)
self.pipe_write.flush()
def handle_inbound_mcp(self, line: bytes) -> bytes:
parts = line.strip().split(b' ')
if parts[0] == b'#$#mcp' and parts[1] == b'authentication-key:':
self.has_mcp = True
self.state["mcp_key"] = parts[2].decode('utf-8')
elif parts[0] == b'#$#mcp-negotiate-can':
vals = mcp.parse_mcp_vars(parts)
self.supported_mcp_packages[vals['package']] = (vals['min-version'], vals['max-version'])
return line
# returns anonymous pipes (readableFromClient, writableToClient)
def proxy(bindAddr: str, listenPort: int) -> tuple[int, int, threading.Event, Callable, list[Client], dict[str, Any]]:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((bindAddr, listenPort))
sock.listen(5)
socketToPipeR, socketToPipeW = os.pipe()
pipeToSocketR, pipeToSocketW = os.pipe()
stop = threading.Event()
print(f'Listening on {bindAddr}:{listenPort}')
session_state = {}
clients: list[Client] = []
return socketToPipeR, pipeToSocketW, stop, lambda: serve(socketToPipeW, pipeToSocketR, sock, stop, clients, session_state), clients, session_state
def serve(PipeW: int, pipeToSocketR: int, sock: socket.socket, stop: threading.Event, clients: list[Client], session_state: dict[str, Any]) -> None:
socketToPipeW = os.fdopen(PipeW, 'wb')
clientSockets: list[socket.socket] = []
clientStates: dict[socket.socket, Client] = {}
clientPipes: list[int] = []
pipeToSocketBuffer: list[bytes] = []
lastTen = collections.deque(maxlen=40)
def remove_socket(fd: Any) -> Client:
fd.close()
clientSockets.remove(fd)
c = clientStates[fd]
clients.remove(c)
if c.pipe_write:
c.pipe_write.close()
clientPipes.remove(c.oob_out[0])
return c
def accept_new_client(sock: socket.socket) -> None:
print("new client")
if SINGLE_CLIENT and clientSockets: # If the user doesn't want to be connected with two clients at once.
print("booting old client")
clientSockets[0].sendall(b"Superseded. Bye!")
clientSockets[0].close()
clientSockets.clear()
clientSocket, addr = sock.accept()
clientSockets.append(clientSocket)
state = Client(clientSocket, addr, os.pipe())
clients.append(state)
clientStates[clientSocket] = state
clientPipes.append(state.oob_out[0])
if session_state.get('mcp_key'):
clientSocket.sendall(b"\n#$#mcp version: 2.1 to: 2.1\n")
state.has_mcp = False
neg = bytearray()
state.connection.start(neg)
clientSocket.sendall(neg)
for item in pipeToSocketBuffer or lastTen:
clientSocket.sendall(item)
pipeToSocketBuffer.clear()
def handle_client_input(fd: socket.socket) -> None:
try:
data: bytes = fd.recv(4096)
if not data: # disconnect
remove_socket(fd)
print("socket disconnected")
else:
s = clientStates[fd]
b = data
while b:
frame, size = mudtelnet.TelnetFrame.parse(b)
out_buffer = bytearray()
out_events = list()
changed = s.connection.process_frame(frame, out_buffer, out_events)
if changed:
print(repr(changed))
s.state = deep_update(s.state, changed)
for e in out_events:
if isinstance(e, mudtelnet.TelnetInMessage):
if e.data.startswith(b'#$#'):
e.data = s.handle_inbound_mcp(e.data)
if 'mcp_key' not in session_state and 'mcp_key' in s.state:
session_state['mcp_key'] = s.state['mcp_key']
# todo replace client key with session key
elif (ckey := s.state.get('mcp_key')) != (skey := session_state.get('mcp_key')) and ckey and skey:
e.data.replace(ckey.encode('utf-8'), skey.encode('utf-8'))
socketToPipeW.write(bytes(e.data))
socketToPipeW.flush()
else:
print('!! ' + repr(e))
b = b[size:]
except ConnectionResetError:
pass
except TimeoutError:
remove_socket(fd)
print("Socket timed out")
except OSError as e:
remove_socket(fd)
print(e)
while not stop.is_set():
fds, _, _ = select([sock, pipeToSocketR] + clientSockets + clientPipes, [], [])
for fd in fds:
if fd == sock:
accept_new_client(sock)
elif fd in clientSockets:
handle_client_input(fd)
elif fd == pipeToSocketR:
data = os.read(pipeToSocketR, 4096)
if not data:
print("EOF from pipe")
break
lastTen.append(data)
if not clientSockets:
pipeToSocketBuffer.append(data)
elif fd in clientPipes:
state = [c for c in clientStates.values() if c.oob_out[0] == fd][0]
data = os.read(fd, 4096)
state.fd.sendall(data)
print("Gracefully shutting down in serve")
if __name__ == "__main__":
def echo(socketToPipeR, pipeToSocketW, stopFlag):
pipeToSocketW = os.fdopen(pipeToSocketW, 'wb')
try:
while not stopFlag.is_set():
data = os.read(socketToPipeR, 4096)
print(b"Got %d, sleeping" % (len(data)))
import time
time.sleep(1)
print(b"Echoing %d" % (len(data)))
pipeToSocketW.write(data)
pipeToSocketW.flush()
except KeyboardInterrupt:
stopFlag.set()
print("Gracefully shutting down in echo")
socketToPipeR, pipeToSocketW, stopFlag, work = proxy('::1', 1234)
echoThr = threading.Thread(target=echo, args=[socketToPipeR, pipeToSocketW, stopFlag])
echoThr.start()
try:
work()
except KeyboardInterrupt:
stopFlag.set()
echoThr.join()