From b1c7de25c9ce6b9ced6a659539bb2dab68c09866 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 24 Oct 2015 10:34:09 +0200 Subject: [PATCH] Use proxy to access The Pirate Bay --- .../pterodactylus/rhynodge/queries/HttpQuery.java | 24 ++++++++++++++-------- .../rhynodge/watchers/PirateBayEpisodeWatcher.java | 23 +++++++++++---------- .../rhynodge/watchers/PirateBayWatcher.java | 16 +++++++++++---- 3 files changed, 39 insertions(+), 24 deletions(-) diff --git a/src/main/java/net/pterodactylus/rhynodge/queries/HttpQuery.java b/src/main/java/net/pterodactylus/rhynodge/queries/HttpQuery.java index e969087..c1c315f 100644 --- a/src/main/java/net/pterodactylus/rhynodge/queries/HttpQuery.java +++ b/src/main/java/net/pterodactylus/rhynodge/queries/HttpQuery.java @@ -26,6 +26,7 @@ import net.pterodactylus.rhynodge.states.FailedState; import net.pterodactylus.rhynodge.states.HttpState; import org.apache.http.HttpEntity; +import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.HttpClient; @@ -42,17 +43,18 @@ import org.apache.http.util.EntityUtils; */ public class HttpQuery implements Query { - /** The uri to request. */ private final String uri; + private final String proxyHost; + private final int proxyPort; - /** - * Creates a new HTTP query. - * - * @param uri - * The uri to request - */ public HttpQuery(String uri) { + this(uri, null, -1); + } + + public HttpQuery(String uri, String proxyHost, int proxyPort) { this.uri = uri; + this.proxyHost = proxyHost; + this.proxyPort = proxyPort; } // @@ -65,9 +67,13 @@ public class HttpQuery implements Query { @Override @SuppressWarnings("deprecation") public State state() { - HttpClient httpClient = HttpClientBuilder.create() + HttpClientBuilder httpClientBuilder = HttpClientBuilder.create() .setSSLHostnameVerifier((hostname, session) -> true) - .addInterceptorFirst(new ResponseContentEncoding()).build(); + .addInterceptorFirst(new ResponseContentEncoding()); + if ((proxyHost != null) && (proxyPort != -1)) { + httpClientBuilder.setProxy(new HttpHost(proxyHost, proxyPort)); + } + HttpClient httpClient = httpClientBuilder.build(); HttpGet get = new HttpGet(uri); try { diff --git a/src/main/java/net/pterodactylus/rhynodge/watchers/PirateBayEpisodeWatcher.java b/src/main/java/net/pterodactylus/rhynodge/watchers/PirateBayEpisodeWatcher.java index 6f8507c..9ef3d79 100644 --- a/src/main/java/net/pterodactylus/rhynodge/watchers/PirateBayEpisodeWatcher.java +++ b/src/main/java/net/pterodactylus/rhynodge/watchers/PirateBayEpisodeWatcher.java @@ -48,24 +48,25 @@ public class PirateBayEpisodeWatcher extends DefaultWatcher { * @param searchTerms * The terms to search for */ - public PirateBayEpisodeWatcher(String searchTerms) { - super(createHttpQuery(searchTerms), createFilters(), createTrigger()); + public PirateBayEpisodeWatcher(String searchTerms, String proxy) { + super(createHttpQuery(searchTerms, extractProxyHost(proxy), extractProxyPort(proxy)), createFilters(), createTrigger()); + } + + private static String extractProxyHost(String proxy) { + return proxy.split(":")[0]; + } + + private static int extractProxyPort(String proxy) { + return Integer.valueOf(proxy.split(":")[1]); } // // STATIC METHODS // - /** - * Creates the query of the watcher. - * - * @param searchTerms - * The search terms of the query - * @return The query of the watcher - */ - private static Query createHttpQuery(String searchTerms) { + private static Query createHttpQuery(String searchTerms, String proxyHost, int proxyPort) { try { - return new HttpQuery("http://thepiratebay.org/search/" + URLEncoder.encode(searchTerms, "UTF-8") + "/0/3/0"); + return new HttpQuery("http://thepiratebay.org/search/" + URLEncoder.encode(searchTerms, "UTF-8") + "/0/3/0", proxyHost, proxyPort); } catch (UnsupportedEncodingException uee1) { /* will not happen. */ return null; diff --git a/src/main/java/net/pterodactylus/rhynodge/watchers/PirateBayWatcher.java b/src/main/java/net/pterodactylus/rhynodge/watchers/PirateBayWatcher.java index a8cad78..b99d6e5 100644 --- a/src/main/java/net/pterodactylus/rhynodge/watchers/PirateBayWatcher.java +++ b/src/main/java/net/pterodactylus/rhynodge/watchers/PirateBayWatcher.java @@ -47,8 +47,16 @@ public class PirateBayWatcher extends DefaultWatcher { * @param searchTerms * The terms to search for */ - public PirateBayWatcher(String searchTerms) { - super(createHttpQuery(searchTerms), createFilters(), createTrigger()); + public PirateBayWatcher(String searchTerms, String proxy) { + super(createHttpQuery(searchTerms, extractProxyHost(proxy), extractProxyPort(proxy)), createFilters(), createTrigger()); + } + + private static String extractProxyHost(String proxy) { + return proxy.split(":")[0]; + } + + private static int extractProxyPort(String proxy) { + return Integer.valueOf(proxy.split(":")[1]); } // @@ -62,9 +70,9 @@ public class PirateBayWatcher extends DefaultWatcher { * The search terms of the query * @return The query of the watcher */ - private static Query createHttpQuery(String searchTerms) { + private static Query createHttpQuery(String searchTerms, String proxyHost, int proxyPort) { try { - return new HttpQuery("http://thepiratebay.org/search/" + URLEncoder.encode(searchTerms, "UTF-8") + "/0/3/0"); + return new HttpQuery("http://thepiratebay.org/search/" + URLEncoder.encode(searchTerms, "UTF-8") + "/0/3/0", proxyHost, proxyPort); } catch (UnsupportedEncodingException uee1) { /* will not happen. */ return null; -- 2.7.4