/// <summary> /// Connect this instance. /// </summary> /// <param name="logger">Logger object</param> /// <returns>The connected data adapter</returns> public IDataAdapter Connect(Logger logger) { if (logger == null) { throw new ArgumentNullException(nameof(logger)); } if ((Port <= 0) || (Port > 65535)) { throw new NetServiceException(Properties.Resources.FixedProxyDocument_MustProvideValidPort); } try { ProxyClient client = Client != null?Client.Create(logger) : new IpProxyClient(); IpProxyToken token = new IpProxyToken(null, Host, Port, UdpEnable ? IpProxyToken.IpClientType.Udp : IpProxyToken.IpClientType.Tcp, IPv6); IDataAdapter adapter = client.Connect(token, logger, new MetaDictionary(), new MetaDictionary(), new PropertyBag()); if (_layers.Count > 0) { MemoryStream initial_stm = new MemoryStream(InitialData ?? new byte[0]); StreamDataAdapter initial = new StreamDataAdapter(initial_stm); IDataAdapter client_adapter = initial; foreach (INetworkLayer layer in _layers.Select(f => f.CreateLayer(logger))) { layer.Negotiate(ref client_adapter, ref adapter, token, logger, new MetaDictionary(), new MetaDictionary(), new PropertyBag(), NetworkLayerBinding.Client); } } return(adapter); } catch (SocketException ex) { throw new NetServiceException(Properties.Resources.FixedProxyDocument_ErrorCreatingService, ex); } catch (IOException ex) { throw new NetServiceException(Properties.Resources.FixedProxyDocument_ErrorCreatingService, ex); } }
private void CloneCertChain(Uri url, string destination) { IProxyClientFactory factory = proxyClientControl.Client; if (factory == null) { factory = new IpProxyClientFactory(); } ProxyClient client = factory.Create(new Logger()); collection = new X509Certificate2Collection(); using (IDataAdapter adapter = client.Connect(new IpProxyToken(null, url.Host, url.Port, IpProxyToken.IpClientType.Tcp, false), new Logger(), new Nodes.MetaDictionary(), new Nodes.MetaDictionary(), new PropertyBag(), new Security.CredentialsManagerService())) { DataAdapterToStream stm = new DataAdapterToStream(adapter); using (SslStream ssl = new SslStream(stm, false, VerifyCallback)) { ssl.AuthenticateAsClient(url.Host); } } if (collection.Count > 0) { File.WriteAllBytes(Path.Combine(destination, String.Format("certchain_{0}.pfx", url.Host)), collection.Export(X509ContentType.Pfx)); int count = 1; foreach (X509Certificate2 cert in collection) { string path = Path.Combine(destination, String.Format("cert_{0}_{1}.cer", url.Host, count++)); File.WriteAllText(path, CertificateUtils.ExportToPEM(cert) + CertificateUtils.ExportToPEM((RSA)cert.PrivateKey, null)); } } }
// TODO: Should merge with implementation for the general connection so that it is 100% compatible /// <summary> /// /// </summary> /// <param name="graph"></param> /// <param name="token"></param> public void ReconnectClient(NetGraph graph, ProxyToken token) { IDataAdapter client = null; bool connected = false; PropertyBag networkService = graph.ConnectionProperties.GetRelativeBag("NetworkService"); PropertyBag clientProperties = graph.ConnectionProperties.GetRelativeBag("Client"); PropertyBag tokenProperties = graph.ConnectionProperties.GetRelativeBag("Token"); try { while (graph.Parent != null) { graph = graph.Parent; } if (token != null) { // If passed in a token we need to apply filters to it token = FilterToken(token); } else { // Use original post-filtered token = (ProxyToken)networkService.GetRelativeValue("Token"); } if (token.Status == NetStatusCodes.Success) { clientProperties.Clear(); if (token.Client == null) { client = _proxyClient.Connect(token, _logger, graph.Meta, _globalMeta, clientProperties); } else { client = token.Client.Connect(token, _logger, graph.Meta, _globalMeta, clientProperties); } tokenProperties.Clear(); token.PopulateBag(tokenProperties); // Negotiate SSL or other bespoke encryption mechanisms if (token.Layers != null) { // Bind but disabling server layer NetworkLayerBinding binding = DefaultBinding & ~NetworkLayerBinding.Server; foreach (INetworkLayer layer in token.Layers) { IDataAdapter server = null; layer.Negotiate(ref server, ref client, token, _logger, graph.Meta, _globalMeta, graph.ConnectionProperties, binding); } } graph.BindEndpoint((Guid)networkService.GetRelativeValue("ClientId"), client); IDataAdapter serverAdapter = networkService.GetRelativeValue("ServerAdapter"); if (token.NetworkDescription != null) { graph.NetworkDescription = token.NetworkDescription; } else { graph.NetworkDescription = String.Format("{0} <=> {1}", serverAdapter.Description, client.Description); } IDataAdapter oldClient = networkService.GetRelativeValue("ClientAdapter"); networkService.AddValue("ClientAdapter", client); networkService.AddValue("Token", token); oldClient.Dispose(); connected = true; } else { _logger.LogVerbose(Properties.Resources.ProxyNetworkService_ConnectionFiltered); } } catch (Exception ex) { _logger.LogException(ex); } finally { if (!connected) { try { if (client != null) { client.Dispose(); } } catch (Exception ex) { Logger.SystemLogger.LogException(ex); } } } }
/// <summary> /// Connect client /// </summary> /// <param name="baseAdapter"></param> /// <param name="connProperties"></param> /// <returns></returns> public NetGraph ConnectClient(IDataAdapter baseAdapter, PropertyBag connProperties) { IDataAdapter server = null; IDataAdapter client = null; ProxyToken token = null; NetGraph graph = null; NetGraph retGraph = null; MetaDictionary meta = new MetaDictionary(); PropertyBag properties = new PropertyBag("Properties"); try { properties.AddBag(connProperties); token = _proxyServer.Accept(baseAdapter, meta, _globalMeta, this); if (token != null) { token = FilterToken(token); if (token.Status == NetStatusCodes.Success) { ProxyClient proxyClient = token.Client ?? _proxyClient; if (token.Bind) { client = proxyClient.Bind(token, _logger, meta, _globalMeta, properties.AddBag("Client")); } else { client = proxyClient.Connect(token, _logger, meta, _globalMeta, properties.AddBag("Client")); } server = _proxyServer.Complete(token, meta, _globalMeta, this, client); if ((token.Status == NetStatusCodes.Success) && (client != null)) { NetGraphFactory factory = token.Graph != null ? token.Graph : _factory; token.PopulateBag(properties.AddBag("Token")); // Negotiate SSL or other bespoke encryption mechanisms if (token.Layers != null) { foreach (INetworkLayer layer in token.Layers) { layer.Negotiate(ref server, ref client, token, _logger, meta, _globalMeta, properties, DefaultBinding); } } var clients = factory.GetNodes <ClientEndpointFactory>(); var servers = factory.GetNodes <ServerEndpointFactory>(); if ((clients.Length > 0) && (servers.Length > 0)) { graph = CreateNetGraph(factory, meta, properties); graph.BindEndpoint(clients[0].Id, client); graph.BindEndpoint(servers[0].Id, server); if (token.NetworkDescription != null) { graph.NetworkDescription = token.NetworkDescription; } else { graph.NetworkDescription = String.Format("{0} <=> {1}", server.Description, client.Description); } PropertyBag networkService = properties.AddBag("NetworkService"); networkService.AddValue("ClientId", clients[0].Id); networkService.AddValue("ServerId", servers[0].Id); networkService.AddValue("ClientAdapter", client); networkService.AddValue("ServerAdapter", server); networkService.AddValue("Token", token); graph.Start(); OnNewConnection(graph); retGraph = graph; } else { _logger.LogError(CANAPE.Net.Properties.Resources.ProxyNetworkService_InvalidGraph); } } } else { _logger.LogVerbose(CANAPE.Net.Properties.Resources.ProxyNetworkService_ConnectionFiltered); server = _proxyServer.Complete(token, meta, _globalMeta, this, client); } } } catch (Exception ex) { _logger.LogException(ex); } finally { if (retGraph == null) { try { if (graph != null) { ((IDisposable)graph).Dispose(); } if (server != null) { server.Dispose(); } if (client != null) { client.Dispose(); } if (token != null) { token.Dispose(); } } catch (Exception ex) { Logger.SystemLogger.LogException(ex); } } } return(retGraph); }