Esempio n. 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);
        }
Esempio n. 2
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);
        }
        /// <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);
        }