コード例 #1
0
ファイル: Globals.cs プロジェクト: Rain0Ash/Derpi-Downloader
        static Globals()
        {
            Config = Config.Factory(Assembly.GetCallingAssembly().GetName().Name, false);

            CurrentDownloadFolder = Config.GetProperty(CurrentDownloadFolderKey, DefaultDownloadFolder,
                                                       path => PathUtils.IsValidPath(path, PathType.All), CryptAction.Crypt, SettingsSection);

            CurrentDownloadFileName = Config.GetProperty(CurrentDownloadFileNameKey, DefaultDownloadFileName,
                                                         path => PathUtils.IsValidPath(path, PathType.LocalFile), CryptAction.Crypt, SettingsSection);

            LanguageCode = Config.GetProperty(LanguageCodeKey, LocalizationBase.BasicCulture.LCID, SettingsSection);

            ExistFileRewrite = Config.GetProperty(ExistFileRewriteKey, false, OptionsSection);

            ConvertSVGToPNG = Config.GetProperty(ConvertSVGToPNGKey, true, OptionsSection);

            NotStrictAPICheck = Config.GetProperty(NotStrictAPICheckKey, false, OptionsSection);

            CheckHash = Config.GetProperty(CheckHashKey, false, OptionsSection);

            QueueAutoDownload = Config.GetProperty(QueueAutoDownloadKey, true, OptionsSection);

            ForceClose = Config.GetProperty(ForceCloseKey, false, OptionsSection);

            ProxyAddress = Config.GetProperty(ProxyAddressKey, @"127.0.0.1", NetworkUtils.ValidateIPv4, ProxySection);

            ProxyPort = Config.GetProperty(ProxyPortKey, 3128, NetworkUtils.ValidatePort, ProxySection);

            ProxyLogin = Config.GetProperty(ProxyLoginKey, String.Empty, null, CryptAction.Crypt, ProxySection);

            ProxyPassword = Config.GetProperty(ProxyPasswordKey, String.Empty, null, CryptAction.Crypt, ProxySection);

            APIKey = Config.GetProperty(APICodeKey, String.Empty, DerpiAPI.CheckAPI, CryptAction.Crypt, null, false, APISection);

            Localization = new ProgramLocalization(LanguageCode.GetValue());
            Logger       = new Logger();

            WebProxy = null;

            if (ProxyAddress.IsValid && ProxyAddress.Value != NetworkUtils.LocalhostIP && ProxyPort.IsValid)
            {
                WebProxy = ProxyUtils.CreateProxy(ProxyAddress.Value, ProxyPort.Value, ProxyLogin.Value, ProxyPassword.Value);
            }
        }
コード例 #2
0
        private async void SaveProxyButtonClickAsync(Object sender, EventArgs e)
        {
            if (_addressTextBox.Text == Globals.ProxyAddress.GetValue() &&
                _portTextBox.Text == Globals.ProxyPort.GetValue().ToString() &&
                _loginTextBox.Text == Globals.ProxyLogin.GetValue() &&
                _passwordTextBox.Text == Globals.ProxyPassword.GetValue())
            {
                Close();
                return;
            }

            if (_addressTextBox.Text == @"127.0.0.1" && String.IsNullOrEmpty(_loginTextBox.Text) && String.IsNullOrEmpty(_passwordTextBox.Text))
            {
                ResetProxy();
                Close();
                return;
            }


            Int32.TryParse(_portTextBox.Text, out Int32 port);

            if (!NetworkUtils.ValidatePort(port))
            {
                new MessageForm(Globals.Localization.InvalidProxyPort, Globals.Localization.Error, Images.Basic.Warning, Images.Basic.Warning,
                                MessageBoxButtons.OK, new[] { Globals.Localization.OK }).ShowDialog();
                return;
            }

            WebProxy proxy = ProxyUtils.CreateProxy(_addressTextBox.Text, port, _loginTextBox.Text, _passwordTextBox.Text);
            Task <HttpStatusCode> checkTask = ProxyUtils.CheckProxyAsync(proxy);

            _addressTextBox.Enabled   = false;
            _portTextBox.Enabled      = false;
            _loginTextBox.Enabled     = false;
            _passwordTextBox.Enabled  = false;
            _resetProxyButton.Enabled = false;
            _saveProxyButton.Enabled  = false;

            HttpStatusCode check = HttpStatusCode.ServiceUnavailable;

            if (await Task.WhenAny(checkTask, Task.Delay(3000)).ConfigureAwait(true) == checkTask)
            {
                check = await checkTask.ConfigureAwait(true);
            }

            switch (check)
            {
            case HttpStatusCode.OK:
                Globals.WebProxy = proxy;
                Globals.ProxyAddress.SetValue(_addressTextBox.Text);
                Globals.ProxyPort.SetValue(port);
                Globals.ProxyLogin.SetValue(_loginTextBox.Text);
                Globals.ProxyPassword.SetValue(_passwordTextBox.Text);
                Close();
                break;

            case HttpStatusCode.Forbidden:
                new MessageForm(Globals.Localization.ProxyConnectionInvalid, Globals.Localization.Error, Images.Basic.Warning, Images.Basic.Warning,
                                MessageBoxButtons.OK, new[] { Globals.Localization.OK }).ShowDialog();
                break;

            case HttpStatusCode.ProxyAuthenticationRequired:
                new MessageForm(Globals.Localization.InvalidProxyCredentials, Globals.Localization.Error, Images.Basic.Warning, Images.Basic.Warning,
                                MessageBoxButtons.OK, new[] { Globals.Localization.OK }).ShowDialog();
                break;

            case HttpStatusCode.ServiceUnavailable:
                new MessageForm(Globals.Localization.ProxyIsUnreachable, Globals.Localization.Error, Images.Basic.Warning, Images.Basic.Warning,
                                MessageBoxButtons.OK, new[] { Globals.Localization.OK }).ShowDialog();
                break;

            default:
                new MessageForm($"{Globals.Localization.UnknownError}\n{check}", Globals.Localization.Error, Images.Basic.Warning, Images.Basic.Warning,
                                MessageBoxButtons.OK, new[] { Globals.Localization.OK }).ShowDialog();
                break;
            }

            _addressTextBox.Enabled   = true;
            _portTextBox.Enabled      = true;
            _loginTextBox.Enabled     = true;
            _passwordTextBox.Enabled  = true;
            _resetProxyButton.Enabled = true;
            CheckAddressValid();
        }