@@ -63,8 +63,11 @@ public class DomainServiceImpl implements DomainService {
@Value ( " ${query.readin.batch} " )
private int queryReadinBatch ;
@Value ( " ${query.isLocal } " )
private boolean isLocal ;
@Value ( " ${query.mode } " )
private int queryMode ;
// private static ThreadLocal<Integer> bcSuccessCounter = ThreadLocal.withInitial(() -> 0);
// private static ThreadLocal<Integer> bcFailureCounter = ThreadLocal.withInitial(() -> 0);
@Override
public List < DomainInfo > getCategoryInfo ( List < String > domains , String username ) throws Exception {
@@ -73,7 +76,80 @@ public class DomainServiceImpl implements DomainService {
logger . info ( " domain category query distinct total : {} " , domains . size ( ) ) ;
int queryNum = domains . size ( ) ;
List < DomainInfo > results = new ArrayList < DomainInfo > ( ) ;
//1. 查询本地数据库
if ( queryMode = = 1 ) {
//1. 查询本地数据库
getDomainsByDB ( domains , results ) ;
} else if ( queryMode = = 2 ) {
//2. 查第三方api ,并更新数据库
getDomainByBrightCloud ( domains , username , queryNum , results , 0 ) ;
getDomainsByDB ( domains , results ) ;
} else {
//3. 查数据库, 再查第三方api
int domainsByDBSize = getDomainsByDB ( domains , results ) ;
getDomainByBrightCloud ( domains , username , queryNum , results , domainsByDBSize ) ;
}
return results ;
}
/**
* @param domains
* @param username
* @param queryNum
* @param results
* @param dbResultNum
* @throws IOException
*/
private void getDomainByBrightCloud ( List < String > domains , String username , int queryNum , List < DomainInfo > results , int dbResultNum ) throws IOException {
//2. 调用api查询
int apiResultNum = 0 ;
int failedQueryNum = 0 ;
List < DomainCategory > bcResults = new ArrayList < > ( ) ;
//批量查询API
BrightCloud brightCloud = new BrightCloud ( ) ;
List < List < String > > apiPartitions = Lists . partition ( domains , bcConfig . getMaximumQueryNum ( ) ) ;
List < String > bcDomainRecords = new ArrayList < > ( ) ;
for ( List < String > partition : apiPartitions ) {
List < DomainInfo > bcDomainInfos = new ArrayList < > ( ) ;
List < DomainCategory > recordsFromBcApi = brightCloud . getBrightCloudDomainCategory ( partition ) ;
for ( DomainCategory record : recordsFromBcApi ) {
//查询成功的结果
if ( record . getQuery_success ( ) . equals ( true ) ) {
record . setSubmit_user ( username ) ;
record . setCreate_time ( new Date ( ) ) ;
record . setUpdate_time ( new Date ( ) ) ;
bcResults . add ( record ) ;
bcDomainInfos . add ( DomainInfo . builder ( )
. domain ( record . getFqdn ( ) )
. result ( Lists . newArrayList ( record ) )
. build ( ) ) ;
apiResultNum + = 1 ;
// bcSuccessCounter.set(bcSuccessCounter.get() + 1);
bcDomainRecords . add ( record . getFqdn ( ) ) ;
} else {
failedQueryNum + = 1 ;
// bcFailureCounter.set(bcFailureCounter.get() + 1);
}
}
if ( bcResults . size ( ) > 0 ) {
insertBatchDomains ( bcResults ) ;
results . addAll ( bcDomainInfos ) ;
bcResults . clear ( ) ;
bcDomainInfos . clear ( ) ;
}
processLog ( " category " , queryNum , apiResultNum , failedQueryNum , dbResultNum ) ;
}
//移除查询到的域名
domains . removeAll ( bcDomainRecords ) ;
// // 记录api调用次数
FileWriter fileWriter = new FileWriter ( bcConfig . getUsereportFilePath ( ) , true ) ;
fileWriter . write ( DateUtil . date ( ) + " , " + " domain category query, brightCloud, success records " + apiResultNum + " , fail records " + failedQueryNum + " \ n " ) ;
fileWriter . close ( ) ;
logger . info ( " domain category query brightCloud success records : " + apiResultNum + " ,fail records : " + failedQueryNum ) ;
}
private int getDomainsByDB ( List < String > domains , List < DomainInfo > results ) {
List < List < String > > partitionDomains = Lists . partition ( domains , appConfig . getDbQueryBatchSize ( ) ) ;
List < String > dBDomains = new ArrayList < > ( ) ;
for ( List < String > partitionDomain : partitionDomains ) {
@@ -84,54 +160,7 @@ public class DomainServiceImpl implements DomainService {
}
domains . removeAll ( dBDomains ) ;
logger . info ( " domain category query DB record {} " , results . size ( ) ) ;
//2. 调用api查询
int apiResultNum = 0 ;
int failedQueryNum = 0 ;
int dbResultNum = dBDomains . size ( ) ;
if ( ! isLocal & & domains . size ( ) > 0 ) {
List < DomainCategory > bcResults = new ArrayList < > ( ) ;
//批量查询API
BrightCloud brightCloud = new BrightCloud ( ) ;
List < List < String > > apiPartitions = Lists . partition ( domains , bcConfig . getMaximumQueryNum ( ) ) ;
for ( List < String > partition : apiPartitions ) {
List < DomainInfo > bcDomainInfos = new ArrayList < > ( ) ;
List < DomainCategory > recordsFromBcApi = brightCloud . getBrightCloudDomainCategory ( partition ) ;
for ( DomainCategory record : recordsFromBcApi ) {
//查询成功的结果
if ( record . getQuery_success ( ) . equals ( true ) ) {
record . setSubmit_user ( username ) ;
record . setCreate_time ( new Date ( ) ) ;
record . setUpdate_time ( new Date ( ) ) ;
bcResults . add ( record ) ;
bcDomainInfos . add ( DomainInfo . builder ( )
. domain ( record . getFqdn ( ) )
. result ( Lists . newArrayList ( record ) )
. build ( ) ) ;
apiResultNum + = 1 ;
} else {
failedQueryNum + = 1 ;
}
}
if ( bcResults . size ( ) > 0 ) {
insertBatchDomains ( bcResults ) ;
results . addAll ( bcDomainInfos ) ;
bcResults . clear ( ) ;
bcDomainInfos . clear ( ) ;
}
processLog ( " category " , queryNum , apiResultNum , failedQueryNum , dbResultNum ) ;
}
// 记录api调用次数
FileWriter fileWriter = new FileWriter ( bcConfig . getUsereportFilePath ( ) , true ) ;
fileWriter . write ( DateUtil . date ( ) + " , " + " domain category query brightCloud success record " + apiResultNum + " ,fail query record " + failedQueryNum + " \ n " ) ;
fileWriter . close ( ) ;
logger . info ( " domain category query brightCloud success record : " + apiResultNum + " ,fail query record : " + failedQueryNum ) ;
}
return results ;
return dBDomains . size ( ) ;
}
@Override
@@ -162,12 +191,12 @@ public class DomainServiceImpl implements DomainService {
domains . removeAll ( dBDomains ) ;
logger . info ( " domain whois query DB record {} " , results . size ( ) ) ;
//2. 调用api
int apiResultNum = 0 ;
int failedQuery Num = 0 ;
int dbResult Num = dBDomains . size ( ) ;
if ( queryMode = = 1 & & domains . size ( ) > 0 ) {
//2. 调用api
int apiResult Num = 0 ;
int failedQuery Num = 0 ;
int dbResultNum = dBDomains . size ( ) ;
if ( ! isLocal & & domains . size ( ) > 0 ) {
List < DomainWhois > chinazResults = new ArrayList < > ( ) ;
ChinaZ chinaz = new ChinaZ ( ) ;
List < List < String > > apiPartitions = Lists . partition ( domains , chinazConfig . getMaximumQueryNum ( ) ) ;
@@ -201,9 +230,9 @@ public class DomainServiceImpl implements DomainService {
// 记录api调用次数
try {
FileWriter fileWriter = new FileWriter ( chinazConfig . getUsereportFilePath ( ) , true ) ;
fileWriter . write ( DateUtil . date ( ) + " , " + " domain whois query chinaz success record " + apiResultNum + " ,fail query record " + failedQueryNum + " \ n " ) ;
fileWriter . write ( DateUtil . date ( ) + " , " + " domain whois query, chinaz, success records " + apiResultNum + " , fail records " + failedQueryNum + " \ n " ) ;
fileWriter . close ( ) ;
logger . info ( " domain whois query chinaz success record " + apiResultNum + " ,fail query record " + failedQueryNum ) ;
logger . info ( " domain whois query chinaz success records " + apiResultNum + " ,fail records " + failedQueryNum ) ;
} catch ( IOException e ) {
logger . error ( e ) ;
}
@@ -229,7 +258,7 @@ public class DomainServiceImpl implements DomainService {
domains . removeAll ( dBDomains ) ;
logger . info ( " domain ICP query DB record {} " , results . size ( ) ) ;
if ( ! isLocal & & domains . size ( ) > 0 ) {
if ( queryMode = = 1 & & domains . size ( ) > 0 ) {
//2. 调用api
int apiResultNum = 0 ;
int failedQueryNum = 0 ;
@@ -267,9 +296,9 @@ public class DomainServiceImpl implements DomainService {
// 记录api调用次数
try {
FileWriter fileWriter = new FileWriter ( chinazConfig . getUsereportFilePath ( ) , true ) ;
fileWriter . write ( DateUtil . date ( ) + " , " + " domain ICP query chinaz success record " + apiResultNum + " ,fail query record " + failedQueryNum + " \ n " ) ;
fileWriter . write ( DateUtil . date ( ) + " , " + " domain ICP query, chinaz, success records " + apiResultNum + " , fail records " + failedQueryNum + " \ n " ) ;
fileWriter . close ( ) ;
logger . info ( " domain ICP query chinaz success record " + apiResultNum + " ,fail query record " + failedQueryNum ) ;
logger . info ( " domain ICP query chinaz success records " + apiResultNum + " ,fail records " + failedQueryNum ) ;
} catch ( IOException e ) {
logger . error ( e ) ;
}
@@ -301,12 +330,9 @@ public class DomainServiceImpl implements DomainService {
@Override
public Map getDomainInfoByFile ( String requestUser , String srcFile , String queryType , Boolean export ) throws Exception {
//输出 文件
//查询的源 文件
String fileName = String . join ( " - " , FileUtil . getPrefix ( srcFile ) , queryType , DateUtil . format ( new Date ( ) , " yyyyMMdd-HHmmss " ) + " .json " ) ;
File resultFile = FileUtil . touch ( queryOutputDir + File . separator + fileName ) ;
Map < String , Object > resultMsg = new HashMap < > ( ) ;
resultMsg . put ( " result " , resultFile . toString ( ) ) ;
resultMsg . put ( " export " , export ) ;
// 文件读取
@@ -316,7 +342,7 @@ public class DomainServiceImpl implements DomainService {
int resultSize = 0 ;
int querySize = 0 ;
List < DomainInfo > queryResults = new ArrayList < > ( ) ;
cn . hutool . core . io . file . FileWriter fileWriter = new cn . hutool . core . io . file . FileWriter ( resultFile ) ;
for ( List < String > domains : partition ) {
switch ( queryType ) {
case " category " :
@@ -329,24 +355,30 @@ public class DomainServiceImpl implements DomainService {
queryResults = getICPInfo ( domains ) ;
break ;
}
querySize + = domains . size ( ) ;
querySize + = domains . size ( ) ;
resultSize + = queryResults . size ( ) ;
if ( ObjectUtil . isNotEmpty ( queryResults ) & & export ) {
List < String > prettyResults = queryResults . stream ( ) . map ( x - > JSON . toJSONString ( x ) ) . collect ( Collectors . toList ( ) ) ;
//List<String> prettyResults = queryResults.stream().map(x -> JSON.toJSONString(x, true)).collect(Collectors.toList());
fileWriter . appendLines ( prettyResults ) ;
logger . info ( " [File query]- " + srcFile + " result: " + queryResults . size ( ) + " Results saved in " + resultFile . getAbsolutePath ( ) ) ;
exportFile ( srcFile , fileName , resultMsg , querySize , queryResults ) ;
}
Double queryTotal = Double . valueOf ( readUtf8Lines . size ( ) ) ;
logger . info ( " [File query]- " + queryType + " query result size : " + resultSize + " , query size : " + querySize + " , process: " + NumberUtil . decimalFormat ( " #.##% " , querySize / queryTotal ) ) ;
logger . info ( " [File query]- " + queryType + " query result records : " + resultSize + " , query records : " + querySize + " , process: " + NumberUtil . decimalFormat ( " #.##% " , querySize / queryTotal ) ) ;
queryResults . clear ( ) ;
}
resultMsg . put ( " total_export " , querySize ) ;
return resultMsg ;
}
private void exportFile ( String srcFile , String fileName , Map < String , Object > resultMsg , int querySize , List < DomainInfo > queryResults ) {
File resultFile = FileUtil . touch ( queryOutputDir + File . separator + fileName ) ;
cn . hutool . core . io . file . FileWriter fileWriter = new cn . hutool . core . io . file . FileWriter ( resultFile ) ;
List < String > prettyResults = queryResults . stream ( ) . map ( x - > JSON . toJSONString ( x ) ) . collect ( Collectors . toList ( ) ) ;
//List<String> prettyResults = queryResults.stream().map(x -> JSON.toJSONString(x, true)).collect(Collectors.toList());
fileWriter . appendLines ( prettyResults ) ;
resultMsg . put ( " filePath " , resultFile . toString ( ) ) ;
resultMsg . put ( " fileSize " , resultFile . length ( ) ) ;
resultMsg . put ( " records " , querySize ) ;
logger . info ( " [File query]- " + srcFile + " result: " + queryResults . size ( ) + " Results saved in " + resultFile . getAbsolutePath ( ) ) ;
}
/**
* 域名入库
*
@@ -386,7 +418,7 @@ public class DomainServiceImpl implements DomainService {
private void processLog ( String logType , int queryNum , int apiResultNum , int failedQueryNum , int dbResultNum ) {
logger . info ( " [domain " + logType + " ]- "
+ " Query result: submit " + queryNum + " valid objects, "
+ dbResultNum + " ( " + new DecimalFormat ( " ##.0% " ) . format ( ( float ) dbResultNum / queryNum ) + " ) " + " results from database, "
+ dbResultNum + " ( " + new DecimalFormat ( " ##.0% " ) . format ( ( float ) dbResultNum / queryNum ) + " ) " + " results from database. "
+ apiResultNum + " ( " + new DecimalFormat ( " ##.0% " ) . format ( ( float ) apiResultNum / queryNum ) + " ) " + " results from api. "
+ failedQueryNum + " ( " + new DecimalFormat ( " ##.0% " ) . format ( ( float ) failedQueryNum / queryNum ) + " ) " + " failed queries, " ) ;
}