Configuring JVM per-socket SOCKS5 proxy

If for some reason sockets opened in JVM need to go through a SOCKS5 proxy, we can use the following example to do it:

val socket = if (useSocksProxy) {
    val sa =
        InetSocketAddress.createUnresolved(socksHost, socksPort)
    val proxy = Proxy(Proxy.Type.SOCKS, sa)
    Socket(proxy)
} else {
    Socket()
}

socket.connect(InetSocketAddress(ip, port))

Configuring SOCKS5 server

Install dante-server

apt-get install dante-server -y

Configure socks5 server

cat <<EOF > /etc/danted.conf
user.privileged: root
user.unprivileged: nobody

# The listening network interface or address.
internal: 0.0.0.0 port=1080

# The proxying network interface or address.
external: eno1

# socks-rules determine what is proxied through the external interface.
socksmethod: none

# client-rules determine who can connect to the internal interface.
clientmethod: none

client pass {
    from: 0.0.0.0/0 to: 0.0.0.0/0
}

socks pass {
    from: 0.0.0.0/0 to: 0.0.0.0/0
}
EOF

Restart socks5 server

sudo systemctl restart danted

Resources

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *