Add stat API

This commit is contained in:
Wildan M 2022-08-16 17:32:01 +07:00
commit 468dcd8715
6 changed files with 80 additions and 12 deletions

View file

@ -1,4 +1,5 @@
HTTP_PORT=80 HTTP_PORT=80
HTTPS_PORT=443 HTTPS_PORT=443
BLACKLIST_HOSTS= STAT_PORT=8081
BLACKLIST_REDIRECT= BLACKLIST_HOSTS="bad.example,evil.example"
BLACKLIST_REDIRECT="https://forwarddomain.net/blacklisted"

View file

@ -1,5 +1,9 @@
# CHANGES # CHANGES
## v2.3 (2022-08-16)
+ Add stat API `s.forwarddomain.net`, separate node script.
## v2.2 (2022-06-16) ## v2.2 (2022-06-16)
+ Moving all parameters to `.env` file + Moving all parameters to `.env` file

View file

@ -79,7 +79,11 @@ We only keep caches of DNS records and SSL certs. This also means we can see how
Star our repo and spread the word, please :) Star our repo and spread the word, please :)
Additionally, you can also help us [cover hosting costs](https://ko-fi.com/willnode). Additionally, you can also help us [cover hosting costs](https://github.com/sponsors/willnode).
## Credits
Things in `package.json`. I also borrow code from [zbo14/certnode](https://github.com/zbo14/certnode).
## Usual Disclaimer ## Usual Disclaimer

13
package-lock.json generated
View file

@ -5,6 +5,7 @@
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "forward-domain",
"version": "1.0.0", "version": "1.0.0",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
@ -1208,9 +1209,9 @@
"dev": true "dev": true
}, },
"node_modules/moment": { "node_modules/moment": {
"version": "2.29.3", "version": "2.29.4",
"resolved": "https://registry.npmjs.org/moment/-/moment-2.29.3.tgz", "resolved": "https://registry.npmjs.org/moment/-/moment-2.29.4.tgz",
"integrity": "sha512-c6YRvhEo//6T2Jz/vVtYzqBzwvPT95JBQ+smCytzf7c50oMZRsR/a4w88aD34I+/QVSfnoAnSBFPJHItlOMJVw==", "integrity": "sha512-5LC9SOxjSc2HF6vO2CyuTDNivEdoz2IvyJJGj6X8DJ0eFyfszE0QiEd+iXmBvUP3WHxSjFH/vIsA0EN00cgr8w==",
"dev": true, "dev": true,
"engines": { "engines": {
"node": "*" "node": "*"
@ -3041,9 +3042,9 @@
"dev": true "dev": true
}, },
"moment": { "moment": {
"version": "2.29.3", "version": "2.29.4",
"resolved": "https://registry.npmjs.org/moment/-/moment-2.29.3.tgz", "resolved": "https://registry.npmjs.org/moment/-/moment-2.29.4.tgz",
"integrity": "sha512-c6YRvhEo//6T2Jz/vVtYzqBzwvPT95JBQ+smCytzf7c50oMZRsR/a4w88aD34I+/QVSfnoAnSBFPJHItlOMJVw==", "integrity": "sha512-5LC9SOxjSc2HF6vO2CyuTDNivEdoz2IvyJJGj6X8DJ0eFyfszE0QiEd+iXmBvUP3WHxSjFH/vIsA0EN00cgr8w==",
"dev": true "dev": true
}, },
"moment-timezone": { "moment-timezone": {

View file

@ -1,7 +1,7 @@
{ {
"name": "forward-domain", "name": "forward-domain",
"version": "1.0.0", "version": "1.0.0",
"description": "", "description": "Public service to forward domain for free",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1", "test": "echo \"Error: no test specified\" && exit 1",
@ -10,8 +10,8 @@
"count": "find .certs -type d | grep '.\\.' | wc -l" "count": "find .certs -type d | grep '.\\.' | wc -l"
}, },
"keywords": [], "keywords": [],
"author": "", "author": "Wildan Mubarok",
"license": "ISC", "license": "MIT",
"dependencies": { "dependencies": {
"await-lock": "^2.1.0", "await-lock": "^2.1.0",
"axios": "^0.21.1", "axios": "^0.21.1",

58
stat.js Normal file
View file

@ -0,0 +1,58 @@
// separate stat endpoint
require('dotenv').config()
const { execSync } = require('child_process');
const http = require('http');
const updateStat = function () {
// run npm stat
var buffer = execSync('npm run count');
var lines = buffer.toString('utf-8').trimEnd().split('\n');
var stat = {
domains: parseInt(lines[lines.length - 1]),
iat: Date.now(),
exp: Date.now() + 1000 * 60 * 60 * 24,
}
return stat;
}
let cacheStat = updateStat();
const listener = async function ( /** @type {import('http').IncomingMessage} */ req, /** @type {import('http').ServerResponse} */ res) {
try {
switch (req.url) {
case '/':
if (cacheStat.exp < Date.now()) {
cacheStat = updateStat();
}
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(cacheStat));
break;
default:
res.writeHead(404, {
'content-type': 'application/json'
});
res.write(JSON.stringify({
error: 'Unknown url'
}));
break;
}
return;
} catch (error) {
res.writeHead(400);
res.write(error.message || 'Unknown error');
} finally {
res.end();
}
}
const server = http.createServer(listener);
if (require.main === module) {
const port = parseInt(process.env.STAT_PORT || "3000");
server.listen(port, function () {
console.log(`server start at port ${port}`);
});
} else {
module.exports = server
}