예제 #1
0
        public ProxyInfo(string hostname, int port, string username, string password)
            : this(hostname, port)
        {
            if (string.IsNullOrEmpty(username))
            {
                throw new ArgumentNullException(nameof(username));
            }
            if (string.IsNullOrEmpty(password))
            {
                throw new ArgumentNullException(nameof(password));
            }

            Authenticate          = true;
            AuthenticationMessage = Socks5.BuildAuthenticationMessage(username, password);
        }
        private void HandleRequest(Socket clientSocket)
        {
            Socket socks5Socket = null;
            bool   success      = true;

            try
            {
                if (TryReadTarget(clientSocket, out string hostname, out int port, out string httpVersion, out bool connect, out string request, out byte[] overRead))
                {
                    try
                    {
                        socks5Socket = CreateSocket();
                        socks5Socket.Connect(ProxyList[0].Hostname.Resolve(), ProxyList[0].Port);
                    }
                    catch (SocketException ex)
                    {
                        SendError(clientSocket, ex.ToConnectionResult());
                        success = false;
                    }

                    if (success)
                    {
                        SocketConnectionResult result;
                        for (int i = 0; i < ProxyList.Length - 1; i++)
                        {
                            var proxy     = ProxyList[i];
                            var nextProxy = ProxyList[i + 1];
                            result = Socks5.TryCreateTunnel(socks5Socket, nextProxy.Hostname, nextProxy.Port, proxy, ResolveHostnamesLocally);
                            if (result != SocketConnectionResult.OK)
                            {
                                SendError(clientSocket, result, httpVersion);
                                success = false;
                                break;
                            }
                        }

                        if (success)
                        {
                            var lastProxy = ProxyList.Last();
                            result = Socks5.TryCreateTunnel(socks5Socket, hostname, port, lastProxy, ResolveHostnamesLocally);
                            if (result != SocketConnectionResult.OK)
                            {
                                SendError(clientSocket, result, httpVersion);
                                success = false;
                            }
                            else
                            {
                                if (!connect)
                                {
                                    SendString(socks5Socket, request);
                                    if (overRead != null)
                                    {
                                        socks5Socket.Send(overRead, SocketFlags.None);
                                    }
                                }
                                else
                                {
                                    SendString(clientSocket, httpVersion + "200 Connection established\r\nProxy-Agent: MihaZupan-HttpToSocks5Proxy\r\n\r\n");
                                }
                            }
                        }
                    }
                }
                else
                {
                    success = false;
                }
            }
예제 #3
0
        private void HandleRequest(Socket clientSocket)
        {
            Socket socks5Socket = null;
            bool   success      = true;

            try
            {
                if (TryRead(clientSocket, out string hostname, out int port, out string httpVersion, out bool connect, out string request, out byte[] overRead))
                {
                    Console.WriteLine("[HttpToSocks5Proxy---->]hostname: " + hostname);
                    Console.WriteLine("[HttpToSocks5Proxy---->]port: " + port);
                    Console.WriteLine("[HttpToSocks5Proxy---->]httpVersion: " + httpVersion);
                    Console.WriteLine("[HttpToSocks5Proxy---->]connect: " + connect);
                    Console.WriteLine("[HttpToSocks5Proxy---->]request: " + request);
                    Console.WriteLine("[HttpToSocks5Proxy---->]overRead: " + overRead);


                    try
                    {
                        Console.WriteLine("[HttpToSocks5Proxy---->]ProxyList[0].Hostname: " + ProxyList[0].Hostname);
                        Console.WriteLine("[HttpToSocks5Proxy---->]ProxyList[0].Port: " + ProxyList[0].Port);
                        socks5Socket = CreateSocket();
                        socks5Socket.Connect(dnsResolver.TryResolve(ProxyList[0].Hostname), ProxyList[0].Port);
                    }
                    catch (SocketException ex)
                    {
                        Console.WriteLine("[HttpToSocks5Proxy---->]SocketException: " + ex);
                        SendError(clientSocket, ex.ToConnectionResult());
                        success = false;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("[HttpToSocks5Proxy---->]Exception: " + ex);
                        SendError(clientSocket, Enums.SocketConnectionResult.UnknownError);
                        success = false;
                    }
                    Console.WriteLine("[HttpToSocks5Proxy---->]success: " + success);
                    if (success)
                    {
                        SocketConnectionResult result;
                        Console.WriteLine("[HttpToSocks5Proxy---->]ProxyList.Length: (如果>=2,跑)" + ProxyList.Length);
                        for (int i = 0; i < ProxyList.Length - 1; i++)//作者并没错,至少有两个才跑
                        {
                            var proxy     = ProxyList[i];
                            var nextProxy = ProxyList[i + 1];
                            Console.WriteLine("[HttpToSocks5Proxy---->]nextProxy.Hostname: " + nextProxy.Hostname);
                            Console.WriteLine("[HttpToSocks5Proxy---->]nextProxy.Port: " + nextProxy.Port);
                            result = Socks5.TryCreateTunnel(socks5Socket, nextProxy.Hostname, nextProxy.Port, proxy, ResolveHostnamesLocally ? dnsResolver : null);
                            if (result != SocketConnectionResult.OK)
                            {
                                SendError(clientSocket, result, httpVersion);
                                success = false;
                                break;
                            }
                        }

                        if (success)
                        {
                            var lastProxy = ProxyList.Last();
                            Console.WriteLine("[HttpToSocks5Proxy---->]lastProxy.Hostname: " + lastProxy.Hostname);
                            Console.WriteLine("[HttpToSocks5Proxy---->]lastProxy.Port: " + lastProxy.Port);
                            result = Socks5.TryCreateTunnel(socks5Socket, hostname, port, lastProxy, ResolveHostnamesLocally ? dnsResolver : null);

                            Console.WriteLine("[HttpToSocks5Proxy---->]!!!");
                            if (result != SocketConnectionResult.OK)
                            {
                                Console.WriteLine("[HttpToSocks5Proxy---->]result != SocketConnectionResult.OK");
                                SendError(clientSocket, result, httpVersion);
                                success = false;
                            }
                            else
                            {
                                if (!connect)
                                {
                                    Console.WriteLine("[HttpToSocks5Proxy---->]connect fail");
                                    SendString(socks5Socket, request);
                                    if (overRead != null)
                                    {
                                        socks5Socket.Send(overRead, SocketFlags.None);
                                    }
                                }
                                else
                                {
                                    Console.WriteLine("[HttpToSocks5Proxy---->]connect success");
                                    SendString(clientSocket, httpVersion + "200 Connection established\r\nProxy-Agent: MihaZupan-HttpToSocks5Proxy\r\n\r\n");
                                }
                            }
                        }
                    }
                }
                else
                {
                    success = false;
                }
            }