Exemple #1
0
        private void RecvFirstByteCompleted(SocketOperationResult result)
        {
            if (result.actualSize != 1)
            {
                this.Stop();
                return;
            }

            byte firstByte = result.buf[0];

            this.mProxyFirstByte = (int)firstByte;

            bool firstByteOK = this.InvokeProxyEventCallback(this.ProxyFirstByteFilter);

            if (firstByteOK)
            {
                if (firstByte > 5)
                {
                    this.StartHttpProxy(firstByte);
                    return;
                }
                else if (5 == firstByte)
                {
                    if (!this.mUseHttpOnly)
                    {
                        this.StartSocks5Proxy();
                        return;
                    }
                }
                //
            }

            this.Stop();
        }
Exemple #2
0
        private void RecvSocks5MethodListCompleted(SocketOperationResult result)
        {
            int nMethod = result.actualSize;

            if (nMethod != result.size)
            {
                this.Stop();
                return;
            }

            bool methodAccepted = false;

            for (int i = 0; i < nMethod; ++i)
            {
                if (0 == this.mBuffer1[i])
                {
                    methodAccepted = true;
                    break;
                }
                //
            }

            if (!methodAccepted)
            {
                this.Stop();
                return;
            }

            this.mBuffer1[0] = 5;
            this.mBuffer1[1] = 0;
            SocketSend(this.mSock1, this.mBuffer1, 0, 2, this.SendSocks5MethodAcceptCompleted, null);
        }
Exemple #3
0
 private void ForwardClientFirstDataCompleted(SocketOperationResult result)
 {
     if (result.actualSize != result.size)
     {
         this.Stop();
         return;
     }
     this.StartProxyTransfer();
 }
Exemple #4
0
        private void SendHttpConnectMethodAcceptCompleted(SocketOperationResult result)
        {
            if (result.actualSize != result.size)
            {
                this.Stop();
                return;
            }

            this.StartProxy();
        }
Exemple #5
0
        private void SendHttpResponseHeaderCompleted(SocketOperationResult result)
        {
            if (result.actualSize != result.size)
            {
                this.Stop();
                return;
            }

            byte[] buf = this.mHttpResponseData;
            SocketSend(this.mSock1, buf, 0, buf.Length, this.SendHttpResponseDataCompleted, null);
        }
Exemple #6
0
 private void SendHttpResponseDataCompleted(SocketOperationResult result)
 {
     try
     {
         this.mSock1.Shutdown(SocketShutdown.Both);
     }
     catch (Exception)
     {
         //
     }
     this.PendingStop();
 }
Exemple #7
0
        private void RecvSocks5MethodCountCompleted(SocketOperationResult result)
        {
            if (result.actualSize != result.size)
            {
                this.Stop();
                return;
            }

            int methodCount = (int)this.mBuffer1[0];

            SocketRecv(this.mSock1, this.mBuffer1, 0, methodCount, true, this.RecvSocks5MethodListCompleted, null);
        }
Exemple #8
0
        private void PairedSockFwdCompleted(SocketOperationResult result)
        {
            if (result.actualSize != result.size)
            {
                this.PendingStop();
                return;
            }

            bool direction1To2 = (bool)result.state;

            this.PairedSocketTransfer(direction1To2);
        }
Exemple #9
0
        private static SocketOperationResult PrepareSocketOperationResult(Socket sock, bool directionOut, byte[] buf, int offset, int size, bool waitAll, SocketOperationCallback callback, object state)
        {
            SocketOperationResult result = new SocketOperationResult();

            result.sock         = sock;
            result.directionOut = directionOut;
            result.buf          = buf;
            result.offset       = offset;
            result.size         = size;
            result.waitAll      = waitAll;
            result.callback     = callback;
            result.state        = state;
            return(result);
        }
Exemple #10
0
        private void RecvSocks5DstAddrDataCompleted(SocketOperationResult result)
        {
            if (result.actualSize != result.size)
            {
                this.Stop();
                return;
            }

            int offset   = 4;
            int addrType = (int)this.mBuffer1[3];

            if (1 == addrType || 4 == addrType)
            {
                byte[] addrBytes = null;
                if (1 == addrType)
                {
                    addrBytes = new byte[4];
                }
                else
                {
                    addrBytes = new byte[16];
                }

                Array.Copy(this.mBuffer1, offset, addrBytes, 0, addrBytes.Length);
                offset += addrBytes.Length;

                IPAddress addr = new IPAddress(addrBytes);
                this.mProxyDstHost = addr.ToString();
            }
            else if (3 == addrType)
            {
                int addrLen = (int)this.mBuffer1[4];
                this.mProxyDstHost = Encoding.ASCII.GetString(this.mBuffer1, 5, addrLen);
                offset            += (addrLen + 1);
            }

            this.mProxyDstPort   = (int)this.mBuffer1[offset++];
            this.mProxyDstPort <<= 8;
            this.mProxyDstPort  += (int)this.mBuffer1[offset];

            for (int i = 0; i < 10; ++i)
            {
                this.mBuffer1[i] = 0;
            }
            this.mBuffer1[0] = 5;
            this.mBuffer1[3] = 1;

            SocketSend(this.mSock1, this.mBuffer1, 0, 10, this.SendSocks5RequestAcceptCompleted, null);
        }
Exemple #11
0
        private void SendSocks5RequestAcceptCompleted(SocketOperationResult result)
        {
            if (result.actualSize != result.size)
            {
                this.Stop();
                return;
            }

            if (!this.PreprocessProxyRemoteConnection())
            {
                return;
            }

            this.StartProxy();
        }
Exemple #12
0
        private static void InvokeSocketOperationCallback(SocketOperationResult result)
        {
            SocketOperationCallback callback = result.callback;

            if (null == callback)
            {
                return;
            }

            try
            {
                callback(result);
            }
            catch (Exception)
            {
            }
        }
Exemple #13
0
        private void RecvPartialHttpHdrCompleted(SocketOperationResult result)
        {
            int n = result.actualSize;

            if (n <= 0)
            {
                this.Stop();
                return;
            }

            bool httpHdrEnd = false;

            for (int i = 0; i < n; ++i)
            {
                this.mRawHttpRequestHdr += (char)this.mBuffer1[i];
                if (this.mRawHttpRequestHdr.EndsWith("\r\n\r\n", StringComparison.Ordinal))
                {
                    this.mFirstBufferLen    = n;
                    this.mFirstBufferOffset = i + 1;
                    httpHdrEnd = true;
                    break;
                }
            }

            if (null == this.mHttpRequestHdrFirstLine)
            {
                this.ProcessHttpRequestHeaderFirstLine();
            }

            if (httpHdrEnd)
            {
                this.ProcessHttpRequestHeader();
            }
            else
            {
                if (this.mRawHttpRequestHdr.Length < HttpHeaderMaxLength)
                {
                    this.RecvHttpHdrNext();
                }
                else
                {
                    this.Stop();
                }
            }
            //
        }
Exemple #14
0
        private static void SendRecvNext(SocketOperationResult result)
        {
            bool waitAll    = result.waitAll;
            int  actualSize = result.actualSize;
            int  remain     = result.size - actualSize;

            if ((waitAll && remain <= 0) || (!waitAll && result.lastBytes > 0))
            {
                InvokeSocketOperationCallback(result);
                return;
            }

            bool ok = false;

            byte[] buf    = result.buf;
            int    offset = result.offset + actualSize;
            Socket sock   = result.sock;

            try
            {
                if (result.directionOut)
                {
                    sock.BeginSend(buf, offset, remain, SocketFlags.None, SendRecvPartialCompleted, result);
                }
                else
                {
                    sock.BeginReceive(buf, offset, remain, SocketFlags.None, SendRecvPartialCompleted, result);
                }
                ok = true;
            }
            catch (Exception)
            {
                //
            }

            if (!ok)
            {
                result.lastBytes = -1;
                InvokeSocketOperationCallback(result);
            }

            //
        }
Exemple #15
0
        private void PairedSockRecvCompleted(SocketOperationResult result)
        {
            int n = result.actualSize;

            if (n <= 0)
            {
                this.PendingStop();
                return;
            }

            bool direction1To2 = (bool)result.state;

            byte[] buf  = result.buf;
            Socket sock = this.mSock2;

            if (!direction1To2)
            {
                sock = this.mSock1;
            }

            SocketSend(sock, buf, 0, n, this.PairedSockFwdCompleted, direction1To2);
        }
Exemple #16
0
        private void RecvSocks5RequestCompleted(SocketOperationResult result)
        {
            if (result.actualSize != result.size)
            {
                this.Stop();
                return;
            }

            if (1 != this.mBuffer1[1])
            {
                this.Stop();
                return;
            }

            int addrLen  = 0;
            int addrType = (int)this.mBuffer1[3];

            if (1 == addrType)
            {
                addrLen = 3;
            }
            else if (3 == addrType)
            {
                addrLen = (int)this.mBuffer1[4];
            }
            else if (4 == addrType)
            {
                addrLen = 15;
            }

            if (addrLen <= 0)
            {
                this.Stop();
                return;
            }

            addrLen += 2;
            SocketRecv(this.mSock1, this.mBuffer1, 5, addrLen, true, this.RecvSocks5DstAddrDataCompleted, null);
        }
Exemple #17
0
        private static void SendRecvPartialCompleted(IAsyncResult iar)
        {
            SocketOperationResult result = iar.AsyncState as SocketOperationResult;

            if (null == result)
            {
                return;
            }

            int    n    = 0;
            Socket sock = result.sock;

            try
            {
                if (result.directionOut)
                {
                    n = sock.EndSend(iar);
                }
                else
                {
                    n = sock.EndReceive(iar);
                }
            }
            catch (Exception)
            {
                //
            }

            result.lastBytes = n;
            if (n <= 0)
            {
                InvokeSocketOperationCallback(result);
                return;
            }

            result.actualSize += n;
            SendRecvNext(result);
        }
Exemple #18
0
        private void RecvHttpRequestDataCompleted(SocketOperationResult result)
        {
            if (result.actualSize != result.size)
            {
                this.Stop();
                return;
            }

            this.mHttpResponseData = null;
            if (!this.InvokeProxyEventCallback(this.HttpRequestDataUserHandler))
            {
                this.Stop();
                return;
            }

            if (null == this.mHttpResponseData || 0 == this.mHttpResponseData.Length)
            {
                this.Stop();
                return;
            }

            StringBuilder sb = new StringBuilder();

            sb.Append(this.mHttpRequestVersion);
            sb.Append(" 200 OK\r\n");
            sb.Append("Server: nginx\r\n");
            sb.Append("Content-Length: ");
            sb.Append(this.mHttpResponseData.Length.ToString());
            sb.Append("\r\n");
            sb.Append("Connection: close\r\n");
            sb.Append("\r\n");

            string hdr = sb.ToString();

            byte[] buf = Encoding.ASCII.GetBytes(hdr);
            SocketSend(this.mSock1, buf, 0, buf.Length, this.SendHttpResponseHeaderCompleted, null);
        }
Exemple #19
0
 private void SendSocks5MethodAcceptCompleted(SocketOperationResult result)
 {
     SocketRecv(this.mSock1, this.mBuffer1, 0, 5, true, this.RecvSocks5RequestCompleted, null);
 }
Exemple #20
0
        private static void SocketRecv(Socket sock, byte[] buf, int offset, int size, bool waitAll, SocketOperationCallback callback, object state)
        {
            SocketOperationResult result = PrepareSocketOperationResult(sock, false, buf, offset, size, waitAll, callback, state);

            SendRecvNext(result);
        }
Exemple #21
0
        private static void SocketSend(Socket sock, byte[] buf, int offset, int size, SocketOperationCallback callback, object state)
        {
            SocketOperationResult result = PrepareSocketOperationResult(sock, true, buf, offset, size, true, callback, state);

            SendRecvNext(result);
        }