public void Connect(VpnChannel channel) { State = VpnPluginState.Connecting; LogLine("Connecting", channel); try { DatagramSocket transport = new DatagramSocket(); channel.AssociateTransport(transport, null); VpnContext context = null; if (channel.PlugInContext == null) { // create and Initialize context LogLine("Initializing new context", channel); channel.PlugInContext = context = new VpnContext(); context.InitTun2Socks(TUN_SERVICE_NAME, VPN_ADDR, VPN_NETMASK, (int)VPN_MTU, "192.168.1.107:60000", "aes-256-cfb", "SSTest"); context.Init(); } else { LogLine("Context exists", channel); context = (VpnContext)channel.PlugInContext; } transport.ConnectAsync(new HostName("127.0.0.1"), TUN_SERVICE_NAME).AsTask().ContinueWith(t => { LogLine("r Connected", channel); }).Wait(); var now = DateTime.Now; LogLine("Starting transport", channel); channel.StartWithMainTransport( new[] { VPN_HOST }, null, null, context.routeScope, null, VPN_MTU, VPN_MAX_FRAME, false, transport ); var delta = DateTime.Now - now; LogLine($"Finished starting transport in {delta.TotalMilliseconds} ms.", channel); LogLine("Connected", channel); State = VpnPluginState.Connected; } catch (Exception ex) { LogLine("Error connecting", channel); LogLine(ex.Message, channel); LogLine(ex.StackTrace, channel); State = VpnPluginState.Disconnected; } }
public void Disconnect(VpnChannel channel) { State = VpnPluginState.Disconnecting; if (channel.PlugInContext == null) { LogLine("Disconnecting with null context", channel); State = VpnPluginState.Disconnected; return; } else { LogLine("Disconnecting with non-null context", channel); } var context = (DebugVpnContext)channel.PlugInContext; context.Stop(); LogLine("channel stopped", channel); //channel.PlugInContext = null; State = VpnPluginState.Disconnected; }
public void Disconnect(VpnChannel channel) { try { State = VpnPluginState.Disconnecting; DebugLogger.Log("Stopping channel"); channel.Stop(); DebugLogger.Log("Stopping context"); context.Stop(); DebugLogger.Log("Context stopped"); } catch (Exception ex) { DebugLogger.Log("Error disconnecting: " + ex.ToString()); } finally { State = VpnPluginState.Disconnected; var _ = DebugLogger.ResetLoggers(); DebugLogger.initNeeded = null; } }
public void Connect(VpnChannel channel) { State = VpnPluginState.Connecting; LogLine("Connecting", channel); try { var transport = new DatagramSocket(); channel.AssociateTransport(transport, null); VpnContext context = null; if (channel.PlugInContext == null) { LogLine("Initializing new context", channel); channel.PlugInContext = context = new VpnContext(); } else { LogLine("Context exists", channel); context = (VpnContext)channel.PlugInContext; } transport.BindEndpointAsync(new HostName("127.0.0.1"), "9007").AsTask().ContinueWith(t => { LogLine("Binded", channel); }).Wait(); #if !DEBUG context.Init("9008"); #endif /* var rport = context.Init(transport.Information.LocalPort, str => * { * LogLine(str, channel); * return null; * }); */ var rport = "9008"; transport.ConnectAsync(new HostName("127.0.0.1"), rport).AsTask().ContinueWith(t => { LogLine("r Connected", channel); }); VpnRouteAssignment routeScope = new VpnRouteAssignment() { ExcludeLocalSubnets = true }; var inclusionRoutes = routeScope.Ipv4InclusionRoutes; // myip.ipip.net //inclusionRoutes.Add(new VpnRoute(new HostName("36.99.18.134"), 32)); // qzworld.net //inclusionRoutes.Add(new VpnRoute(new HostName("188.166.248.242"), 32)); // DNS server inclusionRoutes.Add(new VpnRoute(new HostName("1.1.1.1"), 32)); // main CIDR inclusionRoutes.Add(new VpnRoute(new HostName("172.17.0.0"), 16)); var assignment = new VpnDomainNameAssignment(); var dnsServers = new[] { // DNS servers // new HostName("192.168.1.1"), new HostName("1.1.1.1") }; assignment.DomainNameList.Add(new VpnDomainNameInfo(".", VpnDomainNameType.Suffix, dnsServers, new HostName[] { })); var now = DateTime.Now; LogLine("Starting transport", channel); channel.StartWithMainTransport( new[] { new HostName("192.168.3.1") }, null, null, routeScope, assignment, 1500u, 1512u, false, transport ); var delta = DateTime.Now - now; LogLine($"Finished starting transport in {delta.TotalMilliseconds} ms.", channel); LogLine("Connected", channel); State = VpnPluginState.Connected; } catch (Exception ex) { LogLine("Error connecting", channel); LogLine(ex.Message, channel); LogLine(ex.StackTrace, channel); State = VpnPluginState.Disconnected; } def?.Complete(); }
public void Connect(VpnChannel channel) { State = VpnPluginState.Connecting; LogLine("Connecting", channel); try { var transport = new DatagramSocket(); channel.AssociateTransport(transport, null); DebugVpnContext context = null; if (channel.PlugInContext == null) { LogLine("Initializing new context", channel); #if YT_MOCK channel.PlugInContext = context = new DebugVpnContext(); #else var configPath = AdapterConfig.GetDefaultConfigFilePath(); if (string.IsNullOrEmpty(configPath)) { channel.TerminateConnection("Config not set"); return; } try { var config = AdapterConfig.GetConfigFromFilePath(configPath); if (config == null) { throw new Exception("Cannot read config file."); } } catch (Exception ex) { channel.TerminateConnection("Error reading config file:" + ex.Message); } channel.PlugInContext = context = new DebugVpnContext("9008"); #endif } else { LogLine("Context exists", channel); context = (DebugVpnContext)channel.PlugInContext; } transport.BindEndpointAsync(new HostName("127.0.0.1"), "9007").AsTask().ContinueWith(t => { LogLine("Binded", channel); }).Wait(); #if !YT_MOCK context.Init(); #endif /* var rport = context.Init(transport.Information.LocalPort, str => * { * LogLine(str, channel); * return null; * }); */ var rport = "9008"; transport.ConnectAsync(new HostName("127.0.0.1"), rport).AsTask().ContinueWith(t => { LogLine("r Connected", channel); }); VpnRouteAssignment routeScope = new VpnRouteAssignment() { ExcludeLocalSubnets = true }; var inclusionRoutes = routeScope.Ipv4InclusionRoutes; // myip.ipip.net //inclusionRoutes.Add(new VpnRoute(new HostName("36.99.18.134"), 32)); // qzworld.net //inclusionRoutes.Add(new VpnRoute(new HostName("188.166.248.242"), 32)); // DNS server inclusionRoutes.Add(new VpnRoute(new HostName("1.1.1.1"), 32)); // main CIDR inclusionRoutes.Add(new VpnRoute(new HostName("172.17.0.0"), 16)); var assignment = new VpnDomainNameAssignment(); var dnsServers = new[] { // DNS servers new HostName("1.1.1.1"), }; assignment.DomainNameList.Add(new VpnDomainNameInfo(".", VpnDomainNameType.Suffix, dnsServers, new HostName[] { })); var now = DateTime.Now; LogLine("Starting transport", channel); channel.StartWithMainTransport( new[] { new HostName("192.168.3.1") }, null, null, routeScope, assignment, 1500u, 1512u, false, transport ); var delta = DateTime.Now - now; LogLine($"Finished starting transport in {delta.TotalMilliseconds} ms.", channel); LogLine("Connected", channel); State = VpnPluginState.Connected; } catch (Exception ex) { LogLine("Error connecting", channel); LogLine(ex.Message, channel); LogLine(ex.StackTrace, channel); channel.TerminateConnection("Cannot connect to local tunnel"); State = VpnPluginState.Disconnected; } }
public void Connect(VpnChannel channel) { State = VpnPluginState.Connecting; DebugLogger.Logger = s => { channel.LogDiagnosticMessage(s); }; DebugLogger.Log("Starting connection to VPN tunnel"); try { var transport = new DatagramSocket(); channel.AssociateTransport(transport, null); DebugLogger.Log("Initializing context"); #if !YT_MOCK var configPath = AdapterConfig.GetDefaultConfigFilePath(); if (string.IsNullOrEmpty(configPath)) { channel.TerminateConnection("Default server configuration not set. Please launch YtFlow app and select a server configuration as default."); return; } try { var config = AdapterConfig.GetConfigFromFilePath(configPath); if (config == null) { channel.TerminateConnection("Could not read server configuration."); return; } } catch (Exception ex) { channel.TerminateConnection("Error reading server configuration: " + ex.ToString()); return; } #endif DebugLogger.Log("Config read, binding endpoint"); if (!transport.BindEndpointAsync(new HostName("127.0.0.1"), string.Empty).AsTask().ContinueWith(t => { if (t.IsFaulted || t.IsCanceled) { DebugLogger.Log("Error binding endpoint: " + t.Exception.ToString()); return(false); } else { DebugLogger.Log("Binded"); return(true); } }).Result) { return; } DebugLogger.Log("Endpoint binded, init context"); var rport = context.Init(int.Parse(transport.Information.LocalPort)).ToString(); DebugLogger.Log("Context initialized"); /* var rport = context.Init(transport.Information.LocalPort, str => * { * LogLine(str, channel); * return null; * }); */ DebugLogger.Log("Connecting to local packet processor"); if (!transport.ConnectAsync(new HostName("127.0.0.1"), rport).AsTask().ContinueWith(t => { if (t.IsFaulted || t.IsCanceled) { channel.TerminateConnection("Error connecting to local packet processor: " + t.Exception.ToString()); DebugLogger.Log("Local packet processor connected"); return(false); } else { return(true); } }).Result) { return; } DebugLogger.Log("Connected to local packet processor"); VpnRouteAssignment routeScope = new VpnRouteAssignment() { ExcludeLocalSubnets = true }; var inclusionRoutes = routeScope.Ipv4InclusionRoutes; // DNS server inclusionRoutes.Add(new VpnRoute(new HostName("1.1.1.1"), 32)); // main CIDR inclusionRoutes.Add(new VpnRoute(new HostName("11.17.0.0"), 16)); // proxy inclusionRoutes.Add(new VpnRoute(new HostName("172.17.255.0"), 24)); var assignment = new VpnDomainNameAssignment(); assignment.DomainNameList.Add(new VpnDomainNameInfo( ".", VpnDomainNameType.Suffix, new[] { new HostName("1.1.1.1") }, Array.Empty <HostName>())); var now = DateTime.Now; DebugLogger.Log("Starting transport"); channel.StartWithMainTransport( new[] { new HostName("192.168.3.1") }, null, null, routeScope, assignment, 1500u, 1512u, false, transport ); var delta = DateTime.Now - now; _ = context.u.SendAsync(new byte[] { 0 }, 1, context.pluginEndpoint); DebugLogger.Log($"Transport started in {delta.TotalMilliseconds} ms."); State = VpnPluginState.Connected; } catch (Exception ex) { var msg = "Error connecting to VPN tunnel: " + ex.ToString(); channel.TerminateConnection(msg); DebugLogger.Log(msg); State = VpnPluginState.Disconnected; } }