Skip to content

Commit

Permalink
1.1.0
Browse files Browse the repository at this point in the history
1.1.0 支持api查询
  • Loading branch information
handy-git authored Sep 21, 2023
1 parent b958b1a commit a0d3a87
Show file tree
Hide file tree
Showing 13 changed files with 309 additions and 18 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>cn.handyplus.region</groupId>
<artifactId>ip2region</artifactId>
<version>1.0.9</version>
<version>1.1.0</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/cn/handyplus/region/constants/IpConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,25 @@ public abstract class IpConstants {
*/
public static Map<UUID, Boolean> PLAYER_SHOW_MAP = new HashMap<>();

/**
* ipPlus360 ipv4 api地址
*
* @since 1.1.0
*/
public final static String IP_PLUS_360_IPV4 = "https://api.ipplus360.com/ip/geo/v1/city/";

/**
* ipPlus360 ipv6 api地址
*
* @since 1.1.0
*/
public final static String IP_PLUS_360_IPV6 = "https://api.ipplus360.com/ip/geo/v1/ipv6/";

/**
* ipApi api地址
*
* @since 1.1.0
*/
public final static String IP_API_IPV4 = "http://ip-api.com/json/";

}
9 changes: 8 additions & 1 deletion src/main/java/cn/handyplus/region/hook/PlaceholderUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ public String onRequest(OfflinePlayer player, String identifier) {
String provincial = list.get(2);
String municipal = list.get(3);
String serviceProvider = list.get(4);

String district = "0";
if (list.size() > 5) {
district = list.get(5);
}
String unknown = ConfigUtil.CONFIG.getString("unknown", "未知");
String local = ConfigUtil.CONFIG.getString("local", "内网IP");

Expand Down Expand Up @@ -99,6 +102,10 @@ public String onRequest(OfflinePlayer player, String identifier) {
if ("serviceProvider".equals(identifier)) {
return "0".equals(serviceProvider) ? unknown : serviceProvider;
}
// %ip2region_district%
if ("district".equals(identifier)) {
return "0".equals(district) ? unknown : district;
}
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import cn.handyplus.region.enter.Ip2regionEnter;
import cn.handyplus.region.service.Ip2regionService;
import cn.handyplus.region.util.ConfigUtil;
import cn.handyplus.region.util.IpUtil;
import cn.handyplus.region.util.SearcherUtil;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
Expand Down Expand Up @@ -49,7 +50,7 @@ public void run() {
Ip2regionEnter ip2regionEnter = ip2regionEnterOptional.get();
IpConstants.PLAYER_SHOW_MAP.put(player.getUniqueId(), ip2regionEnter.getShowEnable());
}
SearcherUtil.getPlayerRegion(player);
IpUtil.getPlayerRegion(player);
}
}.runTaskAsynchronously(Ip2region.getInstance());
}
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/cn/handyplus/region/param/IpApiParam.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cn.handyplus.region.param;

import lombok.Data;

/**
* @author handy
* @since 1.1.0
*/
@Data
public class IpApiParam {

private String status;
private String country;
private String countryCode;
private String region;
private String regionName;
private String city;
private String zip;
private Double lat;
private Double lon;
private String timezone;
private String isp;
private String org;
private String as;
private String query;

private String message;
}


46 changes: 46 additions & 0 deletions src/main/java/cn/handyplus/region/param/IpPlus360Param.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cn.handyplus.region.param;

import lombok.Data;

/**
* @author handy
* @since 1.1.0
*/
@Data
public class IpPlus360Param {

private String code;
private IpPlus360ParamData data;
private String msg;

@Data
public static class IpPlus360ParamData {
/**
* 国家
*/
private String country;
/**
* 区域
*/
private String continent;
/**
* 归属
*/
private String owner;
/**
* 省
*/
private String prov;
/**
* 市
*/
private String city;
/**
* 区县
*/
private String district;
}

}


16 changes: 16 additions & 0 deletions src/main/java/cn/handyplus/region/util/ConfigUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.bukkit.configuration.file.FileConfiguration;

import java.io.File;
import java.util.Collections;

