【新增】filter拦截器

This commit is contained in:
wxd-gaming
2025-02-10 17:18:08 +08:00
parent 7747e7474a
commit a3ec6e8839
9 changed files with 85 additions and 57 deletions

View File

@@ -37,6 +37,9 @@ public abstract class Event implements Runnable, IAssistMonitor, RunMonitor {
}
public Event(Method method) {
if (method == null) {
return;
}
/* 虚拟线程 */
AtomicBoolean vt = new AtomicBoolean();
AtomicReference<String> threadName = new AtomicReference<>();

View File

@@ -2,6 +2,7 @@ package wxdgaming.boot.net.controller;
import wxdgaming.boot.agent.LogbackUtil;
import wxdgaming.boot.agent.function.FunctionE;
import wxdgaming.boot.agent.function.FunctionE2;
import wxdgaming.boot.agent.system.AnnUtil;
import wxdgaming.boot.assist.JavaAssistBox;
@@ -11,6 +12,8 @@ import wxdgaming.boot.core.threading.ExecutorLog;
import wxdgaming.boot.net.NioBase;
import wxdgaming.boot.net.Session;
import wxdgaming.boot.net.controller.ann.TextMapping;
import wxdgaming.boot.net.handler.RpcListenerAction;
import wxdgaming.boot.net.web.hs.HttpListenerAction;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
@@ -32,7 +35,9 @@ public class MappingFactory {
public static boolean OUT_FILE_Java_Assist_Code = false;
/** text mapping submit 监听 */
public static FunctionE2<Session, Event, Boolean> TextMappingSubmitBefore = null;
public static FunctionE<HttpListenerAction, Boolean> HttpMappingSubmitBefore = null;
/** rpc mapping submit 监听 */
public static FunctionE<RpcListenerAction, Boolean> RPCMappingSubmitBefore = null;
/** proto mapping submit 监听 */
public static FunctionE2<Session, Event, Boolean> ProtoMappingSubmitBefore = null;
@@ -86,7 +91,7 @@ public class MappingFactory {
PROTO_MAP.put(
service,
messageId,
new ProtoMappingRecord(service, remarks, messageId, protoMappingProxy, instance, method,showLog)
new ProtoMappingRecord(service, remarks, messageId, protoMappingProxy, instance, method, showLog)
);
}

View File

@@ -96,9 +96,9 @@ public class ReqRemoteHandler {
}
});
if (MappingFactory.TextMappingSubmitBefore != null) {
if (MappingFactory.RPCMappingSubmitBefore != null) {
try {
Boolean apply = MappingFactory.TextMappingSubmitBefore.apply(session, listenerAction);
Boolean apply = MappingFactory.RPCMappingSubmitBefore.apply(listenerAction);
if (Boolean.FALSE.equals(apply)) return;
} catch (Throwable e) {
throw new RuntimeException(e);

View File

@@ -27,7 +27,7 @@ import java.util.function.Consumer;
* @version: 2023-12-19 15:38
**/
@Slf4j
class RpcListenerAction extends Event {
public final class RpcListenerAction extends Event {
private final TextMappingRecord mappingRecord;
private final SocketSession session;

View File

@@ -5,6 +5,7 @@ import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import wxdgaming.boot.agent.GlobalUtil;
import wxdgaming.boot.agent.exception.Throw;
@@ -17,8 +18,6 @@ import wxdgaming.boot.core.lang.RunResult;
import wxdgaming.boot.core.str.StringUtil;
import wxdgaming.boot.core.str.json.FastJsonUtil;
import wxdgaming.boot.core.threading.Event;
import wxdgaming.boot.core.threading.ExecutorLog;
import wxdgaming.boot.core.threading.ThreadInfo;
import wxdgaming.boot.core.timer.MyClock;
import wxdgaming.boot.net.Session;
import wxdgaming.boot.net.controller.MappingFactory;
@@ -33,10 +32,10 @@ import wxdgaming.boot.net.web.hs.controller.cmd.HttpSignCheck;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.lang.reflect.Type;
import java.util.Date;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
/**
@@ -46,56 +45,18 @@ import java.util.concurrent.atomic.AtomicReference;
* @version: 2023-12-18 19:38
**/
@Slf4j
class HttpListenerAction extends Event {
@Getter
public final class HttpListenerAction extends Event {
protected HttpServer httpServer;
protected HttpSession session;
final HttpServer httpServer;
final HttpSession session;
public HttpListenerAction(HttpServer httpServer, HttpSession session) {
public HttpListenerAction(Method method, HttpServer httpServer, HttpSession session) {
super(method);
this.httpServer = httpServer;
this.session = session;
}
@Override public long getLogTime() {
return Optional.ofNullable(session.getUriPath())
.map(v -> MappingFactory.textMappingRecord(httpServer.getClass(), v.toLowerCase()))
.map(v -> AnnUtil.ann(v.method(), ExecutorLog.class))
.map(ExecutorLog::logTime)
.orElse(66L);
}
@Override public long getWarningTime() {
return Optional.ofNullable(session.getUriPath())
.map(v -> MappingFactory.textMappingRecord(httpServer.getClass(), v.toLowerCase()))
.map(v -> AnnUtil.ann(v.method(), ExecutorLog.class))
.map(ExecutorLog::warningTime)
.orElse(super.getWarningTime());
}
@Override public boolean isVt() {
return Optional.ofNullable(session.getUriPath())
.map(v -> MappingFactory.textMappingRecord(httpServer.getClass(), v.toLowerCase()))
.map(v -> AnnUtil.ann(v.method(), ThreadInfo.class))
.map(ThreadInfo::vt)
.orElse(false);
}
@Override public String getThreadName() {
return Optional.ofNullable(session.getUriPath())
.map(v -> MappingFactory.textMappingRecord(httpServer.getClass(), v.toLowerCase()))
.map(v -> AnnUtil.ann(v.method(), ThreadInfo.class))
.map(ThreadInfo::threadName)
.orElse("");
}
@Override public String getQueueName() {
return Optional.ofNullable(session.getUriPath())
.map(v -> MappingFactory.textMappingRecord(httpServer.getClass(), v.toLowerCase()))
.map(v -> AnnUtil.ann(v.method(), ThreadInfo.class))
.map(ThreadInfo::queueName)
.orElse("");
}
@Override public String getTaskInfoString() {
return session.getDomainName() + session.getUriPath();
}

View File

@@ -30,6 +30,7 @@ import wxdgaming.boot.net.http.ssl.SslProtocolType;
import javax.net.ssl.SSLContext;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.*;
@@ -160,11 +161,21 @@ public class HttpServer extends NioServer<HttpSession> {
if (reqMethod.equals(HttpMethod.POST)) {
session.actionPostData();
}
HttpListenerAction httpListenerAction = new HttpListenerAction(HttpServer.this, session);
if (MappingFactory.TextMappingSubmitBefore != null) {
Method method = Optional.ofNullable(session.getUriPath())
.map(v -> MappingFactory.textMappingRecord(HttpServer.this.getClass(), v.toLowerCase()))
.map(v -> v.method())
.orElse(null);
HttpListenerAction httpListenerAction = new HttpListenerAction(method, HttpServer.this, session);
if (MappingFactory.HttpMappingSubmitBefore != null) {
try {
Boolean apply = MappingFactory.TextMappingSubmitBefore.apply(session, httpListenerAction);
if (Boolean.FALSE.equals(apply)) return;
Boolean apply = MappingFactory.HttpMappingSubmitBefore.apply(httpListenerAction);
if (Boolean.FALSE.equals(apply)) {
return;
}
} catch (Exception e) {
throw new RuntimeException(e);
}

View File

@@ -2,10 +2,12 @@ package wxdgaming.boot.starter;
import wxdgaming.boot.agent.exception.Throw;
import wxdgaming.boot.agent.system.ReflectContext;
import wxdgaming.boot.core.system.JvmUtil;
import wxdgaming.boot.net.controller.MappingFactory;
import wxdgaming.boot.starter.action.ActionConfig;
import wxdgaming.boot.starter.config.Config;
import wxdgaming.boot.starter.i.IConfigInit;
import wxdgaming.boot.starter.net.filter.HttpFilter;
import wxdgaming.boot.starter.net.filter.RpcFilter;
import wxdgaming.boot.starter.service.*;
import java.util.function.Consumer;
@@ -63,6 +65,20 @@ class BootStarterModule extends BaseModule<BootStarterModule> {
socketAction.accept(WsService3.class, bootConfig.getWebSocket3());
}
/* TODO 添加 aop 拦截器 */
MappingFactory.HttpMappingSubmitBefore = (event) -> {
return AppContext.context()
.beanStream(HttpFilter.class)
.allMatch(filter -> filter.doFilter(event));
};
/* TODO 添加 aop 拦截器 */
MappingFactory.RPCMappingSubmitBefore = (event) -> {
return AppContext.context()
.beanStream(RpcFilter.class)
.allMatch(filter -> filter.doFilter(event));
};
bindSingleton(BootConfig.class, bootConfig);
bindSingleton(IocMainContext.class);
bindSingleton(ScheduledService.class);

View File

@@ -0,0 +1,16 @@
package wxdgaming.boot.starter.net.filter;
import wxdgaming.boot.net.web.hs.HttpListenerAction;
/**
* http 过滤器
*
* @author: wxd-gaming(無心道, 15388152619)
* @version: 2025-02-10 16:38
**/
public abstract class HttpFilter {
public abstract boolean doFilter(HttpListenerAction httpListenerAction);
}

View File

@@ -0,0 +1,16 @@
package wxdgaming.boot.starter.net.filter;
import wxdgaming.boot.net.handler.RpcListenerAction;
/**
* http 过滤器
*
* @author: wxd-gaming(無心道, 15388152619)
* @version: 2025-02-10 16:38
**/
public abstract class RpcFilter {
public abstract boolean doFilter(RpcListenerAction rpcListenerAction);
}