@Override
  public List<Proxy> select(URI uri) {
    String url = uri.toString().replace("www", "");
    if (!uri.getScheme().startsWith("http") && Bukkit.isPrimaryThread()) {
      Exception stackTraceCreator = new Exception();
      StackTraceElement[] stackTrace = stackTraceCreator.getStackTrace();

      // remove the parts from LagMonitor
      StackTraceElement[] copyOfRange = Arrays.copyOfRange(stackTrace, 1, stackTrace.length);
      Entry<Plugin, StackTraceElement> foundPlugin = PluginUtil.findPlugin(copyOfRange);

      PluginViolation violation = new PluginViolation(url);
      if (foundPlugin != null) {
        String pluginName = foundPlugin.getKey().getName();
        violation = new PluginViolation(pluginName, foundPlugin.getValue(), url);

        if (!violatedPlugins.add(violation.getPluginName())
            && plugin.getConfig().getBoolean("oncePerPlugin")) {
          return oldProxySelector == null
              ? Lists.newArrayList(Proxy.NO_PROXY)
              : oldProxySelector.select(uri);
        }
      }

      if (!violations.add(violation)) {
        return oldProxySelector == null
            ? Lists.newArrayList(Proxy.NO_PROXY)
            : oldProxySelector.select(uri);
      }

      plugin
          .getLogger()
          .log(
              Level.WARNING,
              "Plugin {0} is performing a blocking action to {1} on the main thread"
                  + " This could be a performance hit."
                  + " Report it to the plugin author",
              new Object[] {violation.getPluginName(), url});

      if (plugin.getConfig().getBoolean("hideStacktrace")) {
        if (foundPlugin != null) {
          StackTraceElement source = foundPlugin.getValue();
          plugin
              .getLogger()
              .log(
                  Level.WARNING,
                  "Source: {0}, method {1}, line {2}",
                  new Object[] {
                    source.getClassName(), source.getMethodName(), source.getLineNumber()
                  });
        }
      } else {
        plugin.getLogger().log(Level.WARNING, "", stackTraceCreator);
      }
    }

    return oldProxySelector == null
        ? Lists.newArrayList(Proxy.NO_PROXY)
        : oldProxySelector.select(uri);
  }
  // Result host and port are returned in a string in the format host:port
  public static String[] getProxyForURL(String target) {
    String[] proxyInfo = new String[0];
    List<String> proxies = new ArrayList<String>();
    URI uri = null;
    try {
      ProxySelector defaultProxySelector = ProxySelector.getDefault();
      uri = new URI(target);
      List<Proxy> proxyList = defaultProxySelector.select(uri);

      Proxy proxy = proxyList.get(0);
      if (proxy.equals(Proxy.NO_PROXY)) {
        System.out.println("DalvikProxySelector.getProxyForURL(): No proxy found");
        return null;
      }
      SocketAddress address = proxy.address();
      InetSocketAddress inetSocketAddress = (InetSocketAddress) address;
      String host = inetSocketAddress.getHostName();
      int port = inetSocketAddress.getPort();
      if (host == null) {
        System.out.println("DalvikProxySelector.getProxyForURL(): No proxy found");
        return null;
      }

      proxies.add(host); // even index, host
      proxies.add(Integer.toString(port)); // odd index, port

      System.out.println("DalvikProxySelector.getProxyForURL(): host=" + host + " port=" + port);
      return proxies.toArray(new String[0]);
    } catch (Exception e) {
      System.out.println(
          "DalvikProxySelector.getProxyForURL(): exception(ignored): " + e.toString());
      return null;
    }
  }
