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; }
private IDataAdapter BindTcp(IpProxyToken token, Logger logger, PropertyBag properties) { TcpListenerDataAdapter adapter = null; try { bool isIpv6 = IsTokenIpV6(token); adapter = new TcpListenerDataAdapter(new IPEndPoint(isIpv6 ? IPAddress.IPv6Any : IPAddress.Any, token.Port)); NetUtils.PopulateBagFromSocket(adapter.Listener.Server, properties); } catch (SocketException ex) { logger.LogException(ex); token.Status = NetStatusCodes.ConnectFailure; } catch (IOException ex) { logger.LogException(ex); token.Status = NetStatusCodes.ConnectFailure; } return adapter; }
private static IEnumerable<DataFrame> ReadFrames(IDataAdapter client, DynamicScriptContainer container, Logger logger, object config) { DataReader reader = new DataReader(new DataAdapterToStream(client)); while (!reader.Eof) { DynamicStreamDataKey2 key = new DynamicStreamDataKey2("Root", container, logger, config); try { key.FromReader(reader); } catch (ThreadAbortException) { throw; } catch (EndOfStreamException) { // End of stream, do nothing } catch (Exception e) { logger.LogException(e); yield break; } // Only fill in the frame if we read something, should this exit if it continues to read nothing? if (reader.ByteCount > 0) { yield return new DataFrame(key); } } }
/// <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="credmgr">Credentials manager</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, CredentialsManagerService credmgr) { IDataAdapter ret = null; if (token is IpProxyToken) { IpProxyToken iptoken = token as IpProxyToken; TcpClient client = new TcpClient(); try { client.Connect(_hostname, _port); 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.Close(); } } } else { throw new ArgumentException(CANAPE.Net.Properties.Resources.SocksProxyClient_InvalidProxyToken3); } return ret; }
/// <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; }