修复订阅访问IP记录缺失问题
This commit is contained in:
@@ -188,12 +188,7 @@ public class SubService {
|
||||
sendStatus(response, HttpServletResponse.SC_NOT_FOUND, "subscription not found");
|
||||
return;
|
||||
}
|
||||
String ip = request.getRemoteAddr();
|
||||
if ("127.0.0.1".equals(ip)) {
|
||||
ip = request.getHeader("X-Forwarded-For");
|
||||
if (ip != null && ip.contains(",")) ip = ip.split(",")[0].trim();
|
||||
if (ip != null && ip.contains(":")) ip = ip.split(":")[0].trim();
|
||||
}
|
||||
String ip = resolveClientIp(request);
|
||||
String ua = request.getHeader("User-Agent");
|
||||
if (ua == null) return;
|
||||
recordUpdate(subBind.getUser(), ip, ua);
|
||||
@@ -209,6 +204,31 @@ public class SubService {
|
||||
FileDownload.export(request, response, path.toString());
|
||||
}
|
||||
|
||||
private static String resolveClientIp(HttpServletRequest request) {
|
||||
String remoteAddr = headerAddress(request.getRemoteAddr());
|
||||
if (isLoopback(remoteAddr)) {
|
||||
String forwardedFor = headerAddress(request.getHeader("X-Forwarded-For"));
|
||||
if (forwardedFor != null)
|
||||
return forwardedFor;
|
||||
String realIp = headerAddress(request.getHeader("X-Real-IP"));
|
||||
if (realIp != null)
|
||||
return realIp;
|
||||
}
|
||||
return remoteAddr == null ? "unknown" : remoteAddr;
|
||||
}
|
||||
|
||||
private static String headerAddress(String value) {
|
||||
if (value == null || value.isBlank())
|
||||
return null;
|
||||
String address = value.split(",", 2)[0].trim();
|
||||
return address.isBlank() || "unknown".equalsIgnoreCase(address) ? null : address;
|
||||
}
|
||||
|
||||
private static boolean isLoopback(String address) {
|
||||
return "127.0.0.1".equals(address) || "::1".equals(address)
|
||||
|| "0:0:0:0:0:0:0:1".equals(address);
|
||||
}
|
||||
|
||||
private void recordUpdate(String user, String ip, String ua) {
|
||||
String location;
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user