修复自定义 agent 名称

This commit is contained in:
dushixiang committed 2026-01-17 17:39:43 +08:00
1 parent ffde7a32ea
commit 6f08f9447a
2 files changed
+26 -16

No files matched your search

+11 -1
View File
@@ -3,6 +3,7 @@ package handler
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"strings"
"github.com/dushixiang/pika" "github.com/dushixiang/pika"
"github.com/dushixiang/pika/pkg/version" "github.com/dushixiang/pika/pkg/version"
@@ -73,6 +74,13 @@ func getServerURL(c echo.Context) string {
return scheme + "://" + host return scheme + "://" + host
} }
func bashSingleQuote(value string) string {
if value == "" {
return "''"
}
return "'" + strings.ReplaceAll(value, "'", "'\"'\"'") + "'"
}
func (h *AgentHandler) GetServerUrl(c echo.Context) error { func (h *AgentHandler) GetServerUrl(c echo.Context) error {
serverUrl := getServerURL(c) serverUrl := getServerURL(c)
return orz.Ok(c, orz.Map{ return orz.Ok(c, orz.Map{
@@ -89,6 +97,8 @@ func (h *AgentHandler) GetInstallScript(c echo.Context) error {
// 使用统一的 getServerURL 函数获取服务器地址(支持反向代理) // 使用统一的 getServerURL 函数获取服务器地址(支持反向代理)
serverUrl := getServerURL(c) serverUrl := getServerURL(c)
customName := strings.TrimSpace(c.QueryParam("name"))
customNameLiteral := bashSingleQuote(customName)
script := `#!/bin/bash script := `#!/bin/bash
set -e set -e
@@ -112,7 +122,7 @@ echo_warn() {
} }
# 解析参数 # 解析参数
AGENT_NAME_CUSTOM="" AGENT_NAME_CUSTOM=` + customNameLiteral + `
while [[ $# -gt 0 ]]; do while [[ $# -gt 0 ]]; do
case $1 in case $1 in
--name) --name)
+15 -15
View File
@@ -37,7 +37,7 @@ const AgentInstall = () => {
const [selectedOS, setSelectedOS] = useState<OSType>(DEFAULT_OS); const [selectedOS, setSelectedOS] = useState<OSType>(DEFAULT_OS);
const [apiKeys, setApiKeys] = useState<ApiKey[]>([]); const [apiKeys, setApiKeys] = useState<ApiKey[]>([]);
const [selectedApiKey, setSelectedApiKey] = useState<string>(''); const [selectedApiKey, setSelectedApiKey] = useState<string>('');
const [customHostname, setCustomHostname] = useState<string>(''); const [customAgentName, setCustomAgentName] = useState<string>('');
const [loading, setLoading] = useState<boolean>(true); const [loading, setLoading] = useState<boolean>(true);
// 服务器地址相关状态 // 服务器地址相关状态
@@ -132,12 +132,10 @@ const AgentInstall = () => {
if (!backendServerUrl || !selectedApiKey) { if (!backendServerUrl || !selectedApiKey) {
return ''; return '';
} }
let cmd = `curl -fsSL ${backendServerUrl}/api/agent/install.sh?token=${selectedApiKey} | sudo bash`; const trimmedName = customAgentName.trim();
if (customHostname) { const nameParam = trimmedName ? `&name=${encodeURIComponent(trimmedName)}` : '';
cmd = `curl -fsSL ${backendServerUrl}/api/agent/install.sh?token=${selectedApiKey} | sudo bash -s -- --name "${customHostname}"`; return `curl -fsSL ${backendServerUrl}/api/agent/install.sh?token=${selectedApiKey}${nameParam} | sudo bash`;
} }, [backendServerUrl, selectedApiKey, customAgentName]);
return cmd;
}, [backendServerUrl, selectedApiKey, customHostname]);
// API Key 选择器选项 // API Key 选择器选项
const apiKeyOptions = useMemo( const apiKeyOptions = useMemo(
@@ -243,19 +241,19 @@ const AgentInstall = () => {
<div> <div>
<div className="mb-1 text-gray-600 dark:text-slate-400"> <div className="mb-1 text-gray-600 dark:text-slate-400">
自定义主机名 <span className="text-xs text-gray-400">(可选,留空则使用系统主机名)</span> 自定义名称 <span className="text-xs text-gray-400">(可选,留空则使用主机名)</span>
</div> </div>
<Input <Input
placeholder="请输入自定义主机名,例如: my-server-01" placeholder="请输入自定义名称,例如: my-server-01"
value={customHostname} value={customAgentName}
onChange={e => setCustomHostname(e.target.value)} onChange={e => setCustomAgentName(e.target.value)}
className="w-full" className="w-full"
allowClear allowClear
/> />
</div> </div>
</Space> </Space>
</Card> </Card>
), [apiKeys, selectedApiKey, apiKeyOptions, loading, customHostname]); ), [apiKeys, selectedApiKey, apiKeyOptions, loading, customAgentName]);
// 一键安装组件 // 一键安装组件
const InstallByOneClick = useCallback(() => ( const InstallByOneClick = useCallback(() => (
@@ -350,6 +348,8 @@ ${agentCmd} version`;
const getManualInstallSteps = useCallback((os: OSType): InstallStep[] => { const getManualInstallSteps = useCallback((os: OSType): InstallStep[] => {
const config = osConfigs[os]; const config = osConfigs[os];
const trimmedName = customAgentName.trim();
if (os.startsWith('windows')) { if (os.startsWith('windows')) {
return [ return [
{ {
@@ -362,7 +362,7 @@ Invoke-WebRequest -Uri "${backendServerUrl}${config.downloadUrl}?key=${selectedA
}, },
{ {
title: '2. 注册探针', title: '2. 注册探针',
command: `.\\${AGENT_NAME_EXE} register --endpoint "${backendServerUrl}" --token "${selectedApiKey}"${customHostname ? ` --name "${customHostname}"` : ''}` command: `.\\${AGENT_NAME_EXE} register --endpoint "${backendServerUrl}" --token "${selectedApiKey}"${trimmedName ? ` --name "${trimmedName}"` : ''}`
}, },
{ {
title: '3. 验证安装', title: '3. 验证安装',
@@ -390,14 +390,14 @@ curl -L "${backendServerUrl}${config.downloadUrl}?key=${selectedApiKey}" -o ${AG
}, },
{ {
title: '4. 注册探针', title: '4. 注册探针',
command: `sudo ${AGENT_NAME} register --endpoint "${backendServerUrl}" --token "${selectedApiKey}"${customHostname ? ` --name "${customHostname}"` : ''}` command: `sudo ${AGENT_NAME} register --endpoint "${backendServerUrl}" --token "${selectedApiKey}"${trimmedName ? ` --name "${trimmedName}"` : ''}`
}, },
{ {
title: '5. 验证安装', title: '5. 验证安装',
command: `sudo ${AGENT_NAME} status` command: `sudo ${AGENT_NAME} status`
} }
]; ];
}, [osConfigs, backendServerUrl, selectedApiKey, customHostname]); }, [osConfigs, backendServerUrl, selectedApiKey, customAgentName]);
// 手动安装组件 // 手动安装组件
const InstallByManual = useCallback(() => ( const InstallByManual = useCallback(() => (