From ff1a39d04e53b428e34d46c55ecd6689189b5443 Mon Sep 17 00:00:00 2001 From: Haishan Date: Sat, 31 Oct 2020 18:18:04 +0800 Subject: chore: run ts-migrate --- src/api/configs.ts | 3 +- src/api/connections.ts | 10 +++-- src/api/proxies.js | 66 ---------------------------- src/api/proxies.ts | 66 ++++++++++++++++++++++++++++ src/api/traffic.js | 117 ------------------------------------------------- src/api/traffic.ts | 117 +++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 191 insertions(+), 188 deletions(-) delete mode 100644 src/api/proxies.js create mode 100644 src/api/proxies.ts delete mode 100644 src/api/traffic.js create mode 100644 src/api/traffic.ts (limited to 'src/api') diff --git a/src/api/configs.ts b/src/api/configs.ts index 4957e85..69b02d4 100644 --- a/src/api/configs.ts +++ b/src/api/configs.ts @@ -1,4 +1,5 @@ import { getURLAndInit } from 'src/misc/request-helper'; +import { ClashGeneralConfig } from 'src/store/types'; import { ClashAPIConfig } from 'src/types'; const endpoint = '/configs'; @@ -12,7 +13,7 @@ export async function fetchConfigs(apiConfig: ClashAPIConfig) { // req body // { Path: string } -type ClashConfigPartial = { 'socks-port'?: unknown }; +type ClashConfigPartial = Partial; function configsPatchWorkaround(o: ClashConfigPartial) { // backward compatibility for older clash using `socket-port` if ('socks-port' in o) { diff --git a/src/api/connections.ts b/src/api/connections.ts index 7608a41..d4ed58e 100644 --- a/src/api/connections.ts +++ b/src/api/connections.ts @@ -9,11 +9,13 @@ const subscribers = []; // see also https://github.com/Dreamacro/clash/blob/dev/constant/metadata.go#L41 type UUID = string; -type ConnectionItem = { +type ConnNetwork = 'tcp' | 'udp'; +type ConnType = 'HTTP' | 'HTTP Connect' | 'Socks5' | 'Redir' | 'Unknown'; +export type ConnectionItem = { id: UUID; metadata: { - network: 'tcp' | 'udp'; - type: 'HTTP' | 'HTTP Connect' | 'Socks5' | 'Redir' | 'Unknown'; + network: ConnNetwork; + type: ConnType; sourceIP: string; destinationIP: string; sourcePort: string; @@ -24,7 +26,7 @@ type ConnectionItem = { download: number; // e.g. "2019-11-30T22:48:13.416668+08:00", start: string; - chains: Array; + chains: string[]; // e.g. 'Match', 'DomainKeyword' rule: string; }; diff --git a/src/api/proxies.js b/src/api/proxies.js deleted file mode 100644 index 72e1c52..0000000 --- a/src/api/proxies.js +++ /dev/null @@ -1,66 +0,0 @@ -import { getURLAndInit } from '../misc/request-helper'; -const endpoint = '/proxies'; - -/* -$ curl "http://127.0.0.1:8080/proxies/Proxy" -XPUT -d '{ "name": "ss3" }' -i -HTTP/1.1 400 Bad Request -Content-Type: text/plain; charset=utf-8 - -{"error":"Selector update error: Proxy does not exist"} - -~ -$ curl "http://127.0.0.1:8080/proxies/GLOBAL" -XPUT -d '{ "name": "Proxy" }' -i -HTTP/1.1 204 No Content -*/ - -export async function fetchProxies(config) { - const { url, init } = getURLAndInit(config); - const res = await fetch(url + endpoint, init); - return await res.json(); -} - -export async function requestToSwitchProxy(apiConfig, name1, name2) { - const body = { name: name2 }; - const { url, init } = getURLAndInit(apiConfig); - const fullURL = `${url}${endpoint}/${name1}`; - return await fetch(fullURL, { - ...init, - method: 'PUT', - body: JSON.stringify(body) - }); -} - -export async function requestDelayForProxy( - apiConfig, - name, - latencyTestUrl = 'http://www.gstatic.com/generate_204' -) { - const { url, init } = getURLAndInit(apiConfig); - const qs = `timeout=5000&url=${latencyTestUrl}`; - const fullURL = `${url}${endpoint}/${name}/delay?${qs}`; - return await fetch(fullURL, init); -} - -export async function fetchProviderProxies(config) { - const { url, init } = getURLAndInit(config); - const res = await fetch(url + '/providers/proxies', init); - if (res.status === 404) { - return { providers: {} }; - } - return await res.json(); -} - -export async function updateProviderByName(config, name) { - const { url, init } = getURLAndInit(config); - const options = { ...init, method: 'PUT' }; - return await fetch(url + '/providers/proxies/' + name, options); -} - -export async function healthcheckProviderByName(config, name) { - const { url, init } = getURLAndInit(config); - const options = { ...init, method: 'GET' }; - return await fetch( - url + '/providers/proxies/' + name + '/healthcheck', - options - ); -} diff --git a/src/api/proxies.ts b/src/api/proxies.ts new file mode 100644 index 0000000..72e1c52 --- /dev/null +++ b/src/api/proxies.ts @@ -0,0 +1,66 @@ +import { getURLAndInit } from '../misc/request-helper'; +const endpoint = '/proxies'; + +/* +$ curl "http://127.0.0.1:8080/proxies/Proxy" -XPUT -d '{ "name": "ss3" }' -i +HTTP/1.1 400 Bad Request +Content-Type: text/plain; charset=utf-8 + +{"error":"Selector update error: Proxy does not exist"} + +~ +$ curl "http://127.0.0.1:8080/proxies/GLOBAL" -XPUT -d '{ "name": "Proxy" }' -i +HTTP/1.1 204 No Content +*/ + +export async function fetchProxies(config) { + const { url, init } = getURLAndInit(config); + const res = await fetch(url + endpoint, init); + return await res.json(); +} + +export async function requestToSwitchProxy(apiConfig, name1, name2) { + const body = { name: name2 }; + const { url, init } = getURLAndInit(apiConfig); + const fullURL = `${url}${endpoint}/${name1}`; + return await fetch(fullURL, { + ...init, + method: 'PUT', + body: JSON.stringify(body) + }); +} + +export async function requestDelayForProxy( + apiConfig, + name, + latencyTestUrl = 'http://www.gstatic.com/generate_204' +) { + const { url, init } = getURLAndInit(apiConfig); + const qs = `timeout=5000&url=${latencyTestUrl}`; + const fullURL = `${url}${endpoint}/${name}/delay?${qs}`; + return await fetch(fullURL, init); +} + +export async function fetchProviderProxies(config) { + const { url, init } = getURLAndInit(config); + const res = await fetch(url + '/providers/proxies', init); + if (res.status === 404) { + return { providers: {} }; + } + return await res.json(); +} + +export async function updateProviderByName(config, name) { + const { url, init } = getURLAndInit(config); + const options = { ...init, method: 'PUT' }; + return await fetch(url + '/providers/proxies/' + name, options); +} + +export async function healthcheckProviderByName(config, name) { + const { url, init } = getURLAndInit(config); + const options = { ...init, method: 'GET' }; + return await fetch( + url + '/providers/proxies/' + name + '/healthcheck', + options + ); +} diff --git a/src/api/traffic.js b/src/api/traffic.js deleted file mode 100644 index e50ec5e..0000000 --- a/src/api/traffic.js +++ /dev/null @@ -1,117 +0,0 @@ -import { buildWebSocketURL, getURLAndInit } from '../misc/request-helper'; -const endpoint = '/traffic'; -const textDecoder = new TextDecoder('utf-8'); - -const Size = 150; - -const traffic = { - labels: Array(Size), - // labels: [], - up: Array(Size), - down: Array(Size), - - size: Size, - subscribers: [], - appendData(o) { - this.up.push(o.up); - this.down.push(o.down); - const t = new Date(); - const l = '' + t.getMinutes() + t.getSeconds(); - this.labels.push(l); - if (this.up.length > this.size) this.up.shift(); - if (this.down.length > this.size) this.down.shift(); - if (this.labels.length > this.size) this.labels.shift(); - - this.subscribers.forEach((f) => f(o)); - }, - - subscribe(listener) { - this.subscribers.push(listener); - return () => { - const idx = this.subscribers.indexOf(listener); - this.subscribers.splice(idx, 1); - }; - }, -}; - -let fetched = false; -let decoded = ''; - -function parseAndAppend(x) { - traffic.appendData(JSON.parse(x)); -} - -function pump(reader) { - return reader.read().then(({ done, value }) => { - const str = textDecoder.decode(value, { stream: !done }); - decoded += str; - - const splits = decoded.split('\n'); - - const lastSplit = splits[splits.length - 1]; - - for (let i = 0; i < splits.length - 1; i++) { - parseAndAppend(splits[i]); - } - - if (done) { - parseAndAppend(lastSplit); - decoded = ''; - - // eslint-disable-next-line no-console - console.log('GET /traffic streaming done'); - fetched = false; - return; - } else { - decoded = lastSplit; - } - return pump(reader); - }); -} - -// 1 OPEN -// other value CLOSED -// similar to ws readyState but not the same -// https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/readyState -let wsState; -function fetchData(apiConfig) { - if (fetched || wsState === 1) return traffic; - wsState = 1; - const url = buildWebSocketURL(apiConfig, endpoint); - const ws = new WebSocket(url); - ws.addEventListener('error', function (_ev) { - wsState = 3; - }); - ws.addEventListener('close', function (_ev) { - wsState = 3; - fetchDataWithFetch(apiConfig); - }); - ws.addEventListener('message', function (event) { - parseAndAppend(event.data); - }); - return traffic; -} - -function fetchDataWithFetch(apiConfig) { - if (fetched) return traffic; - fetched = true; - const { url, init } = getURLAndInit(apiConfig); - fetch(url + endpoint, init).then( - (response) => { - if (response.ok) { - const reader = response.body.getReader(); - pump(reader); - } else { - fetched = false; - } - }, - (err) => { - // eslint-disable-next-line no-console - console.log('fetch /traffic error', err); - fetched = false; - } - ); - return traffic; -} - -export { fetchData }; diff --git a/src/api/traffic.ts b/src/api/traffic.ts new file mode 100644 index 0000000..e50ec5e --- /dev/null +++ b/src/api/traffic.ts @@ -0,0 +1,117 @@ +import { buildWebSocketURL, getURLAndInit } from '../misc/request-helper'; +const endpoint = '/traffic'; +const textDecoder = new TextDecoder('utf-8'); + +const Size = 150; + +const traffic = { + labels: Array(Size), + // labels: [], + up: Array(Size), + down: Array(Size), + + size: Size, + subscribers: [], + appendData(o) { + this.up.push(o.up); + this.down.push(o.down); + const t = new Date(); + const l = '' + t.getMinutes() + t.getSeconds(); + this.labels.push(l); + if (this.up.length > this.size) this.up.shift(); + if (this.down.length > this.size) this.down.shift(); + if (this.labels.length > this.size) this.labels.shift(); + + this.subscribers.forEach((f) => f(o)); + }, + + subscribe(listener) { + this.subscribers.push(listener); + return () => { + const idx = this.subscribers.indexOf(listener); + this.subscribers.splice(idx, 1); + }; + }, +}; + +let fetched = false; +let decoded = ''; + +function parseAndAppend(x) { + traffic.appendData(JSON.parse(x)); +} + +function pump(reader) { + return reader.read().then(({ done, value }) => { + const str = textDecoder.decode(value, { stream: !done }); + decoded += str; + + const splits = decoded.split('\n'); + + const lastSplit = splits[splits.length - 1]; + + for (let i = 0; i < splits.length - 1; i++) { + parseAndAppend(splits[i]); + } + + if (done) { + parseAndAppend(lastSplit); + decoded = ''; + + // eslint-disable-next-line no-console + console.log('GET /traffic streaming done'); + fetched = false; + return; + } else { + decoded = lastSplit; + } + return pump(reader); + }); +} + +// 1 OPEN +// other value CLOSED +// similar to ws readyState but not the same +// https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/readyState +let wsState; +function fetchData(apiConfig) { + if (fetched || wsState === 1) return traffic; + wsState = 1; + const url = buildWebSocketURL(apiConfig, endpoint); + const ws = new WebSocket(url); + ws.addEventListener('error', function (_ev) { + wsState = 3; + }); + ws.addEventListener('close', function (_ev) { + wsState = 3; + fetchDataWithFetch(apiConfig); + }); + ws.addEventListener('message', function (event) { + parseAndAppend(event.data); + }); + return traffic; +} + +function fetchDataWithFetch(apiConfig) { + if (fetched) return traffic; + fetched = true; + const { url, init } = getURLAndInit(apiConfig); + fetch(url + endpoint, init).then( + (response) => { + if (response.ok) { + const reader = response.body.getReader(); + pump(reader); + } else { + fetched = false; + } + }, + (err) => { + // eslint-disable-next-line no-console + console.log('fetch /traffic error', err); + fetched = false; + } + ); + return traffic; +} + +export { fetchData }; -- cgit v1.3.1