yumeng 1 неделя назад
Родитель
Сommit
6c8d2bdb09
1 измененных файлов с 50 добавлено и 6 удалено
  1. 50 6
      src/main/java/com/adx/tencent/honor/HonorTrackingController.java

+ 50 - 6
src/main/java/com/adx/tencent/honor/HonorTrackingController.java

@@ -15,6 +15,9 @@ import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.server.ResponseStatusException;
 
+import java.nio.charset.StandardCharsets;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
 import java.time.Instant;
 import java.util.ArrayList;
 import java.util.Collections;
@@ -181,16 +184,22 @@ public class HonorTrackingController {
 
         Map<String, Object> device = new LinkedHashMap<>();
         device.put("ip", firstParam(params, "ip", "m6a") != null ? firstParam(params, "ip", "m6a") : clientIp(request));
-        device.put("ua", firstParam(params, "ua", "user_agent", "useragent") != null
-                ? firstParam(params, "ua", "user_agent", "useragent")
-                : defaultUa(resolved.platform()));
+        String rawUa = firstParam(params, "ua", "user_agent", "useragent");
+        String normalizedUa = normalizeUserAgent(rawUa, resolved.platform());
+        device.put("ua", normalizedUa);
         device.put("deviceType", 1);
         if ("ios".equals(resolved.platform())) device.put("os", 1);
         if ("android".equals(resolved.platform())) device.put("os", 2);
         Map<String, Object> uid = new LinkedHashMap<>();
-        putUid(uid, "oaid", params.get("oaid"));
-        putUid(uid, "oaidMd5", firstParam(params, "oaid_md5", "oaidMd5", "hash_oaid"));
-        String muid = firstParam(params, "oaid_md5", "oaidMd5", "hash_oaid", "oaid");
+        String oaid = firstParam(params, "oaid");
+        String oaidMd5 = firstParam(params, "oaid_md5", "oaidMd5", "hash_oaid");
+        if ((oaidMd5 == null || oaidMd5.isBlank()) && oaid != null && !oaid.isBlank()) {
+            oaidMd5 = md5Hex(oaid);
+            params.put("oaidMd5", oaidMd5);
+        }
+        putUid(uid, "oaid", oaid);
+        putUid(uid, "oaidMd5", oaidMd5);
+        String muid = firstParam(params, "muid", "oaid_md5", "oaidMd5", "hash_oaid");
         putUid(uid, "muid", muid);
         if (!uid.isEmpty()) device.put("uid", uid);
 
@@ -320,6 +329,41 @@ public class HonorTrackingController {
         return "Mozilla/5.0 (Linux; Android 14; Pixel 8 Build/UQ1A.240205.002) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.6478.71 Mobile Safari/537.36";
     }
 
+    private static String normalizeUserAgent(String ua, String platform) {
+        if (ua == null || ua.isBlank()) {
+            return defaultUa(platform);
+        }
+        String normalized = ua.trim();
+        String lower = normalized.toLowerCase();
+        if (lower.startsWith("okhttp/")
+                || lower.startsWith("java/")
+                || lower.startsWith("curl/")
+                || lower.startsWith("wget/")
+                || lower.contains("windows nt")
+                || lower.contains("macintosh")
+                || lower.contains("x11;")
+                || lower.contains("win64")
+                || lower.contains("wow64")) {
+            return defaultUa(platform);
+        }
+        return normalized;
+    }
+
+    private static String md5Hex(String value) {
+        try {
+            MessageDigest md = MessageDigest.getInstance("MD5");
+            byte[] digest = md.digest(value.getBytes(StandardCharsets.UTF_8));
+            StringBuilder sb = new StringBuilder(digest.length * 2);
+            for (byte b : digest) {
+                sb.append(Character.forDigit((b >>> 4) & 0xF, 16));
+                sb.append(Character.forDigit(b & 0xF, 16));
+            }
+            return sb.toString();
+        } catch (NoSuchAlgorithmException e) {
+            throw new IllegalStateException("MD5 algorithm unavailable", e);
+        }
+    }
+
     private static String clientIp(HttpServletRequest request) {
         String xff = request.getHeader("X-Forwarded-For");
         if (xff != null && !xff.isBlank()) {