Hello fellow monks.

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:

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; } }
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 :)

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:

  1. Does anyone have any ideas on how exactly the SSL connection is broken when the child dies?
  2. Is there a way to fix it or prevent it from breaking in the first place?
  3. In the latest IO::Socket::SSL I see support for an OpenSSL module instead of Net::SSLeay. Where is this module? Not in CPAN, it seems.
  4. Got any better ideas that Do work?
(Version notes: FreeBSD 4.1.1-stable, Perl 5.6.0, Net::SSLeay 1.12, IO::Socket::SSL 0.80, OpenSSL 0.96b)

Thanks in advance for any help you can give me on this!

Alan


In reply to TCP - SSL proxy problems by ferrency

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.