aboutsummaryrefslogtreecommitdiff
path: root/spark-common
diff options
context:
space:
mode:
authorLuck <git@lucko.me>2023-03-07 21:54:14 +0000
committerLuck <git@lucko.me>2023-03-07 21:54:14 +0000
commit0f30d2983ec6ef487fd1966c1c22fa4a73e081f9 (patch)
treed719c1994e359fbe5387eef8ad74def15e9044e2 /spark-common
parentd61aa1f95e8c9afaf55bb00d869782025efcd953 (diff)
downloadspark-0f30d2983ec6ef487fd1966c1c22fa4a73e081f9.tar.gz
spark-0f30d2983ec6ef487fd1966c1c22fa4a73e081f9.tar.bz2
spark-0f30d2983ec6ef487fd1966c1c22fa4a73e081f9.zip
Don't use multi-release jar for websocket code
Diffstat (limited to 'spark-common')
-rw-r--r--spark-common/build.gradle23
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/SparkPlatform.java5
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/command/modules/SamplerModule.java2
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/util/ws/BytesocksClient.java118
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/util/ws/BytesocksClientImpl.java40
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/ws/ViewerSocket.java2
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/ws/ViewerSocketConnection.java2
-rw-r--r--spark-common/src/main/java11/me/lucko/spark/common/util/ws/BytesocksClientImpl.java146
8 files changed, 7 insertions, 331 deletions
diff --git a/spark-common/build.gradle b/spark-common/build.gradle
index 742d943..1713168 100644
--- a/spark-common/build.gradle
+++ b/spark-common/build.gradle
@@ -4,24 +4,6 @@ plugins {
id 'com.google.protobuf' version '0.9.1'
}
-sourceSets {
- java11 {
- java {
- srcDirs = ['src/main/java11']
- }
- }
-}
-
-compileJava11Java {
- options.release = 11
-}
-
-jar {
- into('META-INF/versions/11') {
- from sourceSets.java11.output
- }
-}
-
license {
exclude '**/sampler/async/jfr/**'
}
@@ -37,9 +19,8 @@ dependencies {
implementation 'com.github.jvm-profiling-tools:async-profiler:v2.8.3'
implementation 'org.ow2.asm:asm:9.1'
implementation 'net.bytebuddy:byte-buddy-agent:1.11.0'
-
implementation 'com.google.protobuf:protobuf-javalite:3.21.11'
- java11Implementation 'com.google.protobuf:protobuf-javalite:3.21.11'
+ implementation 'me.lucko:bytesocks-java-client:1.0-SNAPSHOT'
api('net.kyori:adventure-api:4.12.0') {
exclude(module: 'adventure-bom')
@@ -61,8 +42,6 @@ dependencies {
compileOnly 'com.google.code.gson:gson:2.7'
compileOnly 'com.google.guava:guava:19.0'
compileOnly 'org.checkerframework:checker-qual:3.8.0'
-
- java11Implementation files(sourceSets.main.output.classesDirs) { builtBy compileJava }
}
protobuf {
diff --git a/spark-common/src/main/java/me/lucko/spark/common/SparkPlatform.java b/spark-common/src/main/java/me/lucko/spark/common/SparkPlatform.java
index 61c6062..f609719 100644
--- a/spark-common/src/main/java/me/lucko/spark/common/SparkPlatform.java
+++ b/spark-common/src/main/java/me/lucko/spark/common/SparkPlatform.java
@@ -23,6 +23,8 @@ package me.lucko.spark.common;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
+import me.lucko.bytesocks.client.BytesocksClient;
+import me.lucko.bytesocks.client.BytesocksClientFactory;
import me.lucko.spark.common.activitylog.ActivityLog;
import me.lucko.spark.common.api.SparkApi;
import me.lucko.spark.common.command.Arguments;
@@ -53,7 +55,6 @@ import me.lucko.spark.common.tick.TickReporter;
import me.lucko.spark.common.util.BytebinClient;
import me.lucko.spark.common.util.Configuration;
import me.lucko.spark.common.util.TemporaryFiles;
-import me.lucko.spark.common.util.ws.BytesocksClient;
import me.lucko.spark.common.ws.TrustedKeyStore;
import net.kyori.adventure.text.Component;
@@ -126,7 +127,7 @@ public class SparkPlatform {
String bytesocksHost = this.configuration.getString("bytesocksHost", "spark-usersockets.lucko.me");
this.bytebinClient = new BytebinClient(bytebinUrl, "spark-plugin");
- this.bytesocksClient = BytesocksClient.create(bytesocksHost, "spark-plugin");
+ this.bytesocksClient = BytesocksClientFactory.newClient(bytesocksHost, "spark-plugin");
this.trustedKeyStore = new TrustedKeyStore(this.configuration);
this.disableResponseBroadcast = this.configuration.getBoolean("disableResponseBroadcast", false);
diff --git a/spark-common/src/main/java/me/lucko/spark/common/command/modules/SamplerModule.java b/spark-common/src/main/java/me/lucko/spark/common/command/modules/SamplerModule.java
index 049c817..27e790f 100644
--- a/spark-common/src/main/java/me/lucko/spark/common/command/modules/SamplerModule.java
+++ b/spark-common/src/main/java/me/lucko/spark/common/command/modules/SamplerModule.java
@@ -22,6 +22,7 @@ package me.lucko.spark.common.command.modules;
import com.google.common.collect.Iterables;
+import me.lucko.bytesocks.client.BytesocksClient;
import me.lucko.spark.common.SparkPlatform;
import me.lucko.spark.common.activitylog.Activity;
import me.lucko.spark.common.command.Arguments;
@@ -43,7 +44,6 @@ import me.lucko.spark.common.tick.TickHook;
import me.lucko.spark.common.util.FormatUtil;
import me.lucko.spark.common.util.MediaTypes;
import me.lucko.spark.common.util.MethodDisambiguator;
-import me.lucko.spark.common.util.ws.BytesocksClient;
import me.lucko.spark.common.ws.ViewerSocket;
import me.lucko.spark.proto.SparkSamplerProtos;
diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/ws/BytesocksClient.java b/spark-common/src/main/java/me/lucko/spark/common/util/ws/BytesocksClient.java
deleted file mode 100644
index 1db7a67..0000000
--- a/spark-common/src/main/java/me/lucko/spark/common/util/ws/BytesocksClient.java
+++ /dev/null
@@ -1,118 +0,0 @@
-/*
- * This file is part of spark.
- *
- * Copyright (c) lucko (Luck) <luck@lucko.me>
- * Copyright (c) contributors
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-package me.lucko.spark.common.util.ws;
-
-import java.util.concurrent.CompletableFuture;
-
-/**
- * A client that can interact with bytesocks.
- *
- * @see <a href="https://github.com/lucko/bytesocks">https://github.com/lucko/bytesocks</a>
- */
-public interface BytesocksClient {
-
- /**
- * Creates a new {@link BytesocksClient}.
- *
- * <p>Returns {@code null} on Java versions before 11.</p>
- *
- * @param host the host
- * @param userAgent the user agent
- * @return the client
- */
- static BytesocksClient create(String host, String userAgent) {
- try {
- return new BytesocksClientImpl(host, userAgent);
- } catch (UnsupportedOperationException e) {
- return null;
- }
- }
-
- /**
- * Creates a new bytesocks channel and returns a socket connected to it.
- *
- * @param listener the listener
- * @return the socket
- * @throws Exception if something goes wrong
- */
- Socket createAndConnect(Listener listener) throws Exception;
-
- /**
- * Connects to an existing bytesocks channel.
- *
- * @param channelId the channel id
- * @param listener the listener
- * @return the socket
- * @throws Exception if something goes wrong
- */
- Socket connect(String channelId, Listener listener) throws Exception;
-
- /**
- * A socket connected to a bytesocks channel.
- */
- interface Socket {
-
- /**
- * Gets the id of the connected channel.
- *
- * @return the id of the channel
- */
- String getChannelId();
-
- /**
- * Gets if the socket is open.
- *
- * @return true if the socket is open
- */
- boolean isOpen();
-
- /**
- * Sends a message to the channel using the socket.
- *
- * @param msg the message to send
- * @return a future to encapsulate the progress of sending the message
- */
- CompletableFuture<?> send(CharSequence msg);
-
- /**
- * Closes the socket.
- *
- * @param statusCode the status code
- * @param reason the reason
- */
- void close(int statusCode, String reason);
- }
-
- /**
- * Socket listener
- */
- interface Listener {
-
- default void onOpen() {}
-
- default void onError(Throwable error) {}
-
- default void onText(CharSequence data) {}
-
- default void onClose(int statusCode, String reason) {}
- }
-
-}
diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/ws/BytesocksClientImpl.java b/spark-common/src/main/java/me/lucko/spark/common/util/ws/BytesocksClientImpl.java
deleted file mode 100644
index cf1489c..0000000
--- a/spark-common/src/main/java/me/lucko/spark/common/util/ws/BytesocksClientImpl.java
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * This file is part of spark.
- *
- * Copyright (c) lucko (Luck) <luck@lucko.me>
- * Copyright (c) contributors
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-package me.lucko.spark.common.util.ws;
-
-// Overridden by java 11 source set
-
-class BytesocksClientImpl implements BytesocksClient {
-
- BytesocksClientImpl(String host, String userAgent) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public Socket createAndConnect(Listener listener) throws Exception {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public Socket connect(String channelId, Listener listener) throws Exception {
- throw new UnsupportedOperationException();
- }
-}
diff --git a/spark-common/src/main/java/me/lucko/spark/common/ws/ViewerSocket.java b/spark-common/src/main/java/me/lucko/spark/common/ws/ViewerSocket.java
index 5c7e08c..6a9c2b7 100644
--- a/spark-common/src/main/java/me/lucko/spark/common/ws/ViewerSocket.java
+++ b/spark-common/src/main/java/me/lucko/spark/common/ws/ViewerSocket.java
@@ -22,12 +22,12 @@ package me.lucko.spark.common.ws;
import com.google.protobuf.ByteString;
+import me.lucko.bytesocks.client.BytesocksClient;
import me.lucko.spark.common.SparkPlatform;
import me.lucko.spark.common.sampler.AbstractSampler;
import me.lucko.spark.common.sampler.Sampler;
import me.lucko.spark.common.sampler.window.ProfilingWindowUtils;
import me.lucko.spark.common.util.MediaTypes;
-import me.lucko.spark.common.util.ws.BytesocksClient;
import me.lucko.spark.proto.SparkProtos;
import me.lucko.spark.proto.SparkSamplerProtos;
import me.lucko.spark.proto.SparkWebSocketProtos.ClientConnect;
diff --git a/spark-common/src/main/java/me/lucko/spark/common/ws/ViewerSocketConnection.java b/spark-common/src/main/java/me/lucko/spark/common/ws/ViewerSocketConnection.java
index f870cb7..9079860 100644
--- a/spark-common/src/main/java/me/lucko/spark/common/ws/ViewerSocketConnection.java
+++ b/spark-common/src/main/java/me/lucko/spark/common/ws/ViewerSocketConnection.java
@@ -22,8 +22,8 @@ package me.lucko.spark.common.ws;
import com.google.protobuf.ByteString;
+import me.lucko.bytesocks.client.BytesocksClient;
import me.lucko.spark.common.SparkPlatform;
-import me.lucko.spark.common.util.ws.BytesocksClient;
import me.lucko.spark.proto.SparkWebSocketProtos.PacketWrapper;
import me.lucko.spark.proto.SparkWebSocketProtos.RawPacket;
diff --git a/spark-common/src/main/java11/me/lucko/spark/common/util/ws/BytesocksClientImpl.java b/spark-common/src/main/java11/me/lucko/spark/common/util/ws/BytesocksClientImpl.java
deleted file mode 100644
index 7adb809..0000000
--- a/spark-common/src/main/java11/me/lucko/spark/common/util/ws/BytesocksClientImpl.java
+++ /dev/null
@@ -1,146 +0,0 @@
-/*
- * This file is part of spark.
- *
- * Copyright (c) lucko (Luck) <luck@lucko.me>
- * Copyright (c) contributors
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-package me.lucko.spark.common.util.ws;
-
-import java.net.URI;
-import java.net.http.HttpClient;
-import java.net.http.HttpRequest;
-import java.net.http.HttpResponse;
-import java.net.http.WebSocket;
-import java.time.Duration;
-import java.util.concurrent.CompletableFuture;
-import java.util.concurrent.CompletionStage;
-
-/**
- * Java 11 implementation of {@link BytesocksClient}.
- */
-class BytesocksClientImpl implements BytesocksClient {
-
- private final HttpClient client;
-
- /* The bytesocks urls */
- private final String httpUrl;
- private final String wsUrl;
-
- /** The client user agent */
- private final String userAgent;
-
- BytesocksClientImpl(String host, String userAgent) {
- this.client = HttpClient.newHttpClient();
-
- this.httpUrl = "https://" + host + "/";
- this.wsUrl = "wss://" + host + "/";
- this.userAgent = userAgent;
- }
-
- @Override
- public Socket createAndConnect(Listener listener) throws Exception {
- HttpRequest createRequest = HttpRequest.newBuilder()
- .uri(URI.create(this.httpUrl + "create"))
- .header("User-Agent", this.userAgent)
- .build();
-
- HttpResponse<Void> resp = this.client.send(createRequest, HttpResponse.BodyHandlers.discarding());
- if (resp.statusCode() != 201) {
- throw new RuntimeException("Request failed: " + resp);
- }
-
- String channelId = resp.headers().firstValue("Location").orElse(null);
- if (channelId == null) {
- throw new RuntimeException("Location header not returned: " + resp);
- }
-
- return connect(channelId, listener);
- }
-
- @Override
- public Socket connect(String channelId, Listener listener) throws Exception {
- WebSocket socket = this.client.newWebSocketBuilder()
- .header("User-Agent", this.userAgent)
- .connectTimeout(Duration.ofSeconds(5))
- .buildAsync(URI.create(this.wsUrl + channelId), new ListenerImpl(listener))
- .join();
-
- return new SocketImpl(channelId, socket);
- }
-
- private static final class SocketImpl implements Socket {
- private final String id;
- private final WebSocket ws;
-
- private SocketImpl(String id, WebSocket ws) {
- this.id = id;
- this.ws = ws;
- }
-
- @Override
- public String getChannelId() {
- return this.id;
- }
-
- @Override
- public boolean isOpen() {
- return !this.ws.isOutputClosed() && !this.ws.isInputClosed();
- }
-
- @Override
- public CompletableFuture<?> send(CharSequence msg) {
- return this.ws.sendText(msg, true);
- }
-
- @Override
- public void close(int statusCode, String reason) {
- this.ws.sendClose(statusCode, reason);
- }
- }
-
- private static final class ListenerImpl implements WebSocket.Listener {
- private final Listener listener;
-
- private ListenerImpl(Listener listener) {
- this.listener = listener;
- }
-
- @Override
- public void onOpen(WebSocket webSocket) {
- this.listener.onOpen();
- webSocket.request(1);
- }
-
- @Override
- public CompletionStage<?> onClose(WebSocket webSocket, int statusCode, String reason) {
- return CompletableFuture.runAsync(() -> this.listener.onClose(statusCode, reason));
- }
-
- @Override
- public void onError(WebSocket webSocket, Throwable error) {
- this.listener.onError(error);
- }
-
- @Override
- public CompletionStage<?> onText(WebSocket webSocket, CharSequence data, boolean last) {
- webSocket.request(1);
- return CompletableFuture.runAsync(() -> this.listener.onText(data));
- }
- }
-
-
-}