Пример #1
0
        private IDataAdapter ConnectUdp(IpProxyToken token, Logger logger, PropertyBag properties)
        {
            IDataAdapter adapter = null;

            try
            {
                UdpClient client = new UdpClient(IsTokenIpV6(token) ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork);
                if (token.Hostname == null)
                {
                    client.Connect(token.Address, token.Port);
                }
                else
                {
                    client.Connect(token.Hostname, token.Port);
                }

                NetUtils.PopulateBagFromSocket(client.Client, properties);

                adapter = new UdpClientDataAdapter(client, IpProxyClient.GetDescription(token));
            }
            catch (SocketException ex)
            {
                logger.LogException(ex);
                token.Status = NetStatusCodes.ConnectFailure;
            }
            catch (IOException ex)
            {
                logger.LogException(ex);
                token.Status = NetStatusCodes.ConnectFailure;
            }

            return(adapter);
        }
        /// <summary>
        /// Connect the client
        /// </summary>
        /// <param name="token">The proxy token</param>
        /// <param name="logger">The proxy logger instance</param>
        /// <param name="meta">The proxy meta data</param>
        /// <param name="globalMeta">The proxy global meta data</param>
        /// <param name="properties">The proxy property bag</param>
        /// <returns>The connected adapter</returns>
        public override IDataAdapter Connect(ProxyToken token, Logger logger, MetaDictionary meta, MetaDictionary globalMeta, PropertyBag properties)
        {
            IDataAdapter ret = null;

            for (int i = 0; i < _clients.Length; ++i)
            {
                ret = _clients[i].Connect(token, logger, meta, globalMeta, properties);

                if (ret != null)
                {
                    break;
                }
            }

            if ((ret == null) && (token.Status == NetStatusCodes.Success))
            {
                ret = new IpProxyClient().Connect(token, logger, meta, globalMeta, properties);
            }

            return(ret);
        }
Пример #3
0
        /// <summary>
        /// Connect to the required service with the token
        /// </summary>
        /// <param name="token">The token recevied from proxy</param>
        /// <param name="globalMeta">The global meta</param>
        /// <param name="logger">The logger</param>
        /// <param name="meta">The meta</param>
        /// <param name="properties">Property bag to add any information to</param>
        /// <param name="credmgr">Credentials manager</param>
        /// <returns>The connected data adapter</returns>
        public override IDataAdapter Connect(ProxyToken token, Logger logger, MetaDictionary meta, MetaDictionary globalMeta, PropertyBag properties, CredentialsManagerService credmgr)
        {
            IDataAdapter ret = null;

            token.Status = NetStatusCodes.ConnectFailure;

            if ((token is IpProxyToken) && ((IpProxyToken)token).ClientType == IpProxyToken.IpClientType.Tcp)
            {
                TcpClient client = new TcpClient();

                try
                {
                    client.Connect(_hostname, _port);

                    if (token is FullHttpProxyToken)
                    {
                        bool binary = false;

                        if ((token.Layers != null) && (token.Layers.Length > 0))
                        {
                            foreach (INetworkLayer layer in token.Layers)
                            {
                                // Find a binding layer
                                if (layer.Binding == NetworkLayerBinding.Client ||
                                    layer.Binding == NetworkLayerBinding.ClientAndServer ||
                                    layer.Binding == NetworkLayerBinding.Default)
                                {
                                    if (!(layer is HttpNetworkLayer))
                                    {
                                        binary = true;
                                    }

                                    break;
                                }
                            }
                        }

                        if (!binary)
                        {
                            ret = new FullHttpProxyDataAdapter(client, (FullHttpProxyToken)token, IpProxyClient.GetDescription((IpProxyToken)token), logger);
                            NetUtils.PopulateBagFromSocket(client.Client, properties);
                            token.Status = NetStatusCodes.Success;
                        }
                        else
                        {
                            ConnectWithIpProxyToken(client.GetStream(), (IpProxyToken)token, logger);
                            if (token.Status == NetStatusCodes.Success)
                            {
                                NetUtils.PopulateBagFromSocket(client.Client, properties);
                                ret = new TcpClientDataAdapter(client, IpProxyClient.GetDescription((IpProxyToken)token));
                            }
                        }
                    }
                    else
                    {
                        if (token is HttpProxyToken)
                        {
                            ConnectWithHttpProxyToken(client.GetStream(), (HttpProxyToken)token, logger);
                        }
                        else
                        {
                            ConnectWithIpProxyToken(client.GetStream(), (IpProxyToken)token, logger);
                        }

                        if (token.Status == NetStatusCodes.Success)
                        {
                            NetUtils.PopulateBagFromSocket(client.Client, properties);
                            ret = new TcpClientDataAdapter(client, IpProxyClient.GetDescription((IpProxyToken)token));
                        }
                    }
                }
                catch (SocketException ex)
                {
                    logger.LogException(ex);
                    token.Status = NetStatusCodes.ConnectFailure;
                }
                catch (IOException ex)
                {
                    logger.LogException(ex);
                    token.Status = NetStatusCodes.ConnectFailure;
                }
                finally
                {
                    if (ret == null)
                    {
                        client.Close();
                    }
                }
            }
            else
            {
                throw new ArgumentException(CANAPE.Net.Properties.Resources.HttpProxyClient_InvalidProxyToken);
            }

            return(ret);
        }
