Esempio n. 1
0
        public int FillTrojanRequest(Span <byte> data, Destination.Destination destination, bool isUdpPayload = false, ushort udpPayloadSize = 0)
        {
            Span <byte> crlf = stackalloc byte[2];

            crlf[0] = 0x0D;
            crlf[1] = 0x0A;
            int len = 0;

            if (!isUdpPayload)
            {
                hashedPassword.Span.CopyTo(data);
                len += hashedPassword.Length;
                crlf.CopyTo(data.Slice(len, 2));
                len += crlf.Length;
                switch (destination.TransportProtocol)
                {
                case TransportProtocol.Tcp:
                    data[len++] = 1;
                    break;

                case TransportProtocol.Udp:
                    data[len++] = 3;
                    break;
                }
            }
            len += destination.FillSocks5StyleAddress(data.Slice(len));
            if (isUdpPayload)
            {
                data[len++] = (byte)(udpPayloadSize >> 8);
                data[len++] = (byte)(udpPayloadSize & 0xFF);
            }
            crlf.CopyTo(data.Slice(len));
            len += 2;
            return(len);
        }
Esempio n. 2
0
        public async void SendPacketToRemote(Memory <byte> data, Destination.Destination destination)
        {
            try
            {
                await udpSendLock.WaitAsync().ConfigureAwait(false);
            }
            catch (NullReferenceException)
            {
                return;
            }
            catch (ObjectDisposedException)
            {
                return;
            }
            var udpSendBuffer    = sendArrayPool.Rent(destination.Host.Size + data.Length + 4);
            var udpSendEncBuffer = sendArrayPool.Rent(destination.Host.Size + data.Length + 52);

            try
            {
                // TODO: avoid copy
                var headerLen = destination.FillSocks5StyleAddress(udpSendBuffer);
                data.Span.CopyTo(udpSendBuffer.AsSpan(headerLen));
                var cryptor = ShadowsocksFactory.GlobalCryptorFactory.CreateCryptor();
                var ivLen   = Encrypt(Array.Empty <byte>(), udpSendEncBuffer, cryptor); // Fill IV/Salt first
                var len     = EncryptAll(udpSendBuffer.AsSpan(0, headerLen + data.Length), udpSendEncBuffer.AsSpan((int)ivLen), cryptor);
                _ = udpOutputStream.WriteAsync(udpSendEncBuffer.AsBuffer(0, (int)(len + ivLen)));
            }
            finally
            {
                try
                {
                    udpSendLock.Release();
                }
                catch (ObjectDisposedException) { }
                sendArrayPool.Return(udpSendBuffer);
                sendArrayPool.Return(udpSendEncBuffer);
            }
        }