forked from arpruss/rjmscratch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minecraftproxy.py
executable file
·79 lines (72 loc) · 2.27 KB
/
minecraftproxy.py
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
import logging
from websocket_server import WebsocketServer
import threading
import socket
import select
from sys import argv
from collections import namedtuple
import subprocess
tcpConnections = {}
def socket_readline(sock,id):
out = ""
while True:
ready = select.select([sock], [], [], 1)
if id not in tcpConnections:
return None
if ready[0]:
c = sock.recv(1)
if len(c):
if c == b'\n':
return out
elif c != b'\r':
out += c.decode()
def receive_thread(client, sock):
id = client['id']
out = bytearray()
while True:
ready = select.select([sock], [], [], 1)
if id not in tcpConnections:
print("closed")
return
if ready[0]:
data = sock.recv(128)
for i in range(len(data)):
c = data[i]
if c == 10:
server.send_message(client, out.decode()) # TODO: think about encodings
out = bytearray()
elif c != 13:
out.append(c)
def new_client(client, server):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('localhost', 4711))
id = client['id']
tcpConnections[id] = s
print("new client",id)
t = threading.Thread(target=receive_thread, args=(client, s))
t.daemon = True
t.start()
def client_left(client, server):
id = client['id']
if id in tcpConnections:
print("client gone",id)
tcpConnections[id].close()
del tcpConnections[id]
def message_received(client,server,data):
id = client['id']
if id in tcpConnections:
if data == "player.getRotation()":
server.send_message(client, "0")
tcpConnections[id].sendall((data+'\n').encode())
server = WebsocketServer(14711, '0.0.0.0')
server.set_fn_new_client(new_client)
server.set_fn_client_left(client_left)
server.set_fn_message_received(message_received)
if len(argv)>1:
def run():
subprocess.run(argv[1], shell=True)
server.shutdown()
t = threading.Thread(target=run)
t.daemon = True
t.start()
server.run_forever()