From e4b0c1c646fd53cae9acfa00eaa037599e81e50f Mon Sep 17 00:00:00 2001 From: Zaxar163 <35835496+Zaxar163@users.noreply.github.com> Date: Sun, 30 Sep 2018 14:42:07 +0300 Subject: [PATCH] Add websocket on client. (#27) * Add websocket on client. * Make send methods. --- LaunchServer/build.gradle | 2 +- .../ru/gravit/launchserver/LaunchServer.java | 1 - .../launchserver/auth/hwid/HWIDHandler.java | 1 - .../auth/provider/JsonAuthProvider.java | 3 - .../auth/provider/MySQLAuthProvider.java | 1 - .../binary/JARLauncherBinary.java | 1 - .../manangers/BuildHookManager.java | 2 - .../manangers/ModulesManager.java | 1 + .../response/auth/AuthServerResponse.java | 3 - .../response/update/UpdateListResponse.java | 2 - LauncherAPI/build.gradle | 5 +- .../request/auth/AuthServerRequest.java | 1 - .../request/websockets/ClientJSONPoint.java | 67 +++++++++++++++++++ .../gravit/launcher/server/ServerWrapper.java | 5 +- .../launcher/profiles/ClientProfile.java | 4 -- .../launcher/ssl/LauncherSSLContext.java | 2 - .../SystemClassLoaderTransformer.java | 1 - 17 files changed, 75 insertions(+), 27 deletions(-) create mode 100644 LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ClientJSONPoint.java diff --git a/LaunchServer/build.gradle b/LaunchServer/build.gradle index 695e8bd8..4eb05505 100644 --- a/LaunchServer/build.gradle +++ b/LaunchServer/build.gradle @@ -53,7 +53,7 @@ compile project(':libLauncher') // pack bundle 'org.fusesource.jansi:jansi:1.17.1' bundle 'commons-io:commons-io:2.6' bundle 'org.javassist:javassist:3.23.1-GA' - bundle 'io.netty:netty-all:4.1.28.Final' + bundle 'io.netty:netty-all:4.1.29.Final' bundle 'org.slf4j:slf4j-simple:1.7.25' bundle 'org.slf4j:slf4j-api:1.7.25' diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java b/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java index 247f7a88..d8022245 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java @@ -31,7 +31,6 @@ import ru.gravit.launcher.Launcher; import ru.gravit.launcher.LauncherAPI; -import ru.gravit.launcher.hasher.FileNameMatcher; import ru.gravit.launcher.hasher.HashedDir; import ru.gravit.utils.helper.CommonHelper; import ru.gravit.utils.helper.IOHelper; diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/HWIDHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/HWIDHandler.java index 1fb772a0..c3575c8a 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/HWIDHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/HWIDHandler.java @@ -1,6 +1,5 @@ package ru.gravit.launchserver.auth.hwid; -import java.io.IOException; import java.util.List; import java.util.Map; import java.util.Objects; diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/JsonAuthProvider.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/JsonAuthProvider.java index 04e8d8db..3deb29c2 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/JsonAuthProvider.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/JsonAuthProvider.java @@ -16,9 +16,6 @@ import ru.gravit.launcher.serialize.config.entry.StringConfigEntry; public final class JsonAuthProvider extends AuthProvider { - private static final int TIMEOUT = Integer.parseInt( - System.getProperty("launcher.connection.timeout", Integer.toString(1500))); - private final URL url; private final String userKeyName; private final String passKeyName; diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/MySQLAuthProvider.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/MySQLAuthProvider.java index 59265d95..e28b8cf5 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/MySQLAuthProvider.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/MySQLAuthProvider.java @@ -5,7 +5,6 @@ import java.sql.ResultSet; import java.sql.SQLException; -import clojure.lang.IFn; import ru.gravit.launchserver.LaunchServer; import ru.gravit.utils.helper.CommonHelper; import ru.gravit.utils.helper.SecurityHelper; diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/binary/JARLauncherBinary.java b/LaunchServer/src/main/java/ru/gravit/launchserver/binary/JARLauncherBinary.java index b6f12a51..85ca15f7 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/binary/JARLauncherBinary.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/binary/JARLauncherBinary.java @@ -21,7 +21,6 @@ import ru.gravit.launcher.Launcher; import ru.gravit.launcher.LauncherAPI; import ru.gravit.launcher.LauncherConfig; -import ru.gravit.launchserver.manangers.BuildHookManager; import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.LogHelper; import ru.gravit.utils.helper.SecurityHelper; diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/manangers/BuildHookManager.java b/LaunchServer/src/main/java/ru/gravit/launchserver/manangers/BuildHookManager.java index 25caa0cc..ad2d2d6d 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/manangers/BuildHookManager.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/manangers/BuildHookManager.java @@ -1,11 +1,9 @@ package ru.gravit.launchserver.manangers; -import java.io.InputStream; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; -import java.util.zip.ZipOutputStream; import ru.gravit.launcher.AutogenConfig; import ru.gravit.launcher.modules.TestClientModule; diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/manangers/ModulesManager.java b/LaunchServer/src/main/java/ru/gravit/launchserver/manangers/ModulesManager.java index 76fce1bb..c9571184 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/manangers/ModulesManager.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/manangers/ModulesManager.java @@ -14,6 +14,7 @@ public ModulesManager(LaunchServer lsrv) { modules = new ArrayList<>(1); classloader = new PublicURLClassLoader(new URL[0], ClassLoader.getSystemClassLoader()); context = new LaunchServerModuleContext(lsrv, classloader); + registerCoreModule(); } private void registerCoreModule() { diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/response/auth/AuthServerResponse.java b/LaunchServer/src/main/java/ru/gravit/launchserver/response/auth/AuthServerResponse.java index 87fa5f32..5c620287 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/response/auth/AuthServerResponse.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/response/auth/AuthServerResponse.java @@ -7,12 +7,10 @@ import ru.gravit.launcher.serialize.signed.SignedObjectHolder; import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.auth.AuthException; -import ru.gravit.launchserver.auth.hwid.HWID; import ru.gravit.launchserver.auth.hwid.HWIDException; import ru.gravit.launchserver.auth.provider.AuthProvider; import ru.gravit.launchserver.auth.provider.AuthProviderResult; import ru.gravit.launchserver.response.Response; -import ru.gravit.launchserver.response.profile.ProfileByUUIDResponse; import ru.gravit.launchserver.socket.Client; import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.LogHelper; @@ -23,7 +21,6 @@ import javax.crypto.IllegalBlockSizeException; import java.util.Arrays; import java.util.Collection; -import java.util.UUID; public final class AuthServerResponse extends Response { private static String echo(int length) { diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/response/update/UpdateListResponse.java b/LaunchServer/src/main/java/ru/gravit/launchserver/response/update/UpdateListResponse.java index 06f19a61..08795b82 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/response/update/UpdateListResponse.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/response/update/UpdateListResponse.java @@ -1,13 +1,11 @@ package ru.gravit.launchserver.response.update; -import java.util.ArrayList; import java.util.Map.Entry; import java.util.Set; import ru.gravit.launcher.hasher.HashedDir; import ru.gravit.launcher.serialize.HInput; import ru.gravit.launcher.serialize.HOutput; -import ru.gravit.launcher.serialize.config.entry.ListConfigEntry; import ru.gravit.launcher.serialize.signed.SignedObjectHolder; import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.response.Response; diff --git a/LauncherAPI/build.gradle b/LauncherAPI/build.gradle index 5c3d8a6c..cb326418 100644 --- a/LauncherAPI/build.gradle +++ b/LauncherAPI/build.gradle @@ -2,8 +2,9 @@ targetCompatibility = '1.8' dependencies { - compile project(':libLauncher') + compile project(':libLauncher') + compile 'javax.websocket:javax.websocket-client-api:1.1' compileOnly 'com.google.code.gson:gson:2.8.5' compileOnly 'com.google.guava:guava:26.0-jre' - compile files('../compat/authlib/authlib-clean.jar') + compile files('../compat/authlib/authlib-clean.jar') } diff --git a/LauncherAPI/src/main/java/ru/gravit/launcher/request/auth/AuthServerRequest.java b/LauncherAPI/src/main/java/ru/gravit/launcher/request/auth/AuthServerRequest.java index 3c06ef46..a91871e3 100644 --- a/LauncherAPI/src/main/java/ru/gravit/launcher/request/auth/AuthServerRequest.java +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/auth/AuthServerRequest.java @@ -1,6 +1,5 @@ package ru.gravit.launcher.request.auth; -import ru.gravit.launcher.Launcher; import ru.gravit.launcher.LauncherAPI; import ru.gravit.launcher.LauncherConfig; import ru.gravit.launcher.profiles.PlayerProfile; diff --git a/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ClientJSONPoint.java b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ClientJSONPoint.java new file mode 100644 index 00000000..66069bc3 --- /dev/null +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ClientJSONPoint.java @@ -0,0 +1,67 @@ +package ru.gravit.launcher.request.websockets; + +import java.io.IOException; +import java.io.Reader; + +import javax.websocket.ClientEndpoint; +import javax.websocket.OnError; +import javax.websocket.OnMessage; +import javax.websocket.OnOpen; +import javax.websocket.Session; + +import com.eclipsesource.json.Json; +import com.eclipsesource.json.JsonValue; +import com.eclipsesource.json.ParseException; + +import ru.gravit.utils.helper.LogHelper; + +/* + * public class Client { + * + * final static CountDownLatch messageLatch = new CountDownLatch(1); + * + * public static void main(String[] args) { + * try { + * WebSocketContainer container = ContainerProvider.getWebSocketContainer(); + * String uri = "ws://echo.websocket.org:80/"; + * System.out.println("Connecting to " + uri); + * container.connectToServer(MyClientEndpoint.class, URI.create(uri)); + * messageLatch.await(100, TimeUnit.SECONDS); + * } catch (DeploymentException | InterruptedException | IOException ex) { + * Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex); + * } + * } + * } + */ +@ClientEndpoint +public class ClientJSONPoint { + public Session session = null; + + @OnOpen + public void onOpen(final Session session_r) { + session = session_r; + System.out.println("Connected to endpoint: " + session.getBasicRemote()); + } + + @OnError + public void processError(final Throwable t) { + t.printStackTrace(); + } + + @OnMessage + public void processMessage(Reader message) { + try { + JsonValue json = Json.parse(message); + } catch (ParseException | IOException ex) { + LogHelper.error(ex); + } + } + + public void send(JsonValue js) throws IOException { + session.getBasicRemote().sendText(js.asString()); + } + + public void sendAsync(JsonValue js) throws IOException { + session.getAsyncRemote().sendText(js.asString()); + } +} \ No newline at end of file diff --git a/ServerWrapper/src/main/java/ru/gravit/launcher/server/ServerWrapper.java b/ServerWrapper/src/main/java/ru/gravit/launcher/server/ServerWrapper.java index e38633a6..ecd42fd5 100644 --- a/ServerWrapper/src/main/java/ru/gravit/launcher/server/ServerWrapper.java +++ b/ServerWrapper/src/main/java/ru/gravit/launcher/server/ServerWrapper.java @@ -45,7 +45,7 @@ public static void main(String[] args) throws Throwable { } LauncherConfig cfg = new LauncherConfig(config.address, config.port, SecurityHelper.toPublicRSAKey(IOHelper.read(Paths.get("public.key"))),new HashMap<>(),config.projectname); Boolean auth = new AuthServerRequest(cfg,config.login,SecurityHelper.newRSAEncryptCipher(cfg.publicKey).doFinal(IOHelper.encode(config.password)),0,config.title).request(); - + // TODO check auth... ProfilesRequest.Result result = new ProfilesRequest(cfg).request(); Launcher.setConfig(cfg); for (SignedObjectHolder p : result.profiles) { @@ -61,7 +61,8 @@ public static void main(String[] args) throws Throwable { String classname = config.mainclass.isEmpty() ? args[0] : config.mainclass; Class mainClass; if(config.customClassLoader) { - Class classloader_class = (Class) Class.forName(config.classloader); + @SuppressWarnings("unchecked") + Class classloader_class = (Class) Class.forName(config.classloader); ClassLoader loader = classloader_class.getConstructor(ClassLoader.class).newInstance(ClassLoader.getSystemClassLoader()); Thread.currentThread().setContextClassLoader(loader); mainClass = Class.forName(classname,false,loader); diff --git a/libLauncher/src/main/java/ru/gravit/launcher/profiles/ClientProfile.java b/libLauncher/src/main/java/ru/gravit/launcher/profiles/ClientProfile.java index 2f8481a9..a8ed153d 100644 --- a/libLauncher/src/main/java/ru/gravit/launcher/profiles/ClientProfile.java +++ b/libLauncher/src/main/java/ru/gravit/launcher/profiles/ClientProfile.java @@ -1,16 +1,12 @@ package ru.gravit.launcher.profiles; -import java.io.File; import java.io.IOException; import java.net.InetSocketAddress; -import java.nio.file.Path; -import java.nio.file.Paths; import java.util.*; import ru.gravit.launcher.LauncherAPI; import ru.gravit.launcher.hasher.FileNameMatcher; import ru.gravit.launcher.hasher.HashedDir; -import ru.gravit.launcher.hasher.HashedFile; import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.VerifyHelper; import ru.gravit.launcher.serialize.HInput; diff --git a/libLauncher/src/main/java/ru/gravit/launcher/ssl/LauncherSSLContext.java b/libLauncher/src/main/java/ru/gravit/launcher/ssl/LauncherSSLContext.java index 96ad6050..cdd03ba4 100644 --- a/libLauncher/src/main/java/ru/gravit/launcher/ssl/LauncherSSLContext.java +++ b/libLauncher/src/main/java/ru/gravit/launcher/ssl/LauncherSSLContext.java @@ -1,9 +1,7 @@ package ru.gravit.launcher.ssl; import javax.net.ssl.*; -import java.io.IOException; import java.security.*; -import java.security.cert.CertificateException; public class LauncherSSLContext { public SSLServerSocketFactory ssf; diff --git a/libLauncher/src/main/java/ru/gravit/launcher/transformers/SystemClassLoaderTransformer.java b/libLauncher/src/main/java/ru/gravit/launcher/transformers/SystemClassLoaderTransformer.java index 9fcdaede..ec76f96e 100644 --- a/libLauncher/src/main/java/ru/gravit/launcher/transformers/SystemClassLoaderTransformer.java +++ b/libLauncher/src/main/java/ru/gravit/launcher/transformers/SystemClassLoaderTransformer.java @@ -2,7 +2,6 @@ import java.io.ByteArrayInputStream; import java.lang.instrument.ClassFileTransformer; -import java.lang.instrument.IllegalClassFormatException; import java.security.ProtectionDomain; import javassist.ClassPool;