/////////////////////////////////////////////////////////////////////////////////////////////////////////////////// internal Inbound(WwwProxy wwwProxy, Socket socket) { inboundIpEndPoint_ = (IPEndPoint)socket.RemoteEndPoint; wwwProxy_ = wwwProxy; inboundSocket_ = socket; currentOutboundsMutex_ = new Mutex(); currentOutbounds_ = new Dictionary <Outbound, ProxyRequest>(); inboundThreadExitEvent_ = new ManualResetEvent(false); inboundSocketReceiveEvent_ = new ManualResetEvent(false); sslNetworkStreamReceiveEvent_ = new ManualResetEvent(false); inboundThread_ = new Thread(RunSocket); inboundThread_.Start(); }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////// internal Outbound(WwwProxy wwwProxy, Inbound inbound, bool ssl, string host, ushort port) { wwwProxy_ = wwwProxy; inbound_ = inbound; ssl_ = ssl; host_ = host.ToLower(); port_ = port; if (wwwProxy.remoteProxy_ != null) { if (wwwProxy_.remoteProxyExceptions_ != null) { bool exceptionMatch = false; bool hostIsIp = Regex.Match(host_, @"^[\*\d]{1,3}\.[\*\d]{1,3}\.[\*\d]{1,3}\.[\*\d]{1,3}$").Success; string[] hostElements = host_.Split('.'); foreach (string e in wwwProxy_.remoteProxyExceptions_) { bool exceptionIsIp = Regex.Match(e, @"^[\*\d]{1,3}\.[\*\d]{1,3}\.[\*\d]{1,3}\.[\*\d]{1,3}$").Success; string[] exceptionElements = e.Split('.'); if (hostIsIp == exceptionIsIp) { for (int i = 0; i < Math.Min(hostElements.Length, exceptionElements.Length); ++i) { string hostElement = hostElements[hostIsIp ? i : hostElements.Length - i - 1]; string exceptionElement = exceptionElements[exceptionIsIp ? i : exceptionElements.Length - i - 1]; if ((hostElement != exceptionElement) && (exceptionElement != "*")) { exceptionMatch = false; break; } else { exceptionMatch = true; } } } if (exceptionMatch) { break; } } if (!exceptionMatch) { outboundIpEndPoint_ = wwwProxy.remoteProxy_; useRemoteProxy_ = true; } } else { outboundIpEndPoint_ = wwwProxy.remoteProxy_; useRemoteProxy_ = true; } } if (outboundIpEndPoint_ == null) { if (Regex.Match(host, @"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$").Success) { outboundIpEndPoint_ = new IPEndPoint(IPAddress.Parse(host_), port); } else { outboundIpEndPoint_ = new IPEndPoint(IPAddress.Parse(System.Net.Dns.GetHostEntry(host_).AddressList[0].ToString()), port_); } } outboundThreadExitEvent_ = new ManualResetEvent(false); outboundSocketReceiveEvent_ = new ManualResetEvent(false); sslNetworkStreamReceiveEvent_ = new ManualResetEvent(false); }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////// static void Main(string[] args) { Console.WriteLine(); Console.WriteLine("WwwPyFilter {0}", Assembly.GetExecutingAssembly().GetName().Version.ToString()); Console.WriteLine("Copyright ©2008 Liam Kirton <*****@*****.**>"); Console.WriteLine(); pythonMutex_ = new Mutex(); StartPython(); try { pythonEngine_.ExecuteFile("WwwPyFilter.py", wwwPyFilterMod_); } catch (Exception e) { Console.WriteLine("Error: {0}", e.Message); Console.WriteLine(); Console.WriteLine(e.StackTrace); pythonEngine_.Shutdown(); return; } UserType wwwPyFilterType = wwwPyFilterMod_.Globals["WwwPyFilter"] as UserType; wwwPyFilterClassObject_ = Ops.Call(wwwPyFilterType); wwwProxy_ = WwwProxy.WwwProxy.GetInstance(); wwwProxy_.Connect += new WwwProxy.WwwProxyConnectEvent(WwwProxy_Connect); wwwProxy_.Disconnect += new WwwProxy.WwwProxyConnectEvent(WwwProxy_Disconnect); wwwProxy_.Error += new WwwProxy.WwwProxyErrorEvent(WwwProxy_Error); wwwProxy_.Request += new WwwProxy.WwwProxyRequestEvent(WwwProxy_Request); wwwProxy_.Response += new WwwProxy.WwwProxyResponseEvent(WwwProxy_Response); ushort localPort = 8080; string[] remoteProxy = null; for (int i = 0; i < args.Length; ++i) { switch (i) { case 0: localPort = Convert.ToUInt16(args[i]); break; case 1: remoteProxy = args[i].Split(':'); break; } } wwwProxy_.Start(localPort, (remoteProxy != null) ? new IPEndPoint(IPAddress.Parse(remoteProxy[0]), Convert.ToUInt16(remoteProxy[1])) : null); Console.WriteLine("Running. Press Ctrl+C to Quit."); Console.WriteLine(); exitEvent_ = new ManualResetEvent(false); Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress); exitEvent_.WaitOne(); Console.WriteLine("Closing."); wwwProxy_.Stop(); StopPython(); }