我正在尝试用 python 为托管在我自己的计算机上的 Minecraft 服务器编写代理。虽然我想拦截和修改客户端和服务器之间发送的包,但首先我只想发送所有包而不修改它们。问题是只发送了 2 个包:一个从服务器到客户端,一个从客户端到服务器。
我正在 python 中使用套接字和线程库。另外需要注意的是,在 server.properties 文件中,我关闭了 online_mode,因为当打开 online_mode 时,服务器尝试加密连接,这导致 Minecraft 在通过代理连接时卡在“正在加密...”。
虽然我已经尝试了很多次,但仍然得到相同的结果,所以这是来自 ChatGPT 的示例:
import socket
import threading
def handle_client(client_socket, target_host, target_port):
# Connect to the target server
target_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
target_socket.connect((target_host, target_port))
# Relay data between client and target
while True:
data = client_socket.recv(4096)
if len(data) == 0:
print("Client connection closed.")
break
print(f'Received from client: {data}')
target_socket.send(data)
print("Sent to target.")
response = target_socket.recv(4096)
if len(response) == 0:
print("Target connection closed.")
break
print(f'Received from target: {response}')
client_socket.send(response)
print("Sent to client.")
# Close the connections
client_socket.close()
target_socket.close()
def start_proxy(proxy_port, target_host, target_port):
# Create a server socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('localhost', proxy_port))
server_socket.listen(5)
print(f'Proxy server listening on port {proxy_port}')
while True:
client_socket, addr = server_socket.accept()
print(f'Accepted connection from {addr[0]}:{addr[1]}')
client_handler = threading.Thread(
target=handle_client,
args=(client_socket, target_host, target_port)
)
client_handler.start()
# Usage example
proxy_port = 55555
target_host = "localhost"
target_port = 25565
start_proxy(proxy_port, target_host, target_port)
我的 Minecraft 服务器在端口 25565 上的本地主机(也称为 127.0.0.1)上运行,代理侦听本地主机:55555。当我运行代码,然后通过 Minecraft 中的代理 (localhost:55555) 加入服务器时,我收到以下打印消息:
Proxy server listening on port 55555
Accepted connection from 127.0.0.1:52403
Received from client: b'\x10\x00\xfa\x05\tlocalhost\xd9\x03\x02"\x00\x0fBoterBramKroket\x01\x11Zu\x9eC\xa3N\xd6\xbf\x03\\\x00\xe6\x96\xcc\x00'
Sent to target.
Received from target: b'\x03\x03\x80\x02'
Sent to client.
Client connection closed.
我不明白为什么其他包裹不会被发送。我希望有人有编写 Minecraft 代理的经验并可以告诉我更多信息,提前致谢!
您好,我将测试如何在此处执行此代码。
import socket
import threading
class handle_server(threading.Thread):
def __init__(self,client_socket,target_host, target_port,target_socket):
super().__init__()
self.client_socket = client_socket
self.target_host = target_host
self.target_port = target_port
self.target_socket = target_socket
def run(self):
client_socket = self.client_socket
target_host = self.target_host
target_port = self.target_port
target_socket = self.target_socket
while True:
data = client_socket.recv(4096 * 8 * 8 * 8)
if len(data) == 0:
print("Client connection closed.")
break
print(f'Received from server: {data}')
target_socket.send(data)
print("Sent to target.")
client_socket.close()
target_socket.close()
class handle_client_Thread(threading.Thread):
def __init__(self,client_socket,target_host, target_port,target_socket):
self.client_socket = client_socket
self.target_host = target_host
self.target_port = target_port
self.target_socket = target_socket
super().__init__()
def run(self):
client_socket = self.client_socket
target_host = self.target_host
target_port = self.target_port
target_socket = self.target_socket
while True:
response = target_socket.recv(4096 * 8 * 8 * 8)
if len(response) == 0:
print("Target connection closed.")
break
print(f'Received from client: {response}')
client_socket.send(response)
print("Sent to client.")
client_socket.close()
target_socket.close()
def handle_client(client_socket, target_host, target_port):
# Connect to the target server
target_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
target_socket.connect((target_host, target_port))
handle_server(client_socket, target_host, target_port,target_socket).start()
handle_client_Thread(client_socket, target_host, target_port,target_socket).start()
def start_proxy(proxy_port, target_host, target_port):
# Create a server socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('localhost', proxy_port))
server_socket.listen(5)
print(f'Proxy server listening on port {proxy_port}')
while True:
client_socket, addr = server_socket.accept()
print(f'Accepted connection from {addr[0]}:{addr[1]}')
client_handler = threading.Thread(
target=handle_client,
args=(client_socket, target_host, target_port)
)
client_handler.start()
# Usage example
proxy_port = 55555
target_host = "localhost"
target_port = 25565
start_proxy(proxy_port, target_host, target_port)