Skip to content

Commit 64d228d

Browse files
committed
feat: replace sampleID with secure key generator for Clash API secret
1 parent 3fb527c commit 64d228d

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

frontend/src/constant/profile.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import i18n from '@/lang'
2-
import { sampleID } from '@/utils'
2+
import { generateSecureKey, sampleID } from '@/utils'
33
import { DefaultTestURL } from './app'
44
import {
55
LogLevel,
@@ -60,7 +60,7 @@ export const DefaultExperimental = (): IExperimental => ({
6060
external_ui: '',
6161
external_ui_download_url: '',
6262
external_ui_download_detour: DefaultOutboundIds.Direct,
63-
secret: sampleID(),
63+
secret: generateSecureKey(),
6464
default_mode: ClashMode.Rule,
6565
access_control_allow_origin: ['*'],
6666
access_control_allow_private_network: false,

frontend/src/utils/others.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ export const ignoredError = async <T>(fn: (...args: any) => Promise<T>, ...args:
5757

5858
export const sampleID = () => 'ID_' + Math.random().toString(36).substring(2, 10)
5959

60+
export const generateSecureKey = (bits = 256) => {
61+
const bytes = bits / 8
62+
const array = new Uint8Array(bytes)
63+
crypto.getRandomValues(array)
64+
return Array.from(array)
65+
.map((b) => b.toString(16).padStart(2, '0'))
66+
.join('')
67+
}
68+
6069
export const getValue = (obj: Record<string, any>, expr: string) => {
6170
return expr.split('.').reduce((value, key) => {
6271
return value[key]

frontend/src/views/ProfilesView/components/GeneralConfig.vue

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { useI18n } from 'vue-i18n'
33
44
import { useBool } from '@/hooks'
5+
import { generateSecureKey } from '@/utils'
56
import { ModeOptions, LogLevelOptions } from '@/constant/kernel'
67
78
interface Props {
@@ -48,7 +49,15 @@ const [showMore, toggleMore] = useBool(false)
4849
</div>
4950
<div class="form-item">
5051
{{ t('kernel.clash_api.secret') }}
51-
<Input v-model="model.experimental.clash_api.secret" editable />
52+
<div class="flex items-center">
53+
<Input v-model="model.experimental.clash_api.secret" editable />
54+
<Button
55+
@click="() => (model.experimental.clash_api.secret = generateSecureKey())"
56+
type="text"
57+
size="small"
58+
icon="refresh"
59+
/>
60+
</div>
5261
</div>
5362
<Divider>
5463
<Button @click="toggleMore" type="text" size="small">{{ t('common.more') }}</Button>

0 commit comments

Comments
 (0)