mirror of
https://github.com/qaiu/netdisk-fast-download.git
synced 2026-02-24 22:25:22 +00:00
remove yarn.lock
This commit is contained in:
@@ -2,12 +2,15 @@ package cn.qaiu.lz.common.interceptorImpl;
|
||||
|
||||
import cn.qaiu.vx.core.annotaions.HandleSortFilter;
|
||||
import cn.qaiu.vx.core.interceptor.BeforeInterceptor;
|
||||
import cn.qaiu.vx.core.util.ConfigConstant;
|
||||
import cn.qaiu.vx.core.util.SharedDataUtil;
|
||||
import io.vertx.core.json.JsonArray;
|
||||
import io.vertx.core.json.JsonObject;
|
||||
import io.vertx.ext.web.RoutingContext;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import static cn.qaiu.vx.core.util.ConfigConstant.IGNORES_REG;
|
||||
import static io.vertx.core.http.HttpHeaders.CONTENT_TYPE;
|
||||
|
||||
/**
|
||||
* 前置拦截器实现
|
||||
@@ -20,8 +23,43 @@ public class DefaultInterceptor implements BeforeInterceptor {
|
||||
|
||||
@Override
|
||||
public void handle(RoutingContext ctx) {
|
||||
// System.out.println("进入前置拦截器1->" + ctx.request().path());
|
||||
doNext(ctx);
|
||||
// 读取配置 如果配置了限流 则进行限流
|
||||
if (!SharedDataUtil.getJsonConfig(ConfigConstant.GLOBAL_CONFIG).containsKey("rateLimit")) {
|
||||
doNext(ctx);
|
||||
return;
|
||||
}
|
||||
JsonObject rateLimit = SharedDataUtil.getJsonConfig(ConfigConstant.GLOBAL_CONFIG)
|
||||
.getJsonObject("rateLimit");
|
||||
// # 限流配置
|
||||
//rateLimit:
|
||||
// # 是否启用限流
|
||||
// enable: true
|
||||
// # 限流的请求数
|
||||
// limit: 1000
|
||||
// # 限流的时间窗口(单位秒)
|
||||
// timeWindow: 60
|
||||
if (rateLimit.getBoolean("enable")) {
|
||||
// 获取当前请求的路径
|
||||
String path = ctx.request().path();
|
||||
// 正则匹配路径
|
||||
if (ignores.stream().anyMatch(ignore -> path.matches(ignore.toString()))) {
|
||||
// 如果匹配到忽略的路径,则不进行限流
|
||||
doNext(ctx);
|
||||
return;
|
||||
}
|
||||
RateLimiter.checkRateLimit(ctx.request())
|
||||
.onSuccess(v -> {
|
||||
// 继续执行下一个拦截器
|
||||
doNext(ctx);
|
||||
})
|
||||
.onFailure(t -> {
|
||||
// 限流失败,返回错误响应
|
||||
log.warn("Rate limit exceeded for path: {}", path);
|
||||
ctx.response().putHeader(CONTENT_TYPE, "text/html; charset=utf-8")
|
||||
.setStatusCode(429)
|
||||
.end(t.getMessage());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
package cn.qaiu.lz.common.interceptorImpl;
|
||||
|
||||
import io.vertx.core.Future;
|
||||
import io.vertx.core.Promise;
|
||||
import io.vertx.core.http.HttpServerRequest;
|
||||
import io.vertx.core.json.JsonObject;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@Slf4j
|
||||
public class RateLimiter {
|
||||
|
||||
private static final Map<String, RequestInfo> ipRequestMap = new ConcurrentHashMap<>();
|
||||
private static int MAX_REQUESTS = 10; // 最大请求次数
|
||||
private static long TIME_WINDOW = 60 * 1000; // 时间窗口(毫秒)
|
||||
|
||||
private static String PATH_REG; // 限流路径正则
|
||||
|
||||
public static void init(JsonObject rateLimitConfig) {
|
||||
MAX_REQUESTS = rateLimitConfig.getInteger("limit", 10);
|
||||
TIME_WINDOW = rateLimitConfig.getInteger("timeWindow", 60) * 1000L; // 转换为毫秒
|
||||
PATH_REG = rateLimitConfig.getString("pathReg", "/.*");
|
||||
log.info("RateLimiter initialized with max requests: {}, time window: {} ms, path regex: {}",
|
||||
MAX_REQUESTS, TIME_WINDOW, PATH_REG);
|
||||
}
|
||||
|
||||
synchronized public static Future<Void> checkRateLimit(HttpServerRequest request) {
|
||||
Promise<Void> promise = Promise.promise();
|
||||
if (!request.path().matches(PATH_REG)) {
|
||||
// 如果请求路径不匹配正则,则不进行限流
|
||||
promise.complete();
|
||||
return promise.future();
|
||||
}
|
||||
|
||||
String ip = request.remoteAddress().host();
|
||||
|
||||
ipRequestMap.compute(ip, (key, requestInfo) -> {
|
||||
long currentTime = System.currentTimeMillis();
|
||||
if (requestInfo == null || currentTime - requestInfo.timestamp > TIME_WINDOW) {
|
||||
// 初始化或重置计数器
|
||||
return new RequestInfo(1, currentTime);
|
||||
} else {
|
||||
// 增加计数器
|
||||
requestInfo.count++;
|
||||
return requestInfo;
|
||||
}
|
||||
});
|
||||
|
||||
RequestInfo info = ipRequestMap.get(ip);
|
||||
if (info.count > MAX_REQUESTS) {
|
||||
// 超过限制
|
||||
// 计算剩余时间
|
||||
long remainingTime = TIME_WINDOW - (System.currentTimeMillis() - info.timestamp);
|
||||
BigDecimal bigDecimal = BigDecimal.valueOf(remainingTime / 1000.0)
|
||||
.setScale(2, RoundingMode.HALF_UP);
|
||||
promise.fail("请求次数太多了,请" + bigDecimal + "秒后再试。");
|
||||
} else {
|
||||
// 未超过限制,继续处理
|
||||
promise.complete();
|
||||
}
|
||||
return promise.future();
|
||||
}
|
||||
|
||||
private static class RequestInfo {
|
||||
int count;
|
||||
long timestamp;
|
||||
|
||||
RequestInfo(int count, long time) {
|
||||
this.count = count;
|
||||
this.timestamp = time;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user