public void Connect(VpnChannel channel) { List <HostName> hosts = new List <HostName>(); hosts.Add(new HostName("216.58.208.46")); StreamSocket streamsock = new StreamSocket(); VpnRouteAssignment vpr = new VpnRouteAssignment(); vpr.ExcludeLocalSubnets = true; VpnNamespaceAssignment na = new VpnNamespaceAssignment(); VpnInterfaceId id = new VpnInterfaceId(Encoding.ASCII.GetBytes("1")); channel.AssociateTransport(streamsock, null); //channel.TerminateConnection("FUCKU"); try { channel.Start(null, null, id, vpr, na, 512, 512, true, streamsock, null); VpnPacketBuffer buf = channel.GetVpnReceivePacketBuffer(); } catch (Exception e) { return; } }
public void Connect(VpnChannel channel) { try { var b = goTestFunction(1); //var vpnCustomPromptTextInput = new VpnCustomPromptTextInput() { DisplayName = "Give me some input" }; ////this call is NOT asynchronous. awaiting the result will halt the program //channel.RequestCustomPromptAsync(new IVpnCustomPromptElement[] //{ // new VpnCustomPromptText() {DisplayName = "This is a test", Text = "Message"}, // vpnCustomPromptTextInput //}); //var inputWas = vpnCustomPromptTextInput.Text; var transport = new DatagramSocket(); transport.MessageReceived += Transport_MessageReceived; //var streamSocket = new StreamSocket(); channel.AssociateTransport(transport, null); //channel.LogDiagnosticMessage("this is from the wireguard plugin");// supposedly under Event Viewer, under Application and Services Logs\Microsoft\Windows\Vpn Plugin Platform. but can't find //transport.BindServiceNameAsync("50000").AsTask().Wait(); //transport.BindEndpointAsync(new HostName("192.168.1.100"), "50000").AsTask().Wait(); transport.ConnectAsync(new HostName(channel.Configuration.ServerUris[0].Scheme), channel.Configuration.ServerUris[0].LocalPath).AsTask().Wait(); var vpnRouteAssignment = new VpnRouteAssignment { ExcludeLocalSubnets = false }; vpnRouteAssignment.Ipv4InclusionRoutes.Add(new VpnRoute(new HostName("10.189.129.1"), 32)); channel.StartExistingTransports( new[] { new HostName("10.189.129.2"), //this is our network interface address }, null, null, vpnRouteAssignment, null, VPN_MTU, VPN_MAX_FRAME, false ); } catch (Exception e) { Console.WriteLine(e); } }
internal void ConfigureAndConnect(VpnChannel vpnChannel, string parameters) { parameters = parameters.TrimEnd(); uint mtuSize = 68; var assignedClientIPv4list = new List <HostName>(); var dnsServerList = new List <HostName>(); VpnRouteAssignment assignedRoutes = new VpnRouteAssignment(); VpnDomainNameAssignment assignedDomainName = new VpnDomainNameAssignment(); var ipv4InclusionRoutes = assignedRoutes.Ipv4InclusionRoutes; foreach (var parameter in parameters.Split(null)) { var fields = parameter.Split(","); switch (fields[0]) { case "m": mtuSize = uint.Parse(fields[1]); break; case "a": assignedClientIPv4list.Add(new HostName(fields[1])); break; case "r": ipv4InclusionRoutes.Add(new VpnRoute(new HostName(fields[1]), (byte)(int.Parse(fields[2])))); break; case "d": dnsServerList.Add(new HostName(fields[1])); break; default: break; } } assignedRoutes.Ipv4InclusionRoutes = ipv4InclusionRoutes; assignedDomainName.DomainNameList.Add(new VpnDomainNameInfo(".", VpnDomainNameType.Suffix, dnsServerList, null)); try { vpnChannel.StartExistingTransports(assignedClientIPv4list, null, null, assignedRoutes, assignedDomainName, mtuSize, ushort.MaxValue, false); } catch (Exception e) { vpnChannel.TerminateConnection(e.Message); } }
private string user = "******"; // expected username for the VPN credentials public void Connect(VpnChannel channel) { Debug.WriteLine("VPNDEMO: Entering plugin Connect"); try { var prompt = new VpnCustomPromptTextInput { Compulsory = true, DisplayName = "Username", Emphasized = false, IsTextHidden = false, }; var promptList = new[] { prompt }; // Prompt user for credential info _ = channel.RequestCustomPromptAsync(promptList); if (prompt.Text.Equals(user)) { Debug.WriteLine("VPNDEMO: user verified with success"); socket?.Dispose(); socket = new StreamSocket(); channel.AssociateTransport(this.socket, null); Debug.WriteLine("VPNDEMO: calling ConnectAsync"); this.socket.ConnectAsync(new HostName("www.microsoft.com"), "443", SocketProtectionLevel.Tls12).AsTask().GetAwaiter().GetResult(); Debug.WriteLine("VPNDEMO: ConnectAsync returned"); var myRoute = new VpnRouteAssignment(); myRoute.Ipv4InclusionRoutes.Add(new VpnRoute(new HostName("192.168.80.0"), 24)); var myAddress = new List <HostName> { new HostName("192.168.80.100") }; Debug.WriteLine("VPNDEMO: Calling channel.StartExistingTransports()"); channel.StartExistingTransports(myAddress, null, null, myRoute, new VpnDomainNameAssignment(), 1472, 1500, false); Debug.WriteLine("VPNDEMO: Connected"); } else { Debug.WriteLine("VPNDEMO: user verification failed"); channel.TerminateConnection("Failed credentials"); } } catch (Exception e) { Debug.WriteLine("VPNDEMO: EXCEPTION in plugin connect: " + e.ToString()); //throw; } }
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; } }