private void ButtonStartProxy_Click(object sender, RoutedEventArgs e) { this.ButtonStartProxy.Visibility = Visibility.Hidden; IPAddress host = Dns.GetHostAddresses(Dns.GetHostName()).Where(ip => ip.AddressFamily == AddressFamily.InterNetwork && !ip.ToString().StartsWith("172")).FirstOrDefault(); SocketProxy proxy = new SocketProxy(host); proxy.SocketLog += Proxy_OnLog; try { int port = int.Parse(this.TextBoxPort.Text); proxy.InitializeServer(port); proxy.StartListening(); } catch (Exception ex) { proxy.Close(); //MessageBox.Show("Server window start listening error: " + ex.Message); Proxy_OnLog(this, new SocketLogEventArgs("Proxy window start listening error: " + ex.Message, LogLevel.Error)); } }
public override bool Handle(byte[] firstPacket, int length, SocketProxy socket, object state) { if (socket.ProtocolType != ProtocolType.Tcp) { return(false); } try { string request = Encoding.UTF8.GetString(firstPacket, 0, length); string[] lines = request.Split('\r', '\n'); bool hostMatch = false, pathMatch = false, useSocks = false; bool secretMatch = PacSecret.IsNullOrEmpty(); foreach (string line in lines) { string[] kv = line.Split(new char[] { ':' }, 2); if (kv.Length == 2) { if (kv[0] == "Host") { if (kv[1].Trim() == ((IPEndPoint)socket.LocalEndPoint).ToString()) { hostMatch = true; } } //else if (kv[0] == "User-Agent") //{ // // we need to drop connections when changing servers // if (kv[1].IndexOf("Chrome") >= 0) // { // useSocks = true; // } //} } else if (kv.Length == 1) { if (line.IndexOf("pac", StringComparison.Ordinal) >= 0) { pathMatch = true; } if (!secretMatch) { if (line.IndexOf(PacSecret, StringComparison.Ordinal) >= 0) { secretMatch = true; } } } } if (hostMatch && pathMatch) { if (!secretMatch) { socket.Close(); // Close immediately } else { SendResponse(firstPacket, length, socket, useSocks); } return(true); } return(false); } catch (ArgumentException) { return(false); } }
/// <summary> /// Destroy the current socket. /// </summary> /// <remarks> /// Any outstanding messages physically received from the network but not yet received by the application /// with Receive shall be discarded. The behaviour for discarding messages sent by the application /// with Send but not yet physically transferred to the network depends on the value of /// the <see cref="Linger"/> socket option. /// </remarks> /// <exception cref="ZmqSocketException">The underlying socket object is not valid.</exception> public void Close() { HandleProxyResult(_socketProxy.Close()); }
private void ProcessClientRequest(object state) { var client = (Socket)state; var message = new AsynchReceiver().Receive(client, false, ""); Action<HttpMessage, ISocketProxy> action = ReturnNamedResourceAsWebPage; if (Steps.Count == 0 || Steps.Count < _stepIndex) { Console.WriteLine("no defined action for step " + (_stepIndex++) + " - trying ReturnNamedResourceAsWebPage"); } else { action = Steps[_stepIndex++]; } var proxy = new SocketProxy(client); action(message, proxy); proxy.Close(); }
private void DisposeSocket() { _connection.Shutdown(SocketShutdown.Both); _connection.Close(); }