ShadowsocksR UDP Proxy: Handling Incoming UDP Requests
The StartUDP() function, part of the ShadowsocksRProxy struct, is responsible for initiating a listener for incoming UDP requests and processing them within a ShadowsocksR proxy. It achieves this by creating a new ShadowsocksRDecorate instance for each incoming request, effectively handling the request's encryption and decryption. This process leverages the network.NewShadowsocksRDecorate function, which initializes a ShadowsocksRDecorate object using the provided parameters, including encryption method, password, protocol, and other configuration details. To manage connections and handle potential timeouts, the function employs a NewShadowsocksRUDPMap instance with a default timeout of 30 seconds.
Upon receiving a UDP request, the function reads the data and extracts the remote address. It then generates a ShadowsocksRUDPMapItem instance to store essential information related to the UDP connection, such as the connection itself and associated metadata. These connection details are subsequently added to the ShadowsocksRUDPMap using the udpMap.Add function. If a connection for the same client address already exists, the existing connection is reused instead of creating a new one.
If the proxy has a configured HostFirewall, it checks if the client address is permitted by the firewall. If the address is not allowed, the request is not processed, and the function returns without further action.
Finally, the remotePacketConn.WriteTo function is used to write the received data to the remote address. If an error occurs during this process, an error message is logged for troubleshooting purposes.
Here's a breakdown of the key functionalities within the code:
-
Request Handling: Each incoming UDP request triggers the creation of a
ShadowsocksRDecorateinstance, which handles encryption and decryption according to the proxy's configuration. -
Connection Management: The
NewShadowsocksRUDPMapinstance is used to manage UDP connections, including establishing new connections and reusing existing connections for the same client address. -
Firewall Integration: The code checks against a configured
HostFirewallto ensure the client address is allowed before processing the request. -
Data Transmission: The
remotePacketConn.WriteTofunction sends the data to the remote address, and error handling is implemented to capture potential transmission issues.
原文地址: https://www.cveoy.top/t/topic/lL1o 著作权归作者所有。请勿转载和采集!