Exemplo n.º 1
0
        public void Dispose()
        {
            if (InternalSocketServer != null && !IsDisposed)
            {
                IsDisposed = true;

                InternalSocketServer.Disconnect(false);
                InternalSocketServer.Dispose();
            }
        }
Exemplo n.º 2
0
        private void CreateInternalServer()
        {
            InternalSocketServer = CreateSocketServer();

            InternalSocketServer.Bind(new IPEndPoint(IPAddress.Loopback, 0));
            InternalSocketPort = ((IPEndPoint)(InternalSocketServer.LocalEndPoint)).Port;

            HttpProxyURL = new Uri($"http://127.0.0.1:{InternalSocketPort}");

            InternalSocketServer.Listen(512);
            InternalSocketServer.BeginAccept(new AsyncCallback(AcceptCallback), null);
        }
Exemplo n.º 3
0
        private async void AcceptCallback(IAsyncResult e)
        {
            if (IsDisposed)
            {
                return;
            }

            Socket Socket = InternalSocketServer.EndAccept(e);

            InternalSocketServer.BeginAccept(new AsyncCallback(AcceptCallback), null);

            byte[] HeaderBuffer = new byte[8192]; // Default Header size

            Socket.Receive(HeaderBuffer, HeaderBuffer.Length, 0);

            string Header = Encoding.ASCII.GetString(HeaderBuffer);

            string HttpVersion = Header.Split(' ')[2].Split('\r')[0]?.Trim();
            string TargetURL   = Header.Split(' ')[1]?.Trim();

            if (string.IsNullOrEmpty(HttpVersion) || string.IsNullOrEmpty(TargetURL))
            {
                throw new Exception("Unsupported request.");
            }

            string UriHostname = string.Empty;
            int    UriPort     = 0;

            if (TargetURL.Contains(":") && !TargetURL.Contains("http://"))
            {
                UriHostname = TargetURL.Split(':')[0];
                UriPort     = int.Parse(TargetURL.Split(':')[1]);
            }
            else
            {
                Uri URL = new Uri(TargetURL);

                UriHostname = URL.Host;
                UriPort     = URL.Port;
            }

            Socket TargetSocket = CreateSocketServer();

            SocketError Connection = await TrySocksConnection(UriHostname, UriPort, TargetSocket);

            if (Connection != SocketError.Success)
            {
                if (Connection == SocketError.HostUnreachable || Connection == SocketError.ConnectionRefused || Connection == SocketError.ConnectionReset)
                {
                    Send(Socket, $"{HttpVersion} 502 Bad Gateway\r\n\r\n");
                }
                else if (Connection == SocketError.AccessDenied)
                {
                    Send(Socket, $"{HttpVersion} 401 Unauthorized\r\n\r\n");
                }
                else
                {
                    Send(Socket, $"{HttpVersion} 500 Internal Server Error\r\nX-Proxy-Error-Type: {Connection}\r\n\r\n");
                }

                Dispose(Socket);
                Dispose(TargetSocket);
            }
            else
            {
                Send(Socket, $"{HttpVersion} 200 Connection established\r\n\r\n");

                Relay(Socket, TargetSocket, false);
            }
        }