mirror of
https://github.com/jwdeveloper/TikTokLiveJava.git
synced 2026-02-28 01:09:40 -05:00
Push for proxy test pt 4!
This commit is contained in:
@@ -33,7 +33,7 @@ import java.util.function.Consumer;
|
|||||||
@Setter
|
@Setter
|
||||||
public class ProxyClientSettings implements Iterator<ProxyData>
|
public class ProxyClientSettings implements Iterator<ProxyData>
|
||||||
{
|
{
|
||||||
private boolean enabled;
|
private boolean enabled, lastSuccess;
|
||||||
private Rotation rotation = Rotation.CONSECUTIVE;
|
private Rotation rotation = Rotation.CONSECUTIVE;
|
||||||
private final List<ProxyData> proxyList = new ArrayList<>();
|
private final List<ProxyData> proxyList = new ArrayList<>();
|
||||||
private int index = 0;
|
private int index = 0;
|
||||||
@@ -65,6 +65,8 @@ public class ProxyClientSettings implements Iterator<ProxyData>
|
|||||||
@Override
|
@Override
|
||||||
public ProxyData next()
|
public ProxyData next()
|
||||||
{
|
{
|
||||||
|
if (lastSuccess)
|
||||||
|
return proxyList.get(index);
|
||||||
var nextProxy = switch (rotation)
|
var nextProxy = switch (rotation)
|
||||||
{
|
{
|
||||||
case CONSECUTIVE -> {
|
case CONSECUTIVE -> {
|
||||||
|
|||||||
@@ -37,7 +37,6 @@ 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) {
|
||||||
|
|
||||||
var settings = new HttpClientSettings();
|
var settings = new HttpClientSettings();
|
||||||
settings.setProxyClientSettings(liveClientSettings.getHttpSettings().getProxyClientSettings());
|
settings.setProxyClientSettings(liveClientSettings.getHttpSettings().getProxyClientSettings());
|
||||||
return new HttpClientBuilder(url, settings);
|
return new HttpClientBuilder(url, settings);
|
||||||
|
|||||||
@@ -66,12 +66,15 @@ public class HttpProxyClient extends HttpClient
|
|||||||
|
|
||||||
var response = client.send(request, HttpResponse.BodyHandlers.ofByteArray());
|
var response = client.send(request, HttpResponse.BodyHandlers.ofByteArray());
|
||||||
if (response.statusCode() != 200) {
|
if (response.statusCode() != 200) {
|
||||||
|
proxySettings.setLastSuccess(false);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
proxySettings.setLastSuccess(true);
|
||||||
return Optional.of(response);
|
return Optional.of(response);
|
||||||
} catch (HttpConnectTimeoutException | ConnectException e) {
|
} catch (HttpConnectTimeoutException | ConnectException e) {
|
||||||
if (proxySettings.isAutoDiscard())
|
if (proxySettings.isAutoDiscard())
|
||||||
proxySettings.remove();
|
proxySettings.remove();
|
||||||
|
proxySettings.setLastSuccess(false);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new TikTokLiveRequestException(e);
|
throw new TikTokLiveRequestException(e);
|
||||||
}
|
}
|
||||||
@@ -92,10 +95,9 @@ public class HttpProxyClient extends HttpClient
|
|||||||
|
|
||||||
while (proxySettings.hasNext()) {
|
while (proxySettings.hasNext()) {
|
||||||
try {
|
try {
|
||||||
var proxyData = proxySettings.next();
|
Proxy proxy = new Proxy(Proxy.Type.SOCKS, proxySettings.next().toSocketAddress());
|
||||||
Proxy proxy = new Proxy(Proxy.Type.SOCKS, proxyData.toSocketAddress());
|
|
||||||
|
|
||||||
System.err.println("Attempting connection to "+ url +" with proxy: "+proxyData);
|
System.err.println("Connecting to "+ url);
|
||||||
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,11 +115,13 @@ public class HttpProxyClient extends HttpClient
|
|||||||
|
|
||||||
var response = createHttpResponse(body, toUrl(), responseInfo);
|
var response = createHttpResponse(body, toUrl(), responseInfo);
|
||||||
|
|
||||||
|
proxySettings.setLastSuccess(true);
|
||||||
return Optional.of(response);
|
return Optional.of(response);
|
||||||
} catch (SocketException | SocketTimeoutException e) {
|
} catch (SocketException | SocketTimeoutException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
if (proxySettings.isAutoDiscard())
|
if (proxySettings.isAutoDiscard())
|
||||||
proxySettings.remove();
|
proxySettings.remove();
|
||||||
|
proxySettings.setLastSuccess(false);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new TikTokLiveRequestException(e);
|
throw new TikTokLiveRequestException(e);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,9 @@ import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveException;
|
|||||||
import io.github.jwdeveloper.tiktok.live.LiveClient;
|
import io.github.jwdeveloper.tiktok.live.LiveClient;
|
||||||
import org.java_websocket.client.WebSocketClient;
|
import org.java_websocket.client.WebSocketClient;
|
||||||
|
|
||||||
|
import javax.net.ssl.*;
|
||||||
import java.net.Proxy;
|
import java.net.Proxy;
|
||||||
|
import java.security.cert.X509Certificate;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
public class TikTokWebSocketClient implements SocketClient {
|
public class TikTokWebSocketClient implements SocketClient {
|
||||||
@@ -68,11 +70,11 @@ public class TikTokWebSocketClient implements SocketClient {
|
|||||||
tikTokEventHandler,
|
tikTokEventHandler,
|
||||||
liveClient);
|
liveClient);
|
||||||
|
|
||||||
ProxyClientSettings proxyClientSettings = clientSettings.getHttpSettings().getProxyClientSettings();
|
// ProxyClientSettings proxyClientSettings = clientSettings.getHttpSettings().getProxyClientSettings();
|
||||||
|
//
|
||||||
if (proxyClientSettings.isEnabled())
|
// if (proxyClientSettings.isEnabled())
|
||||||
connectProxy(proxyClientSettings);
|
// connectProxy(proxyClientSettings);
|
||||||
else
|
// else
|
||||||
connectDefault();
|
connectDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,6 +107,15 @@ public class TikTokWebSocketClient implements SocketClient {
|
|||||||
public boolean tryProxyConnection(ProxyClientSettings proxySettings, ProxyData proxyData) {
|
public boolean tryProxyConnection(ProxyClientSettings proxySettings, ProxyData proxyData) {
|
||||||
webSocketClient.setProxy(new Proxy(proxySettings.getType(), proxyData.toSocketAddress()));
|
webSocketClient.setProxy(new Proxy(proxySettings.getType(), proxyData.toSocketAddress()));
|
||||||
try {
|
try {
|
||||||
|
if (proxySettings.getType() == Proxy.Type.SOCKS) {
|
||||||
|
SSLContext sc = SSLContext.getInstance("SSL");
|
||||||
|
sc.init(null, new TrustManager[]{new X509TrustManager() {
|
||||||
|
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) {}
|
||||||
|
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) {}
|
||||||
|
public X509Certificate[] getAcceptedIssuers() { return null; }
|
||||||
|
}}, null);
|
||||||
|
webSocketClient.setSocketFactory(sc.getSocketFactory());
|
||||||
|
}
|
||||||
webSocketClient.connect();
|
webSocketClient.connect();
|
||||||
return true;
|
return true;
|
||||||
} catch (Exception e)
|
} catch (Exception e)
|
||||||
@@ -114,7 +125,7 @@ public class TikTokWebSocketClient implements SocketClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void stop() {
|
public void stop() {
|
||||||
if (isConnected && webSocketClient != null) {
|
if (isConnected && webSocketClient != null && webSocketClient.isOpen()) {
|
||||||
webSocketClient.closeConnection(0, "");
|
webSocketClient.closeConnection(0, "");
|
||||||
}
|
}
|
||||||
webSocketClient = null;
|
webSocketClient = null;
|
||||||
|
|||||||
@@ -23,23 +23,18 @@
|
|||||||
package io.github.jwdeveloper.tiktok.websocket;
|
package io.github.jwdeveloper.tiktok.websocket;
|
||||||
|
|
||||||
import com.google.protobuf.ByteString;
|
import com.google.protobuf.ByteString;
|
||||||
import io.github.jwdeveloper.tiktok.data.events.TikTokConnectedEvent;
|
import io.github.jwdeveloper.tiktok.*;
|
||||||
import io.github.jwdeveloper.tiktok.data.events.TikTokDisconnectedEvent;
|
import io.github.jwdeveloper.tiktok.data.events.*;
|
||||||
import io.github.jwdeveloper.tiktok.data.events.TikTokErrorEvent;
|
|
||||||
import io.github.jwdeveloper.tiktok.exceptions.TikTokProtocolBufferException;
|
import io.github.jwdeveloper.tiktok.exceptions.TikTokProtocolBufferException;
|
||||||
import io.github.jwdeveloper.tiktok.TikTokLiveEventHandler;
|
|
||||||
import io.github.jwdeveloper.tiktok.TikTokLiveMessageHandler;
|
|
||||||
import io.github.jwdeveloper.tiktok.live.LiveClient;
|
import io.github.jwdeveloper.tiktok.live.LiveClient;
|
||||||
import io.github.jwdeveloper.tiktok.messages.webcast.WebcastPushFrame;
|
import io.github.jwdeveloper.tiktok.messages.webcast.*;
|
||||||
import io.github.jwdeveloper.tiktok.messages.webcast.WebcastResponse;
|
|
||||||
import org.java_websocket.client.WebSocketClient;
|
import org.java_websocket.client.WebSocketClient;
|
||||||
import org.java_websocket.drafts.Draft_6455;
|
import org.java_websocket.drafts.Draft_6455;
|
||||||
import org.java_websocket.handshake.ServerHandshake;
|
import org.java_websocket.handshake.ServerHandshake;
|
||||||
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.util.Map;
|
import java.util.*;
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
public class TikTokWebSocketListener extends WebSocketClient {
|
public class TikTokWebSocketListener extends WebSocketClient {
|
||||||
|
|
||||||
@@ -103,8 +98,9 @@ public class TikTokWebSocketListener extends WebSocketClient {
|
|||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onClose(int i, String s, boolean b) {
|
public void onClose(int code, String reason, boolean remote) {
|
||||||
tikTokEventHandler.publish(tikTokLiveClient, new TikTokDisconnectedEvent());
|
tikTokEventHandler.publish(tikTokLiveClient, new TikTokDisconnectedEvent());
|
||||||
|
tikTokLiveClient.disconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -144,6 +140,6 @@ public class TikTokWebSocketListener extends WebSocketClient {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onMessage(String s) {
|
public void onMessage(String s) {
|
||||||
|
System.err.println(s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -40,7 +40,7 @@ public class ProxyExample
|
|||||||
return new AbstractMap.SimpleEntry<>(split[0], Integer.parseInt(split[1]));
|
return new AbstractMap.SimpleEntry<>(split[0], Integer.parseInt(split[1]));
|
||||||
}).toList());
|
}).toList());
|
||||||
|
|
||||||
TikTokLive.newClient("boost_grow_live_qc")
|
TikTokLive.newClient("dash4214")
|
||||||
.configure(clientSettings ->
|
.configure(clientSettings ->
|
||||||
{
|
{
|
||||||
clientSettings.setPrintToConsole(true);
|
clientSettings.setPrintToConsole(true);
|
||||||
@@ -53,14 +53,23 @@ public class ProxyExample
|
|||||||
entries.forEach(entry -> proxySettings.addProxy(entry.getKey(), entry.getValue()));
|
entries.forEach(entry -> proxySettings.addProxy(entry.getKey(), entry.getValue()));
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
|
.onComment((liveClient, event) -> {
|
||||||
|
liveClient.getLogger().info(event.getUser().getName()+": "+event.getText());
|
||||||
|
})
|
||||||
.onConnected((liveClient, event) ->
|
.onConnected((liveClient, event) ->
|
||||||
{
|
{
|
||||||
liveClient.getLogger().info("Hello world!");
|
liveClient.getLogger().info("Hello world!");
|
||||||
})
|
})
|
||||||
|
.onDisconnected((liveClient, event) ->
|
||||||
|
{
|
||||||
|
liveClient.getLogger().info("Goodbye world!");
|
||||||
|
})
|
||||||
.onError((liveClient, event) ->
|
.onError((liveClient, event) ->
|
||||||
{
|
{
|
||||||
event.getException().printStackTrace();
|
event.getException().printStackTrace();
|
||||||
})
|
})
|
||||||
.buildAndConnect();
|
.buildAndConnect();
|
||||||
|
|
||||||
|
System.in.read();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user