From 8b5ecb3f1839808d5e88f635d286fcfdfffd4f86 Mon Sep 17 00:00:00 2001 From: Haishan Date: Sun, 1 Dec 2019 22:41:59 +0800 Subject: feat: support close all connections for https://github.com/haishanh/yacd/issues/338 --- src/api/connections.js | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) (limited to 'src/api') 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, + // e.g. 'Match', 'DomainKeyword' + rule: string +}; +type ConnectionsData = { + downloadTotal: number, + uploadTotal: number, + connections: Array +}; + 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 }; -- cgit v1.3.1