Merge branch 'master' into optimizations

This commit is contained in:
Zaxar163 2018-12-26 16:20:27 +03:00 committed by GitHub
commit 94c1cecd14
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 270 additions and 37 deletions

View file

@ -287,6 +287,8 @@ public static void main(String... args) throws Throwable {
public final ReloadManager reloadManager;
public final ReconfigurableManager reconfigurableManager;
public final BuildHookManager buildHookManager;
@ -378,6 +380,11 @@ public LaunchServer(Path dir) throws IOException, InvalidKeySpecException {
config = Launcher.gson.fromJson(reader,Config.class);
}
config.verify();
for(AuthProvider provider : config.authProvider)
{
provider.init();
}
config.authHandler.init();
// build hooks, anti-brutforce and other
buildHookManager = new BuildHookManager();
@ -386,6 +393,8 @@ public LaunchServer(Path dir) throws IOException, InvalidKeySpecException {
sessionManager = new SessionManager();
mirrorManager = new MirrorManager();
reloadManager = new ReloadManager();
reconfigurableManager = new ReconfigurableManager();
GarbageManager.registerNeedGC(sessionManager);
GarbageManager.registerNeedGC(limiter);
if(config.permissionsHandler instanceof Reloadable)
@ -409,6 +418,27 @@ public LaunchServer(Path dir) throws IOException, InvalidKeySpecException {
}
});
if(config.permissionsHandler instanceof Reconfigurable)
reconfigurableManager.registerReconfigurable("permissionsHandler",(Reconfigurable) config.permissionsHandler);
if(config.authHandler instanceof Reconfigurable)
reconfigurableManager.registerReconfigurable("authHandler",(Reconfigurable) config.authHandler);
for(int i=0;i<config.authProvider.length;++i)
{
AuthProvider provider = config.authProvider[i];
if(provider instanceof Reconfigurable)
reconfigurableManager.registerReconfigurable("authHandler".concat(String.valueOf(i)),(Reconfigurable) provider);
}
if(config.textureProvider instanceof Reconfigurable)
reconfigurableManager.registerReconfigurable("textureProvider",(Reconfigurable) config.textureProvider);
Arrays.stream(config.mirrors).forEach(s -> {
try {
mirrorManager.addMirror(s);
} catch (MalformedURLException e) {
e.printStackTrace();
}
});
// init modules
modulesManager.initModules();

View file

@ -0,0 +1,6 @@
package ru.gravit.launchserver;
public interface Reconfigurable {
void reconfig(String action,String[] args);
void printConfigHelp();
}

View file

@ -65,4 +65,9 @@ public static void registerHandlers() {
public abstract String uuidToUsername(UUID uuid) throws IOException;
public void init()
{
}
}

View file

@ -22,7 +22,8 @@ public final class MySQLAuthHandler extends CachedAuthHandler {
private transient String queryByUsernameSQL;
private transient String updateAuthSQL;
private transient String updateServerIDSQL;
public MySQLAuthHandler()
@Override
public void init()
{
// Prepare SQL queries
queryByUUIDSQL = String.format("SELECT %s, %s, %s, %s FROM %s WHERE %s=? LIMIT 1",

View file

@ -63,4 +63,8 @@ public static String getProviderName(Class<? extends AuthProvider> clazz)
}
return null;
}
public void init()
{
}
}

View file

