mirror of
https://github.com/certd/certd.git
synced 2026-04-24 04:17:25 +08:00
perf: 优化站点ip检查
This commit is contained in:
@@ -4,6 +4,7 @@ import { AuthService } from "../../../modules/sys/authority/service/auth-service
|
||||
import { SiteInfoService } from "../../../modules/monitor/service/site-info-service.js";
|
||||
import { UserSiteMonitorSetting } from "../../../modules/mine/service/models.js";
|
||||
import { merge } from "lodash-es";
|
||||
import {SiteIpService} from "../../../modules/monitor/service/site-ip-service.js";
|
||||
|
||||
/**
|
||||
*/
|
||||
@@ -14,6 +15,8 @@ export class SiteInfoController extends CrudController<SiteInfoService> {
|
||||
service: SiteInfoService;
|
||||
@Inject()
|
||||
authService: AuthService;
|
||||
@Inject()
|
||||
siteIpService: SiteIpService;
|
||||
|
||||
getService(): SiteInfoService {
|
||||
return this.service;
|
||||
@@ -96,7 +99,16 @@ export class SiteInfoController extends CrudController<SiteInfoService> {
|
||||
await this.service.checkAllByUsers(userId);
|
||||
return this.ok();
|
||||
}
|
||||
|
||||
@Post('/ipCheckChange', { summary: Constants.per.authOnly })
|
||||
async ipCheckChange(@Body(ALL) bean: any) {
|
||||
const userId = this.getUserId();
|
||||
await this.service.checkUserId(bean.id, userId)
|
||||
await this.service.ipCheckChange({
|
||||
id: bean.id,
|
||||
ipCheck: bean.ipCheck
|
||||
});
|
||||
return this.ok();
|
||||
}
|
||||
|
||||
|
||||
@Post("/setting/get", { summary: Constants.per.authOnly })
|
||||
|
||||
@@ -44,7 +44,9 @@ export class SiteInfoController extends CrudController<SiteIpService> {
|
||||
bean.userId = this.getUserId();
|
||||
bean.from = "manual"
|
||||
const res = await this.service.add(bean);
|
||||
this.service.check(res.id);
|
||||
const siteEntity = await this.siteInfoService.info(bean.siteId);
|
||||
const {domain, httpsPort} = siteEntity;
|
||||
this.service.check(res.id,domain, httpsPort);
|
||||
return this.ok(res);
|
||||
}
|
||||
|
||||
@@ -53,7 +55,9 @@ export class SiteInfoController extends CrudController<SiteIpService> {
|
||||
await this.service.checkUserId(bean.id, this.getUserId());
|
||||
delete bean.userId;
|
||||
await this.service.update(bean);
|
||||
this.service.check(bean.id);
|
||||
const siteEntity = await this.siteInfoService.info(bean.siteId);
|
||||
const {domain, httpsPort} = siteEntity;
|
||||
this.service.check(bean.id,domain, httpsPort);
|
||||
return this.ok();
|
||||
}
|
||||
@Post('/info', { summary: Constants.per.authOnly })
|
||||
@@ -71,7 +75,11 @@ export class SiteInfoController extends CrudController<SiteIpService> {
|
||||
@Post('/check', { summary: Constants.per.authOnly })
|
||||
async check(@Body('id') id: number) {
|
||||
await this.service.checkUserId(id, this.getUserId());
|
||||
this.service.check(id);
|
||||
const entity = await this.service.info(id);
|
||||
const siteEntity = await this.siteInfoService.info(entity.siteId);
|
||||
const domain = siteEntity.domain;
|
||||
const port = siteEntity.httpsPort;
|
||||
this.service.check(id,domain,port);
|
||||
return this.ok();
|
||||
}
|
||||
|
||||
@@ -79,7 +87,8 @@ export class SiteInfoController extends CrudController<SiteIpService> {
|
||||
async checkAll(@Body('siteId') siteId: number) {
|
||||
const userId = this.getUserId();
|
||||
await this.siteInfoService.checkUserId(siteId, userId);
|
||||
await this.service.checkAll(siteId);
|
||||
const siteEntity = await this.siteInfoService.info(siteId);
|
||||
await this.service.checkAll(siteEntity);
|
||||
return this.ok();
|
||||
}
|
||||
|
||||
@@ -94,4 +103,5 @@ export class SiteInfoController extends CrudController<SiteIpService> {
|
||||
return this.ok();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import { isComm, isPlus } from '@certd/plus-core';
|
||||
import { UserSuiteService } from '@certd/commercial-core';
|
||||
import { UserSettingsService } from "../../mine/service/user-settings-service.js";
|
||||
import { UserSiteMonitorSetting } from "../../mine/service/models.js";
|
||||
import {SiteIpService} from "./site-ip-service.js";
|
||||
|
||||
@Provide()
|
||||
@Scope(ScopeEnum.Request, { allowDowngrade: true })
|
||||
@@ -31,6 +32,8 @@ export class SiteInfoService extends BaseService<SiteInfoEntity> {
|
||||
@Inject()
|
||||
userSettingsService: UserSettingsService;
|
||||
|
||||
@Inject()
|
||||
siteIpService: SiteIpService;
|
||||
|
||||
//@ts-ignore
|
||||
getRepository() {
|
||||
@@ -128,6 +131,13 @@ export class SiteInfoService extends BaseService<SiteInfoEntity> {
|
||||
};
|
||||
|
||||
await this.update(updateData);
|
||||
|
||||
|
||||
//检查ip
|
||||
if( site.ipCheck){
|
||||
await this.siteIpService.checkAll(site)
|
||||
}
|
||||
|
||||
if (!notify) {
|
||||
return;
|
||||
}
|
||||
@@ -156,7 +166,7 @@ export class SiteInfoService extends BaseService<SiteInfoEntity> {
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查,但不发邮件
|
||||
* 检查
|
||||
* @param id
|
||||
* @param notify
|
||||
* @param retryTimes
|
||||
@@ -250,4 +260,16 @@ export class SiteInfoService extends BaseService<SiteInfoEntity> {
|
||||
async saveSetting(userId: number, bean: UserSiteMonitorSetting) {
|
||||
await this.userSettingsService.saveSetting(userId, bean);
|
||||
}
|
||||
|
||||
async ipCheckChange(req: {id: any; ipCheck: any}) {
|
||||
|
||||
await this.update({
|
||||
id: req.id,
|
||||
ipCheck: req.ipCheck,
|
||||
});
|
||||
if(req.ipCheck){
|
||||
const site = await this.info(req.id);
|
||||
await this.siteIpService.sync(site)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +1,17 @@
|
||||
import { Inject, Provide, Scope, ScopeEnum } from "@midwayjs/core";
|
||||
import { BaseService, SysSettingsService } from "@certd/lib-server";
|
||||
import { InjectEntityModel } from "@midwayjs/typeorm";
|
||||
import { Repository } from "typeorm";
|
||||
import { SiteInfoEntity } from "../entity/site-info.js";
|
||||
import { NotificationService } from "../../pipeline/service/notification-service.js";
|
||||
import { UserSuiteService } from "@certd/commercial-core";
|
||||
import { UserSettingsService } from "../../mine/service/user-settings-service.js";
|
||||
import { SiteIpEntity } from "../entity/site-ip.js";
|
||||
import {Inject, Provide, Scope, ScopeEnum} from "@midwayjs/core";
|
||||
import {BaseService, SysSettingsService} from "@certd/lib-server";
|
||||
import {InjectEntityModel} from "@midwayjs/typeorm";
|
||||
import {Repository} from "typeorm";
|
||||
import {SiteInfoEntity} from "../entity/site-info.js";
|
||||
import {NotificationService} from "../../pipeline/service/notification-service.js";
|
||||
import {UserSuiteService} from "@certd/commercial-core";
|
||||
import {UserSettingsService} from "../../mine/service/user-settings-service.js";
|
||||
import {SiteIpEntity} from "../entity/site-ip.js";
|
||||
import dns from "dns";
|
||||
import { logger, safePromise } from "@certd/basic";
|
||||
import {logger, safePromise} from "@certd/basic";
|
||||
import dayjs from "dayjs";
|
||||
import { siteTester } from "./site-tester.js";
|
||||
import { PeerCertificate } from "tls";
|
||||
import { SiteInfoService } from "./site-info-service.js";
|
||||
import {siteTester} from "./site-tester.js";
|
||||
import {PeerCertificate} from "tls";
|
||||
|
||||
@Provide()
|
||||
@Scope(ScopeEnum.Request, { allowDowngrade: true })
|
||||
@@ -31,9 +30,6 @@ export class SiteIpService extends BaseService<SiteIpEntity> {
|
||||
|
||||
@Inject()
|
||||
userSettingsService: UserSettingsService;
|
||||
@Inject()
|
||||
siteInfoService: SiteInfoService;
|
||||
|
||||
|
||||
//@ts-ignore
|
||||
getRepository() {
|
||||
@@ -83,11 +79,11 @@ export class SiteIpService extends BaseService<SiteIpEntity> {
|
||||
});
|
||||
}
|
||||
|
||||
await this.checkAll(entity.id);
|
||||
await this.checkAll(entity);
|
||||
|
||||
}
|
||||
|
||||
async check(ipId: number, domain?: string, port?: number) {
|
||||
async check(ipId: number, domain: string, port: number) {
|
||||
if(!ipId){
|
||||
return
|
||||
}
|
||||
@@ -95,11 +91,6 @@ export class SiteIpService extends BaseService<SiteIpEntity> {
|
||||
if (!entity) {
|
||||
return;
|
||||
}
|
||||
if (domain == null || port == null){
|
||||
const siteEntity = await this.siteInfoService.info(entity.siteId);
|
||||
domain = siteEntity.domain;
|
||||
port = siteEntity.httpsPort;
|
||||
}
|
||||
try {
|
||||
await this.update({
|
||||
id: entity.id,
|
||||
@@ -151,8 +142,8 @@ export class SiteIpService extends BaseService<SiteIpEntity> {
|
||||
}
|
||||
}
|
||||
|
||||
async checkAll(siteId: number) {
|
||||
const siteInfo = await this.siteInfoService.info(siteId);
|
||||
async checkAll(siteInfo: SiteInfoEntity) {
|
||||
const siteId = siteInfo.id;
|
||||
const ips = await this.repository.find({
|
||||
where: {
|
||||
siteId: siteId
|
||||
@@ -162,13 +153,14 @@ export class SiteIpService extends BaseService<SiteIpEntity> {
|
||||
const port = siteInfo.httpsPort;
|
||||
const promiseList = [];
|
||||
for (const ip of ips) {
|
||||
promiseList.push(async () => {
|
||||
const func = async () => {
|
||||
try {
|
||||
await this.check(ip.id, domain, port);
|
||||
} catch (e) {
|
||||
logger.error("check site ip error", e);
|
||||
}
|
||||
});
|
||||
}
|
||||
promiseList.push(func());
|
||||
}
|
||||
Promise.all(promiseList);
|
||||
}
|
||||
|
||||
@@ -52,16 +52,12 @@ export class SiteTester {
|
||||
if (req.ipAddress) {
|
||||
//使用固定的ip
|
||||
const ipAddress = req.ipAddress;
|
||||
options.lookup = (hostname: string, options: any, callback: any) => {
|
||||
//判断ip是v4 还是v6
|
||||
console.log("options", options);
|
||||
console.log("ipaddress", ipAddress);
|
||||
if (ipAddress.indexOf(":") > -1) {
|
||||
callback(null, ipAddress, 6);
|
||||
} else {
|
||||
callback(null, ipAddress, 4);
|
||||
}
|
||||
};
|
||||
options.headers={
|
||||
host: options.host,
|
||||
//sni
|
||||
servername: options.host
|
||||
}
|
||||
options.host = ipAddress;
|
||||
}
|
||||
|
||||
options.agent = new https.Agent({ keepAlive: false });
|
||||
|
||||
Reference in New Issue
Block a user