コード例 #1
0
        public void Ok(object sender, RoutedEventArgs args)
        {
            error.IsVisible = false;
            DisableControls();

            Uri.TryCreate(url.Text, UriKind.Absolute, out Uri? uri);
            if (uri == null || uri.Scheme != Uri.UriSchemeHttp && uri.Scheme != Uri.UriSchemeHttps)
            {
                ShowError("The URL is invalid");
                EnableControls();
                return;
            }

            string filename = Utils.RemoveYamlExt(name.Text ?? "");

            if (filename.Length == 0)
            {
                ShowError("Please specify a profile name.");
                EnableControls();
                return;
            }

            if (filename.IndexOfAny(Path.GetInvalidFileNameChars()) > 0)
            {
                ShowError("The name contains invalid characters.");
                EnableControls();
                return;
            }

            if (ConfigFile.GetClashConfigs().Select(Utils.RemoveYamlExt).Contains(filename))
            {
                ShowError("The profile name has existed. Please specify another name.");
                EnableControls();
                return;
            }

            Task.Run(async delegate {
                try {
                    byte[] data = await Utils.GetProxiedHttpClient().GetByteArrayAsync(uri);

                    string?err = Clash.ValidateConfig(data);
                    if (err != null)
                    {
                        throw new Exception(err);
                    }

                    Dispatcher.UIThread.Post(() => {
                        Close(new Result($"{filename}.yaml", uri.ToString(), data));
                    });
                } catch (Exception e) {
                    Dispatcher.UIThread.Post(() => {
                        ShowError(e.Message);
                        EnableControls();
                    });
                }
            });
        }