解析所有发起购买的客户端ip地址

This commit is contained in:
2025-01-07 11:07:49 +08:00
parent e4a8bf80e9
commit 6a861305d6
19 changed files with 204 additions and 66 deletions

View File

@@ -7,10 +7,12 @@ import lombok.Getter;
@Getter
public enum CreditsEventsEnum {
PRICE("price","6"),
// PRICE("price","6"),
PRICE("price","1"),// for test
// PRICE("price","0.1"),
BUY_CREDITS("Buy Credits","60"),
// BUY_CREDITS("Buy Credits","60"),
BUY_CREDITS("Buy Credits","10"),// for test
REFUND("Refund","60"),
// BUY_CREDITS("Buy Credits","10"),

View File

@@ -1,7 +1,16 @@
package com.ai.da.common.utils;
import javax.servlet.http.HttpServletRequest;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
@Slf4j
public class RequestInfoUtil {
/**
@@ -45,4 +54,58 @@ public class RequestInfoUtil {
return ip;
}
/**
* 免费 API 服务可能有请求频率限制,如果你需要处理大量 IP 地址,可能需要考虑使用付费服务或购买 IP 地理位置数据库。此外,始终要遵守 API 提供商的使用条款和隐私政策
* @param ip
* @return
* {
* "query": "24.48.0.1",
* "status": "success",
* "country": "Canada",
* "countryCode": "CA",
* "region": "QC",
* "regionName": "Quebec",
* "city": "Montreal",
* "zip": "H1L",
* "lat": 45.6026,
* "lon": -73.5167,
* "timezone": "America/Toronto",
* "isp": "Le Groupe Videotron Ltee",
* "org": "Videotron Ltee",
* "as": "AS5769 Videotron Ltee"
* }
*/
public static Map getIPLocation(String ip) {
// String ip = "117.143.125.1"; // 替换为你想查询的 IP 地址
// String ip = "194.5.48.180"; // 替换为你想查询的 IP 地址
String apiURL = "http://ip-api.com/json/" + ip;
try {
URL url = new URL(apiURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String output;
StringBuilder outputBuilder = new StringBuilder();
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
outputBuilder.append(output);
System.out.println(output);
}
conn.disconnect();
Map map = JSONObject.parseObject(outputBuilder.toString(), Map.class);
log.info("map: {}", map);
return map;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}