support more services for testing internet reachability
Massimo Melina committed
Sep 29, 2023 at 23:39 UTC
db113eceaf1ce2061f5b459c9fe6840e2496c9dc
4 files changed
+19
-18
central.json
+8
-5
@@ -5,19 +5,22 @@
5
"url": "https://ports.yougetsignal.com/check-port.php",
6
"headers": {"content-type": "application/x-www-form-urlencoded"},
7
"method": "post",
8
- "selector": "p:nth-child(1)",
8
"body": "remoteAddress=$IP&portNumber=$PORT",
9
"regexpFailure": "is closed",
10
"regexpSuccess": "is open"
11
},
12
{
14
- "url": "https://canyouseeme.org/",
13
+ "url": "https://canyouseeme.org",
14
"headers": {"content-type": "application/x-www-form-urlencoded"},
15
"method": "post",
17
- "selector": "div.tool > p:nth-child(1)",
16
"body": "port=$PORT",
19
- "regexpFailure": "I could not see your service",
20
- "regexpSuccess": "I can see your service"
17
+ "regexpFailure": "Error:",
18
+ "regexpSuccess": "Success:"
19
+ },
20
+ {
21
+ "url": "https://ifconfig.co/port/$PORT",
22
+ "regexpFailure": "reachable\": false",
23
+ "regexpSuccess": "reachable\": true"
24
}
25
],
26
"publicIpServices": [
package.json
-1
@@ -79,7 +79,6 @@
79
"lodash": "^4.17.21",
80
"minimist": "^1.2.6",
81
"nat-upnp-ts": "^2.0.1",
82
- "node-html-parser": "^6.1.5",
82
"open": "^8.4.0",
83
"tssrp6a": "^3.0.0",
84
"unzip-stream": "^0.3.1",
src/api.net.ts
+9
-11
@@ -6,8 +6,6 @@ import {
6
HTTP_BAD_REQUEST, HTTP_FAILED_DEPENDENCY, HTTP_OK, HTTP_SERVER_ERROR, HTTP_SERVICE_UNAVAILABLE,
7
IS_MAC, IS_WINDOWS
8
} from './const'
9
-import axios from 'axios'
10
-import {parse} from 'node-html-parser'
9
import _ from 'lodash'
10
import { cert, getCertObject, getIps, getServerStatus, privateKey } from './listen'
11
import { getProjectInfo } from './github'
@@ -191,16 +189,12 @@ async function checkPort(ip: string, port: number) {
189
const prjInfo = await getProjectInfo()
190
for (const services of _.chunk(_.shuffle<PortScannerService>(prjInfo.checkServerServices), 2)) {
191
try {
194
- return Promise.any(services.map(async svc => {
195
- const service = new URL(svc.url).hostname
192
+ return Promise.any(services.map(async ({ url, body, selector, regexpSuccess, regexpFailure, ...rest }) => {
193
+ const service = new URL(url).hostname
194
console.log('trying service', service)
197
- const api = (axios as any)[svc.method]
198
- const body = svc.body?.replace('$IP', ip).replace('$PORT', String(port)) || ''
199
- const res = await api(svc.url, body, {headers: svc.headers})
200
- const parsed = parse(res.data).querySelector(svc.selector)?.innerText
201
- if (!parsed) throw console.debug('empty:' + service)
202
- const success = new RegExp(svc.regexpSuccess).test(parsed)
203
- const failure = new RegExp(svc.regexpFailure).test(parsed)
195
+ const res = await httpString(applySymbols(url)!, { ...rest, body: applySymbols(body) })
196
+ const success = new RegExp(regexpSuccess).test(res)
197
+ const failure = new RegExp(regexpFailure).test(res)
198
if (success === failure) throw console.debug('inconsistent:' + service) // this result cannot be trusted
199
console.debug(service, 'responded', success)
200
return { success, service }
@@ -208,6 +202,10 @@ async function checkPort(ip: string, port: number) {
202
}
203
catch {}
204
}
205
+
206
+ function applySymbols(s?: string) {
207
+ return s?.replace('$IP', ip).replace('$PORT', String(port))
208
+ }
209
}
210
211
const apis: ApiHandlers = {
src/github.ts
+2
-1
@@ -13,6 +13,7 @@ import {
13
import { ApiError } from './apiMiddleware'
14
import _ from 'lodash'
15
import {
16
+ DEV,
17
HFS_REPO,
18
HFS_REPO_BRANCH,
19
HTTP_BAD_REQUEST,
@@ -190,5 +191,5 @@ let builtIn = JSON.parse(readFileSync(join(__dirname, '..', FN), 'utf8'))
191
export const getProjectInfo = debounceAsync(
192
() => readGithubFile(`${HFS_REPO}/${HFS_REPO_BRANCH}/${FN}`)
193
.then(JSON.parse, () => null)
193
- .then(x => Object.assign(Object.create(builtIn), x) ), // fall back to built-in
194
+ .then(x => Object.assign(Object.create(builtIn), DEV ? null : x) ), // fall back to built-in
195
0, { retain: DAY, retainFailure: 60_000 } )
\ No newline at end of file