diff --git a/src/main/java/com/dispose/ability/DisposeAbility.java b/src/main/java/com/dispose/ability/DisposeAbility.java index 4964d101..6d90459d 100644 --- a/src/main/java/com/dispose/ability/DisposeAbility.java +++ b/src/main/java/com/dispose/ability/DisposeAbility.java @@ -21,9 +21,8 @@ public interface DisposeAbility { * @param urlPath the url path * @param username the username * @param password the password - * @return the error code */ - ErrorCode initDeviceEnv(String urlPath, String username, String password); + void initDeviceEnv(String urlPath, String username, String password); /** * Run dispose mul return type. @@ -43,11 +42,21 @@ public interface DisposeAbility { /** * Stop dispose mul return type. * - * @param ip the ip - * @param capType the cap type + * @param ip the ip + * @param capType the cap type + * @param nfDirection the nf direction + * @param attackType the attack type * @return the mul return type */ - MulReturnType stopDispose(String ip, DisposeCapacityType capType); + MulReturnType stopDispose(String ip, DisposeCapacityType capType, + @Nullable NetflowDirection[] nfDirection, + @Nullable DDoSAttackType[] attackType); + /** + * Gets device link status. + * + * @return the device link status + */ + boolean getDeviceLinkStatus(); } diff --git a/src/main/java/com/dispose/ability/impl/DpTechAbilityImpl.java b/src/main/java/com/dispose/ability/impl/DpTechAbilityImpl.java index 947f8c5a..113a28e4 100644 --- a/src/main/java/com/dispose/ability/impl/DpTechAbilityImpl.java +++ b/src/main/java/com/dispose/ability/impl/DpTechAbilityImpl.java @@ -39,16 +39,26 @@ import java.util.stream.Collectors; @Slf4j public class DpTechAbilityImpl implements DisposeAbility { + /** + * The Clean type port. + */ private AbnormalFlowCleaningServicePortType cleanTypePort; + /** + * Init device env error code. + * + * @param urlPath the url path + * @param username the username + * @param password the password + */ @Override - public ErrorCode initDeviceEnv(String urlPath, String username, String password) { + public void initDeviceEnv(String urlPath, String username, String password) { JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean(); jaxWsProxyFactoryBean.setServiceClass(AbnormalFlowCleaningServicePortType.class); jaxWsProxyFactoryBean.setAddress(urlPath); //WS-Security Head - Map outProps = new HashMap<>(); + Map outProps = new HashMap<>(16); outProps.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN); // 配置用户名,密码类型 @@ -61,8 +71,8 @@ public class DpTechAbilityImpl implements DisposeAbility { this.cleanTypePort = (AbnormalFlowCleaningServicePortType) jaxWsProxyFactoryBean.create(); Map ctx = ((BindingProvider) this.cleanTypePort).getRequestContext(); - ctx.put("password", username); - ctx.put("username", password); + ctx.put("password", password); + ctx.put("username", username); // 配置连接,访问超时时间 Client proxy = ClientProxy.getClient(this.cleanTypePort); @@ -73,15 +83,16 @@ public class DpTechAbilityImpl implements DisposeAbility { //读取超时 policy.setReceiveTimeout(DpTechConfigValue.SOAP_RECEIVE_TIMEOUT_SECOND); conduit.setClient(policy); - - return ErrorCode.ERR_OK; } /** * Run dispose mul return type. * - * @param ip the ip - * @param capType the cap type + * @param ip the ip + * @param capType the cap type + * @param nfDirection the nf direction + * @param attackType the attack type + * @param duration the duration * @return the mul return type */ @Override @@ -119,7 +130,6 @@ public class DpTechAbilityImpl implements DisposeAbility { } } }))); - log.info("----Finish DPTech Start Cleanup Task: {}", ip); } catch (Exception ex) { log.error(ex.getMessage()); @@ -133,12 +143,65 @@ public class DpTechAbilityImpl implements DisposeAbility { /** * Stop dispose mul return type. * - * @param ip the ip - * @param capType the cap type + * @param ipAddr the ip addr + * @param capType the cap type + * @param nfDirection the nf direction + * @param attackType the attack type * @return the mul return type */ @Override - public MulReturnType stopDispose(String ip, DisposeCapacityType capType) { - return null; + public MulReturnType stopDispose(String ipAddr, DisposeCapacityType capType, + @Nullable NetflowDirection[] nfDirection, + @Nullable DDoSAttackType[] attackType) { + ErrorCode err = ErrorCode.ERR_OK; + + try { + log.info("++++Begging DPTech Stop Cleanup Task: {}", ipAddr); + + // 查找需要处置的流量方向集合 + List dirList = Arrays.stream(NetflowDirection.values()) + .filter(d -> nfDirection == null || nfDirection.length == 0 || Arrays.asList(nfDirection).contains(d)) + .collect(Collectors.toList()); + + // 查找需要处理的攻击类型集合 + List typeList = Arrays.stream(DDoSAttackType.values()) + .filter(t -> attackType == null || attackType.length == 0 || Arrays.asList(attackType).contains(t)) + .map(t -> CommonEnumHandler.codeOf(DpTechAttackType.class, + DpTechAttackType.fromDdosAttackTypeValue(t))) + .collect(Collectors.toList()); + + dirList.forEach(d -> typeList.forEach(t -> CompletableFuture.supplyAsync(() -> + cleanTypePort.stopAbnormalTaskForUMC(ipAddr, t.getValue(), d.getCode())) + .whenComplete((v, ex) -> { + if (ex != null) { + log.error("DPTech stop dispose: {}, {}, error:{}", ipAddr, t.getValue(), ex.getMessage()); + } else { + log.debug("Stop Cleanup: {} --> {}:{}", d, t.getDescription(), t.getValue()); + if(v.getResultRetVal() != ErrorCode.ERR_OK.getCode()) { + log.error("DPTech stop dispose {} error: {}", ipAddr, v.getResultInfo()); + } + } + }))); + log.info("----Finish DPTech Stop Cleanup Task: {}", ipAddr); + } catch (Exception ex) { + log.error(ex.getMessage()); + log.error("----Error DPTech Stop Cleanup Task: {}", ipAddr); + err = ErrorCode.ERR_SYSTEMEXCEPTION; + } + + return new MulReturnType<>(err, null); + } + + @Override + public boolean getDeviceLinkStatus() { + try { + // 获取防护对象接口调用成功认为设备心跳正常 + cleanTypePort.getAllProtectionObjectFromUMC().getProtectionObjectDataForService(); + return true; + } catch (Exception ex) { + log.error(ex.getMessage()); + } + + return false; } } diff --git a/src/main/java/com/dispose/common/DDoSAttackType.java b/src/main/java/com/dispose/common/DDoSAttackType.java index 8af1c23f..cec55456 100644 --- a/src/main/java/com/dispose/common/DDoSAttackType.java +++ b/src/main/java/com/dispose/common/DDoSAttackType.java @@ -55,6 +55,8 @@ public enum DDoSAttackType implements BaseEnum { */ HOST_TOTAL_TRAFFIC(11, "Host Total Traffic"); + + /** * The Code. */ diff --git a/src/main/java/com/dispose/manager/DisposeDeviceManager.java b/src/main/java/com/dispose/manager/DisposeDeviceManager.java index 7c40709a..108eb3a3 100644 --- a/src/main/java/com/dispose/manager/DisposeDeviceManager.java +++ b/src/main/java/com/dispose/manager/DisposeDeviceManager.java @@ -40,12 +40,19 @@ public interface DisposeDeviceManager { MulReturnType changeDisposeDeviceStatus(Long id, ObjectStatus status); /** - * Gets all devices. + * Gets pages of devices. * * @param startPage the start page * @param pageSize the page size - * @return the all devices + * @return the pages of devices */ - MulReturnType, List> getAllDevices(Integer startPage, - Integer pageSize); + MulReturnType, List> getPagesOfDevices(Integer startPage, + Integer pageSize); + + /** + * Gets all dispose devices. + * + * @return the all dispose devices + */ + List getAllDisposeDevices(); } diff --git a/src/main/java/com/dispose/manager/impl/DisposeDeviceManagerImpl.java b/src/main/java/com/dispose/manager/impl/DisposeDeviceManagerImpl.java index 6f004965..78268790 100644 --- a/src/main/java/com/dispose/manager/impl/DisposeDeviceManagerImpl.java +++ b/src/main/java/com/dispose/manager/impl/DisposeDeviceManagerImpl.java @@ -205,15 +205,15 @@ public class DisposeDeviceManagerImpl implements DisposeDeviceManager { } /** - * Gets all devices. + * Gets pages of devices. * * @param startPage the start page * @param pageSize the page size - * @return the all devices + * @return the pages of devices */ @Override - public MulReturnType, List> getAllDevices(Integer startPage, - Integer pageSize) { + public MulReturnType, List> getPagesOfDevices(Integer startPage, + Integer pageSize) { // 设置分页信息 PageHelper.startPage(startPage, pageSize); @@ -229,4 +229,20 @@ public class DisposeDeviceManagerImpl implements DisposeDeviceManager { return new MulReturnType<>(pageInfo, devList); } + + /** + * Gets all dispose devices. + * + * @return the all dispose devices + */ + @Override + public List getAllDisposeDevices() { + List devList = disposeDeviceMapper.selectAll(); + + if(devList == null) { + devList = new ArrayList<>(); + } + + return devList; + } } diff --git a/src/main/java/com/dispose/service/DisposeAbilityRouterService.java b/src/main/java/com/dispose/service/DisposeAbilityRouterService.java new file mode 100644 index 00000000..e5b1fd46 --- /dev/null +++ b/src/main/java/com/dispose/service/DisposeAbilityRouterService.java @@ -0,0 +1,9 @@ +package com.dispose.service; + +/** + * The interface Dispose ability router service. + * + * @author + */ +public interface DisposeAbilityRouterService { +} diff --git a/src/main/java/com/dispose/service/impl/DisposeAbilityRouterServiceImpl.java b/src/main/java/com/dispose/service/impl/DisposeAbilityRouterServiceImpl.java new file mode 100644 index 00000000..c59b06ec --- /dev/null +++ b/src/main/java/com/dispose/service/impl/DisposeAbilityRouterServiceImpl.java @@ -0,0 +1,67 @@ +package com.dispose.service.impl; + +import com.dispose.ability.DisposeAbility; +import com.dispose.ability.impl.DpTechAbilityImpl; +import com.dispose.common.HttpType; +import com.dispose.manager.DisposeDeviceManager; +import com.dispose.pojo.entity.DisposeDevice; +import com.dispose.service.DisposeAbilityRouterService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; + +import javax.annotation.PostConstruct; +import javax.annotation.Resource; +import java.util.List; +import java.util.concurrent.ConcurrentHashMap; + +/** + * The type Dispose ability router service. + * + * @author + */ +@Service +@Slf4j +public class DisposeAbilityRouterServiceImpl implements DisposeAbilityRouterService { + /** + * The Dispose ability map. + */ + private ConcurrentHashMap disposeAbilityMap = new ConcurrentHashMap<>(); + + /** + * The Dispose device manager. + */ + @Resource + private DisposeDeviceManager disposeDeviceManager; + + /** + * Init dispose ability. + */ + @PostConstruct + public void initDisposeAbility() { + List devList = disposeDeviceManager.getAllDisposeDevices(); + + devList.forEach(v -> { + DisposeAbility db; + + String httpType = v.getUrlType() == HttpType.HTTP ? "http://" : "https://"; + String addr = v.getIpPort() == null || v.getIpPort().length() == 0 ? v.getIpAddr() : + v.getIpAddr() + ":" + v.getIpPort(); + + String url = httpType + addr + "/" + v.getUrlPath(); + + switch (v.getDeviceType()) { + case DPTECH_UMC: + db = new DpTechAbilityImpl(); + db.initDeviceEnv(url, v.getUserName(), v.getPassword()); + disposeAbilityMap.put(addr, db); + break; + case HAOHAN_PLATFORM: + break; + case VIRTUAL_DISPOSE: + break; + default: + break; + } + }); + } +} diff --git a/src/main/java/com/dispose/service/impl/DisposeDeviceManagerServiceImpl.java b/src/main/java/com/dispose/service/impl/DisposeDeviceManagerServiceImpl.java index 47009f96..08f7966f 100644 --- a/src/main/java/com/dispose/service/impl/DisposeDeviceManagerServiceImpl.java +++ b/src/main/java/com/dispose/service/impl/DisposeDeviceManagerServiceImpl.java @@ -110,6 +110,6 @@ public class DisposeDeviceManagerServiceImpl implements DisposeDeviceManagerServ @Override public MulReturnType, List> getAllDisposeDevice(Integer startPage, Integer pageSize) { - return disposeDeviceManager.getAllDevices(startPage, pageSize); + return disposeDeviceManager.getPagesOfDevices(startPage, pageSize); } } diff --git a/src/main/java/com/dispose/task/DeviceManagerTask.java b/src/main/java/com/dispose/task/DeviceManagerTask.java new file mode 100644 index 00000000..5e896f54 --- /dev/null +++ b/src/main/java/com/dispose/task/DeviceManagerTask.java @@ -0,0 +1,41 @@ +package com.dispose.task; + +import com.dispose.ability.DisposeAbility; +import com.dispose.manager.DisposeDeviceManager; +import lombok.extern.slf4j.Slf4j; +import org.springframework.scheduling.annotation.Async; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +import javax.annotation.Resource; + +/** + * The type Device manager task. + * + * @author + */ +@Component +@Slf4j +public class DeviceManagerTask { + @Resource + private DisposeDeviceManager disposeDeviceManager; + + @Resource + private DisposeAbility disposeAbility; + + /** + * Thread pool task. + */ + @Async("bizExecutor") + @Scheduled(cron = "0/5 * * * * ?") + public void threadPoolTask() { + log.info("task run......... {}", disposeAbility.getDeviceLinkStatus()); + } + + @PostConstruct + public void init() { + disposeAbility.initDeviceEnv("http://10.88.77.15/UMC/service/AbnormalFlowCleaningService", + "admin", "UMCAdministrator"); + } +} diff --git a/src/main/java/com/dispose/thread/ThreadPoolConfig.java b/src/main/java/com/dispose/thread/ThreadPoolConfig.java new file mode 100644 index 00000000..d1c8c43c --- /dev/null +++ b/src/main/java/com/dispose/thread/ThreadPoolConfig.java @@ -0,0 +1,29 @@ +package com.dispose.thread; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; + +import java.util.concurrent.Executor; + +/** + * The type Thread pool config. + * + * @author + */ +@Configuration +public class ThreadPoolConfig { + /** + * Biz executor executor. + * + * @return the executor + */ + @Bean + public Executor bizExecutor() { + ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); + executor.setThreadNamePrefix("myBiz-"); + executor.setMaxPoolSize(Runtime.getRuntime().availableProcessors() << 1 + 1); + executor.setCorePoolSize(Runtime.getRuntime().availableProcessors()); + return executor; + } +}