TlsProxy

Shows how to use TlsServerSocket class to write custom TLS proxy server.

Usage

This sample is a console application that provides TLS proxy server functionality. It listens on specified port for incoming TCP connections, secures them using TLS and forwards all communication between the connected client and the specified remote host. This makes it possible to add TLS 1.3/1.2/1.1/1.0 security to existing servers using plain (unencrypted) protocols

For example, to secure your HTTP service running at IP address 192.168.0.1, making it accessible via HTTPS protocol:

> TlsProxy 0.0.0.0:443 192.168.0.1:80 c:\data\cert.pfx password

C#

// start the server
var server = new TcpListener(IPAddress.Any, 1234);
server.Start();

// accept incoming connection
var inboundSocket = server.AcceptSocket();

// create an instance of TLS server socket
using (var socket = new TlsServerSocket(inboundSocket))
{
    // log communication
    socket.LogWriter = new ConsoleLogWriter(LogLevel.Info);

    // specify certificate to be used for server authentication
    socket.Parameters.Certificate = CertificateChain.LoadPfx(certPath, certPassword);

    // negotiate TLS layer
    socket.Negotiate();

    // receive requests and send responses
    byte[] buffer = new byte[8 * 1024];
    // ... socket.Receive(buffer);
    // ... socket.Send(buffer);
}

Also see Rebex TLS Proxy, our simple yet powerful TLS server with rich command-line interface.