Commit caaa81b0 authored by nanahira's avatar nanahira

rework

parent 8b6e20ac
Pipeline #4917 failed with stages
in 19 seconds
...@@ -111,3 +111,5 @@ dist ...@@ -111,3 +111,5 @@ dist
Dockerfile Dockerfile
.gitlab-ci.yml .gitlab-ci.yml
/config.yaml /config.yaml
/config-*.yaml
.idea
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended',
],
root: true,
env: {
node: true,
jest: true,
},
ignorePatterns: ['.eslintrc.js'],
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
},
};
...@@ -104,5 +104,8 @@ dist ...@@ -104,5 +104,8 @@ dist
.tern-port .tern-port
/build /build
/dist
/output /output
/config.yaml /config.yaml
/config-*.yaml
.idea
stages: stages:
- build - build
- combine
- deploy - deploy
variables: variables:
GIT_DEPTH: "1" GIT_DEPTH: "1"
CONTAINER_TEST_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG CONTAINER_TEST_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
CONTAINER_TEST_ARM_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG-arm
CONTAINER_TEST_X86_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG-x86
CONTAINER_RELEASE_IMAGE: $CI_REGISTRY_IMAGE:latest CONTAINER_RELEASE_IMAGE: $CI_REGISTRY_IMAGE:latest
before_script: before_script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
build: build-x86:
stage: build stage: build
tags: tags:
- docker - docker
variables:
TARGET_IMAGE: $CONTAINER_TEST_X86_IMAGE
script:
- docker build --pull -t $TARGET_IMAGE .
- docker push $TARGET_IMAGE
build-arm:
stage: build
tags:
- docker-arm
variables:
TARGET_IMAGE: $CONTAINER_TEST_ARM_IMAGE
script: script:
- docker build --pull -t $CONTAINER_TEST_IMAGE . - docker build --pull -t $TARGET_IMAGE .
- docker push $CONTAINER_TEST_IMAGE - docker push $TARGET_IMAGE
combine:
stage: combine
tags:
- docker
variables:
SOURCE_IMAGE_1: $CONTAINER_TEST_X86_IMAGE
SOURCE_IMAGE_2: $CONTAINER_TEST_ARM_IMAGE
TARGET_IMAGE: $CONTAINER_TEST_IMAGE
script:
- docker pull $SOURCE_IMAGE_1
- docker pull $SOURCE_IMAGE_2
- docker manifest create $TARGET_IMAGE --amend $SOURCE_IMAGE_1 --amend $SOURCE_IMAGE_2
- docker manifest push $TARGET_IMAGE
deploy_latest: deploy_latest:
stage: deploy stage: deploy
tags: tags:
- docker - docker
variables:
SOURCE_IMAGE: $CONTAINER_TEST_IMAGE
TARGET_IMAGE: $CONTAINER_RELEASE_IMAGE
script: script:
- docker pull $CONTAINER_TEST_IMAGE - docker pull $SOURCE_IMAGE
- docker tag $CONTAINER_TEST_IMAGE $CONTAINER_RELEASE_IMAGE - docker tag $SOURCE_IMAGE $TARGET_IMAGE
- docker push $CONTAINER_RELEASE_IMAGE - docker push $TARGET_IMAGE
only: only:
- master - master
...@@ -34,10 +66,11 @@ deploy_tag: ...@@ -34,10 +66,11 @@ deploy_tag:
tags: tags:
- docker - docker
variables: variables:
CONTAINER_TAG_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_TAG SOURCE_IMAGE: $CONTAINER_TEST_IMAGE
TARGET_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_TAG
script: script:
- docker pull $CONTAINER_TEST_IMAGE - docker pull $SOURCE_IMAGE
- docker tag $CONTAINER_TEST_IMAGE $CONTAINER_TAG_IMAGE - docker tag $SOURCE_IMAGE $TARGET_IMAGE
- docker push $CONTAINER_TAG_IMAGE - docker push $TARGET_IMAGE
only: only:
- tags - tags
/install-npm.sh
.git*
/output
/dest
/config.yaml
/config-*.yaml
.idea
.dockerignore
Dockerfile
/src
.eslintrc.js
.prettierrc
webpack.config.js
tsconfig.json
{
"singleQuote": true,
"trailingComma": "all"
}
\ No newline at end of file
FROM node:buster-slim FROM node:bullseye-slim
LABEL Author="Nanahira <nanahira@momobako.com>"
RUN apt update && apt -y install python3 build-essential && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* RUN apt update && apt -y install python3 build-essential && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
WORKDIR /usr/src/app WORKDIR /usr/src/app
COPY ./package*.json ./ COPY ./package*.json ./
......
GNU GENERAL PUBLIC LICENSE GNU AFFERO GENERAL PUBLIC LICENSE
Version 3, 29 June 2007 Version 3, 19 November 2007
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/> Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies Everyone is permitted to copy and distribute verbatim copies
...@@ -7,17 +7,15 @@ ...@@ -7,17 +7,15 @@
Preamble Preamble
The GNU General Public License is a free, copyleft license for The GNU Affero General Public License is a free, copyleft license for
software and other kinds of works. software and other kinds of works, specifically designed to ensure
cooperation with the community in the case of network server software.
The licenses for most software and other practical works are designed The licenses for most software and other practical works are designed
to take away your freedom to share and change the works. By contrast, to take away your freedom to share and change the works. By contrast,
the GNU General Public License is intended to guarantee your freedom to our General Public Licenses are intended to guarantee your freedom to
share and change all versions of a program--to make sure it remains free share and change all versions of a program--to make sure it remains free
software for all its users. We, the Free Software Foundation, use the software for all its users.
GNU General Public License for most of our software; it applies also to
any other work released this way by its authors. You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you price. Our General Public Licenses are designed to make sure that you
...@@ -26,44 +24,34 @@ them if you wish), that you receive source code or can get it if you ...@@ -26,44 +24,34 @@ them if you wish), that you receive source code or can get it if you
want it, that you can change the software or use pieces of it in new want it, that you can change the software or use pieces of it in new
free programs, and that you know you can do these things. free programs, and that you know you can do these things.
To protect your rights, we need to prevent others from denying you Developers that use our General Public Licenses protect your rights
these rights or asking you to surrender the rights. Therefore, you have with two steps: (1) assert copyright on the software, and (2) offer
certain responsibilities if you distribute copies of the software, or if you this License which gives you legal permission to copy, distribute
you modify it: responsibilities to respect the freedom of others. and/or modify the software.
For example, if you distribute copies of such a program, whether A secondary benefit of defending all users' freedom is that
gratis or for a fee, you must pass on to the recipients the same improvements made in alternate versions of the program, if they
freedoms that you received. You must make sure that they, too, receive receive widespread use, become available for other developers to
or can get the source code. And you must show them these terms so they incorporate. Many developers of free software are heartened and
know their rights. encouraged by the resulting cooperation. However, in the case of
software used on network servers, this result may fail to come about.
Developers that use the GNU GPL protect your rights with two steps: The GNU General Public License permits making a modified version and
(1) assert copyright on the software, and (2) offer you this License letting the public access it on a server without ever releasing its
giving you legal permission to copy, distribute and/or modify it. source code to the public.
For the developers' and authors' protection, the GPL clearly explains The GNU Affero General Public License is designed specifically to
that there is no warranty for this free software. For both users' and ensure that, in such cases, the modified source code becomes available
authors' sake, the GPL requires that modified versions be marked as to the community. It requires the operator of a network server to
changed, so that their problems will not be attributed erroneously to provide the source code of the modified version running there to the
authors of previous versions. users of that server. Therefore, public use of a modified version, on
a publicly accessible server, gives the public access to the source
Some devices are designed to deny users access to install or run code of the modified version.
modified versions of the software inside them, although the manufacturer
can do so. This is fundamentally incompatible with the aim of An older license, called the Affero General Public License and
protecting users' freedom to change the software. The systematic published by Affero, was designed to accomplish similar goals. This is
pattern of such abuse occurs in the area of products for individuals to a different license, not a version of the Affero GPL, but Affero has
use, which is precisely where it is most unacceptable. Therefore, we released a new version of the Affero GPL which permits relicensing under
have designed this version of the GPL to prohibit the practice for those this license.
products. If such problems arise substantially in other domains, we
stand ready to extend this provision to those domains in future versions
of the GPL, as needed to protect the freedom of users.
Finally, every program is threatened constantly by software patents.
States should not allow patents to restrict development and use of
software on general-purpose computers, but in those that do, we wish to
avoid the special danger that patents applied to a free program could
make it effectively proprietary. To prevent this, the GPL assures that
patents cannot be used to render the program non-free.
The precise terms and conditions for copying, distribution and The precise terms and conditions for copying, distribution and
modification follow. modification follow.
...@@ -72,7 +60,7 @@ modification follow. ...@@ -72,7 +60,7 @@ modification follow.
0. Definitions. 0. Definitions.
"This License" refers to version 3 of the GNU General Public License. "This License" refers to version 3 of the GNU Affero General Public License.
"Copyright" also means copyright-like laws that apply to other kinds of "Copyright" also means copyright-like laws that apply to other kinds of
works, such as semiconductor masks. works, such as semiconductor masks.
...@@ -549,35 +537,45 @@ to collect a royalty for further conveying from those to whom you convey ...@@ -549,35 +537,45 @@ to collect a royalty for further conveying from those to whom you convey
the Program, the only way you could satisfy both those terms and this the Program, the only way you could satisfy both those terms and this
License would be to refrain entirely from conveying the Program. License would be to refrain entirely from conveying the Program.
13. Use with the GNU Affero General Public License. 13. Remote Network Interaction; Use with the GNU General Public License.
Notwithstanding any other provision of this License, if you modify the
Program, your modified version must prominently offer all users
interacting with it remotely through a computer network (if your version
supports such interaction) an opportunity to receive the Corresponding
Source of your version by providing access to the Corresponding Source
from a network server at no charge, through some standard or customary
means of facilitating copying of software. This Corresponding Source
shall include the Corresponding Source for any work covered by version 3
of the GNU General Public License that is incorporated pursuant to the
following paragraph.
Notwithstanding any other provision of this License, you have Notwithstanding any other provision of this License, you have
permission to link or combine any covered work with a work licensed permission to link or combine any covered work with a work licensed
under version 3 of the GNU Affero General Public License into a single under version 3 of the GNU General Public License into a single
combined work, and to convey the resulting work. The terms of this combined work, and to convey the resulting work. The terms of this
License will continue to apply to the part which is the covered work, License will continue to apply to the part which is the covered work,
but the special requirements of the GNU Affero General Public License, but the work with which it is combined will remain governed by version
section 13, concerning interaction through a network will apply to the 3 of the GNU General Public License.
combination as such.
14. Revised Versions of this License. 14. Revised Versions of this License.
The Free Software Foundation may publish revised and/or new versions of The Free Software Foundation may publish revised and/or new versions of
the GNU General Public License from time to time. Such new versions will the GNU Affero General Public License from time to time. Such new versions
be similar in spirit to the present version, but may differ in detail to will be similar in spirit to the present version, but may differ in detail to
address new problems or concerns. address new problems or concerns.
Each version is given a distinguishing version number. If the Each version is given a distinguishing version number. If the
Program specifies that a certain numbered version of the GNU General Program specifies that a certain numbered version of the GNU Affero General
Public License "or any later version" applies to it, you have the Public License "or any later version" applies to it, you have the
option of following the terms and conditions either of that numbered option of following the terms and conditions either of that numbered
version or of any later version published by the Free Software version or of any later version published by the Free Software
Foundation. If the Program does not specify a version number of the Foundation. If the Program does not specify a version number of the
GNU General Public License, you may choose any version ever published GNU Affero General Public License, you may choose any version ever published
by the Free Software Foundation. by the Free Software Foundation.
If the Program specifies that a proxy can decide which future If the Program specifies that a proxy can decide which future
versions of the GNU General Public License can be used, that proxy's versions of the GNU Affero General Public License can be used, that proxy's
public statement of acceptance of a version permanently authorizes you public statement of acceptance of a version permanently authorizes you
to choose that version for the Program. to choose that version for the Program.
...@@ -635,40 +633,29 @@ the "copyright" line and a pointer to where the full notice is found. ...@@ -635,40 +633,29 @@ the "copyright" line and a pointer to where the full notice is found.
Copyright (C) <year> <name of author> Copyright (C) <year> <name of author>
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU Affero General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
Also add information on how to contact you by electronic and paper mail. Also add information on how to contact you by electronic and paper mail.
If the program does terminal interaction, make it output a short If your software can interact with users remotely through a computer
notice like this when it starts in an interactive mode: network, you should also make sure that it provides a way for users to
get its source. For example, if your program is a web application, its
<program> Copyright (C) <year> <name of author> interface could display a "Source" link that leads users to an archive
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. of the code. There are many ways you could offer source, and different
This is free software, and you are welcome to redistribute it solutions will be better for different programs; see section 13 for the
under certain conditions; type `show c' for details. specific requirements.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, your program's commands
might be different; for a GUI interface, you would use an "about box".
You should also get your employer (if you work as a programmer) or school, You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary. if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU GPL, see For more information on this, and how to apply and follow the GNU AGPL, see
<https://www.gnu.org/licenses/>. <https://www.gnu.org/licenses/>.
The GNU General Public License does not permit incorporating your program
into proprietary programs. If your program is a subroutine library, you
may consider it more useful to permit linking proprietary applications with
the library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License. But first, please read
<https://www.gnu.org/licenses/why-not-lgpl.html>.
#!/bin/bash
npm install --save \
lodash
npm install --save-dev \
@types/node \
@types/lodash \
typescript \
'@typescript-eslint/eslint-plugin@^4.28.2' \
'@typescript-eslint/parser@^4.28.2 '\
'eslint@^7.30.0' \
'eslint-config-prettier@^8.3.0' \
'eslint-plugin-prettier@^3.4.0' \
prettier
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -4,8 +4,8 @@ ...@@ -4,8 +4,8 @@
"description": "Check CDN nodes and update AliDNS records.", "description": "Check CDN nodes and update AliDNS records.",
"main": "build/run.js", "main": "build/run.js",
"scripts": { "scripts": {
"build": "./node_modules/.bin/tsc", "build": "tsc",
"start": "node build/run.js" "start": "node dist/run.js"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
...@@ -16,15 +16,24 @@ ...@@ -16,15 +16,24 @@
"dependencies": { "dependencies": {
"@alicloud/pop-core": "^1.7.9", "@alicloud/pop-core": "^1.7.9",
"@types/cron": "^1.7.2", "@types/cron": "^1.7.2",
"@types/node": "^14.0.14",
"@types/q": "^1.5.4", "@types/q": "^1.5.4",
"@types/underscore": "^1.10.3",
"@types/yaml": "^1.9.7", "@types/yaml": "^1.9.7",
"axios": "^0.19.2", "axios": "^0.19.2",
"delay": "^5.0.0",
"icmp": "^1.4.0", "icmp": "^1.4.0",
"lodash": "^4.17.21",
"q": "^1.5.1", "q": "^1.5.1",
"typescript": "^3.9.6",
"underscore": "^1.10.2",
"yaml": "^1.10.0" "yaml": "^1.10.0"
},
"devDependencies": {
"@types/lodash": "^4.14.172",
"@types/node": "^16.7.1",
"@typescript-eslint/eslint-plugin": "^4.29.2",
"@typescript-eslint/parser": "^4.29.2",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.1",
"prettier": "^2.3.2",
"typescript": "^4.3.5"
} }
} }
import Aliyun from "@alicloud/pop-core"; import Aliyun from '@alicloud/pop-core';
import axios from "axios"; import axios from 'axios';
import _, { delay } from "underscore"; import _ from 'lodash';
import YAML from "yaml"; import YAML from 'yaml';
import fs from "fs"; import fs from 'fs';
import { CronJob } from "cron"; import { CronJob } from 'cron';
import net from "net"; import net from 'net';
import { assert } from "console"; import { assert } from 'console';
import Q from "q"; import { ping } from 'icmp';
import { ping } from "icmp"; import delay from 'delay';
interface CDNRecord { interface RecordRule {
match: string; match?: string;
protocol: string; protocol: string;
port: number; port: number;
testDomains?: string[];
source?: string;
} }
interface Config { interface Config {
aliyun: Aliyun.Config; aliyun: Aliyun.Config;
domain: string; domain: string;
cdnRecords: CDNRecord[]; cdnRecords: RecordRule[];
testDomains: string[]; timeout: number;
timeout: number; retryCount: number;
retryCount: number; interval: number;
interval: number;
} }
interface DomainRecordObject { interface DomainRecordObject {
Record: DomainRecord[]; Record: DomainRecord[];
} }
interface DomainRecordReturnResult { interface DomainRecordReturnResult {
RequestId: string; RequestId: string;
TotalCount: number; TotalCount: number;
PageNumber: number; PageNumber: number;
PageSize: number; PageSize: number;
DomainRecords: DomainRecordObject; DomainRecords: DomainRecordObject;
} }
interface DomainRecord { interface DomainRecord {
DomainName: string; DomainName: string;
RecordId: string; RecordId: string;
RR: string; RR: string;
Type: string; Type: string;
Value: string; Value: string;
TTL: number; TTL: number;
Priority: number; Priority: number;
Line: string; Line: string;
Status: string; Status: string;
Locked: boolean; Locked: boolean;
Weight: number; Weight: number;
Remark: string; Remark: string;
} }
let config: Config; let config: Config;
interface DomainRecordInfo { interface DomainRecordInfo {
record: DomainRecord; recordRule: RecordRule;
protocol: string; record: DomainRecord;
port: number; protocol: string;
isCDN: boolean; port: number;
good: boolean; isCDN: boolean;
good: ConnectResult;
} }
const requestOption = { const requestOption = {
method: "POST" method: 'POST',
};
enum ConnectResult {
SourceBad,
CDNBad,
Good,
} }
class Checker { class Checker {
config: Config; private client: Aliyun;
client: Aliyun; private cdnRecordsRegex: RegExp[];
cdnRecordsRegex: RegExp[]; static order = 0;
static order: number = 0; private id: number;
id: number; private records: DomainRecordInfo[];
records: DomainRecordInfo[]; private nonCDNRecords: DomainRecordInfo[];
nonCDNRecords: DomainRecordInfo[]; private CDNRecords: DomainRecordInfo[];
CDNRecords: DomainRecordInfo[]; private checkMethods = new Map<
checkMethods: Map<string, (protocol: string, address: string, port: number) => Promise<boolean>>; string,
async checkHttpOrHttps(protocol: string, address: string, port: number) { (record: RecordRule, address: string) => Promise<ConnectResult>
let currentTestDomain: string; >();
try { private availableRecordRules: RecordRule[];
for (let testDomain of this.config.testDomains) {
currentTestDomain = testDomain; private async filterRecordRules() {
await axios.get(`${protocol}://${address}:${port}`, { this.availableRecordRules = [];
headers: { await Promise.all(this.config.cdnRecords.map(async (rule) => {
Host: testDomain if (rule.protocol === 'tcp' && rule.source) {
}, this.message(`Checking source ${rule.source}:${rule.port}.`);
timeout: this.config.timeout, try {
validateStatus: status => status < 500 const ms = await this.checkTcpProcess(rule.source, rule.port)
}); this.availableRecordRules.push(rule);
} this.message(`Source ${rule.source}:${rule.port} is good: ${ms} ms.`)
this.message(`Node ${address}:${port} is good.`); } catch (e) {
return true; this.message(`Skipping rule ${rule.match} fo source ${rule.source}:${rule.port}r unhealthy: ${e.toString()}`)
} catch (e) { }
this.message(`Node ${address}:${port} failed in checking ${currentTestDomain}: ${e.toString()}`); } else if (rule.source && (rule.protocol === 'http' || rule.protocol === 'https')) {
return false; const availableTestDomains: string[] = [];
} for (const domain of rule.testDomains) {
} this.message(`Checking source domain ${domain}:${rule.port}.`);
async checkTcpProcess(address: string, port: number) { const errMessage = await this.tryConnectHttp(rule.protocol, domain, rule.port, domain);
return new Promise<number>((resolve, reject) => { if (errMessage) {
const hrstart = process.hrtime(); this.message(`Skipping domain ${domain} of rule ${rule.match} for bad source: ${errMessage}`);
const socket = new net.Socket(); } else {
this.message(`Source domain ${domain} is good.`);
socket.connect(port, address); availableTestDomains.push(domain);
socket.setTimeout(this.config.timeout || 1000); }
}
socket.on('connect', () => { if (availableTestDomains.length) {
socket.destroy(); rule.testDomains = availableTestDomains;
resolve(milliseconds()); this.availableRecordRules.push(rule);
}); } else {
this.message(`Skipping rule ${rule.match} for no available sources.`);
socket.on('error', (error) => { }
socket.destroy(); } else {
reject(error); this.availableRecordRules.push(rule);
}); }
}))
socket.on('timeout', (error) => { }
socket.destroy();
reject(error || 'socket TIMEOUT'); private async connectHttpProcess(url: string, hostHeader: string) {
}); try {
await axios.get(url, {
function milliseconds() { headers: {Host: hostHeader},
const hrend = process.hrtime(hrstart); timeout: this.config.timeout,
const ms = hrend[0] * 1e3 + hrend[1] / 1e6; validateStatus: (status) => status < 500,
return Math.floor(ms); });
} return null;
}); } catch (e) {
} return `${url}: ${e.toString()}`;
async checkTcp(address: string, port: number) { }
try { }
const ms = await this.checkTcpProcess(address, port);
this.message(`Node ${address}:${port} is good: ${ms} ms.`); private async tryConnectHttp(
return true; protocol: string,
} catch (e) { address: string,
this.message(`Node ${address}:${port} failed: ${e.toString()}`); port: number,
return false; hostHeader: string,
} ) {
} const url = `${protocol}://${address}:${port}`;
async checkIcmp(address: string) { // const urlKey = `${url}-${hostHeader}`;
try { return this.connectHttpProcess(url, hostHeader);
await ping(address, this.config.timeout); }
this.message(`Node ${address}:ICMP is good.`); async checkHttpOrHttps(record: RecordRule, address: string) {
return true; let good = false;
} catch (e) { if (!record.testDomains.length) {
this.message(`Node ${address}:ICMP failed: ${e.toString()}`); return ConnectResult.SourceBad;
return false; }
} for (const testDomain of record.testDomains) {
} /*const sourceErrorMessage = await this.tryConnectHttp(
constructor(config: Config) { record.protocol,
this.config = config; testDomain,
this.client = new Aliyun(config.aliyun); record.port,
this.cdnRecordsRegex = config.cdnRecords.map(m => new RegExp(m.match)); testDomain
this.id = ++Checker.order; );
this.checkMethods = new Map(); if (sourceErrorMessage != null) {
this.checkMethods.set('http', (protocol: string, address: string, port: number) => { this.message(
return this.checkHttpOrHttps(protocol, address, port); `Source ${testDomain}:${record.port} is broken: ${sourceErrorMessage}`,
}); );
this.checkMethods.set('https', (protocol: string, address: string, port: number) => { // return ConnectResult.SourceBad;
return this.checkHttpOrHttps(protocol, address, port); continue;
}); }*/
this.checkMethods.set('tcp', (protocol: string, address: string, port: number) => { const nodeErrorMessage = await this.tryConnectHttp(
return this.checkTcp(address, port); record.protocol,
}); address,
this.checkMethods.set('icmp', (protocol: string, address: string, port: number) => { record.port,
return this.checkIcmp(address); testDomain,
}); );
} if (nodeErrorMessage != null) {
private message(msg: string) { this.message(
console.log(`${this.id} => ${msg}`); `Node ${address}:${record.port} is broken: ${nodeErrorMessage}`,
} );
getRecordPrefix(m: DomainRecord) { return ConnectResult.CDNBad;
if (!m.Value.endsWith(this.config.domain)) { } else {
return null; good = true;
} }
return m.Value.slice(0, m.Value.length - 1 - this.config.domain.length); }
} if (!good) {
isCDNRecord(m: DomainRecord) { this.message(`Node ${address}:${record.port} skipped for no sources.`);
const valuePrefix = this.getRecordPrefix(m); return ConnectResult.SourceBad;
return valuePrefix && _.any(this.cdnRecordsRegex, r => !!valuePrefix.match(r)); }
} this.message(`Node ${address}:${record.port} is good.`);
getRecordPattern(recordInfo: DomainRecordInfo) { return ConnectResult.Good;
const record = recordInfo.record; }
return `${record.RR}.${this.config.domain} => ${record.Value}:${recordInfo.port}`; async checkTcpProcess(address: string, port: number) {
} return new Promise<number>((resolve, reject) => {
async getRecords(): Promise<DomainRecordInfo[]> { const hrstart = process.hrtime();
this.message(`Fetching domain records of ${this.config.domain}.`) const socket = new net.Socket();
const res: DomainRecordInfo[] = [];
for (let i = 1; ; ++i) { socket.connect(port, address);
const ret: DomainRecordReturnResult = await this.client.request("DescribeDomainRecords", { socket.setTimeout(this.config.timeout || 1000);
DomainName: this.config.domain,
PageNumber: i, socket.on('connect', () => {
PageSize: 500, socket.destroy();
}, requestOption); resolve(milliseconds());
//this.message(`${ret.TotalCount} records found.`); });
if (!ret.DomainRecords.Record.length) {
break; socket.on('error', (error) => {
} socket.destroy();
for (let record of ret.DomainRecords.Record.filter(m => { reject(error);
return m.RR && m.Type === "CNAME" && _.any(this.cdnRecordsRegex, r => !!m.RR.match(r)) });
})) {
const matchCDNRecord = _.find(this.config.cdnRecords, r => record.RR.match(r.match)); socket.on('timeout', (error) => {
const { port, protocol } = matchCDNRecord; socket.destroy();
const isCDN = this.isCDNRecord(record); reject(error || 'socket TIMEOUT');
const recordInfo: DomainRecordInfo = { record, protocol, port, isCDN, good: false }; });
this.message(`Found record ${this.getRecordPattern(recordInfo)}`);
res.push(recordInfo); function milliseconds() {
} const hrend = process.hrtime(hrstart);
} const ms = hrend[0] * 1e3 + hrend[1] / 1e6;
return res; return Math.floor(ms);
} }
async checkNode(protocol: string, address: string, port: number): Promise<boolean> { });
let currentTestDomain: string; }
const checkMethodFunction = this.checkMethods.get(protocol); async checkTcp(record: RecordRule, address: string) {
assert(checkMethodFunction, `Check method ${protocol} not supported.`); /*if (record.source) {
for (let i = 1; i <= this.config.retryCount; ++i) { try {
if (await checkMethodFunction(protocol, address, port)) { await this.checkTcpProcess(record.source, record.port);
return true; } catch (e) {
} this.message(
} `Source ${address}:${record.port} failed: ${e.toString()}`,
this.message(`Node ${address}:${port} is bad.`); );
return false; return ConnectResult.SourceBad;
} }
async checkRecord(recordInfo: DomainRecordInfo) { }*/
const record = recordInfo.record; try {
this.message(`Checking record ${this.getRecordPattern(recordInfo)} with old status of ${record.Status}.`) const ms = await this.checkTcpProcess(address, record.port);
const good = await this.checkNode(recordInfo.protocol, record.Value, recordInfo.port); this.message(`Node ${address}:${record.port} is good: ${ms} ms.`);
await this.handleRecordResult(recordInfo, good); return ConnectResult.Good;
} } catch (e) {
isRecordGood(recordInfo: DomainRecordInfo): boolean { this.message(`Node ${address}:${record.port} failed: ${e.toString()}`);
if (recordInfo.isCDN) { return ConnectResult.CDNBad;
const valuePrefix = this.getRecordPrefix(recordInfo.record); }
return _.any(this.records, r => { }
return r.record.RR === valuePrefix && this.isRecordGood(r); async checkIcmp(address: string) {
}); try {
} else { await ping(address, this.config.timeout);
return recordInfo.good; this.message(`Node ${address}:ICMP is good.`);
} return ConnectResult.Good;
} } catch (e) {
async checkCDNRecord(recordInfo: DomainRecordInfo) { this.message(`Node ${address}:ICMP failed: ${e.toString()}`);
this.message(`Checking CDN record ${this.getRecordPattern(recordInfo)} with old status of ${recordInfo.record.Status}.`) return ConnectResult.CDNBad;
const good = this.isRecordGood(recordInfo); }
this.message(`CDN Record ${this.getRecordPattern(recordInfo)} is ${good ? "good" : "bad"}.`); }
await this.handleRecordResult(recordInfo, good); constructor(private config: Config) {
} this.client = new Aliyun(config.aliyun);
async handleRecordResult(recordInfo: DomainRecordInfo, good: boolean) { this.cdnRecordsRegex = config.cdnRecords.map((m) => new RegExp(m.match));
const record = recordInfo.record; this.id = ++Checker.order;
const status = record.Status; this.checkMethods.set('http', (record: RecordRule, address: string) => {
recordInfo.good = good; return this.checkHttpOrHttps(record, address);
const targetStatus = good ? "ENABLE" : "DISABLE"; });
if (status != targetStatus) { this.checkMethods.set('https', (record: RecordRule, address: string) => {
this.message(`Changing record status of ${this.getRecordPattern(recordInfo)} from ${status} to ${targetStatus}.`); return this.checkHttpOrHttps(record, address);
await this.client.request("SetDomainRecordStatus", { });
RecordId: record.RecordId, this.checkMethods.set('tcp', (record: RecordRule, address: string) => {
Status: targetStatus return this.checkTcp(record, address);
}, requestOption); });
} this.checkMethods.set('icmp', (record: RecordRule, address: string) => {
} return this.checkIcmp(address);
async start() { });
this.message(`Started.`); }
this.records = await this.getRecords(); private message(msg: string) {
this.nonCDNRecords = this.records.filter(m => !m.isCDN); console.log(`${this.id} => ${msg}`);
this.CDNRecords = this.records.filter(m => !!m.isCDN); }
await Promise.all(this.nonCDNRecords.map(r => { getRecordPrefix(m: DomainRecord) {
return this.checkRecord(r); if (!m.Value.endsWith(this.config.domain)) {
})); return null;
await Promise.all(this.CDNRecords.map(r => { }
return this.checkCDNRecord(r); return m.Value.slice(0, m.Value.length - 1 - this.config.domain.length);
})); }
this.message(`Finished.`); isCDNRecord(m: DomainRecord) {
} const valuePrefix = this.getRecordPrefix(m);
return (
valuePrefix && this.cdnRecordsRegex.some((r) => !!valuePrefix.match(r))
);
}
getRecordPattern(recordInfo: DomainRecordInfo) {
const record = recordInfo.record;
return `${record.RR}.${this.config.domain} => ${record.Value}:${recordInfo.port}`;
}
async getRecords(): Promise<DomainRecordInfo[]> {
this.message(`Fetching domain records of ${this.config.domain}.`);
const res: DomainRecordInfo[] = [];
for (let i = 1; ; ++i) {
const ret: DomainRecordReturnResult = await this.client.request(
'DescribeDomainRecords',
{
DomainName: this.config.domain,
PageNumber: i,
PageSize: 500,
},
requestOption,
);
//this.message(`${ret.TotalCount} records found.`);
if (!ret.DomainRecords.Record.length) {
break;
}
for (const record of ret.DomainRecords.Record.filter((m) => {
return (
m.RR &&
m.Type === 'CNAME' &&
this.cdnRecordsRegex.some((r) => !!m.RR.match(r))
);
})) {
const matchCDNRecord = this.availableRecordRules.find((r) =>
record.RR.match(r.match),
);
const { port, protocol } = matchCDNRecord;
const isCDN = this.isCDNRecord(record);
const recordInfo: DomainRecordInfo = {
recordRule: matchCDNRecord,
record,
protocol,
port,
isCDN,
good: null,
};
this.message(`Found record ${this.getRecordPattern(recordInfo)}`);
res.push(recordInfo);
}
}
return res;
}
async checkNode(record: RecordRule, address: string): Promise<ConnectResult> {
const checkMethodFunction = this.checkMethods.get(record.protocol);
assert(
checkMethodFunction,
`Check method ${record.protocol} not supported.`,
);
let lastResult: ConnectResult;
for (let i = 1; i <= this.config.retryCount; ++i) {
const result = await checkMethodFunction(record, address);
if (result == ConnectResult.Good) {
return result;
}
lastResult = result;
}
if (lastResult === ConnectResult.CDNBad) {
this.message(`Node ${address}:${record.port} is bad.`);
} else {
this.message(`Node ${address}:${record.port} skipped for source broken.`);
}
return lastResult;
}
async checkRecord(recordInfo: DomainRecordInfo) {
const record = recordInfo.record;
this.message(
`Checking record ${this.getRecordPattern(
recordInfo,
)} with old status of ${record.Status}.`,
);
const good = await this.checkNode(recordInfo.recordRule, record.Value);
await this.handleRecordResult(recordInfo, good);
}
isRecordGood(recordInfo: DomainRecordInfo): boolean {
if (recordInfo.isCDN) {
const valuePrefix = this.getRecordPrefix(recordInfo.record);
return this.records.some((r) => {
return r.record.RR === valuePrefix && this.isRecordGood(r);
});
} else {
return recordInfo.good !== ConnectResult.CDNBad;
}
}
async checkCDNRecord(recordInfo: DomainRecordInfo) {
this.message(
`Checking CDN record ${this.getRecordPattern(
recordInfo,
)} with old status of ${recordInfo.record.Status}.`,
);
const good = this.isRecordGood(recordInfo);
this.message(
`CDN Record ${this.getRecordPattern(recordInfo)} is ${
good ? 'good' : 'bad'
}.`,
);
await this.handleRecordResult(
recordInfo,
good ? ConnectResult.Good : ConnectResult.CDNBad,
);
}
async handleRecordResult(recordInfo: DomainRecordInfo, good: ConnectResult) {
const record = recordInfo.record;
const status = record.Status;
recordInfo.good = good;
if (good === ConnectResult.SourceBad) {
return;
}
const targetStatus = good === ConnectResult.Good ? 'ENABLE' : 'DISABLE';
if (status != targetStatus) {
this.message(
`Changing record status of ${this.getRecordPattern(
recordInfo,
)} from ${status} to ${targetStatus}.`,
);
await this.client.request(
'SetDomainRecordStatus',
{
RecordId: record.RecordId,
Status: targetStatus,
},
requestOption,
);
}
}
async start() {
this.message(`Started.`);
await this.filterRecordRules();
this.records = await this.getRecords();
this.nonCDNRecords = this.records.filter((m) => !m.isCDN);
this.CDNRecords = this.records.filter((m) => !!m.isCDN);
await Promise.all(
this.nonCDNRecords.map((r) => {
return this.checkRecord(r);
}),
);
await Promise.all(
this.CDNRecords.map((r) => {
return this.checkCDNRecord(r);
}),
);
this.message(`Finished.`);
}
} }
async function run() { async function run() {
const checker = new Checker(config); const checker = new Checker(_.cloneDeep(config));
await checker.start(); await checker.start();
} }
async function main() { async function main() {
config = YAML.parse(await fs.promises.readFile("./config.yaml", "utf8")); config = YAML.parse(await fs.promises.readFile('./config.yaml', 'utf8'));
//await run(); //await run();
while (true) { while (true) {
await run(); await run();
await Q.delay(config.interval); await delay(config.interval);
} }
} }
main(); main();
{ {
"compilerOptions": { "compilerOptions": {
"outDir": "build", "outDir": "dist",
"module": "commonjs", "module": "commonjs",
"target": "esnext", "target": "esnext",
"esModuleInterop": true, "esModuleInterop": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"declaration": true,
"sourceMap": true "sourceMap": true
}, },
"compileOnSave": true, "compileOnSave": true,
"allowJs": true, "allowJs": true,
"include": [ "include": [
"*.ts" "*.ts",
"src/**/*.ts",
"test/**/*.ts"
] ]
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment