Skip to content

Commit

Permalink
NanoLimboPlugin support (#74)
Browse files Browse the repository at this point in the history
* Add LimboPluginHook

* Deprecate LimboServerWrapper

* Soft depend on NanoLimboPlugin

* Add NanoLimboPluginHook

* Add VelocityNanoLimboPluginHook

* Add dependencies for NanoLimboPlugin in velocity module pom.xml

* Implement PluginHook in LimboPluginHook

* Replace NanoLimboPluginHook with NanoLimboProvider

* Add BungeeNanoLimboPluginHook

* Add NanoLimbo as dependency in bungee pom.xml

* Implement LimboPluginHook in LimboAPIHook

* Properly implement LimboAPIHook#canHook

* Remove redundant LimboHook

* Use LimboPluginHook in ServerCore implementations

* Create limbo-port in config.yml

* Hook into NanoLimboPlugin if possible

* Reuse IntStream in NanoLimboPluginHook

* Define proper ClassLoader for NanoLimboProvider

* Define limbo as initial server in velocity if required

* Remove unused import statement

* Define initial server on proxy joining

* Remove LimboAPI support

* Define initial server on proxy joining in velocity
  • Loading branch information
bivashy authored Sep 30, 2023
1 parent 1f08129 commit 67b9337
Show file tree
Hide file tree
Showing 29 changed files with 551 additions and 318 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.IntStream;

import com.bivashy.auth.api.config.bossbar.BossBarSettings;
import com.bivashy.auth.api.config.database.DatabaseSettings;
Expand Down Expand Up @@ -42,6 +43,8 @@ public interface PluginConfig {

List<Pattern> getAllowedCommands();

IntStream getLimboPortRange();

List<String> getAuthenticationSteps();

String getAuthenticationStepName(int index);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.bivashy.auth.api.hook;

import com.bivashy.auth.api.server.proxy.ProxyServer;

public interface LimboPluginHook extends PluginHook {

ProxyServer createServer(String serverName);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

import com.bivashy.auth.api.server.proxy.ProxyServer;

@Deprecated
public interface LimboServerWrapper extends ProxyServer {
}
9 changes: 9 additions & 0 deletions bungee/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<telegram.api.version>6.5.0</telegram.api.version>
<vk.api.version>0.4.2</vk.api.version>
<libby.version>2.0.0</libby.version>
<nanolimbo.version>1.0.6</nanolimbo.version>
</properties>

<repositories>
Expand Down Expand Up @@ -87,6 +88,14 @@
<scope>provided</scope>
</dependency>

<!-- NanoLimboPlugin API -->
<dependency>
<groupId>com.github.bivashy.NanoLimboPlugin</groupId>
<artifactId>api</artifactId>
<version>${nanolimbo.version}</version>
<scope>provided</scope>
</dependency>

<!-- Libby -->
<dependency>
<groupId>com.github.bivashy.libby</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package me.mastercapexd.auth.bungee;

import java.util.Collection;
import java.util.Collections;

import com.alessiodp.libby.BungeeLibraryManager;
import com.bivashy.auth.api.AuthPlugin;
import com.bivashy.auth.api.management.LoginManagement;
import com.bivashy.auth.api.hook.LimboPluginHook;
import com.bivashy.messenger.vk.message.VkMessage;
import com.bivashy.messenger.vk.provider.VkApiProvider;
import com.ubivashka.vk.bungee.BungeeVkApiPlugin;

import me.mastercapexd.auth.BaseAuthPlugin;
import me.mastercapexd.auth.bungee.commands.BungeeCommandsRegistry;
import me.mastercapexd.auth.bungee.hooks.BungeeVkPluginHook;
import me.mastercapexd.auth.bungee.hooks.nanolimbo.BungeeNanoLimboPluginHook;
import me.mastercapexd.auth.bungee.listener.AuthenticationListener;
import me.mastercapexd.auth.bungee.listener.VkDispatchListener;
import me.mastercapexd.auth.hooks.VkPluginHook;
Expand All @@ -22,7 +25,6 @@ public class BungeeAuthPluginBootstrap extends Plugin {

private static BungeeAuthPluginBootstrap instance;
private BungeeAudiences bungeeAudiences;
private LoginManagement loginManagement;
private BaseAuthPlugin authPlugin;

public static BungeeAuthPluginBootstrap getInstance() {
Expand All @@ -39,6 +41,7 @@ public void onEnable() {
new BaseLibraryManagement(new BungeeLibraryManager(this)));
initializeListener();
initializeCommand();
initializeLimbo();
if (authPlugin.getConfig().getVKSettings().isEnabled())
initializeVk();
}
Expand All @@ -57,6 +60,13 @@ private void initializeCommand() {
new BungeeCommandsRegistry(this, authPlugin);
}

private void initializeLimbo() {
Collection<LimboPluginHook> limboPluginHooks = Collections.singleton(new BungeeNanoLimboPluginHook(authPlugin.getConfig().getLimboPortRange()));
limboPluginHooks.stream()
.filter(LimboPluginHook::canHook)
.forEach(limboPluginHook -> authPlugin.putHook(LimboPluginHook.class, limboPluginHook));
}

private void initializeVk() {
authPlugin.putHook(VkPluginHook.class, new BungeeVkPluginHook());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
import java.util.stream.Collectors;

import com.bivashy.auth.api.AuthPlugin;
import com.bivashy.auth.api.hook.LimboPluginHook;
import com.bivashy.auth.api.server.ServerCore;
import com.bivashy.auth.api.server.bossbar.ServerBossbar;
import com.bivashy.auth.api.server.message.ServerComponent;
import com.bivashy.auth.api.server.player.ServerPlayer;
import com.bivashy.auth.api.server.proxy.limbo.LimboServerWrapper;
import com.bivashy.auth.api.server.scheduler.ServerScheduler;
import com.bivashy.auth.api.server.title.ServerTitle;

Expand All @@ -25,7 +25,6 @@
import me.mastercapexd.auth.bungee.player.BungeeServerPlayer;
import me.mastercapexd.auth.bungee.scheduler.BungeeSchedulerWrapper;
import me.mastercapexd.auth.bungee.server.BungeeServer;
import me.mastercapexd.auth.hooks.limbo.LimboHook;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.config.ServerInfo;
Expand Down Expand Up @@ -109,15 +108,9 @@ public ServerComponent componentLegacy(String legacy) {
@Override
public Optional<com.bivashy.auth.api.server.proxy.ProxyServer> serverFromName(String serverName) {
ServerInfo serverInfo = PROXY_SERVER.getServerInfo(serverName);
LimboHook limboHook = AuthPlugin.instance().getHook(LimboHook.class);
if (serverInfo == null && limboHook != null) {
if (!limboHook.isLimbo(serverName))
return Optional.empty();
LimboServerWrapper server = limboHook.createLimboWrapper(serverName);
if (!server.isExists())
return Optional.empty();
return Optional.of(server);
}
LimboPluginHook limboHook = AuthPlugin.instance().getHook(LimboPluginHook.class);
if (serverInfo == null && limboHook != null)
return Optional.of(limboHook.createServer(serverName));
return Optional.of(new BungeeServer(serverInfo));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package me.mastercapexd.auth.bungee.hooks.nanolimbo;

import java.net.SocketAddress;
import java.util.stream.IntStream;

import com.bivashy.auth.api.AuthPlugin;
import com.bivashy.auth.api.config.PluginConfig;
import com.bivashy.auth.api.hook.LimboPluginHook;

import me.mastercapexd.auth.bungee.BungeeAuthPluginBootstrap;
import me.mastercapexd.auth.bungee.server.BungeeServer;
import me.mastercapexd.auth.hooks.nanolimbo.NanoLimboProvider;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.event.ServerConnectEvent;
import net.md_5.bungee.api.event.ServerConnectEvent.Reason;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.event.EventHandler;
import net.md_5.bungee.event.EventPriority;

public class BungeeNanoLimboPluginHook implements LimboPluginHook, Listener {

private final int[] limboPorts;
private NanoLimboProvider provider;

public BungeeNanoLimboPluginHook(IntStream limboPortRange) {
this.limboPorts = limboPortRange.toArray();
if (!canHook())
return;
this.provider = new BungeeNanoLimboProvider(ProxyServer.getInstance().getPluginManager().getPlugin("NanoLimboBungee").getClass().getClassLoader());
ProxyServer.getInstance().getPluginManager().registerListener(BungeeAuthPluginBootstrap.getInstance(), this);
}

@Override
public com.bivashy.auth.api.server.proxy.ProxyServer createServer(String serverName) {
SocketAddress address = provider.findAvailableAddress(limboPorts).orElseThrow(
() -> new IllegalStateException("Cannot find available port for limbo server!"));
provider.createAndStartLimbo(address);
ServerInfo serverInfo = ProxyServer.getInstance().constructServerInfo(serverName, address, "", false);
ProxyServer.getInstance().getConfig().getServers().put(serverInfo.getName(), serverInfo);
return new BungeeServer(serverInfo);
}

@EventHandler(priority = EventPriority.HIGHEST)
public void onServerChoose(ServerConnectEvent event) {
// TODO: Implement ServerConnectEvent in the InitialServerManagement
if (event.getReason() != Reason.JOIN_PROXY)
return;
PluginConfig config = AuthPlugin.instance().getConfig();
event.setTarget(config.findServerInfo(config.getAuthServers()).asProxyServer().as(BungeeServer.class).getServerInfo());
}

@Override
public boolean canHook() {
return ProxyServer.getInstance().getPluginManager().getPlugin("NanoLimboBungee") != null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package me.mastercapexd.auth.bungee.hooks.nanolimbo;

import me.mastercapexd.auth.hooks.nanolimbo.NanoLimboProvider;
import ua.nanit.limbo.server.data.InfoForwarding;

public class BungeeNanoLimboProvider implements NanoLimboProvider {

private final ClassLoader classLoader;

public BungeeNanoLimboProvider(ClassLoader classLoader) {
this.classLoader = classLoader;
}

@Override
public InfoForwarding createForwarding() {
return FORWARDING_FACTORY.legacy();
}

@Override
public ClassLoader classLoader() {
return classLoader;
}

}
5 changes: 4 additions & 1 deletion bungee/src/main/resources/bungee.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
name: mcAuth
main: me.mastercapexd.auth.bungee.BungeeAuthPluginBootstrap
version: ${project.version}
softDepends: [VK-API,JavaTelegramBotApi]
softDepends:
- VK-API
- JavaTelegramBotApi
- NanoLimboBungee
author: bivashy, MasterCapeXD
9 changes: 9 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<password4j.version>1.7.0</password4j.version>
<yaml-config-updater.version>1.4.2</yaml-config-updater.version>
<jda.version>5.0.0-beta.11</jda.version>
<nanolimbo.version>1.0.6</nanolimbo.version>
<maven-resolver.version>1.9.15</maven-resolver.version>
<maven-resolver-provider.version>3.9.4</maven-resolver-provider.version>
</properties>
Expand Down Expand Up @@ -162,5 +163,13 @@
<version>${jda.version}</version>
<scope>provided</scope>
</dependency>

<!-- NanoLimboPlugin API -->
<dependency>
<groupId>com.github.bivashy.NanoLimboPlugin</groupId>
<artifactId>api</artifactId>
<version>${nanolimbo.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
11 changes: 11 additions & 0 deletions core/src/main/java/me/mastercapexd/auth/BaseAuthPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.stream.IntStream;

import com.bivashy.auth.api.AuthPlugin;
import com.bivashy.auth.api.AuthPluginProvider;
Expand Down Expand Up @@ -240,6 +241,16 @@ private void registerConfigurationProcessor() {
if (path.isEmpty())
return null;
return new File(path.replace("%plugin_folder%", getFolder().getAbsolutePath()));
})
.registerFieldResolver(IntStream.class, (context) -> {
String number = context.getString("");
if (number.isEmpty())
return IntStream.of(0);
if (number.contains("-")) {
String[] range = number.split("-");
return IntStream.range(Integer.parseInt(range[0]), Integer.parseInt(range[1]));
}
return IntStream.of(Integer.parseInt(number));
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import com.bivashy.auth.api.AuthPlugin;
import com.bivashy.auth.api.config.PluginConfig;
Expand Down Expand Up @@ -37,6 +38,7 @@
import me.mastercapexd.auth.config.vk.BaseVKSettings;

public abstract class PluginConfigTemplate implements PluginConfig {

protected final AuthPlugin plugin;
private final List<Pattern> allowedPatternCommands;
protected ConfigurationSectionHolder configurationRoot;
Expand Down Expand Up @@ -101,6 +103,8 @@ public abstract class PluginConfigTemplate implements PluginConfig {
private ConfigurationDuration joinDelay = new ConfigurationDuration(0);
@ConfigField("block-chat")
private boolean blockChat = true;
@ConfigField("limbo-port")
private IntStream limboPortRange = IntStream.range(49152, 65535);
@ConfigField("authentication-steps")
private List<String> authenticationSteps = Arrays.asList("REGISTER", "LOGIN", "VK_LINK", "TELEGRAM_LINK", "GOOGLE_LINK", "ENTER_SERVER");

Expand Down Expand Up @@ -287,5 +291,11 @@ public String getAuthenticationStepName(int index) {
return index >= 0 && index < authenticationSteps.size() ? authenticationSteps.get(index) : "NULL";
}

@Override
public IntStream getLimboPortRange() {
return limboPortRange;
}

protected abstract ConfigurationSectionHolder createConfiguration(AuthPlugin plugin);

}
10 changes: 0 additions & 10 deletions core/src/main/java/me/mastercapexd/auth/hooks/limbo/LimboHook.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package me.mastercapexd.auth.hooks.nanolimbo;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import ua.nanit.limbo.server.data.InfoForwarding;
import ua.nanit.limbo.server.data.InfoForwarding.Type;

// Class for hiding InfoForwarding dirty way initialization
public class InfoForwardingFactory {

public InfoForwarding none() {
Map<String, Object> map = Collections.singletonMap("type", Type.NONE);
return createForwarding(map);
}

public InfoForwarding legacy() {
Map<String, Object> map = Collections.singletonMap("type", Type.LEGACY);
return createForwarding(map);
}

public InfoForwarding modern(byte[] secretKey) {
Map<String, Object> map = new HashMap<>();
map.put("type", Type.MODERN);
map.put("secretKey", secretKey);
return createForwarding(map);
}

public InfoForwarding bungeeGuard(Collection<String> tokens) {
Map<String, Object> map = new HashMap<>();
map.put("type", Type.BUNGEE_GUARD);
map.put("tokens", new ArrayList<>(tokens));
return createForwarding(map);
}

private InfoForwarding createForwarding(Map<String, Object> map) {
InfoForwarding forwarding = new InfoForwarding();
for (Entry<String, Object> entry : map.entrySet()) {
Class<?> clazz = forwarding.getClass();
try {
Field field = clazz.getDeclaredField(entry.getKey());
field.setAccessible(true);
field.set(forwarding, entry.getValue());
} catch (NoSuchFieldException | IllegalAccessException ignored) {
}
}
return forwarding;
}

}
Loading

0 comments on commit 67b9337

Please sign in to comment.