-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1.1.0 支持api查询
- Loading branch information
Showing
13 changed files
with
309 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
46
src/main/java/cn/handyplus/region/param/IpPlus360Param.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
} | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.