diff options
| author | Kevin <[email protected]> | 2021-05-31 18:58:56 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-05-31 18:58:56 +0800 |
| commit | d4015f64237b2b429a04af0ebe82fdf883b45b01 (patch) | |
| tree | d5f28b916d02c6cad68befa220b1a50eb40c3be6 | |
| parent | 13dcb7d6532a07bb86c54139c9e9d19eb892de4e (diff) | |
Add level query to log websocket url fix #589 (#590)
| -rw-r--r-- | src/api/logs.ts | 8 | ||||
| -rw-r--r-- | src/misc/request-helper.ts | 29 | ||||
| -rw-r--r-- | src/store/configs.ts | 2 | ||||
| -rw-r--r-- | src/types.ts | 2 |
4 files changed, 29 insertions, 12 deletions
diff --git a/src/api/logs.ts b/src/api/logs.ts index 1958638..5965588 100644 --- a/src/api/logs.ts +++ b/src/api/logs.ts @@ -1,8 +1,7 @@ -import { ClashAPIConfig } from 'src/types'; +import { LogsAPIConfig } from 'src/types'; -import { buildWebSocketURL, getURLAndInit } from '../misc/request-helper'; +import { buildLogsWebSocketURL, getURLAndInit } from '../misc/request-helper'; -type LogsAPIConfig = ClashAPIConfig & { logLevel: string }; type LogEntry = { time?: string; id?: string; @@ -77,9 +76,10 @@ let controller: AbortController; // https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/readyState let wsState: number; export function fetchLogs(apiConfig: LogsAPIConfig, appendLog: AppendLogFn) { + if (apiConfig.logLevel === 'uninit') return; if (fetched || wsState === 1) return; wsState = 1; - const url = buildWebSocketURL(apiConfig, endpoint); + const url = buildLogsWebSocketURL(apiConfig, endpoint); const ws = new WebSocket(url); ws.addEventListener('error', () => (wsState = 3)); ws.addEventListener('close', function (_ev) { diff --git a/src/misc/request-helper.ts b/src/misc/request-helper.ts index 1096a67..c982c97 100644 --- a/src/misc/request-helper.ts +++ b/src/misc/request-helper.ts @@ -1,5 +1,6 @@ import { trimTrailingSlash } from 'src/misc/utils'; import { ClashAPIConfig } from 'src/types'; +import { LogsAPIConfig } from 'src/types'; const headersCommon = { 'Content-Type': 'application/json' }; @@ -10,6 +11,12 @@ function genCommonHeaders({ secret }: { secret?: string }) { } return h; } +function buildWebSocketURLBase(baseURL: string, params: URLSearchParams, endpoint: string) { + let qs = '?' + params.toString() + const url = new URL(baseURL); + url.protocol === 'https:' ? (url.protocol = 'wss:') : (url.protocol = 'ws:'); + return `${trimTrailingSlash(url.href)}${endpoint}${qs}`; +} export function getURLAndInit({ baseURL, secret }: ClashAPIConfig) { const headers = genCommonHeaders({ secret }); @@ -21,11 +28,19 @@ export function getURLAndInit({ baseURL, secret }: ClashAPIConfig) { export function buildWebSocketURL(apiConfig: ClashAPIConfig, endpoint: string) { const { baseURL, secret } = apiConfig; - let qs = ''; - if (typeof secret === 'string' && secret !== '') { - qs += '?token=' + encodeURIComponent(secret); - } - const url = new URL(baseURL); - url.protocol === 'https:' ? (url.protocol = 'wss:') : (url.protocol = 'ws:'); - return `${trimTrailingSlash(url.href)}${endpoint}${qs}`; + const params = new URLSearchParams({ + token: secret, + }); + + return buildWebSocketURLBase(baseURL, params, endpoint) +} + +export function buildLogsWebSocketURL(apiConfig: LogsAPIConfig, endpoint: string) { + const { baseURL, secret, logLevel } = apiConfig; + const params = new URLSearchParams({ + token: secret, + level: logLevel, + }); + + return buildWebSocketURLBase(baseURL, params, endpoint) } diff --git a/src/store/configs.ts b/src/store/configs.ts index d18838e..c533508 100644 --- a/src/store/configs.ts +++ b/src/store/configs.ts @@ -96,7 +96,7 @@ export const initialState: StateConfigs = { 'redir-port': 0, 'allow-lan': false, mode: 'Rule', - 'log-level': 'info', + 'log-level': 'uninit', }, haveFetchedConfig: false, }; diff --git a/src/types.ts b/src/types.ts index 8988c5b..694289b 100644 --- a/src/types.ts +++ b/src/types.ts @@ -2,3 +2,5 @@ export type ClashAPIConfig = { baseURL: string; secret?: string; }; + +export type LogsAPIConfig = ClashAPIConfig & { logLevel: string };
\ No newline at end of file |
