[CmdletBinding()] param( [Parameter(Mandatory)][string]$PxhjRoot, [Parameter(Mandatory)][string]$CredentialFile, [string]$CoordinatorUrl = 'http://127.0.0.1:18100', [string]$UdpTargetHost = '127.0.0.1', [switch]$Force ) . (Join-Path $PSScriptRoot 'Common.ps1') $address = $null if (-not [Uri]::TryCreate($CoordinatorUrl, [UriKind]::Absolute, [ref]$address) -or $address.Scheme -notin @('http', 'https') -or -not $address.Host -or $address.UserInfo -or $address.Query -or $address.Fragment -or $address.AbsolutePath -ne '/') { throw 'CoordinatorUrl 必须是完整的总控根地址。' } if ($UdpTargetHost -notmatch '^[a-zA-Z0-9_.:-]{1,200}$') { throw 'UdpTargetHost 必须是网关能够访问的 PXHJ 接收地址。' } $root = [System.IO.Path]::GetFullPath($PxhjRoot) if (-not (Test-Path -LiteralPath (Join-Path $root 'backend/src/main/resources/application.yml'))) { throw 'PxhjRoot 中没有找到 PXHJ 后端配置。' } $credentialContent = Get-Content -LiteralPath $CredentialFile -Raw $tokenMatch = [regex]::Match($credentialContent, '(?m)^fusion\.token=([^\r\n]{24,})\r?$') $clusterMatch = [regex]::Match($credentialContent, '(?m)^fusion\.cluster-id=([a-zA-Z0-9_-]{1,64})\r?$') if (-not $tokenMatch.Success -or -not $clusterMatch.Success) { throw '集群凭证文件格式不正确。' } $configFolder = Join-Path $root 'config' $privateFile = Join-Path $configFolder 'fusion.local.properties' if ((Test-Path -LiteralPath $privateFile) -and -not $Force) { throw 'PXHJ 私有融合配置已存在;需要覆盖时显式使用 -Force。' } [System.IO.Directory]::CreateDirectory($configFolder) | Out-Null $content = @('app.fusion.enabled=true', ('app.fusion.coordinator-url=' + $CoordinatorUrl.TrimEnd('/')), ('app.fusion.cluster-id=' + $clusterMatch.Groups[1].Value), ('app.fusion.token=' + $tokenMatch.Groups[1].Value), ('app.fusion.udp-target-host=' + $UdpTargetHost)) -join "`n" Write-XtrhText -Path $privateFile -Content ($content + "`n") Protect-XtrhPrivateFile -Path $privateFile # 只生成私有桥接配置,不擅自重启正在运行的 PXHJ 或修改采集器上传地址。 Write-Output ('PXHJ 总控连接配置已写入:' + $privateFile + '。后端重启后加载。')