Push for proxy test pt 3!

This commit is contained in:
kohlerpop1
2024-01-11 15:14:11 -05:00
committed by Jacek W
parent 2c12b71e99
commit af4f2b4510
5 changed files with 21 additions and 16 deletions

View File

@@ -46,6 +46,7 @@ public class HttpClientSettings {
final Map<String, String> cookies; final Map<String, String> cookies;
@Getter @Getter
@Setter
ProxyClientSettings proxyClientSettings; ProxyClientSettings proxyClientSettings;
@Getter @Getter
@@ -104,7 +105,7 @@ public class HttpClientSettings {
newSettings.getHeaders().putAll(new TreeMap<>(this.headers)); newSettings.getHeaders().putAll(new TreeMap<>(this.headers));
newSettings.getCookies().putAll(new TreeMap<>(this.cookies)); newSettings.getCookies().putAll(new TreeMap<>(this.cookies));
newSettings.getParams().putAll(new TreeMap<>(this.params)); newSettings.getParams().putAll(new TreeMap<>(this.params));
newSettings.proxyClientSettings = this.proxyClientSettings.clone(); newSettings.proxyClientSettings = this.proxyClientSettings;
return newSettings; return newSettings;
} }

View File

@@ -39,8 +39,7 @@ public class ProxyClientSettings implements Iterator<ProxyData>
private int index = 0; private int index = 0;
private boolean autoDiscard = true; private boolean autoDiscard = true;
private Proxy.Type type = Proxy.Type.DIRECT; private Proxy.Type type = Proxy.Type.DIRECT;
private Consumer<ProxyData> onProxyUpdated = (x)->{}; private Consumer<ProxyData> onProxyUpdated = x -> {};
public boolean addProxy(String addressPort) { public boolean addProxy(String addressPort) {
return proxyList.add(ProxyData.map(addressPort)); return proxyList.add(ProxyData.map(addressPort));
@@ -66,19 +65,17 @@ public class ProxyClientSettings implements Iterator<ProxyData>
@Override @Override
public ProxyData next() public ProxyData next()
{ {
var nextProxy = switch (rotation) var nextProxy = switch (rotation)
{ {
case CONSECUTIVE -> { case CONSECUTIVE -> {
index = (index+1) % proxyList.size(); index = (index+1) % proxyList.size();
yield proxyList.get(index).clone(); yield proxyList.get(index).clone();
} }
case RANDOM -> { case RANDOM -> {
index = new Random().nextInt(proxyList.size()); index = new Random().nextInt(proxyList.size());
yield proxyList.get(index).clone(); yield proxyList.get(index).clone();
} }
default -> { case NONE -> proxyList.get(index).clone();
yield proxyList.get(index).clone();
}
}; };
onProxyUpdated.accept(nextProxy); onProxyUpdated.accept(nextProxy);
return nextProxy; return nextProxy;
@@ -98,13 +95,14 @@ public class ProxyClientSettings implements Iterator<ProxyData>
this.index = index; this.index = index;
} }
} }
public ProxyClientSettings clone()
public ProxyClientSettings clone() { {
ProxyClientSettings settings = new ProxyClientSettings(); ProxyClientSettings settings = new ProxyClientSettings();
settings.setEnabled(enabled); settings.setEnabled(enabled);
settings.setRotation(rotation); settings.setRotation(rotation);
settings.setIndex(index); settings.setIndex(index);
settings.setType(type); settings.setType(type);
settings.setOnProxyUpdated(onProxyUpdated);
proxyList.forEach(proxyData -> settings.addProxy(proxyData.getAddress(), proxyData.getPort())); proxyList.forEach(proxyData -> settings.addProxy(proxyData.getAddress(), proxyData.getPort()));
return settings; return settings;
} }

View File

@@ -22,7 +22,7 @@
*/ */
package io.github.jwdeveloper.tiktok.http; package io.github.jwdeveloper.tiktok.http;
import io.github.jwdeveloper.tiktok.data.settings.LiveClientSettings; import io.github.jwdeveloper.tiktok.data.settings.*;
public class HttpClientFactory { public class HttpClientFactory {
private final LiveClientSettings liveClientSettings; private final LiveClientSettings liveClientSettings;
@@ -37,6 +37,9 @@ public class HttpClientFactory {
//Does not contains default httpClientSettings, Params, headers, etd //Does not contains default httpClientSettings, Params, headers, etd
public HttpClientBuilder clientEmpty(String url) { public HttpClientBuilder clientEmpty(String url) {
return new HttpClientBuilder(url);
var settings = new HttpClientSettings();
settings.setProxyClientSettings(liveClientSettings.getHttpSettings().getProxyClientSettings());
return new HttpClientBuilder(url,settings);
} }
} }

View File

@@ -92,8 +92,10 @@ public class HttpProxyClient extends HttpClient
while (proxySettings.hasNext()) { while (proxySettings.hasNext()) {
try { 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); HttpsURLConnection socksConnection = (HttpsURLConnection) url.openConnection(proxy);
socksConnection.setSSLSocketFactory(sc.getSocketFactory()); socksConnection.setSSLSocketFactory(sc.getSocketFactory());
socksConnection.setConnectTimeout(httpClientSettings.getTimeout().toMillisPart()); socksConnection.setConnectTimeout(httpClientSettings.getTimeout().toMillisPart());
@@ -113,6 +115,7 @@ public class HttpProxyClient extends HttpClient
return Optional.of(response); return Optional.of(response);
} catch (SocketException | SocketTimeoutException e) { } catch (SocketException | SocketTimeoutException e) {
e.printStackTrace();
if (proxySettings.isAutoDiscard()) if (proxySettings.isAutoDiscard())
proxySettings.remove(); proxySettings.remove();
} catch (Exception e) { } catch (Exception e) {

View File

@@ -45,11 +45,11 @@ public class ProxyExample
{ {
clientSettings.setPrintToConsole(true); clientSettings.setPrintToConsole(true);
clientSettings.getHttpSettings().configureProxy(proxySettings -> { clientSettings.getHttpSettings().configureProxy(proxySettings -> {
proxySettings.setType(Proxy.Type.SOCKS);
proxySettings.setOnProxyUpdated(proxyData -> 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())); entries.forEach(entry -> proxySettings.addProxy(entry.getKey(), entry.getValue()));
}); });
}) })