@ -1,7 +1,7 @@
package ru.gravit.launchserver.auth.provider;
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.auth.ClientPermissions;
import ru.gravit.launchserver.manangers.PermissionsManager;
public class AuthProviderResult {
@ -12,7 +12,7 @@ public class AuthProviderResult {
public AuthProviderResult(String username, String accessToken) {
this.username = username;
this.accessToken = accessToken;
permissions = PermissionsManager.getPermissions(username);
permissions = LaunchServer.server.config.permissionsHandler.getPermissions(username);
}
public AuthProviderResult(String username, String accessToken, ClientPermissions permissions) {

View file

@ -1,9 +1,13 @@
package ru.gravit.launchserver.auth.provider;
import ru.gravit.launchserver.Reconfigurable;
import ru.gravit.launchserver.auth.AuthException;
import ru.gravit.utils.helper.LogHelper;
import ru.gravit.utils.helper.SecurityHelper;
public final class RejectAuthProvider extends AuthProvider {
import java.util.ArrayList;
public final class RejectAuthProvider extends AuthProvider implements Reconfigurable {
public RejectAuthProvider() {
}
@ -12,7 +16,7 @@ public RejectAuthProvider(String message) {
}
private String message;
private String[] whitelist;
private ArrayList<String> whitelist;
@Override
public AuthProviderResult auth(String login, String password, String ip) throws AuthException {
@ -33,4 +37,35 @@ public AuthProviderResult auth(String login, String password, String ip) throws
public void close() {
// Do nothing
}
@Override
public void reconfig(String action, String[] args) {
if(action.equals("message"))
{
message = args[0];
LogHelper.info("New reject message: %s", message);
}
else if(action.equals("whitelist.add"))
{
if(whitelist == null) whitelist = new ArrayList<>();
whitelist.add(args[0]);
}
else if(action.equals("whitelist.remove"))
{
if(whitelist == null) whitelist = new ArrayList<>();
whitelist.remove(args[0]);
}
else if(action.equals("whitelist.clear"))
{
whitelist.clear();
}
}
@Override
public void printConfigHelp() {
LogHelper.info("message [new message] - set message");
LogHelper.info("whitelist.add [username] - add username to whitelist");
LogHelper.info("whitelist.remove [username] - remove username into whitelist");
LogHelper.info("whitelist.clear - clear whitelist");
}
}

View file

@ -43,9 +43,7 @@
import ru.gravit.launchserver.command.hash.UnindexAssetCommand;
import ru.gravit.launchserver.command.modules.LoadModuleCommand;
import ru.gravit.launchserver.command.modules.ModulesCommand;
import ru.gravit.launchserver.command.service.ReloadAllCommand;
import ru.gravit.launchserver.command.service.ReloadCommand;
import ru.gravit.launchserver.command.service.ReloadInfoCommand;
import ru.gravit.launchserver.command.service.*;
import ru.gravit.utils.helper.LogHelper;
import ru.gravit.utils.helper.VerifyHelper;
@ -142,7 +140,11 @@ protected CommandHandler(LaunchServer server) {
//Register service commands
registerCommand("reload",new ReloadCommand(server));
registerCommand("reloadAll",new ReloadAllCommand(server));
registerCommand("reloadInfo",new ReloadInfoCommand(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));
}

View file

@ -0,0 +1,30 @@
package ru.gravit.launchserver.command.service;
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.command.Command;
import ru.gravit.utils.helper.LogHelper;
public class ConfigCommand extends Command {
public ConfigCommand(LaunchServer server) {
super(server);
}
@Override
public String getArgsDescription() {
return "[name] [action] [more args]";
}
@Override
public String getUsageDescription() {
return "call reconfigurable action";
}
@Override
public void invoke(String... args) throws Exception {
verifyArgs(args,2);
LogHelper.info("Call %s module % action",args[0],args[1]);
String[] new_args = new String[args.length - 2];
System.arraycopy(args, 2, new_args, 0, args.length - 2);
server.reconfigurableManager.call(args[0],args[1],new_args);
}
}

View file

@ -0,0 +1,28 @@
package ru.gravit.launchserver.command.service;
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.command.Command;
import ru.gravit.utils.helper.LogHelper;
public class ConfigHelpCommand extends Command {
public ConfigHelpCommand(LaunchServer server) {
super(server);
}
@Override
public String getArgsDescription() {
return "[name]";
}
@Override
public String getUsageDescription() {
return "print help for config command";
}
@Override
public void invoke(String... args) throws Exception {
verifyArgs(args,1);
LogHelper.info("Help %s module",args[0]);
server.reconfigurableManager.printHelp(args[0]);
}
}

View file

@ -0,0 +1,25 @@
package ru.gravit.launchserver.command.service;
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.command.Command;
public class ConfigListCommand extends Command {
public ConfigListCommand(LaunchServer server) {
super(server);
}
@Override
public String getArgsDescription() {
return "[name]";
}
@Override
public String getUsageDescription() {
return "print help for config command";
}
@Override
public void invoke(String... args) throws Exception {
server.reconfigurableManager.printReconfigurables();
}
}

View file

@ -4,8 +4,8 @@
import ru.gravit.launchserver.command.Command;
import ru.gravit.utils.helper.LogHelper;
public class ReloadInfoCommand extends Command {
public ReloadInfoCommand(LaunchServer server) {
public class ReloadListCommand extends Command {
public ReloadListCommand(LaunchServer server) {
super(server);
}
@ -21,8 +21,6 @@ public String getUsageDescription() {
@Override
public void invoke(String... args) throws Exception {
verifyArgs(args,1);
LogHelper.info("Reload %s config",args[0]);
server.reloadManager.printReloadables();
}
}

View file

@ -0,0 +1,61 @@
package ru.gravit.launchserver.command.service;
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.auth.provider.AcceptAuthProvider;
import ru.gravit.launchserver.auth.provider.AuthProvider;
import ru.gravit.launchserver.auth.provider.RejectAuthProvider;
import ru.gravit.launchserver.command.Command;
import ru.gravit.utils.helper.LogHelper;
public class SwapAuthProviderCommand extends Command {
public AuthProvider[] providersCache;
public SwapAuthProviderCommand(LaunchServer server) {
super(server);
}
@Override
public String getArgsDescription() {
return "[index] [accept/reject/undo] [message(for reject)]";
}
@Override
public String getUsageDescription() {
return "Change authProvider";
}
@Override
public void invoke(String... args) throws Exception {
verifyArgs(args,2);
if(providersCache == null) providersCache = new AuthProvider[server.config.authProvider.length];
int index = Integer.valueOf(args[0]);
if(args[1].equals("accept"))
{
if(providersCache[index] == null)
{
AcceptAuthProvider provider = new AcceptAuthProvider();
providersCache[index] = server.config.authProvider[index];
server.config.authProvider[index] = provider;
LogHelper.info("AuthProvider[%d] is AcceptAuthProvider",index);
}
else LogHelper.error("Changes detected. Use undo");
} else if(args[1].equals("reject"))
{
if(providersCache[index] == null)
{
RejectAuthProvider rejectAuthProvider;
if(args.length < 3) rejectAuthProvider = new RejectAuthProvider();
else rejectAuthProvider = new RejectAuthProvider(args[2]);
providersCache[index] = server.config.authProvider[index];
server.config.authProvider[index] = rejectAuthProvider;
LogHelper.info("AuthProvider[%d] is RejectAuthProvider",index);
}
else LogHelper.error("Changes detected. Use undo");
} else if(args[1].equals("undo"))
{
if(providersCache[index] == null) LogHelper.error("Cache clean. Undo impossible");
server.config.authProvider[index].close();
server.config.authProvider[index] = providersCache[index];
providersCache[index] = null;
}
}
}

View file

@ -1,24 +0,0 @@
package ru.gravit.launchserver.manangers;
import ru.gravit.launchserver.auth.ClientPermissions;
public class PermissionsManager {
private static PermissionsFunction function = PermissionsManager::returnDefault;
public static void registerPermissionsFunction(PermissionsFunction function)
{
PermissionsManager.function = function;
}
public static ClientPermissions getPermissions(String username)
{
return function.getPermission(username);
}
@FunctionalInterface
public interface PermissionsFunction
{
ClientPermissions getPermission(String username);
}
public static ClientPermissions returnDefault(String username)
{
return ClientPermissions.DEFAULT;
}
}

View file

@ -0,0 +1,32 @@
package ru.gravit.launchserver.manangers;
import ru.gravit.launchserver.Reconfigurable;
import ru.gravit.launchserver.Reloadable;
import ru.gravit.utils.helper.LogHelper;
import ru.gravit.utils.helper.VerifyHelper;
import java.util.HashMap;
import java.util.Objects;
public class ReconfigurableManager {
private final HashMap<String, Reconfigurable> RECONFIGURABLE = new HashMap<>();
public void registerReconfigurable(String name, Reconfigurable reconfigurable)
{
VerifyHelper.verifyIDName(name);
VerifyHelper.putIfAbsent(RECONFIGURABLE, name, Objects.requireNonNull(reconfigurable, "adapter"),
String.format("Reloadable has been already registered: '%s'", name));
}
public void printHelp(String name)
{
RECONFIGURABLE.get(name).printConfigHelp();
}
public void call(String name, String action, String[] args) throws Exception {
RECONFIGURABLE.get(name).reconfig(action,args);
}
public void printReconfigurables()
{
LogHelper.info("Print reconfigurables");
RECONFIGURABLE.forEach((k, v) -> LogHelper.subInfo(k));
LogHelper.info("Found %d reconfigurables", RECONFIGURABLE.size());
}
}