Пример #1
0
        private async void RemoveButton_Click(object sender, RoutedEventArgs e)
        {
            var btn    = (MenuFlyoutItem)sender;
            var config = (IAdapterConfig)btn.DataContext;

            var(secondaryAsClose, result) = await Utils.NotifyUser("Remove this config?", primaryCommandText : "Yes");

            if (result != ContentDialogResult.Primary)
            {
                return;
            }
            try
            {
                File.Delete(config.Path);
                adapterConfigs.Remove(config);
                if (config.Path == AdapterConfig.GetDefaultConfigFilePath() && adapterConfigs.Count > 0)
                {
                    AdapterConfig.SetDefaultConfigFilePath(adapterConfigs[0].Path);
                }
            }
            catch (Exception ex)
            {
                await Utils.NotifyUser("Error while deleting config file: " + ex.Message);
            }
        }
 public object Convert(object configPath, Type targetType, object parameter, string language)
 {
     if (AdapterConfig.GetDefaultConfigFilePath() == configPath as string)
     {
         return(FontWeights.Bold);
     }
     return(FontWeights.Normal);
 }
Пример #3
0
        private async void RemoveButton_Click(object sender, RoutedEventArgs e)
        {
            var btn = (MenuFlyoutItem)sender;

            IList <object>      configs;
            ContentDialogResult result;

            if (configList.SelectedItems.Count == 0)
            {
                var config = (IAdapterConfig)((FrameworkElement)sender).DataContext;
                configs = new List <object>()
                {
                    config
                };
                (_, result) = await UiUtils.NotifyUser("Remove this config?", primaryCommandText : "Yes");
            }
            else
            {
                configs     = configList.SelectedItems;
                (_, result) = await UiUtils.NotifyUser($"Remove {configList.SelectedItems.Count} configs?", primaryCommandText : "Yes");
            }
            if (result != ContentDialogResult.Primary)
            {
                return;
            }
            var deletedConfigs = new List <IAdapterConfig>(configs.Count);

            loadProgressBar.IsIndeterminate = true;
            loadProgressBar.Value           = 0;
            loadProgressBar.Visibility      = Visibility.Visible;
            try
            {
                var defaultConfigPath = AdapterConfig.GetDefaultConfigFilePath();
                var tasks             = configs.Select(async obj =>
                {
                    var config = (IAdapterConfig)obj;
                    var file   = await StorageFile.GetFileFromPathAsync(config.Path);
                    await file.DeleteAsync();
                    deletedConfigs.Add(config);
                    if (config.Path == defaultConfigPath)
                    {
                        AdapterConfig.ClearDefaultConfigFilePath();
                    }
                    loadProgressBar.IsIndeterminate = false;
                    loadProgressBar.Value           = (double)deletedConfigs.Count / configs.Count;
                });
                await Task.WhenAll(tasks);
            }
            catch (Exception ex)
            {
                await UiUtils.NotifyUser("Error while deleting config file: " + ex.ToString());
            }
            finally
            {
                if (deletedConfigs.Count == adapterConfigs.Count)
                {
                    adapterConfigs.Clear();
                }
                else
                {
                    foreach (var deleted in deletedConfigs)
                    {
                        adapterConfigs.Remove(deleted);
                    }
                }
                loadProgressBar.IsIndeterminate = true;
                loadProgressBar.Visibility      = Visibility.Collapsed;
            }
        }
Пример #4
0
        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;
            }
        }
Пример #5
0
        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;
            }
        }