Пример #4
0
        /// <summary>
        /// Connect the client
        /// </summary>
        /// <param name="token">The proxy token</param>
        /// <param name="logger">The proxy logger instance</param>
        /// <param name="meta">The proxy meta data</param>
        /// <param name="globalMeta">The proxy global meta data</param>
        /// <param name="properties">The proxy property bag</param>
        /// <param name="credmgr">Credentials manager</param>
        /// <returns>The connected adapter</returns>
        public override IDataAdapter Connect(ProxyToken token, Logger logger, MetaDictionary meta, MetaDictionary globalMeta, PropertyBag properties, CredentialsManagerService credmgr)
        {
            IDataAdapter ret = null;

            for (int i = 0; i < _clients.Length; ++i)
            {
                ret = _clients[i].Connect(token, logger, meta, globalMeta, properties, credmgr);

                if (ret != null)
                {
                    break;
                }
            }

            if ((ret == null) && (token.Status == NetStatusCodes.Success))
            {
                ret = new IpProxyClient().Connect(token, logger, meta, globalMeta, properties, credmgr);
            }

            return ret;
        }
        /// <summary>
        /// Connection to the socks server
        /// </summary>
        /// <param name="token">The proxy token</param>
        /// <param name="logger">Logger</param>
        /// <param name="globalMeta">Global meta</param>
        /// <param name="meta">Meta</param>
        /// <param name="properties">Property bag to populate</param>
        /// <returns>A connected data adapter, or null if not available</returns>
        /// <exception cref="SocketException">Throw if cannot connect</exception>
        /// <exception cref="ArgumentException">Throw if invalid operation occurs</exception>
        /// <exception cref="EndOfStreamException">Throw if stream ends before reading all data</exception>
        public override IDataAdapter Connect(ProxyToken token, Logger logger, MetaDictionary meta, MetaDictionary globalMeta, PropertyBag properties)
        {
            IDataAdapter ret = null;

            if (token is IpProxyToken)
            {
                IpProxyToken iptoken = token as IpProxyToken;
                TcpClient    client  = new TcpClient();

                try
                {
                    client.ConnectAsync(_hostname, _port).Wait();

                    if (_version == SupportedVersion.Version4)
                    {
                        if (_sendHostName && !String.IsNullOrWhiteSpace(iptoken.Hostname))
                        {
                            ConnectVersion4a(client.GetStream(), iptoken, logger);
                        }
                        else
                        {
                            ConnectVersion4(client.GetStream(), iptoken, logger);
                        }
                    }
                    else
                    {
                        ConnectVersion5(client.GetStream(), iptoken, logger);
                    }

                    if (iptoken.Status == NetStatusCodes.Success)
                    {
                        NetUtils.PopulateBagFromSocket(client.Client, properties);

                        ret = new TcpClientDataAdapter(client, IpProxyClient.GetDescription((IpProxyToken)token));
                    }
                }
                catch (SocketException ex)
                {
                    logger.LogException(ex);
                    token.Status = NetStatusCodes.ConnectFailure;
                }
                catch (IOException ex)
                {
                    logger.LogException(ex);
                    token.Status = NetStatusCodes.ConnectFailure;
                }
                finally
                {
                    if (ret == null)
                    {
                        client.Dispose();
                    }
                }
            }
            else
            {
                throw new ArgumentException(CANAPE.Net.Properties.Resources.SocksProxyClient_InvalidProxyToken3);
            }

            return(ret);
        }