/**
* 配置
Expand All @@ -31,6 +32,21 @@ public static void init() {
DB_PATH = file.getPath();
// 加载语言文件
LANG_CONFIG = HandyConfigUtil.loadLangConfig(CONFIG.getString("language", "zh_CN"));
// 升级配置
upConfig();
}

/**
* 升级节点处理
*
* @since 1.0.0
*/
public static void upConfig() {
// 1.0.0 添加数据来源
HandyConfigUtil.setPathIsNotContains(CONFIG, "dataSource", "offline", Collections.singletonList("数据来源 ( offline 或 ipPlus360 或 ipApi )"), "config.yml");
HandyConfigUtil.setPathIsNotContains(CONFIG, "ipPlus360Ipv4Key", "123456", Collections.singletonList("ipv4在线归属地(有免费额度) 购买地址: https://mall.ipplus360.com/pros/IPVFourGeoAPI"), "config.yml");
HandyConfigUtil.setPathIsNotContains(CONFIG, "ipPlus360Ipv6Key", "123456", Collections.singletonList("ipv6在线归属地 购买地址: https://mall.ipplus360.com/pros/IPGeoAPI"), "config.yml");
CONFIG = HandyConfigUtil.load("config.yml");
}

}
47 changes: 47 additions & 0 deletions src/main/java/cn/handyplus/region/util/IpApiUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package cn.handyplus.region.util;

import cn.handyplus.lib.core.HttpUtil;
import cn.handyplus.lib.core.JsonUtil;
import cn.handyplus.lib.core.StrUtil;
import cn.handyplus.lib.util.MessageUtil;
import cn.handyplus.region.constants.IpConstants;
import cn.handyplus.region.param.IpApiParam;
import org.bukkit.entity.Player;

/**
* 对接 ip-api.com api
*
* @author handy
* @since 1.1.0
*/
public class IpApiUtil {

/**
* 获取地址
*
* @param player 玩家
*/
protected static void getPlayerRegion(Player player) {
try {
// IP地址
String ip = player.getAddress().getAddress().getHostAddress();
String testIp = ConfigUtil.CONFIG.getString("testIp", ip);
String json = HttpUtil.get(IpConstants.IP_API_IPV4 + testIp + "?lang=zh-CN");
// 未获取到数据
if (StrUtil.isEmpty(json)) {
return;
}
IpApiParam ipApiParam = JsonUtil.toBean(json, IpApiParam.class);
// 转换异常
if (!"success".equalsIgnoreCase(ipApiParam.getStatus())) {
MessageUtil.sendConsoleMessage(ipApiParam.getMessage());
return;
}
String region = IpUtil.getStr(ipApiParam.getCountry()) + "|" + IpUtil.getStr(ipApiParam.getAs()) + "|" + IpUtil.getStr(ipApiParam.getRegionName()) + "|" + IpUtil.getStr(ipApiParam.getCity()) + "|" + IpUtil.getStr(ipApiParam.getIsp());
IpConstants.PLAYER_REGION_MAP.put(player.getUniqueId(), region);
} catch (Exception ignored) {
}
}


}
74 changes: 74 additions & 0 deletions src/main/java/cn/handyplus/region/util/IpPlus360Util.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package cn.handyplus.region.util;

import cn.handyplus.lib.core.HttpUtil;
import cn.handyplus.lib.core.JsonUtil;
import cn.handyplus.lib.core.StrUtil;
import cn.handyplus.lib.util.MessageUtil;
import cn.handyplus.region.constants.IpConstants;
import cn.handyplus.region.param.IpPlus360Param;
import org.bukkit.entity.Player;

import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;

