-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
240 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import http from 'node:http' | ||
import https from 'node:https' | ||
|
||
/** | ||
* ping | ||
* @param {string} url 地址 | ||
* @param {number} port 端口 | ||
* @returns {Promise<number>} -1=失败 | ||
*/ | ||
export default function ping(url: string, port?: number): Promise<number> { | ||
const promise = new Promise<number>((resolve) => { | ||
if (!url || !url.startsWith('http')) | ||
return -1 | ||
const isHttps = url.startsWith('https') | ||
const send = isHttps ? https.request : http.request | ||
const outPort = port || (isHttps ? 443 : 80) | ||
const baseUrl = url.replace('http://', '').replace('https://', '') | ||
|
||
const options = { host: baseUrl, port: outPort, path: '/' } | ||
const start = Date.now() | ||
|
||
const req = send(options, () => { | ||
resolve(Date.now() - start) | ||
req.destroy() | ||
}) | ||
|
||
req.on('error', () => { | ||
req.destroy() | ||
resolve(-1) | ||
}) | ||
|
||
req.write('') | ||
req.end() | ||
}) | ||
return promise | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// @ts-expect-error | ||
import { readJson } from 'fs-extra/esm' | ||
import debounce from 'lodash-es/debounce' | ||
import { JSONFilePreset } from 'lowdb/node' | ||
import type { Low } from 'lowdb/lib' | ||
import { RULE_EXTRA_PATH } from './constants' | ||
import _ping from './ping' | ||
|
||
export interface SourceExtraRow { | ||
ping?: number | ||
} | ||
|
||
export interface SourceExtra { | ||
[_: string]: SourceExtraRow | ||
} | ||
|
||
let mDb: Low<SourceExtra> | ||
|
||
async function getDb() { | ||
if (mDb) | ||
return mDb | ||
const data = await readJson(RULE_EXTRA_PATH).catch(() => ({})) | ||
mDb = await JSONFilePreset<SourceExtra>(RULE_EXTRA_PATH, data) | ||
return mDb | ||
} | ||
|
||
const writeDB = debounce(async () => { | ||
const db = await getDb() | ||
db.write() | ||
}, 500) | ||
|
||
export async function ping(id: string, host: string) { | ||
const db = await getDb() | ||
if (!db.data[id]) | ||
db.data[id] = {} | ||
db.data[id].ping = await _ping(host) | ||
writeDB() | ||
return db.data[id] | ||
} | ||
|
||
export async function getExtraInfoById(id: string): Promise<SourceExtraRow> { | ||
const db = await getDb() | ||
return db.data[id] | ||
} | ||
|
||
export async function getRuleExtras(): Promise<SourceExtra> { | ||
const db = await getDb() | ||
return db.data | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,4 @@ declare global { | |
} | ||
} | ||
|
||
export {}; | ||
export {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { getRuleExtras } from '@/api'; | ||
|
||
interface SourceExtraRow { | ||
ping?: number; | ||
} | ||
|
||
interface SourceExtra { | ||
[_: string]: SourceExtraRow; | ||
} | ||
|
||
export function useRuleExtra() { | ||
const data = ref<SourceExtra>({}); | ||
|
||
async function sync() { | ||
const res = await getRuleExtras().catch(() => {}); | ||
if (res?.code === 0) { | ||
data.value = res?.data || []; | ||
} | ||
} | ||
|
||
return { | ||
data, | ||
sync | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export function timeoutWith(tasks: any, t = 6000) { | ||
const delay = () => | ||
new Promise((_, reject) => { | ||
setTimeout(reject, t); | ||
}); | ||
|
||
return Promise.race([tasks, delay()]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default (t: number) => new Promise((c) => setTimeout(c, t)); |