3
\$\begingroup\$

Many applications that depend on network resources (such as games connecting to game servers) do not allow the user to set a proxy. There are a lot of ways to force the usage of a proxy regardless, I personally have always been a fan of a local SOCKS tunnel.

What's that?

Instead of connecting directly, an application on your local machine listens on a locally bound address and relays any connection to a fixed endpoint using a remote SOCKS proxy.

Example: Let our target be 11.22.33.44:5555, and our remote socks proxy be 99.88.77.66:5555. Our application listens on 127.0.0.1:5555. Our route now looks like this:

connect to 127.0.0.1:5555 - relays via SOCKS proxy > 99.88.77.66:5555 - to > 11.22.33.44:5555

Your goal is to implement the local application relaying connections to the target.

Input

Three address/port pairs representing the bind, proxy and target address.

  • You may deviate from the order (but specify if you do).
  • Address parsing is up to your language/tools of choice, but should at the very minimum support IPv4 (11.22.33.44).
  • You may use builtin types of your language. (like InetSocketAddress in Java, struct in_addr in C, etc.)

Output

No rules for output due to .

Additional

  • This is , shortest answer in bytes wins.
  • Standard rules apply, default loopholes are forbidden.
  • You do not have to support more than 1 connection. The program may terminate after the first connection finishes.
  • Program behaviour under network failures is undefined; you may crash the entire program, reject the incoming connection, do whatever you want.
  • You may assume the remote SOCKS proxy's version (4/4a/5).
  • Using libraries/inbuilts/etc. is encouraged, but not required.
  • Stay in input scope (f.e. don't proxy all connections on the machine).
\$\endgroup\$
  • \$\begingroup\$ So the task is to create a local SOCKS server that accepts local connections (e.g. from a suitably configured browser) and connects to a remote SOCKS proxy that forwards the connection to the far end (e.g. web server)? - its not entirely clear to me. \$\endgroup\$ – Digital Trauma Mar 29 '17 at 20:27
  • \$\begingroup\$ @DigitalTrauma Not a local SOCKS server, but more a local tunnel server using a fixed SOCKS proxy to forward connections to a fixed end. \$\endgroup\$ – F. George Mar 29 '17 at 21:15
  • 1
    \$\begingroup\$ So just a simple local TCP port forward then? - that itself knows nothing of the SOCKS protocol? This is not really clear to me \$\endgroup\$ – Digital Trauma Mar 29 '17 at 22:48
  • \$\begingroup\$ I'm not sure what else there is to explain? From the point of the connecting application it is effectively a port forward. The impementing application obviously has to know about the SOCKS protocol. I've even given a sample answer in Kotlin. \$\endgroup\$ – F. George Mar 30 '17 at 7:10
0
\$\begingroup\$

Kotlin, 240 bytes

This was fun and taught me some things I didn't know about Kotlin.

import java.net.*fun a(vararg p:InetSocketAddress){ServerSocket().run{bind(p[0])
accept().let{Socket(Proxy(Proxy.Type.SOCKS,p[1])).run{connect(p[2])
Thread{it.inputStream.copyTo(outputStream)}.start()
inputStream.copyTo(it.outputStream)}}}}

Tested locally with:

fun main(args: Array<String>) {
    thread { a(InetSocketAddress(2000), InetSocketAddress(3000), InetSocketAddress(4000)) }
    thread { Socket().connect(InetSocketAddress(2000)) }
    ServerSocket(3000).accept().getInputStream().run { generateSequence { read() } }
            .forEach { print("$it,") }
}

Prints 5,2,0,2, which confirms an attempted SOCKS5 connection.

| improve this answer | |
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.