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

View file

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