From af4f2b4510f4cc280841f78c70b087eac3480a04 Mon Sep 17 00:00:00 2001 From: kohlerpop1 Date: Thu, 11 Jan 2024 15:14:11 -0500 Subject: [PATCH] Push for proxy test pt 3! --- .../tiktok/data/settings/HttpClientSettings.java | 3 ++- .../data/settings/ProxyClientSettings.java | 16 +++++++--------- .../tiktok/http/HttpClientFactory.java | 9 ++++++--- .../jwdeveloper/tiktok/http/HttpProxyClient.java | 5 ++++- .../github/jwdeveloper/tiktok/ProxyExample.java | 4 ++-- 5 files changed, 21 insertions(+), 16 deletions(-) diff --git a/API/src/main/java/io/github/jwdeveloper/tiktok/data/settings/HttpClientSettings.java b/API/src/main/java/io/github/jwdeveloper/tiktok/data/settings/HttpClientSettings.java index b693396..db2bac8 100644 --- a/API/src/main/java/io/github/jwdeveloper/tiktok/data/settings/HttpClientSettings.java +++ b/API/src/main/java/io/github/jwdeveloper/tiktok/data/settings/HttpClientSettings.java @@ -46,6 +46,7 @@ public class HttpClientSettings { final Map cookies; @Getter + @Setter ProxyClientSettings proxyClientSettings; @Getter @@ -104,7 +105,7 @@ public class HttpClientSettings { newSettings.getHeaders().putAll(new TreeMap<>(this.headers)); newSettings.getCookies().putAll(new TreeMap<>(this.cookies)); newSettings.getParams().putAll(new TreeMap<>(this.params)); - newSettings.proxyClientSettings = this.proxyClientSettings.clone(); + newSettings.proxyClientSettings = this.proxyClientSettings; return newSettings; } diff --git a/API/src/main/java/io/github/jwdeveloper/tiktok/data/settings/ProxyClientSettings.java b/API/src/main/java/io/github/jwdeveloper/tiktok/data/settings/ProxyClientSettings.java index 72bf7b9..5fef5d1 100644 --- a/API/src/main/java/io/github/jwdeveloper/tiktok/data/settings/ProxyClientSettings.java +++ b/API/src/main/java/io/github/jwdeveloper/tiktok/data/settings/ProxyClientSettings.java @@ -39,8 +39,7 @@ public class ProxyClientSettings implements Iterator private int index = 0; private boolean autoDiscard = true; private Proxy.Type type = Proxy.Type.DIRECT; - private Consumer onProxyUpdated = (x)->{}; - + private Consumer onProxyUpdated = x -> {}; public boolean addProxy(String addressPort) { return proxyList.add(ProxyData.map(addressPort)); @@ -66,19 +65,17 @@ public class ProxyClientSettings implements Iterator @Override public ProxyData next() { - var nextProxy = switch (rotation) + var nextProxy = switch (rotation) { case CONSECUTIVE -> { index = (index+1) % proxyList.size(); - yield proxyList.get(index).clone(); + yield proxyList.get(index).clone(); } case RANDOM -> { index = new Random().nextInt(proxyList.size()); yield proxyList.get(index).clone(); } - default -> { - yield proxyList.get(index).clone(); - } + case NONE -> proxyList.get(index).clone(); }; onProxyUpdated.accept(nextProxy); return nextProxy; @@ -98,13 +95,14 @@ public class ProxyClientSettings implements Iterator this.index = index; } } - - public ProxyClientSettings clone() { + public ProxyClientSettings clone() + { ProxyClientSettings settings = new ProxyClientSettings(); settings.setEnabled(enabled); settings.setRotation(rotation); settings.setIndex(index); settings.setType(type); + settings.setOnProxyUpdated(onProxyUpdated); proxyList.forEach(proxyData -> settings.addProxy(proxyData.getAddress(), proxyData.getPort())); return settings; } diff --git a/Client/src/main/java/io/github/jwdeveloper/tiktok/http/HttpClientFactory.java b/Client/src/main/java/io/github/jwdeveloper/tiktok/http/HttpClientFactory.java index dd90db5..e72b4f3 100644 --- a/Client/src/main/java/io/github/jwdeveloper/tiktok/http/HttpClientFactory.java +++ b/Client/src/main/java/io/github/jwdeveloper/tiktok/http/HttpClientFactory.java @@ -22,7 +22,7 @@ */ package io.github.jwdeveloper.tiktok.http; -import io.github.jwdeveloper.tiktok.data.settings.LiveClientSettings; +import io.github.jwdeveloper.tiktok.data.settings.*; public class HttpClientFactory { private final LiveClientSettings liveClientSettings; @@ -37,6 +37,9 @@ public class HttpClientFactory { //Does not contains default httpClientSettings, Params, headers, etd public HttpClientBuilder clientEmpty(String url) { - return new HttpClientBuilder(url); + + var settings = new HttpClientSettings(); + settings.setProxyClientSettings(liveClientSettings.getHttpSettings().getProxyClientSettings()); + return new HttpClientBuilder(url,settings); } -} +} \ No newline at end of file diff --git a/Client/src/main/java/io/github/jwdeveloper/tiktok/http/HttpProxyClient.java b/Client/src/main/java/io/github/jwdeveloper/tiktok/http/HttpProxyClient.java index 8aa8b8b..e62c263 100644 --- a/Client/src/main/java/io/github/jwdeveloper/tiktok/http/HttpProxyClient.java +++ b/Client/src/main/java/io/github/jwdeveloper/tiktok/http/HttpProxyClient.java @@ -92,8 +92,10 @@ public class HttpProxyClient extends HttpClient while (proxySettings.hasNext()) { try { - Proxy proxy = new Proxy(Proxy.Type.SOCKS, proxySettings.next().toSocketAddress()); + var proxyData = proxySettings.next(); + Proxy proxy = new Proxy(Proxy.Type.SOCKS, proxyData.toSocketAddress()); + System.err.println("Attempting connection to "+ url +" with proxy: "+proxyData); HttpsURLConnection socksConnection = (HttpsURLConnection) url.openConnection(proxy); socksConnection.setSSLSocketFactory(sc.getSocketFactory()); socksConnection.setConnectTimeout(httpClientSettings.getTimeout().toMillisPart()); @@ -113,6 +115,7 @@ public class HttpProxyClient extends HttpClient return Optional.of(response); } catch (SocketException | SocketTimeoutException e) { + e.printStackTrace(); if (proxySettings.isAutoDiscard()) proxySettings.remove(); } catch (Exception e) { diff --git a/Examples/src/main/java/io/github/jwdeveloper/tiktok/ProxyExample.java b/Examples/src/main/java/io/github/jwdeveloper/tiktok/ProxyExample.java index e83d2a6..f3ca064 100644 --- a/Examples/src/main/java/io/github/jwdeveloper/tiktok/ProxyExample.java +++ b/Examples/src/main/java/io/github/jwdeveloper/tiktok/ProxyExample.java @@ -45,11 +45,11 @@ public class ProxyExample { clientSettings.setPrintToConsole(true); clientSettings.getHttpSettings().configureProxy(proxySettings -> { - proxySettings.setType(Proxy.Type.SOCKS); proxySettings.setOnProxyUpdated(proxyData -> { - System.out.println("Next proxy! "+proxyData.toString()); + System.err.println("Next proxy: "+proxyData.toString()); }); + proxySettings.setType(Proxy.Type.SOCKS); entries.forEach(entry -> proxySettings.addProxy(entry.getKey(), entry.getValue())); }); })