Search in sources :

Example 1 with NoProxySelector

use of org.kse.utilities.net.NoProxySelector in project keystore-explorer by kaikramer.

the class ApplicationSettings method getCurrentProxySettings.

private void getCurrentProxySettings(Preferences preferences) {
    // Get current proxy settings
    ProxySelector proxySelector = ProxySelector.getDefault();
    if (proxySelector instanceof NoProxySelector) {
        preferences.put(KSE3_PROXY, ProxyConfigurationType.NONE.name());
    } else if (proxySelector instanceof SystemProxySelector) {
        preferences.put(KSE3_PROXY, ProxyConfigurationType.SYSTEM.name());
    } else if (proxySelector instanceof PacProxySelector) {
        PacProxySelector pacProxySelector = (PacProxySelector) proxySelector;
        preferences.put(KSE3_PACURL, pacProxySelector.getPacUrl());
        preferences.put(KSE3_PROXY, ProxyConfigurationType.PAC.name());
    } else if (proxySelector instanceof ManualProxySelector) {
        ManualProxySelector manualProxySelector = (ManualProxySelector) proxySelector;
        ProxyAddress httpProxyAddress = manualProxySelector.getHttpProxyAddress();
        if (httpProxyAddress != null) {
            preferences.put(KSE3_HTTPHOST, httpProxyAddress.getHost());
            preferences.putInt(KSE3_HTTPPORT, httpProxyAddress.getPort());
        }
        ProxyAddress httpsProxyAddress = manualProxySelector.getHttpsProxyAddress();
        if (httpsProxyAddress != null) {
            preferences.put(KSE3_HTTPSHOST, httpsProxyAddress.getHost());
            preferences.putInt(KSE3_HTTPSPORT, httpsProxyAddress.getPort());
        }
        ProxyAddress socksProxyAddress = manualProxySelector.getSocksProxyAddress();
        if (socksProxyAddress != null) {
            preferences.put(KSE3_SOCKSHOST, socksProxyAddress.getHost());
            preferences.putInt(KSE3_SOCKSPORT, socksProxyAddress.getPort());
        }
        preferences.put(KSE3_PROXY, ProxyConfigurationType.MANUAL.name());
    }
}
Also used : ManualProxySelector(org.kse.utilities.net.ManualProxySelector) ProxySelector(java.net.ProxySelector) PacProxySelector(org.kse.utilities.net.PacProxySelector) SystemProxySelector(org.kse.utilities.net.SystemProxySelector) NoProxySelector(org.kse.utilities.net.NoProxySelector) PacProxySelector(org.kse.utilities.net.PacProxySelector) ProxyAddress(org.kse.utilities.net.ProxyAddress) ManualProxySelector(org.kse.utilities.net.ManualProxySelector) SystemProxySelector(org.kse.utilities.net.SystemProxySelector) NoProxySelector(org.kse.utilities.net.NoProxySelector)

Example 2 with NoProxySelector

use of org.kse.utilities.net.NoProxySelector in project keystore-explorer by kaikramer.

the class DPreferences method storeProxyPreferences.

