internal WebSocketResponse(Reactor.Tcp.Socket socket, Reactor.Buffer buffer) { this.Socket = socket; this.Frames = new List <Frame>(); this.Headers = new Dictionary <string, string>(); this.ParseResponseHeader(buffer); this.ParseResponseBody(buffer); }
private void ServerAction(string strSocksIP, int socksPort, int localPort) { Reactor.Loop.Start(System.Threading.SynchronizationContext.Current); notifyln("Ready for local connection."); notifyln(String.Format("Please point your application's proxy settings to 127.0.0.1:{0}", localPort)); Reactor.Tcp.Server.Create(LocalListener => { Reactor.Tcp.Socket ProxyClient = Reactor.Tcp.Socket.Create(strSocksIP, socksPort); notifyln("Attempting to connect to proxy server..."); LocalListener.OnData += (data) => { notifyln(data.ToString(Encoding.ASCII)); }; LocalListener.OnConnect += () => { notifyln(String.Format("Received connection on 127.0.0.1:{0}", localPort)); }; LocalListener.OnError += (error) => { ProxyClient.End(); notifyln("Connection on local listener dropped:"); notifyln(error.Message); }; LocalListener.OnEnd += () => { ServerAction(strSocksIP, socksPort, localPort); LocalListener.End(); ProxyClient.End(); }; ProxyClient.OnEnd += () => { LocalListener.End(); notifyln("Connection to proxy server dropped (Proxy)."); LocalListener.End(); ProxyClient.End(); }; // route local events to the socket. ProxyClient.OnData += (data) => { notifyln(data.ToString(Encoding.ASCII)); LocalListener.Write(data); }; ProxyClient.OnError += (error) => { notifyln(String.Format("Connection to proxy server dropped:\n\r{0}\n\r", error.Message)); ProxyClient.End(); LocalListener.End(); }; ProxyClient.OnConnect += () => { notifyln(String.Format("Connection established with proxy server {0}:{1}.", strSocksIP, socksPort)); LocalListener.OnData += (data) => { ProxyClient.Write(data); }; }; }).Listen(localPort); }
public void GetResponse(Action<Exception, WebSocketResponse> callback) { //------------------------------------------ // create method //------------------------------------------ string http = string.Format("{0} {1} HTTP/1.1", this.Method.ToUpper(), this.Uri.PathAndQuery); //------------------------------------------ // create header //------------------------------------------ this.Headers["Cache-Control"] = "no-cache"; this.Headers["Connection"] = "upgrade"; this.Headers["Host"] = this.Uri.Authority; this.Headers["Pragma"] = "no-cache"; this.Headers["Sec-WebSocket-Extensions"] = "permessage-deflate"; this.Headers["Sec-WebSocket-Key"] = this.CreateSecWebSocketKey(); this.Headers["Sec-WebSocket-Version"] = "13"; this.Headers["Upgrade"] = "websocket"; this.Headers["User-Agent"] = "Reactor.Web.Sockets.WebSocket"; //------------------------------------------ // build request //------------------------------------------ var buffer = Reactor.Buffer.Create(); buffer.Write(http + "\r\n"); foreach (var pair in this.Headers) { buffer.Write("{0}: {1}\r\n", pair.Key, pair.Value); } buffer.Write("\r\n"); //------------------------------------------ // resolve port //------------------------------------------ var port = this.Uri.Port; if (port == -1) { switch(this.Uri.Scheme) { case "ws" : port = 80; break; case "wss": port = 443; break; default: break; } } //------------------------------------------ // send request //------------------------------------------ this.socket = Reactor.Tcp.Socket.Create(this.Uri.DnsSafeHost, port); this.socket.OnConnect += () => { this.socket.Write(buffer, (exception) => { if (exception != null) { callback(exception, null); return; } Reactor.Action<Buffer> ondata = null; ondata = (data) => { this.socket.OnData -= ondata; var response = new Reactor.Web.Socket.WebSocketResponse(this.socket, data); callback(null, response); }; this.socket.OnData += ondata; }); }; this.socket.OnError += (exception) => { callback(exception, null); }; }
public void GetResponse(Action <Exception, WebSocketResponse> callback) { //------------------------------------------ // create method //------------------------------------------ string http = string.Format("{0} {1} HTTP/1.1", this.Method.ToUpper(), this.Uri.PathAndQuery); //------------------------------------------ // create header //------------------------------------------ this.Headers["Cache-Control"] = "no-cache"; this.Headers["Connection"] = "upgrade"; this.Headers["Host"] = this.Uri.Authority; this.Headers["Pragma"] = "no-cache"; this.Headers["Sec-WebSocket-Extensions"] = "permessage-deflate"; this.Headers["Sec-WebSocket-Key"] = this.CreateSecWebSocketKey(); this.Headers["Sec-WebSocket-Version"] = "13"; this.Headers["Upgrade"] = "websocket"; this.Headers["User-Agent"] = "Reactor.Web.Sockets.WebSocket"; //------------------------------------------ // build request //------------------------------------------ var buffer = Reactor.Buffer.Create(); buffer.Write(http + "\r\n"); foreach (var pair in this.Headers) { buffer.Write("{0}: {1}\r\n", pair.Key, pair.Value); } buffer.Write("\r\n"); //------------------------------------------ // resolve port //------------------------------------------ var port = this.Uri.Port; if (port == -1) { switch (this.Uri.Scheme) { case "ws": port = 80; break; case "wss": port = 443; break; default: break; } } //------------------------------------------ // send request //------------------------------------------ this.socket = Reactor.Tcp.Socket.Create(this.Uri.DnsSafeHost, port); this.socket.OnConnect += () => { this.socket.Write(buffer, (exception) => { if (exception != null) { callback(exception, null); return; } Reactor.Action <Buffer> ondata = null; ondata = (data) => { this.socket.OnData -= ondata; var response = new Reactor.Web.Socket.WebSocketResponse(this.socket, data); callback(null, response); }; this.socket.OnData += ondata; }); }; this.socket.OnError += (exception) => { callback(exception, null); }; }