diff options
| author | Haishan <[email protected]> | 2019-12-01 22:41:59 +0800 |
|---|---|---|
| committer | Haishan <[email protected]> | 2019-12-01 22:41:59 +0800 |
| commit | 8b5ecb3f1839808d5e88f635d286fcfdfffd4f86 (patch) | |
| tree | fbbaef42b57a1fe3cb244103ccbb58915e631c66 /src/api | |
| parent | 19ecf435de90800fe284e3333b3a4957d600f410 (diff) | |
feat: support close all connections
for https://github.com/haishanh/yacd/issues/338
Diffstat (limited to 'src/api')
| -rw-r--r-- | src/api/connections.js | 38 |
1 files changed, 36 insertions, 2 deletions
diff --git a/src/api/connections.js b/src/api/connections.js index 594fc5e..fbc9abd 100644 --- a/src/api/connections.js +++ b/src/api/connections.js @@ -1,10 +1,39 @@ +import { getURLAndInit } from 'm/request-helper'; + const endpoint = '/connections'; let fetched = false; let subscribers = []; +// see also https://github.com/Dreamacro/clash/blob/dev/constant/metadata.go#L41 +type UUID = string; +type ConnectionItem = { + id: UUID, + metadata: { + network: 'tcp' | 'udp', + type: 'HTTP' | 'HTTP Connect' | 'Socks5' | 'Redir' | 'Unknown', + sourceIP: string, + destinationIP: string, + sourcePort: string, + destinationPort: string, + host: string + }, + upload: number, + download: number, + // e.g. "2019-11-30T22:48:13.416668+08:00", + start: string, + chains: Array<string>, + // e.g. 'Match', 'DomainKeyword' + rule: string +}; +type ConnectionsData = { + downloadTotal: number, + uploadTotal: number, + connections: Array<ConnectionItem> +}; + function appendData(s) { - let o; + let o: ConnectionsData; try { o = JSON.parse(s); } catch (err) { @@ -48,4 +77,9 @@ function subscribe(listener) { }; } -export { fetchData }; +async function closeAllConnections(apiConfig) { + const { url, init } = getURLAndInit(apiConfig); + return await fetch(url + endpoint, { ...init, method: 'DELETE' }); +} + +export { fetchData, closeAllConnections }; |