private boolean storeProxyPreferences() {
    // Store current proxy selector - compare with new one to see if default needs updated
    ProxySelector defaultProxySelector = ProxySelector.getDefault();
    if (jrbNoProxy.isSelected()) {
        NoProxySelector noProxySelector = new NoProxySelector();
        if (!noProxySelector.equals(defaultProxySelector)) {
            ProxySelector.setDefault(noProxySelector);
        }
    } else if (jrbSystemProxySettings.isSelected()) {
        SystemProxySelector systemProxySelector = new SystemProxySelector();
        if (!systemProxySelector.equals(defaultProxySelector)) {
            ProxySelector.setDefault(systemProxySelector);
        }
    } else if (jrbManualProxyConfig.isSelected()) {
        String httpHost = jtfHttpHost.getText().trim();
        String httpPortStr = jtfHttpPort.getText().trim();
        String httpsHost = jtfHttpsHost.getText().trim();
        String httpsPortStr = jtfHttpsPort.getText().trim();
        String socksHost = jtfSocksHost.getText().trim();
        String socksPortStr = jtfSocksPort.getText().trim();
        ProxyAddress httpProxyAddress = null;
        ProxyAddress httpsProxyAddress = null;
        ProxyAddress socksProxyAddress = null;
        // Require at least one of the HTTP host or HTTPS host or SOCKS host manual settings
        if ((httpHost.length() == 0) && (httpsHost.length() == 0) && (socksHost.length() == 0)) {
            jtpPreferences.setSelectedIndex(3);
            JOptionPane.showMessageDialog(this, res.getString("DPreferences.ManualConfigReq.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
            return false;
        } else {
            if (httpHost.length() > 0) {
                if (httpPortStr.length() == 0) {
                    jtpPreferences.setSelectedIndex(3);
                    JOptionPane.showMessageDialog(this, res.getString("DPreferences.PortReqHttp.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
                    return false;
                } else {
                    try {
                        int httpPort = Integer.parseInt(httpPortStr);
                        if (httpPort < 1) {
                            throw new NumberFormatException();
                        }
                        httpProxyAddress = new ProxyAddress(httpHost, httpPort);
                    } catch (NumberFormatException ex) {
                        jtpPreferences.setSelectedIndex(3);
                        JOptionPane.showMessageDialog(this, res.getString("DPreferences.IntegerPortReqHttp.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
                        return false;
                    }
                }
            }
            if (httpsHost.length() > 0) {
                if (httpsPortStr.length() == 0) {
                    jtpPreferences.setSelectedIndex(3);
                    JOptionPane.showMessageDialog(this, res.getString("DPreferences.PortReqHttps.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
                    return false;
                } else {
                    try {
                        int httpsPort = Integer.parseInt(httpsPortStr);
                        if (httpsPort < 1) {
                            throw new NumberFormatException();
                        }
                        httpsProxyAddress = new ProxyAddress(httpsHost, httpsPort);
                    } catch (NumberFormatException ex) {
                        jtpPreferences.setSelectedIndex(3);
                        JOptionPane.showMessageDialog(this, res.getString("DPreferences.IntegerPortReqHttps.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
                        return false;
                    }
                }
            }
            if (socksHost.length() > 0) {
                if (socksPortStr.length() == 0) {
                    jtpPreferences.setSelectedIndex(3);
                    JOptionPane.showMessageDialog(this, res.getString("DPreferences.PortReqSocks.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
                    return false;
                } else {
                    try {
                        int socksPort = Integer.parseInt(socksPortStr);
                        if (socksPort < 1) {
                            throw new NumberFormatException();
                        }
                        socksProxyAddress = new ProxyAddress(socksHost, socksPort);
                    } catch (NumberFormatException ex) {
                        jtpPreferences.setSelectedIndex(3);
                        JOptionPane.showMessageDialog(this, res.getString("DPreferences.IntegerPortReqSocks.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
                        return false;
                    }
                }
            }
            ManualProxySelector manualProxySelector = new ManualProxySelector(httpProxyAddress, httpsProxyAddress, null, socksProxyAddress);
            if (!manualProxySelector.equals(defaultProxySelector)) {
                ProxySelector.setDefault(manualProxySelector);
            }
        }
    } else if (jrbAutomaticProxyConfig.isSelected()) {
        String pacUrl = jtfPacUrl.getText().trim();
        if (pacUrl.length() == 0) {
            jtpPreferences.setSelectedIndex(3);
            JOptionPane.showMessageDialog(this, res.getString("DPreferences.PacUrlReq.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
            return false;
        }
        PacProxySelector pacProxySelector = new PacProxySelector(pacUrl);
        if (!pacProxySelector.equals(defaultProxySelector)) {
            ProxySelector.setDefault(pacProxySelector);
        }
    }
    return true;
}
Also used : ManualProxySelector(org.kse.utilities.net.ManualProxySelector) ProxySelector(java.net.ProxySelector) PacProxySelector(org.kse.utilities.net.PacProxySelector) SystemProxySelector(org.kse.utilities.net.SystemProxySelector) NoProxySelector(org.kse.utilities.net.NoProxySelector) PacProxySelector(org.kse.utilities.net.PacProxySelector) ProxyAddress(org.kse.utilities.net.ProxyAddress) ManualProxySelector(org.kse.utilities.net.ManualProxySelector) SystemProxySelector(org.kse.utilities.net.SystemProxySelector) NoProxySelector(org.kse.utilities.net.NoProxySelector)

Example 3 with NoProxySelector

use of org.kse.utilities.net.NoProxySelector in project keystore-explorer by kaikramer.

the class ApplicationSettings method load.

/**
 * Load application settings from persistent store.
 */
public void load() {
    Preferences preferences = getUnderlyingPreferences();
    // Authority certificates
    useCaCertificates = preferences.getBoolean(KSE3_USECACERTS, false);
    String cacertsPath = preferences.get(KSE3_CACERTSFILE, AuthorityCertificates.getDefaultCaCertificatesLocation().toString());
    caCertificatesFile = cleanFilePath(new File(cacertsPath));
    useWindowsTrustedRootCertificates = preferences.getBoolean(KSE3_USEWINTRUSTROOTCERTS, false);
    // Trust checks
    enableImportTrustedCertTrustCheck = preferences.getBoolean(KSE3_ENABLEIMPORTTRUSTEDCERTTRUSTCHECK, false);
    enableImportCaReplyTrustCheck = preferences.getBoolean(KSE3_ENABLEIMPORTCAREPLYTRUSTCHECK, false);
    // Key pair generation
    generateKeyPairType = KeyPairType.resolveJce(preferences.get(KSE3_KEYPAIRTYPE, RSA.jce()));
    if (generateKeyPairType == null) {
        generateKeyPairType = RSA;
    }
    int defaultKeyPairSize;
    if (generateKeyPairType == RSA) {
        defaultKeyPairSize = 2048;
    } else {
        // DSA
        defaultKeyPairSize = 1024;
    }
    generateKeyPairSize = preferences.getInt(KSE3_KEYPAIRSIZE, defaultKeyPairSize);
    // Secret key generation
    generateSecretKeyType = SecretKeyType.resolveJce(preferences.get(KSE3_SECKEYTYPE, AES.jce()));
    if (generateSecretKeyType == null) {
        generateSecretKeyType = AES;
    }
    generateSecretKeySize = preferences.getInt(KSE3_SECKEYSIZE, 192);
    // Certificate fingerprint
    certificateFingerprintType = DigestType.resolveJce(preferences.get(KSE3_CERTFINGERTYPE, SHA1.jce()));
    if (certificateFingerprintType == null) {
        certificateFingerprintType = SHA1;
    }
    // Password quality
    passwordQualityConfig = new PasswordQualityConfig(preferences.getBoolean(KSE3_PWDQUALENABLE, false), preferences.getBoolean(KSE3_MINPWDQUALENFORCE, false), preferences.getInt(KSE3_MINPWDQUAL, 60));
    // Internet proxy settings
    ProxyConfigurationType proxyConfigurationType = ProxyConfigurationType.resolve(preferences.get(KSE3_PROXY, ProxyConfigurationType.SYSTEM.name()));
    // default should be system settings because of "java.net.useSystemProxies=true", save it for later usage
    SystemProxySelector.setSystemProxySelector(ProxySelector.getDefault());
    switch(proxyConfigurationType) {
        case NONE:
            ProxySelector.setDefault(new NoProxySelector());
            break;
        case PAC:
            // Use PAC URL for proxy configuration
            String pacUrl = preferences.get(KSE3_PACURL, null);
            if (pacUrl != null) {
                ProxySelector.setDefault(new PacProxySelector(pacUrl));
            } else {
                ProxySelector.setDefault(new NoProxySelector());
            }
            break;
        case MANUAL:
            // Use manual settings for HTTP, HTTPS and SOCKS
            ProxyAddress httpProxyAddress = null;
            ProxyAddress httpsProxyAddress = null;
            ProxyAddress socksProxyAddress = null;
            String httpHost = preferences.get(KSE3_HTTPHOST, null);
            int httpPort = preferences.getInt(KSE3_HTTPPORT, 0);
            if (httpHost != null && httpPort > 0) {
                httpProxyAddress = new ProxyAddress(httpHost, httpPort);
            }
            String httpsHost = preferences.get(KSE3_HTTPSHOST, null);
            int httpsPort = preferences.getInt(KSE3_HTTPSPORT, 0);
            if (httpsHost != null && httpsPort > 0) {
                httpsProxyAddress = new ProxyAddress(httpsHost, httpsPort);
            }
            String socksHost = preferences.get(KSE3_SOCKSHOST, null);
            int socksPort = preferences.getInt(KSE3_SOCKSPORT, 0);
            if (socksHost != null && socksPort > 0) {
                socksProxyAddress = new ProxyAddress(socksHost, socksPort);
            }
            if (httpProxyAddress != null || httpsProxyAddress != null) {
                ProxySelector.setDefault(new ManualProxySelector(httpProxyAddress, httpsProxyAddress, null, socksProxyAddress));
            } else {
                // no manual settings - use no proxy to connect to the Internet
                ProxySelector.setDefault(new NoProxySelector());
            }
            break;
        case SYSTEM:
        default:
            ProxySelector.setDefault(new SystemProxySelector());
            break;
    }
    // Application size and position
    sizeAndPosition = new Rectangle(preferences.getInt(KSE3_XPOS, 0), preferences.getInt(KSE3_YPOS, 0), preferences.getInt(KSE3_WIDTH, KseFrame.DEFAULT_WIDTH), preferences.getInt(KSE3_HEIGHT, KseFrame.DEFAULT_HEIGHT));
    // User interface
    showToolBar = preferences.getBoolean(KSE3_SHOWTOOLBAR, true);
    showStatusBar = preferences.getBoolean(KSE3_SHOWSTATUSBAR, true);
    tabLayout = preferences.getInt(KSE3_TABLAYOUT, JTabbedPane.WRAP_TAB_LAYOUT);
    // Recent files
    ArrayList<File> recentFilesList = new ArrayList<File>();
    for (int i = 1; i <= KseFrame.RECENT_FILES_SIZE; i++) {
        String recentFile = preferences.get(KSE3_RECENTFILE + i, null);
        if (recentFile == null) {
            break;
        } else {
            recentFilesList.add(cleanFilePath(new File(recentFile)));
        }
    }
    recentFiles = recentFilesList.toArray(new File[recentFilesList.size()]);
    // Current directory
    String currentDirectoryStr = preferences.get(KSE3_CURRENTDIR, null);
    if (currentDirectoryStr != null) {
        currentDirectory = cleanFilePath(new File(currentDirectoryStr));
    }
    // Look and feel
    lookAndFeelClass = preferences.get(KSE3_LOOKFEEL, null);
    lookAndFeelDecorated = preferences.getBoolean(KSE3_LOOKFEELDECOR, false);
    // Licensing
    licenseAgreed = preferences.getBoolean(KSE3_LICENSEAGREED, false);
    // Tip of the day
    showTipsOnStartUp = preferences.getBoolean(KSE3_TIPSONSTARTUP, true);
    nextTipIndex = preferences.getInt(KSE3_TIPINDEX, 0);
    // Default distinguished name
    defaultDN = preferences.get(KSE3_DEFAULTDN, "");
    // SSL host names and ports for "Examine SSL"
    sslHosts = preferences.get(KSE3_SSLHOSTS, "www.google.com;www.amazon.com");
    sslPorts = preferences.get(KSE3_SSLPORTS, "443");
    // auto update check
    autoUpdateCheckEnabled = preferences.getBoolean(KSE3_AUTO_UPDATE_CHECK_ENABLED, true);
    autoUpdateCheckInterval = preferences.getInt(KSE3_AUTO_UPDATE_CHECK_INTERVAL, 14);
    autoUpdateCheckLastCheck = getDate(preferences, KSE3_AUTO_UPDATE_CHECK_LAST_CHECK, new Date());
    // PKCS#11 libraries
    p11Libs = preferences.get(KSE3_PKCS11_LIBS, "");
}
Also used : PacProxySelector(org.kse.utilities.net.PacProxySelector) ProxyAddress(org.kse.utilities.net.ProxyAddress) Rectangle(java.awt.Rectangle) ArrayList(java.util.ArrayList) SystemProxySelector(org.kse.utilities.net.SystemProxySelector) NoProxySelector(org.kse.utilities.net.NoProxySelector) Date(java.util.Date) ProxyConfigurationType(org.kse.utilities.net.ProxyConfigurationType) ManualProxySelector(org.kse.utilities.net.ManualProxySelector) Preferences(java.util.prefs.Preferences) File(java.io.File) PasswordQualityConfig(org.kse.gui.password.PasswordQualityConfig)

Aggregations

ManualProxySelector (org.kse.utilities.net.ManualProxySelector)3 NoProxySelector (org.kse.utilities.net.NoProxySelector)3 PacProxySelector (org.kse.utilities.net.PacProxySelector)3 ProxyAddress (org.kse.utilities.net.ProxyAddress)3 SystemProxySelector (org.kse.utilities.net.SystemProxySelector)3 ProxySelector (java.net.ProxySelector)2 Rectangle (java.awt.Rectangle)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Preferences (java.util.prefs.Preferences)1 PasswordQualityConfig (org.kse.gui.password.PasswordQualityConfig)1 ProxyConfigurationType (org.kse.utilities.net.ProxyConfigurationType)1