Ejemplo n.º 1
0
        public ClientProxyConnector(
            IClientConnectorFactoryProducer clientConnectorFactoryProducer,
            IOptions <ProxyOptions> proxyOptions,
            ILogger logger
            )
        {
            this.clientConnector = clientConnectorFactoryProducer.CreateClient(ConnectorFactory.TransmissionControlProtocol)
                                   .Create();

            this.proxyOptions = proxyOptions.Value;
            this.logger       = logger;
        }
Ejemplo n.º 2
0
        public void Connect(IPAddress ipAddress, int port)
        {
            if (proxyOptions == null || string.IsNullOrEmpty(proxyOptions.Server?.Trim()))
            {
                ///Trying to read proxy from WebRequest
                try
                {
                    var proxyRequest = WebRequest.GetSystemWebProxy();
                    var requestedUri = new Uri($"http://{ipAddress.ToString()}");
                    var proxyUri     = proxyRequest.GetProxy(requestedUri);

                    if (proxyUri == null || proxyUri == requestedUri)
                    {
                        throw new ProxyNotFoundException();
                    }

                    proxyOptions = new ProxyOptions
                    {
                        Port   = proxyUri.Port,
                        Server = Dns.GetHostAddresses(proxyUri.Host)[0].ToString()
                    };
                }
                catch (ProxyNotFoundException)
                {
                    proxyOptions = null;
                }
                catch (Exception ex)
                {
                    this.logger.Error(ex);
                    proxyOptions = null;
                }
            }

            if (proxyOptions != null)///Proxy found
            {
                /// Basic of Proxy Protocol
                /// CONNECT: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/CONNECT
                clientConnector.Connect(IPAddress.Parse(proxyOptions.Server), proxyOptions.Port);

                ///Proxy Handshake Connection
                var message = Encoding.ASCII.GetBytes($"CONNECT {ipAddress.ToString()}:{port} HTTP/1.1\r\n"
                                                      + $"Connection: keep-alive\r\n\r\n");
                clientConnector.Send(message, 0, message.Length);

                ///Receiving Proxy Response
                Array.Clear(buffer, 0, buffer.Length);
                clientConnector.Receive(buffer, 0, 1024);

                var bufferAsString = Encoding.ASCII.GetString(buffer);
                bufferAsString = bufferAsString.Replace('\0', ' ').Trim();

                if (!Regex.IsMatch(bufferAsString, @"^HTTP.+200\s+Connection\s+established.*", RegexOptions.Multiline))
                {
                    throw new ProxyConnectionException($"Connection Failed: {bufferAsString}");
                }
            }
            else///Direct connection (PROXY NOT FOUND)
            {
                clientConnector.Connect(ipAddress, port);
            }
        }