summaryrefslogtreecommitdiff
path: root/src/api/connections.js
diff options
context:
space:
mode:
authorHaishan <[email protected]>2019-12-01 22:41:59 +0800
committerHaishan <[email protected]>2019-12-01 22:41:59 +0800
commit8b5ecb3f1839808d5e88f635d286fcfdfffd4f86 (patch)
treefbbaef42b57a1fe3cb244103ccbb58915e631c66 /src/api/connections.js
parent19ecf435de90800fe284e3333b3a4957d600f410 (diff)
feat: support close all connections
for https://github.com/haishanh/yacd/issues/338
Diffstat (limited to 'src/api/connections.js')
-rw-r--r--src/api/connections.js38
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 };