[FEATURE] ServerWrapper расширяет JsonConfigurable

This commit is contained in:
Gravit 2019-04-03 17:30:58 +07:00
parent a636fbe336
commit f1ccfbf58e
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
3 changed files with 88 additions and 66 deletions

View file

@ -11,6 +11,7 @@
import ru.gravit.launcher.request.update.ProfilesRequest; import ru.gravit.launcher.request.update.ProfilesRequest;
import ru.gravit.launcher.server.setup.ServerWrapperSetup; import ru.gravit.launcher.server.setup.ServerWrapperSetup;
import ru.gravit.utils.PublicURLClassLoader; import ru.gravit.utils.PublicURLClassLoader;
import ru.gravit.utils.config.JsonConfigurable;
import ru.gravit.utils.helper.CommonHelper; import ru.gravit.utils.helper.CommonHelper;
import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.IOHelper;
import ru.gravit.utils.helper.LogHelper; import ru.gravit.utils.helper.LogHelper;
@ -22,18 +23,20 @@
import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType; import java.lang.invoke.MethodType;
import java.lang.reflect.Type;
import java.net.URL; import java.net.URL;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.security.spec.InvalidKeySpecException;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
public class ServerWrapper { public class ServerWrapper extends JsonConfigurable<ServerWrapper.Config> {
public static ModulesManager modulesManager; public ModulesManager modulesManager;
public static Config config; public Config config;
public static PublicURLClassLoader ucp; public PublicURLClassLoader ucp;
public static ClassLoader loader; public ClassLoader loader;
public static ClientPermissions permissions; public ClientPermissions permissions;
public static ServerWrapper wrapper; public static ServerWrapper wrapper;
private static Gson gson; private static Gson gson;
private static GsonBuilder gsonBuiler; private static GsonBuilder gsonBuiler;
@ -43,41 +46,45 @@ public class ServerWrapper {
public static Path publicKeyFile = Paths.get(System.getProperty("serverwrapper.publicKeyFile", "public.key")); public static Path publicKeyFile = Paths.get(System.getProperty("serverwrapper.publicKeyFile", "public.key"));
public static boolean disableSetup = Boolean.valueOf(System.getProperty("serverwrapper.disableSetup", "false")); public static boolean disableSetup = Boolean.valueOf(System.getProperty("serverwrapper.disableSetup", "false"));
public static boolean auth(ServerWrapper wrapper) { public ServerWrapper(Type type, Path configPath) {
super(type, configPath);
}
public boolean auth() {
try { try {
LauncherConfig cfg = Launcher.getConfig(); LauncherConfig cfg = Launcher.getConfig();
ServerWrapper.permissions = new AuthServerRequest(cfg, config.login, SecurityHelper.newRSAEncryptCipher(cfg.publicKey).doFinal(IOHelper.encode(config.password)), config.auth_id, config.title).request(); permissions = new AuthServerRequest(cfg, config.login, SecurityHelper.newRSAEncryptCipher(cfg.publicKey).doFinal(IOHelper.encode(config.password)), config.auth_id, config.title).request();
ProfilesRequestEvent result = new ProfilesRequest(cfg).request(); ProfilesRequestEvent result = new ProfilesRequest(cfg).request();
for (ClientProfile p : result.profiles) { for (ClientProfile p : result.profiles) {
LogHelper.debug("Get profile: %s", p.getTitle()); LogHelper.debug("Get profile: %s", p.getTitle());
if (p.getTitle().equals(config.title)) { if (p.getTitle().equals(config.title)) {
wrapper.profile = p; profile = p;
Launcher.profile = p; Launcher.profile = p;
LogHelper.debug("Found profile: %s", Launcher.profile.getTitle()); LogHelper.debug("Found profile: %s", Launcher.profile.getTitle());
break; break;
} }
} }
if (wrapper.profile == null) { if (profile == null) {
LogHelper.error("Your profile not found"); LogHelper.error("Your profile not found");
if (ServerWrapper.config.stopOnError) System.exit(-1); if (config.stopOnError) System.exit(-1);
} }
return true; return true;
} catch (Throwable e) { } catch (Throwable e) {
LogHelper.error(e); LogHelper.error(e);
if (ServerWrapper.config.stopOnError) System.exit(-1); if (config.stopOnError) System.exit(-1);
return false; return false;
} }
} }
public static boolean loopAuth(ServerWrapper wrapper, int count, int sleeptime) { public boolean loopAuth(int count, int sleeptime) {
if (count == 0) { if (count == 0) {
while (true) { while (true) {
if (auth(wrapper)) return true; if (auth()) return true;
} }
} }
for (int i = 0; i < count; ++i) { for (int i = 0; i < count; ++i) {
if (auth(wrapper)) return true; if (auth()) return true;
try { try {
Thread.sleep(sleeptime); Thread.sleep(sleeptime);
} catch (InterruptedException e) { } catch (InterruptedException e) {
@ -95,21 +102,16 @@ public static void initGson() {
Launcher.gson = Launcher.gsonBuilder.create(); Launcher.gson = Launcher.gsonBuilder.create();
} }
public static void main(String... args) throws Throwable { public void run(String... args) throws Throwable
LogHelper.printVersion("ServerWrapper"); {
LogHelper.printLicense("ServerWrapper");
wrapper = new ServerWrapper();
gsonBuiler = new GsonBuilder(); gsonBuiler = new GsonBuilder();
gsonBuiler.setPrettyPrinting(); gsonBuiler.setPrettyPrinting();
gson = gsonBuiler.create(); gson = gsonBuiler.create();
initGson(); initGson();
if(args.length > 0 && args[0].equals("setup") && !disableSetup) if(args.length > 0 && args[0].equals("setup") && !disableSetup)
{ {
generateConfigIfNotExists();
LogHelper.debug("Read ServerWrapperConfig.json"); LogHelper.debug("Read ServerWrapperConfig.json");
try (Reader reader = IOHelper.newReader(configFile)) { loadConfig();
config = gson.fromJson(reader, Config.class);
}
ServerWrapperSetup setup = new ServerWrapperSetup(); ServerWrapperSetup setup = new ServerWrapperSetup();
setup.run(); setup.run();
System.exit(0); System.exit(0);
@ -119,18 +121,14 @@ public static void main(String... args) throws Throwable {
Launcher.modulesManager = modulesManager; Launcher.modulesManager = modulesManager;
modulesManager.preInitModules(); modulesManager.preInitModules();
LogHelper.debug("Read ServerWrapperConfig.json"); LogHelper.debug("Read ServerWrapperConfig.json");
generateConfigIfNotExists(); loadConfig();
try (Reader reader = IOHelper.newReader(configFile)) { updateLauncherConfig();
config = gson.fromJson(reader, Config.class);
}
LauncherConfig cfg = new LauncherConfig(config.address, config.port, SecurityHelper.toPublicRSAKey(IOHelper.read(publicKeyFile)), new HashMap<>(), config.projectname);
Launcher.setConfig(cfg);
if(config.env != null) Launcher.applyLauncherEnv(config.env); if(config.env != null) Launcher.applyLauncherEnv(config.env);
else Launcher.applyLauncherEnv(LauncherConfig.LauncherEnvironment.STD); else Launcher.applyLauncherEnv(LauncherConfig.LauncherEnvironment.STD);
if (config.logFile != null) LogHelper.addOutput(IOHelper.newWriter(Paths.get(config.logFile), true)); if (config.logFile != null) LogHelper.addOutput(IOHelper.newWriter(Paths.get(config.logFile), true));
if (config.syncAuth) auth(wrapper); if (config.syncAuth) auth();
else else
CommonHelper.newThread("Server Auth Thread", true, () -> ServerWrapper.loopAuth(wrapper, config.reconnectCount, config.reconnectSleep)); CommonHelper.newThread("Server Auth Thread", true, () -> loopAuth(config.reconnectCount, config.reconnectSleep));
modulesManager.initModules(); modulesManager.initModules();
String classname = (config.mainclass == null || config.mainclass.isEmpty()) ? args[0] : config.mainclass; String classname = (config.mainclass == null || config.mainclass.isEmpty()) ? args[0] : config.mainclass;
if (classname.length() == 0) { if (classname.length() == 0) {
@ -188,13 +186,32 @@ public static void main(String... args) throws Throwable {
mainMethod.invoke(config.args); mainMethod.invoke(config.args);
} }
} }
public void updateLauncherConfig()
{
private static void generateConfigIfNotExists() throws IOException { LauncherConfig cfg = null;
if (IOHelper.isFile(configFile)) try {
return; cfg = new LauncherConfig(config.address, config.port, SecurityHelper.toPublicRSAKey(IOHelper.read(publicKeyFile)), new HashMap<>(), config.projectname);
} catch (InvalidKeySpecException | IOException e) {
LogHelper.error(e);
}
Launcher.setConfig(cfg);
}
// Create new config public static void main(String... args) throws Throwable {
LogHelper.info("Creating ServerWrapper config"); LogHelper.printVersion("ServerWrapper");
LogHelper.printLicense("ServerWrapper");
ServerWrapper.wrapper = new ServerWrapper(ServerWrapper.Config.class, configFile);
ServerWrapper.wrapper.run(args);
}
@Override
public Config getConfig() {
return config;
}
@Override
public Config getDefaultConfig() {
Config newConfig = new Config(); Config newConfig = new Config();
newConfig.title = "Your profile title"; newConfig.title = "Your profile title";
newConfig.projectname = "MineCraft"; newConfig.projectname = "MineCraft";
@ -208,11 +225,12 @@ private static void generateConfigIfNotExists() throws IOException {
newConfig.reconnectCount = 10; newConfig.reconnectCount = 10;
newConfig.reconnectSleep = 1000; newConfig.reconnectSleep = 1000;
newConfig.env = LauncherConfig.LauncherEnvironment.STD; newConfig.env = LauncherConfig.LauncherEnvironment.STD;
return newConfig;
}
LogHelper.warning("Title is not set. Please show ServerWrapper.cfg"); @Override
public void setConfig(Config config) {
// Write LaunchServer config this.config = config;
newConfig.save();
} }
public static final class Config { public static final class Config {
@ -235,13 +253,6 @@ public static final class Config {
public String password; public String password;
public String auth_id = ""; public String auth_id = "";
public LauncherConfig.LauncherEnvironment env; public LauncherConfig.LauncherEnvironment env;
public void save() throws IOException
{
LogHelper.info("Writing ServerWrapper config file");
try (Writer writer = IOHelper.newWriter(configFile)) {
gson.toJson(this, writer);
}
}
} }
public ClientProfile profile; public ClientProfile profile;

View file

@ -16,8 +16,6 @@
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.security.spec.InvalidKeySpecException;
import java.util.HashMap;
import java.util.jar.JarFile; import java.util.jar.JarFile;
public class ServerWrapperSetup { public class ServerWrapperSetup {
@ -25,6 +23,7 @@ public class ServerWrapperSetup {
public PublicURLClassLoader urlClassLoader; public PublicURLClassLoader urlClassLoader;
public void run() throws IOException public void run() throws IOException
{ {
ServerWrapper wrapper = ServerWrapper.wrapper;
System.out.println("Print jar filename:"); System.out.println("Print jar filename:");
String jarName = commands.commandHandler.readLine(); String jarName = commands.commandHandler.readLine();
Path jarPath = Paths.get(jarName); Path jarPath = Paths.get(jarName);
@ -50,9 +49,9 @@ public void run() throws IOException
String address = commands.commandHandler.readLine(); String address = commands.commandHandler.readLine();
System.out.println("Print launchserver port:"); System.out.println("Print launchserver port:");
int port = Integer.valueOf(commands.commandHandler.readLine()); int port = Integer.valueOf(commands.commandHandler.readLine());
ServerWrapper.config.mainclass = mainClassName; wrapper.config.mainclass = mainClassName;
ServerWrapper.config.address = address; wrapper.config.address = address;
ServerWrapper.config.port = port; wrapper.config.port = port;
if(!Files.exists(ServerWrapper.publicKeyFile)) if(!Files.exists(ServerWrapper.publicKeyFile))
{ {
LogHelper.error("public.key not found"); LogHelper.error("public.key not found");
@ -65,7 +64,7 @@ public void run() throws IOException
else LogHelper.error("public.key not found"); else LogHelper.error("public.key not found");
} }
} }
boolean stopOnError = ServerWrapper.config.stopOnError; boolean stopOnError = wrapper.config.stopOnError;
for(int i=0;i<10;++i) for(int i=0;i<10;++i)
{ {
System.out.println("Print server account login:"); System.out.println("Print server account login:");
@ -74,18 +73,13 @@ public void run() throws IOException
String password = commands.commandHandler.readLine(); String password = commands.commandHandler.readLine();
System.out.println("Print profile title:"); System.out.println("Print profile title:");
String title = commands.commandHandler.readLine(); String title = commands.commandHandler.readLine();
ServerWrapper.config.login = login; wrapper.config.login = login;
ServerWrapper.config.password = password; wrapper.config.password = password;
ServerWrapper.config.title = title; wrapper.config.title = title;
ServerWrapper.config.stopOnError = false; wrapper.config.stopOnError = false;
LauncherConfig cfg = null; LauncherConfig cfg = null;
try {
cfg = new LauncherConfig(ServerWrapper.config.address, ServerWrapper.config.port, SecurityHelper.toPublicRSAKey(IOHelper.read(ServerWrapper.publicKeyFile)), new HashMap<>(), ServerWrapper.config.projectname); if(wrapper.auth())
} catch (InvalidKeySpecException e) {
LogHelper.error(e);
}
Launcher.setConfig(cfg);
if(ServerWrapper.auth(ServerWrapper.wrapper))
{ {
break; break;
} }
@ -94,8 +88,8 @@ public void run() throws IOException
LogHelper.error("Auth error. Recheck account params"); LogHelper.error("Auth error. Recheck account params");
} }
} }
ServerWrapper.config.stopOnError = stopOnError; wrapper.config.stopOnError = stopOnError;
ServerWrapper.config.save(); wrapper.saveConfig();
LogHelper.info("Generate start script"); LogHelper.info("Generate start script");
Path startScript; Path startScript;
if (JVMHelper.OS_TYPE == JVMHelper.OS.MUSTDIE) startScript = Paths.get("start.bat"); if (JVMHelper.OS_TYPE == JVMHelper.OS.MUSTDIE) startScript = Paths.get("start.bat");

View file

@ -35,6 +35,7 @@ public void saveConfig(Path configPath) throws IOException
} }
public void loadConfig(Path configPath) throws IOException public void loadConfig(Path configPath) throws IOException
{ {
if(generateConfigIfNotExists(configPath)) return;
try (BufferedReader reader = IOHelper.newReader(configPath)) { try (BufferedReader reader = IOHelper.newReader(configPath)) {
setConfig(Launcher.gson.fromJson(reader, type)); setConfig(Launcher.gson.fromJson(reader, type));
} }
@ -52,6 +53,22 @@ public void resetConfig(Path newPath) throws IOException
saveConfig(newPath); saveConfig(newPath);
} }
public boolean generateConfigIfNotExists(Path path) throws IOException
{
if(IOHelper.isFile(path))
return false;
resetConfig(path);
return true;
}
public boolean generateConfigIfNotExists() throws IOException
{
if(IOHelper.isFile(configPath))
return false;
resetConfig();
return true;
}
public abstract T getConfig(); public abstract T getConfig();
public abstract T getDefaultConfig(); public abstract T getDefaultConfig();
public abstract void setConfig(T config); public abstract void setConfig(T config);