Example #1
0
        private async Task AuthenticateAsync(Stream s)
        {
            await _negotiationRequest.WriteToAsync(s);

            await s.FlushAsync();

            SocksProxyNegotiationReply negotiationReply = await SocksProxyNegotiationReply.ReadRequestAsync(s);

            if (!negotiationReply.IsVersionSupported)
            {
                throw new SocksProxyException("Socks version 5 is not supported by the proxy server.");
            }

            switch (negotiationReply.Method)
            {
            case SocksProxyAuthenticationMethod.UsernamePassword:
                if (_authRequest == null)
                {
                    throw new SocksProxyAuthenticationFailedException("Socks proxy server requires authentication.");
                }

                await _authRequest.WriteToAsync(s);

                await s.FlushAsync();

                SocksProxyAuthenticationReply authenticationReply = await SocksProxyAuthenticationReply.ReadRequestAsync(s);

                if (!authenticationReply.IsVersionSupported)
                {
                    throw new SocksProxyAuthenticationFailedException("Socks proxy server does not support username/password method version 1.");
                }

                if (authenticationReply.Status != SocksProxyAuthenticationStatus.Success)
                {
                    throw new SocksProxyAuthenticationFailedException("Socks proxy server authentication failed: invalid username or password.");
                }

                break;

            case SocksProxyAuthenticationMethod.NoAuthenticationRequired:
                break;

            case SocksProxyAuthenticationMethod.NoAcceptableMethods:
                if (_authRequest == null)
                {
                    throw new SocksProxyAuthenticationFailedException("Socks proxy server requires authentication.");
                }
                else
                {
                    throw new SocksProxyAuthenticationFailedException("Socks proxy server does not support username/password method.");
                }

            default:
                throw new SocksProxyException("Socks proxy server returned unknown method.");
            }
        }
Example #2
0
        public static async Task <SocksProxyAuthenticationReply> ReadRequestAsync(Stream s)
        {
            SocksProxyAuthenticationReply reply = new SocksProxyAuthenticationReply();

            byte[] buffer = new byte[2];
            await s.ReadBytesAsync(buffer, 0, buffer.Length);

            reply._version = buffer[0];
            reply._status  = buffer[1] == 0 ? SocksProxyAuthenticationStatus.Success : SocksProxyAuthenticationStatus.Failure;

            return(reply);
        }