fix: AgentWizard URL拼接修正(origin/backend分离) + 复制兼容HTTP/移动端

This commit is contained in:
attacker committed 2026-06-18 07:14:19 +00:00
1 parent 2f4c0df0f2
commit 48b2d2281b
7 files changed
+60 -46

No files matched your search

+40 -36
View File
@@ -17,60 +17,64 @@ interface AgentWizardProps {
onOpenChange: (open: boolean) => void;
}
/** 自动检测服务器地址:使用当前浏览器访问的主机名,端口固定 8080 */
function detectServerAddress(): string {
/** 自动检测地址:origin(通过 Nginx 80 代理)用于安装脚本下载,backend 8080 用于 agent 直连 */
function detectServerAddress() {
if (typeof window !== 'undefined' && window.location) {
const host = window.location.hostname;
return host ? `http://${host}:8080` : 'http://127.0.0.1:8080';
return {
origin: window.location.origin, // http://10.10.10.75 → Nginx
backend: `${window.location.protocol}//${window.location.hostname}:8080`, // http://10.10.10.75:8080 → 后端直达
};
}
return 'http://127.0.0.1:8080';
return { origin: 'http://127.0.0.1', backend: 'http://127.0.0.1:8080' };
}
export function AgentWizard({ open, onOpenChange }: AgentWizardProps) {
const token = useUserStore((s) => s.token) || '<YOUR_TOKEN>';
const serverUrl = detectServerAddress();
const { origin, backend } = detectServerAddress();
const [copied, setCopied] = useState(false);
const [showManualInput, setShowManualInput] = useState(false);
const installCommand = `curl -fsSL ${serverUrl.replace(/^http:/, 'http:')}/agent/install.sh | bash -s -- --server ${serverUrl} --token ${token}`;
// install.sh 通过 Nginx 代理下载(origin:80 → /agent/ → backend:8080)
const installCommand = `curl -fsSL ${origin}/agent/install.sh | bash -s -- --server ${backend} --token ${token}`;
// 剪贴板 fallback:execCommand('copy') 兼容 HTTP 环境 // Logan.Li
const fallbackCopy = (text: string): boolean => {
const handleCopy = () => {
const textArea = document.createElement('textarea');
textArea.value = text;
textArea.value = installCommand;
// 设置样式让其可见但不可交互,确保浏览器能选中
textArea.style.position = 'fixed';
textArea.style.left = '-9999px';
textArea.style.top = '-9999px';
textArea.style.top = '0';
textArea.style.left = '0';
textArea.style.width = '2em';
textArea.style.height = '2em';
textArea.style.padding = '0';
textArea.style.border = 'none';
textArea.style.outline = 'none';
textArea.style.boxShadow = 'none';
textArea.style.background = 'transparent';
textArea.style.opacity = '0';
textArea.style.pointerEvents = 'none';
textArea.style.zIndex = '-1';
textArea.style.fontSize = '16px';
// iOS 需要 contentEditable + readOnly 兼容
textArea.contentEditable = 'true';
textArea.readOnly = true;
document.body.appendChild(textArea);
// 选中范围
textArea.focus();
textArea.select();
// 兼容移动端
if (textArea.setSelectionRange) {
textArea.setSelectionRange(0, 999999);
}
let success = false;
try {
success = document.execCommand('copy');
} catch {
// execCommand also failed
// execCommand failed
}
document.body.removeChild(textArea);
return success;
};
const handleCopy = async () => {
let success = false;
// 方式1: Clipboard API(HTTPS / localhost 可用)
try {
if (navigator.clipboard && window.isSecureContext) {
await navigator.clipboard.writeText(installCommand);
success = true;
}
} catch {
// Clipboard API 不可用,尝试 fallback
}
// 方式2: execCommand fallback(HTTP 环境也可用)
if (!success) {
success = fallbackCopy(installCommand);
}
if (success) {
setCopied(true);
@@ -82,7 +86,7 @@ export function AgentWizard({ open, onOpenChange }: AgentWizardProps) {
}
};
const binaryUrl = `${serverUrl}/agent/aiops-agent-linux-amd64`;
const binaryUrl = `${origin}/agent/aiops-agent-linux-amd64`;
const manualSteps = [
{
@@ -108,7 +112,7 @@ export function AgentWizard({ open, onOpenChange }: AgentWizardProps) {
``,
`[Service]`,
`Type=simple`,
`ExecStart=/usr/local/bin/aiops-agent --server ${serverUrl} --token ${token}`,
`ExecStart=/usr/local/bin/aiops-agent --server ${backend} --token ${token}`,
`Restart=always`,
`RestartSec=5`,
``,
@@ -186,7 +190,7 @@ export function AgentWizard({ open, onOpenChange }: AgentWizardProps) {
<div className="flex items-start gap-2 text-xs text-slate-500">
<Terminal className="h-3.5 w-3.5 mt-0.5 shrink-0" />
<span>
服务器地址已自动检测为当前访问地址({serverUrl})。Agent 安装成功后将自动连接并开始上报指标。
服务器地址已自动检测 — 安装脚本通过 Nginx 代理 ({origin}),Agent 直连后端 ({backend})。安装成功后将自动连接并开始上报指标。
</span>
</div>
</TabsContent>