Compare commits

...
3 changed files with 127 additions and 12 deletions

No files matched your search

@@ -141,11 +141,10 @@ public final class ConfigManager {
if (address == null) {
return this.mainConfig();
}
final String hostString = address.getHostString() + ":" + address.getPort();
final String configString = this.pluginSettings().proxySettings().findConfigStringForHost(hostString);
final String configString = this.pluginSettings().proxySettings().findConfigStringForHost(address.getHostString(), address.getPort());
if (this.pluginSettings().proxySettings().virtualHostTestMode()) {
this.miniMOTD.platform().logger().info("[virtual-host-debug] Virtual Host: '{}', Selected Config: '{}'", hostString, configString == null ? "default" : configString);
this.miniMOTD.platform().logger().info("[virtual-host-debug] Virtual Host: '{}:{}', Selected Config: '{}'", address.getHostString(), address.getPort(), configString == null ? "default" : configString);
}
if (configString == null) {
@@ -23,9 +23,10 @@
*/
package xyz.jpenilla.minimotd.common.config;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
@@ -50,11 +51,19 @@ public final class PluginSettings {
this.virtualHostConfigs.put("skyblock.example.com:25565", "skyblock");
}
public ProxySettings(final Map<String, String> virtualHostConfigs) {
this.virtualHostConfigs.putAll(virtualHostConfigs);
}
@Comment("Here you can assign configs in the 'extra-configs' folder to specific virtual hosts\n"
+ "Either use the name of the config in 'extra-configs', or use \"default\" to use the configuration in main.conf\n"
+ "\n"
+ "Format is \"hostname:port\"=\"configName|default\"")
private final Map<String, String> virtualHostConfigs = new HashMap<>();
+ "Format is \"hostname:port\"=\"configName|default\"\n"
+ "Parts of domains can be substituted for wildcards, i.e. \"*.mydomain.com:25565\". Wildcard-containing configs are\n"
+ "checked in the order they are declared if there are no exact matches.")
private final Map<String, String> virtualHostConfigs = new LinkedHashMap<>();
private transient @MonotonicNonNull Map<String[], String> splitVirtualHostConfigs;
@Comment("Set whether to enable virtual host testing mode.\n"
+ "When enabled, MiniMOTD will print virtual host debug info to the console on each server ping.")
@@ -64,8 +73,61 @@ public final class PluginSettings {
return this.virtualHostTestMode;
}
public @Nullable String findConfigStringForHost(final @NonNull String host) {
return this.virtualHostConfigs.get(host.toLowerCase(Locale.ENGLISH));
public @Nullable String findConfigStringForHost(@NonNull String host, final int port) {
host = processTcpShieldHostname(host).toLowerCase(Locale.ENGLISH) + ':' + port;
final @Nullable String exactMatch = this.virtualHostConfigs.get(host);
if (exactMatch != null) {
return exactMatch;
}
if (this.splitVirtualHostConfigs.isEmpty()) {
return null;
}
final String[] splitHost = host.split("\\.");
configs:
for (final Map.Entry<String[], String> e : this.splitVirtualHostConfigs.entrySet()) {
final String[] splitKey = e.getKey();
if (splitKey.length != splitHost.length) {
continue;
}
for (int i = 0; i < splitHost.length; i++) {
final String keyPart = splitKey[i];
if (!keyPart.equals(splitHost[i]) && !keyPart.equals("*")) {
continue configs;
}
}
return e.getValue();
}
return null;
}
private static String processTcpShieldHostname(final String hostname) {
if (hostname.contains("///")) {
final String[] split = hostname.split("///");
if (split.length == 4) {
// <actual hostname>///<user-ip>:<user-port>///<unix timestamp>///<signature>
return split[0];
}
}
return hostname;
}
public void processVirtualHosts() {
final Map<String, String> virtualHosts = new LinkedHashMap<>(this.virtualHostConfigs);
this.virtualHostConfigs.clear();
virtualHosts.forEach((host, config) -> this.virtualHostConfigs.put(host.toLowerCase(Locale.ENGLISH), config));
this.splitVirtualHostConfigs = new LinkedHashMap<>();
this.virtualHostConfigs.forEach((host, config) -> {
if (!host.contains("*")) {
return;
}
this.splitVirtualHostConfigs.put(host.split("\\."), config);
});
}
}
@@ -78,9 +140,7 @@ public final class PluginSettings {
}
@PostProcessor
private void lowercaseVirtualHosts() {
final Map<String, String> virtualHosts = new HashMap<>(this.proxySettings.virtualHostConfigs);
this.proxySettings.virtualHostConfigs.clear();
virtualHosts.forEach((host, config) -> this.proxySettings.virtualHostConfigs.put(host.toLowerCase(Locale.ENGLISH), config));
private void processVirtualHosts() {
this.proxySettings.processVirtualHosts();
}
}
@@ -0,0 +1,56 @@
/*
* This file is part of MiniMOTD, licensed under the MIT License.
*
* Copyright (c) 2020-2024 Jason Penilla
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package xyz.jpenilla.minimotd.common;
import java.util.LinkedHashMap;
import java.util.Map;
import org.junit.jupiter.api.Test;
import xyz.jpenilla.minimotd.common.config.PluginSettings;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
class VirtualHostMatchingTest {
@Test
void testMatching() {
final Map<String, String> map = new LinkedHashMap<>();
map.put("abc.xyz:25565", "1");
map.put("subdomain.abc.xyz:25565", "2");
map.put("*.abc.xyz:25565", "3");
map.put("abc.*.*.domain.com:25565", "4");
map.put("*.*.*.domain.com:25565", "5");
final PluginSettings.ProxySettings proxySettings = new PluginSettings.ProxySettings(map);
proxySettings.processVirtualHosts();
assertEquals("1", proxySettings.findConfigStringForHost("abc.xyz", 25565));
assertEquals("2", proxySettings.findConfigStringForHost("subdomain.abc.xyz", 25565));
assertEquals("3", proxySettings.findConfigStringForHost("other_subdomain.abc.xyz", 25565));
assertEquals("4", proxySettings.findConfigStringForHost("abc.2.3.domain.com", 25565));
assertEquals("5", proxySettings.findConfigStringForHost("1.2.3.domain.com", 25565));
// TCPShield-mangled hostname
assertEquals("5", proxySettings.findConfigStringForHost("1.2.3.domain.com///127.0.0.1:25565///1681016669///MGQCMAW5X13a9cF5ysnWdrBLovH4wfVu60l7eBUwWsCI8vw0cuZ9aj+UM7+y1wC//qGHngIwRsCke7eCpO99HTHOVdvZKjx9G0E51ALqRFGWH7kxMhVKYNF2IfMaT1FfbS0/D0du", 25565));
assertNull(proxySettings.findConfigStringForHost("subdomain.subdomain.abc.xyz", 25565));
assertNull(proxySettings.findConfigStringForHost("google.com", 25565));
}
}