mirror of
https://github.com/jwdeveloper/TikTokLiveJava.git
synced 2026-02-27 16:59:39 -05:00
Push for proxy test pt 3!
This commit is contained in:
@@ -46,6 +46,7 @@ public class HttpClientSettings {
|
||||
final Map<String, String> 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;
|
||||
}
|
||||
|
||||
@@ -39,8 +39,7 @@ public class ProxyClientSettings implements Iterator<ProxyData>
|
||||
private int index = 0;
|
||||
private boolean autoDiscard = true;
|
||||
private Proxy.Type type = Proxy.Type.DIRECT;
|
||||
private Consumer<ProxyData> onProxyUpdated = (x)->{};
|
||||
|
||||
private Consumer<ProxyData> onProxyUpdated = x -> {};
|
||||
|
||||
public boolean addProxy(String addressPort) {
|
||||
return proxyList.add(ProxyData.map(addressPort));
|
||||
@@ -66,19 +65,17 @@ public class ProxyClientSettings implements Iterator<ProxyData>
|
||||
@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<ProxyData>
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
@@ -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()));
|
||||
});
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user