Пример #6
0
        /// <summary>
        /// Create the network service from this document
        /// </summary>
        /// <param name="logger">The logger for the service</param>
        /// <returns>The new network service</returns>
        public override ProxyNetworkService Create(Logger logger)
        {
            ProxyNetworkService ret = null;

            if (logger == null)
            {
                logger = Logger.GetSystemLogger();
            }

            if ((_port <= 0) || (_port > 65535))
            {
                throw new NetServiceException("Must provide a valid port");
            }
            else
            {
                try
                {
                    ProxyServer server = new FixedProxyServer(logger, _destination, _port, _udp ? IpProxyToken.IpClientType.Udp : IpProxyToken.IpClientType.Tcp,
                        _ipv6, _layers);

                    ProxyClient client = null;

                    if (_clientFactory != null)
                    {
                        client = _clientFactory.Create(logger);
                    }
                    else
                    {
                        client = new IpProxyClient();
                    }

                    ret = new ProxyNetworkService(
                        _packets,
                        CreateListener(logger), _netGraph == null ? BuildDefaultProxyFactory() : _netGraph.Factory, logger, _globalMeta, _history,
                                _credentials, server, client, null, Timeout.Infinite, false);

                    ret.DefaultBinding = NetworkLayerBinding.Client;
                }
                catch (SocketException ex)
                {
                    throw new NetServiceException("Error creating network service", ex);
                }
                catch (IOException ex)
                {
                    throw new NetServiceException("Error creating network service", ex);
                }

                return ret;
            }
        }
