@abtnode/core 1.6.5 → 1.6.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/blocklet/manager/disk.js +38 -15
- package/lib/cert.js +124 -0
- package/lib/event.js +9 -2
- package/lib/index.js +30 -10
- package/lib/migrations/1.6.7-certificate.js +30 -0
- package/lib/migrations/1.6.9-update-node-info-and-certificate.js +38 -0
- package/lib/router/helper.js +132 -115
- package/lib/router/manager.js +13 -9
- package/lib/states/base.js +3 -220
- package/lib/states/index.js +4 -21
- package/lib/states/node.js +10 -1
- package/lib/util/blocklet.js +29 -4
- package/lib/util/{get-ip-dns-domain-for-blocklet.js → get-domain-for-blocklet.js} +5 -1
- package/lib/util/index.js +33 -19
- package/lib/webhook/index.js +1 -1
- package/package.json +22 -20
package/lib/router/helper.js
CHANGED
|
@@ -21,15 +21,13 @@ const {
|
|
|
21
21
|
NAME_FOR_WELLKNOWN_SITE,
|
|
22
22
|
DEFAULT_HTTP_PORT,
|
|
23
23
|
DEFAULT_HTTPS_PORT,
|
|
24
|
-
DAY_IN_MS,
|
|
25
24
|
NODE_MODES,
|
|
26
25
|
ROUTING_RULE_TYPES,
|
|
27
26
|
CERTIFICATE_EXPIRES_OFFSET,
|
|
28
|
-
CERTIFICATE_EXPIRES_WARNING_OFFSET,
|
|
29
|
-
DEFAULT_DAEMON_PORT,
|
|
30
27
|
DEFAULT_SERVICE_PATH,
|
|
31
28
|
SLOT_FOR_IP_DNS_SITE,
|
|
32
29
|
BLOCKLET_SITE_GROUP_SUFFIX,
|
|
30
|
+
WELLKNOWN_ACME_CHALLENGE_PREFIX,
|
|
33
31
|
} = require('@abtnode/constant');
|
|
34
32
|
const {
|
|
35
33
|
BLOCKLET_DYNAMIC_PATH_PREFIX,
|
|
@@ -50,7 +48,7 @@ const {
|
|
|
50
48
|
getWellknownSitePort,
|
|
51
49
|
} = require('../util');
|
|
52
50
|
const { getServicesFromBlockletInterface } = require('../util/service');
|
|
53
|
-
const getIpDnsDomainForBlocklet = require('../util/get-
|
|
51
|
+
const { getIpDnsDomainForBlocklet, getDidDomainForBlocklet } = require('../util/get-domain-for-blocklet');
|
|
54
52
|
const { getFromCache: getAccessibleExternalNodeIp } = require('../util/get-accessible-external-node-ip');
|
|
55
53
|
|
|
56
54
|
const Router = require('./index');
|
|
@@ -403,11 +401,10 @@ const decompressCertificates = async (source, dest) => {
|
|
|
403
401
|
return dest;
|
|
404
402
|
};
|
|
405
403
|
|
|
406
|
-
module.exports = function getRouterHelpers({ dataDirs, routingSnapshot, routerManager, blockletManager }) {
|
|
404
|
+
module.exports = function getRouterHelpers({ dataDirs, routingSnapshot, routerManager, blockletManager, certManager }) {
|
|
407
405
|
const nodeState = states.node;
|
|
408
406
|
const blockletState = states.blocklet;
|
|
409
407
|
const siteState = states.site;
|
|
410
|
-
const httpsCertState = states.certificate;
|
|
411
408
|
const notification = states.notification;
|
|
412
409
|
|
|
413
410
|
// site level duplication detection
|
|
@@ -468,58 +465,105 @@ module.exports = function getRouterHelpers({ dataDirs, routingSnapshot, routerMa
|
|
|
468
465
|
const certificate = fs.readFileSync(certificateFilePath).toString();
|
|
469
466
|
const privateKey = fs.readFileSync(privateKeyFilePath).toString();
|
|
470
467
|
|
|
471
|
-
await
|
|
468
|
+
await certManager.upsertByDomain({
|
|
469
|
+
domain: dashboardDomain,
|
|
470
|
+
privateKey,
|
|
471
|
+
certificate,
|
|
472
|
+
isProtected: true,
|
|
473
|
+
});
|
|
472
474
|
logger.info('dashboard certificate updated');
|
|
473
475
|
} catch (error) {
|
|
474
|
-
logger.error('
|
|
476
|
+
logger.error('update dashboard certificate failed', { error });
|
|
477
|
+
throw error;
|
|
475
478
|
} finally {
|
|
476
479
|
fs.removeSync(destFolder);
|
|
477
480
|
}
|
|
478
481
|
};
|
|
479
482
|
|
|
483
|
+
const ensureDashboardCertificate = async () => {
|
|
484
|
+
const info = await nodeState.read();
|
|
485
|
+
const downloadUrl = get(info, 'routing.dashboardCertDownloadAddress', '');
|
|
486
|
+
const dashboardDomain = get(info, 'routing.dashboardDomain', '');
|
|
487
|
+
if (!dashboardDomain || !downloadUrl) {
|
|
488
|
+
throw new Error('dashboardCertDownloadAddress and dashboardDomain are not found in the routing configs');
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
const cert = await certManager.getByDomain(dashboardDomain);
|
|
492
|
+
if (cert) {
|
|
493
|
+
return { status: 'existed' };
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
logger.debug('downloading certificate', { url: downloadUrl, dashboardDomain });
|
|
497
|
+
await updateDashboardCertificate({ checkExpire: false });
|
|
498
|
+
logger.debug('downloading certificate', { url: downloadUrl, dashboardDomain });
|
|
499
|
+
return { status: 'downloaded' };
|
|
500
|
+
};
|
|
501
|
+
|
|
480
502
|
const addWellknownSite = async (sites, context) => {
|
|
481
503
|
const site = (sites || []).find((x) => x.name === NAME_FOR_WELLKNOWN_SITE);
|
|
482
504
|
|
|
483
505
|
try {
|
|
484
|
-
const
|
|
506
|
+
const info = await nodeState.read();
|
|
507
|
+
const proxyTarget = {
|
|
508
|
+
port: info.port,
|
|
509
|
+
type: ROUTING_RULE_TYPES.GENERAL_PROXY,
|
|
510
|
+
interfaceName: BLOCKLET_INTERFACE_WELLKNOWN,
|
|
511
|
+
};
|
|
512
|
+
|
|
485
513
|
const didResolverWellknownRule = {
|
|
486
|
-
from: { pathPrefix },
|
|
487
|
-
to:
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
},
|
|
514
|
+
from: { pathPrefix: joinUrl(WELLKNOWN_PATH_PREFIX, '/did.json') },
|
|
515
|
+
to: proxyTarget,
|
|
516
|
+
};
|
|
517
|
+
|
|
518
|
+
const acmeChallengeWellknownRule = {
|
|
519
|
+
from: { pathPrefix: WELLKNOWN_ACME_CHALLENGE_PREFIX },
|
|
520
|
+
to: proxyTarget,
|
|
492
521
|
};
|
|
493
522
|
|
|
494
523
|
if (site) {
|
|
495
|
-
|
|
496
|
-
|
|
524
|
+
let changed = false;
|
|
525
|
+
const exists = (prefix) => !!site.rules.find((r) => r.from.pathPrefix === normalizePathPrefix(prefix));
|
|
526
|
+
|
|
527
|
+
if (!exists(didResolverWellknownRule.from.pathPrefix)) {
|
|
528
|
+
await routerManager.addRoutingRule(
|
|
529
|
+
{
|
|
530
|
+
id: site.id,
|
|
531
|
+
rule: didResolverWellknownRule,
|
|
532
|
+
},
|
|
533
|
+
context
|
|
534
|
+
);
|
|
535
|
+
changed = true;
|
|
497
536
|
}
|
|
498
537
|
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
context
|
|
505
|
-
);
|
|
506
|
-
} else {
|
|
507
|
-
await routerManager.addRoutingSite(
|
|
508
|
-
{
|
|
509
|
-
site: {
|
|
510
|
-
domain: DOMAIN_FOR_INTERNAL_SITE,
|
|
511
|
-
port: await getWellknownSitePort(),
|
|
512
|
-
name: NAME_FOR_WELLKNOWN_SITE,
|
|
513
|
-
rules: [didResolverWellknownRule],
|
|
514
|
-
isProtected: true,
|
|
538
|
+
if (!exists(acmeChallengeWellknownRule.from.pathPrefix)) {
|
|
539
|
+
await routerManager.addRoutingRule(
|
|
540
|
+
{
|
|
541
|
+
id: site.id,
|
|
542
|
+
rule: acmeChallengeWellknownRule,
|
|
515
543
|
},
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
544
|
+
context
|
|
545
|
+
);
|
|
546
|
+
changed = true;
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
return changed;
|
|
521
550
|
}
|
|
522
551
|
|
|
552
|
+
await routerManager.addRoutingSite(
|
|
553
|
+
{
|
|
554
|
+
site: {
|
|
555
|
+
domain: DOMAIN_FOR_INTERNAL_SITE,
|
|
556
|
+
port: await getWellknownSitePort(),
|
|
557
|
+
name: NAME_FOR_WELLKNOWN_SITE,
|
|
558
|
+
rules: [didResolverWellknownRule, acmeChallengeWellknownRule],
|
|
559
|
+
isProtected: true,
|
|
560
|
+
},
|
|
561
|
+
skipCheckDynamicBlacklist: true,
|
|
562
|
+
skipValidation: true,
|
|
563
|
+
},
|
|
564
|
+
context
|
|
565
|
+
);
|
|
566
|
+
|
|
523
567
|
return true;
|
|
524
568
|
} catch (err) {
|
|
525
569
|
console.error('add well-known site failed:', err);
|
|
@@ -533,7 +577,7 @@ module.exports = function getRouterHelpers({ dataDirs, routingSnapshot, routerMa
|
|
|
533
577
|
*
|
|
534
578
|
* @returns {boolean} if routing changed
|
|
535
579
|
*/
|
|
536
|
-
const ensureDashboardRouting = async (context = {}
|
|
580
|
+
const ensureDashboardRouting = async (context = {}) => {
|
|
537
581
|
const info = await nodeState.read();
|
|
538
582
|
|
|
539
583
|
const provider = getProviderFromNodeInfo(info);
|
|
@@ -579,11 +623,16 @@ module.exports = function getRouterHelpers({ dataDirs, routingSnapshot, routerMa
|
|
|
579
623
|
});
|
|
580
624
|
|
|
581
625
|
const dashboardDomain = get(info, 'routing.dashboardDomain', '');
|
|
582
|
-
|
|
626
|
+
const didDomain = `${info.did.toLowerCase()}.${info.didDomain}`;
|
|
627
|
+
const dashboardAliasDomains = [dashboardDomain, didDomain]
|
|
628
|
+
.filter((item) => item && !isExistsInAlias(item))
|
|
629
|
+
.map((item) => ({ value: item, isProtected: true }));
|
|
630
|
+
|
|
631
|
+
if (dashboardAliasDomains.length > 0) {
|
|
583
632
|
try {
|
|
584
633
|
const result = await siteState.update(
|
|
585
634
|
{ _id: dashboardSite.id },
|
|
586
|
-
{ $push: { domainAliases: {
|
|
635
|
+
{ $push: { domainAliases: { $each: dashboardAliasDomains } } }
|
|
587
636
|
);
|
|
588
637
|
|
|
589
638
|
updatedResult.push(result);
|
|
@@ -618,18 +667,6 @@ module.exports = function getRouterHelpers({ dataDirs, routingSnapshot, routerMa
|
|
|
618
667
|
updatedResult.push(wellknownRes);
|
|
619
668
|
}
|
|
620
669
|
|
|
621
|
-
// Download dashboard certificates if not exists
|
|
622
|
-
const certDownloadAddress = get(info, 'routing.dashboardCertDownloadAddress', '');
|
|
623
|
-
if (dashboardDomain && certDownloadAddress) {
|
|
624
|
-
const cert = await routerManager.findCertificateByDomain(dashboardDomain);
|
|
625
|
-
if (!cert) {
|
|
626
|
-
await updateDashboardCertificate({ checkExpire: false });
|
|
627
|
-
if (typeof output === 'function') {
|
|
628
|
-
output('Dashboard HTTPS certificate was downloaded successfully!');
|
|
629
|
-
}
|
|
630
|
-
}
|
|
631
|
-
}
|
|
632
|
-
|
|
633
670
|
if (updatedResult.length) {
|
|
634
671
|
const hash = await takeRoutingSnapshot({ message: 'ensure dashboard routing rules', dryRun: false }, context);
|
|
635
672
|
logger.info('take routing snapshot on ensure dashboard routing rules', { updatedResult, hash });
|
|
@@ -658,7 +695,7 @@ module.exports = function getRouterHelpers({ dataDirs, routingSnapshot, routerMa
|
|
|
658
695
|
};
|
|
659
696
|
|
|
660
697
|
const domainGroup = `${blocklet.meta.did}${BLOCKLET_SITE_GROUP_SUFFIX}`;
|
|
661
|
-
|
|
698
|
+
|
|
662
699
|
const pathPrefix = getPrefix(webInterface.prefix);
|
|
663
700
|
const rule = {
|
|
664
701
|
from: { pathPrefix },
|
|
@@ -673,11 +710,24 @@ module.exports = function getRouterHelpers({ dataDirs, routingSnapshot, routerMa
|
|
|
673
710
|
|
|
674
711
|
const existSite = await states.site.findOne({ domain: domainGroup });
|
|
675
712
|
if (!existSite) {
|
|
713
|
+
const ipEchoDnsDomain = getIpDnsDomainForBlocklet(blocklet, webInterface, nodeInfo.did);
|
|
714
|
+
const appIdEnv = blocklet.environments.find((e) => e.key === 'BLOCKLET_APP_ID');
|
|
715
|
+
const domainAliases = [{ value: ipEchoDnsDomain, isProtected: true }];
|
|
716
|
+
|
|
717
|
+
const didDomain = getDidDomainForBlocklet({
|
|
718
|
+
appId: appIdEnv.value,
|
|
719
|
+
didDomain: nodeInfo.didDomain,
|
|
720
|
+
});
|
|
721
|
+
|
|
722
|
+
if (blocklet.mode !== 'development') {
|
|
723
|
+
domainAliases.push({ value: didDomain, isProtected: true });
|
|
724
|
+
}
|
|
725
|
+
|
|
676
726
|
await routerManager.addRoutingSite(
|
|
677
727
|
{
|
|
678
728
|
site: {
|
|
679
729
|
domain: domainGroup,
|
|
680
|
-
domainAliases
|
|
730
|
+
domainAliases,
|
|
681
731
|
isProtected: true,
|
|
682
732
|
rules: [rule],
|
|
683
733
|
},
|
|
@@ -685,7 +735,14 @@ module.exports = function getRouterHelpers({ dataDirs, routingSnapshot, routerMa
|
|
|
685
735
|
},
|
|
686
736
|
context
|
|
687
737
|
);
|
|
688
|
-
logger.info('add routing site', { site:
|
|
738
|
+
logger.info('add routing site', { site: domainGroup });
|
|
739
|
+
if (
|
|
740
|
+
process.env.NODE_ENV !== 'development' &&
|
|
741
|
+
process.env.NODE_ENV !== 'test' &&
|
|
742
|
+
blocklet.mode !== 'development'
|
|
743
|
+
) {
|
|
744
|
+
await certManager.issue({ domain: didDomain });
|
|
745
|
+
}
|
|
689
746
|
|
|
690
747
|
return true;
|
|
691
748
|
}
|
|
@@ -700,13 +757,13 @@ module.exports = function getRouterHelpers({ dataDirs, routingSnapshot, routerMa
|
|
|
700
757
|
},
|
|
701
758
|
skipProtectedRuleChecking: true,
|
|
702
759
|
});
|
|
703
|
-
logger.info('update routing rule for site', { site:
|
|
760
|
+
logger.info('update routing rule for site', { site: domainGroup });
|
|
704
761
|
} else {
|
|
705
762
|
await routerManager.addRoutingRule({
|
|
706
763
|
id: existSite.id,
|
|
707
764
|
rule,
|
|
708
765
|
});
|
|
709
|
-
logger.info('add routing rule for site', { site:
|
|
766
|
+
logger.info('add routing rule for site', { site: domainGroup });
|
|
710
767
|
}
|
|
711
768
|
|
|
712
769
|
return true;
|
|
@@ -1068,9 +1125,7 @@ module.exports = function getRouterHelpers({ dataDirs, routingSnapshot, routerMa
|
|
|
1068
1125
|
sites = await ensureAuthService(sites, blockletManager);
|
|
1069
1126
|
sites = await ensureServiceRule(sites);
|
|
1070
1127
|
|
|
1071
|
-
const certificates = httpsEnabled
|
|
1072
|
-
? await httpsCertState.find({ type: providerName }, { domain: 1, certificate: 1, privateKey: 1 })
|
|
1073
|
-
: [];
|
|
1128
|
+
const certificates = httpsEnabled ? await certManager.getAllNormal() : [];
|
|
1074
1129
|
|
|
1075
1130
|
return {
|
|
1076
1131
|
sites,
|
|
@@ -1093,9 +1148,9 @@ module.exports = function getRouterHelpers({ dataDirs, routingSnapshot, routerMa
|
|
|
1093
1148
|
},
|
|
1094
1149
|
});
|
|
1095
1150
|
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1151
|
+
certManager.on('cert.added', () => routers[providerName].restart());
|
|
1152
|
+
certManager.on('cert.removed', () => routers[providerName].restart());
|
|
1153
|
+
certManager.on('cert.issued', () => routers[providerName].restart());
|
|
1099
1154
|
|
|
1100
1155
|
await routers[providerName].start();
|
|
1101
1156
|
}
|
|
@@ -1257,18 +1312,21 @@ module.exports = function getRouterHelpers({ dataDirs, routingSnapshot, routerMa
|
|
|
1257
1312
|
};
|
|
1258
1313
|
|
|
1259
1314
|
const getCertificates = async () => {
|
|
1260
|
-
const certificates = await
|
|
1315
|
+
const certificates = await certManager.getAll();
|
|
1261
1316
|
const sites = await getSitesFromSnapshot();
|
|
1317
|
+
|
|
1318
|
+
const isMatch = (cert, domain) =>
|
|
1319
|
+
domain !== DOMAIN_FOR_DEFAULT_SITE && domain && routerManager.isCertMatchedDomain(cert, domain);
|
|
1320
|
+
|
|
1262
1321
|
return certificates.map((cert) => {
|
|
1263
1322
|
cert.matchedSites = [];
|
|
1264
1323
|
sites.forEach((site) => {
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
}
|
|
1324
|
+
const domains = [site.domain, ...(site.domainAliases || []).map((x) => x.value)];
|
|
1325
|
+
domains.forEach((domain) => {
|
|
1326
|
+
if (isMatch(cert, domain)) {
|
|
1327
|
+
cert.matchedSites.push({ id: site.id, domain });
|
|
1328
|
+
}
|
|
1329
|
+
});
|
|
1272
1330
|
});
|
|
1273
1331
|
|
|
1274
1332
|
return cert;
|
|
@@ -1281,42 +1339,6 @@ module.exports = function getRouterHelpers({ dataDirs, routingSnapshot, routerMa
|
|
|
1281
1339
|
return { domain, isHttps: !!matchedCert, matchedCert };
|
|
1282
1340
|
};
|
|
1283
1341
|
|
|
1284
|
-
const checkCertificatesExpiration = async () => {
|
|
1285
|
-
const now = Date.now();
|
|
1286
|
-
|
|
1287
|
-
const certificates = await getCertificates();
|
|
1288
|
-
for (let i = 0; i < certificates.length; i++) {
|
|
1289
|
-
const cert = certificates[i];
|
|
1290
|
-
const alreadyExpired = now >= cert.validTo;
|
|
1291
|
-
const aboutToExpire = cert.validTo - now > 0 && cert.validTo - now < CERTIFICATE_EXPIRES_WARNING_OFFSET;
|
|
1292
|
-
|
|
1293
|
-
if (alreadyExpired) {
|
|
1294
|
-
logger.info('send certificate expire notification', { domain: cert.domain });
|
|
1295
|
-
notification.create({
|
|
1296
|
-
title: 'SSL Certificate Expired',
|
|
1297
|
-
description: `Your SSL certificate for domain ${cert.domain} has expired, please update it in Blocklet Server`,
|
|
1298
|
-
severity: 'error',
|
|
1299
|
-
entityType: 'certificate',
|
|
1300
|
-
entityId: cert._id, // eslint-disable-line no-underscore-dangle
|
|
1301
|
-
});
|
|
1302
|
-
} else if (aboutToExpire) {
|
|
1303
|
-
logger.info('send certificate about-expire notification', { domain: cert.domain });
|
|
1304
|
-
const expireInDays = Math.ceil((cert.validTo - now) / DAY_IN_MS);
|
|
1305
|
-
notification.create({
|
|
1306
|
-
title: 'SSL Certificate Expire Warning',
|
|
1307
|
-
description: `Your SSL certificate for domain ${
|
|
1308
|
-
cert.domain
|
|
1309
|
-
} will expire in ${expireInDays} days (on ${new Date(
|
|
1310
|
-
cert.validTo
|
|
1311
|
-
).toLocaleString()}), please remember to update it in Blocklet Server`,
|
|
1312
|
-
severity: 'warning',
|
|
1313
|
-
entityType: 'certificate',
|
|
1314
|
-
entityId: cert._id, // eslint-disable-line no-underscore-dangle
|
|
1315
|
-
});
|
|
1316
|
-
}
|
|
1317
|
-
}
|
|
1318
|
-
};
|
|
1319
|
-
|
|
1320
1342
|
return {
|
|
1321
1343
|
ensureDashboardRouting,
|
|
1322
1344
|
ensureBlockletRouting,
|
|
@@ -1328,9 +1350,10 @@ module.exports = function getRouterHelpers({ dataDirs, routingSnapshot, routerMa
|
|
|
1328
1350
|
takeRoutingSnapshot,
|
|
1329
1351
|
getRoutingSites,
|
|
1330
1352
|
getSnapshotSites,
|
|
1331
|
-
getCertificates,
|
|
1332
1353
|
getSitesFromSnapshot,
|
|
1354
|
+
getCertificates,
|
|
1333
1355
|
checkDomain,
|
|
1356
|
+
ensureDashboardCertificate,
|
|
1334
1357
|
|
|
1335
1358
|
getRoutingCrons: () => [
|
|
1336
1359
|
{
|
|
@@ -1339,12 +1362,6 @@ module.exports = function getRouterHelpers({ dataDirs, routingSnapshot, routerMa
|
|
|
1339
1362
|
fn: () => updateDashboardCertificate({ checkExpire: true }),
|
|
1340
1363
|
options: { runOnInit: true },
|
|
1341
1364
|
},
|
|
1342
|
-
{
|
|
1343
|
-
name: 'check-certificate-expiration',
|
|
1344
|
-
time: '0 0 9 * * *', // check on 09:00 every day
|
|
1345
|
-
fn: checkCertificatesExpiration,
|
|
1346
|
-
options: { runOnInit: false },
|
|
1347
|
-
},
|
|
1348
1365
|
{
|
|
1349
1366
|
name: 'rotate-log-files',
|
|
1350
1367
|
time: '5 0 0 * * *', // rotate at 05:00 every day
|
package/lib/router/manager.js
CHANGED
|
@@ -70,8 +70,9 @@ const normalizeRedirectUrl = (url) => {
|
|
|
70
70
|
};
|
|
71
71
|
|
|
72
72
|
class RouterManager extends EventEmitter {
|
|
73
|
-
constructor() {
|
|
73
|
+
constructor({ certManager }) {
|
|
74
74
|
super();
|
|
75
|
+
this.certManager = certManager;
|
|
75
76
|
|
|
76
77
|
// HACK: do not emit any events from CLI
|
|
77
78
|
if (isCLI()) {
|
|
@@ -300,7 +301,10 @@ class RouterManager extends EventEmitter {
|
|
|
300
301
|
await this.validateRouterConfig('updateRoutingRule', { id, rule });
|
|
301
302
|
|
|
302
303
|
// update rules
|
|
303
|
-
const newRules = [
|
|
304
|
+
const newRules = [
|
|
305
|
+
...dbSite.rules.filter((x) => x.groupId !== rule.id || x.id !== rule.id), // 有些路由没有 rule.groupId
|
|
306
|
+
...(await this.getRules(rule)),
|
|
307
|
+
];
|
|
304
308
|
|
|
305
309
|
const updateResult = await states.site.update({ _id: id }, { $set: { rules: newRules } });
|
|
306
310
|
logger.info('update result', { updateResult });
|
|
@@ -363,7 +367,7 @@ class RouterManager extends EventEmitter {
|
|
|
363
367
|
domain,
|
|
364
368
|
});
|
|
365
369
|
logger.info('add certificate result', { domain: newCert.domain });
|
|
366
|
-
this.emit('
|
|
370
|
+
this.emit('cert.added', { type: 'nginx', data: newCert });
|
|
367
371
|
}
|
|
368
372
|
|
|
369
373
|
// eslint-disable-next-line no-unused-vars
|
|
@@ -376,7 +380,7 @@ class RouterManager extends EventEmitter {
|
|
|
376
380
|
const removeResult = await states.certificate.remove({ _id: id });
|
|
377
381
|
|
|
378
382
|
logger.info('delete certificate', { removeResult, domain: tmpCert.domain });
|
|
379
|
-
this.emit('
|
|
383
|
+
this.emit('cert.removed', { type: 'nginx', data: { domain: tmpCert.domain } });
|
|
380
384
|
return {};
|
|
381
385
|
}
|
|
382
386
|
|
|
@@ -386,7 +390,7 @@ class RouterManager extends EventEmitter {
|
|
|
386
390
|
this.fixCertificate(entity);
|
|
387
391
|
this.validateCertificate(entity, entity.domain);
|
|
388
392
|
const dbEntity = await states.certificate.upsert(entity);
|
|
389
|
-
this.emit('
|
|
393
|
+
this.emit('cert.issued', { type: 'nginx', data: dbEntity });
|
|
390
394
|
}
|
|
391
395
|
|
|
392
396
|
findCertificateByDomain(domain) {
|
|
@@ -448,16 +452,16 @@ class RouterManager extends EventEmitter {
|
|
|
448
452
|
}
|
|
449
453
|
|
|
450
454
|
async getMatchedCert(domain) {
|
|
451
|
-
const certs = await
|
|
455
|
+
const certs = await this.certManager.getAll();
|
|
452
456
|
const matchedCert = certs.find((cert) => this.isCertMatchedDomain(cert, domain));
|
|
453
457
|
|
|
454
458
|
if (matchedCert) {
|
|
455
459
|
return {
|
|
456
460
|
id: matchedCert.id,
|
|
457
461
|
domain: matchedCert.domain,
|
|
458
|
-
issuer: matchedCert.issuer,
|
|
459
|
-
validFrom: matchedCert.validFrom,
|
|
460
|
-
validTo: matchedCert.validTo,
|
|
462
|
+
issuer: matchedCert.meta.issuer,
|
|
463
|
+
validFrom: matchedCert.meta.validFrom,
|
|
464
|
+
validTo: matchedCert.meta.validTo,
|
|
461
465
|
};
|
|
462
466
|
}
|
|
463
467
|
|