This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
k18-ntcs-web-ntc/src/main/java/com/nis/web/service/configuration/InterceptCfgService.java

413 lines
16 KiB
Java
Raw Normal View History

2018-08-21 13:38:26 +08:00
package com.nis.web.service.configuration;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.nis.domain.Page;
import com.nis.domain.configuration.AreaIpCfg;
import com.nis.domain.configuration.BaseStringCfg;
2018-08-21 13:38:26 +08:00
import com.nis.domain.configuration.CfgIndexInfo;
import com.nis.domain.configuration.HttpUrlCfg;
import com.nis.domain.configuration.InterceptPktBin;
import com.nis.domain.configuration.IpPortCfg;
import com.nis.domain.maat.MaatCfg;
import com.nis.domain.maat.MaatCfg.NumBoundaryCfg;
import com.nis.domain.maat.ToMaatBean;
import com.nis.domain.maat.ToMaatResult;
import com.nis.domain.maat.MaatCfg.DigestCfg;
import com.nis.domain.maat.MaatCfg.GroupCfg;
import com.nis.domain.maat.MaatCfg.IpCfg;
import com.nis.domain.maat.MaatCfg.StringCfg;
import com.nis.exceptions.MaatConvertException;
import com.nis.util.ConfigServiceUtil;
import com.nis.util.Constants;
import com.nis.util.StringUtil;
import com.nis.web.dao.configuration.AreaIpCfgDao;
import com.nis.web.dao.configuration.InterceptCfgDao;
import com.nis.web.dao.configuration.WebsiteCfgDao;
import com.nis.web.security.UserUtils;
import com.nis.web.service.BaseService;
import com.nis.web.service.CrudService;
/**
* 网站相关配置事务类
* @author dell
*
*/
@Service
public class InterceptCfgService extends CrudService<WebsiteCfgDao,CfgIndexInfo> {
@Autowired
protected WebsiteCfgDao websiteCfgDao;
@Autowired
protected InterceptCfgDao interceptCfgDao;
@Autowired
protected AreaIpCfgDao areaIpCfgDao;
2018-08-23 15:21:50 +08:00
public CfgIndexInfo getInterceptCfg(Long cfgId){
2018-08-21 13:38:26 +08:00
CfgIndexInfo entity = websiteCfgDao.getCfgIndexInfo(cfgId);
List<IpPortCfg> ipPortList = websiteCfgDao.getIpPortList(entity);
List<InterceptPktBin> pktBinList = interceptCfgDao.getInterceptPktBin(entity);
2018-08-23 15:21:50 +08:00
List<HttpUrlCfg> httpUrlList = websiteCfgDao.getHttpUrlList(entity);
2018-08-21 13:38:26 +08:00
entity.setIpPortList(ipPortList);
entity.setInterceptPktBinList(pktBinList);
2018-08-23 15:21:50 +08:00
entity.setHttpUrlList(httpUrlList);
2018-08-21 13:38:26 +08:00
return entity;
}
public Page<CfgIndexInfo> getWebsiteList(Page<CfgIndexInfo> page, CfgIndexInfo entity){
// 生成数据权限过滤条件dsf为dataScopeFilter的简写在xml中使用 ${sqlMap.dsf}调用权限SQL
entity.getSqlMap().put("dsf", configScopeFilter(entity.getCurrentUser(),"a"));
entity.setPage(page);
List<CfgIndexInfo> list = websiteCfgDao.getWebsiteList(entity);
page.setList(list);
return page;
}
/**
* 保存dns配置
* @param entity
*/
2018-08-23 15:21:50 +08:00
public void saveInterceptCfg(CfgIndexInfo entity){
if(!entity.getAction().equals(Constants.REPLACE_ACTION)){
entity.setInterceptPktBinList(null);
}
entity.setIsValid(0);
entity.setIsAudit(0);
2018-08-21 13:38:26 +08:00
//设置区域运营商信息
setAreaEffectiveIds(entity);
if(entity.getCfgId()==null){
Integer compileId = 0;
try {
List<Integer> idList = ConfigServiceUtil.getId(1, 1);
if(idList!=null && idList.size()>0){
compileId = idList.get(0);
}
} catch (Exception e) {
e.printStackTrace();
logger.info("获取编译ID出错");
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+e.getMessage());
}
2018-08-23 15:21:50 +08:00
entity.setCompileId(compileId);
entity.setCreateTime(new Date());
entity.setCreatorId(entity.getCurrentUser().getId());
websiteCfgDao.saveCfgIndex(entity);
if(entity.getIpPortList()!=null){
for(IpPortCfg cfg:entity.getIpPortList()){
BeanUtils.copyProperties(entity, cfg,new String[]{"cfgRegionCode","cfgType"});
websiteCfgDao.saveIpPortCfg(cfg);
2018-08-21 13:38:26 +08:00
}
2018-08-23 15:21:50 +08:00
}
if(entity.getInterceptPktBinList()!=null ){
for(InterceptPktBin cfg:entity.getInterceptPktBinList()){
if(StringUtils.isNotBlank(cfg.getCfgKeywords())){
BeanUtils.copyProperties(entity, cfg,new String[]{"cfgRegionCode","cfgType"});
interceptCfgDao.saveInterceptPktBin(cfg);
2018-08-21 13:38:26 +08:00
}
}
2018-08-23 15:21:50 +08:00
}
if(entity.getHttpUrlList()!=null ){
for(HttpUrlCfg cfg:entity.getHttpUrlList()){
if(StringUtils.isNotBlank(cfg.getCfgKeywords())){
2018-08-21 13:38:26 +08:00
BeanUtils.copyProperties(entity, cfg,new String[]{"cfgRegionCode","cfgType"});
2018-08-23 15:21:50 +08:00
websiteCfgDao.saveHttpUrlCfg(cfg);
2018-08-21 13:38:26 +08:00
}
}
2018-08-23 15:21:50 +08:00
}
//保存区域IP信息
if(entity.getAreaCfg()!=null){
for(AreaIpCfg cfg:entity.getAreaCfg()){
cfg.initDefaultValue();
BeanUtils.copyProperties(entity, cfg,new String[]{"cfgRegionCode","cfgType"});
areaIpCfgDao.saveAreaIpCfg(cfg);
}
}
2018-08-21 13:38:26 +08:00
}else{
entity.setEditTime(new Date());
entity.setEditorId(entity.getCurrentUser().getId());
websiteCfgDao.updateCfgIndex(entity);
//无效子配置后,再新增子配置
interceptCfgDao.deleteInterceptIpCfg(entity);
interceptCfgDao.deleteInterceptPktBin(entity);
2018-08-23 15:21:50 +08:00
websiteCfgDao.deleteHttpUrlCfg(entity);
2018-08-21 13:38:26 +08:00
AreaIpCfg area = new AreaIpCfg();
area.setCompileId(entity.getCompileId());
area.setFunctionId(entity.getFunctionId());
areaIpCfgDao.deleteAreaIpCfg(area);
entity.setCreateTime(new Date());
entity.setCreatorId(entity.getCurrentUser().getId());
if(entity.getIpPortList()!=null){
for(IpPortCfg cfg:entity.getIpPortList()){
BeanUtils.copyProperties(entity, cfg,new String[]{"cfgRegionCode","cfgType"});
websiteCfgDao.saveIpPortCfg(cfg);
}
}
if(entity.getInterceptPktBinList()!=null){
for(InterceptPktBin cfg:entity.getInterceptPktBinList()){
if(StringUtils.isNotBlank(cfg.getCfgKeywords())){
BeanUtils.copyProperties(entity, cfg,new String[]{"cfgRegionCode","cfgType"});
interceptCfgDao.saveInterceptPktBin(cfg);
2018-08-21 13:38:26 +08:00
}
}
}
2018-08-23 15:21:50 +08:00
if(entity.getHttpUrlList()!=null){
for(HttpUrlCfg cfg:entity.getHttpUrlList()){
if(StringUtils.isNotBlank(cfg.getCfgKeywords())){
BeanUtils.copyProperties(entity, cfg,new String[]{"cfgRegionCode","cfgType"});
websiteCfgDao.saveHttpUrlCfg(cfg);
}
}
}
2018-08-21 13:38:26 +08:00
//保存区域IP信息
if(entity.getAreaCfg()!=null){
for(AreaIpCfg cfg:entity.getAreaCfg()){
cfg.initDefaultValue();
BeanUtils.copyProperties(entity, cfg,new String[]{"cfgDesc","cfgRegionCode","cfgType"});
areaIpCfgDao.saveAreaIpCfg(cfg);
}
}
}
}
/**
2018-08-21 13:38:26 +08:00
* dns配置删除
* @param isValid
* @param ids
* @param functionId
*/
public void updatInterceptValid(Integer isValid,String ids,Integer functionId){
2018-08-21 13:38:26 +08:00
String[] idArray = ids.split(",");
for(String id :idArray){
CfgIndexInfo entity = new CfgIndexInfo();
2018-08-21 13:38:26 +08:00
entity.setCfgId(Long.parseLong(id));
entity.setIsValid(isValid);
entity.setEditorId(UserUtils.getUser().getId());
entity.setEditTime(new Date());
entity.setTableName(CfgIndexInfo.getTablename());
entity.setFunctionId(functionId);
websiteCfgDao.updateCfgValid(entity);
//查询子配置
2018-08-23 15:21:50 +08:00
entity = this.getInterceptCfg(Long.parseLong(id));
2018-08-21 13:38:26 +08:00
if(entity.getIpPortList()!=null && entity.getIpPortList().size()>0){
IpPortCfg cfg = new IpPortCfg();
BeanUtils.copyProperties(entity, cfg, new String[]{"cfgId"});
cfg.setTableName(IpPortCfg.getTablename());
websiteCfgDao.updateCfgValid(cfg);
}
if(entity.getInterceptPktBinList()!=null && entity.getInterceptPktBinList().size()>0)
2018-08-21 13:38:26 +08:00
{
InterceptPktBin cfg = new InterceptPktBin();
2018-08-21 13:38:26 +08:00
BeanUtils.copyProperties(entity, cfg, new String[]{"cfgId"});
cfg.setTableName(InterceptPktBin.getTablename());
2018-08-21 13:38:26 +08:00
websiteCfgDao.updateCfgValid(cfg);
}
2018-08-23 15:21:50 +08:00
if(entity.getHttpUrlList()!=null && entity.getHttpUrlList().size()>0)
{
HttpUrlCfg cfg = new HttpUrlCfg();
BeanUtils.copyProperties(entity, cfg, new String[]{"cfgId"});
cfg.setTableName(HttpUrlCfg.getTablename());
websiteCfgDao.updateCfgValid(cfg);
}
2018-08-21 13:38:26 +08:00
//保存区域IP信息
if(entity.getAreaCfg()!=null && entity.getAreaCfg().size()>0){
AreaIpCfg cfg = new AreaIpCfg();
BeanUtils.copyProperties(entity, cfg, new String[]{"cfgId"});
cfg.setTableName(AreaIpCfg.getTablename());
websiteCfgDao.updateCfgValid(cfg);
}
}
}
/**
2018-08-23 15:21:50 +08:00
* 配置审核
2018-08-21 13:38:26 +08:00
* @param entity
* @param isAudit
* @throws MaatConvertException
*/
public void auditInterceptIpCfg(CfgIndexInfo entity,Integer isAudit) throws MaatConvertException{
2018-08-21 13:38:26 +08:00
//修改数据库审核状态信息
entity.setTableName(CfgIndexInfo.getTablename());
websiteCfgDao.auditCfg(entity);
ToMaatBean maatBean = new ToMaatBean();
MaatCfg maatCfg = new MaatCfg();
List<MaatCfg> configCompileList = new ArrayList();
List<GroupCfg> groupRelationList = new ArrayList();
List<IpCfg> ipRegionList = new ArrayList();
List<StringCfg> strRegionList = new ArrayList();
List<NumBoundaryCfg> numRegionList = new ArrayList();
List<DigestCfg> digestRegionList = new ArrayList();
List<IpCfg> areaIpRegionList = new ArrayList();
//查询子配置并修改审核状态
2018-08-23 15:21:50 +08:00
entity = this.getInterceptCfg(entity.getCfgId());
2018-08-21 13:38:26 +08:00
if(entity.getIpPortList()!=null && entity.getIpPortList().size()>0){
IpPortCfg cfg = new IpPortCfg();
BeanUtils.copyProperties(entity, cfg, new String[]{"cfgId"});
cfg.setTableName(IpPortCfg.getTablename());
websiteCfgDao.auditCfg(cfg);
if(isAudit==1){
Map<String,List> map = cfgConvert(ipRegionList,entity.getIpPortList(),1,entity,groupRelationList);
groupRelationList=map.get("groupList");
ipRegionList=map.get("dstList");
if(map.get("numRegionList")!=null){
numRegionList.addAll(map.get("numRegionList"));
}
}
}
if(entity.getInterceptPktBinList()!=null && entity.getInterceptPktBinList().size()>0){
InterceptPktBin cfg = new InterceptPktBin();
2018-08-21 13:38:26 +08:00
BeanUtils.copyProperties(entity, cfg, new String[]{"cfgId"});
cfg.setTableName(InterceptPktBin.getTablename());
2018-08-21 13:38:26 +08:00
websiteCfgDao.auditCfg(cfg);
if(isAudit==1){
Map<String,List> map = cfgConvert(strRegionList,entity.getInterceptPktBinList(),2,entity,groupRelationList);
2018-08-21 13:38:26 +08:00
groupRelationList=map.get("groupList");
strRegionList=map.get("dstList");
}
}
//域名信息
boolean doaminFlag=false;
String domainUserRegion="";
2018-08-23 15:21:50 +08:00
if(entity.getHttpUrlList()!=null && entity.getHttpUrlList().size()>0){
doaminFlag=true;
domainUserRegion=Constants.USERREGION_DOMAIN_ID+"="+entity.getCompileId();
String domainStr="";
if(entity != null && entity.getHttpUrlList() != null && entity.getHttpUrlList().size() > 0){
BaseStringCfg httpUrl= entity.getHttpUrlList().get(0);
domainStr=httpUrl.getCfgKeywords();
}
//entity.getHttpUrlList().get(0).getCfgKeywords();
domainUserRegion=domainUserRegion+";"+Constants.USERREGION_DOMAIN_STR+"="+domainStr;
2018-08-23 15:21:50 +08:00
HttpUrlCfg cfg = new HttpUrlCfg();
BeanUtils.copyProperties(entity, cfg, new String[]{"cfgId"});
cfg.setTableName(HttpUrlCfg.getTablename());
websiteCfgDao.auditCfg(cfg);
if(isAudit==1){
Map<String,List> map = cfgConvert(strRegionList,entity.getHttpUrlList(),2,entity,groupRelationList);
groupRelationList=map.get("groupList");
strRegionList=map.get("dstList");
}
}
2018-08-21 13:38:26 +08:00
//保存区域IP信息
List<AreaIpCfg> areaIpCfgList=areaIpCfgDao.getByCompileId(entity.getCompileId());
if(!StringUtil.isEmpty(areaIpCfgList)){
AreaIpCfg cfg = new AreaIpCfg();
BeanUtils.copyProperties(entity, cfg, new String[]{"cfgId"});
cfg.setTableName(AreaIpCfg.getTablename());
websiteCfgDao.auditCfg(cfg);
if(isAudit==1){
Map<String,List> map = cfgConvert(areaIpRegionList,areaIpCfgList,1,entity,groupRelationList);
groupRelationList=map.get("groupList");
areaIpRegionList=map.get("dstList");
}
}
//构造提交综合服务参数格式,一条配置提交一次综合服务
if(isAudit==1){
maatCfg.initDefaultValue();
BeanUtils.copyProperties(entity, maatCfg);
maatCfg.setAction(entity.getAction());
maatCfg.setAuditTime(entity.getAuditTime());
maatCfg.setIpRegionList(ipRegionList);
maatCfg.setStrRegionList(strRegionList);
maatCfg.setNumRegionList(numRegionList);
maatCfg.setDigestRegionList(digestRegionList);
maatCfg.setGroupRelationList(groupRelationList);
maatCfg.setGroupNum(groupRelationList.size());
maatCfg.setAreaIpRegionList(areaIpRegionList);
maatCfg.setIsValid(entity.getIsValid());
//监测需要发keyring_id
if(entity.getAction().equals(Constants.MONIT_ACTION)){
entity.setUserRegion1(StringUtil.isEmpty(entity.getUserRegion1()) ? "0":entity.getUserRegion1());
maatCfg.setUserRegion(Constants.INTERCEPT_IP_MONIT_USER_REGION_KEY+"="+entity.getUserRegion1());
2018-08-21 13:38:26 +08:00
}
//限速需要发Droprate=0.001 暂不支持Bandwidth=200kbps
if(entity.getAction().equals(Constants.RATELIMIT_ACTION)){
if(entity.getUserRegion1().equals("0")){//丢包率
entity.setUserRegion2(StringUtil.isEmpty(entity.getUserRegion2()) ? "":entity.getUserRegion2());
maatCfg.setUserRegion(Constants.INTERCEPT_IP_RATELIMIT_DROPRATE_USER_REGION_KEY+"="+entity.getUserRegion2());
}else if(entity.getUserRegion1().equals("1")){//带宽
entity.setUserRegion3(StringUtil.isEmpty(entity.getUserRegion3()) ? "":entity.getUserRegion3());
maatCfg.setUserRegion(Constants.INTERCEPT_IP_RATELIMIT_BANDWITH_USER_REGION_KEY+"="+entity.getUserRegion3());
}
}
String userRegion="";
//替换需要发zone
if(entity.getAction().equals(Constants.REPLACE_ACTION)){
//HTTP replace replace type is not null ;find is not null;replace with is not null(userRegion is not null)
userRegion=Constants.REPLACE_ZONE_KEY+"="+entity.getUserRegion1();
String substitute="";
String userRegion2=StringUtil.isEmpty(entity.getUserRegion2()) ? "":entity.getUserRegion2();
substitute="/";
userRegion2 = BaseService.replaceContentEscape(userRegion2);
substitute=substitute+userRegion2;
String userRegion3=StringUtil.isEmpty(entity.getUserRegion3()) ? "":entity.getUserRegion3();
userRegion3 = BaseService.replaceContentEscape(userRegion3);
substitute=substitute+"/"+userRegion3;
userRegion=userRegion+";"+Constants.REPLACE_SUBSTITUTE_KEY+"="+substitute;
maatCfg.setUserRegion(userRegion);
}
if(StringUtil.isEmpty(maatCfg.getUserRegion())
|| maatCfg.getUserRegion().equals(Constants.USER_REGION_PLACEHOLDER)){
if(doaminFlag){
maatCfg.setUserRegion(domainUserRegion);
}else{
maatCfg.setUserRegion(Constants.USER_REGION_PLACEHOLDER);
}
}else{
if(doaminFlag){
maatCfg.setUserRegion(maatCfg.getUserRegion()+";"+domainUserRegion);
}
}
2018-08-21 13:38:26 +08:00
configCompileList.add(maatCfg);
maatBean.setConfigCompileList(configCompileList);
maatBean.setAuditTime(entity.getAuditTime());
maatBean.setCreatorName(entity.getCurrentUser().getName());
maatBean.setVersion(Constants.MAAT_VERSION);
maatBean.setOpAction(Constants.INSERT_ACTION);
//调用服务接口下发配置数据
String json=gsonToJson(maatBean);
2018-08-23 15:21:50 +08:00
logger.info("intercept IP/DNS 配置下发配置参数:"+json);
2018-08-21 13:38:26 +08:00
//调用服务接口下发配置
ToMaatResult result = ConfigServiceUtil.postMaatCfg(json);
2018-08-23 15:21:50 +08:00
logger.info("intercept IP/DNS 配置下发响应信息:"+result.getMsg());
2018-08-21 13:38:26 +08:00
}else if(isAudit==3){
maatCfg.setCompileId(entity.getCompileId());
maatCfg.setServiceId(entity.getServiceId());
maatCfg.setIsValid(0);//无效
configCompileList.add(maatCfg);
maatBean.setConfigCompileList(configCompileList);
maatBean.setAuditTime(entity.getAuditTime());
maatBean.setCreatorName(entity.getCurrentUser().getName());
maatBean.setVersion(Constants.MAAT_VERSION);
maatBean.setOpAction(Constants.UPDATE_ACTION);
//调用服务接口取消配置
String json=gsonToJson(maatBean);
2018-08-23 15:21:50 +08:00
logger.info("intercept IP/DNS 配置下发配置参数:"+json);
2018-08-21 13:38:26 +08:00
//调用服务接口下发配置
ToMaatResult result = ConfigServiceUtil.put(json,1);
2018-08-23 15:21:50 +08:00
logger.info("intercept IP/DNS 配置取消配置响应信息:"+result.getMsg());
2018-08-21 13:38:26 +08:00
}
}
2018-08-21 13:38:26 +08:00
}