REM:
1.  手动合并代码
This commit is contained in:
HuangXin 2021-11-01 15:09:24 +08:00
parent 7456a9947e
commit 325caf2f9e
4 changed files with 202 additions and 159 deletions

View File

@ -10,6 +10,7 @@ import com.cmhi.dispose.common.CmhiDisposeStartTaskRsp;
import com.cmhi.dispose.common.CmhiDisposeStopTaskReq;
import com.cmhi.dispose.common.CmhiDisposeStopTaskRsp;
import com.cmhi.dispose.common.CmhiDisposeTaskStatusRsp;
import com.dispose.common.CommonEnumHandler;
import com.dispose.common.ErrorCode;
import com.dispose.pojo.dto.protocol.base.BaseProtocolDTO;
import com.dispose.pojo.dto.protocol.base.ProtocolRespDTO;

View File

@ -8,6 +8,7 @@ import com.cmhi.dispose.common.CmhiDisposeStopTaskRsp;
import com.cmhi.dispose.common.CmhiDisposeTaskStatusRsp;
import com.cmhi.dispose.protocol.CmhiDisposeInterface;
import com.dispose.ability.DisposeAbility;
import com.dispose.common.CommonEnumHandler;
import com.dispose.common.DisposeCapacityType;
import com.dispose.common.DisposeConfigValue;
import com.dispose.common.DisposeObjectType;
@ -156,16 +157,17 @@ public class CmhiAbilityImpl implements DisposeAbility {
String url = urlRootPath + "dispose_device/task/start";
if (token == null || token.length() == 0) {
upgradeToken();
return new MulReturnType<>(ErrorCode.ERR_LOGOUT, "");
}
try {
log.info("++++Begging PengXin Start Cleanup Task: {}", disposeObject);
if (capType != DisposeCapacityType.BLACKHOOL) {
log.error("----Error PengXin don't support dispose capacity type: {}", capType);
return new MulReturnType<>(ErrorCode.ERR_UNSUPPORT, null);
}
// if (capType != DisposeCapacityType.BLACKHOOL) {
// log.error("----Error PengXin don't support dispose capacity type: {}", capType);
// return new MulReturnType<>(ErrorCode.ERR_UNSUPPORT, null);
// }
// 适配处置时间参数 -1为不限制处置时间
if (duration == null || duration < 0) {
@ -182,14 +184,20 @@ public class CmhiAbilityImpl implements DisposeAbility {
ProtocolRespDTO<CmhiDisposeStartTaskRsp> rspInfo = restfulInterface.startDisposeTask(url, token, reqItems);
// 判断是否token过期
if (rspInfo != null && reLogin(rspInfo, rspInfo.getMsgContent().getItems().get(0).getStatus())) {
rspInfo = restfulInterface.startDisposeTask(url, token, reqItems);
ErrorCode err = verifyInterfaceResp(rspInfo);
if (err != ErrorCode.ERR_OK) {
log.error("----Error PengXin start clean {} server return {}", disposeObject, err.getMsg());
return new MulReturnType<>(err, disposeObject);
}
if (rspInfo == null || rspInfo.getCode() != ErrorCode.ERR_OK.getHttpCode()) {
log.error("----Error PengXin start clean {} server return error", disposeObject);
return new MulReturnType<>(ErrorCode.ERR_PENGXIN_ERROR, null);
err = verifyRespStatus(rspInfo.getMsgContent().getItems().get(0).getStatus());
if (err != ErrorCode.ERR_OK) {
log.error("----Error PengXin start clean {} server return {}({})", disposeObject,
rspInfo.getMsgContent().getItems().get(0).getStatus(),
rspInfo.getMsgContent().getItems().get(0).getMessage());
return new MulReturnType<>(err, disposeObject);
}
if (rspInfo.getMsgContent().getItems().size() == 0) {
@ -203,7 +211,7 @@ public class CmhiAbilityImpl implements DisposeAbility {
return new MulReturnType<>(ErrorCode.ERR_PENGXIN_ERROR, null);
}
log.debug("----Finish PengXin Start Cleanup Task: {}", disposeObject);
log.info("----Finish PengXin Start Cleanup Task: {}", disposeObject);
return new MulReturnType<>(ErrorCode.ERR_OK, rspInfo.getMsgContent().getItems().get(0).getTaskId());
}
} catch (Exception ex) {
@ -230,6 +238,7 @@ public class CmhiAbilityImpl implements DisposeAbility {
String url = urlRootPath + "dispose_device/task/stop";
if (token == null || token.length() == 0) {
upgradeToken();
return new MulReturnType<>(ErrorCode.ERR_LOGOUT, null);
}
@ -248,14 +257,17 @@ public class CmhiAbilityImpl implements DisposeAbility {
ProtocolRespDTO<CmhiDisposeStopTaskRsp> rspInfo = restfulInterface.stopDisposeTask(url, token,
new String[]{taskId});
// 判断是否token过期
if (rspInfo != null && reLogin(rspInfo, rspInfo.getMsgContent().getItems().get(0).getStatus())) {
rspInfo = restfulInterface.stopDisposeTask(url, token, new String[]{taskId});
ErrorCode err = verifyInterfaceResp(rspInfo);
if (err != ErrorCode.ERR_OK) {
log.error("----Error PengXin start clean {} server return {}", taskId, err.getMsg());
return new MulReturnType<>(err, null);
}
if (rspInfo == null || rspInfo.getCode() != ErrorCode.ERR_OK.getHttpCode()) {
log.error("----Error PengXin stop task{} server return error", taskId);
return new MulReturnType<>(ErrorCode.ERR_CALLDEVICE, null);
err = verifyRespStatus(rspInfo.getMsgContent().getItems().get(0).getStatus());
if (err != ErrorCode.ERR_OK) {
log.error("----Error PengXin start clean {} server return {}", taskId, err.getMsg());
return new MulReturnType<>(err, null);
}
if (rspInfo.getMsgContent().getItems().size() == 0) {
@ -269,7 +281,7 @@ public class CmhiAbilityImpl implements DisposeAbility {
return new MulReturnType<>(ErrorCode.ERR_PENGXIN_ERROR, null);
}
log.debug("----Finish PengXin Stop Cleanup Task: {}", taskId);
log.info("----Finish PengXin Stop Cleanup Task: {}", taskId);
return new MulReturnType<>(ErrorCode.ERR_OK, null);
}
} catch (Exception ex) {
@ -325,8 +337,8 @@ public class CmhiAbilityImpl implements DisposeAbility {
* Dev get link status.
*/
private void devGetLinkStatus() {
String url = urlRootPath + "dispose_device/information/linkstatus";
try {
String url = urlRootPath + "dispose_device/information/linkstatus";
if (token == null || token.length() == 0) {
deviceLinkStatus = false;
@ -347,7 +359,7 @@ public class CmhiAbilityImpl implements DisposeAbility {
return;
} catch (Exception ex) {
log.error(ex.getMessage());
log.error("Exception: {}, {}", url, ex.getMessage());
}
deviceLinkStatus = false;
@ -359,53 +371,53 @@ public class CmhiAbilityImpl implements DisposeAbility {
@Override
public void getDisposeDeviceProtectObject() {
if (token == null || token.length() == 0) {
return;
}
try {
String url = urlRootPath + "dispose_device/information/capacity";
ProtocolRespDTO<CmhiDisposeAbilityRsp> rspInfo = restfulInterface.getDeviceDisposeAbility(url, token);
// 判断是否token过期
if (rspInfo != null && reLogin(rspInfo, rspInfo.getMsgContent().getStatus())) {
rspInfo = restfulInterface.getDeviceDisposeAbility(url, token);
}
if (rspInfo != null && rspInfo.getCode() == ErrorCode.ERR_OK.getHttpCode()
&& rspInfo.getMsgContent().getStatus() == ErrorCode.ERR_OK.getCode()) {
rspInfo.getMsgContent().getCapacity().forEach(v -> {
if (v.getObjectType().equals(DisposeObjectType.DOMAIN.getValue())) {
log.error("Unsupported: {}", v.getObjectType());
} else if (v.getObjectType().equals(DisposeObjectType.URL.getValue())) {
log.error("Unsupported: {}", v.getObjectType());
} else {
synchronized (this) {
protectIpV4.clear();
protectIpV6.clear();
if (v.getIpType() == null || v.getIpType().equals(IpAddrType.IPV4_IPV6.getValue())) {
String [] allIpAddr = {null, "0.0.0.0", "0.0.0.0,::"};
if(Arrays.asList(allIpAddr).contains(v.getDisposeIp())) {
protectIpV4.add("");
protectIpV6.add("");
}
} else if (v.getIpType().equals(IpAddrType.IPV4.getValue())) {
protectIpV4.add("");
} else if (v.getIpType().equals(IpAddrType.IPV6.getValue())) {
protectIpV6.add("");
}
}
}
});
}
} catch (Exception ignored) {
}
// if (token == null || token.length() == 0) {
// return;
// }
//
// try {
// String url = urlRootPath + "dispose_device/information/capacity";
//
// ProtocolRespDTO<CmhiDisposeAbilityRsp> rspInfo = restfulInterface.getDeviceDisposeAbility(url, token);
//
// // 判断是否token过期
// if (rspInfo != null && reLogin(rspInfo, rspInfo.getMsgContent().getStatus())) {
// rspInfo = restfulInterface.getDeviceDisposeAbility(url, token);
// }
//
// if (rspInfo != null && rspInfo.getCode() == ErrorCode.ERR_OK.getHttpCode()
// && rspInfo.getMsgContent().getStatus() == ErrorCode.ERR_OK.getCode()) {
//
// rspInfo.getMsgContent().getCapacity().forEach(v -> {
// if (v.getObjectType().equals(DisposeObjectType.DOMAIN.getValue())) {
// log.error("Unsupported: {}", v.getObjectType());
// } else if (v.getObjectType().equals(DisposeObjectType.URL.getValue())) {
// log.error("Unsupported: {}", v.getObjectType());
// } else {
// synchronized (this) {
//
// protectIpV4.clear();
// protectIpV6.clear();
//
// if (v.getIpType() == null || v.getIpType().equals(IpAddrType.IPV4_IPV6.getValue())) {
// String [] allIpAddr = {null, "0.0.0.0", "0.0.0.0,::"};
//
// if(Arrays.asList(allIpAddr).contains(v.getDisposeIp())) {
// protectIpV4.add("");
// protectIpV6.add("");
// }
// } else if (v.getIpType().equals(IpAddrType.IPV4.getValue())) {
// protectIpV4.add("");
// } else if (v.getIpType().equals(IpAddrType.IPV6.getValue())) {
// protectIpV6.add("");
// }
// }
// }
// });
//
// }
// } catch (Exception ignored) {
// }
}
/**
@ -416,34 +428,35 @@ public class CmhiAbilityImpl implements DisposeAbility {
*/
@Override
public boolean isCarryProtectIp(String ipAddr) {
boolean ret = false;
IPAddress addr = new IPAddressString(ipAddr).getAddress();
synchronized (this) {
if (addr.isIPv4()) {
ret = protectIpV4.stream().anyMatch(v -> Helper.ipInRange(v, ipAddr));
}
if (addr.isIPv6()) {
ret = protectIpV6.stream().anyMatch(v -> Helper.ipInRange(v, ipAddr));
}
}
if (!ret) {
// 更新设备防护IP
getDisposeDeviceProtectObject();
synchronized (this) {
if (addr.isIPv4()) {
ret = protectIpV4.stream().anyMatch(v -> Helper.ipInRange(v, ipAddr));
}
if (addr.isIPv6()) {
ret = protectIpV6.stream().anyMatch(v -> Helper.ipInRange(v, ipAddr));
}
}
}
return ret;
return true;
// boolean ret = false;
// IPAddress addr = new IPAddressString(ipAddr).getAddress();
// synchronized (this) {
// if (addr.isIPv4()) {
// ret = protectIpV4.stream().anyMatch(v -> Helper.ipInRange(v, ipAddr));
// }
//
// if (addr.isIPv6()) {
// ret = protectIpV6.stream().anyMatch(v -> Helper.ipInRange(v, ipAddr));
// }
// }
//
// if (!ret) {
// // 更新设备防护IP
// getDisposeDeviceProtectObject();
//
// synchronized (this) {
// if (addr.isIPv4()) {
// ret = protectIpV4.stream().anyMatch(v -> Helper.ipInRange(v, ipAddr));
// }
//
// if (addr.isIPv6()) {
// ret = protectIpV6.stream().anyMatch(v -> Helper.ipInRange(v, ipAddr));
// }
// }
// }
//
// return ret;
}
/**
@ -455,6 +468,7 @@ public class CmhiAbilityImpl implements DisposeAbility {
@Override
public MulReturnType<ErrorCode, Long> taskStatus(String taskId) {
if (token == null || token.length() == 0) {
upgradeToken();
return new MulReturnType<>(ErrorCode.ERR_LOGOUT, -1L);
}
@ -464,23 +478,25 @@ public class CmhiAbilityImpl implements DisposeAbility {
ProtocolRespDTO<CmhiDisposeTaskStatusRsp> rspInfo = restfulInterface.getDeviceTaskStatus(url, token,
new String[]{taskId});
// 判断是否token过期
if (rspInfo != null && reLogin(rspInfo, rspInfo.getMsgContent().getItems().get(0).getStatus())) {
rspInfo = restfulInterface.getDeviceTaskStatus(url, token, new String[]{taskId});
ErrorCode err = verifyInterfaceResp(rspInfo);
if (err != ErrorCode.ERR_OK) {
log.error("----Error PengXin start clean {} server return {}", taskId, err.getMsg());
return new MulReturnType<>(err, -1L);
}
if (rspInfo != null && rspInfo.getCode() == ErrorCode.ERR_OK.getHttpCode()) {
int errCode = rspInfo.getMsgContent().getItems().get(0).getStatus();
if (rspInfo.getMsgContent().getItems().size() == 0 || errCode != ErrorCode.ERR_OK.getCode()) {
return new MulReturnType<>(ErrorCode.ERR_NOSUCHTASK, -1L);
err = verifyRespStatus(rspInfo.getMsgContent().getItems().get(0).getStatus());
if (err != ErrorCode.ERR_OK) {
log.error("----Error PengXin start clean {} server return {}", taskId, err.getMsg());
return new MulReturnType<>(err, -1L);
}
return new MulReturnType<>(ErrorCode.ERR_OK,
rspInfo.getMsgContent().getItems().get(0).getTaskStatus());
} else {
return new MulReturnType<>(ErrorCode.ERR_CALLDEVICE, -1L);
if (rspInfo.getMsgContent().getItems().size() == 0) {
return new MulReturnType<>(ErrorCode.ERR_NOSUCHTASK, -1L);
}
return new MulReturnType<>(ErrorCode.ERR_OK, rspInfo.getMsgContent().getItems().get(0).getTaskStatus());
} catch (Exception ignored) {
return new MulReturnType<>(ErrorCode.ERR_SYSTEMEXCEPTION, -1L);
}
@ -490,41 +506,70 @@ public class CmhiAbilityImpl implements DisposeAbility {
* Upgrade token.
*/
private void upgradeToken() {
String url = urlRootPath + "dispose_device/auth/login";
try {
String url = urlRootPath + "dispose_device/auth/login";
ProtocolRespDTO<CmhiDisposeLoginRsp> logInfo = restfulInterface.login(url, username, password);
if (logInfo != null && logInfo.getMsgContent().getStatus() == ErrorCode.ERR_OK.getCode()) {
this.token = logInfo.getMsgContent().getToken();
}
} catch (Exception ignored) {
} catch (Exception ex) {
log.error("Exception: {}, {}", url, ex.getMessage());
}
}
/**
* reLogin.
* Verify interface resp error code.
*
* @param <T> the type parameter
* @param rspInfo the rsp info
* @return the error code
*/
private <T> boolean reLogin(ProtocolRespDTO<T> rspInfo, int status) {
private <T> ErrorCode verifyInterfaceResp(ProtocolRespDTO<T> rspInfo) {
if (rspInfo == null) {
return ErrorCode.ERR_PENGXIN_ERROR;
} else if (rspInfo.getCode() == HttpServletResponse.SC_UNAUTHORIZED) {
upgradeToken();
return ErrorCode.ERR_TOKENTIMEOUT;
} else if (rspInfo.getCode() != HttpServletResponse.SC_OK) {
return ErrorCode.ERR_PENGXIN_ERROR;
}
if (rspInfo.getMsgContent() == null) {
return ErrorCode.ERR_PARAMS;
}
return ErrorCode.ERR_OK;
}
/**
* Verify resp status error code.
*
* @param status the status
* @return the error code
*/
private ErrorCode verifyRespStatus(int status) {
if (status == ErrorCode.ERR_LOGOUT.getCode()
|| status == ErrorCode.ERR_TOKENTIMEOUT.getCode()
|| status == ErrorCode.ERR_TOKENNOTFOUND.getCode()) {
upgradeToken();
return ErrorCode.ERR_TOKENTIMEOUT;
}
try {
// 判断是否token过期
if (rspInfo.getCode() == HttpServletResponse.SC_UNAUTHORIZED) {
// 重新登录获取 token
upgradeToken();
return true;
}
ErrorCode err = CommonEnumHandler.codeOf(ErrorCode.class, status);
if (rspInfo.getCode() == HttpServletResponse.SC_OK) {
if (status == ErrorCode.ERR_LOGOUT.getCode() ||
status == ErrorCode.ERR_TOKENTIMEOUT.getCode() ||
status == ErrorCode.ERR_TOKENNOTFOUND.getCode()) {
upgradeToken();
return true;
}
if (err == null) {
return ErrorCode.ERR_CALLDEVICE;
} else {
return err;
}
return false;
} catch (Exception ignored) {
return false;
if (status == ErrorCode.ERR_OK.getCode()) {
return ErrorCode.ERR_OK;
} else {
return ErrorCode.ERR_CALLDEVICE;
}
}
}
@ -533,17 +578,16 @@ public class CmhiAbilityImpl implements DisposeAbility {
*/
@Override
public void periodTaskRuntime() {
log.debug("++++PengXin Period Task Running");
// 集团一键处置线上环境不再定时获取设备能力信息
// 更新防护对象
getDisposeDeviceProtectObject();
//getDisposeDeviceProtectObject();
// 更新心跳状态
if (timerCnt++ % DisposeConfigValue.HEART_PERIOD_OF_SECOND == 0) {
devGetLinkStatus();
}
log.debug("----PengXin Period Task Running");
}
/**

View File

@ -15,8 +15,7 @@
*/
SET NAMES utf8mb4;
SET
FOREIGN_KEY_CHECKS = 0;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for device_task
@ -172,8 +171,7 @@ CREATE TABLE `user_account`
COLLATE = utf8_general_ci
ROW_FORMAT = DYNAMIC;
SET
FOREIGN_KEY_CHECKS = 1;
SET FOREIGN_KEY_CHECKS = 1;
-- ----------------------------
-- Table structure for msg_serial
@ -186,6 +184,7 @@ CREATE TABLE `msg_serial`
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=50 DEFAULT CHARSET=utf8;
SET FOREIGN_KEY_CHECKS = 1;
-- ----------------------------
-- Table structure for alarm_information
@ -199,3 +198,5 @@ CREATE TABLE `alarm_information`
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
SET FOREIGN_KEY_CHECKS = 1;

View File

@ -1,10 +1,7 @@
package com.dispose.test.dev.debug;
<<<<<<< HEAD
import com.fasterxml.jackson.core.type.TypeReference;
=======
import cn.hutool.json.JSONObject;
>>>>>>> origin/v2.0.9_dev
import com.cmhi.dispose.common.CmhiDisposeStartTaskRsp;
import com.dispose.common.ConstValue;
import com.dispose.common.DDoSAttackType;
import com.dispose.common.DisposeConfigValue;
@ -17,9 +14,9 @@ import com.dispose.pojo.dto.protocol.base.ProtocolRespDTO;
import com.dispose.pojo.entity.DisposeDevice;
import com.dispose.security.arithmetic.CryptoHelper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.pengxin.dispose.common.PengXinStartTaskRsp;
import inet.ipaddr.IPAddress;
import inet.ipaddr.IPAddressString;
import lombok.extern.slf4j.Slf4j;
@ -80,6 +77,13 @@ public class demo {
// }
//
private static <T> Type createRespType(Class<T> c) {
Type[] types = new Type[1];
types[0] = c;
return ParameterizedTypeImpl.make(ProtocolRespDTO.class, types,
ProtocolRespDTO.class.getDeclaringClass());
}
/**
* A 2 ip range test.
*/
@ -94,13 +98,6 @@ public class demo {
Assert.assertTrue(Helper.ipInRange("::", "::abcd"));
}
private static <T> Type createRespType(Class<T> c) {
Type[] types = new Type[1];
types[0] = c;
return ParameterizedTypeImpl.make(ProtocolRespDTO.class, types,
ProtocolRespDTO.class.getDeclaringClass());
}
@Test
public void json_test() {
ObjectMapper OBJECT_MAPPER = new ObjectMapper();
@ -108,13 +105,13 @@ public class demo {
String json = "{\"ver\":1,\"cryptoType\":0,\"timeStamp\":1634780505248,\"code\":200,\"msgContent\":{\"deviceName\":\"okp\",\"items\":[{\"taskReqId\":\"1634780428667\"," +
"\"taskId\":\"c285c206-0e4e-45f5-beab-9ac21eaf4769\",\"status\":0,\"message\":\"SUCCESS\",\"taskInfo\":\"192.168.123.126\"}]}}";
try {
ProtocolRespDTO<PengXinStartTaskRsp> rspInfo = OBJECT_MAPPER.readValue(json,
new TypeReference<ProtocolRespDTO<PengXinStartTaskRsp>>() {
@Override
public Type getType() {
return createRespType(PengXinStartTaskRsp.class);
}
});
ProtocolRespDTO<CmhiDisposeStartTaskRsp> rspInfo = OBJECT_MAPPER.readValue(json,
new TypeReference<ProtocolRespDTO<CmhiDisposeStartTaskRsp>>() {
@Override
public Type getType() {
return createRespType(CmhiDisposeStartTaskRsp.class);
}
});
log.info("Ret: {}", rspInfo);
} catch (Exception ex) {
@ -203,8 +200,8 @@ public class demo {
.build();
log.debug(new ObjectMapper()
.writerWithDefaultPrettyPrinter()
.writeValueAsString(upgradeDisposeDeviceProperties(dev, dev2)));
.writerWithDefaultPrettyPrinter()
.writeValueAsString(upgradeDisposeDeviceProperties(dev, dev2)));
}
/**
@ -487,8 +484,8 @@ public class demo {
for (String n : src) {
log.info("{} network {}contains {}", n,
Helper.ipInRange(n, ip) ? "is " : "not ",
ip);
Helper.ipInRange(n, ip) ? "is " : "not ",
ip);
}
}
@ -500,8 +497,8 @@ public class demo {
String n = "0.0.0.0";
log.info("{} network {}contains {}", n,
Helper.ipInRange(n, ip) ? "is " : "not ",
ip);
Helper.ipInRange(n, ip) ? "is " : "not ",
ip);
}
/**
@ -618,7 +615,7 @@ public class demo {
String aesKey = "hkoUV5ZWh0q1jSxMnpjovVn19Qg99HY6DD40";
byte[] encode = CryptoHelper.aes128Encryption(plainText.getBytes(StandardCharsets.UTF_8),
aesKey);
aesKey);
log.info("AES Key: {}", aesKey);
log.info("Src: {}", plainText);
log.info("AES256: {}", CryptoHelper.base64Encryption(encode));