11

I'm building a secure WebSockets client using C# and the WebSocket4Net library. I would like for all my connections to be proxied through a standard proxy.

This lib uses the SuperSocket.ClientEngine.Common.IProxyConnector to specify a websocket connection's proxy, but I'm not sure how I'm supposed to implement that.

Has anyone worked with this library and can offer some advice?

wonea
  • 4,783
  • 17
  • 86
  • 139
blizz
  • 4,102
  • 6
  • 36
  • 60

1 Answers1

20

I had to do the same, to push all websocket connections through Fiddler, for easier debugging. Because the WebSocket4Net author chose to re-use his IProxyConnector interface, System.Net.WebProxy is not directly useable.

On this link the author suggests using the implementations from his parent library SuperSocket.ClientEngine which you can download from CodePlex and include both the SuperSocket.ClientEngine.Common.dll and SuperSocket.ClientEngine.Proxy.dll. I do not recommend this. This causes compiling issues because he (poorly) chose to use the same namespace with both ClientEngine and WebSocket4Net with IProxyConnector defined in both dll's.


What worked for me:

To get it working for debugging through Fiddler, I copied these two classes into my solution, and changed them to the local namespace:

HttpConnectProxy seemed to have a bug on the following line:

if (e.UserToken is DnsEndPoint)

change to:

if (e.UserToken is DnsEndPoint || targetEndPoint is DnsEndPoint)


After that, things worked fine. Sample code:

private WebSocket _socket;

public Initialize()
{
    // initialize the client connection
    _socket = new WebSocket("ws://echo.websocket.org", origin: "http://example.com");

    // go through proxy for testing
    var proxy = new HttpConnectProxy(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888));
    _socket.Proxy = (SuperSocket.ClientEngine.IProxyConnector)proxy;

    // hook in all the event handling
    _socket.Opened += new EventHandler(OnSocketOpened);
    //_socket.Error += new EventHandler<ErrorEventArgs>(OnSocketError);
    //_socket.Closed += new EventHandler(OnSocketClosed);
    //_socket.MessageReceived += new EventHandler<MessageReceivedEventArgs>(OnSocketMessageReceived);

    // open the connection if the url is defined
    if (!String.IsNullOrWhiteSpace(url))
        _socket.Open();
}

private void OnSocketOpened(object sender, EventArgs e)
{
    // send the message
    _socket.Send("Hello World!");
}
James
  • 270
  • 1
  • 10
arserbin3
  • 6,010
  • 8
  • 36
  • 52
  • 3
    Is there a way to authenticate with the proxy with this solution? – Thomas Levesque Sep 22 '15 at 16:19
  • Getting many exceptions here, one after another about the proxy, first too much data, then incompatible protocols, then proxy rejected the connection (aka it did not return status code 2xx) ... Please update the answer? – Gizmo Feb 12 '16 at 17:31
  • i also encounter exception at _socket.Open() throwing exception about remoteEndPoint is null at public override void Connect(EndPoint remoteEndPoint) in HttpConnectProxy.cs. – superhuman1314 Aug 25 '16 at 08:17