From 6a52980f97734c6570749841cb08ca3c8f4469a4 Mon Sep 17 00:00:00 2001 From: carm Date: Fri, 5 Jan 2024 01:02:18 +0800 Subject: [PATCH] chore: Refactor project structure --- .../bungeeauthproxy/BungeeAuthProxy.java | 13 +++---- .../injector/bungeeauthproxy/Config.java | 23 ++++++++++++ .../injector/bungeeauthproxy/Logging.java | 8 +++-- .../ProxiedHttpInitializer.java | 2 +- .../ProxyProtocolType.java | 2 +- .../bungeeauthproxy/handler/AuthHandler.java | 10 ++++++ .../handler/ProxiedAuthHandler.java | 36 +++++++------------ ...former.java => HttpClientTransformer.java} | 18 +++++----- 8 files changed, 70 insertions(+), 42 deletions(-) rename src/main/java/com/artformgames/injector/bungeeauthproxy/{handler => channel}/ProxiedHttpInitializer.java (96%) rename src/main/java/com/artformgames/injector/bungeeauthproxy/{handler => channel}/ProxyProtocolType.java (96%) create mode 100644 src/main/java/com/artformgames/injector/bungeeauthproxy/handler/AuthHandler.java rename src/main/java/com/artformgames/injector/bungeeauthproxy/transformer/{ProxyHandlerTransformer.java => HttpClientTransformer.java} (75%) diff --git a/src/main/java/com/artformgames/injector/bungeeauthproxy/BungeeAuthProxy.java b/src/main/java/com/artformgames/injector/bungeeauthproxy/BungeeAuthProxy.java index 675be54..32a111d 100644 --- a/src/main/java/com/artformgames/injector/bungeeauthproxy/BungeeAuthProxy.java +++ b/src/main/java/com/artformgames/injector/bungeeauthproxy/BungeeAuthProxy.java @@ -1,8 +1,9 @@ package com.artformgames.injector.bungeeauthproxy; import cc.carm.lib.configuration.EasyConfiguration; +import com.artformgames.injector.bungeeauthproxy.handler.AuthHandler; import com.artformgames.injector.bungeeauthproxy.handler.ProxiedAuthHandler; -import com.artformgames.injector.bungeeauthproxy.transformer.ProxyHandlerTransformer; +import com.artformgames.injector.bungeeauthproxy.transformer.HttpClientTransformer; import io.netty.channel.EventLoop; import net.md_5.bungee.api.Callback; import net.md_5.bungee.http.HttpClient; @@ -17,7 +18,7 @@ public class BungeeAuthProxy { private BungeeAuthProxy() { } - private static ProxiedAuthHandler handler; + private static AuthHandler handler; public static void premain(String args, Instrumentation instrumentation) { log(Logging.Level.INFO, "Loading auth configurations..."); @@ -25,10 +26,10 @@ public static void premain(String args, Instrumentation instrumentation) { EasyConfiguration.from(configFileName).initialize(Config.class); log(Logging.Level.INFO, "Initializing auth handler..."); - handler = new ProxiedAuthHandler(); + setHandler(new ProxiedAuthHandler()); // Use ProxiedAuthHandler by default log(Logging.Level.INFO, "Registering transformer..."); - instrumentation.addTransformer(new ProxyHandlerTransformer(), true); + instrumentation.addTransformer(new HttpClientTransformer(), true); log(Logging.Level.INFO, "Preload target class..."); debug(" -> Target class: " + HttpClient.class.getName()); @@ -41,11 +42,11 @@ public static void submitRequest(String url, EventLoop loop, Callback ca handler.submit(url, loop, callback); } - public static ProxiedAuthHandler getHandler() { + public static AuthHandler getHandler() { return handler; } - public static void setHandler(ProxiedAuthHandler handler) { + public static void setHandler(AuthHandler handler) { BungeeAuthProxy.handler = handler; } diff --git a/src/main/java/com/artformgames/injector/bungeeauthproxy/Config.java b/src/main/java/com/artformgames/injector/bungeeauthproxy/Config.java index 9d41b0b..2fd22c2 100644 --- a/src/main/java/com/artformgames/injector/bungeeauthproxy/Config.java +++ b/src/main/java/com/artformgames/injector/bungeeauthproxy/Config.java @@ -3,6 +3,7 @@ import cc.carm.lib.configuration.core.Configuration; import cc.carm.lib.configuration.core.annotation.HeaderComment; import cc.carm.lib.configuration.core.value.type.ConfiguredValue; +import com.artformgames.injector.bungeeauthproxy.channel.ProxyProtocolType; @HeaderComment({ "BungeeAuthProxy injector configurations", @@ -10,6 +11,18 @@ }) public interface Config extends Configuration { + static ProxyProtocolType getProxyProtocol() { + return ProxyProtocolType.parse(PROXY.PROTOCOL.getNotNull()); + } + + static int getTimeoutDuration() { + return Math.max(SERVICE.TIME_OUT.getNotNull().intValue(), 100); + } + + static long getDNSCacheDuration() { + return Math.max(SERVICE.DNS_CACHE_EXPIRE.getNotNull(), -1L); + } + @HeaderComment("Debug mode for developers, with more detailed logs.") ConfiguredValue DEBUG = ConfiguredValue.of(false); @@ -55,5 +68,15 @@ interface AUTH extends Configuration { } + interface ADVANCE extends Configuration { + + @HeaderComment({ + "Remove unused field after injection.", + "If any 'NoSuchFieldException' or 'IllegalAccessException' occurred, try to set this to false." + }) + ConfiguredValue REMOVE_UNUSED_FILED = ConfiguredValue.of(true); + + } + } diff --git a/src/main/java/com/artformgames/injector/bungeeauthproxy/Logging.java b/src/main/java/com/artformgames/injector/bungeeauthproxy/Logging.java index 48e6fa5..f03edf7 100644 --- a/src/main/java/com/artformgames/injector/bungeeauthproxy/Logging.java +++ b/src/main/java/com/artformgames/injector/bungeeauthproxy/Logging.java @@ -21,11 +21,13 @@ import java.util.regex.Pattern; public final class Logging { - private Logging() { - } + private static final String PREFIX = "BungeeAuthProxy"; private static final Pattern CONTROL_CHARACTERS_FILTER = Pattern.compile("\\p{Cc}&&[^\r\n\t]"); + private Logging() { + } + public enum Level { DEBUG, INFO, WARNING, ERROR } @@ -53,7 +55,7 @@ public static void warning(String message) { public static void log(Level level, String message, Throwable e) { if (level == Level.DEBUG && !Config.DEBUG.getNotNull()) return; - String log = "[BungeeAuthProxy] [" + level + "] " + message; + String log = "[" + PREFIX + "] [" + level + "] " + message; if (e != null) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); diff --git a/src/main/java/com/artformgames/injector/bungeeauthproxy/handler/ProxiedHttpInitializer.java b/src/main/java/com/artformgames/injector/bungeeauthproxy/channel/ProxiedHttpInitializer.java similarity index 96% rename from src/main/java/com/artformgames/injector/bungeeauthproxy/handler/ProxiedHttpInitializer.java rename to src/main/java/com/artformgames/injector/bungeeauthproxy/channel/ProxiedHttpInitializer.java index 9bb0b22..c58e7dc 100644 --- a/src/main/java/com/artformgames/injector/bungeeauthproxy/handler/ProxiedHttpInitializer.java +++ b/src/main/java/com/artformgames/injector/bungeeauthproxy/channel/ProxiedHttpInitializer.java @@ -1,4 +1,4 @@ -package com.artformgames.injector.bungeeauthproxy.handler; +package com.artformgames.injector.bungeeauthproxy.channel; import com.artformgames.injector.bungeeauthproxy.Config; import io.netty.channel.Channel; diff --git a/src/main/java/com/artformgames/injector/bungeeauthproxy/handler/ProxyProtocolType.java b/src/main/java/com/artformgames/injector/bungeeauthproxy/channel/ProxyProtocolType.java similarity index 96% rename from src/main/java/com/artformgames/injector/bungeeauthproxy/handler/ProxyProtocolType.java rename to src/main/java/com/artformgames/injector/bungeeauthproxy/channel/ProxyProtocolType.java index 137589a..58660b5 100644 --- a/src/main/java/com/artformgames/injector/bungeeauthproxy/handler/ProxyProtocolType.java +++ b/src/main/java/com/artformgames/injector/bungeeauthproxy/channel/ProxyProtocolType.java @@ -1,4 +1,4 @@ -package com.artformgames.injector.bungeeauthproxy.handler; +package com.artformgames.injector.bungeeauthproxy.channel; import io.netty.handler.proxy.HttpProxyHandler; import io.netty.handler.proxy.ProxyHandler; diff --git a/src/main/java/com/artformgames/injector/bungeeauthproxy/handler/AuthHandler.java b/src/main/java/com/artformgames/injector/bungeeauthproxy/handler/AuthHandler.java new file mode 100644 index 0000000..845b52c --- /dev/null +++ b/src/main/java/com/artformgames/injector/bungeeauthproxy/handler/AuthHandler.java @@ -0,0 +1,10 @@ +package com.artformgames.injector.bungeeauthproxy.handler; + +import io.netty.channel.EventLoop; +import net.md_5.bungee.api.Callback; + +public interface AuthHandler { + + void submit(String url, EventLoop loop, Callback callback) throws Exception; + +} diff --git a/src/main/java/com/artformgames/injector/bungeeauthproxy/handler/ProxiedAuthHandler.java b/src/main/java/com/artformgames/injector/bungeeauthproxy/handler/ProxiedAuthHandler.java index 423df27..8aac8a3 100644 --- a/src/main/java/com/artformgames/injector/bungeeauthproxy/handler/ProxiedAuthHandler.java +++ b/src/main/java/com/artformgames/injector/bungeeauthproxy/handler/ProxiedAuthHandler.java @@ -1,6 +1,8 @@ package com.artformgames.injector.bungeeauthproxy.handler; import com.artformgames.injector.bungeeauthproxy.Config; +import com.artformgames.injector.bungeeauthproxy.channel.ProxiedHttpInitializer; +import com.artformgames.injector.bungeeauthproxy.channel.ProxyProtocolType; import com.google.common.base.Preconditions; import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; @@ -21,18 +23,19 @@ import static com.artformgames.injector.bungeeauthproxy.Logging.debug; -public class ProxiedAuthHandler { +public class ProxiedAuthHandler implements AuthHandler { protected Cache addressCache; public ProxiedAuthHandler() { - if (Config.SERVICE.DNS_CACHE_EXPIRE.getNotNull() > 0) { + if (Config.getDNSCacheDuration() > 0) { addressCache = CacheBuilder.newBuilder() - .expireAfterWrite(Config.SERVICE.DNS_CACHE_EXPIRE.getNotNull(), TimeUnit.MILLISECONDS) - .build(); + .expireAfterWrite(Config.getDNSCacheDuration(), TimeUnit.MILLISECONDS) + .maximumSize(5).build(); } } + @Override public void submit(String authURL, EventLoop loop, Callback callback) throws Exception { URI uri = new URI(authURL); Preconditions.checkNotNull((Object) uri.getScheme(), "scheme"); @@ -43,14 +46,14 @@ public void submit(String authURL, EventLoop loop, Callback callback) th Bootstrap bootstrap = new Bootstrap().channel(PipelineUtils.getChannel(null)).group(loop); InetAddress address = resolveAddress(uri.getHost()); - ProxyProtocolType proxyProtocol = getProxyProtocol(); + ProxyProtocolType proxyProtocol = Config.getProxyProtocol(); if (proxyProtocol != null) { debug("Using proxy protocol [" + proxyProtocol.name() + "] for " + uri.getHost() + ":" + port); bootstrap.handler(new ProxiedHttpInitializer(proxyProtocol, callback, ssl, uri.getHost(), port)); } else { bootstrap.handler(new HttpInitializer(callback, ssl, uri.getHost(), port)); } - bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, Math.max(Config.SERVICE.TIME_OUT.getNotNull().intValue(), 100)) + bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, Config.getTimeoutDuration()) .remoteAddress(address, port).connect().addListener((ChannelFutureListener) channel -> { if (channel.isSuccess()) { String path = uri.getRawPath() + (uri.getRawQuery() == null ? "" : "?" + uri.getRawQuery()); @@ -69,24 +72,11 @@ private InetAddress resolveAddress(String host) throws UnknownHostException, Exe else return addressCache.get(host, () -> InetAddress.getByName(host)); } - public ProxyProtocolType getProxyProtocol() { - return ProxyProtocolType.parse(Config.PROXY.PROTOCOL.getNotNull()); - } - private static int getPort(URI uri) { - int port = uri.getPort(); - if (port != -1) return port; - switch (uri.getScheme()) { - case "http": { - return 80; - } - case "https": { - return 443; - } - default: { - throw new IllegalArgumentException("Unknown scheme " + uri.getScheme()); - } - } + if (uri.getPort() != -1) return uri.getPort(); + else if (uri.getScheme().equals("https")) return 443; + else if (uri.getScheme().equals("http")) return 80; + throw new IllegalArgumentException("Unknown scheme " + uri.getScheme()); } } diff --git a/src/main/java/com/artformgames/injector/bungeeauthproxy/transformer/ProxyHandlerTransformer.java b/src/main/java/com/artformgames/injector/bungeeauthproxy/transformer/HttpClientTransformer.java similarity index 75% rename from src/main/java/com/artformgames/injector/bungeeauthproxy/transformer/ProxyHandlerTransformer.java rename to src/main/java/com/artformgames/injector/bungeeauthproxy/transformer/HttpClientTransformer.java index b1f8ca1..95c6fb4 100644 --- a/src/main/java/com/artformgames/injector/bungeeauthproxy/transformer/ProxyHandlerTransformer.java +++ b/src/main/java/com/artformgames/injector/bungeeauthproxy/transformer/HttpClientTransformer.java @@ -1,5 +1,6 @@ package com.artformgames.injector.bungeeauthproxy.transformer; +import com.artformgames.injector.bungeeauthproxy.Config; import javassist.*; import java.io.ByteArrayInputStream; @@ -10,7 +11,7 @@ import static com.artformgames.injector.bungeeauthproxy.Logging.debug; import static com.artformgames.injector.bungeeauthproxy.Logging.error; -public class ProxyHandlerTransformer implements ClassFileTransformer { +public class HttpClientTransformer implements ClassFileTransformer { @Override public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined, @@ -29,14 +30,15 @@ public byte[] transform(ClassLoader loader, String className, Class classBein handleMethod.setBody("{com.artformgames.injector.bungeeauthproxy.BungeeAuthProxy.submitRequest($1, $2, $3);}"); - // remove unused static initializer - CtConstructor staticBlock = clazz.getClassInitializer(); - if (staticBlock != null) clazz.removeConstructor(staticBlock); - - // remove unused cache field - CtField cacheField = clazz.getField("addressCache"); - clazz.removeField(cacheField); + if (Config.ADVANCE.REMOVE_UNUSED_FILED.getNotNull()) { + // remove unused static initializer + CtConstructor staticBlock = clazz.getClassInitializer(); + if (staticBlock != null) clazz.removeConstructor(staticBlock); + // remove unused cache field + CtField cacheField = clazz.getField("addressCache"); + clazz.removeField(cacheField); + } return clazz.toBytecode(); } catch (Exception e) {