Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've been playing around a bit with different variations and can't seem to find something that quite works.

I want to be able to 'bridge' an SSL connection across a particular gateway node in the network. In particular I want to further be able to control the outgoing source port for the TCP connection.

I can use stunnel for part of this -- to bridge the SSL connection. I know I can use nc to set the source port for a connection, but how might I connect these two together to allow specifying the source port for the outgoing connection? (Or is there an alternative solution here I should be considering?)

I feel like if I can get nc to work as a transparent proxy this will be fine, since I can set the source port with it. I feel like I may just be missing something about how nc works. So just to provide a little more context, here's what I'm trying. Suppose I've got an SSL server running locally on 1235. I want to connect to it with an SSL client, but I want to make sure my outgoing source port for the connection is always, say, 1234. (I can connect directly with the client, but I'm given an arbitrary high-number port; I need to be able to specify it.)

My thought was trying to use nc as a bridge here, with something like:

# nc -l localhost:1234 | nc localhost:1235

This definitely establishes proxies something -- I see traffic flowing between the right endpoints -- but it doesn't seem to handle the SSL very well at all. The secure link won't get established and I can't actually communicate between the two using openssl client/server.

What might I need to do to get this working? I'm going to try to determine if stunnel can assist me here; I was thinking that between the two of them I might be able to get this working.

Alright, I have played with socat a little bit, which does seem to bridge the connection -- but I can't seem to figure out how to specify the source port. Any thoughts?

share|improve this question
A transparent proxy provides proxying without needing to specify the proxy settings in the client. Is this something you want also - you don't touch on it in the description. – Paul Jan 15 '12 at 21:26
I must ask why is specifying the source port required? This is highly unusual for TCP connections, especially when the requested port is "arbitrary". If this is required by a firewall, then said firewall should be thrown out. – grawity Jan 15 '12 at 21:45
up vote 1 down vote accepted

Pipes used by | are one-way. When you run nc | nc, the second connection's output goes to your terminal; the first connection's input is read from your keyboard. Basically, the full form of it would be </dev/tty nc | nc >/dev/tty.

To create a bidirectional tunnel, you would have to use something else, such as...

socat tcp-l:localhost:1234 tcp:localhost:1235

(Or create a named pipe and use it with two nc's, but that's less efficient.)

However, that will not do what you want. It will not make 1234 become the source port of the second connection; you will only have it as the destination port of the first connection, and the two connections are unrelated: stunnel simply receives bytes from both connections and resends them to the opposite end.

If you want simply to specify the source port of a connection, it's simpler:

nc -l 56789 localhost 1235

socat stdio tcp:localhost:1235,sourceport=56789

Since stunnel does not have a "local port" option, you can take one of the above commands and use it in the exec= option.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .