Пример #1
0
        internal bool ResendRequest()
        {
            bool b = this.pipeServer != null;

            if (!this.ConnectToHost())
            {
                KProxyApplication.DebugSpew("ConnectToHost returned null. Bailing...");
                this.m_session.SetBitFlag(SessionFlags.ServerPipeReused, b);
                return(false);
            }
            try
            {
                this.pipeServer.IncrementUse(this.m_session.id);
                this.m_session.Timers.ServerConnected = this.pipeServer.dtConnected;
                this._bWasForwarded = this.pipeServer.isConnectedToGateway;
                this.m_session.SetBitFlag(SessionFlags.ServerPipeReused, this.pipeServer.iUseCount > 1);
                this.m_session.SetBitFlag(SessionFlags.SentToGateway, this._bWasForwarded);
                if (!this._bWasForwarded && !this.m_session.isHTTPS)
                {
                    this.m_session.oRequest.headers.RenameHeaderItems("Proxy-Connection", "Connection");
                }
                if (!this.pipeServer.isAuthenticated)
                {
                    string str = this.m_session.oRequest.headers["Authorization"];
                    if ((str != null) && str.StartsWith("N"))
                    {
                        this.pipeServer.MarkAsAuthenticated(this.m_session.LocalProcessID);
                    }
                }
                this.m_session.Timers.KProxyBeginRequest = DateTime.Now;
                if (this.m_session.oFlags.ContainsKey("request-trickle-delay"))
                {
                    int num = int.Parse(this.m_session.oFlags["request-trickle-delay"]);
                    this.pipeServer.TransmitDelay = num;
                }
                this.pipeServer.Send(this.m_session.oRequest.headers.ToByteArray(true, true, this._bWasForwarded && !this.m_session.isHTTPS));
                this.pipeServer.Send(this.m_session.requestBodyBytes);
            }
            catch (Exception exception)
            {
                if (this.bServerSocketReused && (this.m_session.state != SessionStates.Aborted))
                {
                    this.pipeServer = null;
                    return(this.ResendRequest());
                }
                KProxyApplication.DebugSpew("ResendRequest() failed: " + Utilities.DescribeException(exception));
                this.m_session.oRequest.FailSession(0x1f8, "Kavprot Proxy - Send Failure", "ResendRequest() failed: " + Utilities.DescribeException(exception));
                return(false);
            }
            this.m_session.oFlags["x-EgressPort"] = this.pipeServer.LocalPort.ToString();
            if (this.m_session.oFlags.ContainsKey("log-drop-request-body"))
            {
                this.m_session.oFlags["x-RequestBodyLength"] = (this.m_session.requestBodyBytes != null) ? this.m_session.requestBodyBytes.Length.ToString() : "0";
                this.m_session.requestBodyBytes = new byte[0];
            }
            return(true);
        }
Пример #2
0
 internal void Initialize(bool bAlloc)
 {
     if (bAlloc)
     {
         this.m_responseData = new MemoryStream(0x4000);
     }
     else
     {
         this.m_responseData = null;
     }
     this._lngLeakedOffset        = this._iBodySeekProgress = this.iEntityBodyOffset = 0;
     this._lngLastChunkInfoOffset = -1L;
     this.m_inHeaders             = null;
     this._bLeakedHeaders         = false;
     this.pipeServer     = null;
     this._bWasForwarded = false;
     this.m_session.SetBitFlag(SessionFlags.ServerPipeReused, false);
 }
Пример #3
0
 internal void EnqueuePipe(ServerPipe oPipe)
 {
     if ((KPCONFIG.bReuseServerSockets && ((oPipe.sPoolKey != null) && (oPipe.sPoolKey.Length >= 2))) && ((oPipe.ReusePolicy != PipeReusePolicy.MarriedToClientPipe) && (oPipe.ReusePolicy != PipeReusePolicy.NoReuse)))
     {
         Queue <ServerPipe> queue;
         lock (this.thePool)
         {
             if (!this.thePool.TryGetValue(oPipe.sPoolKey, out queue))
             {
                 queue = new Queue <ServerPipe>();
                 this.thePool.Add(oPipe.sPoolKey, queue);
             }
         }
         oPipe.iLastPooled = Environment.TickCount;
         lock (queue)
         {
             queue.Enqueue(oPipe);
         }
     }
 }
