feat: 优化代理准备并完善远端部署

- 增加严格代理、两分钟预取、白名单去重与分组状态展示\n- 增加远端注册令牌和安全地址校验\n- 增加 Docker Compose、Caddy 示例与控制中心部署文档
This commit is contained in:
yml
2026-11-21 20:59:35 +08:00
parent d176c421a8
commit 9c83be798d
43 changed files with 2755 additions and 119 deletions
+2 -9
View File
@@ -6,7 +6,6 @@ import (
"fmt"
"math/rand"
"net/http"
"net/url"
"path/filepath"
"strings"
"sync"
@@ -444,11 +443,7 @@ func cloneResults(source map[string]string) map[string]string {
}
func agentWebSocketURL(raw string) (string, error) {
raw = strings.TrimSpace(raw)
if raw == "" {
return "", errors.New("远端地址为空")
}
u, err := url.Parse(raw)
u, err := normalizeControlCenterURL(raw)
if err != nil {
return "", err
}
@@ -461,9 +456,7 @@ func agentWebSocketURL(raw string) (string, error) {
default:
return "", errors.New("远端地址必须以 http://、https://、ws:// 或 wss:// 开头")
}
if strings.TrimSpace(u.Path) == "" || u.Path == "/" {
u.Path = "/ws/agent"
}
u.Path = "/ws/agent"
return u.String(), nil
}
+29 -4
View File
@@ -139,7 +139,7 @@ func enrollAgent(serverURL string, options AgentOptions) (string, error) {
}
func enrollHTTPURL(raw string) (string, error) {
u, err := url.Parse(strings.TrimSpace(raw))
u, err := normalizeControlCenterURL(raw)
if err != nil {
return "", err
}
@@ -152,8 +152,33 @@ func enrollHTTPURL(raw string) (string, error) {
default:
return "", errors.New("远端地址协议无效")
}
if strings.TrimSpace(u.Path) == "" || u.Path == "/" {
u.Path = "/api/enroll"
}
u.Path = "/api/enroll"
return u.String(), nil
}
func normalizeControlCenterURL(raw string) (*url.URL, error) {
u, err := url.Parse(strings.TrimSpace(raw))
if err != nil {
return nil, err
}
switch strings.ToLower(u.Scheme) {
case "http", "https", "ws", "wss":
default:
return nil, errors.New("远端地址必须以 http://、https://、ws:// 或 wss:// 开头")
}
if u.Host == "" {
return nil, errors.New("远端地址缺少主机名")
}
if u.User != nil {
return nil, errors.New("远端地址不能包含用户名或密码")
}
if u.RawQuery != "" || u.Fragment != "" {
return nil, errors.New("远端地址不能包含查询参数或 #admin 管理令牌")
}
if path := strings.TrimRight(strings.TrimSpace(u.Path), "/"); path != "" {
return nil, errors.New("远端地址必须是控制中心根地址,不能包含子路径")
}
u.Path = ""
u.RawPath = ""
return u, nil
}
+17 -14
View File
@@ -3,6 +3,8 @@ package remote
import (
"encoding/json"
"time"
"douyu_go/internal/types"
)
const (
@@ -17,20 +19,21 @@ const (
// Snapshot is the latest state reported by one application instance.
type Snapshot struct {
MachineID string `json:"machineId"`
MachineName string `json:"machineName"`
InstanceID string `json:"instanceId"`
InstanceName string `json:"instanceName"`
Version string `json:"version"`
ActivityID string `json:"activityId,omitempty"`
State string `json:"state"`
Running bool `json:"running"`
RunID string `json:"runId,omitempty"`
Results map[string]string `json:"results,omitempty"`
AccountCount int `json:"accountCount"`
QueuedEvents uint64 `json:"queuedEvents"`
DroppedEvents uint64 `json:"droppedEvents"`
UpdatedAt time.Time `json:"updatedAt"`
MachineID string `json:"machineId"`
MachineName string `json:"machineName"`
InstanceID string `json:"instanceId"`
InstanceName string `json:"instanceName"`
Version string `json:"version"`
ActivityID string `json:"activityId,omitempty"`
State string `json:"state"`
Running bool `json:"running"`
RunID string `json:"runId,omitempty"`
Results map[string]string `json:"results,omitempty"`
ProxyStatus *types.ProxyStatusView `json:"proxyStatus,omitempty"`
AccountCount int `json:"accountCount"`
QueuedEvents uint64 `json:"queuedEvents"`
DroppedEvents uint64 `json:"droppedEvents"`
UpdatedAt time.Time `json:"updatedAt"`
}
// LogEvent is one structured event uploaded by an application instance.
+67
View File
@@ -34,6 +34,29 @@ func TestLoadOrCreateMachineIDSharedByInstances(t *testing.T) {
}
}
func TestValidateServerURLRequiresControlCenterRoot(t *testing.T) {
for _, raw := range []string{
"https://control.example.com/#admin=secret",
"https://control.example.com/control",
"https://control.example.com/?token=secret",
"https://user:pass@control.example.com",
} {
if err := ValidateServerURL(raw); err == nil {
t.Fatalf("ValidateServerURL(%q) should fail", raw)
}
}
for _, raw := range []string{
"https://control.example.com",
"https://control.example.com/",
"http://127.0.0.1:18080",
"wss://control.example.com",
} {
if err := ValidateServerURL(raw); err != nil {
t.Fatalf("ValidateServerURL(%q): %v", raw, err)
}
}
}
func TestEventSpoolPersistsOffset(t *testing.T) {
dir := t.TempDir()
spool, err := NewEventSpool(dir)
@@ -209,6 +232,50 @@ func TestAgentAutoEnrollsWithoutManualToken(t *testing.T) {
}
}
func TestAgentEnrollsWithConfiguredEnrollmentToken(t *testing.T) {
server, err := NewServer(ServerOptions{
DataDir: t.TempDir(),
OperatorToken: "operator-token",
EnrollToken: "enroll-secret",
})
if err != nil {
t.Fatal(err)
}
defer server.Close()
httpServer := httptest.NewServer(server.Handler())
defer httpServer.Close()
dataDir := t.TempDir()
agent, err := NewAgent(AgentOptions{
DataDir: dataDir,
ServerURL: httpServer.URL,
EnrollmentToken: "enroll-secret",
MachineID: "machine-secure",
MachineName: "PC-SECURE",
InstanceID: "实例 4",
Instance: "实例 4",
Version: "test",
})
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
agent.Start(ctx)
defer agent.Close()
waitUntil(t, 5*time.Second, func() bool {
instances := server.hub.Dashboard().Instances
return len(instances) == 1 && instances[0].Online
})
token, err := loadAgentCredential(dataDir, httpServer.URL, "machine-secure", "实例 4")
if err != nil {
t.Fatal(err)
}
if token == "" || token == "enroll-secret" {
t.Fatalf("unexpected issued instance token %q", token)
}
}
func TestHubAggregatesDynamicInstanceCount(t *testing.T) {
hub, err := NewHub(t.TempDir())
if err != nil {
+44 -1
View File
@@ -310,6 +310,7 @@
for (const item of instances) {
machine.append(renderInstance(item));
machine.append(renderResults(item));
machine.append(renderProxyDetails(item));
}
machinesRoot.append(machine);
}
@@ -329,11 +330,17 @@
const success = results.filter(value => String(value).startsWith('已抢到') || String(value).startsWith('成功') || String(value).includes('|积分-')).length;
const resultCell = textNode('div', `结果 ${success}/${results.length || item.accountCount || 0}`);
const meta = textNode('div', `${item.accountCount || 0} 账号 · ${item.version || ''}`, 'muted');
const proxy = item.proxyStatus;
const proxyText = proxy?.enabled
? `代理 ${proxy.activeCount || 0} · 备用 ${proxy.standbyCount || 0}/${proxy.standbyTarget || 0} · 直连 ${proxy.directCount || 0}`
: '本地直连';
const proxyWarning = ['direct_fallback', 'failed', 'degraded'].includes(proxy?.state);
const meta = textNode('div', `${item.accountCount || 0} 账号 · ${proxyText}`, proxyWarning ? 'bad' : 'muted');
const actions = element('div', 'actions');
actions.append(
resultButton(item),
proxyButton(item),
logButton(item),
commandButton(item, 'status', '刷新'),
commandButton(item, 'stop', '停止', 'danger'),
@@ -343,6 +350,42 @@
return row;
}
function renderProxyDetails(item) {
const panel = element('div', 'results-details');
panel.dataset.proxy = encodeURIComponent(`${item.machineId}:${item.instanceId}`);
const proxy = item.proxyStatus;
if (!proxy?.enabled) {
panel.append(textNode('div', '当前实例未启用代理', 'muted'));
return panel;
}
const summary = element('div', 'result-line');
summary.append(
textNode('span', `状态 ${proxy.state || 'unknown'}${proxy.required ? ' · 严格代理' : ''}`, ['direct_fallback', 'failed', 'degraded'].includes(proxy.state) ? 'bad' : 'ok'),
textNode('span', `活动 ${proxy.activeCount || 0} · 备用 ${proxy.standbyCount || 0}/${proxy.standbyTarget || 0} · 直连 ${proxy.directCount || 0}`, 'muted')
);
panel.append(summary);
for (const group of proxy.groups || []) {
const line = element('div', 'result-line');
line.append(
textNode('span', `G${group.groupIndex} · ${group.accountCount || 0}号 · ${group.state || ''}`, group.state === 'direct' || group.state === 'failed' ? 'bad' : 'muted'),
textNode('span', `${group.proxy || '-'} ${group.region ? '· ' + group.region : ''} ${group.rttMs ? '· ' + group.rttMs + 'ms' : ''} ${group.switchCount ? '· 切换 ' + group.switchCount : ''}`, 'muted')
);
panel.append(line);
}
if (proxy.lastError) panel.append(textNode('div', proxy.lastError, 'bad'));
return panel;
}
function proxyButton(item) {
const button = element('button', '', '代理');
button.addEventListener('click', () => {
const key = encodeURIComponent(`${item.machineId}:${item.instanceId}`);
const panel = machinesRoot.querySelector(`.results-details[data-proxy="${key}"]`);
if (panel) panel.classList.toggle('open');
});
return button;
}
function logButton(item) {
const button = element('button', '', '日志');
button.addEventListener('click', () => {