|
|
|
|
@ -1,12 +1,26 @@ |
|
|
|
|
package com.toesbieya.my.utils; |
|
|
|
|
|
|
|
|
|
import com.toesbieya.my.model.vo.IpResultVo; |
|
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
|
|
import com.alibaba.fastjson.JSONArray; |
|
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
|
import org.springframework.util.StringUtils; |
|
|
|
|
import org.springframework.web.client.RestTemplate; |
|
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
public class IpUtil { |
|
|
|
|
private static final String API = "http://ip.taobao.com/service/getIpInfo.php"; |
|
|
|
|
private static final RestTemplate restTemplate = new RestTemplate(); |
|
|
|
|
|
|
|
|
|
private static List<IpAddressRequestHandler> handlerList = new ArrayList<>(); |
|
|
|
|
|
|
|
|
|
static { |
|
|
|
|
handlerList.add(new PconlineApi()); |
|
|
|
|
handlerList.add(new IphelpApi()); |
|
|
|
|
handlerList.add(new IpipApi()); |
|
|
|
|
handlerList.add(new TaobaoApi()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static final String REDIS_IP_CACHE_KEY = "ipCache"; |
|
|
|
|
|
|
|
|
|
@ -27,34 +41,98 @@ public class IpUtil { |
|
|
|
|
return ip; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static String getIpAddr(String ip) { |
|
|
|
|
public static String getIpAddress(String ip) { |
|
|
|
|
if (StringUtils.isEmpty(ip) || ip.equals("127.0.0.1")) { |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
String redisCache = (String) RedisUtil.hget(REDIS_IP_CACHE_KEY, ip); |
|
|
|
|
if (!StringUtils.isEmpty(redisCache)) { |
|
|
|
|
return redisCache; |
|
|
|
|
String cache = (String) RedisUtil.hget(REDIS_IP_CACHE_KEY, ip); |
|
|
|
|
if (!StringUtils.isEmpty(cache)) { |
|
|
|
|
return cache; |
|
|
|
|
} |
|
|
|
|
IpResultVo vo = HttpUtil.get(API + "?ip=" + ip, IpResultVo.class); |
|
|
|
|
if (vo == null || vo.getCode() != 0 || vo.getData() == null) { |
|
|
|
|
return null; |
|
|
|
|
if (handlerList.size() == 0) return null; |
|
|
|
|
for (IpAddressRequestHandler handler : handlerList) { |
|
|
|
|
try { |
|
|
|
|
cache = handler.getIpAddress(ip); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
if (!StringUtils.isEmpty(cache)) { |
|
|
|
|
RedisUtil.hset(REDIS_IP_CACHE_KEY, ip, cache); |
|
|
|
|
return cache; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static boolean invalidIp(String ip) { |
|
|
|
|
return ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private interface IpAddressRequestHandler { |
|
|
|
|
String getIpAddress(String ip); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//淘宝 部分ip无法查询
|
|
|
|
|
private static class TaobaoApi implements IpAddressRequestHandler { |
|
|
|
|
@Override |
|
|
|
|
public String getIpAddress(String ip) { |
|
|
|
|
String url = "http://ip.taobao.com/service/getIpInfo.php?ip=" + ip; |
|
|
|
|
String response = restTemplate.getForObject(url, String.class); |
|
|
|
|
JSONObject result = JSON.parseObject(response); |
|
|
|
|
if (!result.getInteger("code").equals(0) || result.get("data") == null) { |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
result = result.getJSONObject("data"); |
|
|
|
|
return handleAddress(result.getString("region"), result.getString("city")); |
|
|
|
|
} |
|
|
|
|
String region = vo.getData().getString("region"); |
|
|
|
|
String city = vo.getData().getString("city"); |
|
|
|
|
if (StringUtils.isEmpty(region) || StringUtils.isEmpty(city)) { |
|
|
|
|
redisCache = null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static class IphelpApi implements IpAddressRequestHandler { |
|
|
|
|
@Override |
|
|
|
|
public String getIpAddress(String ip) { |
|
|
|
|
String url = "https://ip.help.bj.cn/?ip=" + ip; |
|
|
|
|
String response = restTemplate.getForObject(url, String.class); |
|
|
|
|
JSONObject result = JSON.parseObject(response); |
|
|
|
|
if (!result.getString("status").equals("200") || result.get("data") == null) { |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
JSONArray array = result.getJSONArray("data"); |
|
|
|
|
if (array.size() == 0) return null; |
|
|
|
|
result = array.getJSONObject(0); |
|
|
|
|
return handleAddress(result.getString("province"), result.getString("city")); |
|
|
|
|
} |
|
|
|
|
else if (region.equals(city)) { |
|
|
|
|
redisCache = region; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//https://www.ipip.net/support/api.html 每天限1000次
|
|
|
|
|
private static class IpipApi implements IpAddressRequestHandler { |
|
|
|
|
@Override |
|
|
|
|
public String getIpAddress(String ip) { |
|
|
|
|
String url = "https://freeapi.ipip.net/" + ip; |
|
|
|
|
String response = restTemplate.getForObject(url, String.class); |
|
|
|
|
JSONArray result = JSON.parseArray(response); |
|
|
|
|
if (result.size() != 5) return null; |
|
|
|
|
return handleAddress(result.getString(1), result.getString(2)); |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
redisCache = region + city; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//这个不错
|
|
|
|
|
private static class PconlineApi implements IpAddressRequestHandler { |
|
|
|
|
@Override |
|
|
|
|
public String getIpAddress(String ip) { |
|
|
|
|
String url = "http://whois.pconline.com.cn/ipJson.jsp?json=true&ip=" + ip; |
|
|
|
|
String response = restTemplate.getForObject(url, String.class); |
|
|
|
|
JSONObject result = JSON.parseObject(response); |
|
|
|
|
return handleAddress(result.getString("pro"), result.getString("city")); |
|
|
|
|
} |
|
|
|
|
RedisUtil.hset(REDIS_IP_CACHE_KEY, ip, redisCache); |
|
|
|
|
return redisCache; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static boolean invalidIp(String ip) { |
|
|
|
|
return ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip); |
|
|
|
|
private static String handleAddress(String province, String city) { |
|
|
|
|
if (StringUtils.isEmpty(province) || StringUtils.isEmpty(city)) { |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
else if (province.equals(city)) { |
|
|
|
|
return province; |
|
|
|
|
} |
|
|
|
|
return province + city; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|