SocketShifter – Warping the Internet Space Time Continuum with the Azure .NET Service Bus

We were in Redmond last week building out some code on the Azure Services Platform and specifically the .NET Services Service Bus.

The Service Bus provides a way of connecting two endpoints on the Internet using WCF. It automagically handles any intermediate firewall traversal. We wondered whether it was possible to use this mechanism to effectively remote any TCP socket – SocketShifter was born !

SocketShifter consists of a server process and a client process. The client process listens on an arbitrary TCP socket which we then connect, via the service bus, to a TCP socket that we establish on the server. So, for instance, a SocketShifter client listening on port 1000 on your laptop can connect to a SocketShifter server running on your home machine to allow you to Remote Desktop into your home PC on port 3389, despite any firewalls that might be in the way.

The magic is achieved by the SocketShifter server implementing an interface, like this:

[ServiceContract(CallbackContract = typeof(IClient), SessionMode = SessionMode.Required)]
public interface IServer
{
 [OperationContract]
 void Connect(string host, int port);

 [OperationContract(IsOneWay = true)]
 void Send(Byte[] buffer);

 [OperationContract]
 void Disconnect();
}

and the SocketShifter client implementing an interface like this:

public interface IClient
{
 [OperationContract(IsOneWay = true)]
 void Receive(byte[] buffer);
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }The client can then connect to the server using the service bus using NetTcpRelayBinding and a duplex channel is established. The client calls the server contract to send data and also implements the callback contract specified by the server to receive data in the return direction. All we then have to do is set up the ports on either end with a TcpListener and a TcpClient and forward all traffic via WCF and the .NET Service Bus.

We’ve posted the code at socketshifter.codeplex.com – See the README.txt for more information and detailed instructions.

Here are some other scenarios that SocketShifter enables:

Manage a SQL Server behind your corporate firewall using SQL Server Management Studio.

Connect via SSH to a UNIX box behind your corporate firewall.

Access your corporate Intranet from a coffee shop.

Our thanks to Clemens Vasters, David Aiken, Mohit Srivastava, Remy Pairault for their, help, inspiration and hospitality last week.

Also thanks to Simon Davies for reviewing the code and providing constructive feedback and fixes.

Rich & Rob

Advertisements