From 1ef575d4866cb6bafb5782fba0bdf8ce9efc3a71 Mon Sep 17 00:00:00 2001 From: devezhao Date: Wed, 28 Aug 2019 17:43:51 +0800 Subject: [PATCH] ip location --- .../java/com/rebuild/utils/JSONUtils.java | 157 +++++++++--------- .../java/com/rebuild/utils/LocationUtils.java | 114 +++++++++++++ .../web/admin/bizz/LoginLogControll.java | 25 +-- src/main/webapp/admin/bizuser/login-logs.jsp | 16 +- .../com/rebuild/utils/CommonsUtilsTest.java | 8 +- .../com/rebuild/utils/LocationUtilsTest.java | 42 +++++ 6 files changed, 263 insertions(+), 99 deletions(-) create mode 100644 src/main/java/com/rebuild/utils/LocationUtils.java create mode 100644 src/test/java/com/rebuild/utils/LocationUtilsTest.java diff --git a/src/main/java/com/rebuild/utils/JSONUtils.java b/src/main/java/com/rebuild/utils/JSONUtils.java index 35d44630c..4457e7775 100644 --- a/src/main/java/com/rebuild/utils/JSONUtils.java +++ b/src/main/java/com/rebuild/utils/JSONUtils.java @@ -18,92 +18,99 @@ along with this program. If not, see . 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 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> array = new ArrayList<>(); - for (Object[] o : valuesArray) { - Map 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 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> array = new ArrayList<>(); + for (Object[] o : valuesArray) { + Map 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); + } } diff --git a/src/main/java/com/rebuild/utils/LocationUtils.java b/src/main/java/com/rebuild/utils/LocationUtils.java new file mode 100644 index 000000000..be0bc0587 --- /dev/null +++ b/src/main/java/com/rebuild/utils/LocationUtils.java @@ -0,0 +1,114 @@ +/* +rebuild - Building your business-systems freely. +Copyright (C) 2019 devezhao + +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 . +*/ + +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; + } +} diff --git a/src/main/java/com/rebuild/web/admin/bizz/LoginLogControll.java b/src/main/java/com/rebuild/web/admin/bizz/LoginLogControll.java index a5ac62d6c..14b30fe1a 100644 --- a/src/main/java/com/rebuild/web/admin/bizz/LoginLogControll.java +++ b/src/main/java/com/rebuild/web/admin/bizz/LoginLogControll.java @@ -18,19 +18,18 @@ along with this program. If not, see . 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); + } } diff --git a/src/main/webapp/admin/bizuser/login-logs.jsp b/src/main/webapp/admin/bizuser/login-logs.jsp index 39a5d81bd..2703b5663 100644 --- a/src/main/webapp/admin/bizuser/login-logs.jsp +++ b/src/main/webapp/admin/bizuser/login-logs.jsp @@ -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 + ')') + } + }) }) } diff --git a/src/test/java/com/rebuild/utils/CommonsUtilsTest.java b/src/test/java/com/rebuild/utils/CommonsUtilsTest.java index 6756daa31..01eb441c8 100644 --- a/src/test/java/com/rebuild/utils/CommonsUtilsTest.java +++ b/src/test/java/com/rebuild/utils/CommonsUtilsTest.java @@ -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 diff --git a/src/test/java/com/rebuild/utils/LocationUtilsTest.java b/src/test/java/com/rebuild/utils/LocationUtilsTest.java new file mode 100644 index 000000000..44c9b335e --- /dev/null +++ b/src/test/java/com/rebuild/utils/LocationUtilsTest.java @@ -0,0 +1,42 @@ +/* +rebuild - Building your business-systems freely. +Copyright (C) 2018 devezhao + +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 . +*/ + +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); + } +} \ No newline at end of file