简体   繁体   English

FTP 服务器未使用 sockets 将数据发送到 Python 中的代理

[英]FTP server not sending data to proxy in Python with sockets

I'm building a proxy in Python between a ftp client and a ftp server.我正在 ZF104B2DFAB9FE8C0676587292A636D3Z 客户端和 ftp 服务器之间的 Python 中构建代理。 All the control phase and data phase is well done (currently on active mode), but when I send the request (LIST, for example), server does not send the information back.所有控制阶段和数据阶段都做得很好(目前处于活动模式),但是当我发送请求(例如 LIST)时,服务器不会发回信息。 I don't know what is causing this fault...我不知道是什么导致了这个错误...

Here is the Wireshark capture:这是 Wireshark 的捕获:

Wireshark 捕获

And the corresponding code:以及对应的代码:

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

The IPs are the following: IP如下:
Client: 192.168.40.50客户:192.168.40.50
Proxy-client: 192.168.40.80代理客户端:192.168.40.80
Proxy-server: 192.168.30.80代理服务器:192.168.30.80
Server: 192.168.30.90服务器:192.168.30.90

Thanks in advance.提前致谢。

EDIT:编辑:

Changed order of listening to port, so that the port is listened before sending the PORT command and the LIST request.更改监听端口的顺序,以便在发送 PORT 命令和 LIST 请求之前监听端口。 Same result, connection refused:结果相同,连接被拒绝:

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)

From the Wireshark capture session, it sounds like the port you are sending to the FTP server and the port you are actually listening for data-connection on the proxy-server are not same hence FTP server reporting connection refused.从 Wireshark 捕获 session 中,听起来您发送到 FTP 服务器的端口和您实际在代理服务器上侦听数据连接的端口不同,因此 ZC728A493673C9A93AF54AZ7 服务器报告连接被拒绝。 Make sure the port server_socket2 is binding ie, port is equal to 256 * tuple[4] + tuple[5] .确保端口server_socket2已绑定,即port等于256 * tuple[4] + tuple[5] I believe the socket_option 25 refers to SO_BINDTODEVICE , in that case when you are binding the socket using bind() then there is no need to explicitly bind the socket to interface (in general use case).我相信 socket_option 25指的是SO_BINDTODEVICE ,在这种情况下,当您使用bind()绑定套接字时,无需将套接字显式绑定到接口(在一般用例中)。 Can you remove that line and check if it is still working?您可以删除该行并检查它是否仍在工作? It will be a learning for me too.这对我来说也将是一个学习。 Thanks.谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM