public int EndReceive(IAsyncResult ar, out bool sendback)
        {
            int bytesRead = _socket.EndReceive(ar);

            sendback = false;
            if (bytesRead > 0)
            {
                CallbackState st = (CallbackState)ar.AsyncState;
                st.size = bytesRead;

                lock (_decryptionLock)
                {
                    int    bytesToSend = 0;
                    int    obfsRecvSize;
                    byte[] remoteRecvObfsBuffer = _obfs.ClientDecode(st.buffer, bytesRead, out obfsRecvSize, out sendback);
                    if (obfsRecvSize > 0)
                    {
                        _encryptor.Decrypt(remoteRecvObfsBuffer, obfsRecvSize, ReceiveDecryptBuffer, out bytesToSend);
                        int    outlength;
                        byte[] buffer = _protocol.ClientPostDecrypt(ReceiveDecryptBuffer, bytesToSend, out outlength);
                        Array.Copy(buffer, 0, st.buffer, 0, outlength);
                        return(outlength);
                    }
                }
                return(0);
            }
            else
            {
                _close = true;
            }
            return(bytesRead);
        }
Beispiel #2
0
        public int EndReceiveFrom(IAsyncResult ar, ref EndPoint ep)
        {
            int bytesRead = _socket.EndReceiveFrom(ar, ref ep);

            if (bytesRead > 0)
            {
                CallbackState st = (CallbackState)ar.AsyncState;
                st.size = bytesRead;

                int bytesToSend;
                if (!RemoveRemoteUDPRecvBufferHeader(st.buffer, ref bytesRead))
                {
                    return(0); // drop
                }
                byte[] remoteSendBuffer = new byte[65536];
                byte[] obfsBuffer;
                lock (_decryptionLock)
                {
                    byte[] decryptBuffer = new byte[65536];
                    _encryptor.ResetDecrypt();
                    _encryptor.Decrypt(st.buffer, bytesRead, decryptBuffer, out bytesToSend);
                    obfsBuffer    = _protocol.ClientUdpPostDecrypt(decryptBuffer, bytesToSend, out bytesToSend);
                    decryptBuffer = ParseUDPHeader(obfsBuffer, ref bytesToSend);
                    AddRemoteUDPRecvBufferHeader(decryptBuffer, remoteSendBuffer, ref bytesToSend);
                }
                Array.Copy(remoteSendBuffer, 0, st.buffer, 0, bytesToSend);
                return(bytesToSend);
            }
            else
            {
                _close = true;
            }
            return(bytesRead);
        }
Beispiel #3
0
        public IAsyncResult BeginReceiveFrom(byte[] buffer, int size, SocketFlags flags, ref EndPoint ep, AsyncCallback callback, object state)
        {
            CallbackState st = new CallbackState();

            st.buffer = buffer;
            st.size   = size;
            st.state  = state;
            return(_socket.BeginReceiveFrom(buffer, 0, size, flags, ref ep, callback, st));
        }
Beispiel #4
0
        public int BeginSend(byte[] buffer, int size, SocketFlags flags, AsyncCallback callback, object state)
        {
            CallbackState st = new CallbackState();

            st.size  = size;
            st.state = state;

            _socket.BeginSend(buffer, 0, size, 0, callback, st);
            return(size);
        }
Beispiel #5
0
        public int EndReceive(IAsyncResult ar)
        {
            int bytesRead = _socket.EndReceive(ar);

            if (bytesRead > 0)
            {
                CallbackState st = (CallbackState)ar.AsyncState;
                st.size = bytesRead;
                return(bytesRead);
            }
            else
            {
                _close = true;
            }
            return(bytesRead);
        }
        public int BeginSend(byte[] buffer, int size, SocketFlags flags, AsyncCallback callback, object state)
        {
            CallbackState st = new CallbackState();

            st.size  = size;
            st.state = state;

            int bytesToSend = 0;
            int obfsSendSize;

            byte[] obfsBuffer;
            lock (_encryptionLock)
            {
                int    outlength;
                byte[] bytesToEncrypt = _protocol.ClientPreEncrypt(buffer, size, out outlength);
                _encryptor.Encrypt(bytesToEncrypt, outlength, SendEncryptBuffer, out bytesToSend);
                obfsBuffer = _obfs.ClientEncode(SendEncryptBuffer, bytesToSend, out obfsSendSize);
                _socket.BeginSend(obfsBuffer, 0, obfsSendSize, 0, callback, st);
            }
            return(obfsSendSize);
        }
Beispiel #7
0
        public int BeginSendTo(byte[] buffer, int size, SocketFlags flags, AsyncCallback callback, object state)
        {
            CallbackState st = new CallbackState();

            st.buffer = buffer;
            st.size   = size;
            st.state  = state;

            int bytesToSend;

            byte[] bytesToEncrypt      = null;
            byte[] connetionSendBuffer = new byte[65536];
            int    bytes_beg           = 3;
            int    length = size - bytes_beg;

            bytesToEncrypt = new byte[length];
            Array.Copy(buffer, bytes_beg, bytesToEncrypt, 0, length);
            lock (_encryptionLock)
            {
                _encryptor.ResetEncrypt();
                _protocol.SetServerInfoIV(_encryptor.getIV());
                int    obfsSendSize;
                byte[] obfsBuffer = _protocol.ClientUdpPreEncrypt(bytesToEncrypt, length, out obfsSendSize);
                _encryptor.Encrypt(obfsBuffer, obfsSendSize, connetionSendBuffer, out bytesToSend);
            }

            if (_proxy)
            {
                IPAddress ipAddress;
                string    serverURI  = _proxy_server;
                int       serverPort = _proxy_udp_port;
                bool      parsed     = IPAddress.TryParse(serverURI, out ipAddress);
                if (!parsed)
                {
                    bytesToEncrypt = new byte[bytes_beg + 1 + 1 + serverURI.Length + 2 + bytesToSend];
                    Array.Copy(connetionSendBuffer, 0, bytesToEncrypt, bytes_beg + 1 + 1 + serverURI.Length + 2, bytesToSend);
                    bytesToEncrypt[0] = 0;
                    bytesToEncrypt[1] = 0;
                    bytesToEncrypt[2] = 0;
                    bytesToEncrypt[3] = (byte)3;
                    bytesToEncrypt[4] = (byte)serverURI.Length;
                    for (int i = 0; i < serverURI.Length; ++i)
                    {
                        bytesToEncrypt[5 + i] = (byte)serverURI[i];
                    }
                    bytesToEncrypt[5 + serverURI.Length]     = (byte)(serverPort / 0x100);
                    bytesToEncrypt[5 + serverURI.Length + 1] = (byte)(serverPort % 0x100);
                }
                else
                {
                    byte[] addBytes = ipAddress.GetAddressBytes();
                    bytesToEncrypt = new byte[bytes_beg + 1 + addBytes.Length + 2 + bytesToSend];
                    Array.Copy(connetionSendBuffer, 0, bytesToEncrypt, bytes_beg + 1 + addBytes.Length + 2, bytesToSend);
                    bytesToEncrypt[0] = 0;
                    bytesToEncrypt[1] = 0;
                    bytesToEncrypt[2] = 0;
                    bytesToEncrypt[3] = ipAddress.AddressFamily == AddressFamily.InterNetworkV6 ? (byte)4 : (byte)1;
                    for (int i = 0; i < addBytes.Length; ++i)
                    {
                        bytesToEncrypt[4 + i] = addBytes[i];
                    }
                    bytesToEncrypt[4 + addBytes.Length]     = (byte)(serverPort / 0x100);
                    bytesToEncrypt[4 + addBytes.Length + 1] = (byte)(serverPort % 0x100);
                }

                bytesToSend = bytesToEncrypt.Length;
                Array.Copy(bytesToEncrypt, connetionSendBuffer, bytesToSend);
            }
            _socket.BeginSendTo(connetionSendBuffer, 0, bytesToSend, flags, _remoteUDPEndPoint, callback, st);
            return(bytesToSend);
        }
Beispiel #8
0
        public byte[] GetAsyncResultBuffer(IAsyncResult ar)
        {
            CallbackState st = (CallbackState)ar.AsyncState;

            return(st.buffer);
        }
Beispiel #9
0
        public int GetAsyncResultSize(IAsyncResult ar)
        {
            CallbackState st = (CallbackState)ar.AsyncState;

            return(st.size);
        }
Beispiel #10
0
        public int GetAsyncProtocolSize(IAsyncResult ar)
        {
            CallbackState st = (CallbackState)ar.AsyncState;

            return(st.protocol_size);
        }