Ejemplo n.º 1
0
        public override async Task <Stream> OpenStreamAsync(HostPortCollection dstSrv)
        {
            var socket = new TcpClient();

            if (await SwallowExceptionUtils.TryExec(() => socket.ConnectAsync(_proxyInformation.Addr, _proxyInformation.Port)) && socket.Connected)
            {
                var buf       = new byte[2];
                int bytesRead = 0;
                var nStream   = socket.GetStream();

                await nStream.WriteAsync(Socks5Payload, 0, Socks5Payload.Length);

                bytesRead = await nStream.ReadAsync(buf, 0, 2);

                if (bytesRead != 2)
                {
                    Status = ProxyStatus.ProtocolError;
                    socket.Close();
                    socket.Dispose();
                }

                if (buf[0] == 0x05 && buf[1] == 0x00)
                {
                    //Continue...
                    var cmd = ProxyCommandBuildUtils.BuildSocks5Command(0x01, dstSrv); //0x01 = SOCKS5_CMD_CONNECT
                    await nStream.WriteAsync(cmd, 0, cmd.Length);

                    Array.Clear(cmd, 0, cmd.Length);
                    bytesRead = await nStream.ReadAsync(cmd, 0, cmd.Length);

                    if (bytesRead > 2 && cmd[1] == 0x00)
                    {
                        return(nStream);
                    }
                    else
                    {
                        Status = ProxyStatus.ProtocolError;
                        Debugger.Break();
                    }
                }
                else
                {
                    Status = buf[0] == 0x02 ? ProxyStatus.PasswordRequired : ProxyStatus.ProtocolError;
                }
                nStream?.Close();
                nStream?.Dispose();
            }
            else
            {
                Status = ProxyStatus.ConnectionFail;
            }

            return(null);
        }
Ejemplo n.º 2
0
        public override async Task <Stream> OpenStreamAsync(HostPortCollection dstSrv)
        {
            var socket = new TcpClient();

            if (await SwallowExceptionUtils.TryExec(() => socket.ConnectAsync(_proxyInformation.Addr, _proxyInformation.Port)) && socket.Connected)
            {
                var nStream = socket.GetStream();
                var cmd     = ProxyCommandBuildUtils.GenerateHttpConnectCommand(dstSrv.Host, dstSrv.Port);

                await nStream.WriteAsync(cmd, 0, cmd.Length);

                return(nStream);
            }
            return(null);
        }
Ejemplo n.º 3
0
        public override async Task <Stream> OpenStreamAsync(HostPortCollection dstSrv)
        {
            var socket = new TcpClient();

            if (await SwallowExceptionUtils.TryExec(
                    () => socket.ConnectAsync(_proxyInformation.Addr, _proxyInformation.Port)) && socket.Connected)
            {
                var buf       = new byte[8];
                int bytesRead = 0;

                var nStream = socket.GetStream();
                var cmd     = ProxyCommandBuildUtils.BuildSocks4Command(0x01, dstSrv); //SOCKS4_CMD_CONNECT

                await nStream.WriteAsync(cmd, 0, cmd.Length);

                bytesRead = await nStream.ReadAsync(buf, 0, buf.Length);

                if (bytesRead > 1 && buf[1] == 0x5A)
                {
                    return(nStream);
                }
                else
                {
                    if (buf[1] == 0xFF)
                    {
                        //May be socks5?
                        Debugger.Break();
                    }
                    Status = ProxyStatus.ProtocolError;
                }
            }
            else
            {
                Status = ProxyStatus.ConnectionFail;
            }
            socket.Close();
            socket.Dispose();
            return(null);
        }