List of usage examples for org.apache.http.impl.conn SystemDefaultDnsResolver INSTANCE
SystemDefaultDnsResolver INSTANCE
To view the source code for org.apache.http.impl.conn SystemDefaultDnsResolver INSTANCE.
Click Source Link
From source file:com.ninjas.movietime.conf.vendor.metrics.InstrumentedHttpClientConnectionManager.java
public InstrumentedHttpClientConnectionManager(MetricRegistry metricsRegistry, Registry<ConnectionSocketFactory> socketFactoryRegistry, long connTTL, TimeUnit connTTLTimeUnit) { this(metricsRegistry, socketFactoryRegistry, null, null, SystemDefaultDnsResolver.INSTANCE, connTTL, connTTLTimeUnit, null);/*from www . j av a 2 s .co m*/ }
From source file:org.apache.asterix.external.input.stream.factory.SocketClientInputStreamFactory.java
@Override public void configure(Map<String, String> configuration) throws AsterixException { try {//from w ww .j a va 2 s .c o m this.sockets = new ArrayList<Pair<String, Integer>>(); String socketsValue = configuration.get(ExternalDataConstants.KEY_SOCKETS); if (socketsValue == null) { throw new IllegalArgumentException( "\'sockets\' parameter not specified as part of adapter configuration"); } String[] socketsArray = socketsValue.split(","); for (String socket : socketsArray) { String[] socketTokens = socket.split(":"); String host = socketTokens[0].trim(); int port = Integer.parseInt(socketTokens[1].trim()); InetAddress[] resolved; resolved = SystemDefaultDnsResolver.INSTANCE.resolve(host); Pair<String, Integer> p = new Pair<String, Integer>(resolved[0].getHostAddress(), port); sockets.add(p); } } catch (UnknownHostException e) { throw new AsterixException(e); } }
From source file:com.vuze.plugin.azVPN_Helper.Checker_AirVPN.java
@Override protected boolean callRPCforPort(InetAddress bindIP, StringBuilder sReply) { InetAddress[] resolve = null; try {// w ww . j a v a 2 s.c om String user = getDefaultUsername(); String pass = null; if (user == null || user.length() == 0) { user = config.getPluginStringParameter(PluginConstants.CONFIG_USER); pass = new String(config.getPluginByteParameter(PluginConstants.CONFIG_P, new byte[0]), "utf-8"); } else { pass = getPassword(); } if (user == null || user.length() == 0 || pass == null || pass.length() == 0) { addReply(sReply, CHAR_WARN, "airvpn.rpc.nocreds"); return false; } // If Vuze has a proxy set up (Tools->Options->Connection->Proxy), then // we'll need to disable it for the URL AEProxySelector selector = AEProxySelectorFactory.getSelector(); if (selector != null) { resolve = SystemDefaultDnsResolver.INSTANCE.resolve(VPN_DOMAIN); for (InetAddress address : resolve) { selector.setProxy(new InetSocketAddress(address, 443), Proxy.NO_PROXY); } } RequestConfig requestConfig; StringBuffer token = new StringBuffer(); boolean skipLoginPage = false; boolean alreadyLoggedIn = false; PortInfo[] ports = null; if (httpClientContext == null) { httpClientContext = HttpClientContext.create(); } else { PluginVPNHelper.log("Have existing context. Trying to grab port list without logging in."); ports = scrapePorts(bindIP, token); // assume no token means we aren't logged in if (token.length() > 0) { PluginVPNHelper.log("Valid ports page. Skipping Login"); skipLoginPage = true; alreadyLoggedIn = true; if (ports == null) { addReply(sReply, CHAR_WARN, "airvpn.vpnhelper.rpc.notconnected"); return false; } } else { ports = null; } } if (!skipLoginPage) { String loginURL = null; String authKey = null; PluginVPNHelper.log("Getting Login post URL and auth_key"); HttpGet getLoginPage = new HttpGet(VPN_LOGIN_URL); requestConfig = RequestConfig.custom().setLocalAddress(bindIP).setConnectTimeout(15000).build(); getLoginPage.setConfig(requestConfig); getLoginPage.setHeader("User-Agent", USER_AGENT); CloseableHttpClient httpClientLoginPage = HttpClients.createDefault(); CloseableHttpResponse loginPageResponse = httpClientLoginPage.execute(getLoginPage, httpClientContext); BufferedReader rd = new BufferedReader( new InputStreamReader(loginPageResponse.getEntity().getContent())); Pattern patAuthKey = Pattern.compile(REGEX_AuthKey); String line = ""; while ((line = rd.readLine()) != null) { if (line.contains("<form") && line.matches(".*id=['\"]login['\"].*")) { Matcher matcher = Pattern.compile(REGEX_ActionURL).matcher(line); if (matcher.find()) { loginURL = matcher.group(1); if (authKey != null) { break; } } } Matcher matcherAuthKey = patAuthKey.matcher(line); if (matcherAuthKey.find()) { authKey = matcherAuthKey.group(1); if (loginURL != null) { break; } } if (line.contains("['member_id']") && line.matches(".*parseInt\\s*\\(\\s*[1-9][0-9]*\\s*.*")) { alreadyLoggedIn = true; } } rd.close(); if (loginURL == null) { PluginVPNHelper.log("Could not scrape Login URL. Using default"); loginURL = "https://airvpn.org/index.php?app=core&module=global§ion=login&do=process"; } if (authKey == null) { addReply(sReply, CHAR_WARN, "vpnhelper.rpc.noauthkey"); return false; } loginURL = UrlUtils.unescapeXML(loginURL); /////////////////////////////// if (alreadyLoggedIn) { PluginVPNHelper.log("Already Logged In"); } else { PluginVPNHelper.log("Login URL:" + loginURL); //https://airvpn.org/index.php?app=core&module=global§ion=login&do=process //https://airvpn.org/index.php?app=core&module=global§ion=login&do=process HttpPost httpPostLogin = new HttpPost(loginURL); requestConfig = RequestConfig.custom().setLocalAddress(bindIP).setConnectTimeout(15000).build(); httpPostLogin.setConfig(requestConfig); httpPostLogin.setHeader("User-Agent", USER_AGENT); CloseableHttpClient httpClient = HttpClients.createDefault(); List<NameValuePair> urlParameters = new ArrayList<NameValuePair>(); urlParameters.add(new BasicNameValuePair("ips_username", user)); urlParameters.add(new BasicNameValuePair("ips_password", pass)); urlParameters.add(new BasicNameValuePair("auth_key", authKey)); urlParameters.add(new BasicNameValuePair("referer", "http://airvpn.org/")); urlParameters.add(new BasicNameValuePair("anonymous", "1")); urlParameters.add(new BasicNameValuePair("rememberMe", "1")); httpPostLogin.setEntity(new UrlEncodedFormEntity(urlParameters)); CloseableHttpResponse response = httpClient.execute(httpPostLogin, httpClientContext); rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); line = ""; while ((line = rd.readLine()) != null) { } PluginVPNHelper.log("Login Result: " + response.getStatusLine().toString()); } } //////////////////////////// if (ports == null) { ports = scrapePorts(bindIP, token); if (ports == null && token.length() > 0) { addReply(sReply, CHAR_WARN, "airvpn.vpnhelper.rpc.notconnected"); return false; } } PluginVPNHelper.log("Found Ports: " + Arrays.toString(ports)); int existingIndex = ourPortInList(ports); if (existingIndex >= 0) { addReply(sReply, CHAR_GOOD, "vpnhelper.port.from.rpc.match", new String[] { ports[existingIndex].port }); return true; } boolean gotPort = false; // There's a limit of 20 ports. If [0] isn't ours and 20 of them are // created, then assume our detection of "ours" is broke and just use // the first one if (ports != null && ((ports.length > 0 && ports[0].ourBinding) || ports.length == 20)) { int port = Integer.parseInt(ports[0].port); gotPort = true; addReply(sReply, CHAR_GOOD, "vpnhelper.port.from.rpc", new String[] { Integer.toString(port) }); changePort(port, sReply); } else if (ports != null) { // create port ports = createPort(bindIP, token); if (ports.length == 0) { // form post should have got the new port, but if it didn't, try // reloading the ports page again. token.setLength(0); ports = scrapePorts(bindIP, token); } PluginVPNHelper.log("Added a port. Ports: " + Arrays.toString(ports)); existingIndex = ourPortInList(ports); if (existingIndex >= 0) { addReply(sReply, CHAR_GOOD, "vpnhelper.port.from.rpc.match", new String[] { ports[existingIndex].port }); return true; } if ((ports.length > 0 && ports[0].ourBinding) || ports.length == 20) { int port = Integer.parseInt(ports[0].port); gotPort = true; addReply(sReply, CHAR_GOOD, "vpnhelper.port.from.rpc", new String[] { Integer.toString(port) }); changePort(port, sReply); } } if (!gotPort) { addReply(sReply, CHAR_WARN, "vpnhelper.rpc.no.connect", new String[] { bindIP.toString() }); return false; } } catch (Exception e) { e.printStackTrace(); addReply(sReply, CHAR_BAD, "vpnhelper.rpc.no.connect", new String[] { bindIP + ": " + e.getMessage() }); return false; } finally { AEProxySelector selector = AEProxySelectorFactory.getSelector(); if (selector != null && resolve != null) { for (InetAddress address : resolve) { AEProxySelectorFactory.getSelector().removeProxy(new InetSocketAddress(address, 443)); } } } return true; }
From source file:com.vuze.plugin.azVPN_Helper.Checker_PIA.java
private boolean callRPCforPort(File pathPIAManagerData, InetAddress bindIP, StringBuilder sReply) { InetAddress[] resolve = null; try {//from w w w . ja va 2 s . c o m // Let's assume the client_id.txt file is the one for port forwarding. File fileClientID = new File(pathPIAManagerData, "client_id.txt"); String clientID; if (fileClientID.isFile() && fileClientID.canRead()) { clientID = FileUtil.readFileAsString(fileClientID, -1); } else { clientID = config.getPluginStringParameter("client.id", null); if (clientID == null) { clientID = RandomUtils.generateRandomAlphanumerics(20); config.setPluginParameter("client.id", clientID); } } HttpPost post = new HttpPost(PIA_RPC_URL); String user = config.getPluginStringParameter(PluginConstants.CONFIG_USER); String pass = new String(config.getPluginByteParameter(PluginConstants.CONFIG_P, new byte[0]), "utf-8"); if (user == null || user.length() == 0 || pass == null || pass.length() == 0) { return false; } List<NameValuePair> urlParameters = new ArrayList<NameValuePair>(); urlParameters.add(new BasicNameValuePair("user", user)); urlParameters.add(new BasicNameValuePair("pass", pass)); urlParameters.add(new BasicNameValuePair("client_id", clientID)); urlParameters.add(new BasicNameValuePair("local_ip", bindIP.getHostAddress())); // Call needs to be from the VPN interface (the bindIP) RequestConfig requestConfig = RequestConfig.custom().setLocalAddress(bindIP).setConnectTimeout(15000) .build(); post.setConfig(requestConfig); post.setEntity(new UrlEncodedFormEntity(urlParameters)); CloseableHttpClient httpClient = HttpClients.createDefault(); // If Vuze has a proxy set up (Tools->Options->Connection->Proxy), then // we'll need to disable it for the URL AEProxySelector selector = AEProxySelectorFactory.getSelector(); if (selector != null) { resolve = SystemDefaultDnsResolver.INSTANCE.resolve(VPN_DOMAIN); for (InetAddress address : resolve) { selector.setProxy(new InetSocketAddress(address, 443), Proxy.NO_PROXY); } } CloseableHttpResponse response = httpClient.execute(post); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuffer result = new StringBuffer(); String line = ""; while ((line = rd.readLine()) != null) { result.append(line); } boolean gotPort = false; // should be {"port":xyz} Map<?, ?> mapResult = JSONUtils.decodeJSON(result.toString()); if (mapResult.containsKey("port")) { Object oPort = mapResult.get("port"); if (oPort instanceof Number) { gotPort = true; Number nPort = (Number) oPort; int port = nPort.intValue(); addReply(sReply, CHAR_GOOD, "pia.port.from.rpc", new String[] { Integer.toString(port) }); changePort(port, sReply); } } if (!gotPort) { addReply(sReply, CHAR_WARN, "vpnhelper.rpc.bad", new String[] { result.toString() }); // mapResult.containsKey("error") return false; } } catch (Exception e) { e.printStackTrace(); addReply(sReply, CHAR_BAD, "vpnhelper.rpc.no.connect", new String[] { bindIP + ": " + e.getMessage() }); return false; } finally { AEProxySelector selector = AEProxySelectorFactory.getSelector(); if (selector != null && resolve != null) { for (InetAddress address : resolve) { AEProxySelectorFactory.getSelector().removeProxy(new InetSocketAddress(address, 443)); } } } return true; }
From source file:org.apache.cxf.transport.http.asyncclient.AsyncHTTPConduitFactory.java
public synchronized void setupNIOClient(HTTPClientPolicy clientPolicy) throws IOReactorException { if (client != null) { return;// ww w . j a v a2 s . com } IOReactorConfig config = IOReactorConfig.custom().setIoThreadCount(ioThreadCount) .setSelectInterval(selectInterval).setInterestOpQueued(interestOpQueued).setSoLinger(soLinger) .setSoTimeout(soTimeout).setSoKeepAlive(soKeepalive).setTcpNoDelay(tcpNoDelay).build(); Registry<SchemeIOSessionStrategy> ioSessionFactoryRegistry = RegistryBuilder .<SchemeIOSessionStrategy>create().register("http", NoopIOSessionStrategy.INSTANCE) .register("https", SSLIOSessionStrategy.getSystemDefaultStrategy()).build(); ManagedNHttpClientConnectionFactory connectionFactory = new ManagedNHttpClientConnectionFactory() { @Override public ManagedNHttpClientConnection create(final IOSession iosession, final ConnectionConfig config) { ManagedNHttpClientConnection conn = super.create(iosession, config); return conn; } }; DefaultConnectingIOReactor ioreactor = new DefaultConnectingIOReactor(config); connectionManager = new PoolingNHttpClientConnectionManager(ioreactor, connectionFactory, ioSessionFactoryRegistry, DefaultSchemePortResolver.INSTANCE, SystemDefaultDnsResolver.INSTANCE, connectionTTL, TimeUnit.MILLISECONDS); connectionManager.setDefaultMaxPerRoute(maxPerRoute); connectionManager.setMaxTotal(maxConnections); ConnectionConfig connectionConfig = ConnectionConfig.custom() .setBufferSize(clientPolicy.getChunkLength() > 0 ? clientPolicy.getChunkLength() : 16332).build(); connectionManager.setDefaultConnectionConfig(connectionConfig); RedirectStrategy redirectStrategy = new RedirectStrategy() { public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException { return false; } public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException { return null; } }; HttpAsyncClientBuilder httpAsyncClientBuilder = HttpAsyncClients.custom() .setConnectionManager(connectionManager).setRedirectStrategy(redirectStrategy) .setDefaultCookieStore(new BasicCookieStore() { private static final long serialVersionUID = 1L; public void addCookie(Cookie cookie) { } }); adaptClientBuilder(httpAsyncClientBuilder); client = httpAsyncClientBuilder.build(); // Start the client thread client.start(); if (this.connectionTTL == 0) { //if the connection does not have an expiry deadline //use the ConnectionMaxIdle to close the idle connection new CloseIdleConnectionThread(connectionManager, client).start(); } }
From source file:com.helger.httpclient.HttpClientFactory.java
/** * @return The DNS resolver to be used for * {@link PoolingHttpClientConnectionManager}. May be * <code>null</code> to use the default. * @see #isUseDNSClientCache()/* w w w. ja v a 2 s .c om*/ * @see #setUseDNSClientCache(boolean) * @since 8.8.0 */ @Nullable public DnsResolver createDNSResolver() { // If caching is active, use the default System resolver return m_bUseDNSClientCache ? SystemDefaultDnsResolver.INSTANCE : NonCachingDnsResolver.INSTANCE; }
From source file:com.vuze.plugin.azVPN_Air.Checker.java
private boolean callRPCforPort(InetAddress bindIP, StringBuilder sReply) { InetAddress[] resolve = null; try {/*from ww w.ja v a2 s . co m*/ String user = getDefaultUsername(); String pass = null; if (user == null || user.length() == 0) { user = config.getPluginStringParameter(PluginAir.CONFIG_USER); pass = new String(config.getPluginByteParameter(PluginAir.CONFIG_P, new byte[0]), "utf-8"); } else { pass = getPassword(); } if (user == null || user.length() == 0 || pass == null || pass.length() == 0) { addReply(sReply, CHAR_WARN, "airvpn.rpc.nocreds"); return false; } // If Vuze has a proxy set up (Tools->Options->Connection->Proxy), then // we'll need to disable it for the URL AEProxySelector selector = AEProxySelectorFactory.getSelector(); if (selector != null) { resolve = SystemDefaultDnsResolver.INSTANCE.resolve(VPN_DOMAIN); for (InetAddress address : resolve) { selector.setProxy(new InetSocketAddress(address, 443), Proxy.NO_PROXY); } } RequestConfig requestConfig; StringBuffer token = new StringBuffer(); boolean skipLoginPage = false; boolean alreadyLoggedIn = false; PortInfo[] ports = null; if (httpClientContext == null) { httpClientContext = HttpClientContext.create(); } else { PluginAir.log("Have existing context. Trying to grab port list without logging in."); ports = scrapePorts(bindIP, token); // assume no token means we aren't logged in if (token.length() > 0) { PluginAir.log("Valid ports page. Skipping Login"); skipLoginPage = true; alreadyLoggedIn = true; } else { ports = null; } } if (!skipLoginPage) { String loginURL = null; String authKey = null; PluginAir.log("Getting Login post URL and auth_key"); HttpGet getLoginPage = new HttpGet(VPN_LOGIN_URL); requestConfig = RequestConfig.custom().setLocalAddress(bindIP).setConnectTimeout(10000).build(); getLoginPage.setConfig(requestConfig); CloseableHttpClient httpClientLoginPage = HttpClients.createDefault(); CloseableHttpResponse loginPageResponse = httpClientLoginPage.execute(getLoginPage, httpClientContext); BufferedReader rd = new BufferedReader( new InputStreamReader(loginPageResponse.getEntity().getContent())); Pattern patAuthKey = Pattern.compile(REGEX_AuthKey); String line = ""; while ((line = rd.readLine()) != null) { if (line.contains("<form") && line.matches(".*id=['\"]login['\"].*")) { Matcher matcher = Pattern.compile(REGEX_ActionURL).matcher(line); if (matcher.find()) { loginURL = matcher.group(1); if (authKey != null) { break; } } } Matcher matcherAuthKey = patAuthKey.matcher(line); if (matcherAuthKey.find()) { authKey = matcherAuthKey.group(1); if (loginURL != null) { break; } } if (line.contains("['member_id']") && line.matches(".*parseInt\\s*\\(\\s*[1-9][0-9]*\\s*.*")) { alreadyLoggedIn = true; } } rd.close(); if (loginURL == null) { PluginAir.log("Could not scrape Login URL. Using default"); loginURL = "https://airvpn.org/index.php?app=core&module=global§ion=login&do=process"; } if (authKey == null) { addReply(sReply, CHAR_WARN, "airvpn.rpc.noauthkey"); return false; } loginURL = UrlUtils.unescapeXML(loginURL); /////////////////////////////// if (alreadyLoggedIn) { PluginAir.log("Already Logged In"); } else { PluginAir.log("Login URL:" + loginURL); //https://airvpn.org/index.php?app=core&module=global§ion=login&do=process //https://airvpn.org/index.php?app=core&module=global§ion=login&do=process HttpPost httpPostLogin = new HttpPost(loginURL); requestConfig = RequestConfig.custom().setLocalAddress(bindIP).setConnectTimeout(10000).build(); httpPostLogin.setConfig(requestConfig); CloseableHttpClient httpClient = HttpClients.createDefault(); List<NameValuePair> urlParameters = new ArrayList<NameValuePair>(); urlParameters.add(new BasicNameValuePair("ips_username", user)); urlParameters.add(new BasicNameValuePair("ips_password", pass)); urlParameters.add(new BasicNameValuePair("auth_key", authKey)); urlParameters.add(new BasicNameValuePair("invisible", "1")); urlParameters.add(new BasicNameValuePair("inline_invisible", "1")); httpPostLogin.setEntity(new UrlEncodedFormEntity(urlParameters)); CloseableHttpResponse response = httpClient.execute(httpPostLogin, httpClientContext); rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); line = ""; while ((line = rd.readLine()) != null) { } PluginAir.log("Login Result: " + response.getStatusLine().toString()); } } //////////////////////////// if (ports == null) { ports = scrapePorts(bindIP, token); } PluginAir.log("Found Ports: " + Arrays.toString(ports)); int existingIndex = ourPortInList(ports); if (existingIndex >= 0) { addReply(sReply, CHAR_GOOD, "airvpn.port.from.rpc.match", new String[] { ports[existingIndex].port }); return true; } boolean gotPort = false; // There's a limit of 20 ports. If [0] isn't ours and 20 of them are // created, then assume our detection of "ours" is broke and just use // the first one if ((ports.length > 0 && ports[0].ourBinding) || ports.length == 20) { int port = Integer.parseInt(ports[0].port); gotPort = true; addReply(sReply, CHAR_GOOD, "airvpn.port.from.rpc", new String[] { Integer.toString(port) }); changePort(port, sReply); } else { // create port ports = createPort(bindIP, token); if (ports.length == 0) { // form post should have got the new port, but if it didn't, try // reloading the ports page again. token.setLength(0); ports = scrapePorts(bindIP, token); } PluginAir.log("Added a port. Ports: " + Arrays.toString(ports)); existingIndex = ourPortInList(ports); if (existingIndex >= 0) { addReply(sReply, CHAR_GOOD, "airvpn.port.from.rpc.match", new String[] { ports[existingIndex].port }); return true; } if ((ports.length > 0 && ports[0].ourBinding) || ports.length == 20) { int port = Integer.parseInt(ports[0].port); gotPort = true; addReply(sReply, CHAR_GOOD, "airvpn.port.from.rpc", new String[] { Integer.toString(port) }); changePort(port, sReply); } } if (!gotPort) { addReply(sReply, CHAR_WARN, "airvpn.rpc.no.connect", new String[] { bindIP.toString() }); return false; } } catch (Exception e) { e.printStackTrace(); addReply(sReply, CHAR_BAD, "airvpn.rpc.no.connect", new String[] { bindIP + ": " + e.getMessage() }); return false; } finally { AEProxySelector selector = AEProxySelectorFactory.getSelector(); if (selector != null && resolve != null) { for (InetAddress address : resolve) { AEProxySelectorFactory.getSelector().removeProxy(new InetSocketAddress(address, 443)); } } } return true; }
From source file:com.vuze.plugin.azVPN_PIA.Checker.java
private boolean callRPCforPort(File pathPIAManagerData, InetAddress bindIP, StringBuilder sReply) { InetAddress[] resolve = null; try {//from w w w. j a v a 2 s .c o m // Let's assume the client_id.txt file is the one for port forwarding. File fileClientID = new File(pathPIAManagerData, "client_id.txt"); String clientID; if (fileClientID.isFile() && fileClientID.canRead()) { clientID = FileUtil.readFileAsString(fileClientID, -1); } else { clientID = config.getPluginStringParameter("client.id", null); if (clientID == null) { clientID = RandomUtils.generateRandomAlphanumerics(20); config.setPluginParameter("client.id", clientID); } } HttpPost post = new HttpPost(PIA_RPC_URL); String user = config.getPluginStringParameter(PluginPIA.CONFIG_USER); String pass = new String(config.getPluginByteParameter(PluginPIA.CONFIG_P, new byte[0]), "utf-8"); if (user == null || user.length() == 0 || pass == null || pass.length() == 0) { return false; } List<NameValuePair> urlParameters = new ArrayList<NameValuePair>(); urlParameters.add(new BasicNameValuePair("user", user)); urlParameters.add(new BasicNameValuePair("pass", pass)); urlParameters.add(new BasicNameValuePair("client_id", clientID)); urlParameters.add(new BasicNameValuePair("local_ip", bindIP.getHostAddress())); // Call needs to be from the VPN interface (the bindIP) RequestConfig requestConfig = RequestConfig.custom().setLocalAddress(bindIP).setConnectTimeout(10000) .build(); post.setConfig(requestConfig); post.setEntity(new UrlEncodedFormEntity(urlParameters)); CloseableHttpClient httpClient = HttpClients.createDefault(); // If Vuze has a proxy set up (Tools->Options->Connection->Proxy), then // we'll need to disable it for the URL AEProxySelector selector = AEProxySelectorFactory.getSelector(); if (selector != null) { resolve = SystemDefaultDnsResolver.INSTANCE.resolve(PIA_DOMAIN); for (InetAddress address : resolve) { selector.setProxy(new InetSocketAddress(address, 443), Proxy.NO_PROXY); } } CloseableHttpResponse response = httpClient.execute(post); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuffer result = new StringBuffer(); String line = ""; while ((line = rd.readLine()) != null) { result.append(line); } boolean gotPort = false; // should be {"port":xyz} Map<?, ?> mapResult = JSONUtils.decodeJSON(result.toString()); if (mapResult.containsKey("port")) { Object oPort = mapResult.get("port"); if (oPort instanceof Number) { gotPort = true; Number nPort = (Number) oPort; int port = nPort.intValue(); addReply(sReply, CHAR_GOOD, "pia.port.from.rpc", new String[] { Integer.toString(port) }); changePort(port, sReply); } } if (!gotPort) { addReply(sReply, CHAR_WARN, "pia.rpc.bad", new String[] { result.toString() }); // mapResult.containsKey("error") return false; } } catch (Exception e) { e.printStackTrace(); addReply(sReply, CHAR_BAD, "pia.rpc.no.connect", new String[] { bindIP + ": " + e.getMessage() }); return false; } finally { AEProxySelector selector = AEProxySelectorFactory.getSelector(); if (selector != null && resolve != null) { for (InetAddress address : resolve) { AEProxySelectorFactory.getSelector().removeProxy(new InetSocketAddress(address, 443)); } } } return true; }
From source file:com.vuze.plugin.azVPN_Helper.CheckerCommon.java
protected boolean canReach(InetAddress addressToReach, URI uri) { InetAddress[] resolve = null; try {//ww w .j ava2s . c om String domain = uri.getHost(); // If Vuze has a proxy set up (Tools->Options->Connection->Proxy), then // we'll need to disable it for the URL AEProxySelector selector = AEProxySelectorFactory.getSelector(); if (selector != null) { resolve = SystemDefaultDnsResolver.INSTANCE.resolve(domain); for (InetAddress address : resolve) { selector.setProxy(new InetSocketAddress(address, 443), Proxy.NO_PROXY); } } HttpHead getHead = new HttpHead(uri); RequestConfig requestConfig = RequestConfig.custom().setLocalAddress(addressToReach) .setConnectTimeout(12000).build(); getHead.setConfig(requestConfig); CloseableHttpResponse response = HttpClients.createDefault().execute(getHead); response.close(); } catch (Throwable t) { t.printStackTrace(); return false; } finally { AEProxySelector selector = AEProxySelectorFactory.getSelector(); if (selector != null && resolve != null) { for (InetAddress address : resolve) { AEProxySelectorFactory.getSelector().removeProxy(new InetSocketAddress(address, 443)); } } } return true; }