mirror of
https://github.com/getrebuild/rebuild.git
synced 2025-02-25 23:05:06 +08:00
ip location
This commit is contained in:
parent
17055d8d65
commit
1ef575d486
6 changed files with 263 additions and 99 deletions
|
@ -18,92 +18,99 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|||
|
||||
package com.rebuild.utils;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
|
||||
/**
|
||||
* JSON format
|
||||
*
|
||||
*
|
||||
* @author devezhao
|
||||
* @since 09/29/2018
|
||||
*/
|
||||
public class JSONUtils {
|
||||
|
||||
public static final String EMPTY_OBJECT_STR = "{}";
|
||||
public static final String EMPTY_ARRAY_STR = "[]";
|
||||
public static final JSONObject EMPTY_OBJECT = JSON.parseObject(EMPTY_OBJECT_STR);
|
||||
public static final JSONArray EMPTY_ARRAY = JSON.parseArray(EMPTY_ARRAY_STR);
|
||||
|
||||
/**
|
||||
* @param key
|
||||
* @param value
|
||||
* @return
|
||||
*/
|
||||
public static JSONObject toJSONObject(String key, Object value) {
|
||||
return toJSONObject(new String[] { key }, new Object[] { value });
|
||||
}
|
||||
public static final String EMPTY_OBJECT_STR = "{}";
|
||||
public static final String EMPTY_ARRAY_STR = "[]";
|
||||
public static final JSONObject EMPTY_OBJECT = JSON.parseObject(EMPTY_OBJECT_STR);
|
||||
public static final JSONArray EMPTY_ARRAY = JSON.parseArray(EMPTY_ARRAY_STR);
|
||||
|
||||
/**
|
||||
* @param keys
|
||||
* @param values
|
||||
* @return
|
||||
*/
|
||||
public static JSONObject toJSONObject(String keys[], Object values[]) {
|
||||
Assert.isTrue(keys.length <= values.length, "K/V 长度不匹配");
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
map.put(keys[i], values[i]);
|
||||
}
|
||||
return (JSONObject) JSON.toJSON(map);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param keys
|
||||
* @param valuesArray
|
||||
* @return
|
||||
*/
|
||||
public static JSONArray toJSONArray(String keys[], Object valuesArray[][]) {
|
||||
List<Map<String, Object>> array = new ArrayList<>();
|
||||
for (Object[] o : valuesArray) {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
map.put(keys[i], o[i]);
|
||||
}
|
||||
array.add(map);
|
||||
}
|
||||
return (JSONArray) JSON.toJSON(array);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param items
|
||||
* @return
|
||||
*/
|
||||
public static JSONArray toJSONArray(JSONable[] items) {
|
||||
if (items == null || items.length == 0) {
|
||||
return EMPTY_ARRAY;
|
||||
}
|
||||
|
||||
JSONArray array = new JSONArray();
|
||||
for (JSONable e : items) {
|
||||
array.add(e.toJSON());
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param json
|
||||
* @return
|
||||
*/
|
||||
public static JSON clone(JSON json) {
|
||||
String tostr = json.toJSONString();
|
||||
return (JSON) JSON.parse(tostr);
|
||||
}
|
||||
/**
|
||||
* @param key
|
||||
* @param value
|
||||
* @return
|
||||
*/
|
||||
public static JSONObject toJSONObject(String key, Object value) {
|
||||
return toJSONObject(new String[]{key}, new Object[]{value});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param keys
|
||||
* @param values
|
||||
* @return
|
||||
*/
|
||||
public static JSONObject toJSONObject(String[] keys, Object[] values) {
|
||||
Assert.isTrue(keys.length <= values.length, "K/V 长度不匹配");
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
map.put(keys[i], values[i]);
|
||||
}
|
||||
return (JSONObject) JSON.toJSON(map);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param keys
|
||||
* @param valuesArray
|
||||
* @return
|
||||
*/
|
||||
public static JSONArray toJSONArray(String[] keys, Object[][] valuesArray) {
|
||||
List<Map<String, Object>> array = new ArrayList<>();
|
||||
for (Object[] o : valuesArray) {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
map.put(keys[i], o[i]);
|
||||
}
|
||||
array.add(map);
|
||||
}
|
||||
return (JSONArray) JSON.toJSON(array);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param items
|
||||
* @return
|
||||
*/
|
||||
public static JSONArray toJSONArray(JSONable[] items) {
|
||||
if (items == null || items.length == 0) {
|
||||
return EMPTY_ARRAY;
|
||||
}
|
||||
|
||||
JSONArray array = new JSONArray();
|
||||
for (JSONable e : items) {
|
||||
array.add(e.toJSON());
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param json
|
||||
* @return
|
||||
*/
|
||||
public static JSON clone(JSON json) {
|
||||
String tostr = json.toJSONString();
|
||||
return (JSON) JSON.parse(tostr);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param json
|
||||
* @return
|
||||
*/
|
||||
public static String prettyPrint(Object json) {
|
||||
return JSON.toJSONString(json, true);
|
||||
}
|
||||
}
|
||||
|
|
114
src/main/java/com/rebuild/utils/LocationUtils.java
Normal file
114
src/main/java/com/rebuild/utils/LocationUtils.java
Normal file
|
@ -0,0 +1,114 @@
|
|||
/*
|
||||
rebuild - Building your business-systems freely.
|
||||
Copyright (C) 2019 devezhao <zhaofang123@gmail.com>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.rebuild.utils;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.rebuild.server.Application;
|
||||
import com.rebuild.server.helper.cache.CommonCache;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
/**
|
||||
* 位置工具
|
||||
*
|
||||
* @author devezhao
|
||||
* @since 2019/8/28
|
||||
*/
|
||||
public class LocationUtils {
|
||||
|
||||
private static final Log LOG = LogFactory.getLog(LocationUtils.class);
|
||||
|
||||
/**
|
||||
* 获取 IP 所在位置
|
||||
*
|
||||
* @param ip ip v4 or v6
|
||||
* @return
|
||||
*/
|
||||
public static JSON getLocation(String ip) {
|
||||
return getLocation(ip, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 IP 所在位置
|
||||
*
|
||||
* @param ip
|
||||
* @param useCache 7天缓存
|
||||
* @return
|
||||
*/
|
||||
public static JSON getLocation(String ip, boolean useCache) {
|
||||
JSONObject result = null;
|
||||
if (useCache) {
|
||||
result = (JSONObject) Application.getCommonCache().getx("IPLocation2" + ip);
|
||||
if (result != null) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
result = new JSONObject();
|
||||
result.put("ip", ip);
|
||||
|
||||
JSONObject loc = getJSON(String.format("https://ipapi.co/%s/json/", ip));
|
||||
if (loc != null) {
|
||||
if (loc.getString("country") != null) {
|
||||
result.put("country", loc.getString("country"));
|
||||
result.put("region", loc.getString("region"));
|
||||
result.put("city", loc.getString("city"));
|
||||
} else if (loc.getBooleanValue("reserved")) {
|
||||
result.put("country", "R");
|
||||
}
|
||||
}
|
||||
|
||||
if (result.getString("country") == null) {
|
||||
loc = getJSON(String.format("http://ip-api.com/json/%s", ip));
|
||||
LOG.warn("Use backup IP location service : " + loc);
|
||||
|
||||
if (loc != null) {
|
||||
String message = loc.getString("message");
|
||||
if (loc.getString("countryCode") != null) {
|
||||
result.put("country", loc.getString("countryCode"));
|
||||
result.put("region", loc.getString("regionName"));
|
||||
result.put("city", loc.getString("city"));
|
||||
} else if (message != null && (message.contains("private") || message.contains("reserved"))) {
|
||||
result.put("country", "R");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (result.getString("country") == null) {
|
||||
result.put("country", "N");
|
||||
}
|
||||
|
||||
Application.getCommonCache().putx("IPLocation2" + ip, result, CommonCache.TS_DAY * 7);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param url
|
||||
* @return
|
||||
*/
|
||||
private static JSONObject getJSON(String url) {
|
||||
try {
|
||||
return JSON.parseObject(CommonsUtils.get(url));
|
||||
} catch (Exception e) {
|
||||
LOG.debug("Error occured : " + url + " >> " + e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -18,19 +18,18 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|||
|
||||
package com.rebuild.web.admin.bizz;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import cn.devezhao.persist4j.engine.ID;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.rebuild.server.configuration.portals.DataListManager;
|
||||
import com.rebuild.utils.LocationUtils;
|
||||
import com.rebuild.web.BaseEntityControll;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.rebuild.server.configuration.portals.DataListManager;
|
||||
import com.rebuild.web.BaseEntityControll;
|
||||
|
||||
import cn.devezhao.persist4j.engine.ID;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author devezhao-mac zhaofang123@gmail.com
|
||||
|
@ -48,5 +47,11 @@ public class LoginLogControll extends BaseEntityControll {
|
|||
mv.getModel().put("DataListConfig", JSON.toJSONString(config));
|
||||
return mv;
|
||||
}
|
||||
|
||||
|
||||
@RequestMapping("ip-location")
|
||||
public void getIpLocation(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
||||
String ip = getParameterNotNull(request, "ip");
|
||||
JSON location = LocationUtils.getLocation(ip);
|
||||
writeSuccess(response, location);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -74,16 +74,12 @@ RbList.renderAfter = function() {
|
|||
$('.rb-datatable-body tbody>tr').each(function() {
|
||||
let ipAddr = $(this).find('td:eq(' + ipAddrIndex + ') div')
|
||||
let ip = (ipAddr.text() || '').split('(')[0].trim()
|
||||
if (ip.contains('0:0:0:0:0:0:0') || /^(127\.0\.0\.1)|(localhost)$/.test(ip)) {
|
||||
ipAddr.text(ip + ' (本机)')
|
||||
} else if (/^(10\.\d{1,3}\.\d{1,3}\.\d{1,3})|(172\.((1[6-9])|(2\d)|(3[01]))\.\d{1,3}\.\d{1,3})|(192\.168\.\d{1,3}\.\d{1,3})$/.test(ip)) {
|
||||
ipAddr.text(ip + ' (局域网)')
|
||||
} else {
|
||||
//$.getJSON('http://ip.taobao.com/service/getIpInfo.php?ip=' + ip + '&jsoncallback=?', function(res) {
|
||||
// console.log(res)
|
||||
//})
|
||||
//return false
|
||||
}
|
||||
$.get(rb.baseUrl + '/admin/bizuser/ip-location?ip=' + ip, (res) => {
|
||||
if (res.error_code === 0 && res.data.country !== 'N') {
|
||||
let L = res.data.country === 'R' ? '局域网' : [res.data.region, res.data.country].join(', ')
|
||||
ipAddr.text(ip + ' (' + L + ')')
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
</script>
|
||||
|
|
|
@ -64,14 +64,14 @@ public class CommonsUtilsTest {
|
|||
|
||||
@Test
|
||||
public void testGet() throws Exception {
|
||||
String ret = CommonsUtils.get("http://ip.taobao.com/service/getIpInfo.php?ip=8.8.8.8");
|
||||
System.out.println(ret);
|
||||
String ret = CommonsUtils.get("https://ipapi.co/58.39.87.252/json/");
|
||||
System.out.println(JSONUtils.prettyPrint(JSON.parse(ret)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPost() throws Exception {
|
||||
String ret = CommonsUtils.post("http://ip.taobao.com/service/getIpInfo.php?ip=8.8.8.8", null);
|
||||
System.out.println(ret);
|
||||
String ret = CommonsUtils.post("http://ip-api.com/json/58.39.87.252", null);
|
||||
System.out.println(JSONUtils.prettyPrint(JSON.parse(ret)));
|
||||
}
|
||||
|
||||
@Ignore
|
||||
|
|
42
src/test/java/com/rebuild/utils/LocationUtilsTest.java
Normal file
42
src/test/java/com/rebuild/utils/LocationUtilsTest.java
Normal file
|
@ -0,0 +1,42 @@
|
|||
/*
|
||||
rebuild - Building your business-systems freely.
|
||||
Copyright (C) 2018 devezhao <zhaofang123@gmail.com>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.rebuild.utils;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.rebuild.server.TestSupport;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author devezhao
|
||||
* @since 01/31/2019
|
||||
*/
|
||||
public class LocationUtilsTest extends TestSupport {
|
||||
|
||||
@Test
|
||||
public void getLocation() {
|
||||
JSON r = LocationUtils.getLocation("180.162.13.205", false);
|
||||
System.out.println(r);
|
||||
|
||||
r = LocationUtils.getLocation("192.168.0.110", false);
|
||||
System.out.println(r);
|
||||
|
||||
r = LocationUtils.getLocation("127.0.0.1", false);
|
||||
System.out.println(r);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue