|
| 1 | +/** |
| 2 | + * 配置合并方法 |
| 3 | + * 将预设配置和用户配置合并成最终的 frp 配置 |
| 4 | + */ |
| 5 | + |
| 6 | +import type { ProxyConfig } from '@frp-bridge/types' |
| 7 | +import type { PresetConfig } from './preset-config' |
| 8 | +import { writeFileSync } from 'node:fs' |
| 9 | +import { ensureDir } from '@frp-bridge/core' |
| 10 | + |
| 11 | +/** |
| 12 | + * 合并预设配置和用户配置,生成最终的 TOML 配置 |
| 13 | + * @param presetConfig 预设配置 |
| 14 | + * @param userConfig 用户配置的 TOML 字符串 |
| 15 | + * @param type 配置类型 'frps' | 'frpc' |
| 16 | + * @returns 最终的 TOML 配置字符串 |
| 17 | + */ |
| 18 | +export function mergeConfigs( |
| 19 | + presetConfig: PresetConfig, |
| 20 | + userConfig: string, |
| 21 | + type: 'frps' | 'frpc' |
| 22 | +): string { |
| 23 | + const typeConfig = presetConfig[type] |
| 24 | + |
| 25 | + if (!typeConfig) { |
| 26 | + // 如果没有预设配置,直接返回用户配置 |
| 27 | + return userConfig |
| 28 | + } |
| 29 | + |
| 30 | + // 解析用户配置 |
| 31 | + const userConfigLines = userConfig.split('\n') |
| 32 | + |
| 33 | + // 提取用户配置中的代理部分 |
| 34 | + const proxiesSection = extractProxiesSection(userConfigLines) |
| 35 | + |
| 36 | + // 生成最终配置 |
| 37 | + const finalConfigLines: string[] = [] |
| 38 | + |
| 39 | + // 1. 添加预设配置的基础参数 |
| 40 | + if (type === 'frps') { |
| 41 | + const frpsConfig = typeConfig as import('./preset-config').FrpsPresetConfig |
| 42 | + |
| 43 | + if (frpsConfig.bindPort) { |
| 44 | + finalConfigLines.push(`bindPort = ${frpsConfig.bindPort}`) |
| 45 | + } |
| 46 | + if (frpsConfig.vhostHTTPPort) { |
| 47 | + finalConfigLines.push(`vhostHTTPPort = ${frpsConfig.vhostHTTPPort}`) |
| 48 | + } |
| 49 | + if (frpsConfig.dashboardPort) { |
| 50 | + finalConfigLines.push('') |
| 51 | + finalConfigLines.push('[webServer]') |
| 52 | + finalConfigLines.push(`addr = "0.0.0.0"`) |
| 53 | + finalConfigLines.push(`port = ${frpsConfig.dashboardPort}`) |
| 54 | + if (frpsConfig.dashboardUser) { |
| 55 | + finalConfigLines.push(`user = "${frpsConfig.dashboardUser}"`) |
| 56 | + } |
| 57 | + if (frpsConfig.dashboardPassword) { |
| 58 | + finalConfigLines.push(`password = "${frpsConfig.dashboardPassword}"`) |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + else if (type === 'frpc') { |
| 63 | + const frpcConfig = typeConfig as import('./preset-config').FrpcPresetConfig |
| 64 | + |
| 65 | + if (frpcConfig.serverAddr) { |
| 66 | + finalConfigLines.push(`serverAddr = "${frpcConfig.serverAddr}"`) |
| 67 | + } |
| 68 | + if (frpcConfig.serverPort) { |
| 69 | + finalConfigLines.push(`serverPort = ${frpcConfig.serverPort}`) |
| 70 | + } |
| 71 | + if (frpcConfig.authToken) { |
| 72 | + finalConfigLines.push(`auth.token = "${frpcConfig.authToken}"`) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + // 2. 添加用户配置中的代理部分 |
| 77 | + if (proxiesSection) { |
| 78 | + finalConfigLines.push('') |
| 79 | + finalConfigLines.push(...proxiesSection) |
| 80 | + } |
| 81 | + |
| 82 | + return finalConfigLines.join('\n') |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * 从 tunnels 数组生成并保存 FRP 配置文件 |
| 87 | + * @param configPath 配置文件路径 |
| 88 | + * @param tunnels tunnels 数组 |
| 89 | + * @param presetConfig 预设配置 |
| 90 | + * @param type 配置类型 'frps' | 'frpc' |
| 91 | + */ |
| 92 | +export function saveFrpConfigFile( |
| 93 | + configPath: string, |
| 94 | + tunnels: ProxyConfig[], |
| 95 | + presetConfig: PresetConfig, |
| 96 | + type: 'frps' | 'frpc' |
| 97 | +): void { |
| 98 | + // 1. 将 tunnels 转换为 TOML 格式 |
| 99 | + const userConfig = tunnelsToToml(tunnels) |
| 100 | + |
| 101 | + // 2. 合并预设配置和用户配置 |
| 102 | + const finalConfig = mergeConfigs(presetConfig, userConfig, type) |
| 103 | + |
| 104 | + // 3. 确保目录存在 |
| 105 | + const targetDir = configPath.includes('/') || configPath.includes('\\') |
| 106 | + ? configPath.substring(0, Math.max(configPath.lastIndexOf('/'), configPath.lastIndexOf('\\'))) |
| 107 | + : '.' |
| 108 | + ensureDir(targetDir) |
| 109 | + |
| 110 | + // 4. 写入配置文件 |
| 111 | + writeFileSync(configPath, finalConfig, 'utf-8') |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * 将 tunnels 数组转换为 TOML 格式 |
| 116 | + */ |
| 117 | +function tunnelsToToml(tunnels: ProxyConfig[]): string { |
| 118 | + if (!tunnels || tunnels.length === 0) { |
| 119 | + return '' |
| 120 | + } |
| 121 | + |
| 122 | + const lines: string[] = [] |
| 123 | + |
| 124 | + for (const tunnel of tunnels) { |
| 125 | + lines.push('') |
| 126 | + lines.push('[[proxies]]') |
| 127 | + for (const [key, value] of Object.entries(tunnel)) { |
| 128 | + if (value === undefined || value === null) |
| 129 | + continue |
| 130 | + if (typeof value === 'string') { |
| 131 | + lines.push(`${key} = "${value}"`) |
| 132 | + } |
| 133 | + else if (typeof value === 'number' || typeof value === 'boolean') { |
| 134 | + lines.push(`${key} = ${value}`) |
| 135 | + } |
| 136 | + else if (typeof value === 'object') { |
| 137 | + lines.push(`[${key}]`) |
| 138 | + for (const [subKey, subValue] of Object.entries(value)) { |
| 139 | + if (typeof subValue === 'string') { |
| 140 | + lines.push(`${subKey} = "${subValue}"`) |
| 141 | + } |
| 142 | + else { |
| 143 | + lines.push(`${subKey} = ${subValue}`) |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + return lines.join('\n') |
| 151 | +} |
| 152 | + |
| 153 | +/** |
| 154 | + * 从用户配置中提取代理部分 |
| 155 | + */ |
| 156 | +function extractProxiesSection(lines: string[]): string[] { |
| 157 | + const proxiesSection: string[] = [] |
| 158 | + let inProxies = false |
| 159 | + |
| 160 | + for (const line of lines) { |
| 161 | + // 检查是否是代理定义 |
| 162 | + if (line.trim().startsWith('[[proxies]]')) { |
| 163 | + inProxies = true |
| 164 | + } |
| 165 | + |
| 166 | + if (inProxies) { |
| 167 | + proxiesSection.push(line) |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + return proxiesSection |
| 172 | +} |
| 173 | + |
| 174 | +/** |
| 175 | + * 将配置对象转换为 TOML 格式 |
| 176 | + */ |
| 177 | +export function configToToml(config: Record<string, any>): string { |
| 178 | + const lines: string[] = [] |
| 179 | + |
| 180 | + for (const [key, value] of Object.entries(config)) { |
| 181 | + if (value === undefined || value === null) { |
| 182 | + continue |
| 183 | + } |
| 184 | + |
| 185 | + if (typeof value === 'string') { |
| 186 | + lines.push(`${key} = "${value}"`) |
| 187 | + } |
| 188 | + else if (typeof value === 'number' || typeof value === 'boolean') { |
| 189 | + lines.push(`${key} = ${value}`) |
| 190 | + } |
| 191 | + else if (typeof value === 'object') { |
| 192 | + // 嵌套对象 |
| 193 | + lines.push(`[${key}]`) |
| 194 | + for (const [subKey, subValue] of Object.entries(value)) { |
| 195 | + if (typeof subValue === 'string') { |
| 196 | + lines.push(`${subKey} = "${subValue}"`) |
| 197 | + } |
| 198 | + else if (typeof subValue === 'number' || typeof subValue === 'boolean') { |
| 199 | + lines.push(`${subKey} = ${subValue}`) |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + return lines.join('\n') |
| 206 | +} |
| 207 | + |
| 208 | +/** |
| 209 | + * 验证预设配置 |
| 210 | + */ |
| 211 | +export function validatePresetConfig( |
| 212 | + config: PresetConfig, |
| 213 | + type: 'frps' | 'frpc' |
| 214 | +): { valid: boolean, errors: string[] } { |
| 215 | + const errors: string[] = [] |
| 216 | + const typeConfig = config[type] |
| 217 | + |
| 218 | + if (!typeConfig) { |
| 219 | + return { valid: true, errors: [] } |
| 220 | + } |
| 221 | + |
| 222 | + if (type === 'frps') { |
| 223 | + const frpsConfig = typeConfig as import('./preset-config').FrpsPresetConfig |
| 224 | + |
| 225 | + if (frpsConfig.bindPort !== undefined) { |
| 226 | + if (frpsConfig.bindPort < 1 || frpsConfig.bindPort > 65535) { |
| 227 | + errors.push('bindPort must be between 1-65535') |
| 228 | + } |
| 229 | + } |
| 230 | + |
| 231 | + if (frpsConfig.vhostHTTPPort !== undefined) { |
| 232 | + if (frpsConfig.vhostHTTPPort < 1 || frpsConfig.vhostHTTPPort > 65535) { |
| 233 | + errors.push('vhostHTTPPort must be between 1-65535') |
| 234 | + } |
| 235 | + } |
| 236 | + |
| 237 | + if (frpsConfig.domain !== undefined && !frpsConfig.domain) { |
| 238 | + errors.push('domain cannot be empty') |
| 239 | + } |
| 240 | + } |
| 241 | + else if (type === 'frpc') { |
| 242 | + const frpcConfig = typeConfig as import('./preset-config').FrpcPresetConfig |
| 243 | + |
| 244 | + if (frpcConfig.serverPort !== undefined) { |
| 245 | + if (frpcConfig.serverPort < 1 || frpcConfig.serverPort > 65535) { |
| 246 | + errors.push('serverPort must be between 1-65535') |
| 247 | + } |
| 248 | + } |
| 249 | + |
| 250 | + if (frpcConfig.serverAddr !== undefined && !frpcConfig.serverAddr) { |
| 251 | + errors.push('serverAddr cannot be empty') |
| 252 | + } |
| 253 | + } |
| 254 | + |
| 255 | + return { |
| 256 | + valid: errors.length === 0, |
| 257 | + errors |
| 258 | + } |
| 259 | +} |
0 commit comments