Esempio n. 3
0
  /** Get or create the http client to be used to fetch all the map data. */
  public HttpClient getHttpClient(URI uri) {
    MultiThreadedHttpConnectionManager connectionManager = getConnectionManager();
    HttpClient httpClient = new HttpClient(connectionManager);

    // httpclient is a bit pesky about loading everything in memory...
    // disabling the warnings.
    Logger.getLogger(HttpMethodBase.class).setLevel(Level.ERROR);

    // configure proxies for URI
    ProxySelector selector = ProxySelector.getDefault();

    List<Proxy> proxyList = selector.select(uri);
    Proxy proxy = proxyList.get(0);

    if (!proxy.equals(Proxy.NO_PROXY)) {
      InetSocketAddress socketAddress = (InetSocketAddress) proxy.address();
      String hostName = socketAddress.getHostName();
      int port = socketAddress.getPort();

      httpClient.getHostConfiguration().setProxy(hostName, port);
    }

    for (SecurityStrategy sec : security)
      if (sec.matches(uri)) {
        sec.configure(uri, httpClient);
        break;
      }
    return httpClient;
  }
Esempio n. 4
0
 /**
  * Gets proxy list according to the URI by system ProxySelector.
  *
  * @param uri
  * @return a list of proxy for the URI. Returns null if no proxy is available.
  */
 public static List<Proxy> getProxyList(URI uri) {
   // use system default selector to get proxy list
   ProxySelector selector = ProxySelector.getDefault();
   if (null == selector) {
     return null;
   }
   return selector.select(uri);
 }
Esempio n. 5
0
 @Override
 public List<Proxy> select(URI uri) {
   if (HTTP.equalsIgnoreCase(uri.getScheme()) || SOCKS.equalsIgnoreCase(uri.getScheme())) {
     if (isInExcludedSet(uri.toASCIIString())) {
       if (isLogEnabled()) {
         Log.d(TAG, "Found in excludedSet. Returning default proxy for url : " + uri);
       }
       if (sDefaultProxy == null) {
         return mDefaultProxySelector.select(uri);
       }
       return Arrays.asList(
           new Proxy(
               Proxy.Type.HTTP,
               new InetSocketAddress(sDefaultProxy.getHost(), sDefaultProxy.getPort())));
     }
     ProxyInfo proxyForUrl = getProxyForUrl(uri.toASCIIString());
     if (proxyForUrl != null) {
       if (isLogEnabled()) {
         Log.d(
             TAG,
             "Found in includeSet. Returning proxy ("
                 + proxyForUrl.getHost()
                 + ", "
                 + proxyForUrl.getPort()
                 + ") for url : "
                 + uri);
       }
       return Arrays.asList(
           new Proxy(
               Proxy.Type.HTTP,
               new InetSocketAddress(proxyForUrl.getHost(), proxyForUrl.getPort())));
     }
   }
   if (isLogEnabled()) {
     Log.d(TAG, "Not found anywhere. Returning default proxy (which is none generally).");
   }
   if (sDefaultProxy == null) {
     return mDefaultProxySelector.select(uri);
   }
   return Arrays.asList(
       new Proxy(
           Proxy.Type.HTTP,
           new InetSocketAddress(sDefaultProxy.getHost(), sDefaultProxy.getPort())));
 }
