Redsocks cannot natively chain SOCKS5 proxies because it lacks support for upstream proxy types beyond direct TCP connections. To redirect incoming traffic through an external SOCKS5 server, you must configure the redsocks backend as socks5 and point it directly to the remote proxy host. This approach bypasses the need for chaining by treating the external SOCKS5 server as the final hop for all redirected packets.
Understanding Redsocks Limitations
The core issue arises when users attempt to use Redsocks to forward traffic from one SOCKS5 proxy to another. Redsocks is designed to intercept local traffic and send it to a single backend connection. It does not support nested proxy configurations where the backend itself requires authentication or further redirection via another proxy protocol like HTTP CONNECT or SOCKS4a. Attempting to use invalid backend types such astcp-connect or tcp-relay for a SOCKS5 destination will fail because these types are intended for raw TCP connections or specific relay protocols, not for negotiating a SOCKS5 handshake. The only valid backend type for connecting to a SOCKS5 server is socks5. Therefore, the solution relies on configuring Redsocks to connect directly to the outermost proxy in your chain. If your architecture involves multiple layers of proxies, you typically need to configure the innermost proxy to accept connections from the outermost one, rather than trying to force Redsocks to handle the middleman logic.
Configuring redsocks.conf
To set up Redsocks correctly, you must edit the configuration file, typically located at/etc/redsocks.conf or /etc/redsocks/redsocks.conf. The critical section is the redsocks block, which defines how traffic is intercepted and forwarded.
First, ensure the type is set to socks5. This tells Redsocks to perform the proper SOCKS5 handshake with the target server. Next, specify the local_ip and local_port that Redsocks will listen on for redirected packets. These should match the ports defined in your iptables rules. The most important fields are proxy_ip and proxy_port, which define the address of your external SOCKS5 proxy server.
If your external SOCKS5 proxy requires authentication, you can include the user and password fields within the same block. However, note that Redsocks sends these credentials in cleartext during the SOCKS5 handshake unless the connection is encrypted via TLS (which standard SOCKS5 does not do by default). For basic setups without auth, simply omitting these fields allows anonymous access if the proxy permits it.
Here is a minimal example of a correct configuration:
redsocks {
local_ip = 127.0.0.1;
local_port = 12345;
ip = 203.0.113.50;
port = 1080;
type = socks5;
// Optional: if your proxy requires authentication
// user = "username";
// password = "password";
}
In this configuration, replace 203.0.113.50 with the IP address of your external SOCKS5 server and 1080 with its listening port. The local_port of 12345 is arbitrary but must be consistent with your firewall rules.
Setting Up Iptables Redirect Rules
Once Redsocks is configured, you must instruct the Linux kernel to redirect incoming traffic to the local Redsocks listener. This is achieved usingiptables with the REDIRECT target. You need to apply these rules to the PREROUTING chain in the nat table to catch traffic before routing decisions are made.
Run the following command to redirect TCP traffic destined for non-local addresses to your local Redsocks port:
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 12345
sudo iptables -t nat -A PREROUTING -p tcp --dport 443 -j REDIRECT --to-port 12345
These rules assume you want to redirect HTTP (80) and HTTPS (443) traffic. Adjust the --dport values if you need to handle other services. The --to-port 12345 must match the local_port specified in your redsocks.conf.
It is crucial to exclude local traffic from being redirected, as this would cause loops or prevent access to localhost services. You can add an exception rule before the redirect rules:
sudo iptables -t nat -A PREROUTING -i lo -j RETURN
This ensures that traffic originating from or destined to the loopback interface is not processed by Redsocks.
Starting and Verifying the Service
After configuring both the application and the firewall, start the Redsocks service. On systemd-based systems, use:sudo systemctl start redsocks
sudo systemctl enable redsocks
Verify that Redsocks is running and listening on the expected port:
netstat -tlnp | grep 12345
You should see a process bound to 127.0.0.1:12345. Test the redirection by making a request from a client machine or locally using curl if applicable. Check the Redsocks logs (usually in /var/log/syslog or via journalctl -u redsocks) to confirm that connections are being established to the external SOCKS5 proxy.
For more details on managing proxy settings during system initialization, see How to Set Proxy on Login if Proxy Server is Reachable. Additionally, if you encounter issues with dynamic port forwarding over SSH, refer to Troubleshooting SSH: Unable to Forward Dynamic Port to Host.
Frequently Asked Questions
Can Redsocks chain multiple SOCKS5 proxies?
No, Redsocks does not support proxy chaining. It can only connect to one backend server. If you need to route traffic through multiple proxies, you must configure the first proxy to forward to the second, or use a different tool that supports nested connections.Why is my Redsocks connection failing with "Connection Refused"?
This usually indicates that the external SOCKS5 proxy is unreachable or blocking the connection. Verify theproxy_ip and proxy_port in your config, and ensure that your firewall allows outbound connections to that specific IP and port.
Does Redsocks support UDP traffic redirection?
Standard Redsocks only handles TCP traffic. If you need to redirect UDP traffic, you must use a separate module or tool likeredudp, which is often included in the same package but requires separate configuration and iptables rules.
How do I debug Redsocks connection issues?
Enable verbose logging by starting Redsocks with the-v flag or checking the system journal with journalctl -u redsocks -f. Look for errors related to authentication failures or network timeouts to pinpoint the problem.