示例#1
0
 static void Main(string[] args)
 {
     // create a new ProxySocket
     ProxySocket s = new ProxySocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     // set the proxy settings
     s.ProxyEndPoint = new IPEndPoint(IPAddress.Parse("10.0.0.5"), 1080);
     s.ProxyUser = "******";
     s.ProxyPass = "******";
     s.ProxyType = ProxyTypes.Socks5;	// if you set this to ProxyTypes.None,
                                         // the ProxySocket will act as a normal Socket
     // connect to the remote server
     // (note that the proxy server will resolve the domain name for us)
     s.Connect("www.mentalis.org", 80);
     // send an HTTP request
     s.Send(Encoding.ASCII.GetBytes("GET / HTTP/1.0\r\nHost: www.mentalis.org\r\n\r\n"));
     // read the HTTP reply
     int recv = 0;
     byte [] buffer = new byte[1024];
     recv = s.Receive(buffer);
     while (recv > 0) {
         Console.Write(Encoding.ASCII.GetString(buffer, 0, recv));
         recv = s.Receive(buffer);
     }
     // wait until the user presses enter
     Console.WriteLine("Press enter to continue...");
     Console.ReadLine();
 }
        public void Process()
        {
            var localEndPoint = (IPEndPoint)client.LocalEndPoint;
            var remoteEndPoint = (IPEndPoint)client.RemoteEndPoint;
            var connection = connectionTracker[new Connection(ProtocolType.Tcp, localEndPoint, remoteEndPoint)].Mirror;

            if (connection != null)
            {
                var initialEndPoint = connection.Source;
                var requestedEndPoint = connection.Destination;
                var tcpConnection = connectionTracker.GetTCPConnection(initialEndPoint, requestedEndPoint);

                var logMessage = string.Format("{0}[{1}] {2} {{0}} connection to {3}",
                    tcpConnection != null ? tcpConnection.ProcessName : "unknown",
                    tcpConnection != null ? tcpConnection.PID : 0,
                    initialEndPoint, requestedEndPoint);
                try
                {
                    var proxy = new ProxySocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    configureProxySocket(proxy, requestedEndPoint);

                    debug.Log(1, logMessage + " via {1}", "requested", proxy.ProxyEndPoint);

                    proxy.Connect(requestedEndPoint);

                    SocketPump.Pump(client, proxy);

                    proxy.Close();
                    debug.Log(1, logMessage, "closing");
                }
                catch (Exception ex)
                {
                    debug.Log(1, logMessage + ": {1}", "failed", ex.Message);
                }

                connectionTracker.QueueForCleanUp(connection);
            }
            else
            {
                var tcpConnection = connectionTracker.GetTCPConnection(remoteEndPoint, localEndPoint);
                debug.Log(1, "{0}[{1}] {2} has no mapping",
                    tcpConnection != null ? tcpConnection.ProcessName : "unknown",
                    tcpConnection != null ? tcpConnection.PID : 0,
                    remoteEndPoint);
                client.Send(Encoding.ASCII.GetBytes("No mapping\r\n"));
            }

            client.Close();
        }
        private SocksHttpWebResponse InternalGetResponse()
        {
            MemoryStream data = null;
            string header = string.Empty;

            using (var _socksConnection = new ProxySocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
            {
                var proxyUri = Proxy.GetProxy(RequestUri);
                var ipAddress = GetProxyIpAddress(proxyUri);
                _socksConnection.ProxyEndPoint = new IPEndPoint(ipAddress, proxyUri.Port);
                _socksConnection.ProxyType = ProxyTypes.Socks5;

                // open connection
                _socksConnection.Connect(RequestUri.Host, 80);

                // send an HTTP request
                _socksConnection.Send(Encoding.UTF8.GetBytes(RequestMessage));

                // read the HTTP reply
                var buffer = new byte[1024 * 4];
                int bytesReceived = 0;
                bool headerDone = false;

                while ((bytesReceived = _socksConnection.Receive(buffer)) > 0)
                {
                    if (!headerDone)
                    {
                        var headPart = Encoding.UTF8.GetString(buffer, 0, bytesReceived > 1024 ? 1024 : bytesReceived);
                        var indexOfFirstBlankLine = headPart.IndexOf("\r\n\r\n");
                        if (indexOfFirstBlankLine > 0)
                        {
                            headPart = headPart.Substring(0, indexOfFirstBlankLine);
                            header += headPart;
                            headerDone = true;

                            var headerPartLength = Encoding.UTF8.GetByteCount(headPart) + 4;

                            // 0123456789
                            //   ----
                            if (headerPartLength < bytesReceived)
                            {
                                data = new MemoryStream();
                                data.Write(buffer, headerPartLength, bytesReceived - headerPartLength);
                            }
                        }
                        else
                        {
                            header += headPart;
                        }
                    }
                    else
                    {
                        if (data == null)
                        {
                            data = new MemoryStream();
                        }
                        data.Write(buffer, 0, bytesReceived);
                    }
                }

                if (data != null)
                {
                    data.Position = 0;
                }
            }

            return new SocksHttpWebResponse(data, header);
        }
    private SocksHttpWebResponse InternalGetResponse()
    {
        var response = new StringBuilder();
            using (var _socksConnection =
                new ProxySocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
            {
                var proxyUri = Proxy.GetProxy(RequestUri);
                var ipAddress = GetProxyIpAddress(proxyUri);
                _socksConnection.ProxyEndPoint = new IPEndPoint(ipAddress, proxyUri.Port);
                _socksConnection.ProxyType = ProxyTypes.Socks5;

                // open connection
                _socksConnection.Connect(RequestUri.Host, 80);
                // send an HTTP request
                _socksConnection.Send(_correctEncoding.GetBytes(RequestMessage));
                // read the HTTP reply
                var buffer = new byte[1024];

                var bytesReceived = _socksConnection.Receive(buffer);
                while (bytesReceived > 0)
                {
                    string chunk = _correctEncoding.GetString(buffer, 0, bytesReceived);
                    string encString = EncodingHelper.GetEncodingFromChunk(chunk);
                    if (!string.IsNullOrEmpty(encString))
                    {
                        try
                        {
                            _correctEncoding = Encoding.GetEncoding(encString);
                        }
                        catch
                        {
                            //TODO: do something here
                        }
                    }
                    response.Append(chunk);
                    bytesReceived = _socksConnection.Receive(buffer);
                }
            }
            return new SocksHttpWebResponse(response.ToString(),_correctEncoding);
    }
示例#5
0
    static void Main(string[] args)
    {
        {
            // create a new ProxySocket
            ProxySocket s = new ProxySocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            // set the proxy settings
            //s.ProxyEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9150);
            //s.ProxyUser = "******";
            //s.ProxyPass = "******";
            s.ProxyType = ProxyTypes.None;    // if you set this to ProxyTypes.None, 
                                                // the ProxySocket will act as a normal Socket

            // http://www.whatsmyip.org/

            //<!-- Please DO NOT use our site to power an IP bot, script or other automated IP-lookup software! - for humans only! -->
            //<h1>Your IP Address is <span id="ip">169.120.138.139</span></h1>

            // connect to the remote server
            // (note that the proxy server will resolve the domain name for us)
            s.Connect("torguard.net", 80);
            // send an HTTP request
            s.Send(Encoding.ASCII.GetBytes("GET /whats-my-ip.php HTTP/1.0\r\nHost: torguard.net\r\n\r\n"));
            // read the HTTP reply
            int recv = 0;
            byte[] buffer = new byte[8096];
            recv = s.Receive(buffer);
            while (recv > 0)
            {
                Console.Write(Encoding.ASCII.GetString(buffer, 0, recv));
                recv = s.Receive(buffer);
            }
        }

        {
            // create a new ProxySocket
            ProxySocket s = new ProxySocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            // set the proxy settings
            s.ProxyEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9150);
            //s.ProxyUser = "******";
            //s.ProxyPass = "******";
            s.ProxyType = ProxyTypes.Socks5;    // if you set this to ProxyTypes.None, 
                                                // the ProxySocket will act as a normal Socket

            // http://www.whatsmyip.org/

            //<!-- Please DO NOT use our site to power an IP bot, script or other automated IP-lookup software! - for humans only! -->
            //<h1>Your IP Address is <span id="ip">169.120.138.139</span></h1>

            // connect to the remote server
            // (note that the proxy server will resolve the domain name for us)
            s.Connect("torguard.net", 80);
            // send an HTTP request
            s.Send(Encoding.ASCII.GetBytes("GET /whats-my-ip.php HTTP/1.0\r\nHost: torguard.net\r\n\r\n"));
            // read the HTTP reply
            int recv = 0;
            byte[] buffer = new byte[1024];
            recv = s.Receive(buffer);
            while (recv > 0)
            {
                Console.Write(Encoding.ASCII.GetString(buffer, 0, recv));
                recv = s.Receive(buffer);
            }
        }

        // wait until the user presses enter
        Console.WriteLine("Press enter to continue...");
        Console.ReadLine();
    }
示例#6
0
 /// <summary>
 /// 连接到服务器
 /// </summary>
 public bool Connect()
 {
     if (socket != null && socket.Connected)
     {
         return true;
     }
     int retry = 0;
     Connect:
     try
     {
         socket = GetSocket();
         socket.Connect(epServer);
         BeginDataReceive(this.socket);
         return true;
     }
     catch (Exception e)
     {
         retry++;
         if (retry < 3)
         {
             goto Connect;
         }
         policy.OnConnectServerError(e);
         return false;
     }
 }