Пример #4
0
 internal void _detachServerPipe()
 {
     if (this.pipeServer != null)
     {
         if (((this.pipeServer.ReusePolicy != PipeReusePolicy.NoReuse) && (this.pipeServer.ReusePolicy != PipeReusePolicy.MarriedToClientPipe)) && (this.pipeServer.isClientCertAttached && !this.pipeServer.isAuthenticated))
         {
             this.pipeServer.MarkAsAuthenticated(this.m_session.LocalProcessID);
         }
         if ((this.pipeServer.ReusePolicy == PipeReusePolicy.NoReuse) || (this.pipeServer.ReusePolicy == PipeReusePolicy.MarriedToClientPipe))
         {
             if (this.pipeServer.Connected)
             {
                 this.pipeServer.End();
             }
         }
         else
         {
             Proxy.htServerPipePool.EnqueuePipe(this.pipeServer);
         }
         this.pipeServer = null;
     }
 }
Пример #5
0
        private bool ConnectToHost()
        {
            IPEndPoint point = null;
            string     str2;
            string     str3;

            IPAddress[] addressArray;
            string      sHostAndPort = this.m_session.oFlags["x-overrideHost"];

            if (sHostAndPort == null)
            {
                sHostAndPort = this.m_session.host;
            }
            if (this.m_session.oFlags["x-overrideGateway"] != null)
            {
                if (string.Equals("DIRECT", this.m_session.oFlags["x-overrideGateway"], StringComparison.OrdinalIgnoreCase))
                {
                    this.m_session.bypassGateway = true;
                }
                else
                {
                    point = Utilities.IPEndPointFromHostPortString(this.m_session.oFlags["x-overrideGateway"]);
                }
            }
            else if (!this.m_session.bypassGateway)
            {
                int tickCount = Environment.TickCount;
                point = KProxyApplication.oProxy.FindGatewayForOrigin(this.m_session.oRequest.headers.UriScheme, sHostAndPort);
                this.m_session.Timers.GatewayDeterminationTime = Environment.TickCount - tickCount;
            }
            if (point != null)
            {
                this._bWasForwarded = true;
            }
            else if (this.m_session.isFTP)
            {
                this.m_session.oRequest.FailSession(0x1f6, "Kavprot Proxy - FTP Connection Failed", "[KProxy] Kavprot Proxy does not support proxying FTP traffic without an upstream HTTP->FTP proxy.");
                return(false);
            }
            int iPort = this.m_session.isHTTPS ? 0x1bb : (this.m_session.isFTP ? 0x15 : 80);

            Utilities.CrackHostAndPort(sHostAndPort, out str2, ref iPort);
            if (point != null)
            {
                if (this.m_session.isHTTPS)
                {
                    str3 = "GATEWAY:HTTPS:" + str2 + ":" + iPort.ToString();
                }
                else
                {
                    str3 = "GW:" + point.ToString();
                }
            }
            else
            {
                str3 = (this.m_session.isHTTPS ? "HTTPS:" : "") + str2 + ":" + iPort.ToString();
            }
            if (((this.pipeServer != null) && !this.m_session.oFlags.ContainsKey("X-ServerPipe-Marriage-Trumps-All")) && !this.SIDsMatch(this.m_session.LocalProcessID, str3, this.pipeServer.sPoolKey))
            {
                this.m_session.oFlags["X-Divorced-ServerPipe"] = string.Format("Had: {0} but needs: {1}", this.pipeServer.sPoolKey, str3);
                this._detachServerPipe();
            }
            if ((this.pipeServer == null) && !this.m_session.oFlags.ContainsKey("X-Bypass-ServerPipe-Reuse-Pool"))
            {
                this.pipeServer = Proxy.htServerPipePool.DequeuePipe(str3, this.m_session.LocalProcessID, this.m_session.id);
            }
            if (this.pipeServer != null)
            {
                StringDictionary dictionary;
                this.m_session.Timers.ServerConnected = this.pipeServer.dtConnected;
                (dictionary = this.m_session.oFlags)["x-serversocket"] = dictionary["x-serversocket"] + "REUSE " + this.pipeServer._sPipeName;
                if ((this.pipeServer.Address != null) && !this.pipeServer.isConnectedToGateway)
                {
                    this.m_session.m_hostIP           = this.pipeServer.Address.ToString();
                    this.m_session.oFlags["x-hostIP"] = this.m_session.m_hostIP;
                }
                return(true);
            }
            int port = iPort;

            if (point != null)
            {
                addressArray = new IPAddress[] { point.Address };
                port         = point.Port;
            }
            else
            {
                try
                {
                    addressArray = DNSResolver.GetIPAddressList(str2, true, this.m_session.Timers);
                }
                catch (Exception exception)
                {
                    this.m_session.oRequest.FailSession(0x1f6, "Kavprot Proxy - DNS Lookup Failed", "KProxy: DNS Lookup for " + Utilities.HtmlEncode(str2) + " failed. " + Utilities.DescribeException(exception));
                    return(false);
                }
                if ((port < 0) || (port > 0xffff))
                {
                    this.m_session.oRequest.FailSession(0x1f6, "Invalid Request", "HTTP Request specified an invalid port number.");
                    return(false);
                }
            }
            try
            {
                this.pipeServer = new ServerPipe("ServerPipe#" + this.m_session.id.ToString(), this._bWasForwarded);
                Socket oSocket = CreateConnectedSocket(addressArray, port, this.m_session);
                if (this._bWasForwarded)
                {
                    if (!this.m_session.isHTTPS)
                    {
                        this.pipeServer.WrapSocketInPipe(this.m_session, oSocket, false, false, str2, this.m_session.oFlags["https-Client-Certificate"], "GW:" + point.ToString(), ref this.m_session.Timers.HTTPSHandshakeTime);
                    }
                    else
                    {
                        this.m_session.oFlags["x-CreatedTunnel"] = "KProxy-Created-A-CONNECT-Tunnel";
                        this.pipeServer.WrapSocketInPipe(this.m_session, oSocket, true, true, str2, this.m_session.oFlags["https-Client-Certificate"], "HTTPS:" + str2 + ":" + iPort.ToString(), ref this.m_session.Timers.HTTPSHandshakeTime);
                    }
                }
                else
                {
                    this.pipeServer.WrapSocketInPipe(this.m_session, oSocket, this.m_session.isHTTPS, false, str2, this.m_session.oFlags["https-Client-Certificate"], (this.m_session.isHTTPS ? "HTTPS:" : "") + str2 + ":" + iPort.ToString(), ref this.m_session.Timers.HTTPSHandshakeTime);
                }
                return(true);
            }
            catch (Exception exception2)
            {
                if (this._bWasForwarded)
                {
                    this.m_session.oRequest.FailSession(0x1f6, "Kavprot Proxy - Gateway Connection Failed", "[Kavprot Proxy] Connection to Gateway failed.<BR>Exception Text: " + Utilities.DescribeException(exception2));
                }
                else
                {
                    this.m_session.oRequest.FailSession(0x1f6, "Kavprot Proxy - Connection Failed", "[Kavprot Proxy] Connection to " + Utilities.HtmlEncode(str2) + " failed.<BR>Exception Text: " + Utilities.DescribeException(exception2));
                }
                return(false);
            }
        }