Exemplo n.º 1
0
        void CopyResponse()
        {
            var response = ReadResponse();

            response.SetHeader("Connection", "close");
            response.SetHeader("Proxy-Connection", "close");
            proxy.WriteResponse(response);
        }
Exemplo n.º 2
0
        protected override bool HandleConnection(Socket socket, StreamReader reader, StreamWriter writer)
        {
            var connection = new Connection(reader, writer);
            var request    = connection.ReadRequest();

            var remoteAddress = ((IPEndPoint)socket.RemoteEndPoint).Address;

            request.AddHeader("X-Forwarded-For", remoteAddress);

            Console.Error.Write("PROXY HANDLE CONNECTION: {0} {1}", remoteAddress, request);
            request.DumpHeaders();

            if (authManager != null)
            {
                string authHeader;
                if (!request.Headers.TryGetValue("Proxy-Authorization", out authHeader))
                {
                    authHeader = null;
                }
                var response = authManager.HandleAuthentication(request, authHeader);
                if (response != null)
                {
                    request.ReadBody();
                    connection.WriteResponse(response);
                    return(false);
                }

                // HACK: Mono rewrites chunked requests into non-chunked.
                request.AddHeader("X-Mono-Redirected", "true");
            }

            if (request.Method.Equals("CONNECT"))
            {
                CreateTunnel(connection, socket, reader, writer, request);
                return(false);
            }

            var targetSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            targetSocket.Connect(target.Uri.Host, target.Uri.Port);

            using (var targetStream = new NetworkStream(targetSocket)) {
                var targetReader = new StreamReader(targetStream);
                var targetWriter = new StreamWriter(targetStream);
                targetWriter.AutoFlush = true;

                var targetConnection = new ProxyConnection(connection, targetReader, targetWriter);
                targetConnection.HandleRequest(request);

                targetConnection.Close();
            }

            targetSocket.Close();
            return(false);
        }