gpt4 book ai didi

python - FTP服务器不使用套接字将数据发送到Python中的代理

转载 作者:行者123 更新时间:2023-12-03 12:07:44 24 4
gpt4 key购买 nike

我在 ftp 客户端和 ftp 服务器之间用 Python 构建代理。所有控制阶段和数据阶段都做得很好(目前处于事件模式),但是当我发送请求(例如 LIST)时,服务器不会发回信息。我不知道是什么导致了这个错误...

这是 Wireshark 的捕获:

Wireshark capture

以及对应的代码:

print(f"Waiting for a message from the server to the PORT message")
answer = fw_proxy_server.recv(BUFFER_FTP)
print(answer) # 200 PORT command successful
fw_proxy_client.send(answer) # forward to client

# Create the socket to listen on 192.168.30.80:port (IP_PROXY:port)
server_socket2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket2.setsockopt(socket.SOL_SOCKET, 25, str(INTERFACE_PROXY_SERVER + '\0').encode('utf-8'))
server_socket2.bind((IP_PROXY, port))
server_socket2.listen()

print(f"Waiting for a message from the client")
message = fw_proxy_client.recv(BUFFER_FTP) # REQUEST: LIST

print(f"Forwarding message from the server")
message = send(fw_proxy_server, "LIST") # REQUEST: LIST (sent to port 21)

# Accept an incoming connection from the Server with data

server_socket2, data_address = server_socket2.accept() # HERE PROGRAM HANG
print(f"Data connection from {data_address} has been established!")

print(f"Waiting for a message from the data from server")
message = fw_proxy_server.recv(BUFFER_FTP) # never executed

IP如下:
客户:192.168.40.50
代理客户端:192.168.40.80
代理服务器:192.168.30.80
服务器:192.168.30.90

提前致谢。

编辑:

更改监听端口的顺序,以便在发送 PORT 命令和 LIST 请求之前监听端口。结果相同,连接被拒绝:
start = str(message).find("(")
end = str(message).find(")")
tuple = str(message)[start+1:end].split(',')
port = int(tuple[4])*256 + int(list(filter(str.isdigit, tuple[5]))[0])
#Create the socket to listen on 192.168.40.80:port
server_socket2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket2.setsockopt(socket.SOL_SOCKET, 25, str(INTERFACE_PROXY_SERVER + '\0').encode('utf-8'))
server_socket2.bind(("192.168.40.80", port)) # With 192.168.30.80 it doesn't work too
server_socket2.listen()

answer = send(fw_proxy_server, "PORT 192,168,30,80," + tuple[4] + "," + tuple[5])
print(f"Waiting for a message from the server to the PORT message")
print(answer) # 200 PORT command successful
fw_proxy_client.send(answer)

#Create the socket to forward the data to the server

print(f"Waiting for a request from the client") # REQUEST: LIST
message = fw_proxy_client.recv(BUFFER_FTP)
print(message)

fw_proxy_server.send(message)
print(f"Waiting for an answer from the server") # Response 425: Unable to build data connection: Connection refused
answer = fw_proxy_server.recv(BUFFER_FTP)
print(answer)


#Accept an incoming connection from the Client
server_socket2, data_address = server_socket2.accept() #Program hang
print(f"Data connection from {data_address} has been established!")

print(f"Waiting for a message from the server")
print(answer)

最佳答案

从 Wireshark 捕获 session 中,听起来您发送到 FTP 服务器的端口和您在代理服务器上实际监听数据连接的端口不同,因此 FTP 服务器报告连接被拒绝。确保端口 server_socket2具有约束力,即 port等于 256 * tuple[4] + tuple[5] .我相信 socket_option 25SO_BINDTODEVICE , 在这种情况下,当您使用 bind() 绑定(bind)套接字时那么就不需要显式地将套接字绑定(bind)到接口(interface)(在一般用例中)。您可以删除该行并检查它是否仍在工作?这对我来说也是一个学习!谢谢。

关于python - FTP服务器不使用套接字将数据发送到Python中的代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61965266/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com