Ejemplo n.º 1
0
        private static void PerformTunnelHandshake(HttpSocket httpSocket, Proxy proxy, string hostAddress, int port, BrowserConfig browserCfg)
        {
            StringStringKeyValuePairContainer headersContainer = new StringStringKeyValuePairContainer();

            headersContainer.Put(HeaderKeys.Host, hostAddress + ":" + port);
            headersContainer.Put(HeaderKeys.UserAgent, browserCfg.UserAgent);
            headersContainer.Put(HeaderKeys.Connection, HeaderValues.ConnectionKeepAlive);

            if (proxy != null && proxy.HasAuthentication)
            {
                headersContainer.Put(HeaderKeys.ProxyConnection, HeaderValues.ConnectionKeepAlive);
                headersContainer.Put(HeaderKeys.ProxyAuthorization, "Basic " + proxy.ProxyCreds.GetBase64Auth());
            }
            httpSocket.WriteLine(RequestType.Connect.Notation + " " + hostAddress + ":" + port + " " + browserCfg.HttpVersion.Notation);

            foreach (StringStringKeyValuePair kvp in headersContainer.Kvps)
            {
                httpSocket.WriteLine(kvp.Key + ": " + kvp.Value);
            }
            httpSocket.WriteLine();
            httpSocket.FlushUnderlying();

            HttpHeaderDecoder hd = new HttpHeaderDecoder();

            hd.Decode(httpSocket.Stream);

            if (hd.ResponseStatus.HttpCode != HttpCode.Ok)
            {
                throw new IOException("Unable to tunnel through proxy => " + hd.ResponseStatus);
            }
            logger.Debug("Successfully tunneled through proxy");
        }
Ejemplo n.º 2
0
        private static HttpSocket InsecureConnect(Proxy proxy, string hostAddress, int port, BrowserConfig browserCfg)
        {
            logger.Debug(string.Format("Connecting socket ({0}:{1}, proxy => {2})", hostAddress, port, proxy != null));

            Socket socket = proxy is null
                ? ConnectToServerHost(hostAddress, port, browserCfg)
                : ConnectToProxyHost(proxy, browserCfg);

            if (socket is null || !socket.Connected)
            {
                return(null);
            }
            HttpSocket httpSocket = new HttpSocket(socket, browserCfg.ReadTimeout * 1000, browserCfg.WriteTimeout * 1000);

            if (proxy != null && proxy.HasAuthentication)
            {
                PerformTunnelHandshake(httpSocket, proxy, hostAddress, port, browserCfg);
            }
            logger.Debug(string.Format("Successfully connected socket ({0}:{1}, proxy => {2})", hostAddress, port, proxy != null));
            return(httpSocket);
        }
Ejemplo n.º 3
0
        private static HttpSocket SecureConnect(Proxy proxy, string hostAddress, int port, BrowserConfig browserCfg)
        {
            logger.Debug(string.Format("Connecting SSL socket ({0}:{1}, proxy => {2})", hostAddress, port, proxy != null));

            Socket socket = proxy is null
                ? ConnectToServerHost(hostAddress, port == 80? 443 : port, browserCfg)
                : ConnectToProxyHost(proxy, browserCfg);

            if (socket is null || !socket.Connected)
            {
                return(null);
            }
            HttpSocket httpSocket = new HttpSocket(socket, browserCfg.ReadTimeout * 1000, browserCfg.WriteTimeout * 1000);

            if (proxy != null)
            {
                PerformTunnelHandshake(httpSocket, proxy, hostAddress, 443, browserCfg);
            }
            httpSocket.OpenSslStream();

            try {
                httpSocket.SslStream.AuthenticateAsClient(hostAddress);
            } catch (IOException ex) {
                Console.WriteLine(ex.ToString());
            }
            if (!httpSocket.SslStream.IsAuthenticated)
            {
                Console.WriteLine("not authenticated");
                return(null);
            }
            logger.Debug(string.Format("Successfully connected SSL socket ({0}:{1}, proxy => {2})", hostAddress, port, proxy != null));

            if (!SslHosts.Contains(hostAddress))
            {
                SslHosts.Add(hostAddress);
            }
            return(httpSocket);
        }