| 1 | import type { ProxyType, VisitorType } from './constants' |
| 2 | import type { ProxyFormData, VisitorFormData } from './proxy-form' |
| 3 | import { createDefaultProxyForm, createDefaultVisitorForm } from './proxy-form' |
| 4 | import type { ProxyDefinition, VisitorDefinition } from './proxy-store' |
| 5 | |
| 6 | // ======================================== |
| 7 | // CONVERTERS: Form -> Store API |
| 8 |
| // ======================================== |
| 9 |
| 10 | export function formToStoreProxy(form: ProxyFormData): ProxyDefinition { |
| 11 | const block: Record<string, any> = {} |
| 12 |
| 13 | // Enabled (nil/true = enabled, false = disabled) |
| 14 | if (!form.enabled) { |
| 15 | block.enabled = false |
| 16 | } |
| 17 |
| 18 | // Backend - LocalIP/LocalPort |
| 19 | if (form.pluginType === '') { |
| 20 | if (form.localIP && form.localIP !== '127.0.0.1') { |
| 21 | block.localIP = form.localIP |
| 22 | } |
| 23 | if (form.localPort != null) { |
| 24 | block.localPort = form.localPort |
| 25 | } |
| 26 | } else { |
| 27 | block.plugin = { |
| 28 | type: form.pluginType, |
| 29 | ...form.pluginConfig, |
| 30 | } |
| 31 | } |
| 32 |
| 33 | // Transport |
| 34 | if ( |
| 35 | form.useEncryption || |
| 36 | form.useCompression || |
| 37 | form.bandwidthLimit || |
| 38 | (form.bandwidthLimitMode && form.bandwidthLimitMode !== 'client') || |
| 39 | form.proxyProtocolVersion |
| 40 | ) { |
| 41 | block.transport = {} |
| 42 | if (form.useEncryption) block.transport.useEncryption = true |
| 43 | if (form.useCompression) block.transport.useCompression = true |
| 44 | if (form.bandwidthLimit) block.transport.bandwidthLimit = form.bandwidthLimit |
| 45 | if (form.bandwidthLimitMode && form.bandwidthLimitMode !== 'client') { |
| 46 | block.transport.bandwidthLimitMode = form.bandwidthLimitMode |
| 47 | } |
| 48 | if (form.proxyProtocolVersion) { |
| 49 | block.transport.proxyProtocolVersion = form.proxyProtocolVersion |
| 50 | } |
| 51 | } |
| 52 |
| 53 | // Load Balancer |
| 54 | if (form.loadBalancerGroup) { |
| 55 | block.loadBalancer = { |
| 56 | group: form.loadBalancerGroup, |
| 57 | } |
| 58 | if (form.loadBalancerGroupKey) { |
| 59 | block.loadBalancer.groupKey = form.loadBalancerGroupKey |
| 60 | } |
| 61 | } |
| 62 |
| 63 | // Health Check |
| 64 | if (form.healthCheckType) { |
| 65 | block.healthCheck = { |
| 66 | type: form.healthCheckType, |
| 67 | } |
| 68 | if (form.healthCheckTimeoutSeconds != null) { |
| 69 | block.healthCheck.timeoutSeconds = form.healthCheckTimeoutSeconds |
| 70 | } |
| 71 | if (form.healthCheckMaxFailed != null) { |
| 72 | block.healthCheck.maxFailed = form.healthCheckMaxFailed |
| 73 | } |
| 74 | if (form.healthCheckIntervalSeconds != null) { |
| 75 | block.healthCheck.intervalSeconds = form.healthCheckIntervalSeconds |
| 76 | } |
| 77 | if (form.healthCheckPath) { |
| 78 | block.healthCheck.path = form.healthCheckPath |
| 79 | } |
| 80 | if (form.healthCheckHTTPHeaders.length > 0) { |
| 81 | block.healthCheck.httpHeaders = form.healthCheckHTTPHeaders |
| 82 | } |
| 83 | } |
| 84 |
| 85 | // Metadata |
| 86 | if (form.metadatas.length > 0) { |
| 87 | block.metadatas = Object.fromEntries( |
| 88 | form.metadatas.map((m) => [m.key, m.value]), |
| 89 | ) |
| 90 | } |
| 91 |
| 92 | // Annotations |
| 93 | if (form.annotations.length > 0) { |
| 94 | block.annotations = Object.fromEntries( |
| 95 | form.annotations.map((a) => [a.key, a.value]), |
| 96 | ) |
| 97 | } |
| 98 |
| 99 | // Type-specific fields |
| 100 | if ((form.type === 'tcp' || form.type === 'udp') && form.remotePort != null) { |
| 101 | block.remotePort = form.remotePort |
| 102 | } |
| 103 |
| 104 | if (form.type === 'http' || form.type === 'https' || form.type === 'tcpmux') { |
| 105 | if (form.customDomains.length > 0) { |
| 106 | block.customDomains = form.customDomains.filter(Boolean) |
| 107 | } |
| 108 | if (form.subdomain) { |
| 109 | block.subdomain = form.subdomain |
| 110 | } |
| 111 | } |
| 112 |
| 113 | if (form.type === 'http') { |
| 114 | if (form.locations.length > 0) { |
| 115 | block.locations = form.locations.filter(Boolean) |
| 116 | } |
| 117 | if (form.httpUser) block.httpUser = form.httpUser |
| 118 | if (form.httpPassword) block.httpPassword = form.httpPassword |
| 119 | if (form.hostHeaderRewrite) block.hostHeaderRewrite = form.hostHeaderRewrite |
| 120 | if (form.routeByHTTPUser) block.routeByHTTPUser = form.routeByHTTPUser |
| 121 |
| 122 | if (form.requestHeaders.length > 0) { |
| 123 | block.requestHeaders = { |
| 124 | set: Object.fromEntries( |
| 125 | form.requestHeaders.map((h) => [h.key, h.value]), |
| 126 | ), |
| 127 | } |
| 128 | } |
| 129 | if (form.responseHeaders.length > 0) { |
| 130 | block.responseHeaders = { |
| 131 | set: Object.fromEntries( |
| 132 | form.responseHeaders.map((h) => [h.key, h.value]), |
| 133 | ), |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 |
| 138 | if (form.type === 'tcpmux') { |
| 139 | if (form.httpUser) block.httpUser = form.httpUser |
| 140 | if (form.httpPassword) block.httpPassword = form.httpPassword |
| 141 | if (form.routeByHTTPUser) block.routeByHTTPUser = form.routeByHTTPUser |
| 142 | if (form.multiplexer && form.multiplexer !== 'httpconnect') { |
| 143 | block.multiplexer = form.multiplexer |
| 144 | } |
| 145 | } |
| 146 |
| 147 | if (form.type === 'stcp' || form.type === 'sudp' || form.type === 'xtcp') { |
| 148 | if (form.secretKey) block.secretKey = form.secretKey |
| 149 | if (form.allowUsers.length > 0) { |
| 150 | block.allowUsers = form.allowUsers.filter(Boolean) |
| 151 | } |
| 152 | } |
| 153 |
| 154 | if (form.type === 'xtcp' && form.natTraversalDisableAssistedAddrs) { |
| 155 | block.natTraversal = { |
| 156 | disableAssistedAddrs: true, |
| 157 | } |
| 158 | } |
| 159 |
| 160 | return withStoreProxyBlock( |
| 161 | { |
| 162 | name: form.name, |
| 163 | type: form.type, |
| 164 | }, |
| 165 | form.type, |
| 166 | block, |
| 167 | ) |
| 168 | } |
| 169 |
| 170 | export function formToStoreVisitor(form: VisitorFormData): VisitorDefinition { |
| 171 | const block: Record<string, any> = {} |
| 172 |
| 173 | if (!form.enabled) { |
| 174 | block.enabled = false |
| 175 | } |
| 176 |
| 177 | if (form.useEncryption || form.useCompression) { |
| 178 | block.transport = {} |
| 179 | if (form.useEncryption) block.transport.useEncryption = true |
| 180 | if (form.useCompression) block.transport.useCompression = true |
| 181 | } |
| 182 |
| 183 | if (form.secretKey) block.secretKey = form.secretKey |
| 184 | if (form.serverUser) block.serverUser = form.serverUser |
| 185 | if (form.serverName) block.serverName = form.serverName |
| 186 | if (form.bindAddr && form.bindAddr !== '127.0.0.1') { |
| 187 | block.bindAddr = form.bindAddr |
| 188 | } |
| 189 | if (form.bindPort != null) { |
| 190 | block.bindPort = form.bindPort |
| 191 | } |
| 192 |
| 193 | if (form.type === 'xtcp') { |
| 194 | if (form.protocol && form.protocol !== 'quic') { |
| 195 | block.protocol = form.protocol |
| 196 | } |
| 197 | if (form.keepTunnelOpen) { |
| 198 | block.keepTunnelOpen = true |
| 199 | } |
| 200 | if (form.maxRetriesAnHour != null) { |
| 201 | block.maxRetriesAnHour = form.maxRetriesAnHour |
| 202 | } |
| 203 | if (form.minRetryInterval != null) { |
| 204 | block.minRetryInterval = form.minRetryInterval |
| 205 | } |
| 206 | if (form.fallbackTo) { |
| 207 | block.fallbackTo = form.fallbackTo |
| 208 | } |
| 209 | if (form.fallbackTimeoutMs != null) { |
| 210 | block.fallbackTimeoutMs = form.fallbackTimeoutMs |
| 211 | } |
| 212 | if (form.natTraversalDisableAssistedAddrs) { |
| 213 | block.natTraversal = { |
| 214 | disableAssistedAddrs: true, |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 |
| 219 | return withStoreVisitorBlock( |
| 220 | { |
| 221 | name: form.name, |
| 222 | type: form.type, |
| 223 | }, |
| 224 | form.type, |
| 225 | block, |
| 226 | ) |
| 227 | } |
| 228 |
| 229 | // ======================================== |
| 230 | // CONVERTERS: Store API -> Form |
| 231 | // ======================================== |
| 232 |
| 233 | function getStoreProxyBlock(config: ProxyDefinition): Record<string, any> { |
| 234 | switch (config.type) { |
| 235 | case 'tcp': |
| 236 | return config.tcp || {} |
| 237 | case 'udp': |
| 238 | return config.udp || {} |
| 239 | case 'http': |
| 240 | return config.http || {} |
| 241 | case 'https': |
| 242 | return config.https || {} |
| 243 | case 'tcpmux': |
| 244 | return config.tcpmux || {} |
| 245 | case 'stcp': |
| 246 | return config.stcp || {} |
| 247 | case 'sudp': |
| 248 | return config.sudp || {} |
| 249 | case 'xtcp': |
| 250 | return config.xtcp || {} |
| 251 | } |
| 252 | } |
| 253 |
| 254 | function withStoreProxyBlock( |
| 255 | payload: ProxyDefinition, |
| 256 | type: ProxyType, |
| 257 | block: Record<string, any>, |
| 258 | ): ProxyDefinition { |
| 259 | switch (type) { |
| 260 | case 'tcp': |
| 261 | payload.tcp = block |
| 262 | break |
| 263 | case 'udp': |
| 264 | payload.udp = block |
| 265 | break |
| 266 | case 'http': |
| 267 | payload.http = block |
| 268 | break |
| 269 | case 'https': |
| 270 | payload.https = block |
| 271 | break |
| 272 | case 'tcpmux': |
| 273 | payload.tcpmux = block |
| 274 | break |
| 275 | case 'stcp': |
| 276 | payload.stcp = block |
| 277 | break |
| 278 | case 'sudp': |
| 279 | payload.sudp = block |
| 280 | break |
| 281 | case 'xtcp': |
| 282 | payload.xtcp = block |
| 283 | break |
| 284 | } |
| 285 | return payload |
| 286 | } |
| 287 |
| 288 | function getStoreVisitorBlock(config: VisitorDefinition): Record<string, any> { |
| 289 | switch (config.type) { |
| 290 | case 'stcp': |
| 291 | return config.stcp || {} |
| 292 | case 'sudp': |
| 293 | return config.sudp || {} |
| 294 | case 'xtcp': |
| 295 | return config.xtcp || {} |
| 296 | } |
| 297 | } |
| 298 |
| 299 | function withStoreVisitorBlock( |
| 300 | payload: VisitorDefinition, |
| 301 | type: VisitorType, |
| 302 | block: Record<string, any>, |
| 303 | ): VisitorDefinition { |
| 304 | switch (type) { |
| 305 | case 'stcp': |
| 306 | payload.stcp = block |
| 307 | break |
| 308 | case 'sudp': |
| 309 | payload.sudp = block |
| 310 | break |
| 311 | case 'xtcp': |
| 312 | payload.xtcp = block |
| 313 | break |
| 314 | } |
| 315 | return payload |
| 316 | } |
| 317 |
| 318 | export function storeProxyToForm(config: ProxyDefinition): ProxyFormData { |
| 319 | const c = getStoreProxyBlock(config) |
| 320 | const form = createDefaultProxyForm() |
| 321 |
| 322 | form.name = config.name || '' |
| 323 | form.type = config.type || 'tcp' |
| 324 | form.enabled = c.enabled !== false |
| 325 |
| 326 | // Backend |
| 327 | form.localIP = c.localIP || '127.0.0.1' |
| 328 | form.localPort = c.localPort |
| 329 | if (c.plugin?.type) { |
| 330 | form.pluginType = c.plugin.type |
| 331 | form.pluginConfig = { ...c.plugin } |
| 332 | delete form.pluginConfig.type |
| 333 | } |
| 334 |
| 335 | // Transport |
| 336 | if (c.transport) { |
| 337 | form.useEncryption = c.transport.useEncryption || false |
| 338 | form.useCompression = c.transport.useCompression || false |
| 339 | form.bandwidthLimit = c.transport.bandwidthLimit || '' |
| 340 | form.bandwidthLimitMode = c.transport.bandwidthLimitMode || 'client' |
| 341 | form.proxyProtocolVersion = c.transport.proxyProtocolVersion || '' |
| 342 | } |
| 343 |
| 344 | // Load Balancer |
| 345 | if (c.loadBalancer) { |
| 346 | form.loadBalancerGroup = c.loadBalancer.group || '' |
| 347 | form.loadBalancerGroupKey = c.loadBalancer.groupKey || '' |
| 348 | } |
| 349 |
| 350 | // Health Check |
| 351 | if (c.healthCheck) { |
| 352 | form.healthCheckType = c.healthCheck.type || '' |
| 353 | form.healthCheckTimeoutSeconds = c.healthCheck.timeoutSeconds |
| 354 | form.healthCheckMaxFailed = c.healthCheck.maxFailed |
| 355 | form.healthCheckIntervalSeconds = c.healthCheck.intervalSeconds |
| 356 | form.healthCheckPath = c.healthCheck.path || '' |
| 357 | form.healthCheckHTTPHeaders = c.healthCheck.httpHeaders || [] |
| 358 | } |
| 359 |
| 360 | // Metadata |
| 361 | if (c.metadatas) { |
| 362 | form.metadatas = Object.entries(c.metadatas).map(([key, value]) => ({ |
| 363 | key, |
| 364 | value: String(value), |
| 365 | })) |
| 366 | } |
| 367 |
| 368 | // Annotations |
| 369 | if (c.annotations) { |
| 370 | form.annotations = Object.entries(c.annotations).map(([key, value]) => ({ |
| 371 | key, |
| 372 | value: String(value), |
| 373 | })) |
| 374 | } |
| 375 |
| 376 | // Type-specific fields |
| 377 | form.remotePort = c.remotePort |
| 378 |
| 379 | // Domain config |
| 380 | if (Array.isArray(c.customDomains)) { |
| 381 | form.customDomains = c.customDomains |
| 382 | } else if (c.customDomains) { |
| 383 | form.customDomains = [c.customDomains] |
| 384 | } |
| 385 | form.subdomain = c.subdomain || '' |
| 386 |
| 387 | // HTTP specific |
| 388 | if (Array.isArray(c.locations)) { |
| 389 | form.locations = c.locations |
| 390 | } else if (c.locations) { |
| 391 | form.locations = [c.locations] |
| 392 | } |
| 393 | form.httpUser = c.httpUser || '' |
| 394 | form.httpPassword = c.httpPassword || '' |
| 395 | form.hostHeaderRewrite = c.hostHeaderRewrite || '' |
| 396 | form.routeByHTTPUser = c.routeByHTTPUser || '' |
| 397 |
| 398 | // Header operations |
| 399 | if (c.requestHeaders?.set) { |
| 400 | form.requestHeaders = Object.entries(c.requestHeaders.set).map( |
| 401 | ([key, value]) => ({ key, value: String(value) }), |
| 402 | ) |
| 403 | } |
| 404 | if (c.responseHeaders?.set) { |
| 405 | form.responseHeaders = Object.entries(c.responseHeaders.set).map( |
| 406 | ([key, value]) => ({ key, value: String(value) }), |
| 407 | ) |
| 408 | } |
| 409 |
| 410 | // TCPMux |
| 411 | form.multiplexer = c.multiplexer || 'httpconnect' |
| 412 |
| 413 | // Secure types |
| 414 | form.secretKey = c.secretKey || '' |
| 415 | if (Array.isArray(c.allowUsers)) { |
| 416 | form.allowUsers = c.allowUsers |
| 417 | } else if (c.allowUsers) { |
| 418 | form.allowUsers = [c.allowUsers] |
| 419 | } |
| 420 |
| 421 | // XTCP NAT traversal |
| 422 | form.natTraversalDisableAssistedAddrs = |
| 423 | c.natTraversal?.disableAssistedAddrs || false |
| 424 |
| 425 | return form |
| 426 | } |
| 427 |
| 428 | export function storeVisitorToForm( |
| 429 | config: VisitorDefinition, |
| 430 | ): VisitorFormData { |
| 431 | const c = getStoreVisitorBlock(config) |
| 432 | const form = createDefaultVisitorForm() |
| 433 |
| 434 | form.name = config.name || '' |
| 435 | form.type = config.type || 'stcp' |
| 436 | form.enabled = c.enabled !== false |
| 437 |
| 438 | // Transport |
| 439 | if (c.transport) { |
| 440 | form.useEncryption = c.transport.useEncryption || false |
| 441 | form.useCompression = c.transport.useCompression || false |
| 442 | } |
| 443 |
| 444 | // Base fields |
| 445 | form.secretKey = c.secretKey || '' |
| 446 | form.serverUser = c.serverUser || '' |
| 447 | form.serverName = c.serverName || '' |
| 448 | form.bindAddr = c.bindAddr || '127.0.0.1' |
| 449 | form.bindPort = c.bindPort |
| 450 |
| 451 | // XTCP specific |
| 452 | form.protocol = c.protocol || 'quic' |
| 453 | form.keepTunnelOpen = c.keepTunnelOpen || false |
| 454 | form.maxRetriesAnHour = c.maxRetriesAnHour |
| 455 | form.minRetryInterval = c.minRetryInterval |
| 456 | form.fallbackTo = c.fallbackTo || '' |
| 457 | form.fallbackTimeoutMs = c.fallbackTimeoutMs |
| 458 | form.natTraversalDisableAssistedAddrs = |
| 459 | c.natTraversal?.disableAssistedAddrs || false |
| 460 |
| 461 | return form |
| 462 | } |