/**
* 对接 ipPlus360.com api
*
* @author handy
* @since 1.1.0
*/
public class IpPlus360Util {

/**
* 获取地址
*
* @param player 玩家
*/
protected static void getPlayerRegion(Player player) {
try {
String ipPlus360Ipv4Key = ConfigUtil.CONFIG.getString("ipPlus360Ipv4Key", "123456");
String ipPlus360Ipv6Key = ConfigUtil.CONFIG.getString("ipPlus360Ipv6Key", "123456");
// IP地址
InetAddress inetAddress = player.getAddress().getAddress();
String ip = inetAddress.getHostAddress();
String json = null;

String testIp4 = ConfigUtil.CONFIG.getString("testIp4", "");
String testIp6 = ConfigUtil.CONFIG.getString("testIp6", "");
// 判断是何种类型
if (StrUtil.isNotEmpty(testIp4) || inetAddress instanceof Inet4Address) {
// 未填写key
if ("123456".equals(ipPlus360Ipv4Key)) {
return;
}
ip = StrUtil.isNotEmpty(testIp4) ? testIp4 : ip;
String ipPlus360Ipv4Url = ConfigUtil.CONFIG.getString("ipPlus360Ipv4Url", IpConstants.IP_PLUS_360_IPV4);
json = HttpUtil.get(ipPlus360Ipv4Url + "?key=" + ipPlus360Ipv4Key + "&ip=" + ip + "&coordsys=WGS84");
} else if (StrUtil.isNotEmpty(testIp6) || inetAddress instanceof Inet6Address) {
// 未填写key
if ("123456".equals(ipPlus360Ipv6Key)) {
return;
}
ip = StrUtil.isNotEmpty(testIp6) ? testIp6 : ip;
String ipPlus360Ipv6Url = ConfigUtil.CONFIG.getString("ipPlus360Ipv6Url", IpConstants.IP_PLUS_360_IPV6);
json = HttpUtil.get(ipPlus360Ipv6Url + "?key=" + ipPlus360Ipv6Key + "&ip=" + ip + "&coordsys=WGS84");
}
// 未获取到数据
if (StrUtil.isEmpty(json)) {
return;
}
IpPlus360Param ipPlus360Param = JsonUtil.toBean(json, IpPlus360Param.class);
// 转换异常
if (!"Success".equalsIgnoreCase(ipPlus360Param.getCode())) {
MessageUtil.sendConsoleMessage(ipPlus360Param.getMsg());
return;
}
IpPlus360Param.IpPlus360ParamData data = ipPlus360Param.getData();
String region = IpUtil.getStr(data.getCountry()) + "|" + IpUtil.getStr(data.getContinent()) + "|" + IpUtil.getStr(data.getProv()) + "|" + IpUtil.getStr(data.getCity()) + "|" + IpUtil.getStr(data.getOwner() + "|" + IpUtil.getStr(data.getDistrict()));
IpConstants.PLAYER_REGION_MAP.put(player.getUniqueId(), region);
} catch (Exception ignored) {
}
}

}
45 changes: 45 additions & 0 deletions src/main/java/cn/handyplus/region/util/IpUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package cn.handyplus.region.util;

import cn.handyplus.lib.core.StrUtil;
import org.bukkit.entity.Player;

/**
* 获取ip地址
*
* @author handy
* @since 1.1.0
*/
public class IpUtil {

/**
* 获取地址
*
* @param player 玩家
*/
public static void getPlayerRegion(Player player) {
String dataSource = ConfigUtil.CONFIG.getString("dataSource", "offline");
// 离线模式
if ("offline".equalsIgnoreCase(dataSource)) {
SearcherUtil.getPlayerRegion(player);
}
// 请求 ipPlus360 模式
if ("ipPlus360".equalsIgnoreCase(dataSource)) {
IpPlus360Util.getPlayerRegion(player);
}
// 请求 ipApi 模式
if ("ipApi".equalsIgnoreCase(dataSource)) {
IpApiUtil.getPlayerRegion(player);
}
}

/**
* 兼容默认值
*
* @param str 值
* @return 默认值
*/
protected static String getStr(String str) {
return StrUtil.isNotEmpty(str) ? str : "0";
}

}
17 changes: 5 additions & 12 deletions src/main/java/cn/handyplus/region/util/SearcherUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,11 @@ public class SearcherUtil {
*
* @param player 玩家
*/
public static void getPlayerRegion(Player player) {
try {
// 1、创建 searcher 对象
Searcher searcher = Searcher.newWithFileOnly(ConfigUtil.DB_PATH);
// 2、查询
String ip = player.getAddress().getAddress().getHostAddress();
String region = searcher.search(ip);
IpConstants.PLAYER_REGION_MAP.put(player.getUniqueId(), region);
// 3、关闭资源
searcher.close();
} catch (Exception ignored) {
}
protected static void getPlayerRegion(Player player) {
String ip = player.getAddress().getAddress().getHostAddress();
ip = ConfigUtil.CONFIG.getString("testIp", ip);
String region = getIpRegion(ip);
IpConstants.PLAYER_REGION_MAP.put(player.getUniqueId(), region);
}

/**
Expand Down
13 changes: 12 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,15 @@ removeProvinceAndCity: false

unknown: "未知"

local: "内网IP"
local: "内网IP"

# 数据来源 ( offline 或 ipPlus360 或 ipApi )
# offline 本地数据源模式(只支持ipv4 百分之99.9数据准确性)
# ipPlus360 在线api模式(准确度差不多,支持ipv4/ipv6,必需先购买key填下面)
# ipApi 在线api模式(支持ipv4/ipv6 免费 限制 每分钟请求 45 次,也就是一分钟服务器同时进45人 官网:https://ip-api.com/)
dataSource: offline

# ipPlus360模式 ipv4在线归属地(有试用额度) 购买地址: https://mall.ipplus360.com/pros/IPVFourGeoAPI
ipPlus360Ipv4Key: 123456
# ipPlus360模式 ipv6在线归属地 购买地址: https://mall.ipplus360.com/pros/IPGeoAPI
ipPlus360Ipv6Key: 123456
Loading

0 comments on commit a0d3a87

Please sign in to comment.