From 39c3b5b1bcae1695be9fdbf8854fcee6a2411fcd Mon Sep 17 00:00:00 2001 From: kohlerpop1 Date: Sun, 14 Jan 2024 14:52:54 -0500 Subject: [PATCH] Final Proxy Commit! --- .../data/events/TikTokDisconnectedEvent.java | 12 +++- .../tiktok/data/requests/LiveUserData.java | 22 +++--- .../data/settings/ProxyClientSettings.java | 9 +-- .../TikTokProxyRequestException.java | 49 +++++++++++++ .../github/jwdeveloper/tiktok/TikTokLive.java | 7 +- .../jwdeveloper/tiktok/TikTokLiveClient.java | 20 ++---- .../tiktok/TikTokLiveClientBuilder.java | 6 +- .../tiktok/TikTokLiveHttpClient.java | 69 +++++++++++++++++-- .../tiktok/http/HttpProxyClient.java | 15 ++-- .../http/mappers/LiveUserDataMapper.java | 11 ++- .../websocket/TikTokWebSocketClient.java | 2 +- .../websocket/TikTokWebSocketListener.java | 11 +-- .../jwdeveloper/tiktok/ProxyExample.java | 45 +++--------- 13 files changed, 173 insertions(+), 105 deletions(-) create mode 100644 API/src/main/java/io/github/jwdeveloper/tiktok/exceptions/TikTokProxyRequestException.java diff --git a/API/src/main/java/io/github/jwdeveloper/tiktok/data/events/TikTokDisconnectedEvent.java b/API/src/main/java/io/github/jwdeveloper/tiktok/data/events/TikTokDisconnectedEvent.java index 4565b7f..1d597e3 100644 --- a/API/src/main/java/io/github/jwdeveloper/tiktok/data/events/TikTokDisconnectedEvent.java +++ b/API/src/main/java/io/github/jwdeveloper/tiktok/data/events/TikTokDisconnectedEvent.java @@ -24,7 +24,7 @@ package io.github.jwdeveloper.tiktok.data.events; import io.github.jwdeveloper.tiktok.annotations.EventMeta; import io.github.jwdeveloper.tiktok.annotations.EventType; import io.github.jwdeveloper.tiktok.data.events.common.TikTokLiveClientEvent; - +import lombok.Getter; /** * Triggered when the connection gets disconnected. In that case you can call connect() again to have a reconnect logic. @@ -32,4 +32,12 @@ import io.github.jwdeveloper.tiktok.data.events.common.TikTokLiveClientEvent; */ @EventMeta(eventType = EventType.Control) public class TikTokDisconnectedEvent extends TikTokLiveClientEvent { -} + @Getter private final String reason; + public TikTokDisconnectedEvent(String reason) { + this.reason = reason.isBlank() ? "None" : reason; + } + + public TikTokDisconnectedEvent() { + this("None"); + } +} \ No newline at end of file diff --git a/API/src/main/java/io/github/jwdeveloper/tiktok/data/requests/LiveUserData.java b/API/src/main/java/io/github/jwdeveloper/tiktok/data/requests/LiveUserData.java index ee002c2..ce20ce8 100644 --- a/API/src/main/java/io/github/jwdeveloper/tiktok/data/requests/LiveUserData.java +++ b/API/src/main/java/io/github/jwdeveloper/tiktok/data/requests/LiveUserData.java @@ -22,10 +22,7 @@ */ package io.github.jwdeveloper.tiktok.data.requests; -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.Getter; -import lombok.Setter; +import lombok.*; public class LiveUserData { @@ -38,15 +35,18 @@ public class LiveUserData { @Getter @AllArgsConstructor public static class Response { - private String json; - private UserStatus userStatus; - private String roomId; - - private long startedAtTimeStamp; + + public boolean isLiveOnline() { + return userStatus == LiveUserData.UserStatus.Live || userStatus == LiveUserData.UserStatus.LivePaused; + } + + public boolean isHostNameValid() { + return userStatus != LiveUserData.UserStatus.NotFound; + } } public enum UserStatus { @@ -55,6 +55,4 @@ public class LiveUserData { LivePaused, Live, } -} - - +} \ No newline at end of file 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 0832b02..6354acb 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 @@ -63,10 +63,11 @@ public class ProxyClientSettings implements Iterator } @Override - public ProxyData next() - { - if (lastSuccess) - return proxyList.get(index); + public ProxyData next() { + return lastSuccess ? proxyList.get(index) : rotate(); + } + + public ProxyData rotate() { var nextProxy = switch (rotation) { case CONSECUTIVE -> { diff --git a/API/src/main/java/io/github/jwdeveloper/tiktok/exceptions/TikTokProxyRequestException.java b/API/src/main/java/io/github/jwdeveloper/tiktok/exceptions/TikTokProxyRequestException.java new file mode 100644 index 0000000..560c4fa --- /dev/null +++ b/API/src/main/java/io/github/jwdeveloper/tiktok/exceptions/TikTokProxyRequestException.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2023-2023 jwdeveloper jacekwoln@gmail.com + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package io.github.jwdeveloper.tiktok.exceptions; + + +/* + * Happens while bad response from http proxy request to TikTok + */ +public class TikTokProxyRequestException extends TikTokLiveException +{ + public TikTokProxyRequestException() { + } + + public TikTokProxyRequestException(String message) { + super(message); + } + + public TikTokProxyRequestException(String message, Throwable cause) { + super(message, cause); + } + + public TikTokProxyRequestException(Throwable cause) { + super(cause); + } + + public TikTokProxyRequestException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} \ No newline at end of file diff --git a/Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLive.java b/Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLive.java index 9eea3f2..d224319 100644 --- a/Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLive.java +++ b/Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLive.java @@ -23,7 +23,6 @@ package io.github.jwdeveloper.tiktok; -import io.github.jwdeveloper.tiktok.data.requests.LiveUserData; import io.github.jwdeveloper.tiktok.http.LiveHttpClient; import io.github.jwdeveloper.tiktok.live.builder.LiveClientBuilder; @@ -48,8 +47,7 @@ public class TikTokLive { */ public static boolean isLiveOnline(String hostName) { - LiveUserData.UserStatus status = requests().fetchLiveUserData(hostName).getUserStatus(); - return status == LiveUserData.UserStatus.Live || status == LiveUserData.UserStatus.LivePaused; + return requests().fetchLiveUserData(hostName).isLiveOnline(); } @@ -72,8 +70,7 @@ public class TikTokLive { */ public static boolean isHostNameValid(String hostName) { - LiveUserData.UserStatus status = requests().fetchLiveUserData(hostName).getUserStatus(); - return status != LiveUserData.UserStatus.NotFound; + return requests().fetchLiveUserData(hostName).isHostNameValid(); } /** diff --git a/Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLiveClient.java b/Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLiveClient.java index 19d0065..81709a0 100644 --- a/Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLiveClient.java +++ b/Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLiveClient.java @@ -76,19 +76,15 @@ public class TikTokLiveClient implements LiveClient { public void connectAsync(Consumer onConnection) { - CompletableFuture.supplyAsync(() -> - { + CompletableFuture.runAsync(() -> { connect(); onConnection.accept(this); - return this; }); - } public CompletableFuture connectAsync() { - return CompletableFuture.supplyAsync(() -> - { + return CompletableFuture.supplyAsync(() -> { connect(); return this; }); @@ -105,8 +101,7 @@ public class TikTokLiveClient implements LiveClient { if (e instanceof TikTokLiveOfflineHostException && clientSettings.isRetryOnConnectionFailure()) { try { Thread.sleep(clientSettings.getRetryConnectionTimeout().toMillis()); - } catch (Exception ignored) { - } + } catch (Exception ignored) {} logger.info("Reconnecting"); tikTokEventHandler.publish(this, new TikTokReconnectingEvent()); this.connect(); @@ -120,14 +115,12 @@ public class TikTokLiveClient implements LiveClient { } public void tryConnect() { - if (!liveRoomInfo.hasConnectionState(ConnectionState.DISCONNECTED)) - { + if (!liveRoomInfo.hasConnectionState(ConnectionState.DISCONNECTED)) { throw new TikTokLiveException("Already connected"); } setState(ConnectionState.CONNECTING); - var userDataRequest = new LiveUserData.Request(liveRoomInfo.getHostName()); var userData = httpClient.fetchLiveUserData(userDataRequest); liveRoomInfo.setStartTime(userData.getStartedAtTimeStamp()); @@ -139,7 +132,6 @@ public class TikTokLiveClient implements LiveClient { throw new TikTokLiveOfflineHostException("User not found: "+liveRoomInfo.getHostUser()); } - var liveDataRequest = new LiveData.Request(userData.getRoomId()); var liveData = httpClient.fetchLiveData(liveDataRequest); if (liveData.getLiveStatus() == LiveData.LiveStatus.HostNotFound) { @@ -155,7 +147,6 @@ public class TikTokLiveClient implements LiveClient { liveRoomInfo.setAgeRestricted(liveData.isAgeRestricted()); liveRoomInfo.setHost(liveData.getHost()); - var liveConnectionRequest =new LiveConnectionData.Request(userData.getRoomId()); var liveConnectionData = httpClient.fetchLiveConnectionData(liveConnectionRequest); webSocketClient.start(liveConnectionData, this); @@ -181,7 +172,6 @@ public class TikTokLiveClient implements LiveClient { tikTokEventHandler.publish(this, event); } - public LiveRoomInfo getRoomInfo() { return liveRoomInfo; } @@ -200,6 +190,4 @@ public class TikTokLiveClient implements LiveClient { public GiftManager getGiftManager() { return tikTokGiftManager; } - - } \ No newline at end of file diff --git a/Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLiveClientBuilder.java b/Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLiveClientBuilder.java index fb25ce6..b431ab5 100644 --- a/Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLiveClientBuilder.java +++ b/Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLiveClientBuilder.java @@ -156,7 +156,7 @@ public class TikTokLiveClientBuilder implements LiveClientBuilder { var httpClientFactory = new HttpClientFactory(clientSettings); - var tikTokLiveHttpClient = new TikTokLiveHttpClient(httpClientFactory); + var tikTokLiveHttpClient = new TikTokLiveHttpClient(httpClientFactory, clientSettings); var webSocketClient = new TikTokWebSocketClient( clientSettings, @@ -346,7 +346,7 @@ public class TikTokLiveClientBuilder implements LiveClientBuilder { @Override - public LiveClientBuilder onRoomInfo(EventConsumer event) { + public TikTokLiveClientBuilder onRoomInfo(EventConsumer event) { tikTokEventHandler.subscribe(TikTokRoomInfoEvent.class, event); return this; } @@ -358,7 +358,7 @@ public class TikTokLiveClientBuilder implements LiveClientBuilder { } @Override - public LiveClientBuilder onLiveUnpaused(EventConsumer event) { + public TikTokLiveClientBuilder onLiveUnpaused(EventConsumer event) { tikTokEventHandler.subscribe(TikTokLiveUnpausedEvent.class, event); return this; } diff --git a/Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLiveHttpClient.java b/Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLiveHttpClient.java index 40a41c5..b694b05 100644 --- a/Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLiveHttpClient.java +++ b/Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLiveHttpClient.java @@ -24,13 +24,14 @@ package io.github.jwdeveloper.tiktok; import com.google.protobuf.InvalidProtocolBufferException; import io.github.jwdeveloper.tiktok.data.requests.*; -import io.github.jwdeveloper.tiktok.data.settings.LiveClientSettings; +import io.github.jwdeveloper.tiktok.data.settings.*; import io.github.jwdeveloper.tiktok.exceptions.*; import io.github.jwdeveloper.tiktok.http.*; import io.github.jwdeveloper.tiktok.http.mappers.*; import io.github.jwdeveloper.tiktok.messages.webcast.WebcastResponse; import java.net.http.HttpResponse; +import java.util.Optional; public class TikTokLiveHttpClient implements LiveHttpClient { @@ -43,13 +44,15 @@ public class TikTokLiveHttpClient implements LiveHttpClient { private static final String TIKTOK_URL_WEBCAST = "https://webcast.tiktok.com/webcast/"; private final HttpClientFactory httpFactory; + private final LiveClientSettings clientSettings; private final LiveUserDataMapper liveUserDataMapper; private final LiveDataMapper liveDataMapper; private final SignServerResponseMapper signServerResponseMapper; private final GiftsDataMapper giftsDataMapper; - public TikTokLiveHttpClient(HttpClientFactory factory) { + public TikTokLiveHttpClient(HttpClientFactory factory, LiveClientSettings settings) { this.httpFactory = factory; + clientSettings = settings; liveUserDataMapper = new LiveUserDataMapper(); liveDataMapper = new LiveDataMapper(); signServerResponseMapper = new SignServerResponseMapper(); @@ -57,7 +60,7 @@ public class TikTokLiveHttpClient implements LiveHttpClient { } public TikTokLiveHttpClient() { - this(new HttpClientFactory(LiveClientSettings.createDefault())); + this(new HttpClientFactory(LiveClientSettings.createDefault()), LiveClientSettings.createDefault()); } @@ -82,7 +85,26 @@ public class TikTokLiveHttpClient implements LiveHttpClient { @Override public LiveUserData.Response fetchLiveUserData(LiveUserData.Request request) { + var proxyClientSettings = clientSettings.getHttpSettings().getProxyClientSettings(); + if (proxyClientSettings.isEnabled()) { + while (proxyClientSettings.hasNext()) { + try { + var url = TIKTOK_URL_WEB + "api-live/user/room"; + var optional = httpFactory.client(url) + .withParam("uniqueId", request.getUserName()) + .withParam("sourceType", "54") + .build() + .toJsonResponse(); + if (optional.isEmpty()) { + throw new TikTokLiveRequestException("Unable to get information's about user"); + } + + var json = optional.get(); + return liveUserDataMapper.map(json); + } catch (TikTokProxyRequestException ignored) {} + } + } var url = TIKTOK_URL_WEB + "api-live/user/room"; var optional = httpFactory.client(url) .withParam("uniqueId", request.getUserName()) @@ -105,7 +127,25 @@ public class TikTokLiveHttpClient implements LiveHttpClient { @Override public LiveData.Response fetchLiveData(LiveData.Request request) { + var proxyClientSettings = clientSettings.getHttpSettings().getProxyClientSettings(); + if (proxyClientSettings.isEnabled()) { + while (proxyClientSettings.hasNext()) { + try { + var url = TIKTOK_URL_WEBCAST + "room/info"; + var optional = httpFactory.client(url) + .withParam("room_id", request.getRoomId()) + .build() + .toJsonResponse(); + if (optional.isEmpty()) { + throw new TikTokLiveRequestException("Unable to get info about live room"); + } + + var json = optional.get(); + return liveDataMapper.map(json); + } catch (TikTokProxyRequestException ignored) {} + } + } var url = TIKTOK_URL_WEBCAST + "room/info"; var optional = httpFactory.client(url) .withParam("room_id", request.getRoomId()) @@ -127,14 +167,15 @@ public class TikTokLiveHttpClient implements LiveHttpClient { @Override public LiveConnectionData.Response fetchLiveConnectionData(LiveConnectionData.Request request) { - - var signServerResponse = getSignedUrl(request.getRoomId()); - var credentialsResponse = getWebsocketCredentialsResponse(signServerResponse.getSignedUrl()); + HttpResponse credentialsResponse = getOptionalProxyResponse(request).orElseGet(()-> { + SignServerResponse signServerResponse = getSignedUrl(request.getRoomId()); + return getWebsocketCredentialsResponse(signServerResponse.getSignedUrl()); + }); try { var optionalHeader = credentialsResponse.headers().firstValue("set-cookie"); if (optionalHeader.isEmpty()) { - throw new TikTokSignServerException("Sign server does not returned set-cookie header"); + throw new TikTokSignServerException("Sign server did not return the set-cookie header"); } var websocketCookie = optionalHeader.get(); var webcastResponse = WebcastResponse.parseFrom(credentialsResponse.body()); @@ -189,4 +230,18 @@ public class TikTokLiveHttpClient implements LiveHttpClient { return optionalResponse.get(); } + Optional> getOptionalProxyResponse(LiveConnectionData.Request request) { + var proxyClientSettings = clientSettings.getHttpSettings().getProxyClientSettings(); + if (proxyClientSettings.isEnabled()) { + while (proxyClientSettings.hasNext()) { + try { + SignServerResponse signServerResponse = getSignedUrl(request.getRoomId()); + HttpResponse credentialsResponse = getWebsocketCredentialsResponse(signServerResponse.getSignedUrl()); + clientSettings.getHttpSettings().getProxyClientSettings().rotate(); + return Optional.of(credentialsResponse); + } catch (TikTokProxyRequestException | TikTokSignServerException ignored) {} + } + } + return Optional.empty(); + } } \ 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 e228c7d..88b523c 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 @@ -23,12 +23,14 @@ package io.github.jwdeveloper.tiktok.http; import io.github.jwdeveloper.tiktok.data.settings.*; -import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveRequestException; +import io.github.jwdeveloper.tiktok.exceptions.*; import javax.net.ssl.*; +import java.io.IOException; import java.net.*; import java.net.http.*; import java.net.http.HttpResponse.ResponseInfo; +import java.security.*; import java.security.cert.X509Certificate; import java.util.*; import java.util.stream.Collectors; @@ -93,7 +95,7 @@ public class HttpProxyClient extends HttpClient URL url = toUrl().toURL(); - while (proxySettings.hasNext()) { + if (proxySettings.hasNext()) { try { Proxy proxy = new Proxy(Proxy.Type.SOCKS, proxySettings.next().toSocketAddress()); @@ -117,21 +119,24 @@ public class HttpProxyClient extends HttpClient proxySettings.setLastSuccess(true); return Optional.of(response); - } catch (SocketException | SocketTimeoutException e) { - e.printStackTrace(); + } catch (IOException e) { if (proxySettings.isAutoDiscard()) proxySettings.remove(); proxySettings.setLastSuccess(false); + throw new TikTokProxyRequestException(e); } catch (Exception e) { throw new TikTokLiveRequestException(e); } } throw new TikTokLiveRequestException("No more proxies available!"); - } catch (Exception e) { + } catch (NoSuchAlgorithmException | MalformedURLException | KeyManagementException e) { // Should never be reached! System.out.println("handleSocksProxyRequest()! If you see this message, reach us on discord!"); e.printStackTrace(); return Optional.empty(); + } catch (TikTokLiveRequestException e) { + e.printStackTrace(); + return Optional.empty(); } } diff --git a/Client/src/main/java/io/github/jwdeveloper/tiktok/http/mappers/LiveUserDataMapper.java b/Client/src/main/java/io/github/jwdeveloper/tiktok/http/mappers/LiveUserDataMapper.java index 4e97bbe..6dd2aed 100644 --- a/Client/src/main/java/io/github/jwdeveloper/tiktok/http/mappers/LiveUserDataMapper.java +++ b/Client/src/main/java/io/github/jwdeveloper/tiktok/http/mappers/LiveUserDataMapper.java @@ -26,12 +26,10 @@ import com.google.gson.JsonParser; import io.github.jwdeveloper.tiktok.data.requests.LiveUserData; import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveRequestException; -public class LiveUserDataMapper { - - +public class LiveUserDataMapper +{ public LiveUserData.Response map(String json) { - var parsedJson = JsonParser.parseString(json); - var jsonObject = parsedJson.getAsJsonObject(); + var jsonObject = JsonParser.parseString(json).getAsJsonObject(); var message = jsonObject.get("message").getAsString(); @@ -64,6 +62,5 @@ public class LiveUserDataMapper { }; return new LiveUserData.Response(json, statusEnum, roomId, startTime); - } -} +} \ No newline at end of file diff --git a/Client/src/main/java/io/github/jwdeveloper/tiktok/websocket/TikTokWebSocketClient.java b/Client/src/main/java/io/github/jwdeveloper/tiktok/websocket/TikTokWebSocketClient.java index 936ca0b..5b32b76 100644 --- a/Client/src/main/java/io/github/jwdeveloper/tiktok/websocket/TikTokWebSocketClient.java +++ b/Client/src/main/java/io/github/jwdeveloper/tiktok/websocket/TikTokWebSocketClient.java @@ -52,6 +52,7 @@ public class TikTokWebSocketClient implements SocketClient { this.tikTokEventHandler = tikTokEventHandler; isConnected = false; } + @Override public void start(LiveConnectionData.Response connectionData, LiveClient liveClient) { @@ -71,7 +72,6 @@ public class TikTokWebSocketClient implements SocketClient { liveClient); // ProxyClientSettings proxyClientSettings = clientSettings.getHttpSettings().getProxyClientSettings(); - // // if (proxyClientSettings.isEnabled()) // connectProxy(proxyClientSettings); // else diff --git a/Client/src/main/java/io/github/jwdeveloper/tiktok/websocket/TikTokWebSocketListener.java b/Client/src/main/java/io/github/jwdeveloper/tiktok/websocket/TikTokWebSocketListener.java index a642827..b42c184 100644 --- a/Client/src/main/java/io/github/jwdeveloper/tiktok/websocket/TikTokWebSocketListener.java +++ b/Client/src/main/java/io/github/jwdeveloper/tiktok/websocket/TikTokWebSocketListener.java @@ -81,13 +81,12 @@ public class TikTokWebSocketListener extends WebSocketClient { pushFrameBuilder.setPayload(webcastResponse.getInternalExtBytes()); if (isNotClosing()) { - this.send(pushFrameBuilder.build().toByteArray()); + this.send(pushFrameBuilder.build().toByteArray()); } } messageHandler.handle(tikTokLiveClient, webcastResponse); } - @Override public void onOpen(ServerHandshake serverHandshake) { tikTokEventHandler.publish(tikTokLiveClient, new TikTokConnectedEvent()); @@ -96,10 +95,9 @@ public class TikTokWebSocketListener extends WebSocketClient { } } - @Override public void onClose(int code, String reason, boolean remote) { - tikTokEventHandler.publish(tikTokLiveClient, new TikTokDisconnectedEvent()); + tikTokEventHandler.publish(tikTokLiveClient, new TikTokDisconnectedEvent(reason)); tikTokLiveClient.disconnect(); } @@ -111,8 +109,6 @@ public class TikTokWebSocketListener extends WebSocketClient { } } - - private Optional getWebcastPushFrame(byte[] buffer) { try { var websocketMessage = WebcastPushFrame.parseFrom(buffer); @@ -137,9 +133,8 @@ public class TikTokWebSocketListener extends WebSocketClient { return !isClosed() && !isClosing(); } - @Override public void onMessage(String s) { - System.err.println(s); + // System.err.println(s); } } \ No newline at end of file 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 ea16df6..3746bf4 100644 --- a/Examples/src/main/java/io/github/jwdeveloper/tiktok/ProxyExample.java +++ b/Examples/src/main/java/io/github/jwdeveloper/tiktok/ProxyExample.java @@ -22,54 +22,29 @@ */ package io.github.jwdeveloper.tiktok; -import java.net.*; -import java.net.http.*; -import java.util.*; -import java.util.stream.Stream; +import java.net.Proxy; public class ProxyExample { public static void main(String[] args) throws Exception { - // TikTokLive.newClient(SimpleExample.TIKTOK_HOSTNAME) - - HttpRequest request = HttpRequest.newBuilder(URI.create("https://api.proxyscrape.com/v2/?request=displayproxies&protocol=socks4,socks5&timeout=10000&country=us")).GET().build(); - HttpResponse> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofLines()); - - List> entries = new ArrayList<>(response.body().map(s -> { - String[] split = s.split(":"); - return new AbstractMap.SimpleEntry<>(split[0], Integer.parseInt(split[1])); - }).toList()); - - TikTokLive.newClient("dash4214") - .configure(clientSettings -> - { + TikTokLive.newClient(SimpleExample.TIKTOK_HOSTNAME) + .configure(clientSettings -> { clientSettings.setPrintToConsole(true); clientSettings.getHttpSettings().configureProxy(proxySettings -> { - proxySettings.setOnProxyUpdated(proxyData -> - { - System.err.println("Next proxy: "+proxyData.toString()); - }); + proxySettings.setOnProxyUpdated(proxyData -> System.err.println("Next proxy: " + proxyData.toString())); proxySettings.setType(Proxy.Type.SOCKS); - entries.forEach(entry -> proxySettings.addProxy(entry.getKey(), entry.getValue())); + proxySettings.addProxy("localhost", 8080); }); }) - .onComment((liveClient, event) -> { - liveClient.getLogger().info(event.getUser().getName()+": "+event.getText()); - }) .onConnected((liveClient, event) -> - { - liveClient.getLogger().info("Hello world!"); - }) + liveClient.getLogger().info("Connected "+liveClient.getRoomInfo().getHostName())) .onDisconnected((liveClient, event) -> - { - liveClient.getLogger().info("Goodbye world!"); - }) + liveClient.getLogger().info("Disconnect reason: "+event.getReason())) + .onLiveEnded((liveClient, event) -> + liveClient.getLogger().info("Live Ended")) .onError((liveClient, event) -> - { - event.getException().printStackTrace(); - }) + event.getException().printStackTrace()) .buildAndConnect(); - System.in.read(); } } \ No newline at end of file