/// <summary> /// Initializes a new instance of the <see cref="ActiveConnections"/> class. /// </summary> public ActiveConnections() { this.InitializeComponent(); this.defaultProcessIcon = new BitmapImage( new Uri( "pack://application:,,,/PeaRoxy.Windows.WPFClient;component/Images/SettingsPanel/Process_def.png")); this.connectionContextMenu = new ContextMenu(); MenuItem menuItem = new MenuItem { Header = "Close this connection" }; menuItem.Click += (RoutedEventHandler) delegate { TreeViewItem placementTarget = this.connectionContextMenu.PlacementTarget as TreeViewItem; if (placementTarget == null) { return; } try { ProxyClient client = placementTarget.Tag as ProxyClient; if (client != null) { client.Close("Closed By User"); } } catch { } }; this.connectionContextMenu.Items.Add(menuItem); menuItem = new MenuItem { Header = "Copy" }; menuItem.Click += (RoutedEventHandler) delegate { TreeViewItem placementTarget = this.connectionContextMenu.PlacementTarget as TreeViewItem; if (placementTarget == null) { return; } try { ProxyClient client = placementTarget.Tag as ProxyClient; if (client != null) { Clipboard.SetText(client.RequestAddress); } } catch { } }; this.connectionContextMenu.Items.Add(menuItem); }
public static void Handle(byte[] firstResponse, ProxyClient client) { if (!client.Controller.IsHttpsSupported || !IsHttps(firstResponse)) { client.Close(); return; } string textData = Encoding.ASCII.GetString(firstResponse); // Getting Information From HTTPS Header string[] parts = textData.Split('\n')[0].Split(' '); Uri url = new Uri("https://" + parts[1].Trim()); string clientConnectionAddress = url.Host; ushort clientConnectionPort = (ushort)url.Port; client.RequestAddress = "https://" + clientConnectionAddress + ":" + clientConnectionPort; if (textData.ToUpper().IndexOf("FASTCONNECT ", StringComparison.OrdinalIgnoreCase) != 0) { byte[] serverResponse = Encoding.ASCII.GetBytes("HTTP/1.1 200 OK\r\n\r\n"); client.Write(serverResponse); firstResponse = new byte[0]; } else { int endLine = textData.IndexOf("\r\n\r\n", StringComparison.Ordinal) + 4; Array.Copy(firstResponse, endLine, firstResponse, 0, firstResponse.Length - endLine); Array.Resize(ref firstResponse, firstResponse.Length - endLine); } client.IsReceivingStarted = false; if (client.Controller.Status.HasFlag(ProxyController.ControllerStatus.Proxy)) { DirectHandle(client, clientConnectionAddress, clientConnectionPort, firstResponse); } else { client.Close( "Currently we only serve AutoConfig files. Try restarting your browser.", null, ErrorRenderer.HttpHeaderCode.C417ExpectationFailed); } }
public static void Handle(byte[] firstResponse, ProxyClient client) { if (!client.Controller.IsSocksSupported || !IsSocks(firstResponse)) { client.Close(); return; } // Responding with socks protocol byte clientVersion = firstResponse[0]; if (clientVersion == 5) { byte[] clientSupportedAuths = new byte[firstResponse[1]]; Array.Copy(firstResponse, 2, clientSupportedAuths, 0, firstResponse[1]); byte[] serverResponse = new byte[2]; if (clientSupportedAuths.Length == 0) { client.Close( "No authentication method found.", null, ErrorRenderer.HttpHeaderCode.C417ExpectationFailed); return; } serverResponse[0] = clientVersion; // -------------Selecting authentication type byte serverSelectedAuth = 255; foreach (byte item in clientSupportedAuths) { if (item == 0) { serverSelectedAuth = 0; } } serverResponse[1] = serverSelectedAuth; client.Write(serverResponse); client.IsReceivingStarted = false; // -------------Doing authentication if (serverSelectedAuth == 255) { client.Close( "SOCKS Connection only accept clients with no authentication information.", null, ErrorRenderer.HttpHeaderCode.C417ExpectationFailed); return; } // -------------Establishing connection byte[] response = new byte[client.UnderlyingSocket.ReceiveBufferSize]; client.UnderlyingSocket.ReceiveTimeout = client.NoDataTimeOut; client.UnderlyingSocket.BeginReceive( response, 0, response.Length, SocketFlags.None, delegate(IAsyncResult ar) { try { int bytes = client.UnderlyingSocket.EndReceive(ar); Array.Resize(ref response, bytes); if (response == null || response.Length == 0) { client.Close( "No request received. Connection Timeout.", null, ErrorRenderer.HttpHeaderCode.C417ExpectationFailed); return; } if (response[0] != clientVersion) { client.Close( "Unknown SOCKS version, Expected " + clientVersion, null, ErrorRenderer.HttpHeaderCode.C417ExpectationFailed); return; } byte clientConnectionType = response[1]; byte clientAddressType = response[3]; string clientConnectionAddress = null; byte[] clientUnformatedConnectionAddress; ushort clientConnectionPort = 0; byte serverAresponse = 0; if (clientConnectionType != 1) { serverAresponse = 7; } switch (clientAddressType) { case 1: clientUnformatedConnectionAddress = new byte[4]; Array.Copy(response, 4, clientUnformatedConnectionAddress, 0, 4); clientConnectionAddress = new IPAddress(clientUnformatedConnectionAddress).ToString(); clientConnectionPort = (ushort)((ushort)(response[8] * 256) + response[9]); break; case 3: clientUnformatedConnectionAddress = new byte[response[4]]; Array.Copy(response, 5, clientUnformatedConnectionAddress, 0, response[4]); clientConnectionAddress = Encoding.ASCII.GetString(clientUnformatedConnectionAddress); clientConnectionPort = (ushort) ((ushort)(response[5 + response[4]] * 256) + response[5 + response[4] + 1]); break; case 4: clientUnformatedConnectionAddress = new byte[16]; Array.Copy(response, 4, clientUnformatedConnectionAddress, 0, 16); clientConnectionAddress = new IPAddress(clientUnformatedConnectionAddress).ToString(); clientConnectionPort = (ushort)((ushort)(response[20] * 256) + response[21]); break; default: serverAresponse = 8; break; } serverResponse = new byte[3 + (response.Length - 3)]; serverResponse[0] = clientVersion; serverResponse[1] = serverAresponse; serverResponse[2] = 0; serverResponse[3] = 3; Array.Copy(response, 3, serverResponse, 3, response.Length - 3); client.RequestAddress = "socks://" + clientConnectionAddress + ":" + clientConnectionPort; client.Write(serverResponse); client.IsReceivingStarted = false; if (serverAresponse != 0) { client.Close( "Response Error, Code: " + serverAresponse, null, ErrorRenderer.HttpHeaderCode.C417ExpectationFailed, true); return; } firstResponse = new byte[0]; DirectHandle(client, clientConnectionAddress, clientConnectionPort, firstResponse); } catch (Exception e) { client.Close(e.Message, e.StackTrace); } }, response); } else if (clientVersion == 4) { try { byte clientConnectionType = firstResponse[1]; byte serverAresponse = 90; if (clientConnectionType != 1) { serverAresponse = 91; } ushort clientConnectionPort = (ushort)((ushort)(firstResponse[2] * 256) + firstResponse[3]); byte[] clientUnformatedConnectionAddress = new byte[4]; Array.Copy(firstResponse, 4, clientUnformatedConnectionAddress, 0, 4); string clientConnectionAddress = new IPAddress(clientUnformatedConnectionAddress).ToString(); if (clientConnectionAddress.StartsWith("0.0.0.") && !clientConnectionAddress.EndsWith(".0")) { int domainStart = 0, domainEnd = 0; for (int i = 8; i < firstResponse.Length; i++) { if (firstResponse[i] == 0) { if (domainStart == 0) { domainStart = i + 1; } else if (domainEnd == 0) { domainEnd = i; } } } if (domainEnd != 0 && domainStart != 0) { clientConnectionAddress = Encoding.ASCII.GetString( firstResponse, domainStart, domainEnd - domainStart); } else { serverAresponse = 91; } } byte[] serverResponse = new byte[8]; serverResponse[0] = 0; serverResponse[1] = serverAresponse; Array.Copy(firstResponse, 2, serverResponse, 2, 2); // PORT Array.Copy(firstResponse, 4, serverResponse, 4, 4); // IP client.RequestAddress = "socks://" + clientConnectionAddress + ":" + clientConnectionPort; client.Write(serverResponse); client.IsReceivingStarted = false; if (serverAresponse != 90) { client.Close( "Response Error, Code: " + serverAresponse, null, ErrorRenderer.HttpHeaderCode.C417ExpectationFailed, true); return; } firstResponse = new byte[0]; DirectHandle(client, clientConnectionAddress, clientConnectionPort, firstResponse); } catch (Exception e) { client.Close(e.Message, e.StackTrace); } } }
public static void Handle(byte[] firstResponse, ProxyClient client, bool ignoreEnd = false) { if (!client.Controller.IsHttpSupported || client.Controller.Status == ProxyController.ControllerStatus.None || !IsHttp(firstResponse, ignoreEnd)) { client.Close(); return; } string textData = Encoding.ASCII.GetString(firstResponse); int headerLocation = textData.IndexOf("\r\n\r\n", StringComparison.OrdinalIgnoreCase); if (headerLocation == -1) { headerLocation = textData.Length; } else { headerLocation += 4; } textData = textData.Substring(0, headerLocation); string[] headerlines = textData.Split('\n'); string[] parts = headerlines[0].Split(' '); bool usedAsProxy = false; string host = string.Empty; foreach (string line in headerlines) { if (line.Trim() == string.Empty) { break; } string[] ls = line.Split(':'); if (ls[0].Trim().ToUpper().IndexOf("PROXY-", StringComparison.OrdinalIgnoreCase) != -1) { usedAsProxy = true; int startRemove = textData.IndexOf(line, StringComparison.OrdinalIgnoreCase); int countRemove = line.Trim().Length + 2; textData = textData.Remove(startRemove, countRemove); } else if (ls[0].Trim().ToUpper().IndexOf("CONNECTION", StringComparison.OrdinalIgnoreCase) != -1) { int startRemove = textData.IndexOf(line, StringComparison.OrdinalIgnoreCase); int countRemove = line.Trim().Length; textData = textData.Remove(startRemove, countRemove); textData = textData.Insert(startRemove, "Connection: close"); } else if (ls[0].Trim().ToUpper().IndexOf("HOST", StringComparison.OrdinalIgnoreCase) != -1) { host = ls[1].Trim(); } } if (!usedAsProxy && client.Controller.Status.HasFlag(ProxyController.ControllerStatus.AutoConfig) && !string.IsNullOrEmpty(client.Controller.AutoConfigPath) && parts[1].ToLower().Split('?')[0] == "/" + client.Controller.AutoConfigPath.ToLower()) { client.Write(GenerateAutoConfigScript(client)); client.Close(); } else if (client.Controller.Status.HasFlag(ProxyController.ControllerStatus.Proxy)) { if (!Uri.IsWellFormedUriString(parts[1], UriKind.Absolute) && host != string.Empty && Uri.IsWellFormedUriString("http://" + host + parts[1], UriKind.Absolute)) { parts[1] = "http://" + host + parts[1]; } if (!Uri.IsWellFormedUriString(parts[1], UriKind.Absolute) && Uri.IsWellFormedUriString("http://" + parts[1], UriKind.Absolute)) { parts[1] = "http://" + parts[1]; } Uri url = new Uri(parts[1]); textData = textData.Replace(headerlines[0], headerlines[0].Replace(parts[1], url.PathAndQuery)); string clientConnectionAddress = url.Host; ushort clientConnectionPort = (ushort)url.Port; byte[] headerData = Encoding.ASCII.GetBytes(textData); client.RequestAddress = parts[1]; if (headerData.Length > headerLocation) { Array.Resize(ref firstResponse, firstResponse.Length + (headerData.Length - headerLocation)); Array.Copy( firstResponse, headerLocation, firstResponse, headerData.Length, firstResponse.Length - headerData.Length); } else if (headerData.Length < headerLocation) { Array.Copy( firstResponse, headerLocation, firstResponse, headerData.Length, firstResponse.Length - headerLocation); Array.Resize(ref firstResponse, firstResponse.Length - (headerLocation - headerData.Length)); } Array.Copy(headerData, 0, firstResponse, 0, headerData.Length); DirectHandle(client, clientConnectionAddress, clientConnectionPort, firstResponse); } else { client.RequestAddress = parts[1]; client.Close( "Currently we only serve AutoConfig files. Try restarting your browser.", null, ErrorRenderer.HttpHeaderCode.C404NotFound); } }