中国算力平台算力登记系统2.0
yanzhaofeige
2024-09-30 3c4fee1db116c11d4f04727cfe076d7c94daeaf2
commit | author | age
43dc29 1 package com.odcc.cpzidc.common.utils.ip;
Y 2
3 import org.slf4j.Logger;
4 import org.slf4j.LoggerFactory;
5 import com.alibaba.fastjson2.JSON;
6 import com.alibaba.fastjson2.JSONObject;
f21d30 7 import com.odcc.cpzidc.common.config.CpzIdcConfig;
43dc29 8 import com.odcc.cpzidc.common.constant.Constants;
Y 9 import com.odcc.cpzidc.common.utils.StringUtils;
10 import com.odcc.cpzidc.common.utils.http.HttpUtils;
11
12 /**
13  * 获取地址类
14  * 
15  * @author ruoyi
16  */
17 public class AddressUtils
18 {
19     private static final Logger log = LoggerFactory.getLogger(AddressUtils.class);
20
21     // IP地址查询
22     public static final String IP_URL = "http://whois.pconline.com.cn/ipJson.jsp";
23
24     // 未知地址
25     public static final String UNKNOWN = "XX XX";
26
27     public static String getRealAddressByIP(String ip)
28     {
29         // 内网不查询
30         if (IpUtils.internalIp(ip))
31         {
32             return "内网IP";
33         }
f21d30 34         if (CpzIdcConfig.isAddressEnabled())
43dc29 35         {
Y 36             try
37             {
38                 String rspStr = HttpUtils.sendGet(IP_URL, "ip=" + ip + "&json=true", Constants.GBK);
39                 if (StringUtils.isEmpty(rspStr))
40                 {
41                     log.error("获取地理位置异常 {}", ip);
42                     return UNKNOWN;
43                 }
44                 JSONObject obj = JSON.parseObject(rspStr);
45                 String region = obj.getString("pro");
46                 String city = obj.getString("city");
47                 return String.format("%s %s", region, city);
48             }
49             catch (Exception e)
50             {
51                 log.error("获取地理位置异常 {}", ip);
52             }
53         }
54         return UNKNOWN;
55     }
56 }