ferrency has asked for the wisdom of the Perl Monks concerning the following question:
I am currently working on a proxy server, which opens and maintains an SSL connection, accepts normal TCP connections, and then proxies information in both directions in a protocol-independant way (though it is limited to text line oriented and not binary protocols).
The technique I'm using for proxying is similar to a recipe in the Perl Cookbook: chapter 17.10. This recipe uses forking to build a bidirectional TCP client. When a connection is accepted, the process forks. The parent does a blocking read on the connecting socket, and blocking writes to the SSL socket. The child does a blocking read on the SSL socket and blocking writes to the connecting socket. To quote the cookbook, "To accomplish the same thing using just one process is remarkably more difficult."
Here is a (pseudo) code sample which demonstrates the technique for a single-connection proxy:
I open one SSL socket, and keep it open for use with multiple connecting sockets. This cuts down on the overhead of establishing SSL connections, and is really the whole point of using this proxy server in the first place. So rebuilding the SSL socket for every connection isn't really an option :)my $asock = new IO::Socket::INET (..., Listen => 5); # accepting socket my $sslsock = new IO::Socket::SSL(..., PeerAddr => ...,); # SSL socket while (1) { my $csock = $psock->accept(); # Accept $csock, connecting socket if (my $p = fork()) { my $line; $sslsock->print($line) while (defined($line = <$csock>)); # Now the connecting socket disconnected kill 1, $p; wait (); } else { my $line; $csock->print($line) while (defined($line = <$sslsock>)); # Lost SSL connection... this may cause our parent to block # forever, but this is just for demonstration of the Real prob +lem... exit; } }
The technique above works, if $sslsock is an IO::Socket::INET instead of an IO::Socket::SSL. It also works for the first connection, using an SSL socket for the outgoing socket. But after the child process exits (or is killed by its parent), something deep within the SSL object is broken, and the $sslsock stops working correctly. This is not a problem with IO::Socket::SSL, but with Net::SSLeay or something deeper in the C libraries- I rewrote the server using Net::SSLeay instead of IO::Socket::SSL and the same problem exists there.
So, after all this explanation, my questions are:
Thanks in advance for any help you can give me on this!
Alan
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: TCP - SSL proxy problems
by ferrency (Deacon) on Jan 19, 2002 at 01:29 UTC |