diff options
6 files changed, 92 insertions, 123 deletions
diff --git a/src/main/java/com/example/nis/Scheduled/Entity.java b/src/main/java/com/example/nis/Scheduled/Entity.java index 1ee5dbe..ddfec21 100644 --- a/src/main/java/com/example/nis/Scheduled/Entity.java +++ b/src/main/java/com/example/nis/Scheduled/Entity.java @@ -112,7 +112,7 @@ public class Entity { log.info("{} all objects is empty", CALLING_STATION_ID_IP); return ResponseData.ok(); } - boolean b = tsgService.updateObject(arrayObjects); + boolean b = tsgService.updateObject(null, null); if (b) { return ResponseData.ok(); } else { @@ -292,7 +292,7 @@ public class Entity { private ResponseData optionDeleteToTsg(String body) { try { - return tsgService.deleteExpireItemsOfObject(body) ? ResponseData.ok() : ResponseData.error("delete failed"); + return tsgService.deleteExpireItemsOfObject(null, null) ? ResponseData.ok() : ResponseData.error("delete failed"); } catch (Exception e) { throw new RuntimeException(e); } @@ -353,7 +353,7 @@ public class Entity { JSONArray array = new JSONArray(); JSONObject jsonObject = buildJsonObject(source, ipObjectId, ipObjectName); array.add(jsonObject); - boolean b = tsgService.updateObject(array); + boolean b = tsgService.updateObject(null, null); if (b) { return ResponseData.ok(); } else { diff --git a/src/main/java/com/example/nis/common/TsgServiceImpl.java b/src/main/java/com/example/nis/common/TsgServiceImpl.java index 7934b7a..9eb4dea 100644 --- a/src/main/java/com/example/nis/common/TsgServiceImpl.java +++ b/src/main/java/com/example/nis/common/TsgServiceImpl.java @@ -2,6 +2,7 @@ package com.example.nis.common; import cn.hutool.http.Header; import cn.hutool.http.HttpRequest; +import cn.hutool.http.HttpUtil; import cn.hutool.json.JSONArray; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; @@ -95,35 +96,26 @@ public class TsgServiceImpl { } /** - * 对象 修改 - * @param objectList + * 对象更新 + * @param jsonObject * @return */ - public boolean updateObject(JSONArray objectList) throws Exception { - JSONObject jsonObject1 = new JSONObject(); - jsonObject1.set(Constant.TSG_RETURN_DATA, 1); - jsonObject1.set(Constant.TSG_OP_ACTION, Constant.TSG_UPDATE); - if (objectList !=null && objectList.size()>0) { - jsonObject1.set(Constant.TSG_OBJECT_LIST, objectList); - } else { - log.error("对象修改失败!objectList参数不能为空"); - throw new RuntimeException("对象修改失败!objectList参数不能为空"); - } + public boolean updateObject(Integer id, JSONObject jsonObject) throws Exception { boolean result; try { // 修改 String token = ToTsgSystemUtil.sendLoginRequest(); long start = System.currentTimeMillis(); log.info("开始修改"); - String result1 = HttpRequest.put(ToTsgSystemUtil.TSG_URL+"v1/policy/object") + String result1 = HttpRequest.put(ToTsgSystemUtil.TSG_URL + "/v1/policy/object/" + id) .header(Header.AUTHORIZATION, token) .header(Header.CONTENT_TYPE, Constant.TSG_APPLICATION_JSON) - .body(jsonObject1.toString()) + .body(jsonObject.toString()) .timeout(httpTimeOut) .execute().body(); if (StringUtils.isNotBlank(result1)) { - JSONObject jsonObject = JSONUtil.parseObj(result1); - if (Constant.TSG_SUCCESS_CODE.equals(jsonObject.get(Constant.TSG_CODE).toString())) { + JSONObject resP = JSONUtil.parseObj(result1); + if (Constant.TSG_SUCCESS_CODE.equals(resP.get(Constant.TSG_CODE).toString())) { result = true; log.info("对象修改成功,耗时: {} millisecond", System.currentTimeMillis() - start); } else { @@ -149,16 +141,16 @@ public class TsgServiceImpl { * @created by wWei * @date 2023/2/17 17:55 */ - public boolean deleteExpireItemsOfObject(String body) throws Exception { + public boolean deleteExpireItemsOfObject(Integer id, Map<String, Object> forms) throws Exception { boolean result; try { String token = ToTsgSystemUtil.sendLoginRequest(); long start = System.currentTimeMillis(); - log.info("start delete, Body params: {}", body); - String result1 = HttpRequest.delete(ToTsgSystemUtil.TSG_URL + "v1/policy/items") + log.info("start delete, url params: {}", forms); + String urlParams = HttpUtil.toParams(forms); + String result1 = HttpRequest.delete(ToTsgSystemUtil.TSG_URL + "/v1/policy/object/" + id + "/item?" + urlParams) .header(Header.AUTHORIZATION, token) .header(Header.CONTENT_TYPE, Constant.TSG_APPLICATION_JSON) - .body(body) .timeout(httpTimeOut) .execute().body(); if (StringUtils.isNotBlank(result1)) { diff --git a/src/main/java/com/example/nis/controller/FqdnController.java b/src/main/java/com/example/nis/controller/FqdnController.java index eeac860..c163497 100644 --- a/src/main/java/com/example/nis/controller/FqdnController.java +++ b/src/main/java/com/example/nis/controller/FqdnController.java @@ -124,7 +124,7 @@ public class FqdnController { jsonObject.set("deleteItemIds",new JSONArray()); } array.add(jsonObject); - boolean b = tsgService.updateObject(array); + boolean b = tsgService.updateObject(null, null); if (b) { return ResponseData.ok(); } else { diff --git a/src/main/java/com/example/nis/controller/IpController.java b/src/main/java/com/example/nis/controller/IpController.java index 9849380..11a2639 100644 --- a/src/main/java/com/example/nis/controller/IpController.java +++ b/src/main/java/com/example/nis/controller/IpController.java @@ -8,7 +8,6 @@ import cn.hutool.core.util.BooleanUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.json.JSONArray; import cn.hutool.json.JSONObject; -import cn.hutool.json.JSONUtil; import com.example.nis.common.Code; import com.example.nis.common.ResponseData; import com.example.nis.common.TsgServiceImpl; @@ -23,6 +22,7 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.web.bind.annotation.*; import sun.net.util.IPAddressUtil; + import java.util.*; import java.util.stream.Collectors; @@ -78,9 +78,8 @@ public class IpController { IpSource ipSource = new IpSource(); ipSource.setDeleteItemList(new ArrayList<>()); long start = System.currentTimeMillis(); - List<IpObject> addList = new ArrayList<>(); List<String> rawLines = FileUtil.readUtf8Lines(ipUpdateFilePath); - List<String> toAddList = new ArrayList<>(); + List<Map<String, Object>> itemList = new ArrayList<>(); ip: for (String line : rawLines) { if (StrUtil.isBlank(line)) { @@ -94,61 +93,83 @@ public class IpController { continue ip; } } + Map<String, Object> item = new HashMap<>(); + item.put("op", "add"); + Map<String, Object> ip = new HashMap<>(); if (IPAddressUtil.isIPv4LiteralAddress(line)) { if (StrUtil.isBlankIfStr(ipUpdateMaskBit)) { - toAddList.add(line); + ip.put("ip_address", line); + ip.put("port_range", "0-65535"); + ip.put("addr_type", 4); + item.put("ip", ip); + itemList.add(item); } else { String mask = MaskBit.get(ipUpdateMaskBit); String beginIpStr = Ipv4Util.getBeginIpStr(line, ipUpdateMaskBit); - toAddList.add(Ipv4Util.formatIpBlock(beginIpStr, mask)); + String ipCidr = Ipv4Util.formatIpBlock(beginIpStr, mask); + ip.put("ip_cidr", ipCidr); + ip.put("port_range", "0-65535"); + ip.put("addr_type", 4); + item.put("ip", ip); + itemList.add(item); } continue; } if (IPAddressUtil.isIPv6LiteralAddress(line)) { - toAddList.add(line); + ip.put("ip_address", line); + ip.put("port_range", "0-65535"); + ip.put("addr_type", 6); + item.put("ip", ip); + itemList.add(item); continue; } log.warn("Not IPv4 or IPv6: {}", line); } - toAddList = toAddList.stream().distinct().collect(Collectors.toList()); - toAddList = toAddList.size() > upperLimit ? toAddList.subList(0, upperLimit) : toAddList; - toAddList.forEach(o -> { - IpObject ipObject = new IpObject(); - ipObject.setIp(o); - ipObject.setPort("0-65535"); - addList.add(ipObject); - }); - - log.info(" [OLAP] reader entity,cost: {} millisecond, wait to add count: {}", System.currentTimeMillis() - start, addList.size()); - ipSource.setAddItemList(addList); - ResponseData putResp = put(ipSource, ipObjectId, ipObjectName); + itemList = itemList.stream().distinct().collect(Collectors.toList()); + itemList = itemList.size() > upperLimit ? itemList.subList(0, upperLimit) : itemList; + log.info(" [OLAP] reader entity,cost: {} millisecond, wait to add count: {}", System.currentTimeMillis() - start, itemList.size()); + if (itemList.size() == 0) { + log.warn(" [OLAP] wait to add count: 0, No Update"); + return ResponseData.ok(); + } + JSONObject jsonObject = buildBody(itemList); + ResponseData putResp = put(ipObjectId, jsonObject); if (!StrUtil.isBlankIfStr(putResp) && Code.SUCCESS.getCode().equals(putResp.get("code"))) { return scheduledExecutorOfDelete(); } return putResp; } + private JSONObject buildBody(List<Map<String, Object>> items) { + Map<String, Object> member = new HashMap<>(); + member.put("type", 1); + member.put("items", items); + + Map<String, Object> object = new HashMap<>(); + object.put("name", ipObjectName); + object.put("type", "ip"); + object.put("member", member); + + JSONObject jsonObject = new JSONObject(); + jsonObject.set("vsys_id", 1); + jsonObject.set("object", object); + return jsonObject; + } + public ResponseData scheduledExecutorOfDelete() { log.info("start server ip delete task"); - List<Object> objectIds = new ArrayList<>(); - objectIds.add(ipObjectId); String data = df.format(DateUtil.offsetSecond(new Date(), deleteOffset)); - return deleteOption(objectIds, data); + Map<String, Object> urlParamMaps = new HashMap<>(16); + urlParamMaps.put("vsys_id", 1); + urlParamMaps.put("type", "ip"); + urlParamMaps.put("created_before", data); + return optionDeleteToTsg(urlParamMaps); } - private ResponseData deleteOption(List<Object> objectIds, String dataUtcIso) { - Map<String, Object> body = new HashMap<>(16); - body.put("deleteItemsByLtTime", dataUtcIso); - body.put(Constant.TSG_ITEM_TYPE, Constant.TSG_ITEM_IP); - body.put(Constant.TSG_VSYS_ID, 1); - body.put(Constant.TSG_OBJECT_IDS, objectIds); - return optionDeleteToTsg(JSONUtil.toJsonStr(body)); - } - - private ResponseData optionDeleteToTsg(String body) { + private ResponseData optionDeleteToTsg(Map<String, Object> forms) { try { - return tsgService.deleteExpireItemsOfObject(body) ? ResponseData.ok() : ResponseData.error("delete failed"); + return tsgService.deleteExpireItemsOfObject(ipObjectId, forms) ? ResponseData.ok() : ResponseData.error("delete failed"); } catch (Exception e) { throw new RuntimeException(e); } @@ -156,82 +177,41 @@ public class IpController { @GetMapping public ResponseData get(IpObject object) { - try { - if (object !=null) { - Integer[] itemIds = null; - if (StringUtils.isNotEmpty(object.getItemIds())) { - String[] split = object.getItemIds().split(","); - itemIds = new Integer[split.length]; - for (int i =0;i<split.length;i++) { - itemIds[i] = Integer.parseInt(split[i]); - } - } - JSONArray itemList = tsgService.getItemList(ToTsgSystemUtil.ipObjectId, Constant.TSG_ITEM_IP, object.getItemId(), itemIds, object.getIp(), object.getPort(), null); - List<IpObject> list = new ArrayList<>(); - if (itemList!=null && itemList.size()>0) { - for (int i = 0;i<itemList.size();i++) { + try { + if (object != null) { + Integer[] itemIds = null; + if (StringUtils.isNotEmpty(object.getItemIds())) { + String[] split = object.getItemIds().split(","); + itemIds = new Integer[split.length]; + for (int i = 0; i < split.length; i++) { + itemIds[i] = Integer.parseInt(split[i]); + } + } + JSONArray itemList = tsgService.getItemList(ToTsgSystemUtil.ipObjectId, Constant.TSG_ITEM_IP, object.getItemId(), itemIds, object.getIp(), object.getPort(), null); + List<IpObject> list = new ArrayList<>(); + if (itemList != null && itemList.size() > 0) { + for (int i = 0; i < itemList.size(); i++) { JSONObject jsonObject = itemList.getJSONObject(i); - IpObject ipObject = new IpObject(); + IpObject ipObject = new IpObject(); ipObject.setItemId(Integer.parseInt(jsonObject.get(Constant.TSG_ITEM_ID).toString())); ipObject.setIp(jsonObject.get(Constant.TSG_ITEM_IP).toString()); ipObject.setPort(jsonObject.get(Constant.TSG_ITEM_PORT).toString()); list.add(ipObject); } } - return ResponseData.ok(list); - }else { - return ResponseData.error(); - } + return ResponseData.ok(list); + } else { + return ResponseData.error(); + } } catch (Exception e) { e.printStackTrace(); return ResponseData.error(); } } - @PutMapping - public ResponseData put(@RequestBody IpSource source, Integer id, String name) { + public ResponseData put(Integer id, JSONObject body) { try { - JSONArray array =new JSONArray(); - JSONObject jsonObject = new JSONObject(); - jsonObject.set(Constant.TSG_OBJECT_ID, id); - jsonObject.set(Constant.TSG_OBJECT_TYPE,Constant.TSG_IP_ADDR_OBJECT); - jsonObject.set(Constant.TSG_OBJECT_NAME,name); - jsonObject.set(Constant.TSG_IS_BUILTIN,0); - jsonObject.set(Constant.TSG_IS_EXCLUSION,0); - jsonObject.set(Constant.TSG_IS_VALID,1); - if (source.getAddItemList()!=null && source.getAddItemList().size()>0) { - JSONArray jsonArray = JSONUtil.parseArray(source.getAddItemList()); - for (int i =0;i<jsonArray.size();i++) { - JSONObject obj = jsonArray.getJSONObject(i); - obj.set(Constant.TSG_ITEM_ISSESSION,Constant.TSG_ITEM_ENDPOINT); - } - jsonObject.set("addItemList",jsonArray); - }else { - jsonObject.set("addItemList",new JSONArray()); - } - if (source.getUpdateItemList()!=null && source.getUpdateItemList().size()>0) { - JSONArray jsonArray = JSONUtil.parseArray(source.getUpdateItemList()); - for (int i =0;i<jsonArray.size();i++) { - JSONObject obj = jsonArray.getJSONObject(i); - obj.set(Constant.TSG_ITEM_ISSESSION,Constant.TSG_ITEM_ENDPOINT); - } - jsonObject.set("updateItemList",jsonArray); - }else { - jsonObject.set("updateItemList",new JSONArray()); - } - if (source.getDeleteItemList()!=null && source.getDeleteItemList().size()>0) { - JSONArray jsonArray = JSONUtil.parseArray(source.getDeleteItemList()); - Integer[] deleteItemIds = new Integer[source.getDeleteItemList().size()]; - for (int i =0;i<jsonArray.size();i++) { - JSONObject obj = jsonArray.getJSONObject(i); - deleteItemIds[i] = Integer.parseInt(obj.get("itemId").toString()); - } - jsonObject.set("deleteItemIds",deleteItemIds); - }else { - jsonObject.set("deleteItemIds",new JSONArray()); - } - array.add(jsonObject); - boolean b = tsgService.updateObject(array); + boolean b = tsgService.updateObject(id, body); if (b) { return ResponseData.ok(); } else { @@ -242,6 +222,4 @@ public class IpController { return ResponseData.error(); } } - - } diff --git a/src/main/java/com/example/nis/controller/UrlController.java b/src/main/java/com/example/nis/controller/UrlController.java index e0e3c67..94a1db3 100644 --- a/src/main/java/com/example/nis/controller/UrlController.java +++ b/src/main/java/com/example/nis/controller/UrlController.java @@ -5,7 +5,6 @@ import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; import com.example.nis.common.ResponseData; import com.example.nis.common.TsgServiceImpl; -import com.example.nis.domain.IpObject; import com.example.nis.domain.KeywordsObject; import com.example.nis.domain.KeywordsSource; import com.example.nis.util.Constant; @@ -125,7 +124,7 @@ public class UrlController { jsonObject.set("deleteItemIds",new JSONArray()); } array.add(jsonObject); - boolean b = tsgService.updateObject(array); + boolean b = tsgService.updateObject(null, null); if (b) { return ResponseData.ok(); } else { diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index e225689..40b2907 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -8,7 +8,7 @@ spring: # TSG 账号 tsg: system: - url: http://192.168.44.29:8083/ + url: http://192.168.44.29:8083 username: galaxy password: ozcQzjngV8A2C34NuW9KCQ== token: |
