-
-
Notifications
You must be signed in to change notification settings - Fork 140
/
Copy pathconfig.ts
182 lines (158 loc) · 4.41 KB
/
config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import path from 'path'
import { URL } from 'url'
import fs from 'fs-extra'
import _ from 'lodash'
import {
INTERNET_TEST_INTERVAL,
INTERNET_TEST_URL,
PROXY_TEST_INTERVAL,
PROXY_TEST_URL,
} from './constant'
import redis from './redis'
import { CommandConfig, CommandConfigBeforeNormalize } from './types'
import { SurgioConfigValidator } from './validators'
import { addFlagMap } from './utils/flag'
import { ensureConfigFolder } from './utils'
let finalConfig: CommandConfig | null = null
export const loadConfig = (
cwd: string,
override?: Partial<CommandConfig>,
): CommandConfig => {
const absPath = path.join(cwd, 'surgio.conf.js')
// istanbul ignore next
if (!fs.existsSync(absPath)) {
throw new Error(`配置文件 ${absPath} 不存在`)
}
const userConfig = validateConfig(_.cloneDeep(require(absPath)))
if (userConfig.flags) {
Object.keys(userConfig.flags).forEach((emoji) => {
if (userConfig.flags) {
if (typeof userConfig.flags[emoji] === 'string') {
addFlagMap(userConfig.flags[emoji] as string, emoji)
} else if (_.isRegExp(userConfig.flags[emoji])) {
addFlagMap(userConfig.flags[emoji] as RegExp, emoji)
} else {
;(userConfig.flags[emoji] as ReadonlyArray<string | RegExp>).forEach(
(name) => {
addFlagMap(name, emoji)
},
)
}
}
})
}
if (override) {
return {
...normalizeConfig(cwd, userConfig),
...override,
}
}
finalConfig = normalizeConfig(cwd, userConfig)
return finalConfig
}
export const getConfig = () => {
// istanbul ignore next
if (!finalConfig) {
throw new Error('请先调用 loadConfig 方法')
}
return finalConfig
}
export const setConfig = <T extends keyof CommandConfig>(
key: T,
value: CommandConfig[T],
): CommandConfig => {
// istanbul ignore next
if (!finalConfig) {
throw new Error('请先调用 loadConfig 方法')
}
if (_.isPlainObject(value)) {
finalConfig[key] = {
...(finalConfig[key] as object),
...(value as object),
} as CommandConfig[T]
} else {
finalConfig[key] = value
}
return finalConfig
}
export const normalizeConfig = (
cwd: string,
userConfig: Partial<CommandConfigBeforeNormalize>,
): CommandConfig => {
const defaultConfig: Partial<CommandConfig> = {
artifacts: [],
urlBase: '/',
output: path.join(cwd, './dist'),
templateDir: path.join(cwd, './template'),
providerDir: path.join(cwd, './provider'),
configDir: ensureConfigFolder(),
surgeConfig: {
resolveHostname: false,
vmessAEAD: true,
},
clashConfig: {
enableShadowTls: false,
enableTuic: false,
enableHysteria2: false,
enableVless: false,
clashCore: 'clash',
},
quantumultXConfig: {
vmessAEAD: true,
},
surfboardConfig: {
vmessAEAD: true,
},
proxyTestUrl: PROXY_TEST_URL,
proxyTestInterval: PROXY_TEST_INTERVAL,
internetTestUrl: INTERNET_TEST_URL,
internetTestInterval: INTERNET_TEST_INTERVAL,
checkHostname: false,
resolveHostname: false,
cache: {
type: 'default',
},
gateway: {
passRequestUserAgent: false,
},
}
const config: CommandConfig = _.defaultsDeep(userConfig, defaultConfig)
// istanbul ignore next
if (!fs.existsSync(config.templateDir)) {
throw new Error(`仓库内缺少 ${config.templateDir} 目录`)
}
// istanbul ignore next
if (!fs.existsSync(config.providerDir)) {
throw new Error(`仓库内缺少 ${config.providerDir} 目录`)
}
if (/http/i.test(config.urlBase)) {
const urlObject = new URL(config.urlBase)
config.publicUrl = urlObject.origin + '/'
} else {
config.publicUrl = '/'
}
// istanbul ignore next
if (config.cache && config.cache.type === 'redis') {
if (!config.cache.redisUrl) {
throw new Error('缓存配置错误,请检查 cache.redisUrl 配置')
}
redis.createRedis(config.cache.redisUrl)
}
// istanbul ignore next
if (config.gateway) {
if (config.gateway.auth && !config.gateway.accessToken) {
throw new Error('请检查 gateway.accessToken 配置')
}
}
return config
}
export const validateConfig = (
userConfig: Partial<CommandConfig>,
): CommandConfigBeforeNormalize => {
const result = SurgioConfigValidator.safeParse(userConfig)
// istanbul ignore next
if (!result.success) {
throw result.error
}
return result.data
}