Example #1
0
        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);
        }