Пример #7
0
        /// <summary>
        /// Method to create a proxy client
        /// </summary>
        /// <param name="logger">The logger to use</param>
        /// <returns>The new proxy client</returns>
        public override ProxyClient Create(Utils.Logger logger)
        {
            ProxyClient ret = new IpProxyClient();

            try
            {
                RegistryKey settings = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings");

                int enabled = (int)settings.GetValue("ProxyEnable", 0);

                if (enabled != 0)
                {
                    string autoConfigUrl = settings.GetValue("AutoConfigURL") as string;

                    if (autoConfigUrl != null)
                    {
                        Uri autoConfigUri = new Uri(autoConfigUrl, UriKind.Absolute);

                        if ((_scriptFactory == null) || (!_scriptFactory.ScriptUri.Equals(autoConfigUri)))
                        {
                            using (WebClient client = new WebClient())
                            {
                                client.Proxy = null;
                                string scriptData = client.DownloadString(autoConfigUrl);

                                logger.LogVerbose("Received auto config script from {0}", autoConfigUrl);
                                logger.LogVerbose(scriptData);

                                _scriptFactory = new ScriptProxyClientFactory();
                                _scriptFactory.ScriptUri = autoConfigUri;
                                _scriptFactory.Script = scriptData;
                            }
                        }

                        ret = _scriptFactory.Create(logger);
                    }
                    else
                    {
                        string proxyServer = settings.GetValue("ProxyServer") as string;

                        if (proxyServer != null)
                        {
                            string[] servers = proxyServer.ToLower().Split(new[] { ' ', ';' }, StringSplitOptions.RemoveEmptyEntries);
                            string currServer = null;
                            bool socks = false;

                            // Take socks in preference, otherwise accept HTTP or default
                            foreach (string server in servers)
                            {
                                if (server.Contains('='))
                                {
                                    if (server.StartsWith("socks="))
                                    {
                                        currServer = server.Substring(6).Trim();
                                        logger.LogVerbose("Found system SOCKS server {0}", currServer);
                                        socks = true;
                                        break;
                                    }
                                    else if (server.StartsWith("http="))
                                    {
                                        currServer = server.Substring(5).Trim();
                                        logger.LogVerbose("Found system HTTP proxy {0}", currServer);
                                    }
                                }
                                else
                                {
                                    currServer = server.Trim();
                                    logger.LogVerbose("Found default HTTP proxy {0}", currServer);
                                }
                            }

                            if (currServer != null)
                            {
                                string host = null;
                                int port = 0;

                                if (currServer.Contains("/"))
                                {
                                    if (Uri.IsWellFormedUriString(currServer, UriKind.Absolute))
                                    {
                                        Uri uri = new Uri(currServer);

                                        host = uri.Host;
                                        port = uri.Port;
                                    }
                                }
                                else
                                {
                                    string[] values = currServer.Split(':');
                                    if (values.Length == 2)
                                    {
                                        host = values[0].Trim();
                                        int.TryParse(values[1].Trim(), out port);
                                    }
                                }

                                if (String.IsNullOrWhiteSpace(host) || (port <= 0) || (port > 65535))
                                {
                                    logger.LogError("Invalid system proxy string {0}", currServer);
                                }
                                else
                                {
                                    if (socks)
                                    {
                                        ret = new SocksProxyClient(host, port, false, SocksProxyClient.SupportedVersion.Version4, false);
                                    }
                                    else
                                    {
                                        ret = new HttpProxyClient(host, port, false);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (SecurityException)
            {
            }
            catch (UnauthorizedAccessException)
            {
            }
            catch (WebException ex)
            {
                logger.LogException(ex);
            }

            return ret;
        }
Пример #8
0
        /// <summary>
        /// Method to create a network service
        /// </summary>
        /// <param name="logger">The logger to use</param>
        /// <returns>The network service</returns>
        /// <exception cref="NetServiceException">Thrown in configuration invalid</exception>
        public override ProxyNetworkService Create(Logger logger)
        {
            ProxyNetworkService ret = null;

            if (logger == null)
            {
                logger = Logger.GetSystemLogger();
            }

            if ((_port < 0) || (_port > 65535))
            {
                throw new NetServiceException(Properties.Resources.GenericProxyDocument_MustSpecifyAValidPort);
            }
            else
            {
                try
                {
                    List<ProxyFilter> filters = new List<ProxyFilter>();

                    foreach (ProxyFilterFactory item in _filters)
                    {
                        filters.Add(item.CreateFilter());
                    }

                    ProxyServer server = CreateServer(logger);
                    ProxyClient client = null;

                    if (_clientFactory != null)
                    {
                        client = _clientFactory.Create(logger);
                    }
                    else
                    {
                        client = new IpProxyClient();
                    }

                    INetworkListener listener = null;

                    if (NetUtils.OSSupportsIPv4)
                    {
                        listener = new TcpNetworkListener(_anyBind, false, _port, logger, false);
                    }

                    if (_ipv6Bind && NetUtils.OSSupportsIPv6)
                    {
                        INetworkListener ipv6Listener = new TcpNetworkListener(_anyBind, true, _port, logger, false);

                        if (listener != null)
                        {
                            listener = new AggregateNetworkListener(listener, ipv6Listener);
                        }
                        else
                        {
                            listener = ipv6Listener;
                        }
                    }

                    if (listener == null)
                    {
                        throw new NetServiceException(CANAPE.Documents.Properties.Resources.NetServiceDocument_CannotSetupListener);
                    }

                    ret = new ProxyNetworkService(
                        _packets,
                        listener,
                        _netGraph == null ? BuildDefaultProxyFactory() : _netGraph.Factory, logger, _globalMeta, _history,
                        _credentials, server, client, filters.ToArray(), Timeout.Infinite, false);

                    ret.DefaultBinding = CANAPE.Net.Layers.NetworkLayerBinding.ClientAndServer;
                }
                catch (SocketException ex)
                {
                    throw new NetServiceException(Properties.Resources.GenericProxyDocument_ErrorCreatingService, ex);
                }
                catch (IOException ex)
                {
                    throw new NetServiceException(Properties.Resources.GenericProxyDocument_ErrorCreatingService, ex);
                }

                return ret;
            }
        }
Пример #9
0
        /// <summary>
        /// Method to create the network service
        /// </summary>
        /// <param name="logger">The logger to use</param>
        /// <returns>The network service</returns>
        /// <exception cref="NetServiceException">Thrown if invalid configuration</exception>
        public override ProxyNetworkService Create(Logger logger)
        {
            ProxyNetworkService ret = null;

            if (logger == null)
            {
                logger = Logger.GetSystemLogger();
            }

            if ((_port <= 0) || (_port > 65535))
            {
                throw new NetServiceException(Properties.Resources.FixedProxyDocument_MustProvideValidPort);
            }
            else if ((_localPort < 0) || (_localPort > 65535))
            {
                throw new NetServiceException(Properties.Resources.FixedProxyDocument_MustProvideValidLocalPort);
            }
            else
            {
                try
                {
                    ProxyServer server = new FixedProxyServer(logger, _host, _port,
                        _udp ? IpProxyToken.IpClientType.Udp : IpProxyToken.IpClientType.Tcp,
                        _ipv6, _layers);

                    ProxyClient client = null;

                    if (_clientFactory != null)
                    {
                        client = _clientFactory.Create(logger);
                    }
                    else
                    {
                        client = new IpProxyClient();
                    }

                    INetworkListener listener = null;

                    if (!_udp)
                    {
                        if (NetUtils.OSSupportsIPv4)
                        {
                            listener = new TcpNetworkListener(_anyBind, false, _localPort, logger, false);
                        }

                        if (_ipv6Bind && NetUtils.OSSupportsIPv6)
                        {
                            INetworkListener ipv6Listener = new TcpNetworkListener(_anyBind, true, _localPort, logger, false);

                            if (listener != null)
                            {
                                listener = new AggregateNetworkListener(listener, ipv6Listener);
                            }
                            else
                            {
                                listener = ipv6Listener;
                            }
                        }
                    }
                    else
                    {
                        if (NetUtils.OSSupportsIPv4)
                        {
                            listener = new UdpNetworkListener(_anyBind, false, _localPort, _enableBroadcast, logger);
                        }

                        if (_ipv6Bind && NetUtils.OSSupportsIPv6)
                        {
                            INetworkListener ipv6Listener = new UdpNetworkListener(_anyBind, true, _localPort, _enableBroadcast, logger);

                            if(listener != null)
                            {
                                listener = new AggregateNetworkListener(listener, ipv6Listener);
                            }
                            else
                            {
                                listener = ipv6Listener;
                            }
                        }
                    }

                    if (listener == null)
                    {
                        throw new NetServiceException(CANAPE.Documents.Properties.Resources.NetServiceDocument_CannotSetupListener);
                    }

                    ret = new ProxyNetworkService(_packets,
                        listener,
                        _netGraph == null ? BuildDefaultProxyFactory() : _netGraph.Factory, logger, _globalMeta, _history,
                        _credentials, server, client, null, Timeout.Infinite, false);

                    ret.DefaultBinding = NetworkLayerBinding.ClientAndServer;
                }
                catch (SocketException ex)
                {
                    throw new NetServiceException(Properties.Resources.FixedProxyDocument_ErrorCreatingService, ex);
                }
                catch (IOException ex)
                {
                    throw new NetServiceException(Properties.Resources.FixedProxyDocument_ErrorCreatingService, ex);
                }

                return ret;
            }
        }