diff --git a/LaunchServer/build.gradle b/LaunchServer/build.gradle index c6702389..0f2209c1 100644 --- a/LaunchServer/build.gradle +++ b/LaunchServer/build.gradle @@ -18,8 +18,10 @@ bundle hikari pack + launch4j + launch4jCJ bundleOnly.extendsFrom bundle - compile.extendsFrom bundle, hikari, pack + compile.extendsFrom bundle, hikari, pack, launch4jCJ, launch4j } jar { @@ -57,10 +59,18 @@ pack project(':libLauncher') exclude group: 'io.micrometer' exclude group: 'org.slf4j' } - - compileOnly('net.sf.launch4j:launch4j:3.12') { // need user + + launch4j('net.sf.launch4j:launch4j:3.12') { exclude group: '*' } + + launch4jCJ('net.sf.launch4j:launch4j:3.12:workdir-win32') { + exclude group: '*' + } + + launch4jCJ('net.sf.launch4j:launch4j:3.12:workdir-linux') { + exclude group: '*' + } } task hikari(type: Copy) { @@ -68,16 +78,49 @@ task hikari(type: Copy) { from configurations.hikari } +task launch4jM(type: Copy) { + into "$buildDir/libs/libraries/launch4j" + from(configurations.launch4jCJ.collect { it.isDirectory() ? it : zipTree(it) }) + includeEmptyDirs false + eachFile { FileCopyDetails fcp -> + if (fcp.relativePath.pathString.startsWith("launch4j-")) { + def segments = fcp.relativePath.segments + def pathSegments = segments[1..-1] as String[] + fcp.relativePath = new RelativePath(!fcp.file.isDirectory(), pathSegments) + fcp.mode = 0755 + } else { + fcp.exclude() + } + } +} + +task launch4jA(type: Copy) { + into "$buildDir/libs/libraries/launch4j" + from(configurations.launch4j) + includeEmptyDirs false + eachFile { FileCopyDetails fcp -> + if (fcp.name.startsWith("launch4j")) { + fcp.name = "launch4j.jar" + fcp.mode = 0755 + } else { + fcp.exclude() + } + } +} + task dumpLibs(type: Copy) { - dependsOn tasks.hikari + dependsOn tasks.hikari, tasks.launch4jM, tasks.launch4jA into "$buildDir/libs/libraries" from configurations.bundleOnly } -task dumpClientLibs(type: Copy) { - dependsOn parent.childProjects.Launcher.tasks.build - into "$buildDir/libs/launcher-libraries" - from parent.childProjects.Launcher.tasks.dumpLibs.destinationDir +task bundle(type: Zip) { + dependsOn parent.childProjects.Launcher.tasks.build, tasks.dumpLibs, tasks.jar + archiveName 'LaunchServer.zip' + destinationDir file("$buildDir") + from(tasks.dumpLibs.destinationDir) { into 'libraries' } + from tasks.jar.archivePath + from(parent.childProjects.Launcher.tasks.dumpLibs) { into 'launcher-libraries' } } -build.dependsOn tasks.dumpLibs, tasks.dumpClientLibs +build.dependsOn tasks.dumpLibs, tasks.bundle diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java b/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java index 8631d732..7d300b5a 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java @@ -18,9 +18,9 @@ import ru.gravit.launchserver.auth.provider.AuthProvider; import ru.gravit.launchserver.auth.provider.RejectAuthProvider; import ru.gravit.launchserver.binary.*; -import ru.gravit.launchserver.command.handler.CommandHandler; -import ru.gravit.launchserver.command.handler.JLineCommandHandler; -import ru.gravit.launchserver.command.handler.StdCommandHandler; +import ru.gravit.utils.command.CommandHandler; +import ru.gravit.utils.command.JLineCommandHandler; +import ru.gravit.utils.command.StdCommandHandler; import ru.gravit.launchserver.config.*; import ru.gravit.launchserver.manangers.*; import ru.gravit.launchserver.manangers.hook.AuthHookManager; @@ -165,6 +165,10 @@ public void verify() { if (env == null) { throw new NullPointerException("Env must not be null"); } + if(netty == null) + { + throw new NullPointerException("Netty must not be null"); + } } } @@ -349,12 +353,13 @@ public LaunchServer(Path dir, String[] args) throws IOException, InvalidKeySpecE Class.forName("jline.Terminal"); // JLine2 available - localCommandHandler = new JLineCommandHandler(this); + localCommandHandler = new JLineCommandHandler(); LogHelper.info("JLine2 terminal enabled"); } catch (ClassNotFoundException ignored) { - localCommandHandler = new StdCommandHandler(this, true); + localCommandHandler = new StdCommandHandler(true); LogHelper.warning("JLine2 isn't in classpath, using std"); } + ru.gravit.launchserver.command.handler.CommandHandler.registerCommands(localCommandHandler); commandHandler = localCommandHandler; // Set key pair diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/MySQLSourceConfig.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/MySQLSourceConfig.java index 95c16c79..fccb3b40 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/MySQLSourceConfig.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/MySQLSourceConfig.java @@ -89,9 +89,9 @@ public synchronized Connection getConnection() throws SQLException { // Set HikariCP pool // Replace source with hds source = new HikariDataSource(cfg); - LogHelper.info("HikariCP pooling enabled for '%s'", poolName); + LogHelper.warning("HikariCP pooling enabled for '%s'", poolName); } catch (ClassNotFoundException ignored) { - LogHelper.warning("HikariCP isn't in classpath for '%s'", poolName); + LogHelper.debug("HikariCP isn't in classpath for '%s'", poolName); } } return source.getConnection(); diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/MysqlHWIDHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/MysqlHWIDHandler.java index b3852c7e..11ae3d6f 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/MysqlHWIDHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/MysqlHWIDHandler.java @@ -36,6 +36,11 @@ public class MysqlHWIDHandler extends HWIDHandler { private String banMessage; + private boolean compareMode = false; + //Using queryHWID "queryHwids": "SELECT * FROM `users_hwids` WHERE `totalMemory` = ? or `serialNumber` = ? or `HWDiskSerial` = ? or `processorID` = ?" + private int compare = 50; //При наборе схожести в 50 очков + private boolean oneCompareMode = false; + /* //Добавить поля hwid в базу с пользователями @@ -136,12 +141,31 @@ public void onCheckInfo(OshiHWID hwid, String username, Connection c) throws HWI a.setString(i + 1, CommonHelper.replace(paramsHwids[i], replaceParams)); } ResultSet set = a.executeQuery(); - if (set.next()) { + boolean isOne = false; + while(set.next()) { + if(!oneCompareMode) isOne = true; + if(compareMode) + { + OshiHWID db_hwid = new OshiHWID(); + db_hwid.serialNumber = set.getString(hwidFieldSerialNumber); + db_hwid.processorID = set.getString(hwidFieldProcessorID); + db_hwid.HWDiskSerial = set.getString(hwidFieldHWDiskSerial); + db_hwid.totalMemory = Long.valueOf(set.getString(hwidFieldTotalMemory)); + LogHelper.dev("Compare HWID: %s vs %s", hwid.getSerializeString(), db_hwid.getSerializeString()); + int compare_point = hwid.compare(db_hwid); + if(compare_point < compare) continue; + else + { + LogHelper.debug("User %s hwid check: found compare %d in %d", username, compare_point, set.getInt("id")); + } + } + if(oneCompareMode) isOne = true; boolean isBanned = set.getBoolean(hwidFieldBanned); if (isBanned) { throw new HWIDException(banMessage); } - } else { + } + if(isOne) { onUpdateInfo(hwid, username, c); } } catch (SQLException e) { diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/binary/JAConfigurator.java b/LaunchServer/src/main/java/ru/gravit/launchserver/binary/JAConfigurator.java index d6d683c0..e4280842 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/binary/JAConfigurator.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/binary/JAConfigurator.java @@ -36,7 +36,7 @@ public void addModuleClass(String fullName) { moduleBody.append("();"); moduleBody.append("ru.gravit.launcher.Launcher.modulesManager.registerModule( mod"); moduleBody.append(autoincrement); - moduleBody.append(" , true );"); + moduleBody.append(");"); autoincrement++; } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/command/Command.java b/LaunchServer/src/main/java/ru/gravit/launchserver/command/Command.java index a69b023a..22817021 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/command/Command.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/command/Command.java @@ -1,28 +1,12 @@ package ru.gravit.launchserver.command; import ru.gravit.launchserver.LaunchServer; +import ru.gravit.utils.command.CommandException; import ru.gravit.utils.helper.VerifyHelper; import java.util.UUID; -public abstract class Command { - - protected static String parseUsername(String username) throws CommandException { - try { - return VerifyHelper.verifyUsername(username); - } catch (IllegalArgumentException e) { - throw new CommandException(e.getMessage()); - } - } - - - protected static UUID parseUUID(String s) throws CommandException { - try { - return UUID.fromString(s); - } catch (IllegalArgumentException ignored) { - throw new CommandException(String.format("Invalid UUID: '%s'", s)); - } - } +public abstract class Command extends ru.gravit.utils.command.Command { protected final LaunchServer server; @@ -31,19 +15,4 @@ protected static UUID parseUUID(String s) throws CommandException { protected Command(LaunchServer server) { this.server = server; } - - - public abstract String getArgsDescription(); // " [optional]" - - - public abstract String getUsageDescription(); - - - public abstract void invoke(String... args) throws Exception; - - - protected final void verifyArgs(String[] args, int min) throws CommandException { - if (args.length < min) - throw new CommandException("Command usage: " + getArgsDescription()); - } } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/command/auth/UUIDToUsernameCommand.java b/LaunchServer/src/main/java/ru/gravit/launchserver/command/auth/UUIDToUsernameCommand.java index 918a5e7f..75f7e774 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/command/auth/UUIDToUsernameCommand.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/command/auth/UUIDToUsernameCommand.java @@ -2,7 +2,7 @@ import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.command.Command; -import ru.gravit.launchserver.command.CommandException; +import ru.gravit.utils.command.CommandException; import ru.gravit.utils.helper.LogHelper; import java.io.IOException; diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/command/auth/UsernameToUUIDCommand.java b/LaunchServer/src/main/java/ru/gravit/launchserver/command/auth/UsernameToUUIDCommand.java index 24d226ac..ac3afaad 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/command/auth/UsernameToUUIDCommand.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/command/auth/UsernameToUUIDCommand.java @@ -2,7 +2,7 @@ import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.command.Command; -import ru.gravit.launchserver.command.CommandException; +import ru.gravit.utils.command.CommandException; import ru.gravit.utils.helper.LogHelper; import java.io.IOException; diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/command/basic/HelpCommand.java b/LaunchServer/src/main/java/ru/gravit/launchserver/command/basic/HelpCommand.java index 34dd6282..f4fb699f 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/command/basic/HelpCommand.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/command/basic/HelpCommand.java @@ -1,13 +1,13 @@ package ru.gravit.launchserver.command.basic; import ru.gravit.launchserver.LaunchServer; -import ru.gravit.launchserver.command.Command; -import ru.gravit.launchserver.command.CommandException; +import ru.gravit.utils.command.Command; +import ru.gravit.utils.command.CommandException; import ru.gravit.utils.helper.LogHelper; import java.util.Map.Entry; -public final class HelpCommand extends Command { +public final class HelpCommand extends ru.gravit.launchserver.command.Command { private static void printCommand(String name, Command command) { String args = command.getArgsDescription(); LogHelper.subInfo("%s %s - %s", name, args == null ? "[nothing]" : args, command.getUsageDescription()); diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/command/handler/CommandHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/command/handler/CommandHandler.java index 1da94327..d92c4b2c 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/command/handler/CommandHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/command/handler/CommandHandler.java @@ -2,7 +2,7 @@ import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.command.Command; -import ru.gravit.launchserver.command.CommandException; +import ru.gravit.utils.command.CommandException; import ru.gravit.launchserver.command.auth.*; import ru.gravit.launchserver.command.basic.*; import ru.gravit.launchserver.command.dump.DumpEntryCacheCommand; @@ -13,6 +13,7 @@ import ru.gravit.launchserver.command.modules.LoadModuleCommand; import ru.gravit.launchserver.command.modules.ModulesCommand; import ru.gravit.launchserver.command.service.*; +import ru.gravit.utils.helper.CommonHelper; import ru.gravit.utils.helper.LogHelper; import ru.gravit.utils.helper.VerifyHelper; @@ -22,191 +23,59 @@ import java.util.*; import java.util.concurrent.ConcurrentHashMap; -public abstract class CommandHandler implements Runnable { - private static String[] parse(CharSequence line) throws CommandException { - boolean quoted = false; - boolean wasQuoted = false; - - // Read line char by char - Collection result = new LinkedList<>(); - StringBuilder builder = new StringBuilder(100); - for (int i = 0; i <= line.length(); i++) { - boolean end = i >= line.length(); - char ch = end ? '\0' : line.charAt(i); - - // Maybe we should read next argument? - if (end || !quoted && Character.isWhitespace(ch)) { - if (end && quoted) - throw new CommandException("Quotes wasn't closed"); - - // Empty args are ignored (except if was quoted) - if (wasQuoted || builder.length() > 0) - result.add(builder.toString()); - - // Reset file builder - wasQuoted = false; - builder.setLength(0); - continue; - } - - // Append next char - switch (ch) { - case '"': // "abc"de, "abc""de" also allowed - quoted = !quoted; - wasQuoted = true; - break; - case '\\': // All escapes, including spaces etc - if (i + 1 >= line.length()) - throw new CommandException("Escape character is not specified"); - char next = line.charAt(i + 1); - builder.append(next); - i++; - break; - default: // Default char, simply append - builder.append(ch); - break; - } - } - - // Return result as array - return result.toArray(new String[0]); - } - - private final Map commands = new ConcurrentHashMap<>(32); - - protected CommandHandler(LaunchServer server) { +public abstract class CommandHandler extends ru.gravit.utils.command.CommandHandler { + public static void registerCommands(ru.gravit.utils.command.CommandHandler handler) + { + LaunchServer server = LaunchServer.server; // Register basic commands - registerCommand("help", new HelpCommand(server)); - registerCommand("version", new VersionCommand(server)); - registerCommand("build", new BuildCommand(server)); - registerCommand("stop", new StopCommand(server)); - registerCommand("restart", new RestartCommand(server)); - registerCommand("rebind", new RebindCommand(server)); - registerCommand("debug", new DebugCommand(server)); - registerCommand("clear", new ClearCommand(server)); - registerCommand("gc", new GCCommand(server)); - registerCommand("proguardClean", new ProguardCleanCommand(server)); - registerCommand("proguardDictRegen", new RegenProguardDictCommand(server)); - registerCommand("proguardMappingsRemove", new RemoveMappingsProguardCommand(server)); - registerCommand("logConnections", new LogConnectionsCommand(server)); - registerCommand("loadModule", new LoadModuleCommand(server)); - registerCommand("modules", new ModulesCommand(server)); - registerCommand("test", new TestCommand(server)); + handler.registerCommand("help", new HelpCommand(server)); + handler.registerCommand("version", new VersionCommand(server)); + handler.registerCommand("build", new BuildCommand(server)); + handler.registerCommand("stop", new StopCommand(server)); + handler.registerCommand("restart", new RestartCommand(server)); + handler.registerCommand("rebind", new RebindCommand(server)); + handler.registerCommand("debug", new DebugCommand(server)); + handler.registerCommand("clear", new ClearCommand(server)); + handler.registerCommand("gc", new GCCommand(server)); + handler.registerCommand("proguardClean", new ProguardCleanCommand(server)); + handler.registerCommand("proguardDictRegen", new RegenProguardDictCommand(server)); + handler.registerCommand("proguardMappingsRemove", new RemoveMappingsProguardCommand(server)); + handler.registerCommand("logConnections", new LogConnectionsCommand(server)); + handler.registerCommand("loadModule", new LoadModuleCommand(server)); + handler.registerCommand("modules", new ModulesCommand(server)); + handler.registerCommand("test", new TestCommand(server)); // Register sync commands - registerCommand("indexAsset", new IndexAssetCommand(server)); - registerCommand("unindexAsset", new UnindexAssetCommand(server)); - registerCommand("downloadAsset", new DownloadAssetCommand(server)); - registerCommand("downloadClient", new DownloadClientCommand(server)); - registerCommand("syncBinaries", new SyncBinariesCommand(server)); - registerCommand("syncUpdates", new SyncUpdatesCommand(server)); - registerCommand("syncProfiles", new SyncProfilesCommand(server)); + handler.registerCommand("indexAsset", new IndexAssetCommand(server)); + handler.registerCommand("unindexAsset", new UnindexAssetCommand(server)); + handler.registerCommand("downloadAsset", new DownloadAssetCommand(server)); + handler.registerCommand("downloadClient", new DownloadClientCommand(server)); + handler.registerCommand("syncBinaries", new SyncBinariesCommand(server)); + handler.registerCommand("syncUpdates", new SyncUpdatesCommand(server)); + handler.registerCommand("syncProfiles", new SyncProfilesCommand(server)); // Register auth commands - registerCommand("auth", new AuthCommand(server)); - registerCommand("usernameToUUID", new UsernameToUUIDCommand(server)); - registerCommand("uuidToUsername", new UUIDToUsernameCommand(server)); - registerCommand("ban", new BanCommand(server)); - registerCommand("unban", new UnbanCommand(server)); + handler.registerCommand("auth", new AuthCommand(server)); + handler.registerCommand("usernameToUUID", new UsernameToUUIDCommand(server)); + handler.registerCommand("uuidToUsername", new UUIDToUsernameCommand(server)); + handler.registerCommand("ban", new BanCommand(server)); + handler.registerCommand("unban", new UnbanCommand(server)); //Register dump commands - registerCommand("dumpSessions", new DumpSessionsCommand(server)); - registerCommand("dumpEntryCache", new DumpEntryCacheCommand(server)); + handler.registerCommand("dumpSessions", new DumpSessionsCommand(server)); + handler.registerCommand("dumpEntryCache", new DumpEntryCacheCommand(server)); //Register service commands - registerCommand("reload", new ReloadCommand(server)); - registerCommand("reloadAll", new ReloadAllCommand(server)); - registerCommand("reloadList", new ReloadListCommand(server)); - registerCommand("config", new ConfigCommand(server)); - registerCommand("configHelp", new ConfigHelpCommand(server)); - registerCommand("configList", new ConfigListCommand(server)); - registerCommand("swapAuthProvider", new SwapAuthProviderCommand(server)); - registerCommand("serverStatus", new ServerStatusCommand(server)); - registerCommand("checkInstall", new CheckInstallCommand(server)); - registerCommand("multi", new MultiCommand(server)); - registerCommand("getModulus", new GetModulusCommand(server)); - } - - - public abstract void bell() throws IOException; - - - public abstract void clear() throws IOException; - - - public final Map commandsMap() { - return Collections.unmodifiableMap(commands); - } - - - public final void eval(String line, boolean bell) { - LogHelper.info("Command '%s'", line); - - // Parse line to tokens - String[] args; - try { - args = parse(line); - } catch (Exception e) { - LogHelper.error(e); - return; - } - - // Evaluate command - eval(args, bell); - } - - - public final void eval(String[] args, boolean bell) { - if (args.length == 0) - return; - - // Measure start time and invoke command - Instant startTime = Instant.now(); - try { - lookup(args[0]).invoke(Arrays.copyOfRange(args, 1, args.length)); - } catch (Exception e) { - LogHelper.error(e); - } - - // Bell if invocation took > 1s - Instant endTime = Instant.now(); - if (bell && Duration.between(startTime, endTime).getSeconds() >= 5) - try { - bell(); - } catch (IOException e) { - LogHelper.error(e); - } - } - - - public final Command lookup(String name) throws CommandException { - Command command = commands.get(name); - if (command == null) - throw new CommandException(String.format("Unknown command: '%s'", name)); - return command; - } - - - public abstract String readLine() throws IOException; - - private void readLoop() throws IOException { - for (String line = readLine(); line != null; line = readLine()) - eval(line, true); - } - - - public final void registerCommand(String name, Command command) { - VerifyHelper.verifyIDName(name); - VerifyHelper.putIfAbsent(commands, name, Objects.requireNonNull(command, "command"), - String.format("Command has been already registered: '%s'", name)); - } - - @Override - public final void run() { - try { - readLoop(); - } catch (IOException e) { - LogHelper.error(e); - } + handler.registerCommand("reload", new ReloadCommand(server)); + handler.registerCommand("reloadAll", new ReloadAllCommand(server)); + handler.registerCommand("reloadList", new ReloadListCommand(server)); + handler.registerCommand("config", new ConfigCommand(server)); + handler.registerCommand("configHelp", new ConfigHelpCommand(server)); + handler.registerCommand("configList", new ConfigListCommand(server)); + handler.registerCommand("swapAuthProvider", new SwapAuthProviderCommand(server)); + handler.registerCommand("serverStatus", new ServerStatusCommand(server)); + handler.registerCommand("checkInstall", new CheckInstallCommand(server)); + handler.registerCommand("multi", new MultiCommand(server)); + handler.registerCommand("getModulus", new GetModulusCommand(server)); } } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/command/hash/DownloadClientCommand.java b/LaunchServer/src/main/java/ru/gravit/launchserver/command/hash/DownloadClientCommand.java index 2d0a5a88..7886f1f3 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/command/hash/DownloadClientCommand.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/command/hash/DownloadClientCommand.java @@ -4,7 +4,7 @@ import ru.gravit.launcher.profiles.ClientProfile.Version; import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.command.Command; -import ru.gravit.launchserver.command.CommandException; +import ru.gravit.utils.command.CommandException; import ru.gravit.utils.HttpDownloader; import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.LogHelper; diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/command/hash/IndexAssetCommand.java b/LaunchServer/src/main/java/ru/gravit/launchserver/command/hash/IndexAssetCommand.java index 4dfd6710..0d220e84 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/command/hash/IndexAssetCommand.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/command/hash/IndexAssetCommand.java @@ -4,7 +4,7 @@ import com.google.gson.JsonObject; import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.command.Command; -import ru.gravit.launchserver.command.CommandException; +import ru.gravit.utils.command.CommandException; 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/command/hash/UnindexAssetCommand.java b/LaunchServer/src/main/java/ru/gravit/launchserver/command/hash/UnindexAssetCommand.java index 9bb0c797..b74f7c9d 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/command/hash/UnindexAssetCommand.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/command/hash/UnindexAssetCommand.java @@ -5,7 +5,7 @@ import com.google.gson.JsonParser; import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.command.Command; -import ru.gravit.launchserver.command.CommandException; +import ru.gravit.utils.command.CommandException; import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.LogHelper; diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/command/install/CheckInstallCommand.java b/LaunchServer/src/main/java/ru/gravit/launchserver/command/install/CheckInstallCommand.java index 0c9c2ae2..72e78c22 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/command/install/CheckInstallCommand.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/command/install/CheckInstallCommand.java @@ -2,6 +2,7 @@ import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.command.Command; +import ru.gravit.utils.helper.JVMHelper; import ru.gravit.utils.helper.LogHelper; public class CheckInstallCommand extends Command { @@ -22,5 +23,6 @@ public String getUsageDescription() { @Override public void invoke(String... args) throws Exception { LogHelper.info("Check install success"); + JVMHelper.RUNTIME.exit(0); } } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/fileserver/FileServerHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/fileserver/FileServerHandler.java index 3b78e8ad..8d6165d1 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/fileserver/FileServerHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/fileserver/FileServerHandler.java @@ -130,22 +130,6 @@ public void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) thr lastContentFuture = sendFileFuture; } - sendFileFuture.addListener(new ChannelProgressiveFutureListener() { - @Override - public void operationProgressed(ChannelProgressiveFuture future, long progress, long total) { - if (total < 0) { // total unknown - System.err.println(future.channel() + " Transfer progress: " + progress); - } else { - System.err.println(future.channel() + " Transfer progress: " + progress + " / " + total); - } - } - - @Override - public void operationComplete(ChannelProgressiveFuture future) { - System.err.println(future.channel() + " Transfer complete."); - } - }); - // Decide whether to close the connection or not. if (!HttpUtil.isKeepAlive(request)) { lastContentFuture.addListener(new ClosingChannelFutureListener(raf)); diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/response/auth/CheckServerResponse.java b/LaunchServer/src/main/java/ru/gravit/launchserver/response/auth/CheckServerResponse.java index dbdd2b44..b2974674 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/response/auth/CheckServerResponse.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/response/auth/CheckServerResponse.java @@ -28,7 +28,7 @@ public void reply() throws IOException { debug("Username: %s, Server ID: %s", username, serverID); Client clientData = server.sessionManager.getClient(session); if (!clientData.isAuth || clientData.type != Client.Type.SERVER) { - requestError("Assess denied"); + requestError("Access denied"); return; } // Try check server with auth handler diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/response/auth/JoinServerResponse.java b/LaunchServer/src/main/java/ru/gravit/launchserver/response/auth/JoinServerResponse.java index dd5d9463..821848a6 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/response/auth/JoinServerResponse.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/response/auth/JoinServerResponse.java @@ -26,7 +26,7 @@ public void reply() throws IOException { String serverID = VerifyHelper.verifyServerID(input.readASCII(SerializeLimits.MAX_SERVERID)); // With minus sign Client clientData = server.sessionManager.getClient(session); if (!clientData.isAuth || clientData.type != Client.Type.USER) { - requestError("Assess denied"); + requestError("Access denied"); return; } // Try join server with auth handler diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/response/update/ProfilesResponse.java b/LaunchServer/src/main/java/ru/gravit/launchserver/response/update/ProfilesResponse.java index 039e018b..47dd872e 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/response/update/ProfilesResponse.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/response/update/ProfilesResponse.java @@ -25,7 +25,7 @@ public void reply() throws IOException { input.readBoolean(); if (client.type == Client.Type.USER && !client.checkSign) { LogHelper.warning("User session: %d ip %s try get profiles", session, ip); - requestError("Assess denied"); + requestError("Access denied"); return; } writeNoError(output); diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/response/update/UpdateResponse.java b/LaunchServer/src/main/java/ru/gravit/launchserver/response/update/UpdateResponse.java index 530b489d..8e4c547b 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/response/update/UpdateResponse.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/response/update/UpdateResponse.java @@ -39,7 +39,7 @@ public void reply() throws IOException { } Client clientData = server.sessionManager.getClient(session); if (!clientData.isAuth || clientData.type != Client.Type.USER || clientData.profile == null) { - requestError("Assess denied"); + requestError("Access denied"); return; } if (!clientData.permissions.canAdmin) { diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/update/UpdateResponse.java b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/update/UpdateResponse.java index 6c0502c6..26965f25 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/update/UpdateResponse.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/update/UpdateResponse.java @@ -19,7 +19,7 @@ public String getType() { @Override public void execute(WebSocketService service, ChannelHandlerContext ctx, Client client) throws Exception { if (!client.isAuth || client.type != Client.Type.USER || client.profile == null) { - service.sendObject(ctx,new ErrorRequestEvent("Assess denied")); + service.sendObject(ctx,new ErrorRequestEvent("Access denied")); return; } if (!client.permissions.canAdmin) { diff --git a/LaunchServerConsole/build.gradle b/LaunchServerConsole/build.gradle new file mode 100644 index 00000000..78f606d2 --- /dev/null +++ b/LaunchServerConsole/build.gradle @@ -0,0 +1,20 @@ +String mainClassName = "ru.gravit.launchserver.console.ConsoleMain" + + +repositories { + maven { + url "http://repo.spring.io/plugins-release/" + } +} + +sourceCompatibility = '1.8' +targetCompatibility = '1.8' + +jar { + from { configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) } } + manifest.attributes("Main-Class": mainClassName) +} + +dependencies { + compileOnly project(':ServerWrapper') +} diff --git a/LaunchServerConsole/src/main/java/ru/gravit/launchserver/console/ConsoleMain.java b/LaunchServerConsole/src/main/java/ru/gravit/launchserver/console/ConsoleMain.java new file mode 100644 index 00000000..652e89e3 --- /dev/null +++ b/LaunchServerConsole/src/main/java/ru/gravit/launchserver/console/ConsoleMain.java @@ -0,0 +1,35 @@ +package ru.gravit.launchserver.console; + +import ru.gravit.launcher.server.ServerWrapper; +import ru.gravit.utils.command.CommandHandler; +import ru.gravit.utils.command.JLineCommandHandler; +import ru.gravit.utils.command.StdCommandHandler; +import ru.gravit.utils.helper.LogHelper; + +import java.io.IOException; + +public class ConsoleMain { + public static CommandHandler commandHandler; + public static void main(String[] args) throws IOException { + if(ServerWrapper.config == null) + { + LogHelper.warning("ServerWrapper not found"); + } + if(!ServerWrapper.permissions.canAdmin) + { + LogHelper.warning("Permission canAdmin not found"); + } + try { + Class.forName("jline.Terminal"); + + // JLine2 available + commandHandler = new RemoteJLineCommandHandler(); + LogHelper.info("JLine2 terminal enabled"); + } catch (ClassNotFoundException ignored) { + commandHandler = new RemoteStdCommandHandler(true); + LogHelper.warning("JLine2 isn't in classpath, using std"); + } + LogHelper.info("CommandHandler started. Use 'exit' to exit this console"); + commandHandler.run(); + } +} diff --git a/LaunchServerConsole/src/main/java/ru/gravit/launchserver/console/RemoteJLineCommandHandler.java b/LaunchServerConsole/src/main/java/ru/gravit/launchserver/console/RemoteJLineCommandHandler.java new file mode 100644 index 00000000..bfdb634f --- /dev/null +++ b/LaunchServerConsole/src/main/java/ru/gravit/launchserver/console/RemoteJLineCommandHandler.java @@ -0,0 +1,24 @@ +package ru.gravit.launchserver.console; + +import ru.gravit.launcher.request.admin.ExecCommandRequest; +import ru.gravit.utils.command.Command; +import ru.gravit.utils.command.JLineCommandHandler; +import ru.gravit.utils.helper.LogHelper; + +import java.io.IOException; + +public class RemoteJLineCommandHandler extends JLineCommandHandler { + public RemoteJLineCommandHandler() throws IOException { + } + @Override + public void eval(String line, boolean bell) + { + if(line.equals("exit")) System.exit(0); + ExecCommandRequest request = new ExecCommandRequest(System.out::println, line); + try { + request.request(); + } catch (Exception e) { + LogHelper.error(e); + } + } +} diff --git a/LaunchServerConsole/src/main/java/ru/gravit/launchserver/console/RemoteStdCommandHandler.java b/LaunchServerConsole/src/main/java/ru/gravit/launchserver/console/RemoteStdCommandHandler.java new file mode 100644 index 00000000..b5255d66 --- /dev/null +++ b/LaunchServerConsole/src/main/java/ru/gravit/launchserver/console/RemoteStdCommandHandler.java @@ -0,0 +1,24 @@ +package ru.gravit.launchserver.console; + +import ru.gravit.launcher.request.admin.ExecCommandRequest; +import ru.gravit.utils.command.Command; +import ru.gravit.utils.command.CommandHandler; +import ru.gravit.utils.command.StdCommandHandler; +import ru.gravit.utils.helper.LogHelper; + +public class RemoteStdCommandHandler extends StdCommandHandler { + public RemoteStdCommandHandler(boolean readCommands) { + super(readCommands); + } + @Override + public void eval(String line, boolean bell) + { + if(line.equals("exit")) System.exit(0); + ExecCommandRequest request = new ExecCommandRequest(System.out::println, line); + try { + request.request(); + } catch (Exception e) { + LogHelper.error(e); + } + } +} diff --git a/Launcher/src/main/java/ru/gravit/launcher/ClientLauncherWrapper.java b/Launcher/src/main/java/ru/gravit/launcher/ClientLauncherWrapper.java index 39a9633c..3c2d9df1 100644 --- a/Launcher/src/main/java/ru/gravit/launcher/ClientLauncherWrapper.java +++ b/Launcher/src/main/java/ru/gravit/launcher/ClientLauncherWrapper.java @@ -44,9 +44,9 @@ public static void main(String[] arguments) throws IOException, InterruptedExcep if (!process.isAlive()) { int errorcode = process.exitValue(); if (errorcode != 0) - LogHelper.error("Process exit witch error code: %d", errorcode); + LogHelper.error("Process exit with error code: %d", errorcode); else - LogHelper.info("Process exit witch code 0"); + LogHelper.info("Process exit with code 0"); } else { LogHelper.debug("Process started success"); } diff --git a/Launcher/src/main/java/ru/gravit/launcher/hwid/OshiHWIDProvider.java b/Launcher/src/main/java/ru/gravit/launcher/hwid/OshiHWIDProvider.java index 01909b20..4c2ea950 100644 --- a/Launcher/src/main/java/ru/gravit/launcher/hwid/OshiHWIDProvider.java +++ b/Launcher/src/main/java/ru/gravit/launcher/hwid/OshiHWIDProvider.java @@ -62,6 +62,29 @@ public String getHWDisk() { } } + public String getSoundCardInfo() + { + for(SoundCard soundcard : hardware.getSoundCards()) + { + return soundcard.getName(); + } + return ""; + } + + public String getMacAddr() + { + for(NetworkIF networkIF : hardware.getNetworkIFs()) + { + for(String ipv4 : networkIF.getIPv4addr()) + { + if(ipv4.startsWith("127.")) continue; + if(ipv4.startsWith("10.")) continue; + return networkIF.getMacaddr(); + } + } + return ""; + } + public long getTotalMemory() { if (noHWID) return -1; if (hardware == null) hardware = systemInfo.getHardware(); @@ -85,6 +108,20 @@ public void printHardwareInformation() { for (UsbDevice s : hardware.getUsbDevices(true)) { LogHelper.debug("USBDevice Serial: %s Name: %s", s.getSerialNumber(), s.getName()); } + for(NetworkIF networkIF : hardware.getNetworkIFs()) + { + LogHelper.debug("Network Interface: %s mac: %s", networkIF.getName(), networkIF.getMacaddr()); + for(String ipv4 : networkIF.getIPv4addr()) + { + if(ipv4.startsWith("127.")) continue; + if(ipv4.startsWith("10.")) continue; + LogHelper.subDebug("IPv4: %s", ipv4); + } + } + for(SoundCard soundcard : hardware.getSoundCards()) + { + LogHelper.debug("SoundCard %s", soundcard.getName()); + } CentralProcessor processor = hardware.getProcessor(); LogHelper.debug("Processor Model: %s ID: %s", processor.getModel(), processor.getProcessorID()); } catch (Throwable e) { @@ -100,6 +137,7 @@ public HWID getHWID() { hwid.totalMemory = getTotalMemory(); hwid.HWDiskSerial = getHWDisk(); hwid.processorID = getProcessorID(); + hwid.macAddr = getMacAddr(); printHardwareInformation(); return hwid; } diff --git a/libLauncher/build.gradle b/libLauncher/build.gradle index 0e527171..e5db09f7 100644 --- a/libLauncher/build.gradle +++ b/libLauncher/build.gradle @@ -3,5 +3,6 @@ dependencies { compileOnly 'org.fusesource.jansi:jansi:1.17.1' + compileOnly 'jline:jline:2.14.6' compile 'com.google.code.gson:gson:2.8.5' } diff --git a/libLauncher/src/main/java/ru/gravit/launcher/HWID.java b/libLauncher/src/main/java/ru/gravit/launcher/HWID.java index 4daa1c31..c72e8e74 100644 --- a/libLauncher/src/main/java/ru/gravit/launcher/HWID.java +++ b/libLauncher/src/main/java/ru/gravit/launcher/HWID.java @@ -5,5 +5,7 @@ public interface HWID { int getLevel(); //Уровень доверия, насколько уникальные значения + int compare(HWID hwid); + boolean isNull(); } diff --git a/libLauncher/src/main/java/ru/gravit/launcher/Launcher.java b/libLauncher/src/main/java/ru/gravit/launcher/Launcher.java index ccca5eaa..6bfbdec0 100644 --- a/libLauncher/src/main/java/ru/gravit/launcher/Launcher.java +++ b/libLauncher/src/main/java/ru/gravit/launcher/Launcher.java @@ -60,7 +60,7 @@ public final class Launcher { private static final Pattern UUID_PATTERN = Pattern.compile("-", Pattern.LITERAL); public static final int MAJOR = 4; public static final int MINOR = 4; - public static final int PATCH = 0; + public static final int PATCH = 1; public static final int BUILD = 1; public static final Version.Type RELEASE = Version.Type.STABLE; public static GsonBuilder gsonBuilder; diff --git a/libLauncher/src/main/java/ru/gravit/launcher/OshiHWID.java b/libLauncher/src/main/java/ru/gravit/launcher/OshiHWID.java index 1d13951d..4911d294 100644 --- a/libLauncher/src/main/java/ru/gravit/launcher/OshiHWID.java +++ b/libLauncher/src/main/java/ru/gravit/launcher/OshiHWID.java @@ -12,6 +12,8 @@ public class OshiHWID implements HWID { public String HWDiskSerial; @LauncherAPI public String processorID; + @LauncherAPI + public String macAddr; @Override public String getSerializeString() { @@ -22,15 +24,33 @@ public String getSerializeString() { public int getLevel() //Уровень доверия, насколько уникальные значения { int result = 0; - if (totalMemory != 0) result++; - if (serialNumber != null && !serialNumber.equals("unknown")) result += 4; - if (HWDiskSerial != null && !HWDiskSerial.equals("unknown")) result += 15; - if (processorID != null && !processorID.equals("unknown")) result += 6; + if (totalMemory != 0) result+=8; + if (serialNumber != null && !serialNumber.equals("unknown")) result += 12; + if (HWDiskSerial != null && !HWDiskSerial.equals("unknown")) result += 30; + if (processorID != null && !processorID.equals("unknown")) result += 10; + if (macAddr != null && !macAddr.equals("00:00:00:00:00:00")) result += 15; return result; } + @Override + public int compare(HWID hwid) { + if(hwid instanceof OshiHWID) + { + int rate = 0; + OshiHWID oshi = (OshiHWID) hwid; + if(Math.abs(oshi.totalMemory - totalMemory) < 1024*1024) rate+=5; + if(oshi.totalMemory == totalMemory) rate+=15; + if(oshi.HWDiskSerial.equals(HWDiskSerial)) rate+=45; + if(oshi.processorID.equals(processorID)) rate+=18; + if(oshi.serialNumber.equals(serialNumber)) rate+=15; + if(oshi.macAddr.equals(macAddr)) rate+=19; + return rate; + } + return 0; + } + @Override public boolean isNull() { - return getLevel() < 2; + return getLevel() < 15; } } diff --git a/libLauncher/src/main/java/ru/gravit/launcher/managers/SimpleModuleManager.java b/libLauncher/src/main/java/ru/gravit/launcher/managers/SimpleModuleManager.java index fc4202d5..4ac0336f 100644 --- a/libLauncher/src/main/java/ru/gravit/launcher/managers/SimpleModuleManager.java +++ b/libLauncher/src/main/java/ru/gravit/launcher/managers/SimpleModuleManager.java @@ -132,7 +132,7 @@ public void printModules() { @Override public void registerModule(Module module) { - load(module); + modules.add(module); LogHelper.info("Module %s version: %s registered", module.getName(), module.getVersion()); } 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 8de9245a..9d533337 100644 --- a/libLauncher/src/main/java/ru/gravit/launcher/profiles/ClientProfile.java +++ b/libLauncher/src/main/java/ru/gravit/launcher/profiles/ClientProfile.java @@ -391,6 +391,24 @@ public void verify() { // Client launcher VerifyHelper.verify(getTitle(), VerifyHelper.NOT_EMPTY, "Main class can't be empty"); + for(String s : classPath) + { + if(s == null) throw new IllegalArgumentException("Found null entry in classPath"); + } + for(String s : jvmArgs) + { + if(s == null) throw new IllegalArgumentException("Found null entry in jvmArgs"); + } + for(String s : clientArgs) + { + if(s == null) throw new IllegalArgumentException("Found null entry in clientArgs"); + } + for(OptionalFile f : updateOptional) + { + if(f == null) throw new IllegalArgumentException("Found null entry in updateOptional"); + if(f.name == null) throw new IllegalArgumentException("Optional: name must not be null"); + if(f.list == null) throw new IllegalArgumentException("Optional: list must not be null"); + } } @Override diff --git a/libLauncher/src/main/java/ru/gravit/utils/command/Command.java b/libLauncher/src/main/java/ru/gravit/utils/command/Command.java new file mode 100644 index 00000000..7e594d50 --- /dev/null +++ b/libLauncher/src/main/java/ru/gravit/utils/command/Command.java @@ -0,0 +1,40 @@ +package ru.gravit.utils.command; + +import ru.gravit.utils.helper.VerifyHelper; + +import java.util.UUID; + +public abstract class Command { + + + protected static String parseUsername(String username) throws CommandException { + try { + return VerifyHelper.verifyUsername(username); + } catch (IllegalArgumentException e) { + throw new CommandException(e.getMessage()); + } + } + + + protected static UUID parseUUID(String s) throws CommandException { + try { + return UUID.fromString(s); + } catch (IllegalArgumentException ignored) { + throw new CommandException(String.format("Invalid UUID: '%s'", s)); + } + } + + public abstract String getArgsDescription(); // " [optional]" + + + public abstract String getUsageDescription(); + + + public abstract void invoke(String... args) throws Exception; + + + protected final void verifyArgs(String[] args, int min) throws CommandException { + if (args.length < min) + throw new CommandException("Command usage: " + getArgsDescription()); + } +} diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/command/CommandException.java b/libLauncher/src/main/java/ru/gravit/utils/command/CommandException.java similarity index 90% rename from LaunchServer/src/main/java/ru/gravit/launchserver/command/CommandException.java rename to libLauncher/src/main/java/ru/gravit/utils/command/CommandException.java index 514a4571..ce2009b1 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/command/CommandException.java +++ b/libLauncher/src/main/java/ru/gravit/utils/command/CommandException.java @@ -1,4 +1,4 @@ -package ru.gravit.launchserver.command; +package ru.gravit.utils.command; public final class CommandException extends Exception { private static final long serialVersionUID = -6588814993972117772L; diff --git a/libLauncher/src/main/java/ru/gravit/utils/command/CommandHandler.java b/libLauncher/src/main/java/ru/gravit/utils/command/CommandHandler.java new file mode 100644 index 00000000..b66f2913 --- /dev/null +++ b/libLauncher/src/main/java/ru/gravit/utils/command/CommandHandler.java @@ -0,0 +1,104 @@ +package ru.gravit.utils.command; + +import ru.gravit.utils.helper.CommonHelper; +import ru.gravit.utils.helper.LogHelper; +import ru.gravit.utils.helper.VerifyHelper; + +import java.io.IOException; +import java.time.Duration; +import java.time.Instant; +import java.util.Arrays; +import java.util.Collections; +import java.util.Map; +import java.util.Objects; +import java.util.concurrent.ConcurrentHashMap; + +public abstract class CommandHandler implements Runnable { + private final Map commands = new ConcurrentHashMap<>(32); + + public void eval(String line, boolean bell) { + LogHelper.info("Command '%s'", line); + + // Parse line to tokens + String[] args; + try { + args = CommonHelper.parseCommand(line); + } catch (Exception e) { + LogHelper.error(e); + return; + } + + // Evaluate command + eval(args, bell); + } + + + public void eval(String[] args, boolean bell) { + if (args.length == 0) + return; + + // Measure start time and invoke command + Instant startTime = Instant.now(); + try { + lookup(args[0]).invoke(Arrays.copyOfRange(args, 1, args.length)); + } catch (Exception e) { + LogHelper.error(e); + } + + // Bell if invocation took > 1s + Instant endTime = Instant.now(); + if (bell && Duration.between(startTime, endTime).getSeconds() >= 5) + try { + bell(); + } catch (IOException e) { + LogHelper.error(e); + } + } + + + public Command lookup(String name) throws CommandException { + Command command = commands.get(name); + if (command == null) + throw new CommandException(String.format("Unknown command: '%s'", name)); + return command; + } + + + public abstract String readLine() throws IOException; + + private void readLoop() throws IOException { + for (String line = readLine(); line != null; line = readLine()) + eval(line, true); + } + + + public void registerCommand(String name, Command command) { + VerifyHelper.verifyIDName(name); + VerifyHelper.putIfAbsent(commands, name, Objects.requireNonNull(command, "command"), + String.format("Command has been already registered: '%s'", name)); + } + + @Override + public void run() { + try { + readLoop(); + } catch (IOException e) { + LogHelper.error(e); + } + } + + + + public abstract void bell() throws IOException; + + + public abstract void clear() throws IOException; + + + public Map commandsMap() { + return Collections.unmodifiableMap(commands); + } + + + +} diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/command/handler/JLineCommandHandler.java b/libLauncher/src/main/java/ru/gravit/utils/command/JLineCommandHandler.java similarity index 81% rename from LaunchServer/src/main/java/ru/gravit/launchserver/command/handler/JLineCommandHandler.java rename to libLauncher/src/main/java/ru/gravit/utils/command/JLineCommandHandler.java index 039afe3d..504393b7 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/command/handler/JLineCommandHandler.java +++ b/libLauncher/src/main/java/ru/gravit/utils/command/JLineCommandHandler.java @@ -1,13 +1,12 @@ -package ru.gravit.launchserver.command.handler; +package ru.gravit.utils.command; import jline.console.ConsoleReader; -import ru.gravit.launchserver.LaunchServer; import ru.gravit.utils.helper.LogHelper; import ru.gravit.utils.helper.LogHelper.Output; import java.io.IOException; -public final class JLineCommandHandler extends CommandHandler { +public class JLineCommandHandler extends CommandHandler { private final class JLineOutput implements Output { @Override public void println(String message) { @@ -23,8 +22,8 @@ public void println(String message) { private final ConsoleReader reader; - public JLineCommandHandler(LaunchServer server) throws IOException { - super(server); + public JLineCommandHandler() throws IOException { + super(); // Set reader reader = new ConsoleReader(); diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/command/handler/StdCommandHandler.java b/libLauncher/src/main/java/ru/gravit/utils/command/StdCommandHandler.java similarity index 67% rename from LaunchServer/src/main/java/ru/gravit/launchserver/command/handler/StdCommandHandler.java rename to libLauncher/src/main/java/ru/gravit/utils/command/StdCommandHandler.java index 7f77b039..27cab6cd 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/command/handler/StdCommandHandler.java +++ b/libLauncher/src/main/java/ru/gravit/utils/command/StdCommandHandler.java @@ -1,16 +1,15 @@ -package ru.gravit.launchserver.command.handler; +package ru.gravit.utils.command; -import ru.gravit.launchserver.LaunchServer; import ru.gravit.utils.helper.IOHelper; import java.io.BufferedReader; import java.io.IOException; -public final class StdCommandHandler extends CommandHandler { +public class StdCommandHandler extends CommandHandler { private final BufferedReader reader; - public StdCommandHandler(LaunchServer server, boolean readCommands) { - super(server); + public StdCommandHandler(boolean readCommands) { + super(); reader = readCommands ? IOHelper.newReader(System.in) : null; } diff --git a/libLauncher/src/main/java/ru/gravit/utils/downloader/Downloader.java b/libLauncher/src/main/java/ru/gravit/utils/downloader/Downloader.java index 12dfa4c1..9187a1f8 100644 --- a/libLauncher/src/main/java/ru/gravit/utils/downloader/Downloader.java +++ b/libLauncher/src/main/java/ru/gravit/utils/downloader/Downloader.java @@ -39,6 +39,8 @@ public interface Handler { private final int skip; private final Handler handler; + private HttpURLConnection connect = null; + public Downloader(URL url, File file) { this.requestProps = new HashMap<>(requestClient); this.file = file; @@ -128,6 +130,7 @@ public void downloadFile() throws IOException { interrupted.set(false); if (url.getProtocol().equalsIgnoreCase("http")) { HttpURLConnection connect = (HttpURLConnection) (url).openConnection(); + this.connect = connect; if (method != null) connect.setRequestMethod(method); for (Map.Entry ent : requestProps.entrySet()) { connect.setRequestProperty(ent.getKey(), ent.getValue()); @@ -158,6 +161,7 @@ public void downloadFile() throws IOException { } } else { HttpsURLConnection connect = (HttpsURLConnection) (url).openConnection(); + this.connect = connect; if (method != null) connect.setRequestMethod(method); for (Map.Entry ent : requestProps.entrySet()) { connect.setRequestProperty(ent.getKey(), ent.getValue()); @@ -200,5 +204,9 @@ public void run() { this.ex.set(ex); LogHelper.error(ex); } + if (connect != null) + try { + connect.disconnect(); + } catch (Throwable ignored) { } } } diff --git a/libLauncher/src/main/java/ru/gravit/utils/downloader/DownloadingThread.java b/libLauncher/src/main/java/ru/gravit/utils/downloader/DownloadingThread.java deleted file mode 100644 index 01fb2691..00000000 --- a/libLauncher/src/main/java/ru/gravit/utils/downloader/DownloadingThread.java +++ /dev/null @@ -1,34 +0,0 @@ -package ru.gravit.utils.downloader; - -import java.io.File; -import java.net.URL; - -public class DownloadingThread extends Thread { - private final Downloader runnable; - - public DownloadingThread(File file, URL url, String name) { - super(name); - runnable = new Downloader(url, file); - } - - public Downloader getDownloader() { - return runnable; - } - - @Override - public void interrupt() { - runnable.interrupt.set(true); - while (!runnable.interrupted.get()) { - } - super.interrupt(); - } - - public void hardInterrupt() { - super.interrupt(); - } - - @Override - public void run() { - runnable.run(); - } -} diff --git a/libLauncher/src/main/java/ru/gravit/utils/helper/CommonHelper.java b/libLauncher/src/main/java/ru/gravit/utils/helper/CommonHelper.java index bc7d21f7..5f7548e3 100644 --- a/libLauncher/src/main/java/ru/gravit/utils/helper/CommonHelper.java +++ b/libLauncher/src/main/java/ru/gravit/utils/helper/CommonHelper.java @@ -1,10 +1,13 @@ package ru.gravit.utils.helper; import ru.gravit.launcher.LauncherAPI; +import ru.gravit.utils.command.CommandException; import javax.script.ScriptEngine; import javax.script.ScriptEngineFactory; import javax.script.ScriptEngineManager; +import java.util.Collection; +import java.util.LinkedList; import java.util.Locale; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -68,4 +71,53 @@ public static String replace(String source, String... params) { private CommonHelper() { } + + public static String[] parseCommand(CharSequence line) throws CommandException { + boolean quoted = false; + boolean wasQuoted = false; + + // Read line char by char + Collection result = new LinkedList<>(); + StringBuilder builder = new StringBuilder(100); + for (int i = 0; i <= line.length(); i++) { + boolean end = i >= line.length(); + char ch = end ? '\0' : line.charAt(i); + + // Maybe we should read next argument? + if (end || !quoted && Character.isWhitespace(ch)) { + if (end && quoted) + throw new CommandException("Quotes wasn't closed"); + + // Empty args are ignored (except if was quoted) + if (wasQuoted || builder.length() > 0) + result.add(builder.toString()); + + // Reset file builder + wasQuoted = false; + builder.setLength(0); + continue; + } + + // Append next char + switch (ch) { + case '"': // "abc"de, "abc""de" also allowed + quoted = !quoted; + wasQuoted = true; + break; + case '\\': // All escapes, including spaces etc + if (i + 1 >= line.length()) + throw new CommandException("Escape character is not specified"); + char next = line.charAt(i + 1); + builder.append(next); + i++; + break; + default: // Default char, simply append + builder.append(ch); + break; + } + } + + // Return result as array + return result.toArray(new String[0]); + } } diff --git a/modules b/modules index d4320ce2..b7fbe50c 160000 --- a/modules +++ b/modules @@ -1 +1 @@ -Subproject commit d4320ce29b8a142a75b86237139e92a72e00b481 +Subproject commit b7fbe50c08a3e274aadb93292fed6d05918647b3 diff --git a/settings.gradle b/settings.gradle index 0ef17ca5..3fc709d9 100644 --- a/settings.gradle +++ b/settings.gradle @@ -5,7 +5,8 @@ include 'LauncherAPI' include 'ServerWrapper' include 'LaunchServer' +include 'LaunchServerConsole' include 'modules' file('modules').eachDir { sub -> if (sub.name.endsWith('_module')) include 'modules:' + sub.name -} \ No newline at end of file +}