[FEATURE] WhitelistComponent

This commit is contained in:
Gravita 2021-06-22 12:09:34 +07:00
parent b4718a8062
commit 3f0c972b08
4 changed files with 103 additions and 1 deletions

View file

@ -63,6 +63,7 @@ public void invoke(String... args) throws Exception {
if (component instanceof AutoCloseable) {
((AutoCloseable) component).close();
}
server.unregisterObject("component." + componentName, component);
server.config.components.remove(componentName);
logger.info("Component %s unloaded. Use 'config launchserver save' to save changes");
}
@ -94,6 +95,7 @@ public void invoke(String... args) throws Exception {
component.setComponentName(componentName);
server.config.components.put(componentName, component);
component.init(server);
server.registerObject("component." + componentName, component);
logger.info("Component %s ready. Use 'config launchserver save' to save changes");
} catch (Throwable throwable) {
logger.error(throwable);

View file

@ -14,6 +14,7 @@ public static void registerComponents() {
providers.register("regLimiter", RegLimiterComponent.class);
providers.register("commandRemover", CommandRemoverComponent.class);
providers.register("proguard", ProGuardComponent.class);
providers.register("whitelist", WhitelistComponent.class);
registredComp = true;
}
}

View file

@ -0,0 +1,99 @@
package pro.gravit.launchserver.components;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import pro.gravit.launchserver.LaunchServer;
import pro.gravit.launchserver.Reconfigurable;
import pro.gravit.launchserver.socket.Client;
import pro.gravit.launchserver.socket.response.auth.AuthResponse;
import pro.gravit.launchserver.socket.response.auth.JoinServerResponse;
import pro.gravit.utils.HookException;
import pro.gravit.utils.command.Command;
import pro.gravit.utils.command.SubCommand;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class WhitelistComponent extends Component implements AutoCloseable, Reconfigurable {
private transient LaunchServer server;
private transient final Logger logger = LogManager.getLogger();
public String message = "auth.message.techwork";
public boolean enabled = true;
public List<String> whitelist = new ArrayList<>();
@Override
public void init(LaunchServer launchServer) {
this.server = launchServer;
this.server.authHookManager.preHook.registerHook(this::hookAuth);
this.server.authHookManager.joinServerHook.registerHook(this::hookJoin);
}
public boolean hookAuth(AuthResponse.AuthContext context, Client client) throws HookException {
if (enabled) {
if (!whitelist.contains(context.login)) {
throw new HookException(message);
}
}
return false;
}
public boolean hookJoin(JoinServerResponse response, Client client) throws HookException {
if (enabled) {
if (!whitelist.contains(response.username)) {
throw new HookException(message);
}
}
return false;
}
@Override
public void close() throws Exception {
this.server.authHookManager.preHook.unregisterHook(this::hookAuth);
this.server.authHookManager.joinServerHook.unregisterHook(this::hookJoin);
}
@Override
public Map<String, Command> getCommands() {
var commands = defaultCommandsMap();
commands.put("setmessage", new SubCommand("[new message]", "set message") {
@Override
public void invoke(String... args) throws Exception {
verifyArgs(args, 1);
message = args[0];
logger.info("Message: {}", args[0]);
}
});
commands.put("whitelist.add", new SubCommand("[login]", "add login to whitelist") {
@Override
public void invoke(String... args) throws Exception {
verifyArgs(args, 1);
whitelist.add(args[0]);
logger.info("{} added to whitelist", args[0]);
}
});
commands.put("whitelist.remove", new SubCommand("[login]", "remove login from whitelist") {
@Override
public void invoke(String... args) throws Exception {
verifyArgs(args, 1);
whitelist.remove(args[0]);
logger.info("{} removed from whitelist", args[0]);
}
});
commands.put("disable", new SubCommand() {
@Override
public void invoke(String... args) throws Exception {
enabled = false;
logger.info("Whitelist disabled");
}
});
commands.put("enable", new SubCommand() {
@Override
public void invoke(String... args) throws Exception {
enabled = true;
logger.info("Whitelist enabled");
}
});
return commands;
}
}

View file

@ -208,7 +208,7 @@ public void init(LaunchServer.ReloadType type) {
server.registerObject("auth.".concat(pair.name).concat(".provider"), pair.provider);
server.registerObject("auth.".concat(pair.name).concat(".handler"), pair.handler);
server.registerObject("auth.".concat(pair.name).concat(".core"), pair.core);
server.registerObject("auth.".concat(pair.name).concat(".social"), pair.core);
server.registerObject("auth.".concat(pair.name).concat(".social"), pair.social);
server.registerObject("auth.".concat(pair.name).concat(".texture"), pair.textureProvider);
}
}