package org.helioviewer.jhv.base;

import java.net.Authenticator;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;

import org.helioviewer.jhv.Settings;

public class ProxySettings {

    public static final Proxy proxy;

    static {
        String[] httpVars = { "http.proxyHost", "http.proxyPort", "http.proxyUser", "http.proxyPassword" };
        String[] socksVars = { "socksProxyHost", "socksProxyPort", "java.net.socks.username", "java.net.socks.password" };

        Proxy _proxy = detectProxy(httpVars[0], httpVars[1], Proxy.Type.HTTP);
        if (_proxy == null)
            _proxy = detectProxy(socksVars[0], socksVars[1], Proxy.Type.SOCKS);
        if (_proxy == null)
            _proxy = Proxy.NO_PROXY;

        proxy = _proxy;

        if (proxy != Proxy.NO_PROXY) {
            Authenticator.setDefault(new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    if (getRequestorType() == RequestorType.PROXY) {
                        String[] vars = proxy.type().equals(Proxy.Type.HTTP) ? httpVars : socksVars;
                        String host = System.getProperty(vars[0]);
                        String port = System.getProperty(vars[1]);
                        String user = System.getProperty(vars[2]);
                        String pass = System.getProperty(vars[3]);

                        if (user == null || pass == null) {
                            user = Settings.getSingletonInstance().getProperty("default.proxyUsername");
                            pass = Settings.getSingletonInstance().getProperty("default.proxyPassword");
                        }

                        if (user != null && pass != null && getRequestingHost().equalsIgnoreCase(host) && Integer.toString(getRequestingPort()).equals(port)) {
                            return new PasswordAuthentication(user, pass.toCharArray());
                        }
                    }
                    return null;
                }
            });
        }
    }

    private static Proxy detectProxy(String host, String port, Proxy.Type type) {
        String proxyHost = System.getProperty(host);
        if (proxyHost != null) {
            try {
                int proxyPort = Integer.parseInt(System.getProperty(port));
                return new Proxy(type, InetSocketAddress.createUnresolved(proxyHost, proxyPort));
            } catch (Exception ignore) {
            }
        }
        return null;
    }

    public static void init() {}

}

@@ -994,81 +994,8 @@
 ) %7B%0A
-                    if (getRequestorType() == RequestorType.PROXY) %7B%0A    
     
@@ -1103,28 +1103,24 @@
             
-    
 String host 
@@ -1146,24 +1146,166 @@
 y(vars%5B0%5D);%0A
+                    if (getRequestingHost().equalsIgnoreCase(host)) %7B%0A                    // if (getRequestorType() == RequestorType.PROXY) %7B%0A
             
@@ -1854,54 +1854,8 @@
 l &&
- getRequestingHost().equalsIgnoreCase(host) &&
  Int

Check against server name not type