diff options
| -rw-r--r-- | config/application.yml | 41 | ||||
| -rw-r--r-- | src/main/java/com/geedge/common/constant/TsgObject.java | 52 | ||||
| -rw-r--r-- | src/main/java/com/geedge/common/util/TsgUtil.java | 90 | ||||
| -rw-r--r-- | src/main/java/com/geedge/scheduler/FqdnScheduler.java | 226 | ||||
| -rw-r--r-- | src/main/java/com/geedge/scheduler/IpScheduler.java | 310 |
5 files changed, 213 insertions, 506 deletions
diff --git a/config/application.yml b/config/application.yml index c26b19c..a5947bf 100644 --- a/config/application.yml +++ b/config/application.yml @@ -12,15 +12,14 @@ logging: config: ./config/logback-spring.xml tsg: system: - url: http://192.168.44.29:8080 - token: 1ca0350bc499a208a0edadb74d76a0ee&1& + url: http://192.168.44.72:8080 + token: aa2bdec5518ad131f71944b13ce5c298&1& httpTimeout: 3600000 - isLatestVersion: true object: ip: excludeValue: 0.0.0.0,255.255.255.255,127.*,8.8.8.8,1.1.1.1 hotspotvpn_serverip: - id: + uuid: name: hotspotvpn_serverip update: cron: 0 0 0/1 * * ? @@ -31,7 +30,7 @@ tsg: enable: false offsetSecond: -10800 ipvanishvpn_serverip: - id: + uuid: name: ipvanishvpn_serverip update: cron: 0 0 0/1 * * ? @@ -42,7 +41,7 @@ tsg: enable: false offsetSecond: -10800 psiphon3vpn_serverip: - id: + uuid: name: psiphon3vpn_serverip update: cron: 0 0 0/1 * * ? @@ -53,7 +52,7 @@ tsg: enable: false offsetSecond: -10800 cyberghostvpn_serverip: - id: + uuid: name: cyberghostvpn_serverip update: cron: 0 0 1 * * ? @@ -64,7 +63,7 @@ tsg: enable: false offsetSecond: -259200 geckovpn_serverip: - id: + uuid: name: geckovpn_serverip update: cron: 0 0 0/1 * * ? @@ -75,7 +74,7 @@ tsg: enable: false offsetSecond: -10800 ivacyvpn_serverip: - id: + uuid: name: ivacyvpn_serverip update: cron: 0 0 1 * * ? @@ -86,7 +85,7 @@ tsg: enable: false offsetSecond: -259200 turbovpn_serverip: - id: + uuid: name: turbovpn_serverip update: cron: 0 0 0/1 * * ? @@ -97,7 +96,7 @@ tsg: enable: false offsetSecond: -10800 vpnunlimited_serverip: - id: + uuid: name: vpnunlimited_serverip update: cron: 0 0 0/1 * * ? @@ -108,7 +107,7 @@ tsg: enable: false offsetSecond: -10800 windscribevpn_serverip: - id: + uuid: name: windscribevpn_serverip update: cron: 0 0 0/1 * * ? @@ -119,7 +118,7 @@ tsg: enable: false offsetSecond: -10800 vpn4fame_serverip: - id: + uuid: name: vpn4fame_serverip update: cron: 0 0 0/1 * * ? @@ -130,7 +129,7 @@ tsg: enable: false offsetSecond: -600 protonvpn_ip: - id: + uuid: name: protonvpn_serverip update: cron: 0 0 0/1 * * ? @@ -141,7 +140,7 @@ tsg: enable: false offsetSecond: -10800 expressvpn_ip: - id: + uuid: name: expressvpn_ip update: cron: 0 0 0/1 * * ? @@ -153,7 +152,7 @@ tsg: offsetSecond: -600 fqdn: cyberghostvpn_servername: - id: + uuid: name: cyberghostvpn_servername update: cron: 0 0 1 * * ? @@ -164,7 +163,7 @@ tsg: enable: false offsetSecond: -259200 ipvanishvpn_servername: - id: + uuid: name: ipvanishvpn_servername update: cron: 0 0 0/1 * * ? @@ -175,7 +174,7 @@ tsg: enable: false offsetSecond: -10800 ivacyvpn_servername: - id: + uuid: name: ivacyvpn_servername update: cron: 0 0 1 * * ? @@ -186,7 +185,7 @@ tsg: enable: false offsetSecond: -259200 windscribevpn_servername: - id: + uuid: name: windscribevpn_servername update: cron: 0 0 0/1 * * ? @@ -197,7 +196,7 @@ tsg: enable: false offsetSecond: -10800 vpnunlimited_servername: - id: + uuid: name: vpnunlimited_servername update: cron: 0 0 0/1 * * ? @@ -208,7 +207,7 @@ tsg: enable: false offsetSecond: -600 vpn4fame_servername: - id: + uuid: name: vpn4fame_servername update: cron: 0 0 0/1 * * ? diff --git a/src/main/java/com/geedge/common/constant/TsgObject.java b/src/main/java/com/geedge/common/constant/TsgObject.java index a672d29..2cfe447 100644 --- a/src/main/java/com/geedge/common/constant/TsgObject.java +++ b/src/main/java/com/geedge/common/constant/TsgObject.java @@ -8,57 +8,23 @@ package com.geedge.common.constant; * @Author wWei */ public class TsgObject { - public static final String KEY_VSYS_ID = "vsys_id"; + public static final String KEY_VSYS = "vsys"; public static final String KEY_OBJECT = "object"; public static final String KEY_CREATED_BEFORE = "created_before"; public static final String KEY_OBJECT_NAME = "name"; - public static final String KEY_OBJECT_TYPE = "type"; - public static final String KEY_OBJECT_MEMBER = "member"; - public static final String KEY_OBJECT_MEMBER_TYPE = "type"; public static final String KEY_OBJECT_MEMBER_ITEMS = "items"; - public static final String KEY_OBJECT_MEMBER_ITEMS_OP = "op"; public static final String KEY_OBJECT_MEMBER_ITEMS_IP = "ip"; - public static final String KEY_OBJECT_MEMBER_ITEMS_STRING = "string"; - public static final String KEY_OBJECT_MEMBER_ITEMS_STRING_PATTERNS = "patterns"; - public static final String KEY_OBJECT_MEMBER_ITEMS_STRING_EXPR_TYPE = "expr_type"; - public static final String KEY_OBJECT_MEMBER_ITEMS_STRING_IS_HEXBIN = "is_hexbin"; - public static final String KEY_OBJECT_MEMBER_ITEMS_IP_IP_ADDRESS = "ip_address"; - public static final String KEY_OBJECT_MEMBER_ITEMS_IP_PORT_RANGE = "port_range"; - public static final String KEY_OBJECT_MEMBER_ITEMS_STRING_PATTERNS_KEYWORDS = "keywords"; - public static final String KEY_OBJECT_MEMBER_ITEMS_IP_ADDR_TYPE = "addr_type"; - public static final String KEY_OBJECT_MEMBER_ITEMS_IP_IP_RANGE = "ip_range"; - public static final String KEY_OBJECT_MEMBER_ITEMS_IP_IP_CIDR = "ip_cidr"; + public static final String KEY_OBJECT_MEMBER_ITEMS_EXPRESSION = "expression"; - public static final String VALUE_OBJECT_TYPE_IP = "ip"; - public static final String VALUE_OBJECT_TYPE_FQDN = "fqdn"; - public static final String VALUE_OBJECT_MEMBER_ITEMS_IP_PORT_RANGE = "0-65535"; + public static final String KEY_OBJECT_MEMBER_ITEMS_OP = "op"; public static final String VALUE_OBJECT_MEMBER_ITEMS_OP_ADD = "add"; - public static final Integer VALUE_OBJECT_MEMBER_TYPE_1 = 1; - public static final Integer VALUE_VSYS_ID_1 = 1; - public static final Integer VALUE_OBJECT_MEMBER_ITEMS_STRING_EXPR_TYPE_0 = 0; - public static final Integer VALUE_OBJECT_MEMBER_ITEMS_STRING_IS_HEXBIN_0 = 0; - public static String TSG_OBJECT_ID_V2310 = "objectId"; - public static String TSG_OBJECT_TYPE_V2310 = "objectType"; - public static String TSG_IP_ADDR_OBJECT_V2310 = "ip"; - public static String TSG_FQDN_OBJECT_V2310 = "fqdn"; - public static String TSG_OBJECT_NAME_V2310 = "objectName"; - public static String TSG_IS_BUILTIN_V2310 = "isBuiltin"; - public static String TSG_IS_EXCLUSION_V2310 = "isExclusion"; - public static String TSG_IS_VALID_V2310 = "isValid"; - public static String TSG_ITEM_IP_V2310 = "ip"; - public static String TSG_ITEM_PORT_V2310 = "port"; - public static String TSG_VALUE_OBJECT_MEMBER_ITEMS_IP_PORT_RANGE_V2310 = "0-65535"; - public static String TSG_ITEM_ISSESSION_V2310 = "isSession"; - public static String TSG_ITEM_ENDPOINT_V2310 = "endpoint"; - public static String TSG_OP_ACTION_V2310 = "opAction"; - public static String TSG_UPDATE_V2310 = "update"; - public static String TSG_OBJECT_LIST_V2310 = "objectList"; - public static String TSG_VSYS_ID_V2310 = "vsysId"; - public static String TSG_TSG_OBJECT_IDS_V2310 = "objectIds"; - public static String TSG_ITEM_TYPE_V2310 = "itemType"; - public static String TSG_KEYWORDARRAY_V2310 = "keywordArray"; - public static String TSG_DELETE_ITEMS_BYL_TTIME_V2310 = "deleteItemsByLtTime"; + public static final String KEY_OBJECT_MEMBER_ITEMS_EXPR_TYPE = "expr_type"; + public static final String VALUE_OBJECT_MEMBER_ITEMS_EXPR_TYPE = "and"; + + public static final String VALUE_OBJECT_TYPE_IP = "ip-addresses"; + public static final String VALUE_OBJECT_TYPE_FQDN = "fqdns"; + public static final Integer VALUE_VSYS_1 = 1; } diff --git a/src/main/java/com/geedge/common/util/TsgUtil.java b/src/main/java/com/geedge/common/util/TsgUtil.java index 9bf9134..b8eb133 100644 --- a/src/main/java/com/geedge/common/util/TsgUtil.java +++ b/src/main/java/com/geedge/common/util/TsgUtil.java @@ -5,6 +5,7 @@ import cn.hutool.http.Header; import cn.hutool.http.HttpRequest; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; +import com.geedge.common.constant.TsgObject; import com.google.common.base.Stopwatch; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; @@ -27,12 +28,8 @@ public class TsgUtil { public static String TSG_URL; private static String TSG_TOKEN; private static Integer httpTimeOut; - public static Boolean isLatestVersion; - @Value("${tsg.system.isLatestVersion}") - public void isLatestVersion(Boolean latestVersion) { - isLatestVersion = latestVersion; - } + private static final String URL_PREFIX = "/v1/objects/"; @Value("${tsg.system.httpTimeout}") public void setTsgUrl(Integer timeOut) { @@ -57,98 +54,51 @@ public class TsgUtil { throw new IllegalArgumentException("failed to get TSG system token."); } - public static JSONObject getObjectItemList(Integer objectId, String objectType) { + public static JSONObject getObjectItemList(String uuid, String objectType) { Stopwatch watch = Stopwatch.createStarted(); - String response = HttpRequest.get(TSG_URL + "/v1/policy/object/" + objectId + "/item?page_no=1&page_size=1&type=" + objectType) - .header(Header.AUTHORIZATION, getToken()) - .timeout(httpTimeOut) - .execute().body(); + String response = HttpRequest.get(TSG_URL + URL_PREFIX + objectType + "/" + uuid + "/items?page_no=1&page_size=1&" + TsgObject.KEY_VSYS + "=" + TsgObject.VALUE_VSYS_1).header(Header.AUTHORIZATION, getToken()).timeout(httpTimeOut).execute().body(); log.info("get tsg-api, cost {} seconds", watch.elapsed(TimeUnit.SECONDS)); if (StrUtil.isBlank(response)) { - log.error("get {} Object error, response: {}", objectId, response); - throw new RuntimeException("get " + objectId + " Object error, response: " + response); + log.error("get {} Object error, response: {}", uuid, response); + throw new RuntimeException("get " + uuid + " Object error, response: " + response); } JSONObject jsonObject = JSONUtil.parseObj(response); if (!"200".equals(jsonObject.get("code").toString())) { - log.error("get {} Object error, response: {}", objectId, response); - throw new RuntimeException("get " + objectId + " Object error, response: " + response); + log.error("get {} Object error, response: {}", uuid, response); + throw new RuntimeException("get " + uuid + " Object error, response: " + response); } return jsonObject; } - public static void updateObjectById(Integer id, Map<String, Object> body) { + public static void updateObjectByUUID(String type, String uuid, Map<String, Object> body) { Stopwatch watch = Stopwatch.createStarted(); - String response = HttpRequest.put(TSG_URL + "/v1/policy/object/" + id) - .header(Header.AUTHORIZATION, getToken()) - .body(JSONUtil.toJsonStr(body)) - .timeout(httpTimeOut) - .execute().body(); + String response = HttpRequest.put(TSG_URL + URL_PREFIX + type + "/" + uuid).header(Header.AUTHORIZATION, getToken()).body(JSONUtil.toJsonStr(body)).timeout(httpTimeOut).execute().body(); log.info("update tsg-api, cost {} seconds", watch.elapsed(TimeUnit.SECONDS)); if (StrUtil.isBlank(response)) { - log.error("update {} Object error, response: {}", id, response); - throw new RuntimeException("update " + id + " Object error, response: " + response); + log.error("update {} Object error, response: {}", uuid, response); + throw new RuntimeException("update " + uuid + " Object error, response: " + response); } JSONObject jsonObject = JSONUtil.parseObj(response); if (!"200".equals(jsonObject.get("code").toString())) { - log.error("update {} Object error, response: {}", id, response); - throw new RuntimeException("update " + id + " Object error, response: " + response); + log.error("update {} Object error, response: {}", uuid, response); + throw new RuntimeException("update " + uuid + " Object error, response: " + response); } } - public static void updateObjectOld(Map<String, Object> body) { - Stopwatch watch = Stopwatch.createStarted(); - String response = HttpRequest.put(TSG_URL + "/v1/policy/object") - .header(Header.AUTHORIZATION, getToken()) - .body(JSONUtil.toJsonStr(body)) - .timeout(httpTimeOut) - .execute().body(); - log.info("update tsg-api, cost {} seconds", watch.elapsed(TimeUnit.SECONDS)); - if (StrUtil.isBlank(response)) { - log.error("update {} Object error, response: {}", body, response); - throw new RuntimeException("update " + body + " Object error, response: " + response); - } - JSONObject jsonObject = JSONUtil.parseObj(response); - if (!"200".equals(jsonObject.get("code").toString())) { - log.error("update {} Object error, response: {}", body, response); - throw new RuntimeException("update " + body + " Object error, response: " + response); - } - } - public static void deleteItemOfObjectById(Integer id, Map<String, Object> form) { + public static void deleteItemOfObjectByUUID(String type, String uuid, Map<String, Object> form) { Stopwatch watch = Stopwatch.createStarted(); - String response = HttpRequest.delete(TSG_URL + "/v1/policy/object/" + id + "/item") - .header(Header.AUTHORIZATION, getToken()) - .form(form) - .timeout(httpTimeOut) - .execute().body(); + String response = HttpRequest.delete(TSG_URL + URL_PREFIX + type + "/" + uuid + "/items").header(Header.AUTHORIZATION, getToken()).form(form).timeout(httpTimeOut).execute().body(); log.info("delete tsg-api, cost {} seconds", watch.elapsed(TimeUnit.SECONDS)); if (StrUtil.isBlank(response)) { - log.error("update {} Object error, response: {}", id, response); - throw new RuntimeException("update " + id + " Object error, response: " + response); + log.error("update {} Object error, response: {}", uuid, response); + throw new RuntimeException("update " + uuid + " Object error, response: " + response); } JSONObject jsonObject = JSONUtil.parseObj(response); if (!"200".equals(jsonObject.get("code").toString())) { - log.error("update {} Object error, response: {}", id, response); - throw new RuntimeException("update " + id + " Object error, response: " + response); + log.error("update {} Object error, response: {}", uuid, response); + throw new RuntimeException("update " + uuid + " Object error, response: " + response); } } - public static void deleteItemOfObjectOld(Map<String, Object> body) { - Stopwatch watch = Stopwatch.createStarted(); - String response = HttpRequest.delete(TSG_URL + "/v1/policy/items") - .header(Header.AUTHORIZATION, getToken()) - .body(JSONUtil.toJsonStr(body)) - .timeout(httpTimeOut) - .execute().body(); - log.info("delete tsg-api, cost {} seconds", watch.elapsed(TimeUnit.SECONDS)); - if (StrUtil.isBlank(response)) { - log.error("update {} Object error, response: {}", body, response); - throw new RuntimeException("update " + body + " Object error, response: " + response); - } - JSONObject jsonObject = JSONUtil.parseObj(response); - if (!"200".equals(jsonObject.get("code").toString())) { - log.error("update {} Object error, response: {}", body, response); - throw new RuntimeException("update " + body + " Object error, response: " + response); - } - } } diff --git a/src/main/java/com/geedge/scheduler/FqdnScheduler.java b/src/main/java/com/geedge/scheduler/FqdnScheduler.java index c076963..386aff3 100644 --- a/src/main/java/com/geedge/scheduler/FqdnScheduler.java +++ b/src/main/java/com/geedge/scheduler/FqdnScheduler.java @@ -5,7 +5,6 @@ import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.BooleanUtil; import cn.hutool.core.util.StrUtil; -import cn.hutool.json.JSONArray; import cn.hutool.json.JSONObject; import com.geedge.common.constant.TsgObject; import com.geedge.common.util.TsgUtil; @@ -35,8 +34,8 @@ import java.util.concurrent.TimeUnit; @Component public class FqdnScheduler { - @Value("${tsg.object.fqdn.cyberghostvpn_servername.id}") - private Integer cyberghostvpnServernameId; + @Value("${tsg.object.fqdn.cyberghostvpn_servername.uuid}") + private String cyberghostvpnServernameUUID; @Value("${tsg.object.fqdn.cyberghostvpn_servername.name}") private String cyberghostvpnServernameName; @Value("${tsg.object.fqdn.cyberghostvpn_servername.update.enable}") @@ -51,16 +50,16 @@ public class FqdnScheduler { @Scheduled(cron = "${tsg.object.fqdn.cyberghostvpn_servername.update.cron}") public void updateCyberghostvpn() { - executeUpdate(cyberghostvpnServernameId, cyberghostvpnServernameName, cyberghostvpnServernameUpdateEnable, cyberghostvpnServernameSql, cyberghostvpnServernameCounter); + executeUpdate(cyberghostvpnServernameUUID, cyberghostvpnServernameName, cyberghostvpnServernameUpdateEnable, cyberghostvpnServernameSql, cyberghostvpnServernameCounter); } @Scheduled(cron = "${tsg.object.fqdn.cyberghostvpn_servername.delete.cron}") public void deleteCyberghostvpn() { - executeDelete(cyberghostvpnServernameId, cyberghostvpnServernameDeleteEnable, cyberghostvpnServernameOffsetSecond); + executeDelete(cyberghostvpnServernameUUID, cyberghostvpnServernameDeleteEnable, cyberghostvpnServernameOffsetSecond); } - @Value("${tsg.object.fqdn.ipvanishvpn_servername.id}") - private Integer ipvanishvpnServernameId; + @Value("${tsg.object.fqdn.ipvanishvpn_servername.uuid}") + private String ipvanishvpnServernameUUID; @Value("${tsg.object.fqdn.ipvanishvpn_servername.name}") private String ipvanishvpnServernameName; @Value("${tsg.object.fqdn.ipvanishvpn_servername.update.enable}") @@ -75,17 +74,17 @@ public class FqdnScheduler { @Scheduled(cron = "${tsg.object.fqdn.ipvanishvpn_servername.update.cron}") public void updateIpvanishvpn() { - executeUpdate(ipvanishvpnServernameId, ipvanishvpnServernameName, ipvanishvpnServernameUpdateEnable, ipvanishvpnServernameSql, ipvanishvpnServernameCounter); + executeUpdate(ipvanishvpnServernameUUID, ipvanishvpnServernameName, ipvanishvpnServernameUpdateEnable, ipvanishvpnServernameSql, ipvanishvpnServernameCounter); } @Scheduled(cron = "${tsg.object.fqdn.ipvanishvpn_servername.delete.cron}") public void deleteIpvanishvpn() { - executeDelete(ipvanishvpnServernameId, ipvanishvpnServernameDeleteEnable, ipvanishvpnServernameOffsetSecond); + executeDelete(ipvanishvpnServernameUUID, ipvanishvpnServernameDeleteEnable, ipvanishvpnServernameOffsetSecond); } - @Value("${tsg.object.fqdn.ivacyvpn_servername.id}") - private Integer ivacyvpnServernameId; + @Value("${tsg.object.fqdn.ivacyvpn_servername.uuid}") + private String ivacyvpnServernameUUID; @Value("${tsg.object.fqdn.ivacyvpn_servername.name}") private String ivacyvpnServernameName; @Value("${tsg.object.fqdn.ivacyvpn_servername.update.enable}") @@ -100,16 +99,16 @@ public class FqdnScheduler { @Scheduled(cron = "${tsg.object.fqdn.ivacyvpn_servername.update.cron}") public void updateIvacyvpn() { - executeUpdate(ivacyvpnServernameId, ivacyvpnServernameName, ivacyvpnServernameUpdateEnable, ivacyvpnServernameSql, ivacyvpnServernameCounter); + executeUpdate(ivacyvpnServernameUUID, ivacyvpnServernameName, ivacyvpnServernameUpdateEnable, ivacyvpnServernameSql, ivacyvpnServernameCounter); } @Scheduled(cron = "${tsg.object.fqdn.ivacyvpn_servername.delete.cron}") public void deleteIvacyvpn() { - executeDelete(ivacyvpnServernameId, ivacyvpnServernameDeleteEnable, ivacyvpnServernameOffsetSecond); + executeDelete(ivacyvpnServernameUUID, ivacyvpnServernameDeleteEnable, ivacyvpnServernameOffsetSecond); } - @Value("${tsg.object.fqdn.vpnunlimited_servername.id}") - private Integer vpnunlimitedServernameId; + @Value("${tsg.object.fqdn.vpnunlimited_servername.uuid}") + private String vpnunlimitedServernameUUID; @Value("${tsg.object.fqdn.vpnunlimited_servername.name}") private String vpnunlimitedServernameName; @Value("${tsg.object.fqdn.vpnunlimited_servername.update.enable}") @@ -124,16 +123,16 @@ public class FqdnScheduler { @Scheduled(cron = "${tsg.object.fqdn.vpnunlimited_servername.update.cron}") public void updateVpnunlimited() { - executeUpdate(vpnunlimitedServernameId, vpnunlimitedServernameName, vpnunlimitedServernameUpdateEnable, vpnunlimitedServernameSql, vpnunlimitedServernameCounter); + executeUpdate(vpnunlimitedServernameUUID, vpnunlimitedServernameName, vpnunlimitedServernameUpdateEnable, vpnunlimitedServernameSql, vpnunlimitedServernameCounter); } @Scheduled(cron = "${tsg.object.fqdn.vpnunlimited_servername.delete.cron}") public void deleteVpnunlimited() { - executeDelete(vpnunlimitedServernameId, vpnunlimitedServernameDeleteEnable, vpnunlimitedServernameOffsetSecond); + executeDelete(vpnunlimitedServernameUUID, vpnunlimitedServernameDeleteEnable, vpnunlimitedServernameOffsetSecond); } - @Value("${tsg.object.fqdn.vpn4fame_servername.id}") - private Integer vpn4fameServernameId; + @Value("${tsg.object.fqdn.vpn4fame_servername.uuid}") + private String vpn4fameServernameUUID; @Value("${tsg.object.fqdn.vpn4fame_servername.name}") private String vpn4fameServernameName; @Value("${tsg.object.fqdn.vpn4fame_servername.update.enable}") @@ -148,16 +147,16 @@ public class FqdnScheduler { @Scheduled(cron = "${tsg.object.fqdn.vpn4fame_servername.update.cron}") public void updateVpn4fame() { - executeUpdate(vpn4fameServernameId, vpn4fameServernameName, vpn4fameServernameUpdateEnable, vpn4fameServernameSql, vpn4fameServernameCounter); + executeUpdate(vpn4fameServernameUUID, vpn4fameServernameName, vpn4fameServernameUpdateEnable, vpn4fameServernameSql, vpn4fameServernameCounter); } @Scheduled(cron = "${tsg.object.fqdn.vpn4fame_servername.delete.cron}") public void deleteVpn4fame() { - executeDelete(vpn4fameServernameId, vpn4fameServernameDeleteEnable, vpn4fameServernameOffsetSecond); + executeDelete(vpn4fameServernameUUID, vpn4fameServernameDeleteEnable, vpn4fameServernameOffsetSecond); } - @Value("${tsg.object.fqdn.windscribevpn_servername.id}") - private Integer windscribevpnServernameId; + @Value("${tsg.object.fqdn.windscribevpn_servername.uuid}") + private String windscribevpnServernameUUID; @Value("${tsg.object.fqdn.windscribevpn_servername.name}") private String windscribevpnServernameName; @Value("${tsg.object.fqdn.windscribevpn_servername.update.enable}") @@ -172,155 +171,93 @@ public class FqdnScheduler { @Scheduled(cron = "${tsg.object.fqdn.windscribevpn_servername.update.cron}") public void updateWindscribevpn() { - executeUpdate(windscribevpnServernameId, windscribevpnServernameName, windscribevpnServernameUpdateEnable, windscribevpnServernameSql, windscribevpnServernameCounter); + executeUpdate(windscribevpnServernameUUID, windscribevpnServernameName, windscribevpnServernameUpdateEnable, windscribevpnServernameSql, windscribevpnServernameCounter); } @Scheduled(cron = "${tsg.object.fqdn.windscribevpn_servername.delete.cron}") public void deleteWindscribevpn() { - executeDelete(windscribevpnServernameId, windscribevpnServernameDeleteEnable, windscribevpnServernameOffsetSecond); + executeDelete(windscribevpnServernameUUID, windscribevpnServernameDeleteEnable, windscribevpnServernameOffsetSecond); } - private static void executeUpdate(Integer id, String name, Boolean enable, String sql, Counter counter) { - if (TsgUtil.isLatestVersion) { - executeUpdateLatest(id, name, enable, sql, counter); - } else { - executeUpdateOld(id, name, enable, sql, counter); - } - } - - private static void executeDelete(Integer id, Boolean enable, Integer offsetSecond) { - if (TsgUtil.isLatestVersion) { - executeDeleteLatest(id, enable, offsetSecond); - } else { - executeDeleteOld(id, enable, offsetSecond); - } - } - - private static void executeUpdateLatest(Integer id, String name, Boolean enable, String sql, Counter counter) { - log.info("{}: started update task.", id); + private static void executeUpdate(String uuid, String name, Boolean enable, String sql, Counter counter) { + log.info("{}: started update task.", uuid); if (BooleanUtil.isFalse(enable)) { - log.warn("{}: interrupted update task. enable: {}", id, enable); + log.warn("{}: interrupted update task. enable: {}", uuid, enable); return; } try { Stopwatch watch = Stopwatch.createStarted(); List<Record> data = Db.find(sql); - log.info("{}: query knowledge base content, cost {} seconds", id, watch.elapsed(TimeUnit.SECONDS)); + log.info("{}: query knowledge base content, cost {} seconds", uuid, watch.elapsed(TimeUnit.SECONDS)); watch.reset().start(); List<Map<String, Object>> items = Lists.newArrayList(); for (Record record : data) { - Map<String, Object> item = Maps.newHashMap(); String domain = record.get("domain"); - - List<Map<String, Object>> patterns = Lists.newArrayList(); - Map<String, Object> pattern = Maps.newHashMap(); - pattern.put(TsgObject.KEY_OBJECT_MEMBER_ITEMS_STRING_PATTERNS_KEYWORDS, domain); - patterns.add(pattern); - - Map<String, Object> str = Maps.newHashMap(); - str.put(TsgObject.KEY_OBJECT_MEMBER_ITEMS_STRING_PATTERNS, patterns); - str.put(TsgObject.KEY_OBJECT_MEMBER_ITEMS_STRING_EXPR_TYPE, TsgObject.VALUE_OBJECT_MEMBER_ITEMS_STRING_EXPR_TYPE_0); - str.put(TsgObject.KEY_OBJECT_MEMBER_ITEMS_STRING_IS_HEXBIN, TsgObject.VALUE_OBJECT_MEMBER_ITEMS_STRING_IS_HEXBIN_0); - + if (StrUtil.isEmptyIfStr(domain)) { + continue; + } + Map<String, Object> item = Maps.newHashMap(); item.put(TsgObject.KEY_OBJECT_MEMBER_ITEMS_OP, TsgObject.VALUE_OBJECT_MEMBER_ITEMS_OP_ADD); - item.put(TsgObject.KEY_OBJECT_MEMBER_ITEMS_STRING, str); - + item.put(TsgObject.KEY_OBJECT_MEMBER_ITEMS_EXPR_TYPE, TsgObject.VALUE_OBJECT_MEMBER_ITEMS_EXPR_TYPE); + if (domain.startsWith("$")) { + domain = domain.substring(1); + domain = "^" + domain + "$"; + } else if (domain.startsWith("*")) { + domain = domain.substring(1); + domain = domain + "$"; + } else if (domain.endsWith("*")) { + domain = domain.substring(0, domain.length() - 1); + domain = "^" + domain; + } else { + domain = "^" + domain + "$"; + } + item.put(TsgObject.KEY_OBJECT_MEMBER_ITEMS_EXPRESSION, domain); items.add(item); } + Map<String, Object> object = Maps.newHashMap(); + object.put(TsgObject.KEY_OBJECT_MEMBER_ITEMS, items); + object.put(TsgObject.KEY_OBJECT_NAME, name); + object.put("type", "fqdn"); + object.put("statistics_option", "none"); + object.put("member_type", "item"); - Map<String, Object> member = Maps.newHashMap(); - member.put(TsgObject.KEY_OBJECT_MEMBER_TYPE, TsgObject.VALUE_OBJECT_MEMBER_TYPE_1); - member.put(TsgObject.KEY_OBJECT_MEMBER_ITEMS, items); - - Map<String, Object> obj = Maps.newHashMap(); - obj.put(TsgObject.KEY_OBJECT_NAME, name); - obj.put(TsgObject.KEY_OBJECT_TYPE, TsgObject.VALUE_OBJECT_TYPE_FQDN); - obj.put(TsgObject.KEY_OBJECT_MEMBER, member); Map<String, Object> body = Maps.newHashMap(); - body.put(TsgObject.KEY_VSYS_ID, TsgObject.VALUE_VSYS_ID_1); - body.put(TsgObject.KEY_OBJECT, obj); - log.info("{}: build api params, items size: {}, cost {} seconds", id, items.size(), watch.elapsed(TimeUnit.SECONDS)); - if (items.isEmpty()) { - return; - } - TsgUtil.updateObjectById(id, body); - counter.inc(items.size()); - } catch (Exception e) { - log.error("{}: failed to execute update task. message: {}", id, e.getMessage()); - throw new RuntimeException(e); - } - } - - - private static void executeUpdateOld(Integer id, String name, Boolean enable, String sql, Counter counter) { - log.info("{}: started update task.", id); - if (BooleanUtil.isFalse(enable)) { - log.warn("{}: interrupted update task. enable: {}", id, enable); - return; - } - try { - Stopwatch watch = Stopwatch.createStarted(); - List<Record> data = Db.find(sql); - log.info("{}: query knowledge base content, cost {} seconds", id, watch.elapsed(TimeUnit.SECONDS)); - watch.reset().start(); - - List<Map<String, Object>> items = Lists.newArrayList(); - for (Record record : data) { - String domain = record.get("domain"); - List<String> keywordArray = new ArrayList<>(); - keywordArray.add(domain); - JSONObject obj = new JSONObject(); - obj.set("isHexbin", 0); - obj.set(TsgObject.TSG_IS_BUILTIN_V2310, 0); - obj.set(TsgObject.TSG_KEYWORDARRAY_V2310, keywordArray); - items.add(obj); - } + body.put(TsgObject.KEY_VSYS, TsgObject.VALUE_VSYS_1); + body.put(TsgObject.KEY_OBJECT, object); + body.put("op", "update"); - JSONArray array = new JSONArray(); - JSONObject jsonObject = new JSONObject(); - jsonObject.set(TsgObject.TSG_OBJECT_ID_V2310, id); - jsonObject.set(TsgObject.TSG_OBJECT_TYPE_V2310, TsgObject.TSG_FQDN_OBJECT_V2310); - jsonObject.set(TsgObject.TSG_OBJECT_NAME_V2310, name); - jsonObject.set(TsgObject.TSG_IS_BUILTIN_V2310, 0); - jsonObject.set(TsgObject.TSG_IS_EXCLUSION_V2310, 0); - jsonObject.set(TsgObject.TSG_IS_VALID_V2310, 1); - jsonObject.set("addItemList", items); - log.info("{}: build api params, items size: {}, cost {} seconds", id, items.size(), watch.elapsed(TimeUnit.SECONDS)); + log.info("{}: build api params, items size: {}, cost {} seconds", uuid, items.size(), watch.elapsed(TimeUnit.SECONDS)); if (items.isEmpty()) { return; } - array.add(jsonObject); - JSONObject body = new JSONObject(); - body.set(TsgObject.TSG_OP_ACTION_V2310, TsgObject.TSG_UPDATE_V2310); - body.set(TsgObject.TSG_OBJECT_LIST_V2310, array); - TsgUtil.updateObjectOld(body); + TsgUtil.updateObjectByUUID(TsgObject.VALUE_OBJECT_TYPE_FQDN, uuid, body); counter.inc(items.size()); } catch (Exception e) { - log.error("{}: failed to execute update task. message: {}", id, e.getMessage()); + log.error("{}: failed to execute update task. message: {}", uuid, e.getMessage()); throw new RuntimeException(e); } } - private static void executeDeleteLatest(Integer id, Boolean enable, Integer offsetSecond) { - log.info("{}: started delete task.", id); + private static void executeDelete(String uuid, Boolean enable, Integer offsetSecond) { + log.info("{}: started delete task.", uuid); if (BooleanUtil.isFalse(enable)) { - log.warn("{}: interrupted delete task. enable: {}", id, enable); + log.warn("{}: interrupted delete task. enable: {}", uuid, enable); return; } try { DateTime deleteCursor = DateUtil.offsetSecond(new Date(), offsetSecond).setTimeZone(TimeZone.getTimeZone("UTC")); String deleteCursorStr = DateUtil.format(deleteCursor, DatePattern.UTC_PATTERN); - JSONObject objectItemList = TsgUtil.getObjectItemList(id, "fqdn"); + JSONObject objectItemList = TsgUtil.getObjectItemList(uuid, TsgObject.VALUE_OBJECT_TYPE_FQDN); Object data = objectItemList.get("data"); if (StrUtil.isEmptyIfStr(data)) { log.warn("No item does not need to be deleted"); return; } Map<String, Object> dataMap = (Map<String, Object>) data; - Object itemsObj = dataMap.get("items"); + Object itemsObj = dataMap.get("list"); if (StrUtil.isEmptyIfStr(itemsObj)) { log.warn("No item does not need to be deleted"); return; @@ -335,13 +272,7 @@ public class FqdnScheduler { log.warn("No item does not need to be deleted"); return; } - Object itemObj = map.get("string"); - if (StrUtil.isEmptyIfStr(itemObj)) { - log.warn("No item does not need to be deleted"); - return; - } - Map<String, Object> itemMap = (Map<String, Object>) itemObj; - Object createdTimeObj = itemMap.get("created_time"); + Object createdTimeObj = map.get("created_time"); DateTime latestCreatedDateTime = DateUtil.parse(createdTimeObj.toString()); int compare = DateUtil.compare(deleteCursor, latestCreatedDateTime); if (compare > 0) { @@ -350,37 +281,14 @@ public class FqdnScheduler { } Map<String, Object> form = Maps.newHashMap(); - form.put(TsgObject.KEY_OBJECT_TYPE, TsgObject.VALUE_OBJECT_TYPE_FQDN); - form.put(TsgObject.KEY_VSYS_ID, TsgObject.VALUE_VSYS_ID_1); + form.put(TsgObject.KEY_VSYS, TsgObject.VALUE_VSYS_1); form.put(TsgObject.KEY_CREATED_BEFORE, deleteCursorStr); - TsgUtil.deleteItemOfObjectById(id, form); + TsgUtil.deleteItemOfObjectByUUID(TsgObject.VALUE_OBJECT_TYPE_FQDN, uuid, form); } catch (Exception e) { - log.error("{}: failed to execute delete task. message: {}", id, e.getMessage()); + log.error("{}: failed to execute delete task. message: {}", uuid, e.getMessage()); throw new RuntimeException(e); } } - private static void executeDeleteOld(Integer id, Boolean enable, Integer offsetSecond) { - log.info("{}: started delete task.", id); - if (BooleanUtil.isFalse(enable)) { - log.warn("{}: interrupted delete task. enable: {}", id, enable); - return; - } - try { - DateTime dateTime = DateUtil.offsetSecond(new Date(), offsetSecond).setTimeZone(TimeZone.getTimeZone("UTC")); - String datetimeZ = DateUtil.format(dateTime, DatePattern.UTC_PATTERN); - Map<String, Object> body = new HashMap<>(16); - body.put(TsgObject.TSG_DELETE_ITEMS_BYL_TTIME_V2310, datetimeZ); - body.put(TsgObject.TSG_ITEM_TYPE_V2310, TsgObject.TSG_FQDN_OBJECT_V2310); - body.put(TsgObject.TSG_VSYS_ID_V2310, 1); - List<Object> objectIds = new ArrayList<>(); - objectIds.add(id); - body.put(TsgObject.TSG_TSG_OBJECT_IDS_V2310, objectIds); - TsgUtil.deleteItemOfObjectOld(body); - } catch (Exception e) { - log.error("{}: failed to execute delete task. message: {}", id, e.getMessage()); - throw new RuntimeException(e); - } - } } diff --git a/src/main/java/com/geedge/scheduler/IpScheduler.java b/src/main/java/com/geedge/scheduler/IpScheduler.java index bc5c854..2db8b12 100644 --- a/src/main/java/com/geedge/scheduler/IpScheduler.java +++ b/src/main/java/com/geedge/scheduler/IpScheduler.java @@ -7,7 +7,6 @@ import cn.hutool.core.net.Ipv4Util; import cn.hutool.core.net.MaskBit; import cn.hutool.core.util.BooleanUtil; import cn.hutool.core.util.StrUtil; -import cn.hutool.json.JSONArray; import cn.hutool.json.JSONObject; import com.geedge.common.constant.TsgObject; import com.geedge.common.enums.AddressFormat; @@ -50,8 +49,8 @@ public class IpScheduler { excludeList = Arrays.asList(values.split(",")); } - @Value("${tsg.object.ip.cyberghostvpn_serverip.id}") - private Integer cyberghostvpnServeripId; + @Value("${tsg.object.ip.cyberghostvpn_serverip.uuid}") + private String cyberghostvpnServeripUUID; @Value("${tsg.object.ip.cyberghostvpn_serverip.name}") private String cyberghostvpnServeripName; @Value("${tsg.object.ip.cyberghostvpn_serverip.update.enable}") @@ -66,16 +65,16 @@ public class IpScheduler { @Scheduled(cron = "${tsg.object.ip.cyberghostvpn_serverip.update.cron}") public void updateCyberghostvpn() { - executeUpdate(cyberghostvpnServeripId, cyberghostvpnServeripName, cyberghostvpnServeripUpdateEnable, cyberghostvpnServeripSql, cyberghostvpnServeripCounter); + executeUpdate(cyberghostvpnServeripUUID, cyberghostvpnServeripName, cyberghostvpnServeripUpdateEnable, cyberghostvpnServeripSql, cyberghostvpnServeripCounter); } @Scheduled(cron = "${tsg.object.ip.cyberghostvpn_serverip.delete.cron}") public void deleteCyberghostvpn() { - executeDelete(cyberghostvpnServeripId, cyberghostvpnServeripDeleteEnable, cyberghostvpnServeripOffsetSecond); + executeDelete(cyberghostvpnServeripUUID, cyberghostvpnServeripDeleteEnable, cyberghostvpnServeripOffsetSecond); } - @Value("${tsg.object.ip.hotspotvpn_serverip.id}") - private Integer hotspotvpnServeripId; + @Value("${tsg.object.ip.hotspotvpn_serverip.uuid}") + private String hotspotvpnServeripUUID; @Value("${tsg.object.ip.hotspotvpn_serverip.name}") private String hotspotvpnServeripName; @Value("${tsg.object.ip.hotspotvpn_serverip.update.enable}") @@ -90,16 +89,16 @@ public class IpScheduler { @Scheduled(cron = "${tsg.object.ip.hotspotvpn_serverip.update.cron}") public void updateHotspotvpn() { - executeUpdate(hotspotvpnServeripId, hotspotvpnServeripName, hotspotvpnServeripUpdateEnable, hotspotvpnServeripSql, hotspotvpnServeripCounter); + executeUpdate(hotspotvpnServeripUUID, hotspotvpnServeripName, hotspotvpnServeripUpdateEnable, hotspotvpnServeripSql, hotspotvpnServeripCounter); } @Scheduled(cron = "${tsg.object.ip.hotspotvpn_serverip.delete.cron}") public void deleteHotspotvpn() { - executeDelete(hotspotvpnServeripId, hotspotvpnServeripDeleteEnable, hotspotvpnServeripOffsetSecond); + executeDelete(hotspotvpnServeripUUID, hotspotvpnServeripDeleteEnable, hotspotvpnServeripOffsetSecond); } - @Value("${tsg.object.ip.ipvanishvpn_serverip.id}") - private Integer ipvanishvpnServeripId; + @Value("${tsg.object.ip.ipvanishvpn_serverip.uuid}") + private String ipvanishvpnServeripUUID; @Value("${tsg.object.ip.ipvanishvpn_serverip.name}") private String ipvanishvpnServeripName; @Value("${tsg.object.ip.ipvanishvpn_serverip.update.enable}") @@ -114,16 +113,16 @@ public class IpScheduler { @Scheduled(cron = "${tsg.object.ip.ipvanishvpn_serverip.update.cron}") public void updateIpvanishvpn() { - executeUpdate(ipvanishvpnServeripId, ipvanishvpnServeripName, ipvanishvpnServeripUpdateEnable, ipvanishvpnServeripSql, ipvanishvpnServeripCounter); + executeUpdate(ipvanishvpnServeripUUID, ipvanishvpnServeripName, ipvanishvpnServeripUpdateEnable, ipvanishvpnServeripSql, ipvanishvpnServeripCounter); } @Scheduled(cron = "${tsg.object.ip.ipvanishvpn_serverip.delete.cron}") public void deleteIpvanishvpn() { - executeDelete(ipvanishvpnServeripId, ipvanishvpnServeripDeleteEnable, ipvanishvpnServeripOffsetSecond); + executeDelete(ipvanishvpnServeripUUID, ipvanishvpnServeripDeleteEnable, ipvanishvpnServeripOffsetSecond); } - @Value("${tsg.object.ip.geckovpn_serverip.id}") - private Integer geckovpnServeripId; + @Value("${tsg.object.ip.geckovpn_serverip.uuid}") + private String geckovpnServeripUUID; @Value("${tsg.object.ip.geckovpn_serverip.name}") private String geckovpnServeripName; @Value("${tsg.object.ip.geckovpn_serverip.update.enable}") @@ -138,16 +137,16 @@ public class IpScheduler { @Scheduled(cron = "${tsg.object.ip.geckovpn_serverip.update.cron}") public void updateGeckovpn() { - executeUpdate(geckovpnServeripId, geckovpnServeripName, geckovpnServeripUpdateEnable, geckovpnServeripSql, geckovpnServeripCounter); + executeUpdate(geckovpnServeripUUID, geckovpnServeripName, geckovpnServeripUpdateEnable, geckovpnServeripSql, geckovpnServeripCounter); } @Scheduled(cron = "${tsg.object.ip.geckovpn_serverip.delete.cron}") public void deleteGeckovpn() { - executeDelete(geckovpnServeripId, geckovpnServeripDeleteEnable, geckovpnServeripOffsetSecond); + executeDelete(geckovpnServeripUUID, geckovpnServeripDeleteEnable, geckovpnServeripOffsetSecond); } - @Value("${tsg.object.ip.ivacyvpn_serverip.id}") - private Integer ivacyvpnServeripId; + @Value("${tsg.object.ip.ivacyvpn_serverip.uuid}") + private String geckivacyvpnServeripUUID; @Value("${tsg.object.ip.ivacyvpn_serverip.name}") private String ivacyvpnServeripName; @Value("${tsg.object.ip.ivacyvpn_serverip.update.enable}") @@ -162,17 +161,17 @@ public class IpScheduler { @Scheduled(cron = "${tsg.object.ip.ivacyvpn_serverip.update.cron}") public void updateIvacyvpn() { - executeUpdate(ivacyvpnServeripId, ivacyvpnServeripName, ivacyvpnServeripUpdateEnable, ivacyvpnServeripSql, ivacyvpnServeripCounter); + executeUpdate(geckivacyvpnServeripUUID, ivacyvpnServeripName, ivacyvpnServeripUpdateEnable, ivacyvpnServeripSql, ivacyvpnServeripCounter); } @Scheduled(cron = "${tsg.object.ip.ivacyvpn_serverip.delete.cron}") public void deleteIvacyvpn() { - executeDelete(ivacyvpnServeripId, ivacyvpnServeripDeleteEnable, ivacyvpnServeripOffsetSecond); + executeDelete(geckivacyvpnServeripUUID, ivacyvpnServeripDeleteEnable, ivacyvpnServeripOffsetSecond); } - @Value("${tsg.object.ip.turbovpn_serverip.id}") - private Integer turbovpnServeripId; + @Value("${tsg.object.ip.turbovpn_serverip.uuid}") + private String turbovpnServeripUUID; @Value("${tsg.object.ip.turbovpn_serverip.name}") private String turbovpnServeripName; @Value("${tsg.object.ip.turbovpn_serverip.update.enable}") @@ -187,16 +186,16 @@ public class IpScheduler { @Scheduled(cron = "${tsg.object.ip.turbovpn_serverip.update.cron}") public void updateTurbovpn() { - executeUpdate(turbovpnServeripId, turbovpnServeripName, turbovpnServeripUpdateEnable, turbovpnServeripSql, turbovpnServeripCounter); + executeUpdate(turbovpnServeripUUID, turbovpnServeripName, turbovpnServeripUpdateEnable, turbovpnServeripSql, turbovpnServeripCounter); } @Scheduled(cron = "${tsg.object.ip.turbovpn_serverip.delete.cron}") public void deleteTurbovpn() { - executeDelete(turbovpnServeripId, turbovpnServeripDeleteEnable, turbovpnServeripOffsetSecond); + executeDelete(turbovpnServeripUUID, turbovpnServeripDeleteEnable, turbovpnServeripOffsetSecond); } - @Value("${tsg.object.ip.vpnunlimited_serverip.id}") - private Integer vpnunlimitedServeripId; + @Value("${tsg.object.ip.vpnunlimited_serverip.uuid}") + private String vpnunlimitedServeripUUID; @Value("${tsg.object.ip.vpnunlimited_serverip.name}") private String vpnunlimitedServeripName; @Value("${tsg.object.ip.vpnunlimited_serverip.update.enable}") @@ -211,16 +210,16 @@ public class IpScheduler { @Scheduled(cron = "${tsg.object.ip.vpnunlimited_serverip.update.cron}") public void updateVpnunlimited() { - executeUpdate(vpnunlimitedServeripId, vpnunlimitedServeripName, vpnunlimitedServeripUpdateEnable, vpnunlimitedServeripSql, vpnunlimitedServeripCounter); + executeUpdate(vpnunlimitedServeripUUID, vpnunlimitedServeripName, vpnunlimitedServeripUpdateEnable, vpnunlimitedServeripSql, vpnunlimitedServeripCounter); } @Scheduled(cron = "${tsg.object.ip.vpnunlimited_serverip.delete.cron}") public void deleteVpnunlimited() { - executeDelete(vpnunlimitedServeripId, vpnunlimitedServeripDeleteEnable, vpnunlimitedServeripOffsetSecond); + executeDelete(vpnunlimitedServeripUUID, vpnunlimitedServeripDeleteEnable, vpnunlimitedServeripOffsetSecond); } - @Value("${tsg.object.ip.windscribevpn_serverip.id}") - private Integer windscribevpnServeripId; + @Value("${tsg.object.ip.windscribevpn_serverip.uuid}") + private String windscribevpnServeripUUID; @Value("${tsg.object.ip.windscribevpn_serverip.name}") private String windscribevpnServeripName; @Value("${tsg.object.ip.windscribevpn_serverip.update.enable}") @@ -235,16 +234,16 @@ public class IpScheduler { @Scheduled(cron = "${tsg.object.ip.windscribevpn_serverip.update.cron}") public void updateWindscribevpn() { - executeUpdate(windscribevpnServeripId, windscribevpnServeripName, windscribevpnServeripUpdateEnable, windscribevpnServeripSql, windscribevpnServeripCounter); + executeUpdate(windscribevpnServeripUUID, windscribevpnServeripName, windscribevpnServeripUpdateEnable, windscribevpnServeripSql, windscribevpnServeripCounter); } @Scheduled(cron = "${tsg.object.ip.windscribevpn_serverip.delete.cron}") public void deleteWindscribevpn() { - executeDelete(windscribevpnServeripId, windscribevpnServeripDeleteEnable, windscribevpnServeripOffsetSecond); + executeDelete(windscribevpnServeripUUID, windscribevpnServeripDeleteEnable, windscribevpnServeripOffsetSecond); } - @Value("${tsg.object.ip.vpn4fame_serverip.id}") - private Integer vpn4fameServeripId; + @Value("${tsg.object.ip.vpn4fame_serverip.uuid}") + private String vpn4fameServeripUUID; @Value("${tsg.object.ip.vpn4fame_serverip.name}") private String vpn4fameServeripName; @Value("${tsg.object.ip.vpn4fame_serverip.update.enable}") @@ -259,16 +258,16 @@ public class IpScheduler { @Scheduled(cron = "${tsg.object.ip.vpn4fame_serverip.update.cron}") public void updateVpn4fame() { - executeUpdate(vpn4fameServeripId, vpn4fameServeripName, vpn4fameServeripUpdateEnable, vpn4fameServeripSql, vpn4fameServeripCounter); + executeUpdate(vpn4fameServeripUUID, vpn4fameServeripName, vpn4fameServeripUpdateEnable, vpn4fameServeripSql, vpn4fameServeripCounter); } @Scheduled(cron = "${tsg.object.ip.vpn4fame_serverip.delete.cron}") public void deleteVpn4fame() { - executeDelete(vpn4fameServeripId, vpn4fameServeripDeleteEnable, vpn4fameServeripOffsetSecond); + executeDelete(vpn4fameServeripUUID, vpn4fameServeripDeleteEnable, vpn4fameServeripOffsetSecond); } - @Value("${tsg.object.ip.protonvpn_ip.id}") - private Integer protonvpnIpId; + @Value("${tsg.object.ip.protonvpn_ip.uuid}") + private String protonvpnIpUUID; @Value("${tsg.object.ip.protonvpn_ip.name}") private String protonvpnIpName; @Value("${tsg.object.ip.protonvpn_ip.update.enable}") @@ -283,16 +282,16 @@ public class IpScheduler { @Scheduled(cron = "${tsg.object.ip.protonvpn_ip.update.cron}") public void updateProtonvpn() { - executeUpdate(protonvpnIpId, protonvpnIpName, protonvpnIpUpdateEnable, protonvpnIpSql, protonvpnIpCounter); + executeUpdate(protonvpnIpUUID, protonvpnIpName, protonvpnIpUpdateEnable, protonvpnIpSql, protonvpnIpCounter); } @Scheduled(cron = "${tsg.object.ip.protonvpn_ip.delete.cron}") public void deleteProtonvpn() { - executeDelete(protonvpnIpId, protonvpnIpDeleteEnable, protonvpnIpOffsetSecond); + executeDelete(protonvpnIpUUID, protonvpnIpDeleteEnable, protonvpnIpOffsetSecond); } - @Value("${tsg.object.ip.expressvpn_ip.id}") - private Integer expressvpnIpId; + @Value("${tsg.object.ip.expressvpn_ip.uuid}") + private String expressvpnIpUUID; @Value("${tsg.object.ip.expressvpn_ip.name}") private String expressvpnIpName; @Value("${tsg.object.ip.expressvpn_ip.update.enable}") @@ -307,16 +306,16 @@ public class IpScheduler { @Scheduled(cron = "${tsg.object.ip.expressvpn_ip.update.cron}") public void updateExpressvpn() { - executeUpdate(expressvpnIpId, expressvpnIpName, expressvpnIpUpdateEnable, expressvpnIpSql, expressvpnIpCounter); + executeUpdate(expressvpnIpUUID, expressvpnIpName, expressvpnIpUpdateEnable, expressvpnIpSql, expressvpnIpCounter); } @Scheduled(cron = "${tsg.object.ip.expressvpn_ip.delete.cron}") public void deleteExpressvpn() { - executeDelete(expressvpnIpId, expressvpnIpDeleteEnable, expressvpnIpOffsetSecond); + executeDelete(expressvpnIpUUID, expressvpnIpDeleteEnable, expressvpnIpOffsetSecond); } - @Value("${tsg.object.ip.psiphon3vpn_serverip.id}") - private Integer psiphon3vpnServeripId; + @Value("${tsg.object.ip.psiphon3vpn_serverip.uuid}") + private String psiphon3vpnServeripUUID; @Value("${tsg.object.ip.psiphon3vpn_serverip.name}") private String psiphon3vpnServeripName; @Value("${tsg.object.ip.psiphon3vpn_serverip.update.enable}") @@ -331,46 +330,28 @@ public class IpScheduler { @Scheduled(cron = "${tsg.object.ip.psiphon3vpn_serverip.update.cron}") public void updatePsiphon3vpn() { - executeUpdate(psiphon3vpnServeripId, psiphon3vpnServeripName, psiphon3vpnServeripUpdateEnable, psiphon3vpnServeripSql, psiphon3vpnServeripCounter); + executeUpdate(psiphon3vpnServeripUUID, psiphon3vpnServeripName, psiphon3vpnServeripUpdateEnable, psiphon3vpnServeripSql, psiphon3vpnServeripCounter); } @Scheduled(cron = "${tsg.object.ip.psiphon3vpn_serverip.delete.cron}") public void deletePsiphon3vpn() { - executeDelete(psiphon3vpnServeripId, psiphon3vpnServeripDeleteEnable, psiphon3vpnServeripOffsetSecond); + executeDelete(psiphon3vpnServeripUUID, psiphon3vpnServeripDeleteEnable, psiphon3vpnServeripOffsetSecond); } - private static void executeUpdate(Integer id, String name, Boolean enable, String sql, Counter counter) { - if (TsgUtil.isLatestVersion) { - executeUpdateLatest(id, name, enable, sql, counter); - } else { - executeUpdateOld(id, name, enable, sql, counter); - } - } - - private static void executeDelete(Integer id, Boolean enable, Integer offsetSecond) { - if (TsgUtil.isLatestVersion) { - executeDeleteLatest(id, enable, offsetSecond); - } else { - executeDeleteOld(id, enable, offsetSecond); - } - } - - - private static void executeUpdateLatest(Integer id, String name, Boolean enable, String sql, Counter counter) { - log.info("{}: started update task.", id); + private static void executeUpdate(String uuid, String name, Boolean enable, String sql, Counter counter) { + log.info("{}: started update task.", uuid); if (BooleanUtil.isFalse(enable)) { - log.warn("{}: interrupted update task. enable: {}", id, enable); + log.warn("{}: interrupted update task. enable: {}", uuid, enable); return; } try { Stopwatch watch = Stopwatch.createStarted(); List<Record> data = Db.find(sql); - log.info("{}: query knowledge base content, cost {} seconds", id, watch.elapsed(TimeUnit.SECONDS)); + log.info("{}: query knowledge base content, cost {} seconds", uuid, watch.elapsed(TimeUnit.SECONDS)); watch.reset().start(); List<Map<String, Object>> items = Lists.newArrayList(); row: for (Record record : data) { - Map<String, Object> item = Maps.newHashMap(); String addressFormat = record.get("addrFormat"); String ip1 = record.get("ip1"); for (String excludeItem : excludeList) { @@ -381,165 +362,91 @@ public class IpScheduler { continue row; } } + InetAddress inetAddressIp1 = InetAddress.getByName(ip1); + if (!(inetAddressIp1 instanceof Inet4Address) && !(inetAddressIp1 instanceof Inet6Address)) { + continue; + } + String ip2 = record.get("ip2"); - Map<String, Object> ip = Maps.newHashMap(); - if (AddressFormat.CIDR.getValue().equalsIgnoreCase(addressFormat)) { + if (AddressFormat.RANGE.getValue().equalsIgnoreCase(addressFormat)) { + InetAddress inetAddressIp2 = InetAddress.getByName(ip2); + if (!(inetAddressIp2 instanceof Inet4Address) && !(inetAddressIp2 instanceof Inet6Address)) { + continue; + } + } + + Map<String, Object> item = Maps.newHashMap(); + if (AddressFormat.SINGLE.getValue().equalsIgnoreCase(addressFormat)) { + item.put(TsgObject.KEY_OBJECT_MEMBER_ITEMS_IP, ip1); + } else if (AddressFormat.CIDR.getValue().equalsIgnoreCase(addressFormat)) { int maskBit = Integer.parseInt(ip2); String mask = MaskBit.get(Integer.parseInt(ip2)); String beginIpStr = Ipv4Util.getBeginIpStr(ip1, maskBit); String ipMask = Ipv4Util.formatIpBlock(beginIpStr, mask); - ip.put(TsgObject.KEY_OBJECT_MEMBER_ITEMS_IP_IP_CIDR, ipMask); + item.put(TsgObject.KEY_OBJECT_MEMBER_ITEMS_IP, ipMask); } else if (AddressFormat.RANGE.getValue().equalsIgnoreCase(addressFormat)) { - ip.put(TsgObject.KEY_OBJECT_MEMBER_ITEMS_IP_IP_RANGE, ip1 + "-" + ip2); - } else { - ip.put(TsgObject.KEY_OBJECT_MEMBER_ITEMS_IP_IP_ADDRESS, ip1); - } - ip.put(TsgObject.KEY_OBJECT_MEMBER_ITEMS_IP_PORT_RANGE, TsgObject.VALUE_OBJECT_MEMBER_ITEMS_IP_PORT_RANGE); - InetAddress inetAddress = InetAddress.getByName(ip1); - if (inetAddress instanceof Inet4Address) { - ip.put(TsgObject.KEY_OBJECT_MEMBER_ITEMS_IP_ADDR_TYPE, 4); - } else if (inetAddress instanceof Inet6Address) { - ip.put(TsgObject.KEY_OBJECT_MEMBER_ITEMS_IP_ADDR_TYPE, 6); + item.put(TsgObject.KEY_OBJECT_MEMBER_ITEMS_IP, ip1 + "-" + ip2); } else { continue; } item.put(TsgObject.KEY_OBJECT_MEMBER_ITEMS_OP, TsgObject.VALUE_OBJECT_MEMBER_ITEMS_OP_ADD); - item.put(TsgObject.KEY_OBJECT_MEMBER_ITEMS_IP, ip); + item.put("interval", "0-65535"); items.add(item); } - Map<String, Object> member = Maps.newHashMap(); - member.put(TsgObject.KEY_OBJECT_MEMBER_TYPE, TsgObject.VALUE_OBJECT_MEMBER_TYPE_1); - member.put(TsgObject.KEY_OBJECT_MEMBER_ITEMS, items); - - Map<String, Object> obj = Maps.newHashMap(); - obj.put(TsgObject.KEY_OBJECT_NAME, name); - obj.put(TsgObject.KEY_OBJECT_TYPE, TsgObject.VALUE_OBJECT_TYPE_IP); - obj.put(TsgObject.KEY_OBJECT_MEMBER, member); + Map<String, Object> object = Maps.newHashMap(); + object.put(TsgObject.KEY_OBJECT_MEMBER_ITEMS, items); + object.put(TsgObject.KEY_OBJECT_NAME, name); + object.put("type", "ip"); + object.put("sub_type", "ip"); + object.put("statistics_option", "none"); + object.put("member_type", "item"); Map<String, Object> body = Maps.newHashMap(); - body.put(TsgObject.KEY_VSYS_ID, TsgObject.VALUE_VSYS_ID_1); - body.put(TsgObject.KEY_OBJECT, obj); - log.info("{}: build api params, items size: {}, cost {} seconds", id, items.size(), watch.elapsed(TimeUnit.SECONDS)); + body.put(TsgObject.KEY_VSYS, TsgObject.VALUE_VSYS_1); + body.put("op", "update"); + body.put(TsgObject.KEY_OBJECT, object); + log.info("{}: build api params, items size: {}, cost {} seconds", uuid, items.size(), watch.elapsed(TimeUnit.SECONDS)); if (items.isEmpty()) { return; } - TsgUtil.updateObjectById(id, body); + TsgUtil.updateObjectByUUID(TsgObject.VALUE_OBJECT_TYPE_IP, uuid, body); counter.inc(items.size()); } catch (Exception e) { - log.error("{}: failed to execute update task. message: {}", id, e.getMessage()); + log.error("{}: failed to execute update task. message: {}", uuid, e.getMessage()); throw new RuntimeException(e); } } - private static void executeUpdateOld(Integer id, String name, Boolean enable, String sql, Counter counter) { - log.info("{}: started update task.", id); - if (BooleanUtil.isFalse(enable)) { - log.warn("{}: interrupted update task. enable: {}", id, enable); - return; - } - try { - Stopwatch watch = Stopwatch.createStarted(); - List<Record> data = Db.find(sql); - log.info("{}: query knowledge base content, cost {} seconds", id, watch.elapsed(TimeUnit.SECONDS)); - watch.reset().start(); - List<Map<String, Object>> items = Lists.newArrayList(); - row: - for (Record record : data) { - String addressFormat = record.get("addrFormat"); - String ip1 = record.get("ip1"); - if (StrUtil.isBlank(ip1)) { - continue; - } - for (String excludeItem : excludeList) { - if (excludeItem.equals(ip1)) { - continue row; - } - if (excludeItem.endsWith("*") && ip1.startsWith(excludeItem.replace("*", ""))) { - continue row; - } - } - String ip2 = record.get("ip2"); - Map<String, Object> ip = Maps.newHashMap(); - if (AddressFormat.CIDR.getValue().equalsIgnoreCase(addressFormat)) { - int maskBit = Integer.parseInt(ip2); - String mask = MaskBit.get(Integer.parseInt(ip2)); - String beginIpStr = Ipv4Util.getBeginIpStr(ip1, maskBit); - String ipMask = Ipv4Util.formatIpBlock(beginIpStr, mask); - ip.put(TsgObject.TSG_ITEM_IP_V2310, ipMask); - } else if (AddressFormat.RANGE.getValue().equalsIgnoreCase(addressFormat)) { - ip.put(TsgObject.TSG_ITEM_IP_V2310, ip1 + "-" + ip2); - } else if (AddressFormat.SINGLE.getValue().equalsIgnoreCase(addressFormat)) { - ip.put(TsgObject.TSG_ITEM_IP_V2310, ip1); - } else { - log.warn("address format parse error: {}", record); - continue; - } - ip.put(TsgObject.TSG_ITEM_PORT_V2310, TsgObject.TSG_VALUE_OBJECT_MEMBER_ITEMS_IP_PORT_RANGE_V2310); - ip.put(TsgObject.TSG_ITEM_ISSESSION_V2310, TsgObject.TSG_ITEM_ENDPOINT_V2310); - items.add(ip); - } - JSONArray array = new JSONArray(); - - JSONObject jsonObject = new JSONObject(); - jsonObject.set(TsgObject.TSG_OBJECT_ID_V2310, id); - jsonObject.set(TsgObject.TSG_OBJECT_TYPE_V2310, TsgObject.TSG_IP_ADDR_OBJECT_V2310); - jsonObject.set(TsgObject.TSG_OBJECT_NAME_V2310, name); - jsonObject.set(TsgObject.TSG_IS_BUILTIN_V2310, 0); - jsonObject.set(TsgObject.TSG_IS_EXCLUSION_V2310, 0); - jsonObject.set(TsgObject.TSG_IS_VALID_V2310, 1); - jsonObject.set("addItemList", items); - log.info("{}: build api params, items size: {}, cost {} seconds", id, items.size(), watch.elapsed(TimeUnit.SECONDS)); - if (items.isEmpty()) { - return; - } - array.add(jsonObject); - JSONObject body = new JSONObject(); - body.set(TsgObject.TSG_OP_ACTION_V2310, TsgObject.TSG_UPDATE_V2310); - body.set(TsgObject.TSG_OBJECT_LIST_V2310, array); - - TsgUtil.updateObjectOld(body); - counter.inc(items.size()); - } catch (Exception e) { - log.error("{}: failed to execute update task. message: {}", id, e.getMessage()); - throw new RuntimeException(e); - } - } - private static void executeDeleteLatest(Integer id, Boolean enable, Integer offsetSecond) { - log.info("{}: started delete task.", id); + private static void executeDelete(String uuid, Boolean enable, Integer offsetSecond) { + log.info("{}: started delete task.", uuid); if (BooleanUtil.isFalse(enable)) { - log.warn("{}: interrupted delete task. enable: {}", id, enable); + log.warn("{}: interrupted delete task. enable: {}", uuid, enable); return; } try { DateTime deleteCursor = DateUtil.offsetSecond(new Date(), offsetSecond).setTimeZone(TimeZone.getTimeZone("UTC")); String deleteCursorStr = DateUtil.format(deleteCursor, DatePattern.UTC_PATTERN); - JSONObject objectItemList = TsgUtil.getObjectItemList(id, "ip"); + JSONObject objectItemList = TsgUtil.getObjectItemList(uuid, TsgObject.VALUE_OBJECT_TYPE_IP); Object data = objectItemList.get("data"); if (StrUtil.isEmptyIfStr(data)) { log.warn("No item does not need to be deleted"); return; } Map<String, Object> dataMap = (Map<String, Object>) data; - Object itemsObj = dataMap.get("items"); - if (StrUtil.isEmptyIfStr(itemsObj) || StrUtil.isBlankIfStr(itemsObj)) { - log.warn("No item does not need to be deleted"); - return; - } - List<Map<String, Object>> itemList = (List<Map<String, Object>>) itemsObj; - if (itemList.isEmpty()) { + Object list = dataMap.get("list"); + if (StrUtil.isEmptyIfStr(list) || StrUtil.isBlankIfStr(list)) { log.warn("No item does not need to be deleted"); return; } - Map<String, Object> map = itemList.get(0); - Object itemObj = map.get("ip"); - if (StrUtil.isEmptyIfStr(itemObj)) { + List<Map<String, Object>> listItems = (List<Map<String, Object>>) list; + if (listItems.isEmpty()) { log.warn("No item does not need to be deleted"); return; } - Map<String, Object> itemMap = (Map<String, Object>) itemObj; - Object createdTimeObj = itemMap.get("created_time"); + Map<String, Object> listItem = listItems.get(0); + Object createdTimeObj = listItem.get("created_time"); DateTime latestCreatedDateTime = DateUtil.parse(createdTimeObj.toString()); int compare = DateUtil.compare(deleteCursor, latestCreatedDateTime); if (compare > 0) { @@ -547,36 +454,13 @@ public class IpScheduler { return; } Map<String, Object> form = Maps.newHashMap(); - form.put(TsgObject.KEY_OBJECT_TYPE, TsgObject.VALUE_OBJECT_TYPE_IP); - form.put(TsgObject.KEY_VSYS_ID, TsgObject.VALUE_VSYS_ID_1); + form.put(TsgObject.KEY_VSYS, TsgObject.VALUE_VSYS_1); form.put(TsgObject.KEY_CREATED_BEFORE, deleteCursorStr); - TsgUtil.deleteItemOfObjectById(id, form); + TsgUtil.deleteItemOfObjectByUUID(TsgObject.VALUE_OBJECT_TYPE_IP, uuid, form); } catch (Exception e) { - log.error("{}: failed to execute delete task. message: {}", id, e.getMessage()); + log.error("{}: failed to execute delete task. message: {}", uuid, e.getMessage()); throw new RuntimeException(e); } } - private static void executeDeleteOld(Integer id, Boolean enable, Integer offsetSecond) { - log.info("{}: started delete task.", id); - if (BooleanUtil.isFalse(enable)) { - log.warn("{}: interrupted delete task. enable: {}", id, enable); - return; - } - try { - DateTime dateTime = DateUtil.offsetSecond(new Date(), offsetSecond).setTimeZone(TimeZone.getTimeZone("UTC")); - String datetimeZ = DateUtil.format(dateTime, DatePattern.UTC_PATTERN); - Map<String, Object> body = new HashMap<>(16); - body.put(TsgObject.TSG_DELETE_ITEMS_BYL_TTIME_V2310, datetimeZ); - body.put(TsgObject.TSG_ITEM_TYPE_V2310, TsgObject.TSG_ITEM_IP_V2310); - body.put(TsgObject.TSG_VSYS_ID_V2310, 1); - List<Object> objectIds = new ArrayList<>(); - objectIds.add(id); - body.put(TsgObject.TSG_TSG_OBJECT_IDS_V2310, objectIds); - TsgUtil.deleteItemOfObjectOld(body); - } catch (Exception e) { - log.error("{}: failed to execute delete task. message: {}", id, e.getMessage()); - throw new RuntimeException(e); - } - } } |