Esempio n. 6
0
 /** Resets {@link #nextProxy} to the first option. */
 private void resetNextProxy(URI uri, Proxy proxy) {
   this.hasNextProxy = true; // This includes NO_PROXY!
   if (proxy != null) {
     this.userSpecifiedProxy = proxy;
   } else {
     List<Proxy> proxyList = proxySelector.select(uri);
     if (proxyList != null) {
       this.proxySelectorProxies = proxyList.iterator();
     }
   }
 }
 /**
  * Establishes the connection to the remote HTTP server
  *
  * <p>Any methods that requires a valid connection to the resource will call this method
  * implicitly. After the connection is established, <code>connected</code> is set to true.
  *
  * @see #connected
  * @see java.io.IOException
  * @see URLStreamHandler
  */
 @Override
 public void connect() throws IOException {
   if (connected) {
     return;
   }
   if (getFromCache()) {
     return;
   }
   try {
     uri = url.toURI();
   } catch (URISyntaxException e1) {
     // ignore
   }
   // socket to be used for connection
   connection = null;
   // try to determine: to use the proxy or not
   if (proxy != null) {
     // try to make the connection to the proxy
     // specified in constructor.
     // IOException will be thrown in the case of failure
     connection = getHTTPConnection(proxy);
   } else {
     // Use system-wide ProxySelect to select proxy list,
     // then try to connect via elements in the proxy list.
     ProxySelector selector = ProxySelector.getDefault();
     List<Proxy> proxyList = selector.select(uri);
     if (proxyList != null) {
       for (Proxy selectedProxy : proxyList) {
         if (selectedProxy.type() == Proxy.Type.DIRECT) {
           // the same as NO_PROXY
           continue;
         }
         try {
           connection = getHTTPConnection(selectedProxy);
           proxy = selectedProxy;
           break; // connected
         } catch (IOException e) {
           // failed to connect, tell it to the selector
           selector.connectFailed(uri, selectedProxy.address(), e);
         }
       }
     }
   }
   if (connection == null) {
     // make direct connection
     connection = getHTTPConnection(null);
   }
   connection.setSoTimeout(getReadTimeout());
   setUpTransportIO(connection);
   connected = true;
 }
 /**
  * *********************************************************************** Test method
  *
  * @throws ProxyException on proxy detection error.
  * @throws URISyntaxException on invalid URL syntax.
  *     **********************************************************************
  */
 @Test
 public void testManualFtp() throws ProxyException, URISyntaxException {
   System.setProperty(
       OsxProxySearchStrategy.OVERRIDE_SETTINGS_FILE,
       "test"
           + File.separator
           + "data"
           + File.separator
           + "osx"
           + File.separator
           + "osx_manual.plist");
   ProxySelector ps = new OsxProxySearchStrategy().getProxySelector();
   List<Proxy> result = ps.select(TestUtil.FTP_TEST_URI);
   assertEquals(TestUtil.FTP_TEST_PROXY, result.get(0));
 }
 /**
  * To run this test it is necessary that "http://localhost/pac42.js" is entered in the "automatic
  * configuration script" of the "internet options" dialog.
  *
  * @throws URISyntaxException
  * @throws IOException
  */
 public void testPac() {
   TestServer server = null;
   try {
     server = new TestServer(80, getFile("pac42.js").getParentFile()); // $NON-NLS-1$
     final ProxySelector selector = new CoreNetProxySelector();
     final List<Proxy> proxies = selector.select(new URI("http://www.eclipse.org")); // $NON-NLS-1$
     assertEquals(2, proxies.size());
     assertEquals(
         "idproxy", ((InetSocketAddress) proxies.get(0).address()).getHostName()); // $NON-NLS-1$
     assertEquals(3128, ((InetSocketAddress) proxies.get(0).address()).getPort());
   } catch (final Throwable e) {
     fail();
   } finally {
     server.stop();
   }
 }
Esempio n. 10
0
 private static void setToUseSystemProxies() {
   final String JAVA_NET_USESYSTEMPROXIES = "java.net.useSystemProxies";
   System.setProperty(JAVA_NET_USESYSTEMPROXIES, "true");
   List<Proxy> proxyList = null;
   try {
     final ProxySelector def = ProxySelector.getDefault();
     if (def != null) {
       proxyList = def.select(new URI("http://sourceforge.net/"));
       ProxySelector.setDefault(null);
       if (proxyList != null && !proxyList.isEmpty()) {
         final Proxy proxy = proxyList.get(0);
         final InetSocketAddress address = (InetSocketAddress) proxy.address();
         if (address != null) {
           final String host = address.getHostName();
           final int port = address.getPort();
           System.setProperty(HTTP_PROXYHOST, host);
           System.setProperty(HTTP_PROXYPORT, Integer.toString(port));
           System.setProperty(PROXY_HOST, host);
           System.setProperty(PROXY_PORT, Integer.toString(port));
         } else {
           System.clearProperty(HTTP_PROXYHOST);
           System.clearProperty(HTTP_PROXYPORT);
           System.clearProperty(PROXY_HOST);
           System.clearProperty(PROXY_PORT);
         }
       }
     } else {
       final String host = System.getProperty(PROXY_HOST);
       final String port = System.getProperty(PROXY_PORT);
       if (host == null) System.clearProperty(HTTP_PROXYHOST);
       else System.setProperty(HTTP_PROXYHOST, host);
       if (port == null) System.clearProperty(HTTP_PROXYPORT);
       else {
         try {
           Integer.parseInt(port);
           System.setProperty(HTTP_PROXYPORT, port);
         } catch (final NumberFormatException nfe) {
           // nothing
         }
       }
     }
   } catch (final Exception e) {
     e.printStackTrace();
   } finally {
     System.setProperty(JAVA_NET_USESYSTEMPROXIES, "false");
   }
 }
Esempio n. 11
0
    public List<Proxy> select(URI uri) {

      int useProxy = ProxySettings.getInstance().getUseProxy();
      if (useProxy == PROXY_AUTOMATIC) return origSelector.select(uri);
      else if (useProxy == PROXY_MANUAL) {
        Proxy p = (Proxy) ProxySettings.getInstance().getProxyForJavaNet(uri.toString());
        if (p != null) {
          List<Proxy> proxies = new ArrayList<Proxy>(1);
          proxies.add(p);
          return proxies;
        } else {
          return NO_PROXY_LIST;
        }
      } else {
        return NO_PROXY_LIST;
      }
    }
Esempio n. 12
0
  // Decide what proxy, if any, to use for a request. Manual settings take precedence,
  // then we fall back to using system settings (e.g., Internet Explorer settings on Windows)
  // as handled by the ProxySelector class introduced in Java 1.5.
  //
  // Public so that it can be called from Mathematica.
  //
  public synchronized String[] getProxyHostAndPort(String url) {

    int port = 0;
    String host = null;

    if (useProxy == PROXY_AUTOMATIC) {
      ProxySelector ps = ProxySelector.getDefault(); // Will get MyProxySelector.
      try {
        URI uri = new URI(url);
        List<Proxy> proxyList = ps.select(uri);
        int len = proxyList.size();
        for (int i = 0; i < len; i++) {
          Proxy p = (Proxy) proxyList.get(i);
          InetSocketAddress addr = (InetSocketAddress) p.address();
          if (addr != null) {
            host = addr.getHostName();
            port = addr.getPort();
            break;
          }
        }
      } catch (Exception e) {
        // TODO: Handle?
      }
    } else if (useProxy == PROXY_MANUAL) {
      String protocol;
      int colonPos = url.indexOf(':');
      if (colonPos != -1) {
        protocol = url.substring(0, colonPos).toLowerCase();
      } else {
        // Shouldn't happen, but might as well do something and let it fail later.
        protocol = "http";
      }
      if (protocol.equals("http")) {
        host = httpProxyHost;
        port = httpProxyPort;
      }
      // Don't handle directly Socks calls.
    }

    return new String[] {host, String.valueOf(port)};
  }
 /** {@inheritDoc} */
 @Override
 public List<Proxy> select(final URI uri) {
   if (uri == null) {
     throw new IllegalArgumentException("Arguments can not be null.");
   }
   final String protocol = uri.getScheme();
   if ("http".equalsIgnoreCase(protocol) || "https".equalsIgnoreCase(protocol)) {
     final ArrayList<Proxy> proxyList = new ArrayList<>();
     for (final ProxyDecorator p : proxies.values()) {
       proxyList.add(p.getProxy());
     }
     return proxyList;
   }
   if (defaultSelector != null) {
     return defaultSelector.select(uri);
   } else {
     final ArrayList<Proxy> proxyList = new ArrayList<>();
     proxyList.add(Proxy.NO_PROXY);
     return proxyList;
   }
 }