mirror of
https://github.com/Sagit-chu/flvx.git
synced 2026-11-16 09:34:22 +08:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f357b30500 | |||
| 8339f52280 | |||
| 2e85234612 | |||
| b26e9ee9e4 |
@@ -103,10 +103,10 @@ jobs:
|
|||||||
- name: Download dependencies
|
- name: Download dependencies
|
||||||
run: go mod download
|
run: go mod download
|
||||||
|
|
||||||
- name: Run PostgreSQL contract test
|
- name: Run PostgreSQL contract tests
|
||||||
env:
|
env:
|
||||||
FLVX_POSTGRES_TEST_DSN: 'postgres://flux_test:flux_test_pass@127.0.0.1:5432/flux_test?sslmode=disable'
|
FLVX_POSTGRES_TEST_DSN: 'postgres://flux_test:flux_test_pass@127.0.0.1:5432/flux_test?sslmode=disable'
|
||||||
run: go test ./tests/contract -run TestPostgresNodeCreateRepairsMissingIDDefaultContract -count=1
|
run: go test ./tests/contract -run 'TestPostgres(NodeCreateRepairsMissingIDDefaultContract|NewRelayTunnelEmitsMatchingPrivateEndpointAuthentication)$' -count=1
|
||||||
|
|
||||||
agent:
|
agent:
|
||||||
name: Build Agent
|
name: Build Agent
|
||||||
|
|||||||
@@ -25,6 +25,27 @@ Go 后端和 agent 的最低编译版本为 **Go 1.26.10**;若使用 Go 1.27
|
|||||||
|
|
||||||
## 部署流程
|
## 部署流程
|
||||||
---
|
---
|
||||||
|
### 让 AI Agent 帮你安装(复制即用)
|
||||||
|
|
||||||
|
将下面整段提示词复制给能执行终端命令的 AI Agent(如 Codex、Claude Code)。Agent 已运行在目标 Linux 服务器上时可直接安装;远程安装时,提供 SSH 主机别名或连接方式,使用已有 SSH 密钥登录即可,无需把私钥贴进对话。
|
||||||
|
|
||||||
|
```text
|
||||||
|
请帮我在目标 Linux 服务器上安装 FLVX 流量转发面板,直接执行安装并完成验证。
|
||||||
|
|
||||||
|
项目仓库:https://github.com/Sagit-chu/flvx
|
||||||
|
安装文档:https://github.com/Sagit-chu/flvx/blob/main/doc/install.md
|
||||||
|
|
||||||
|
1. 确认目标服务器:如果当前终端就在目标 Linux 服务器上,直接使用当前环境;否则使用我提供的 SSH 连接。缺少连接信息时,只询问必要信息,确认目标后再执行安装。
|
||||||
|
2. 先阅读项目 README、安装文档及将要执行的安装脚本,检查系统、架构、root/sudo 权限、Docker、Compose、端口占用和已有 FLVX 容器、数据卷及部署文件。已有部署时先确认是更新还是新装,保留原配置和数据。
|
||||||
|
3. 新安装默认使用仓库标记为 latest 的 Release、Docker Compose、SQLite,部署目录 /opt/flvx-panel,前端端口 6366、后端端口 6365。我已指定的版本、目录或端口优先;端口冲突时先与我确认替代端口。
|
||||||
|
4. 缺少依赖时按该发行版的官方方式安装 Docker Engine 和 Compose 插件。进入部署目录,从项目 Releases 下载所选版本的 panel_install.sh,先检查下载成功和脚本内容,再执行。使用交互式终端操作菜单,选择“安装面板”并按上述配置输入;以脚本实际提示为准。
|
||||||
|
5. 默认直连 GitHub,可设置 PROXY_ENABLED=false;连接失败时说明原因,再与我确认是否使用代理。脚本可能修改 Docker IPv6 配置并重启 Docker;若会影响现有服务,先说明影响并确认。保留现有防火墙和 SSH 规则,仅按需放行实际使用的面板端口;云安全组无法操作时列出需要我放行的端口。
|
||||||
|
6. 安装后检查 Compose 服务状态、后端健康检查、前端 HTTP 页面及后端 /flow/test 的响应,并从实际访问端验证连通性。失败时查看相关日志并修复后重试;受网络或权限限制无法完成的检查要明确说明,不能直接宣称安装成功。
|
||||||
|
7. 最后给我实际安装版本、部署目录、访问地址、验证结果,以及在该目录查看状态、查看日志和重启服务的命令。说明默认管理员账号和密码均为 admin_user,提醒首次登录立即修改密码;不要输出 .env 中的 JWT_SECRET、数据库密码等密钥。
|
||||||
|
```
|
||||||
|
|
||||||
|
需要安装转发节点时,告诉 Agent 按[节点端部署文档](doc/install.md#二节点端部署)操作,并提供节点服务器、面板后端地址及从面板获取的节点接入密钥;安装完成后以服务正常运行且面板显示节点在线为准。
|
||||||
|
|
||||||
### Docker Compose部署
|
### Docker Compose部署
|
||||||
#### 快速部署(安装最新版)
|
#### 快速部署(安装最新版)
|
||||||
面板端:
|
面板端:
|
||||||
|
|||||||
@@ -228,6 +228,7 @@ func (h *Handler) Register(mux *http.ServeMux) {
|
|||||||
mux.HandleFunc("/api/v1/tunnel/update-order", h.tunnelUpdateOrder)
|
mux.HandleFunc("/api/v1/tunnel/update-order", h.tunnelUpdateOrder)
|
||||||
mux.HandleFunc("/api/v1/tunnel/batch-delete", h.tunnelBatchDelete)
|
mux.HandleFunc("/api/v1/tunnel/batch-delete", h.tunnelBatchDelete)
|
||||||
mux.HandleFunc("/api/v1/tunnel/batch-redeploy", h.tunnelBatchRedeploy)
|
mux.HandleFunc("/api/v1/tunnel/batch-redeploy", h.tunnelBatchRedeploy)
|
||||||
|
mux.HandleFunc("/api/v1/tunnel/batch-redeploy/stream", h.tunnelBatchRedeployStream)
|
||||||
mux.HandleFunc("/api/v1/tunnel/user/assign", h.userTunnelAssign)
|
mux.HandleFunc("/api/v1/tunnel/user/assign", h.userTunnelAssign)
|
||||||
mux.HandleFunc("/api/v1/tunnel/user/batch-assign", h.userTunnelBatchAssign)
|
mux.HandleFunc("/api/v1/tunnel/user/batch-assign", h.userTunnelBatchAssign)
|
||||||
mux.HandleFunc("/api/v1/tunnel/user/remove", h.userTunnelRemove)
|
mux.HandleFunc("/api/v1/tunnel/user/remove", h.userTunnelRemove)
|
||||||
@@ -1122,6 +1123,14 @@ func normalizeAndValidateConfigValue(key, value string) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return pngDataURLPrefix + payload, nil
|
return pngDataURLPrefix + payload, nil
|
||||||
|
case sequentialPortConfigKey:
|
||||||
|
normalized := strings.TrimSpace(strings.ToLower(value))
|
||||||
|
switch normalized {
|
||||||
|
case "true", "false":
|
||||||
|
return normalized, nil
|
||||||
|
default:
|
||||||
|
return "", fmt.Errorf("顺次分配端口开关配置值无效")
|
||||||
|
}
|
||||||
case monitorTunnelQualityEnabledConfigKey:
|
case monitorTunnelQualityEnabledConfigKey:
|
||||||
normalized := strings.TrimSpace(strings.ToLower(value))
|
normalized := strings.TrimSpace(strings.ToLower(value))
|
||||||
switch normalized {
|
switch normalized {
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"math/big"
|
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sort"
|
"sort"
|
||||||
@@ -1315,13 +1314,13 @@ func (h *Handler) tunnelUpdate(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var runtimePending error
|
||||||
newEntryNodeIDs, _ = h.tunnelEntryNodeIDs(id)
|
newEntryNodeIDs, _ = h.tunnelEntryNodeIDs(id)
|
||||||
if !sameInt64Set(oldEntryNodeIDs, newEntryNodeIDs) {
|
if !sameInt64Set(oldEntryNodeIDs, newEntryNodeIDs) {
|
||||||
h.cleanupTunnelForwardRuntimesOnRemovedEntryNodes(id, oldEntryNodeIDs, newEntryNodeIDs)
|
h.cleanupTunnelForwardRuntimesOnRemovedEntryNodes(id, oldEntryNodeIDs, newEntryNodeIDs)
|
||||||
h.syncTunnelForwardsEntryPorts(id, newEntryNodeIDs)
|
runtimePending = h.syncTunnelForwardsEntryPorts(id, newEntryNodeIDs)
|
||||||
}
|
}
|
||||||
|
|
||||||
var runtimePending error
|
|
||||||
if typeVal == 2 {
|
if typeVal == 2 {
|
||||||
applyRuntime := h.applyTunnelRuntime
|
applyRuntime := h.applyTunnelRuntime
|
||||||
if oldTunnel != nil && oldTunnel.Type == 2 {
|
if oldTunnel != nil && oldTunnel.Type == 2 {
|
||||||
@@ -1329,9 +1328,9 @@ func (h *Handler) tunnelUpdate(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
createdChains, createdServices, applyErr := applyRuntime(runtimeState)
|
createdChains, createdServices, applyErr := applyRuntime(runtimeState)
|
||||||
if isTunnelRuntimePending(applyErr) {
|
if isTunnelRuntimePending(applyErr) {
|
||||||
runtimePending = applyErr
|
runtimePending = errors.Join(runtimePending, applyErr)
|
||||||
}
|
}
|
||||||
if applyErr != nil && runtimePending == nil {
|
if applyErr != nil && !isTunnelRuntimePending(applyErr) {
|
||||||
updateProtocol := "tls"
|
updateProtocol := "tls"
|
||||||
if len(runtimeState.InNodes) > 0 && strings.TrimSpace(runtimeState.InNodes[0].Protocol) != "" {
|
if len(runtimeState.InNodes) > 0 && strings.TrimSpace(runtimeState.InNodes[0].Protocol) != "" {
|
||||||
updateProtocol = strings.TrimSpace(runtimeState.InNodes[0].Protocol)
|
updateProtocol = strings.TrimSpace(runtimeState.InNodes[0].Protocol)
|
||||||
@@ -1597,19 +1596,20 @@ func (h *Handler) validateTunnelEntryPortConflictsForNewEntriesTx(tx *gorm.DB, t
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) syncTunnelForwardsEntryPorts(tunnelID int64, entryNodeIDs []int64) {
|
func (h *Handler) syncTunnelForwardsEntryPorts(tunnelID int64, entryNodeIDs []int64) error {
|
||||||
if h == nil || h.repo == nil || tunnelID <= 0 {
|
if h == nil || h.repo == nil || tunnelID <= 0 {
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
entryNodeIDs = uniqueInt64s(entryNodeIDs)
|
entryNodeIDs = uniqueInt64s(entryNodeIDs)
|
||||||
if len(entryNodeIDs) == 0 {
|
if len(entryNodeIDs) == 0 {
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
forwards, err := h.listForwardsByTunnel(tunnelID)
|
forwards, err := h.listForwardsByTunnel(tunnelID)
|
||||||
if err != nil || len(forwards) == 0 {
|
if err != nil {
|
||||||
return
|
return err
|
||||||
}
|
}
|
||||||
|
var syncErrors []error
|
||||||
|
|
||||||
allowInIP := len(entryNodeIDs) == 1
|
allowInIP := len(entryNodeIDs) == 1
|
||||||
for i := range forwards {
|
for i := range forwards {
|
||||||
@@ -1619,6 +1619,7 @@ func (h *Handler) syncTunnelForwardsEntryPorts(tunnelID int64, entryNodeIDs []in
|
|||||||
}
|
}
|
||||||
oldPorts, err := h.listForwardPorts(f.ID)
|
oldPorts, err := h.listForwardPorts(f.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
syncErrors = append(syncErrors, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
referencePort := pickForwardPortFromRecords(oldPorts)
|
referencePort := pickForwardPortFromRecords(oldPorts)
|
||||||
@@ -1660,14 +1661,17 @@ func (h *Handler) syncTunnelForwardsEntryPorts(tunnelID int64, entryNodeIDs []in
|
|||||||
}
|
}
|
||||||
entries = append(entries, forwardPortReplaceEntry{NodeID: nid, Port: port, InIP: inIP})
|
entries = append(entries, forwardPortReplaceEntry{NodeID: nid, Port: port, InIP: inIP})
|
||||||
}
|
}
|
||||||
_ = h.repo.ReplaceForwardPorts(f.ID, entries)
|
if err := h.repo.ReplaceForwardPortsIndividually(f.ID, entries, h.isSequentialPortEnabled()); err != nil {
|
||||||
|
syncErrors = append(syncErrors, fmt.Errorf("转发 %s 入口端口更新失败: %w", f.Name, err))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
return errors.Join(syncErrors...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// resolvePortForNewEntryNode determines the port for a forward on a newly added
|
// resolvePortForNewEntryNode determines the port for a forward on a newly added
|
||||||
// entry node. It tries to reuse referencePort (from existing entries); if that
|
// entry node. It tries to reuse referencePort (from existing entries); if that
|
||||||
// port is out of range or already occupied, it picks a random available port
|
// port is out of range or already occupied, zero requests atomic allocation
|
||||||
// for this specific node.
|
// when ReplaceForwardPorts persists the new entry. Explicit ports are rechecked there.
|
||||||
func (h *Handler) resolvePortForNewEntryNode(nodeID int64, referencePort int, forwardID int64) int {
|
func (h *Handler) resolvePortForNewEntryNode(nodeID int64, referencePort int, forwardID int64) int {
|
||||||
node, err := h.getNodeRecord(nodeID)
|
node, err := h.getNodeRecord(nodeID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -1684,48 +1688,23 @@ func (h *Handler) resolvePortForNewEntryNode(nodeID int64, referencePort int, fo
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// referencePort doesn't work for this node; pick a random one.
|
// Defer fallback selection to the same transaction that saves the entries.
|
||||||
newPort := h.pickRandomPortForNode(nodeID)
|
return 0
|
||||||
if newPort > 0 {
|
|
||||||
return newPort
|
|
||||||
}
|
|
||||||
return referencePort // last resort fallback
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// pickRandomPortForNode picks a random available port from a single node's
|
// sequentialPortConfigKey 是「未指定端口时按端口范围顺次分配」开关的配置键。
|
||||||
// port range, excluding ports already occupied by other forwards or chains.
|
const sequentialPortConfigKey = "sequential_port_enabled"
|
||||||
func (h *Handler) pickRandomPortForNode(nodeID int64) int {
|
|
||||||
portRange, err := h.repo.GetNodePortRange(nodeID)
|
// isSequentialPortEnabled 返回是否开启了顺次分配端口(默认关闭,保持随机分配)。
|
||||||
|
func (h *Handler) isSequentialPortEnabled() bool {
|
||||||
|
if h == nil || h.repo == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
value, err := h.repo.GetViteConfigValue(sequentialPortConfigKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0
|
return false
|
||||||
}
|
}
|
||||||
if portRange == "" {
|
return strings.EqualFold(strings.TrimSpace(value), "true")
|
||||||
portRange = "1000-65535"
|
|
||||||
}
|
|
||||||
|
|
||||||
nodePorts, err := parsePorts(portRange)
|
|
||||||
if err != nil || len(nodePorts) == 0 {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
used, err := h.getUsedPorts(nodeID)
|
|
||||||
if err != nil {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
var available []int
|
|
||||||
for _, p := range nodePorts {
|
|
||||||
if !used[p] {
|
|
||||||
available = append(available, p)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(available) == 0 {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
idx, _ := rand.Int(rand.Reader, big.NewInt(int64(len(available))))
|
|
||||||
return available[idx.Int64()]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) tunnelDelete(w http.ResponseWriter, r *http.Request) {
|
func (h *Handler) tunnelDelete(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -2046,51 +2025,37 @@ func normalizeBatchFailureReason(reason string) string {
|
|||||||
return trimmed
|
return trimmed
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *Handler) redeployTunnelConfiguration(tunnelID int64) error {
|
||||||
|
if _, err := h.getTunnelRecord(tunnelID); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if nftMode, entryNodeIDs, err := h.tunnelUsesNftables(tunnelID); err != nil {
|
||||||
|
return err
|
||||||
|
} else if nftMode {
|
||||||
|
if len(entryNodeIDs) == 0 {
|
||||||
|
return errors.New("nftables 转发缺少入口节点")
|
||||||
|
}
|
||||||
|
return h.reconcileNftablesNodeByRequest(entryNodeIDs[0])
|
||||||
|
}
|
||||||
|
return h.redeployTunnelAndForwards(tunnelID)
|
||||||
|
}
|
||||||
|
|
||||||
func (h *Handler) tunnelBatchRedeploy(w http.ResponseWriter, r *http.Request) {
|
func (h *Handler) tunnelBatchRedeploy(w http.ResponseWriter, r *http.Request) {
|
||||||
ids := idsFromBody(r, w)
|
ids := idsFromBody(r, w)
|
||||||
if ids == nil {
|
if ids == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
success := 0
|
result := batchOperationResult{}
|
||||||
fail := 0
|
|
||||||
failures := make([]batchFailureDetail, 0)
|
|
||||||
for _, tunnelID := range ids {
|
for _, tunnelID := range ids {
|
||||||
tunnel, tunnelErr := h.getTunnelRecord(tunnelID)
|
name, _ := h.repo.GetTunnelName(tunnelID)
|
||||||
tunnelName := ""
|
if err := h.redeployTunnelConfiguration(tunnelID); err != nil {
|
||||||
if tunnelErr == nil && tunnel != nil {
|
result.FailCount++
|
||||||
tunnelName, _ = h.repo.GetTunnelName(tunnelID)
|
result.Failures = appendBatchFailure(result.Failures, tunnelID, name, err)
|
||||||
|
} else {
|
||||||
|
result.SuccessCount++
|
||||||
}
|
}
|
||||||
if tunnelErr != nil {
|
|
||||||
fail++
|
|
||||||
failures = appendBatchFailure(failures, tunnelID, tunnelName, tunnelErr)
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
if nftMode, entryNodeIDs, modeErr := h.tunnelUsesNftables(tunnelID); modeErr != nil {
|
response.WriteJSON(w, response.OK(result))
|
||||||
fail++
|
|
||||||
failures = appendBatchFailure(failures, tunnelID, tunnelName, modeErr)
|
|
||||||
continue
|
|
||||||
} else if nftMode {
|
|
||||||
if len(entryNodeIDs) == 0 {
|
|
||||||
fail++
|
|
||||||
failures = appendBatchFailureReason(failures, tunnelID, tunnelName, "nftables 转发缺少入口节点")
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if reconcileErr := h.reconcileNftablesNodeByRequest(entryNodeIDs[0]); reconcileErr != nil {
|
|
||||||
fail++
|
|
||||||
failures = appendBatchFailure(failures, tunnelID, tunnelName, reconcileErr)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
success++
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if err := h.redeployTunnelAndForwards(tunnelID); err != nil {
|
|
||||||
fail++
|
|
||||||
failures = appendBatchFailure(failures, tunnelID, tunnelName, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
success++
|
|
||||||
}
|
|
||||||
response.WriteJSON(w, response.OK(batchOperationResult{SuccessCount: success, FailCount: fail, Failures: failures}))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) userTunnelAssign(w http.ResponseWriter, r *http.Request) {
|
func (h *Handler) userTunnelAssign(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -2304,12 +2269,7 @@ func (h *Handler) forwardCreate(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
port := asInt(req["inPort"], 0)
|
port := asInt(req["inPort"], 0)
|
||||||
if port <= 0 {
|
// A zero port is allocated and persisted atomically by the repository.
|
||||||
port = h.pickTunnelPort(tunnelID)
|
|
||||||
}
|
|
||||||
if port <= 0 {
|
|
||||||
port = 10000
|
|
||||||
}
|
|
||||||
entryNodes, _ := h.tunnelEntryNodeIDs(tunnelID)
|
entryNodes, _ := h.tunnelEntryNodeIDs(tunnelID)
|
||||||
isNftTunnel, _, err := h.tunnelUsesNftables(tunnelID)
|
isNftTunnel, _, err := h.tunnelUsesNftables(tunnelID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -2328,6 +2288,9 @@ func (h *Handler) forwardCreate(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
for _, nodeID := range entryNodes {
|
for _, nodeID := range entryNodes {
|
||||||
|
if port <= 0 {
|
||||||
|
break // Repository validates all nodes while allocating atomically.
|
||||||
|
}
|
||||||
node, nodeErr := h.getNodeRecord(nodeID)
|
node, nodeErr := h.getNodeRecord(nodeID)
|
||||||
if nodeErr != nil {
|
if nodeErr != nil {
|
||||||
continue
|
continue
|
||||||
@@ -2360,7 +2323,7 @@ func (h *Handler) forwardCreate(w http.ResponseWriter, r *http.Request) {
|
|||||||
proxyProtocolReceive := asInt(req["proxyProtocolReceive"], 0)
|
proxyProtocolReceive := asInt(req["proxyProtocolReceive"], 0)
|
||||||
proxyProtocolSend := asInt(req["proxyProtocolSend"], proxyProtocol)
|
proxyProtocolSend := asInt(req["proxyProtocolSend"], proxyProtocol)
|
||||||
|
|
||||||
forwardID, err := h.repo.CreateForwardTx(userID, userName, name, tunnelID, remoteAddr, defaultString(asString(req["strategy"]), "fifo"), now, inx, entryNodes, port, inIp, nullableInt(speedID), maxConn, ipMaxConn, nullableInt(ipSpeedID), proxyProtocol, proxyProtocolReceive, proxyProtocolSend)
|
forwardID, err := h.repo.CreateForwardTx(userID, userName, name, tunnelID, remoteAddr, defaultString(asString(req["strategy"]), "fifo"), now, inx, entryNodes, port, inIp, nullableInt(speedID), maxConn, ipMaxConn, nullableInt(ipSpeedID), proxyProtocol, proxyProtocolReceive, proxyProtocolSend, h.isSequentialPortEnabled())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
response.WriteJSON(w, response.Err(-2, err.Error()))
|
response.WriteJSON(w, response.Err(-2, err.Error()))
|
||||||
return
|
return
|
||||||
@@ -2498,9 +2461,6 @@ func (h *Handler) forwardUpdate(w http.ResponseWriter, r *http.Request) {
|
|||||||
if minPort.Valid {
|
if minPort.Valid {
|
||||||
port = int(minPort.Int64)
|
port = int(minPort.Int64)
|
||||||
}
|
}
|
||||||
if port <= 0 {
|
|
||||||
port = h.pickTunnelPort(tunnelID)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
hasInIP := false
|
hasInIP := false
|
||||||
inIp := ""
|
inIp := ""
|
||||||
@@ -2525,6 +2485,9 @@ func (h *Handler) forwardUpdate(w http.ResponseWriter, r *http.Request) {
|
|||||||
keptNodeIDs = diffInt64s(oldNodeIDs, removedNodeIDs)
|
keptNodeIDs = diffInt64s(oldNodeIDs, removedNodeIDs)
|
||||||
}
|
}
|
||||||
for _, nodeID := range fwdEntryNodes {
|
for _, nodeID := range fwdEntryNodes {
|
||||||
|
if port <= 0 {
|
||||||
|
break // Repository validates all nodes while allocating atomically.
|
||||||
|
}
|
||||||
node, nodeErr := h.getNodeRecord(nodeID)
|
node, nodeErr := h.getNodeRecord(nodeID)
|
||||||
if nodeErr != nil {
|
if nodeErr != nil {
|
||||||
continue
|
continue
|
||||||
@@ -3047,9 +3010,6 @@ func (h *Handler) forwardBatchChangeTunnel(w http.ResponseWriter, r *http.Reques
|
|||||||
if port.Valid {
|
if port.Valid {
|
||||||
p = int(port.Int64)
|
p = int(port.Int64)
|
||||||
}
|
}
|
||||||
if p <= 0 {
|
|
||||||
p = h.pickTunnelPort(req.TargetTunnelID)
|
|
||||||
}
|
|
||||||
bctEntryNodes, _ := h.tunnelEntryNodeIDs(req.TargetTunnelID)
|
bctEntryNodes, _ := h.tunnelEntryNodeIDs(req.TargetTunnelID)
|
||||||
newNodeIDs := uniqueInt64s(bctEntryNodes)
|
newNodeIDs := uniqueInt64s(bctEntryNodes)
|
||||||
removedNodeIDs := diffInt64s(oldNodeIDs, newNodeIDs)
|
removedNodeIDs := diffInt64s(oldNodeIDs, newNodeIDs)
|
||||||
@@ -3057,6 +3017,9 @@ func (h *Handler) forwardBatchChangeTunnel(w http.ResponseWriter, r *http.Reques
|
|||||||
portRangeOk := true
|
portRangeOk := true
|
||||||
var portRangeErr error
|
var portRangeErr error
|
||||||
for _, nid := range bctEntryNodes {
|
for _, nid := range bctEntryNodes {
|
||||||
|
if p <= 0 {
|
||||||
|
break // Repository validates all nodes while allocating atomically.
|
||||||
|
}
|
||||||
nd, ndErr := h.getNodeRecord(nid)
|
nd, ndErr := h.getNodeRecord(nid)
|
||||||
if ndErr != nil {
|
if ndErr != nil {
|
||||||
portRangeErr = ndErr
|
portRangeErr = ndErr
|
||||||
@@ -4986,75 +4949,6 @@ func (h *Handler) tunnelEntryNodeIDs(tunnelID int64) ([]int64, error) {
|
|||||||
return h.repo.TunnelEntryNodeIDs(tunnelID)
|
return h.repo.TunnelEntryNodeIDs(tunnelID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) pickTunnelPort(tunnelID int64) int {
|
|
||||||
entryNodes, err := h.tunnelEntryNodeIDs(tunnelID)
|
|
||||||
if err != nil || len(entryNodes) == 0 {
|
|
||||||
return 10000
|
|
||||||
}
|
|
||||||
|
|
||||||
var commonAvailable []int
|
|
||||||
firstNode := true
|
|
||||||
|
|
||||||
for _, nodeID := range entryNodes {
|
|
||||||
portRange, err := h.repo.GetNodePortRange(nodeID)
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if portRange == "" {
|
|
||||||
portRange = "1000-65535"
|
|
||||||
}
|
|
||||||
|
|
||||||
nodePorts, err := parsePorts(portRange)
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
used, err := h.getUsedPorts(nodeID)
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
var available []int
|
|
||||||
for _, p := range nodePorts {
|
|
||||||
if !used[p] {
|
|
||||||
available = append(available, p)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if firstNode {
|
|
||||||
commonAvailable = available
|
|
||||||
firstNode = false
|
|
||||||
} else {
|
|
||||||
set := make(map[int]bool)
|
|
||||||
for _, p := range available {
|
|
||||||
set[p] = true
|
|
||||||
}
|
|
||||||
var newCommon []int
|
|
||||||
for _, p := range commonAvailable {
|
|
||||||
if set[p] {
|
|
||||||
newCommon = append(newCommon, p)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
commonAvailable = newCommon
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(commonAvailable) == 0 {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(commonAvailable) > 0 {
|
|
||||||
idx, _ := rand.Int(rand.Reader, big.NewInt(int64(len(commonAvailable))))
|
|
||||||
return commonAvailable[idx.Int64()]
|
|
||||||
}
|
|
||||||
|
|
||||||
return 10000
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *Handler) getUsedPorts(nodeID int64) (map[int]bool, error) {
|
|
||||||
return h.repo.GetUsedPortsOnNodeAsMap(nodeID)
|
|
||||||
}
|
|
||||||
|
|
||||||
func parsePorts(portRange string) ([]int, error) {
|
func parsePorts(portRange string) ([]int, error) {
|
||||||
var ports []int
|
var ports []int
|
||||||
parts := strings.Split(portRange, ",")
|
parts := strings.Split(portRange, ",")
|
||||||
@@ -5127,7 +5021,7 @@ func (h *Handler) replaceForwardPorts(forwardID, tunnelID int64, port int, inIp
|
|||||||
for i, nid := range entryNodes {
|
for i, nid := range entryNodes {
|
||||||
entries[i] = forwardPortReplaceEntry{NodeID: nid, Port: port, InIP: inIp}
|
entries[i] = forwardPortReplaceEntry{NodeID: nid, Port: port, InIP: inIp}
|
||||||
}
|
}
|
||||||
return h.repo.ReplaceForwardPorts(forwardID, entries)
|
return h.repo.ReplaceForwardPorts(forwardID, entries, h.isSequentialPortEnabled())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) replaceForwardPortsPreservingInIP(forwardID, tunnelID int64, port int, oldPorts []forwardPortRecord) error {
|
func (h *Handler) replaceForwardPortsPreservingInIP(forwardID, tunnelID int64, port int, oldPorts []forwardPortRecord) error {
|
||||||
@@ -5136,7 +5030,7 @@ func (h *Handler) replaceForwardPortsPreservingInIP(forwardID, tunnelID int64, p
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
entries := buildForwardPortEntriesWithPreservedInIP(entryNodes, oldPorts, port)
|
entries := buildForwardPortEntriesWithPreservedInIP(entryNodes, oldPorts, port)
|
||||||
return h.repo.ReplaceForwardPorts(forwardID, entries)
|
return h.repo.ReplaceForwardPorts(forwardID, entries, h.isSequentialPortEnabled())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) replaceForwardPortsWithRecords(forwardID int64, ports []forwardPortRecord) error {
|
func (h *Handler) replaceForwardPortsWithRecords(forwardID int64, ports []forwardPortRecord) error {
|
||||||
@@ -5144,7 +5038,7 @@ func (h *Handler) replaceForwardPortsWithRecords(forwardID int64, ports []forwar
|
|||||||
for i, fp := range ports {
|
for i, fp := range ports {
|
||||||
entries[i] = forwardPortReplaceEntry{NodeID: fp.NodeID, Port: fp.Port, InIP: fp.InIP}
|
entries[i] = forwardPortReplaceEntry{NodeID: fp.NodeID, Port: fp.Port, InIP: fp.InIP}
|
||||||
}
|
}
|
||||||
return h.repo.ReplaceForwardPorts(forwardID, entries)
|
return h.repo.ReplaceForwardPorts(forwardID, entries, h.isSequentialPortEnabled())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) rollbackForwardMutation(oldForward *forwardRecord, oldPorts []forwardPortRecord) {
|
func (h *Handler) rollbackForwardMutation(oldForward *forwardRecord, oldPorts []forwardPortRecord) {
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
package handler
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestNormalizeSequentialPortConfigValue(t *testing.T) {
|
||||||
|
for in, want := range map[string]string{"true": "true", " FALSE ": "false"} {
|
||||||
|
got, err := normalizeAndValidateConfigValue(sequentialPortConfigKey, in)
|
||||||
|
if err != nil || got != want {
|
||||||
|
t.Fatalf("value %q: got %q err=%v, want %q", in, got, err, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if _, err := normalizeAndValidateConfigValue(sequentialPortConfigKey, "maybe"); err == nil {
|
||||||
|
t.Fatal("expected invalid value to be rejected")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSequentialPortConfigReadsRepositoryAndDefaultsOff(t *testing.T) {
|
||||||
|
_, r := setupConfigAccessTestRouter(t)
|
||||||
|
h := &Handler{repo: r}
|
||||||
|
if h.isSequentialPortEnabled() {
|
||||||
|
t.Fatal("missing config must preserve random allocation")
|
||||||
|
}
|
||||||
|
for _, tc := range []struct {
|
||||||
|
value string
|
||||||
|
want bool
|
||||||
|
}{
|
||||||
|
{"true", true}, {" TRUE ", true}, {"false", false}, {"invalid", false},
|
||||||
|
} {
|
||||||
|
seedConfigValue(t, r, sequentialPortConfigKey, tc.value)
|
||||||
|
if got := h.isSequentialPortEnabled(); got != tc.want {
|
||||||
|
t.Fatalf("config %q: got %v, want %v", tc.value, got, tc.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -500,10 +500,7 @@ func (h *Handler) planSingleTunnelDeleteForwardMigration(forward *forwardRecord,
|
|||||||
targetPort = int(minPort.Int64)
|
targetPort = int(minPort.Int64)
|
||||||
}
|
}
|
||||||
if targetPort <= 0 {
|
if targetPort <= 0 {
|
||||||
targetPort = h.pickTunnelPort(targetTunnelID)
|
return tunnelForwardMigrationPlan{}, errors.New("转发入口端口无效,请先修复该规则")
|
||||||
}
|
|
||||||
if targetPort <= 0 {
|
|
||||||
targetPort = 10000
|
|
||||||
}
|
}
|
||||||
|
|
||||||
hasCustomInIP := false
|
hasCustomInIP := false
|
||||||
|
|||||||
@@ -0,0 +1,105 @@
|
|||||||
|
package handler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"go-backend/internal/http/response"
|
||||||
|
)
|
||||||
|
|
||||||
|
type redeployProgress struct {
|
||||||
|
Total int `json:"total"`
|
||||||
|
Completed int `json:"completed"`
|
||||||
|
Success int `json:"success"`
|
||||||
|
Failed int `json:"failed"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type redeployItemResult struct {
|
||||||
|
ID int64 `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Success bool `json:"success"`
|
||||||
|
Reason string `json:"reason,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// tunnelBatchRedeployStream reports completed tunnels, not estimated runtime.
|
||||||
|
// A disconnected caller may have an in-flight mutation whose result it did not
|
||||||
|
// receive. Finish that mutation, but never start another or automatically retry.
|
||||||
|
func (h *Handler) tunnelBatchRedeployStream(w http.ResponseWriter, r *http.Request) {
|
||||||
|
ids := idsFromBody(r, w)
|
||||||
|
if ids == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// idsFromBody sorts IDs; ignore duplicate selections so each tunnel runs once.
|
||||||
|
unique := ids[:0]
|
||||||
|
for _, id := range ids {
|
||||||
|
if len(unique) == 0 || unique[len(unique)-1] != id {
|
||||||
|
unique = append(unique, id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ids = unique
|
||||||
|
if len(ids) == 0 {
|
||||||
|
response.WriteJSON(w, response.ErrDefault("ids不能为空"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_, err := prepareDiagnosisStreamResponse(w)
|
||||||
|
if err != nil {
|
||||||
|
response.WriteJSON(w, response.ErrDefault(err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
encoder := json.NewEncoder(w)
|
||||||
|
controller := http.NewResponseController(w)
|
||||||
|
emit := func(kind string, data interface{}) bool {
|
||||||
|
if r.Context().Err() != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
// The server's WriteTimeout is an absolute request deadline. Renew only
|
||||||
|
// this stream's bounded write window as progress is emitted, so a long
|
||||||
|
// batch is not cut off despite making steady progress. Recorders and
|
||||||
|
// other non-network writers may not support deadlines.
|
||||||
|
if err := controller.SetWriteDeadline(time.Now().Add(2 * time.Minute)); err != nil && !errors.Is(err, http.ErrNotSupported) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
event := diagnosisStreamEvent{Type: kind, Data: data, TS: time.Now().UnixMilli()}
|
||||||
|
if err := encoder.Encode(event); err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
// Buffered writes can succeed while the actual socket flush fails.
|
||||||
|
// Do not start a mutation after its processing event failed to flush.
|
||||||
|
return controller.Flush() == nil
|
||||||
|
}
|
||||||
|
progress := redeployProgress{Total: len(ids)}
|
||||||
|
result := batchOperationResult{}
|
||||||
|
if !emit("start", progress) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for _, id := range ids {
|
||||||
|
if r.Context().Err() != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
name, _ := h.repo.GetTunnelName(id)
|
||||||
|
if !emit("processing", map[string]interface{}{"id": id, "name": name, "progress": progress}) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if r.Context().Err() != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
item := redeployItemResult{ID: id, Name: name}
|
||||||
|
if err := h.redeployTunnelConfiguration(id); err != nil {
|
||||||
|
result.FailCount++
|
||||||
|
result.Failures = appendBatchFailure(result.Failures, id, name, err)
|
||||||
|
item.Reason = result.Failures[len(result.Failures)-1].Reason
|
||||||
|
progress.Failed++
|
||||||
|
} else {
|
||||||
|
result.SuccessCount++
|
||||||
|
item.Success = true
|
||||||
|
progress.Success++
|
||||||
|
}
|
||||||
|
progress.Completed++
|
||||||
|
if !emit("item", map[string]interface{}{"result": item, "progress": progress}) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
emit("done", map[string]interface{}{"progress": progress, "result": result})
|
||||||
|
}
|
||||||
@@ -0,0 +1,248 @@
|
|||||||
|
package handler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"go-backend/internal/http/middleware"
|
||||||
|
runtimenft "go-backend/internal/runtime/nftables"
|
||||||
|
)
|
||||||
|
|
||||||
|
type blockingRedeployManager struct {
|
||||||
|
fakeNftablesManager
|
||||||
|
entered chan struct{}
|
||||||
|
release chan struct{}
|
||||||
|
once sync.Once
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *blockingRedeployManager) Reconcile(ctx context.Context, cfg runtimenft.SSHConfig, plan runtimenft.NodePlan) (runtimenft.ApplyResult, error) {
|
||||||
|
m.once.Do(func() { close(m.entered); <-m.release })
|
||||||
|
return m.fakeNftablesManager.Reconcile(ctx, cfg, plan)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTunnelRedeployStreamFlushesBeforeSlowMutationAndReportsMixedResults(t *testing.T) {
|
||||||
|
fixture := setupNftablesHandler(t)
|
||||||
|
h := fixture.handler
|
||||||
|
seedNftablesSSHConfig(t, h, fixture.nodeID)
|
||||||
|
id := seedTunnelForNftables(t, h, "stream-tunnel", fixture.nodeID)
|
||||||
|
manager := &blockingRedeployManager{entered: make(chan struct{}), release: make(chan struct{})}
|
||||||
|
h.nftablesManager = manager
|
||||||
|
var releaseOnce sync.Once
|
||||||
|
release := func() { releaseOnce.Do(func() { close(manager.release) }) }
|
||||||
|
defer release()
|
||||||
|
server := httptest.NewServer(http.HandlerFunc(h.tunnelBatchRedeployStream))
|
||||||
|
defer server.Close()
|
||||||
|
client := &http.Client{Timeout: 5 * time.Second}
|
||||||
|
response, err := client.Post(server.URL, "application/json", strings.NewReader(fmt.Sprintf(`{"ids":[%d,999999,%d]}`, id, id)))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer response.Body.Close()
|
||||||
|
decoder := json.NewDecoder(response.Body)
|
||||||
|
read := func(want string) map[string]interface{} {
|
||||||
|
t.Helper()
|
||||||
|
var event struct {
|
||||||
|
Type string
|
||||||
|
Data map[string]interface{}
|
||||||
|
}
|
||||||
|
if err := decoder.Decode(&event); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if event.Type != want {
|
||||||
|
t.Fatalf("expected %s, got %s", want, event.Type)
|
||||||
|
}
|
||||||
|
return event.Data
|
||||||
|
}
|
||||||
|
if start := read("start"); start["total"] != float64(2) {
|
||||||
|
t.Fatalf("duplicate IDs not deduplicated: %+v", start)
|
||||||
|
}
|
||||||
|
processing := read("processing")
|
||||||
|
if processing["name"] != "stream-tunnel" || processing["id"] != float64(id) {
|
||||||
|
t.Fatalf("wrong current tunnel: %+v", processing)
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case <-manager.entered:
|
||||||
|
case <-time.After(2 * time.Second):
|
||||||
|
t.Fatal("did not start mutation")
|
||||||
|
}
|
||||||
|
// Both events arrived over HTTP while the actual mutation is still blocked.
|
||||||
|
release()
|
||||||
|
item := read("item")
|
||||||
|
if item["result"].(map[string]interface{})["success"] != true || item["progress"].(map[string]interface{})["completed"] != float64(1) {
|
||||||
|
t.Fatalf("wrong success result: %+v", item)
|
||||||
|
}
|
||||||
|
read("processing")
|
||||||
|
item = read("item")
|
||||||
|
if item["result"].(map[string]interface{})["success"] != false || item["result"].(map[string]interface{})["reason"] == "" {
|
||||||
|
t.Fatalf("wrong failure result: %+v", item)
|
||||||
|
}
|
||||||
|
done := read("done")
|
||||||
|
progress := done["progress"].(map[string]interface{})
|
||||||
|
result := done["result"].(map[string]interface{})
|
||||||
|
if progress["completed"] != float64(2) || progress["success"] != float64(1) || progress["failed"] != float64(1) || result["successCount"] != float64(1) || result["failCount"] != float64(1) {
|
||||||
|
t.Fatalf("incorrect final result: %+v", done)
|
||||||
|
}
|
||||||
|
if manager.reconcileCount() != 1 {
|
||||||
|
t.Fatalf("expected one execution, got %d", manager.reconcileCount())
|
||||||
|
}
|
||||||
|
if err := decoder.Decode(new(interface{})); err != io.EOF {
|
||||||
|
t.Fatalf("expected EOF after done, got %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTunnelRedeployStreamStopsAfterDisconnect(t *testing.T) {
|
||||||
|
fixture := setupNftablesHandler(t)
|
||||||
|
h := fixture.handler
|
||||||
|
seedNftablesSSHConfig(t, h, fixture.nodeID)
|
||||||
|
first := seedTunnelForNftables(t, h, "first", fixture.nodeID)
|
||||||
|
second := seedTunnelForNftables(t, h, "second", fixture.nodeID)
|
||||||
|
manager := &blockingRedeployManager{entered: make(chan struct{}), release: make(chan struct{})}
|
||||||
|
h.nftablesManager = manager
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/tunnel/batch-redeploy/stream", strings.NewReader(fmt.Sprintf(`{"ids":[%d,%d]}`, first, second))).WithContext(ctx)
|
||||||
|
res := httptest.NewRecorder()
|
||||||
|
finished := make(chan struct{})
|
||||||
|
go func() { defer close(finished); h.tunnelBatchRedeployStream(res, req) }()
|
||||||
|
select {
|
||||||
|
case <-manager.entered:
|
||||||
|
case <-time.After(2 * time.Second):
|
||||||
|
close(manager.release)
|
||||||
|
t.Fatal("mutation did not start")
|
||||||
|
}
|
||||||
|
cancel()
|
||||||
|
close(manager.release)
|
||||||
|
select {
|
||||||
|
case <-finished:
|
||||||
|
case <-time.After(2 * time.Second):
|
||||||
|
t.Fatal("handler did not stop")
|
||||||
|
}
|
||||||
|
if manager.reconcileCount() != 1 {
|
||||||
|
t.Fatalf("started another mutation after disconnect: %d", manager.reconcileCount())
|
||||||
|
}
|
||||||
|
if strings.Contains(res.Body.String(), `"type":"done"`) {
|
||||||
|
t.Fatal("disconnected stream claimed completion")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type failedRedeployWriter struct{ *httptest.ResponseRecorder }
|
||||||
|
|
||||||
|
func (w failedRedeployWriter) Write([]byte) (int, error) { return 0, io.ErrClosedPipe }
|
||||||
|
|
||||||
|
func TestTunnelRedeployStreamDoesNotMutateWhenInitialWriteFails(t *testing.T) {
|
||||||
|
fixture := setupNftablesHandler(t)
|
||||||
|
h := fixture.handler
|
||||||
|
seedNftablesSSHConfig(t, h, fixture.nodeID)
|
||||||
|
id := seedTunnelForNftables(t, h, "stream-tunnel", fixture.nodeID)
|
||||||
|
manager := &fakeNftablesManager{}
|
||||||
|
h.nftablesManager = manager
|
||||||
|
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(fmt.Sprintf(`{"ids":[%d]}`, id)))
|
||||||
|
h.tunnelBatchRedeployStream(failedRedeployWriter{httptest.NewRecorder()}, req)
|
||||||
|
if manager.reconcileCount() != 0 {
|
||||||
|
t.Fatal("mutation ran despite failed progress stream")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type delayedRedeployManager struct{ fakeNftablesManager }
|
||||||
|
|
||||||
|
func (m *delayedRedeployManager) Reconcile(ctx context.Context, cfg runtimenft.SSHConfig, plan runtimenft.NodePlan) (runtimenft.ApplyResult, error) {
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
return m.fakeNftablesManager.Reconcile(ctx, cfg, plan)
|
||||||
|
}
|
||||||
|
|
||||||
|
type shortRedeployDeadlineWriter struct {
|
||||||
|
http.ResponseWriter
|
||||||
|
deadlineCalls *atomic.Int32
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w shortRedeployDeadlineWriter) Unwrap() http.ResponseWriter { return w.ResponseWriter }
|
||||||
|
func (w shortRedeployDeadlineWriter) SetWriteDeadline(deadline time.Time) error {
|
||||||
|
remaining := time.Until(deadline)
|
||||||
|
if remaining < time.Minute || remaining > 2*time.Minute {
|
||||||
|
return fmt.Errorf("expected bounded two-minute write window, got %v", remaining)
|
||||||
|
}
|
||||||
|
w.deadlineCalls.Add(1)
|
||||||
|
// Shorten the production idle window for a real, fast timeout regression.
|
||||||
|
return http.NewResponseController(w.ResponseWriter).SetWriteDeadline(time.Now().Add(200 * time.Millisecond))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTunnelRedeployStreamRenewsDeadlineThroughRequestLog(t *testing.T) {
|
||||||
|
fixture := setupNftablesHandler(t)
|
||||||
|
h := fixture.handler
|
||||||
|
seedNftablesSSHConfig(t, h, fixture.nodeID)
|
||||||
|
ids := make([]int64, 3)
|
||||||
|
for i := range ids {
|
||||||
|
ids[i] = seedTunnelForNftables(t, h, fmt.Sprintf("slow-%d", i), fixture.nodeID)
|
||||||
|
}
|
||||||
|
manager := &delayedRedeployManager{}
|
||||||
|
h.nftablesManager = manager
|
||||||
|
var deadlineCalls atomic.Int32
|
||||||
|
logged := middleware.RequestLog(http.HandlerFunc(h.tunnelBatchRedeployStream))
|
||||||
|
server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
logged.ServeHTTP(shortRedeployDeadlineWriter{w, &deadlineCalls}, r)
|
||||||
|
}))
|
||||||
|
server.Config.WriteTimeout = 50 * time.Millisecond
|
||||||
|
server.Start()
|
||||||
|
defer server.Close()
|
||||||
|
body, _ := json.Marshal(map[string]interface{}{"ids": ids})
|
||||||
|
client := &http.Client{Timeout: 5 * time.Second}
|
||||||
|
response, err := client.Post(server.URL, "application/json", strings.NewReader(string(body)))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer response.Body.Close()
|
||||||
|
payload, err := io.ReadAll(response.Body)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("stream timed out across batch: %v (body %s)", err, payload)
|
||||||
|
}
|
||||||
|
if !strings.Contains(string(payload), `"type":"done"`) {
|
||||||
|
t.Fatalf("batch did not finish: %s", payload)
|
||||||
|
}
|
||||||
|
if manager.reconcileCount() != 3 {
|
||||||
|
t.Fatalf("wrong execution count: %d", manager.reconcileCount())
|
||||||
|
}
|
||||||
|
if deadlineCalls.Load() != 8 {
|
||||||
|
t.Fatalf("each of 8 events must renew its deadline, got %d", deadlineCalls.Load())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type flushFailureRedeployWriter struct {
|
||||||
|
*httptest.ResponseRecorder
|
||||||
|
flushes int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *flushFailureRedeployWriter) FlushError() error {
|
||||||
|
w.flushes++
|
||||||
|
if w.flushes == 2 {
|
||||||
|
return io.ErrClosedPipe
|
||||||
|
}
|
||||||
|
w.ResponseRecorder.Flush()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTunnelRedeployStreamStopsBeforeMutationOnProcessingFlushError(t *testing.T) {
|
||||||
|
fixture := setupNftablesHandler(t)
|
||||||
|
h := fixture.handler
|
||||||
|
seedNftablesSSHConfig(t, h, fixture.nodeID)
|
||||||
|
id := seedTunnelForNftables(t, h, "flush-error", fixture.nodeID)
|
||||||
|
manager := &fakeNftablesManager{}
|
||||||
|
h.nftablesManager = manager
|
||||||
|
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(fmt.Sprintf(`{"ids":[%d]}`, id)))
|
||||||
|
writer := &flushFailureRedeployWriter{ResponseRecorder: httptest.NewRecorder()}
|
||||||
|
middleware.RequestLog(http.HandlerFunc(h.tunnelBatchRedeployStream)).ServeHTTP(writer, req)
|
||||||
|
if writer.flushes != 2 {
|
||||||
|
t.Fatalf("expected start and processing flush, got %d", writer.flushes)
|
||||||
|
}
|
||||||
|
if manager.reconcileCount() != 0 {
|
||||||
|
t.Fatal("mutation ran after failed processing flush")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -14,6 +14,11 @@ type statusWriter struct {
|
|||||||
status int
|
status int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Unwrap preserves ResponseController features such as per-response deadlines.
|
||||||
|
func (w *statusWriter) Unwrap() http.ResponseWriter {
|
||||||
|
return w.ResponseWriter
|
||||||
|
}
|
||||||
|
|
||||||
func (w *statusWriter) WriteHeader(code int) {
|
func (w *statusWriter) WriteHeader(code int) {
|
||||||
w.status = code
|
w.status = code
|
||||||
w.ResponseWriter.WriteHeader(code)
|
w.ResponseWriter.WriteHeader(code)
|
||||||
@@ -27,10 +32,12 @@ func (w *statusWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
|||||||
return hj.Hijack()
|
return hj.Hijack()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (w *statusWriter) FlushError() error {
|
||||||
|
return http.NewResponseController(w.ResponseWriter).Flush()
|
||||||
|
}
|
||||||
|
|
||||||
func (w *statusWriter) Flush() {
|
func (w *statusWriter) Flush() {
|
||||||
if f, ok := w.ResponseWriter.(http.Flusher); ok {
|
_ = w.FlushError()
|
||||||
f.Flush()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *statusWriter) ReadFrom(r io.Reader) (int64, error) {
|
func (w *statusWriter) ReadFrom(r io.Reader) (int64, error) {
|
||||||
|
|||||||
@@ -1609,7 +1609,15 @@ func (r *Repository) CreatePeerShareRuntime(item *model.PeerShareRuntime) error
|
|||||||
if item == nil {
|
if item == nil {
|
||||||
return errors.New("runtime item is nil")
|
return errors.New("runtime item is nil")
|
||||||
}
|
}
|
||||||
return r.db.Create(item).Error
|
return r.db.Transaction(func(tx *gorm.DB) error {
|
||||||
|
if err := lockPortAllocationTx(tx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := r.checkPeerRuntimePortTx(tx, item); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return tx.Create(item).Error
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Repository) UpdatePeerShareRuntime(item *model.PeerShareRuntime) error {
|
func (r *Repository) UpdatePeerShareRuntime(item *model.PeerShareRuntime) error {
|
||||||
@@ -1619,7 +1627,23 @@ func (r *Repository) UpdatePeerShareRuntime(item *model.PeerShareRuntime) error
|
|||||||
if item == nil {
|
if item == nil {
|
||||||
return errors.New("runtime item is nil")
|
return errors.New("runtime item is nil")
|
||||||
}
|
}
|
||||||
return r.db.Model(&model.PeerShareRuntime{}).Where("id = ?", item.ID).Updates(map[string]interface{}{
|
return r.db.Transaction(func(tx *gorm.DB) error {
|
||||||
|
if err := lockPortAllocationTx(tx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
var current model.PeerShareRuntime
|
||||||
|
if err := tx.Where("id = ?", item.ID).First(¤t).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if current.NodeID != item.NodeID {
|
||||||
|
return errors.New("runtime node cannot change")
|
||||||
|
}
|
||||||
|
if !(current.Status == 1 && current.Port == item.Port) {
|
||||||
|
if err := r.checkPeerRuntimePortTx(tx, item); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return tx.Model(&model.PeerShareRuntime{}).Where("id = ?", item.ID).Updates(map[string]interface{}{
|
||||||
"reservation_id": item.ReservationID,
|
"reservation_id": item.ReservationID,
|
||||||
"relay_auth": item.RelayAuth,
|
"relay_auth": item.RelayAuth,
|
||||||
"relay_auth_version": item.RelayAuthVersion,
|
"relay_auth_version": item.RelayAuthVersion,
|
||||||
@@ -1630,6 +1654,7 @@ func (r *Repository) UpdatePeerShareRuntime(item *model.PeerShareRuntime) error
|
|||||||
"applied": item.Applied, "status": item.Status,
|
"applied": item.Applied, "status": item.Status,
|
||||||
"updated_time": item.UpdatedTime,
|
"updated_time": item.UpdatedTime,
|
||||||
}).Error
|
}).Error
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Repository) MarkPeerShareRuntimeReleased(id int64, updatedTime int64) error {
|
func (r *Repository) MarkPeerShareRuntimeReleased(id int64, updatedTime int64) error {
|
||||||
@@ -2664,6 +2689,15 @@ func importTunnels(tx *gorm.DB, tunnels []model.TunnelBackup, now int64) (int, e
|
|||||||
return count, err
|
return count, err
|
||||||
}
|
}
|
||||||
for _, ct := range t.ChainTunnels {
|
for _, ct := range t.ChainTunnels {
|
||||||
|
if ct.Port > 0 {
|
||||||
|
used, err := (&Repository{}).usedPortSetTx(tx, ct.NodeID, 0, ct.TunnelID)
|
||||||
|
if err != nil {
|
||||||
|
return count, err
|
||||||
|
}
|
||||||
|
if used[ct.Port] {
|
||||||
|
return count, errors.New("导入链路端口已占用")
|
||||||
|
}
|
||||||
|
}
|
||||||
chainItem := model.ChainTunnel{
|
chainItem := model.ChainTunnel{
|
||||||
ID: ct.ID,
|
ID: ct.ID,
|
||||||
TunnelID: ct.TunnelID,
|
TunnelID: ct.TunnelID,
|
||||||
@@ -2735,6 +2769,13 @@ func importForwards(tx *gorm.DB, forwards []model.ForwardBackup, now int64) (int
|
|||||||
return count, err
|
return count, err
|
||||||
}
|
}
|
||||||
for _, fp := range *f.ForwardPorts {
|
for _, fp := range *f.ForwardPorts {
|
||||||
|
used, err := (&Repository{}).usedPortSetTx(tx, fp.NodeID, f.ID, 0)
|
||||||
|
if err != nil {
|
||||||
|
return count, err
|
||||||
|
}
|
||||||
|
if used[fp.Port] {
|
||||||
|
return count, errors.New("导入转发端口已占用")
|
||||||
|
}
|
||||||
if err := tx.Create(&model.ForwardPort{ForwardID: f.ID, NodeID: fp.NodeID, Port: fp.Port}).Error; err != nil {
|
if err := tx.Create(&model.ForwardPort{ForwardID: f.ID, NodeID: fp.NodeID, Port: fp.Port}).Error; err != nil {
|
||||||
return count, err
|
return count, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,12 @@ var errBackupImportBusy = errors.New("database is busy; retry the backup import"
|
|||||||
func backupImportTransaction(db *gorm.DB, importRows func(*gorm.DB) error) error {
|
func backupImportTransaction(db *gorm.DB, importRows func(*gorm.DB) error) error {
|
||||||
var err error
|
var err error
|
||||||
for attempt := 0; attempt < 10; attempt++ {
|
for attempt := 0; attempt < 10; attempt++ {
|
||||||
err = db.Transaction(importRows)
|
err = db.Transaction(func(tx *gorm.DB) error {
|
||||||
|
if err := lockPortAllocationTx(tx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return importRows(tx)
|
||||||
|
})
|
||||||
if !errors.Is(err, errBackupImportBusy) {
|
if !errors.Is(err, errBackupImportBusy) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -146,19 +146,13 @@ func (r *Repository) CreateFederationTunnel(name string, tunnelType int, protoco
|
|||||||
InIP: sql.NullString{String: "", Valid: false},
|
InIP: sql.NullString{String: "", Valid: false},
|
||||||
}
|
}
|
||||||
err := r.db.Transaction(func(tx *gorm.DB) error {
|
err := r.db.Transaction(func(tx *gorm.DB) error {
|
||||||
|
if err := lockPortAllocationTx(tx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
if err := tx.Create(&tunnel).Error; err != nil {
|
if err := tx.Create(&tunnel).Error; err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
ct := model.ChainTunnel{
|
if err := r.CreateChainTunnelTx(tx, tunnel.ID, "1", nodeID, sql.NullInt64{Int64: int64(remotePort), Valid: true}, "fifo", 0, protocol, ""); err != nil {
|
||||||
TunnelID: tunnel.ID,
|
|
||||||
ChainType: "1",
|
|
||||||
NodeID: nodeID,
|
|
||||||
Port: sql.NullInt64{Int64: int64(remotePort), Valid: true},
|
|
||||||
Strategy: sql.NullString{String: "fifo", Valid: true},
|
|
||||||
Inx: sql.NullInt64{Int64: 0, Valid: true},
|
|
||||||
Protocol: sql.NullString{String: protocol, Valid: true},
|
|
||||||
}
|
|
||||||
if err := tx.Create(&ct).Error; err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -160,6 +160,9 @@ func TestForwardRepositoryPersistsPerIPLimits(t *testing.T) {
|
|||||||
defer r.Close()
|
defer r.Close()
|
||||||
|
|
||||||
now := time.Now().UnixMilli()
|
now := time.Now().UnixMilli()
|
||||||
|
if err := r.db.Create(&model.Node{ID: 3, Port: "1000-65535"}).Error; err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
forwardID, err := r.CreateForwardTx(1, "admin", "per-ip-forward", 2, "1.1.1.1:443", "fifo", now, 1, []int64{3}, 24000, "", nil, 0, 5, int64(21), 0, 0, 0)
|
forwardID, err := r.CreateForwardTx(1, "admin", "per-ip-forward", 2, "1.1.1.1:443", "fifo", now, 1, []int64{3}, 24000, "", nil, 0, 5, int64(21), 0, 0, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("CreateForwardTx: %v", err)
|
t.Fatalf("CreateForwardTx: %v", err)
|
||||||
@@ -224,6 +227,9 @@ func TestForwardRepositoryPersistsProxyProtocolReceiveAndSend(t *testing.T) {
|
|||||||
defer r.Close()
|
defer r.Close()
|
||||||
|
|
||||||
now := time.Now().UnixMilli()
|
now := time.Now().UnixMilli()
|
||||||
|
if err := r.db.Create(&model.Node{ID: 3, Port: "1000-65535"}).Error; err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
forwardID, err := r.CreateForwardTx(1, "admin", "proxy-protocol-forward", 2, "1.1.1.1:443", "fifo", now, 1, []int64{3}, 24000, "", nil, 0, 0, nil, 0, 1, 2)
|
forwardID, err := r.CreateForwardTx(1, "admin", "proxy-protocol-forward", 2, "1.1.1.1:443", "fifo", now, 1, []int64{3}, 24000, "", nil, 0, 0, nil, 0, 1, 2)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("CreateForwardTx: %v", err)
|
t.Fatalf("CreateForwardTx: %v", err)
|
||||||
@@ -301,7 +307,10 @@ func TestRollbackForwardFieldsRestoresPerIPLimits(t *testing.T) {
|
|||||||
defer r.Close()
|
defer r.Close()
|
||||||
|
|
||||||
now := time.Now().UnixMilli()
|
now := time.Now().UnixMilli()
|
||||||
forwardID, err := r.CreateForwardTx(1, "admin", "rollback-per-ip-forward", 2, "1.1.1.1:443", "fifo", now, 1, nil, 0, "", nil, 7, 5, int64(21), 2, 0, 2)
|
if err := r.db.Create(&model.Node{ID: 3, Port: "1000-65535"}).Error; err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
forwardID, err := r.CreateForwardTx(1, "admin", "rollback-per-ip-forward", 2, "1.1.1.1:443", "fifo", now, 1, []int64{3}, 24000, "", nil, 7, 5, int64(21), 2, 0, 2)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("CreateForwardTx: %v", err)
|
t.Fatalf("CreateForwardTx: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -448,7 +448,14 @@ func (r *Repository) BeginTx() *gorm.DB {
|
|||||||
if r == nil || r.db == nil {
|
if r == nil || r.db == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return r.db.Begin()
|
tx := r.db.Begin()
|
||||||
|
if tx.Error == nil {
|
||||||
|
if err := lockPortAllocationTx(tx); err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
tx.AddError(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return tx
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Repository) UpdateTunnelOrder(tunnelID int64, inx int, now int64) {
|
func (r *Repository) UpdateTunnelOrder(tunnelID int64, inx int, now int64) {
|
||||||
@@ -492,6 +499,18 @@ func (r *Repository) CreateChainTunnelTx(tx *gorm.DB, tunnelID int64, chainType
|
|||||||
if tx == nil {
|
if tx == nil {
|
||||||
return errors.New("database unavailable")
|
return errors.New("database unavailable")
|
||||||
}
|
}
|
||||||
|
if port.Valid && port.Int64 > 0 {
|
||||||
|
if err := r.LockPortNodesTx(tx, []int64{nodeID}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
used, err := r.usedPortSetTx(tx, nodeID, 0, tunnelID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if used[int(port.Int64)] {
|
||||||
|
return errors.New("端口已占用")
|
||||||
|
}
|
||||||
|
}
|
||||||
ct := model.ChainTunnel{
|
ct := model.ChainTunnel{
|
||||||
TunnelID: tunnelID,
|
TunnelID: tunnelID,
|
||||||
ChainType: chainType,
|
ChainType: chainType,
|
||||||
@@ -538,6 +557,9 @@ func (r *Repository) pickNodePortTx(tx *gorm.DB, nodeID int64, allocated map[int
|
|||||||
if nodeID <= 0 {
|
if nodeID <= 0 {
|
||||||
return 0, errors.New("节点不存在")
|
return 0, errors.New("节点不存在")
|
||||||
}
|
}
|
||||||
|
if err := r.LockPortNodesTx(tx, []int64{nodeID}); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
if port, ok := allocated[nodeID]; ok && port > 0 {
|
if port, ok := allocated[nodeID]; ok && port > 0 {
|
||||||
return port, nil
|
return port, nil
|
||||||
}
|
}
|
||||||
@@ -556,47 +578,16 @@ func (r *Repository) pickNodePortTx(tx *gorm.DB, nodeID int64, allocated map[int
|
|||||||
return 0, errors.New("节点端口已满,无可用端口")
|
return 0, errors.New("节点端口已满,无可用端口")
|
||||||
}
|
}
|
||||||
|
|
||||||
used := make(map[int]struct{})
|
used, err := r.usedPortSetTx(tx, nodeID, 0, excludeTunnelID)
|
||||||
|
|
||||||
chainQuery := tx.Model(&model.ChainTunnel{}).Where("node_id = ? AND port > 0", nodeID)
|
|
||||||
if excludeTunnelID > 0 {
|
|
||||||
chainQuery = chainQuery.Where("tunnel_id != ?", excludeTunnelID)
|
|
||||||
}
|
|
||||||
var chainPorts []int
|
|
||||||
if err := chainQuery.Pluck("port", &chainPorts).Error; err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
for _, p := range chainPorts {
|
|
||||||
if p > 0 {
|
|
||||||
used[p] = struct{}{}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var forwardPorts []int
|
|
||||||
if err := tx.Model(&model.ForwardPort{}).
|
|
||||||
Where("node_id = ? AND port > 0", nodeID).
|
|
||||||
Pluck("port", &forwardPorts).Error; err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
for _, p := range forwardPorts {
|
|
||||||
if p > 0 {
|
|
||||||
used[p] = struct{}{}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pendingPorts, err := r.PendingTunnelCleanupPortsTx(tx, nodeID, excludeTunnelID)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
for _, port := range pendingPorts {
|
|
||||||
used[port] = struct{}{}
|
|
||||||
}
|
|
||||||
var available []int
|
var available []int
|
||||||
for _, candidate := range candidates {
|
for _, candidate := range candidates {
|
||||||
if candidate <= 0 {
|
if candidate <= 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if _, ok := used[candidate]; ok {
|
if used[candidate] {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
available = append(available, candidate)
|
available = append(available, candidate)
|
||||||
@@ -836,11 +827,64 @@ func (r *Repository) ReplaceForwardPorts(forwardID int64, entries []struct {
|
|||||||
NodeID int64
|
NodeID int64
|
||||||
Port int
|
Port int
|
||||||
InIP string
|
InIP string
|
||||||
}) error {
|
}, sequential ...bool) error {
|
||||||
|
return r.replaceForwardPorts(forwardID, entries, len(sequential) > 0 && sequential[0], false)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReplaceForwardPortsIndividually preserves independent ports when adding entry nodes.
|
||||||
|
func (r *Repository) ReplaceForwardPortsIndividually(forwardID int64, entries []struct {
|
||||||
|
NodeID int64
|
||||||
|
Port int
|
||||||
|
InIP string
|
||||||
|
}, sequential bool) error {
|
||||||
|
return r.replaceForwardPorts(forwardID, entries, sequential, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repository) replaceForwardPorts(forwardID int64, entries []struct {
|
||||||
|
NodeID int64
|
||||||
|
Port int
|
||||||
|
InIP string
|
||||||
|
}, sequential, individually bool) error {
|
||||||
if r == nil || r.db == nil {
|
if r == nil || r.db == nil {
|
||||||
return errors.New("repository not initialized")
|
return errors.New("repository not initialized")
|
||||||
}
|
}
|
||||||
return r.db.Transaction(func(tx *gorm.DB) error {
|
return r.db.Transaction(func(tx *gorm.DB) error {
|
||||||
|
ids := make([]int64, 0, len(entries))
|
||||||
|
autoIDs := []int64{}
|
||||||
|
for _, e := range entries {
|
||||||
|
ids = append(ids, e.NodeID)
|
||||||
|
if e.Port <= 0 {
|
||||||
|
autoIDs = append(autoIDs, e.NodeID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := r.LockPortNodesTx(tx, ids); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
autoPort := 0
|
||||||
|
if len(autoIDs) > 0 && !individually {
|
||||||
|
var err error
|
||||||
|
autoPort, err = r.selectForwardPortTx(tx, autoIDs, 0, forwardID, sequential)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
entries = append(entries[:0:0], entries...)
|
||||||
|
for i := range entries {
|
||||||
|
if entries[i].Port <= 0 {
|
||||||
|
if individually {
|
||||||
|
var err error
|
||||||
|
entries[i].Port, err = r.selectForwardPortTx(tx, []int64{entries[i].NodeID}, 0, forwardID, sequential)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
entries[i].Port = autoPort
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if _, err := r.selectForwardPortTx(tx, []int64{entries[i].NodeID}, entries[i].Port, forwardID, false); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
if err := tx.Where("forward_id = ?", forwardID).Delete(&model.ForwardPort{}).Error; err != nil {
|
if err := tx.Where("forward_id = ?", forwardID).Delete(&model.ForwardPort{}).Error; err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -1362,13 +1406,21 @@ func (r *Repository) EnsureUserTunnelGrant(userID, tunnelID int64) (int64, bool,
|
|||||||
return ut.ID, true, nil
|
return ut.ID, true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Repository) CreateForwardTx(userID int64, userName, name string, tunnelID int64, remoteAddr, strategy string, now int64, inx int, entryNodeIDs []int64, port int, inIp string, speedID interface{}, maxConn int, ipMaxConn int, ipSpeedID interface{}, proxyProtocol int, proxyProtocolReceive int, proxyProtocolSend int) (int64, error) {
|
func (r *Repository) CreateForwardTx(userID int64, userName, name string, tunnelID int64, remoteAddr, strategy string, now int64, inx int, entryNodeIDs []int64, port int, inIp string, speedID interface{}, maxConn int, ipMaxConn int, ipSpeedID interface{}, proxyProtocol int, proxyProtocolReceive int, proxyProtocolSend int, sequential ...bool) (int64, error) {
|
||||||
if r == nil || r.db == nil {
|
if r == nil || r.db == nil {
|
||||||
return 0, errors.New("repository not initialized")
|
return 0, errors.New("repository not initialized")
|
||||||
}
|
}
|
||||||
_, proxyProtocolSend = normalizeForwardProxyProtocol(proxyProtocol, proxyProtocolReceive, proxyProtocolSend)
|
_, proxyProtocolSend = normalizeForwardProxyProtocol(proxyProtocol, proxyProtocolReceive, proxyProtocolSend)
|
||||||
var forwardID int64
|
var forwardID int64
|
||||||
err := r.db.Transaction(func(tx *gorm.DB) error {
|
err := r.db.Transaction(func(tx *gorm.DB) error {
|
||||||
|
if err := r.LockPortNodesTx(tx, entryNodeIDs); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
selectedPort, err := r.selectForwardPortTx(tx, entryNodeIDs, port, 0, len(sequential) > 0 && sequential[0])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
port = selectedPort
|
||||||
fwd := model.Forward{
|
fwd := model.Forward{
|
||||||
UserID: userID,
|
UserID: userID,
|
||||||
UserName: userName,
|
UserName: userName,
|
||||||
|
|||||||
@@ -0,0 +1,184 @@
|
|||||||
|
package repo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"go-backend/internal/store/model"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
"math/big"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// lockPortAllocationTx serializes reservation mutations across processes sharing a database.
|
||||||
|
// PostgreSQL uses a transaction-scoped advisory lock; SQLite acquires its writer
|
||||||
|
// lock without changing any row, including when the node table is empty.
|
||||||
|
// All panel instances sharing this database must use this protocol; mixed old
|
||||||
|
// binaries and out-of-band SQL writes are not protected by advisory locks.
|
||||||
|
func lockPortAllocationTx(tx *gorm.DB) error {
|
||||||
|
if tx.Dialector.Name() == "postgres" {
|
||||||
|
return tx.Exec("SELECT pg_advisory_xact_lock(?)", int64(0x464c5658504f5254)).Error
|
||||||
|
}
|
||||||
|
if tx.Dialector.Name() == "sqlite" {
|
||||||
|
return tx.Model(&model.Node{}).Where("1 = 0").UpdateColumn("id", gorm.Expr("id")).Error
|
||||||
|
}
|
||||||
|
return errors.New("unsupported database for atomic port allocation")
|
||||||
|
}
|
||||||
|
|
||||||
|
// LockPortNodesTx must precede occupancy reads and reservation writes.
|
||||||
|
func (r *Repository) LockPortNodesTx(tx *gorm.DB, ids []int64) error {
|
||||||
|
if err := lockPortAllocationTx(tx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, id := range ids {
|
||||||
|
if id <= 0 {
|
||||||
|
return errors.New("节点不存在")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repository) usedPortSetTx(tx *gorm.DB, nodeID, excludeForwardID, excludeTunnelID int64, excludeRuntimeIDs ...int64) (map[int]bool, error) {
|
||||||
|
used := map[int]bool{}
|
||||||
|
var ports []int
|
||||||
|
if err := tx.Model(&model.ForwardPort{}).Where("node_id = ? AND forward_id <> ?", nodeID, excludeForwardID).Pluck("port", &ports).Error; err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, p := range ports {
|
||||||
|
used[p] = true
|
||||||
|
}
|
||||||
|
query := tx.Model(&model.ChainTunnel{}).Where("node_id = ? AND port > 0", nodeID)
|
||||||
|
if excludeTunnelID > 0 {
|
||||||
|
query = query.Where("tunnel_id <> ?", excludeTunnelID)
|
||||||
|
}
|
||||||
|
if err := query.Pluck("port", &ports).Error; err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, p := range ports {
|
||||||
|
used[p] = true
|
||||||
|
}
|
||||||
|
runtimeQuery := tx.Model(&model.PeerShareRuntime{}).Where("node_id = ? AND (status = 1 OR release_pending <> 0) AND port > 0", nodeID)
|
||||||
|
if len(excludeRuntimeIDs) > 0 {
|
||||||
|
runtimeQuery = runtimeQuery.Where("id <> ?", excludeRuntimeIDs[0])
|
||||||
|
}
|
||||||
|
if err := runtimeQuery.Pluck("port", &ports).Error; err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, p := range ports {
|
||||||
|
used[p] = true
|
||||||
|
}
|
||||||
|
pending, err := r.PendingTunnelCleanupPortsTx(tx, nodeID, excludeTunnelID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, p := range pending {
|
||||||
|
used[p] = true
|
||||||
|
}
|
||||||
|
return used, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Selection and insert must be in the same transaction after locking all nodes.
|
||||||
|
func (r *Repository) selectForwardPortTx(tx *gorm.DB, ids []int64, requested int, excludeForwardID int64, sequential bool) (int, error) {
|
||||||
|
if len(ids) == 0 {
|
||||||
|
return 0, errors.New("隧道没有可用入口节点")
|
||||||
|
}
|
||||||
|
var common []int
|
||||||
|
for i, id := range ids {
|
||||||
|
var node model.Node
|
||||||
|
if err := tx.Where("id = ?", id).First(&node).Error; err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
spec := strings.TrimSpace(node.Port)
|
||||||
|
if spec == "" {
|
||||||
|
spec = "1000-65535"
|
||||||
|
if requested > 0 {
|
||||||
|
spec = "1-65535"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
candidates := parsePortRangeSpec(spec)
|
||||||
|
used, err := r.usedPortSetTx(tx, id, excludeForwardID, 0)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
var remote struct {
|
||||||
|
Start int `json:"portRangeStart"`
|
||||||
|
End int `json:"portRangeEnd"`
|
||||||
|
}
|
||||||
|
if node.IsRemote == 1 {
|
||||||
|
_ = json.Unmarshal([]byte(node.RemoteConfig.String), &remote)
|
||||||
|
}
|
||||||
|
available := map[int]bool{}
|
||||||
|
for _, p := range candidates {
|
||||||
|
if p <= 65535 && !used[p] && (remote.Start <= 0 || remote.End <= 0 || (p >= remote.Start && p <= remote.End)) {
|
||||||
|
available[p] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if requested > 0 {
|
||||||
|
if !available[requested] {
|
||||||
|
return 0, errors.New("端口已占用或不在节点允许范围内")
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if i == 0 {
|
||||||
|
for _, p := range candidates {
|
||||||
|
if available[p] {
|
||||||
|
common = append(common, p)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
filtered := common[:0]
|
||||||
|
for _, p := range common {
|
||||||
|
if available[p] {
|
||||||
|
filtered = append(filtered, p)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
common = filtered
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if requested > 0 {
|
||||||
|
return requested, nil
|
||||||
|
}
|
||||||
|
if len(common) == 0 {
|
||||||
|
return 0, errors.New("节点端口已满,无可用端口")
|
||||||
|
}
|
||||||
|
if sequential {
|
||||||
|
return common[0], nil
|
||||||
|
}
|
||||||
|
idx, err := rand.Int(rand.Reader, big.NewInt(int64(len(common))))
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return common[idx.Int64()], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// checkPeerRuntimePortTx permits only a verified federation-forward attachment
|
||||||
|
// to share its owning chain reservation. Other new reservations must be free.
|
||||||
|
func (r *Repository) checkPeerRuntimePortTx(tx *gorm.DB, item *model.PeerShareRuntime) error {
|
||||||
|
if item.Port <= 0 || (item.Status != 1 && item.ReleasePending == 0) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
var ownedTunnel int64
|
||||||
|
if item.Role == "forward" {
|
||||||
|
var shareID, tunnelID int64
|
||||||
|
var port int
|
||||||
|
_, err := fmt.Sscanf(item.ResourceKey, "federation-forward-%d-%d-%d", &shareID, &tunnelID, &port)
|
||||||
|
if err == nil && shareID == item.ShareID && port == item.Port && item.ResourceKey == fmt.Sprintf("federation-forward-%d-%d-%d", shareID, tunnelID, port) {
|
||||||
|
var count int64
|
||||||
|
if err := tx.Model(&model.ChainTunnel{}).Where("tunnel_id = ? AND node_id = ? AND port = ?", tunnelID, item.NodeID, item.Port).Count(&count).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if count > 0 {
|
||||||
|
ownedTunnel = tunnelID
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
used, err := r.usedPortSetTx(tx, item.NodeID, 0, ownedTunnel, item.ID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if used[item.Port] {
|
||||||
|
return errors.New("端口已占用")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,326 @@
|
|||||||
|
package repo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"go-backend/internal/store/model"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
func portTestCreate(r *Repository, port int, nodes ...int64) (int64, error) {
|
||||||
|
return r.CreateForwardTx(1, "admin", "concurrent", 1, "127.0.0.1:80", "fifo", time.Now().UnixMilli(), 0, nodes, port, "", nil, 0, 0, nil, 0, 0, 0, true)
|
||||||
|
}
|
||||||
|
func TestAtomicPortAllocationAcrossRepositories(t *testing.T) {
|
||||||
|
path := filepath.Join(t.TempDir(), "ports.db")
|
||||||
|
a, err := Open(path)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer a.Close()
|
||||||
|
b, err := Open(path)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer b.Close()
|
||||||
|
runConcurrentPortAllocation(t, a, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func runConcurrentPortAllocation(t *testing.T, a, b *Repository) {
|
||||||
|
for _, id := range []int64{1, 2} {
|
||||||
|
if err := a.db.Create(&model.Node{ID: id, Name: fmt.Sprint(id), Port: "20000-20031"}).Error; err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const count = 16
|
||||||
|
start := make(chan struct{})
|
||||||
|
errs := make(chan error, count)
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
for i := 0; i < count; i++ {
|
||||||
|
wg.Add(1)
|
||||||
|
go func(i int) {
|
||||||
|
defer wg.Done()
|
||||||
|
<-start
|
||||||
|
r := a
|
||||||
|
if i%2 == 1 {
|
||||||
|
r = b
|
||||||
|
}
|
||||||
|
_, err := portTestCreate(r, 0, 1, 2)
|
||||||
|
errs <- err
|
||||||
|
}(i)
|
||||||
|
}
|
||||||
|
close(start)
|
||||||
|
wg.Wait()
|
||||||
|
close(errs)
|
||||||
|
for err := range errs {
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var rows []model.ForwardPort
|
||||||
|
if err := a.db.Order("port").Find(&rows).Error; err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if len(rows) != count*2 {
|
||||||
|
t.Fatalf("got %d reservations", len(rows))
|
||||||
|
}
|
||||||
|
seen := map[string]bool{}
|
||||||
|
for _, row := range rows {
|
||||||
|
k := fmt.Sprintf("%d:%d", row.NodeID, row.Port)
|
||||||
|
if seen[k] {
|
||||||
|
t.Fatalf("duplicate %s", k)
|
||||||
|
}
|
||||||
|
seen[k] = true
|
||||||
|
}
|
||||||
|
explicitStart := make(chan struct{})
|
||||||
|
explicitResults := make(chan error, 2)
|
||||||
|
for _, r := range []*Repository{a, b} {
|
||||||
|
go func(r *Repository) { <-explicitStart; _, err := portTestCreate(r, 20031, 1, 2); explicitResults <- err }(r)
|
||||||
|
}
|
||||||
|
close(explicitStart)
|
||||||
|
success := 0
|
||||||
|
for i := 0; i < 2; i++ {
|
||||||
|
if <-explicitResults == nil {
|
||||||
|
success++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if success != 1 {
|
||||||
|
t.Fatalf("explicit port race: %d successes, want exactly 1", success)
|
||||||
|
}
|
||||||
|
for p := 20000; p < 20000+count; p++ {
|
||||||
|
for _, n := range []int{1, 2} {
|
||||||
|
if !seen[fmt.Sprintf("%d:%d", n, p)] {
|
||||||
|
t.Fatalf("gap at %d:%d", n, p)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func TestAtomicPortAllocationRejectsConflictsAndExhaustion(t *testing.T) {
|
||||||
|
r, err := Open(":memory:")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer r.Close()
|
||||||
|
if err := r.db.Create(&model.Node{ID: 1, Port: "21000-21001"}).Error; err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
id, err := portTestCreate(r, 21000, 1)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if _, err := portTestCreate(r, 21000, 1); err == nil {
|
||||||
|
t.Fatal("explicit collision accepted")
|
||||||
|
}
|
||||||
|
if err := r.db.Transaction(func(tx *gorm.DB) error {
|
||||||
|
return r.CreateChainTunnelTx(tx, 2, "2", 1, sql.NullInt64{Int64: 21000, Valid: true}, "", 0, "", "")
|
||||||
|
}); err == nil {
|
||||||
|
t.Fatal("chain collision accepted")
|
||||||
|
}
|
||||||
|
if _, err := portTestCreate(r, 0, 1); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if _, err := portTestCreate(r, 0, 1); err == nil {
|
||||||
|
t.Fatal("exhausted range accepted")
|
||||||
|
}
|
||||||
|
entries := []struct {
|
||||||
|
NodeID int64
|
||||||
|
Port int
|
||||||
|
InIP string
|
||||||
|
}{{1, 21001, ""}}
|
||||||
|
if err := r.ReplaceForwardPorts(id, entries, true); err == nil {
|
||||||
|
t.Fatal("replacement collision accepted")
|
||||||
|
}
|
||||||
|
var old model.ForwardPort
|
||||||
|
if err := r.db.Where("forward_id = ?", id).First(&old).Error; err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if old.Port != 21000 {
|
||||||
|
t.Fatal("failed replacement lost original reservation")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAtomicPortAllocationPostgresAcrossRepositories(t *testing.T) {
|
||||||
|
dsn := os.Getenv("FLVX_POSTGRES_TEST_DSN")
|
||||||
|
if dsn == "" {
|
||||||
|
t.Skip("set FLVX_POSTGRES_TEST_DSN for isolated PostgreSQL allocation test")
|
||||||
|
}
|
||||||
|
admin, err := sql.Open("pgx", dsn)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
schema := fmt.Sprintf("port_allocation_%d", time.Now().UnixNano())
|
||||||
|
if _, err := admin.Exec("CREATE SCHEMA " + schema); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Cleanup(func() { admin.Exec("DROP SCHEMA " + schema + " CASCADE"); admin.Close() })
|
||||||
|
scoped := dsn + " search_path=" + schema
|
||||||
|
if strings.Contains(dsn, "://") {
|
||||||
|
u, err := url.Parse(dsn)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
q := u.Query()
|
||||||
|
q.Set("search_path", schema)
|
||||||
|
u.RawQuery = q.Encode()
|
||||||
|
scoped = u.String()
|
||||||
|
}
|
||||||
|
a, err := OpenPostgres(scoped)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer a.Close()
|
||||||
|
b, err := OpenPostgres(scoped)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer b.Close()
|
||||||
|
runConcurrentPortAllocation(t, a, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAtomicPortAllocationRespectsRangesAndPendingCleanup(t *testing.T) {
|
||||||
|
r, err := Open(":memory:")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer r.Close()
|
||||||
|
nodes := []model.Node{{ID: 1, Port: "23004,23000-23002", IsRemote: 1, RemoteConfig: sql.NullString{String: `{"portRangeStart":23001,"portRangeEnd":23004}`, Valid: true}}, {ID: 2, Port: "23001-23004"}}
|
||||||
|
if err := r.db.Create(&nodes).Error; err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := r.db.Create(&model.TunnelRuntimeCleanup{TunnelID: 2, NodeID: 2, Kind: "service", Port: 23001}).Error; err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
id, err := portTestCreate(r, 0, 1, 2)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if got := r.GetMinForwardPort(id).Int64; got != 23002 {
|
||||||
|
t.Fatalf("got %d, want23002", got)
|
||||||
|
}
|
||||||
|
id2, err := portTestCreate(r, 0, 1, 2)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if got := r.GetMinForwardPort(id2).Int64; got != 23004 {
|
||||||
|
t.Fatalf("got %d, want23004", got)
|
||||||
|
}
|
||||||
|
if err := r.DeleteForwardCascade(id); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
id3, err := portTestCreate(r, 0, 1, 2)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if got := r.GetMinForwardPort(id3).Int64; got != 23002 {
|
||||||
|
t.Fatalf("released gap not reused: %d", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAtomicPortAllocationIndependentNewEntries(t *testing.T) {
|
||||||
|
r, err := Open(":memory:")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer r.Close()
|
||||||
|
if err := r.db.Create(&[]model.Node{{ID: 1, Port: "24000-24001"}, {ID: 2, Port: "25000-25001"}}).Error; err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
entries := []struct {
|
||||||
|
NodeID int64
|
||||||
|
Port int
|
||||||
|
InIP string
|
||||||
|
}{{1, 0, ""}, {2, 0, ""}}
|
||||||
|
if err := r.ReplaceForwardPortsIndividually(1, entries, true); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
var rows []model.ForwardPort
|
||||||
|
if err := r.db.Order("node_id").Find(&rows).Error; err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if len(rows) != 2 || rows[0].Port != 24000 || rows[1].Port != 25000 {
|
||||||
|
t.Fatalf("unexpected independent allocation: %+v", rows)
|
||||||
|
}
|
||||||
|
if _, err := r.CreateFederationTunnel("conflict", 1, "tcp", 0, 1, 24000); err == nil {
|
||||||
|
t.Fatal("federation collision accepted")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAtomicPortPeerRuntimeConflicts(t *testing.T) {
|
||||||
|
path := filepath.Join(t.TempDir(), "mixed.db")
|
||||||
|
a, err := Open(path)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer a.Close()
|
||||||
|
b, err := Open(path)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer b.Close()
|
||||||
|
if err := a.db.Create(&model.Node{ID: 1, Port: "25000-25010"}).Error; err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
runtime := func(key string, port int) *model.PeerShareRuntime {
|
||||||
|
return &model.PeerShareRuntime{ShareID: 1, NodeID: 1, Port: port, ResourceKey: key, ReservationID: key, Status: 1}
|
||||||
|
}
|
||||||
|
start := make(chan struct{})
|
||||||
|
results := make(chan error, 2)
|
||||||
|
go func() { <-start; _, err := portTestCreate(a, 25000, 1); results <- err }()
|
||||||
|
go func() { <-start; results <- b.CreatePeerShareRuntime(runtime("peer", 25000)) }()
|
||||||
|
close(start)
|
||||||
|
success := 0
|
||||||
|
for i := 0; i < 2; i++ {
|
||||||
|
if <-results == nil {
|
||||||
|
success++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if success != 1 {
|
||||||
|
t.Fatalf("mixed reservation race succeeded %d times", success)
|
||||||
|
}
|
||||||
|
first := runtime("first", 25001)
|
||||||
|
if err := a.CreatePeerShareRuntime(first); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := b.CreatePeerShareRuntime(runtime("second", 25001)); err == nil {
|
||||||
|
t.Fatal("duplicate peer port accepted")
|
||||||
|
}
|
||||||
|
first.Role = "middle"
|
||||||
|
first.Applied = 1
|
||||||
|
if err := a.UpdatePeerShareRuntime(first); err != nil {
|
||||||
|
t.Fatalf("own metadata update: %v", err)
|
||||||
|
}
|
||||||
|
owned := runtime("federation-forward-1-22-25002", 25002)
|
||||||
|
owned.Role = "forward"
|
||||||
|
if err := a.db.Transaction(func(tx *gorm.DB) error {
|
||||||
|
return a.CreateChainTunnelTx(tx, 22, "1", 1, sql.NullInt64{Int64: 25002, Valid: true}, "fifo", 0, "tcp", "")
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := a.CreatePeerShareRuntime(runtime("federation-forward-1-22-25002", 25002)); err == nil {
|
||||||
|
t.Fatal("generic spoof of ownership accepted")
|
||||||
|
}
|
||||||
|
if err := a.CreatePeerShareRuntime(owned); err != nil {
|
||||||
|
t.Fatalf("valid attachment rejected: %v", err)
|
||||||
|
}
|
||||||
|
other := runtime("other-forward", 25002)
|
||||||
|
other.Role = "forward"
|
||||||
|
if err := b.CreatePeerShareRuntime(other); err == nil {
|
||||||
|
t.Fatal("unowned forward attachment accepted")
|
||||||
|
}
|
||||||
|
pending := runtime("pending", 25003)
|
||||||
|
pending.Status = 0
|
||||||
|
pending.ReleasePending = 1
|
||||||
|
if err := a.db.Create(pending).Error; err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if _, err := portTestCreate(b, 25003, 1); err == nil {
|
||||||
|
t.Fatal("pending runtime reservation reused")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,6 +2,8 @@ package repo
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"go-backend/internal/relaysecurity"
|
"go-backend/internal/relaysecurity"
|
||||||
"go-backend/internal/store/model"
|
"go-backend/internal/store/model"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
@@ -22,7 +24,8 @@ func (r *Repository) RelayEndpointCredentials(tx *gorm.DB, tunnelID, nodeID int6
|
|||||||
err := find()
|
err := find()
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) && create {
|
if errors.Is(err, gorm.ErrRecordNotFound) && create {
|
||||||
var existing int64
|
var existing int64
|
||||||
if e := db.Model(&model.ChainTunnel{}).Where("tunnel_id = ? AND node_id = ? AND chain_type = ?", tunnelID, nodeID, role).Count(&existing).Error; e != nil {
|
// chain_tunnel stores roles as text; relay_endpoint_auth uses integers.
|
||||||
|
if e := db.Model(&model.ChainTunnel{}).Where("tunnel_id = ? AND node_id = ? AND chain_type = ?", tunnelID, nodeID, strconv.Itoa(role)).Count(&existing).Error; e != nil {
|
||||||
return nil, e
|
return nil, e
|
||||||
}
|
}
|
||||||
if existing > 0 {
|
if existing > 0 {
|
||||||
|
|||||||
@@ -0,0 +1,98 @@
|
|||||||
|
package repo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
|
||||||
|
"go-backend/internal/relaysecurity"
|
||||||
|
"go-backend/internal/store/model"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestRelayEndpointCredentialsBindsLegacyChainTypeAsText(t *testing.T) {
|
||||||
|
for _, role := range []int{2, 3} {
|
||||||
|
t.Run(strconv.Itoa(role), func(t *testing.T) {
|
||||||
|
r, err := Open(filepath.Join(t.TempDir(), "relay.db"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Cleanup(func() { _ = r.Close() })
|
||||||
|
|
||||||
|
// SQLite coerces integer parameters for this legacy text column.
|
||||||
|
// Check the actual repository query with pgx's text encoder as well,
|
||||||
|
// so the PostgreSQL regression is caught without a running server.
|
||||||
|
checked := 0
|
||||||
|
if err := r.DB().Callback().Query().After("gorm:query").Register("test:relay_chain_text_parameter", func(db *gorm.DB) {
|
||||||
|
if db.Statement.Table != "chain_tunnel" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
checked++
|
||||||
|
if len(db.Statement.Vars) != 3 {
|
||||||
|
db.AddError(fmt.Errorf("expected three chain lookup arguments, got %d", len(db.Statement.Vars)))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
encoded, err := pgtype.NewMap().Encode(pgtype.TextOID, pgtype.TextFormatCode, db.Statement.Vars[2], nil)
|
||||||
|
if err != nil {
|
||||||
|
db.AddError(fmt.Errorf("encode chain_type for PostgreSQL: %w", err))
|
||||||
|
} else if string(encoded) != strconv.Itoa(role) {
|
||||||
|
db.AddError(fmt.Errorf("unexpected chain_type argument: %q", encoded))
|
||||||
|
}
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
tunnel := model.Tunnel{Name: "relay", Type: 2, Flow: 1, RelayAuthMode: relaysecurity.Required}
|
||||||
|
if err := r.DB().Create(&tunnel).Error; err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
const nodeID = int64(7)
|
||||||
|
if err := r.DB().Transaction(func(tx *gorm.DB) error {
|
||||||
|
created, err := r.RelayEndpointCredentials(tx, tunnel.ID, nodeID, role, true)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, create := range []bool{false, true} {
|
||||||
|
loaded, err := r.RelayEndpointCredentials(tx, tunnel.ID, nodeID, role, create)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if *created != *loaded {
|
||||||
|
return fmt.Errorf("reloading endpoint changed credentials")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("create and reload endpoint: %v", err)
|
||||||
|
}
|
||||||
|
if checked != 1 {
|
||||||
|
t.Fatalf("expected one chain lookup for a new endpoint, got %d", checked)
|
||||||
|
}
|
||||||
|
|
||||||
|
chain := model.ChainTunnel{TunnelID: tunnel.ID, NodeID: nodeID, ChainType: strconv.Itoa(role)}
|
||||||
|
if err := r.DB().Create(&chain).Error; err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := r.DB().Where("tunnel_id = ?", tunnel.ID).Delete(&model.RelayEndpointAuth{}).Error; err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if _, err := r.RelayEndpointCredentials(nil, tunnel.ID, nodeID, role, true); err == nil || !strings.Contains(err.Error(), "refusing regeneration") {
|
||||||
|
t.Fatalf("missing protected credentials must fail closed, got %v", err)
|
||||||
|
}
|
||||||
|
if checked != 2 {
|
||||||
|
t.Fatalf("expected chain lookup before refusing regeneration, got %d", checked)
|
||||||
|
}
|
||||||
|
var count int64
|
||||||
|
if err := r.DB().Model(&model.RelayEndpointAuth{}).Count(&count).Error; err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if count != 0 {
|
||||||
|
t.Fatal("regenerated credentials for an existing protected endpoint")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,6 +7,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (r *Repository) QueueTunnelRuntimeCleanupTx(tx *gorm.DB, items []model.TunnelRuntimeCleanup) error {
|
func (r *Repository) QueueTunnelRuntimeCleanupTx(tx *gorm.DB, items []model.TunnelRuntimeCleanup) error {
|
||||||
|
if err := lockPortAllocationTx(tx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
for _, item := range items {
|
for _, item := range items {
|
||||||
var node model.Node
|
var node model.Node
|
||||||
if err := tx.Select("id", "is_remote").Where("id = ?", item.NodeID).First(&node).Error; err != nil {
|
if err := tx.Select("id", "is_remote").Where("id = ?", item.NodeID).First(&node).Error; err != nil {
|
||||||
|
|||||||
@@ -440,13 +440,15 @@ func TestFederationDualPanelRemoteEntryRuntimeContract(t *testing.T) {
|
|||||||
func insertContractNode(t *testing.T, r *repo.Repository, name, ip, portRange, secret string, status int) int64 {
|
func insertContractNode(t *testing.T, r *repo.Repository, name, ip, portRange, secret string, status int) int64 {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
now := time.Now().UnixMilli()
|
now := time.Now().UnixMilli()
|
||||||
if err := r.DB().Exec(`
|
var id int64
|
||||||
|
if err := r.DB().Raw(`
|
||||||
INSERT INTO node(name, secret, server_ip, server_ip_v4, server_ip_v6, port, interface_name, version, http, tls, socks, created_time, updated_time, status, tcp_listen_addr, udp_listen_addr, inx)
|
INSERT INTO node(name, secret, server_ip, server_ip_v4, server_ip_v6, port, interface_name, version, http, tls, socks, created_time, updated_time, status, tcp_listen_addr, udp_listen_addr, inx)
|
||||||
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
`, name, secret, ip, ip, "", portRange, "", "v1", 1, 1, 1, now, now, status, "[::]", "[::]", 0).Error; err != nil {
|
RETURNING id
|
||||||
|
`, name, secret, ip, ip, "", portRange, "", "v1", 1, 1, 1, now, now, status, "[::]", "[::]", 0).Row().Scan(&id); err != nil {
|
||||||
t.Fatalf("insert node %s: %v", name, err)
|
t.Fatalf("insert node %s: %v", name, err)
|
||||||
}
|
}
|
||||||
return mustLastInsertID(t, r, name)
|
return id
|
||||||
}
|
}
|
||||||
|
|
||||||
func insertPeerShare(t *testing.T, r *repo.Repository, share *repo.PeerShare) int64 {
|
func insertPeerShare(t *testing.T, r *repo.Repository, share *repo.PeerShare) int64 {
|
||||||
|
|||||||
@@ -190,7 +190,7 @@ func TestPostgresBackupImportAdvancesEveryExplicitIDTable(t *testing.T) {
|
|||||||
{"tunnel", func() error { _, err := r.CreateTunnelTx(r.DB(), "new", 1, 1, 0, 0, 1, nil, 0, "", "", 0); return err }},
|
{"tunnel", func() error { _, err := r.CreateTunnelTx(r.DB(), "new", 1, 1, 0, 0, 1, nil, 0, "", "", 0); return err }},
|
||||||
{"chain tunnel", func() error { return r.CreateChainTunnelTx(r.DB(), 1, "1", 2, sql.NullInt64{}, "", 1, "tls", "") }},
|
{"chain tunnel", func() error { return r.CreateChainTunnelTx(r.DB(), 1, "1", 2, sql.NullInt64{}, "", 1, "tls", "") }},
|
||||||
{"forward", func() error {
|
{"forward", func() error {
|
||||||
_, err := r.CreateForwardTx(1, "old", "new", 1, "127.0.0.1:80", "", 0, 0, nil, 0, "", nil, 0, 0, nil, 0, 0, 0)
|
_, err := r.CreateForwardTx(1, "old", "new", 1, "127.0.0.1:80", "", 0, 0, []int64{1}, 12346, "", nil, 0, 0, nil, 0, 0, 0)
|
||||||
return err
|
return err
|
||||||
}},
|
}},
|
||||||
{"user tunnel", func() error { return r.InsertUserTunnel(2, 1, nil, 0, 0, 0, 0, 1) }},
|
{"user tunnel", func() error { return r.InsertUserTunnel(2, 1, nil, 0, 0, 0, 0, 1) }},
|
||||||
@@ -428,6 +428,9 @@ func TestPostgresBackupImportAvoidsCascadeDeleteDeadlock(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
if err := r.DB().Create(&model.Node{ID: 1, Name: "entry", Port: "10000-20000"}).Error; err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
forward, err := r.CreateForwardTx(user, "delete-me", "old", 1, "127.0.0.1:80", "", 0, 0, []int64{1}, 12345, "", nil, 0, 0, nil, 0, 0, 0)
|
forward, err := r.CreateForwardTx(user, "delete-me", "old", 1, "127.0.0.1:80", "", 0, 0, []int64{1}, 12345, "", nil, 0, 0, nil, 0, 0, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
|||||||
@@ -11,11 +11,24 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"go-backend/internal/auth"
|
"go-backend/internal/auth"
|
||||||
|
httpserver "go-backend/internal/http"
|
||||||
|
"go-backend/internal/http/handler"
|
||||||
"go-backend/internal/store/repo"
|
"go-backend/internal/store/repo"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNewRelayTunnelEmitsMatchingPrivateEndpointAuthentication(t *testing.T) {
|
func TestNewRelayTunnelEmitsMatchingPrivateEndpointAuthentication(t *testing.T) {
|
||||||
router, r := setupContractRouter(t, "relay-auth-contract")
|
router, r := setupContractRouter(t, "relay-auth-contract")
|
||||||
|
assertNewRelayTunnelEndpointAuthentication(t, router, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPostgresNewRelayTunnelEmitsMatchingPrivateEndpointAuthentication(t *testing.T) {
|
||||||
|
r := ownerNamesRepository(t, "postgres")()
|
||||||
|
router := httpserver.NewRouter(handler.New(r, "relay-auth-contract"), "relay-auth-contract")
|
||||||
|
assertNewRelayTunnelEndpointAuthentication(t, router, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func assertNewRelayTunnelEndpointAuthentication(t *testing.T, router http.Handler, r *repo.Repository) {
|
||||||
|
t.Helper()
|
||||||
server := httptest.NewServer(router)
|
server := httptest.NewServer(router)
|
||||||
defer server.Close()
|
defer server.Close()
|
||||||
token, err := auth.GenerateToken(1, "admin", 0, "relay-auth-contract")
|
token, err := auth.GenerateToken(1, "admin", 0, "relay-auth-contract")
|
||||||
|
|||||||
@@ -0,0 +1,98 @@
|
|||||||
|
package contract_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"go-backend/internal/auth"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestTunnelRedeployStreamContract(t *testing.T) {
|
||||||
|
const secret = "contract-jwt-secret"
|
||||||
|
router, _ := setupContractRouter(t, secret)
|
||||||
|
token, err := auth.GenerateToken(1, "admin_user", 0, secret)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/tunnel/batch-redeploy/stream", strings.NewReader(`{"ids":[999998,999999]}`))
|
||||||
|
req.Header.Set("Authorization", token)
|
||||||
|
res := httptest.NewRecorder()
|
||||||
|
router.ServeHTTP(res, req)
|
||||||
|
if !strings.HasPrefix(res.Header().Get("Content-Type"), "application/x-ndjson") {
|
||||||
|
t.Fatalf("expected streamed progress, got %s: %s", res.Header().Get("Content-Type"), res.Body.String())
|
||||||
|
}
|
||||||
|
decoder := json.NewDecoder(res.Body)
|
||||||
|
for _, want := range []string{"start", "processing", "item", "processing", "item", "done"} {
|
||||||
|
var event struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
Data json.RawMessage `json:"data"`
|
||||||
|
}
|
||||||
|
if err := decoder.Decode(&event); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if event.Type != want {
|
||||||
|
t.Fatalf("got %s, want %s", event.Type, want)
|
||||||
|
}
|
||||||
|
if want == "item" {
|
||||||
|
var data struct{ Result map[string]interface{} }
|
||||||
|
if err := json.Unmarshal(event.Data, &data); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if _, ok := data.Result["name"].(string); !ok {
|
||||||
|
t.Fatalf("item name must be a string, including missing tunnels: %s", event.Data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if want == "done" {
|
||||||
|
var data struct {
|
||||||
|
Progress struct{ Total, Completed, Success, Failed int }
|
||||||
|
Result struct {
|
||||||
|
SuccessCount, FailCount int
|
||||||
|
Failures []struct {
|
||||||
|
ID int64
|
||||||
|
Reason string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(event.Data, &data); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if data.Progress.Total != 2 || data.Progress.Completed != 2 || data.Progress.Failed != 2 || data.Progress.Success != 0 || data.Result.FailCount != 2 || len(data.Result.Failures) != 2 {
|
||||||
|
t.Fatalf("incorrect completion: %+v", data)
|
||||||
|
}
|
||||||
|
if data.Result.Failures[0].Reason == "" {
|
||||||
|
t.Fatal("missing failure detail")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !res.Flushed {
|
||||||
|
t.Fatal("stream did not flush")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTunnelRedeployStreamAuthorizationContract(t *testing.T) {
|
||||||
|
const secret = "contract-jwt-secret"
|
||||||
|
router, repository := setupContractRouter(t, secret)
|
||||||
|
seedLegacyUser(t, repository, 9111, "stream-normal-user", "password")
|
||||||
|
token, err := auth.GenerateToken(9111, "stream-normal-user", 1, secret)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
for _, tc := range []struct {
|
||||||
|
name, token string
|
||||||
|
code int
|
||||||
|
}{{"anonymous", "", 401}, {"non-admin", token, 403}} {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/tunnel/batch-redeploy/stream", strings.NewReader(`{"ids":[999999]}`))
|
||||||
|
req.Header.Set("Authorization", tc.token)
|
||||||
|
res := httptest.NewRecorder()
|
||||||
|
router.ServeHTTP(res, req)
|
||||||
|
assertCode(t, res, tc.code)
|
||||||
|
if strings.Contains(res.Body.String(), `"type":"start"`) {
|
||||||
|
t.Fatal("unauthorized request started stream")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -71,6 +71,20 @@ http {
|
|||||||
}
|
}
|
||||||
|
|
||||||
# API代理到后端
|
# API代理到后端
|
||||||
|
location = /api/v1/tunnel/batch-redeploy/stream {
|
||||||
|
proxy_set_header Host $host;
|
||||||
|
proxy_set_header X-Real-IP $remote_addr;
|
||||||
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||||
|
proxy_set_header X-Forwarded-Proto $scheme;
|
||||||
|
proxy_http_version 1.1;
|
||||||
|
proxy_buffering off;
|
||||||
|
proxy_cache off;
|
||||||
|
chunked_transfer_encoding on;
|
||||||
|
proxy_read_timeout 120s;
|
||||||
|
proxy_send_timeout 120s;
|
||||||
|
proxy_pass http://backend:6365/api/v1/tunnel/batch-redeploy/stream;
|
||||||
|
}
|
||||||
|
|
||||||
location = /api/v1/tunnel/diagnose/stream {
|
location = /api/v1/tunnel/diagnose/stream {
|
||||||
proxy_set_header Host $host;
|
proxy_set_header Host $host;
|
||||||
proxy_set_header X-Real-IP $remote_addr;
|
proxy_set_header X-Real-IP $remote_addr;
|
||||||
|
|||||||
@@ -0,0 +1,287 @@
|
|||||||
|
import type { BatchOperationResult } from "@/api/types";
|
||||||
|
|
||||||
|
import axios from "axios";
|
||||||
|
|
||||||
|
import { clearSession, getToken } from "@/utils/session";
|
||||||
|
|
||||||
|
const STREAM_IDLE_TIMEOUT_MS = 2 * 60 * 1000;
|
||||||
|
const MAX_EVENT_LENGTH = 1024 * 1024;
|
||||||
|
const UNCERTAIN_RESULT =
|
||||||
|
"下发进度连接中断,部分配置可能已执行,结果尚未确认。请核对节点与隧道状态后再决定是否重新下发,系统不会自动重试。";
|
||||||
|
|
||||||
|
export interface TunnelRedeployProgress {
|
||||||
|
total: number;
|
||||||
|
completed: number;
|
||||||
|
success: number;
|
||||||
|
failed: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface TunnelRedeployProcessing {
|
||||||
|
id: number;
|
||||||
|
name: string;
|
||||||
|
progress: TunnelRedeployProgress;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface TunnelRedeployItem {
|
||||||
|
result: {
|
||||||
|
id: number;
|
||||||
|
name: string;
|
||||||
|
success: boolean;
|
||||||
|
reason?: string;
|
||||||
|
};
|
||||||
|
progress: TunnelRedeployProgress;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface TunnelRedeployCallbacks {
|
||||||
|
onStart?: (progress: TunnelRedeployProgress) => void;
|
||||||
|
onProcessing?: (payload: TunnelRedeployProcessing) => void;
|
||||||
|
onItem?: (payload: TunnelRedeployItem) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
type RawObject = Record<string, unknown>;
|
||||||
|
|
||||||
|
class RequestRejectedError extends Error {}
|
||||||
|
|
||||||
|
const object = (value: unknown): RawObject => {
|
||||||
|
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
||||||
|
throw new Error(UNCERTAIN_RESULT);
|
||||||
|
}
|
||||||
|
|
||||||
|
return value as RawObject;
|
||||||
|
};
|
||||||
|
|
||||||
|
const readProgress = (value: unknown): TunnelRedeployProgress => {
|
||||||
|
const candidate = object(value);
|
||||||
|
const { total, completed, success, failed } = candidate;
|
||||||
|
|
||||||
|
if (
|
||||||
|
![total, completed, success, failed].every(
|
||||||
|
(count) =>
|
||||||
|
typeof count === "number" && Number.isSafeInteger(count) && count >= 0,
|
||||||
|
) ||
|
||||||
|
(completed as number) > (total as number) ||
|
||||||
|
(success as number) + (failed as number) !== completed
|
||||||
|
) {
|
||||||
|
throw new Error(UNCERTAIN_RESULT);
|
||||||
|
}
|
||||||
|
|
||||||
|
return { total, completed, success, failed } as TunnelRedeployProgress;
|
||||||
|
};
|
||||||
|
|
||||||
|
const expireSession = () => {
|
||||||
|
clearSession();
|
||||||
|
if (window.location.pathname !== "/") window.location.href = "/";
|
||||||
|
};
|
||||||
|
|
||||||
|
/** A mutation stream must never be retried or replayed through the legacy API. */
|
||||||
|
export const batchRedeployTunnelsStream = async (
|
||||||
|
ids: number[],
|
||||||
|
callbacks: TunnelRedeployCallbacks,
|
||||||
|
signal?: AbortSignal,
|
||||||
|
): Promise<BatchOperationResult> => {
|
||||||
|
if (signal?.aborted) throw new DOMException("Aborted", "AbortError");
|
||||||
|
if (!ids.length || ids.some((id) => !Number.isSafeInteger(id) || id <= 0)) {
|
||||||
|
throw new Error("请选择有效的隧道");
|
||||||
|
}
|
||||||
|
if (typeof fetch !== "function" || typeof TextDecoder === "undefined") {
|
||||||
|
throw new Error("当前浏览器不支持实时下发进度,请使用支持流式响应的浏览器");
|
||||||
|
}
|
||||||
|
|
||||||
|
const requestedIds = new Set(ids);
|
||||||
|
const controller = new AbortController();
|
||||||
|
const abort = () => controller.abort();
|
||||||
|
let timer: ReturnType<typeof setTimeout> | undefined;
|
||||||
|
let reader: ReadableStreamDefaultReader<Uint8Array> | undefined;
|
||||||
|
const resetTimeout = () => {
|
||||||
|
clearTimeout(timer);
|
||||||
|
timer = setTimeout(abort, STREAM_IDLE_TIMEOUT_MS);
|
||||||
|
};
|
||||||
|
|
||||||
|
signal?.addEventListener("abort", abort, { once: true });
|
||||||
|
resetTimeout();
|
||||||
|
|
||||||
|
try {
|
||||||
|
const base = axios.defaults.baseURL || "/api/v1/";
|
||||||
|
const response = await fetch(
|
||||||
|
`${base.endsWith("/") ? base : `${base}/`}tunnel/batch-redeploy/stream`,
|
||||||
|
{
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Accept: "application/x-ndjson, application/json",
|
||||||
|
Authorization: getToken() || "",
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ ids: Array.from(requestedIds) }),
|
||||||
|
signal: controller.signal,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
if (response.status === 401) {
|
||||||
|
expireSession();
|
||||||
|
throw new RequestRejectedError("登录已过期,请重新登录");
|
||||||
|
}
|
||||||
|
if (response.status === 404 || response.status === 405) {
|
||||||
|
throw new RequestRejectedError(
|
||||||
|
"后端尚不支持实时下发进度,请升级后端后再操作;未自动改用旧接口下发",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (
|
||||||
|
!response.headers.get("content-type")?.includes("application/x-ndjson")
|
||||||
|
) {
|
||||||
|
let payload: RawObject | undefined;
|
||||||
|
|
||||||
|
try {
|
||||||
|
payload = object(await response.json());
|
||||||
|
} catch {
|
||||||
|
throw new Error(UNCERTAIN_RESULT);
|
||||||
|
}
|
||||||
|
if (payload.code === 401) {
|
||||||
|
expireSession();
|
||||||
|
throw new RequestRejectedError("登录已过期,请重新登录");
|
||||||
|
}
|
||||||
|
if (typeof payload.code === "number" && payload.code !== 0) {
|
||||||
|
throw new RequestRejectedError(
|
||||||
|
typeof payload.msg === "string" ? payload.msg : "下发请求失败",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
throw new Error(UNCERTAIN_RESULT);
|
||||||
|
}
|
||||||
|
if (!response.ok || !response.body) throw new Error(UNCERTAIN_RESULT);
|
||||||
|
|
||||||
|
reader = response.body.getReader();
|
||||||
|
const decoder = new TextDecoder("utf-8", { fatal: true });
|
||||||
|
let buffer = "";
|
||||||
|
let progress: TunnelRedeployProgress | undefined;
|
||||||
|
let currentId: number | undefined;
|
||||||
|
const completedIds = new Set<number>();
|
||||||
|
let finalResult: BatchOperationResult | undefined;
|
||||||
|
|
||||||
|
const processLine = (line: string) => {
|
||||||
|
if (!line.trim()) return;
|
||||||
|
const event = object(JSON.parse(line));
|
||||||
|
const data = object(event.data);
|
||||||
|
const next = readProgress(event.type === "start" ? data : data.progress);
|
||||||
|
|
||||||
|
if (
|
||||||
|
next.total !== requestedIds.size ||
|
||||||
|
(progress &&
|
||||||
|
(next.success < progress.success || next.failed < progress.failed))
|
||||||
|
)
|
||||||
|
throw new Error(UNCERTAIN_RESULT);
|
||||||
|
|
||||||
|
if (event.type === "start") {
|
||||||
|
if (progress || next.completed !== 0) throw new Error(UNCERTAIN_RESULT);
|
||||||
|
progress = next;
|
||||||
|
callbacks.onStart?.(next);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!progress) throw new Error(UNCERTAIN_RESULT);
|
||||||
|
if (event.type === "processing") {
|
||||||
|
if (
|
||||||
|
typeof data.id !== "number" ||
|
||||||
|
!requestedIds.has(data.id) ||
|
||||||
|
typeof data.name !== "string" ||
|
||||||
|
completedIds.has(data.id) ||
|
||||||
|
currentId !== undefined ||
|
||||||
|
next.completed !== progress.completed
|
||||||
|
)
|
||||||
|
throw new Error(UNCERTAIN_RESULT);
|
||||||
|
currentId = data.id;
|
||||||
|
callbacks.onProcessing?.({
|
||||||
|
id: data.id,
|
||||||
|
name: data.name,
|
||||||
|
progress: next,
|
||||||
|
});
|
||||||
|
} else if (event.type === "item") {
|
||||||
|
const result = object(data.result);
|
||||||
|
|
||||||
|
if (
|
||||||
|
result.id !== currentId ||
|
||||||
|
currentId === undefined ||
|
||||||
|
typeof result.name !== "string" ||
|
||||||
|
typeof result.success !== "boolean" ||
|
||||||
|
(result.reason !== undefined && typeof result.reason !== "string") ||
|
||||||
|
next.completed !== progress.completed + 1 ||
|
||||||
|
next.success !== progress.success + (result.success ? 1 : 0) ||
|
||||||
|
next.failed !== progress.failed + (result.success ? 0 : 1)
|
||||||
|
)
|
||||||
|
throw new Error(UNCERTAIN_RESULT);
|
||||||
|
completedIds.add(currentId);
|
||||||
|
currentId = undefined;
|
||||||
|
callbacks.onItem?.({
|
||||||
|
result: result as unknown as TunnelRedeployItem["result"],
|
||||||
|
progress: next,
|
||||||
|
});
|
||||||
|
} else if (event.type === "done") {
|
||||||
|
const result = object(data.result);
|
||||||
|
const failures = result.failures ?? [];
|
||||||
|
|
||||||
|
if (
|
||||||
|
currentId !== undefined ||
|
||||||
|
next.completed !== next.total ||
|
||||||
|
next.completed !== completedIds.size ||
|
||||||
|
next.completed !== progress.completed ||
|
||||||
|
result.successCount !== next.success ||
|
||||||
|
result.failCount !== next.failed ||
|
||||||
|
!Array.isArray(failures) ||
|
||||||
|
failures.length !== next.failed ||
|
||||||
|
failures.some((failure) => {
|
||||||
|
const item = object(failure);
|
||||||
|
|
||||||
|
return (
|
||||||
|
typeof item.id !== "number" ||
|
||||||
|
!completedIds.has(item.id) ||
|
||||||
|
typeof item.reason !== "string" ||
|
||||||
|
(item.name !== undefined && typeof item.name !== "string")
|
||||||
|
);
|
||||||
|
})
|
||||||
|
)
|
||||||
|
throw new Error(UNCERTAIN_RESULT);
|
||||||
|
finalResult = result as unknown as BatchOperationResult;
|
||||||
|
} else {
|
||||||
|
throw new Error(UNCERTAIN_RESULT);
|
||||||
|
}
|
||||||
|
progress = next;
|
||||||
|
};
|
||||||
|
|
||||||
|
while (!finalResult) {
|
||||||
|
const { value, done } = await reader.read();
|
||||||
|
|
||||||
|
if (done) {
|
||||||
|
buffer += decoder.decode();
|
||||||
|
if (buffer.trim()) processLine(buffer);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
resetTimeout();
|
||||||
|
buffer += decoder.decode(value, { stream: true });
|
||||||
|
let newline: number;
|
||||||
|
|
||||||
|
while ((newline = buffer.indexOf("\n")) >= 0) {
|
||||||
|
const line = buffer.slice(0, newline);
|
||||||
|
|
||||||
|
buffer = buffer.slice(newline + 1);
|
||||||
|
if (line.length > MAX_EVENT_LENGTH) throw new Error(UNCERTAIN_RESULT);
|
||||||
|
processLine(line);
|
||||||
|
if (finalResult) break;
|
||||||
|
}
|
||||||
|
if (buffer.length > MAX_EVENT_LENGTH) throw new Error(UNCERTAIN_RESULT);
|
||||||
|
}
|
||||||
|
if (!finalResult) throw new Error(UNCERTAIN_RESULT);
|
||||||
|
|
||||||
|
return finalResult;
|
||||||
|
} catch (error) {
|
||||||
|
if (signal?.aborted) throw new DOMException("Aborted", "AbortError");
|
||||||
|
if (error instanceof RequestRejectedError) throw error;
|
||||||
|
throw new Error(UNCERTAIN_RESULT);
|
||||||
|
} finally {
|
||||||
|
clearTimeout(timer);
|
||||||
|
controller.abort();
|
||||||
|
signal?.removeEventListener("abort", abort);
|
||||||
|
if (reader) {
|
||||||
|
// Cancel only the response reader, never resend an interrupted mutation.
|
||||||
|
await reader.cancel().catch(() => undefined);
|
||||||
|
reader.releaseLock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -58,7 +58,7 @@ export function OrderItem({
|
|||||||
{...attributes}
|
{...attributes}
|
||||||
{...listeners}
|
{...listeners}
|
||||||
aria-label={`拖动排序:${label}`}
|
aria-label={`拖动排序:${label}`}
|
||||||
className="shrink-0 rounded p-1 text-default-500 hover:bg-default-100 focus-visible:outline focus-visible:outline-2 focus-visible:outline-primary disabled:opacity-40 disabled:cursor-not-allowed cursor-grab active:cursor-grabbing"
|
className={`inline-flex items-center justify-center shrink-0 rounded text-default-500 hover:bg-default-100 focus-visible:outline focus-visible:outline-2 focus-visible:outline-primary disabled:opacity-40 disabled:cursor-not-allowed cursor-grab active:cursor-grabbing ${row ? "p-1" : "h-8 w-8 [@media(pointer:coarse)]:h-11 [@media(pointer:coarse)]:w-11"}`}
|
||||||
disabled={disabled}
|
disabled={disabled}
|
||||||
style={{ touchAction: "none" }}
|
style={{ touchAction: "none" }}
|
||||||
type="button"
|
type="button"
|
||||||
|
|||||||
@@ -231,6 +231,13 @@ const CONFIG_ITEMS: ConfigItem[] = [
|
|||||||
"开启后,普通用户创建或编辑规则时可将目标地址指向 127.0.0.1、10.x.x.x、172.16-31.x.x、192.168.x.x 等本地或内网地址。默认关闭以降低开放代理风险。",
|
"开启后,普通用户创建或编辑规则时可将目标地址指向 127.0.0.1、10.x.x.x、172.16-31.x.x、192.168.x.x 等本地或内网地址。默认关闭以降低开放代理风险。",
|
||||||
type: "switch",
|
type: "switch",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
key: "sequential_port_enabled",
|
||||||
|
label: "顺次分配入口端口",
|
||||||
|
description:
|
||||||
|
"开启后,创建规则时如果不指定入口端口,将从节点端口范围内按从小到大的顺序分配第一个空闲端口(例如范围 100-1000,已用 100、101、103,则下一个为 102,再下一个为 104)。关闭时为随机分配。",
|
||||||
|
type: "switch",
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
const BACKUP_TYPE_OPTIONS = [
|
const BACKUP_TYPE_OPTIONS = [
|
||||||
@@ -274,6 +281,7 @@ const getInitialConfigs = (): Record<string, string> => {
|
|||||||
"is_commercial",
|
"is_commercial",
|
||||||
"license_expiry",
|
"license_expiry",
|
||||||
"hide_footer_brand",
|
"hide_footer_brand",
|
||||||
|
"sequential_port_enabled",
|
||||||
];
|
];
|
||||||
const initialConfigs: Record<string, string> = {};
|
const initialConfigs: Record<string, string> = {};
|
||||||
|
|
||||||
|
|||||||
@@ -358,14 +358,18 @@ export default function LimitPage() {
|
|||||||
>
|
>
|
||||||
<Card className="shadow-sm overflow-hidden h-full">
|
<Card className="shadow-sm overflow-hidden h-full">
|
||||||
<CardHeader className="pb-2 md:pb-2">
|
<CardHeader className="pb-2 md:pb-2">
|
||||||
|
<div className="flex items-center gap-2 w-full min-w-0">
|
||||||
|
<div className="flex items-center gap-1 flex-1 min-w-0">
|
||||||
<OrderHandle />
|
<OrderHandle />
|
||||||
<div className="flex justify-between items-start w-full">
|
<h3
|
||||||
<div>
|
className="font-semibold text-foreground truncate"
|
||||||
<h3 className="font-semibold text-foreground">
|
title={rule.name}
|
||||||
|
>
|
||||||
{rule.name}
|
{rule.name}
|
||||||
</h3>
|
</h3>
|
||||||
</div>
|
</div>
|
||||||
<Chip
|
<Chip
|
||||||
|
className="shrink-0"
|
||||||
color={rule.status === 1 ? "success" : "danger"}
|
color={rule.status === 1 ? "success" : "danger"}
|
||||||
size="sm"
|
size="sm"
|
||||||
variant="flat"
|
variant="flat"
|
||||||
|
|||||||
@@ -45,7 +45,6 @@ import { Chip } from "@/shadcn-bridge/heroui/chip";
|
|||||||
import { Switch } from "@/shadcn-bridge/heroui/switch";
|
import { Switch } from "@/shadcn-bridge/heroui/switch";
|
||||||
import { Spinner } from "@/shadcn-bridge/heroui/spinner";
|
import { Spinner } from "@/shadcn-bridge/heroui/spinner";
|
||||||
import { Alert } from "@/shadcn-bridge/heroui/alert";
|
import { Alert } from "@/shadcn-bridge/heroui/alert";
|
||||||
import { Progress } from "@/shadcn-bridge/heroui/progress";
|
|
||||||
import { Accordion, AccordionItem } from "@/shadcn-bridge/heroui/accordion";
|
import { Accordion, AccordionItem } from "@/shadcn-bridge/heroui/accordion";
|
||||||
import { Select, SelectItem } from "@/shadcn-bridge/heroui/select";
|
import { Select, SelectItem } from "@/shadcn-bridge/heroui/select";
|
||||||
import { Checkbox } from "@/shadcn-bridge/heroui/checkbox";
|
import { Checkbox } from "@/shadcn-bridge/heroui/checkbox";
|
||||||
@@ -70,6 +69,10 @@ import {
|
|||||||
getConnectionStatusMeta,
|
getConnectionStatusMeta,
|
||||||
getRemoteSyncErrorMessage,
|
getRemoteSyncErrorMessage,
|
||||||
} from "@/pages/node/display";
|
} from "@/pages/node/display";
|
||||||
|
import {
|
||||||
|
NodeUpgradeProgress,
|
||||||
|
type NodeUpgradeProgressState,
|
||||||
|
} from "@/pages/node/upgrade-progress";
|
||||||
import { tryCopyInstallCommand } from "@/pages/node/install-command";
|
import { tryCopyInstallCommand } from "@/pages/node/install-command";
|
||||||
import {
|
import {
|
||||||
getNodeRenewalSnapshot,
|
getNodeRenewalSnapshot,
|
||||||
@@ -422,7 +425,7 @@ export default function NodePage() {
|
|||||||
const [selectedVersion, setSelectedVersion] = useState("");
|
const [selectedVersion, setSelectedVersion] = useState("");
|
||||||
const [batchUpgradeLoading, setBatchUpgradeLoading] = useState(false);
|
const [batchUpgradeLoading, setBatchUpgradeLoading] = useState(false);
|
||||||
const [upgradeProgress, setUpgradeProgress] = useState<
|
const [upgradeProgress, setUpgradeProgress] = useState<
|
||||||
Record<number, { stage: string; percent: number; message: string }>
|
Record<number, NodeUpgradeProgressState>
|
||||||
>({});
|
>({});
|
||||||
|
|
||||||
const [infoPopoverPlacement, setInfoPopoverPlacement] = useState<
|
const [infoPopoverPlacement, setInfoPopoverPlacement] = useState<
|
||||||
@@ -1120,6 +1123,13 @@ export default function NodePage() {
|
|||||||
const node = nodeList.find((n) => n.id === upgradeTargetNodeId);
|
const node = nodeList.find((n) => n.id === upgradeTargetNodeId);
|
||||||
|
|
||||||
if (!node) return;
|
if (!node) return;
|
||||||
|
setUpgradeProgress((prev) => {
|
||||||
|
const next = { ...prev };
|
||||||
|
|
||||||
|
delete next[upgradeTargetNodeId];
|
||||||
|
|
||||||
|
return next;
|
||||||
|
});
|
||||||
setNodeList((prev) =>
|
setNodeList((prev) =>
|
||||||
prev.map((n) =>
|
prev.map((n) =>
|
||||||
n.id === upgradeTargetNodeId ? { ...n, upgradeLoading: true } : n,
|
n.id === upgradeTargetNodeId ? { ...n, upgradeLoading: true } : n,
|
||||||
@@ -1160,6 +1170,13 @@ export default function NodePage() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setUpgradeProgress((prev) => {
|
||||||
|
const next = { ...prev };
|
||||||
|
|
||||||
|
selectedLocalIds.forEach((id) => delete next[id]);
|
||||||
|
|
||||||
|
return next;
|
||||||
|
});
|
||||||
setBatchUpgradeLoading(true);
|
setBatchUpgradeLoading(true);
|
||||||
setUpgradeModalOpen(false);
|
setUpgradeModalOpen(false);
|
||||||
try {
|
try {
|
||||||
@@ -1956,6 +1973,11 @@ export default function NodePage() {
|
|||||||
<span className="text-sm text-default-500">
|
<span className="text-sm text-default-500">
|
||||||
{node.version || "-"}
|
{node.version || "-"}
|
||||||
</span>
|
</span>
|
||||||
|
{!isRemoteNode && (
|
||||||
|
<NodeUpgradeProgress
|
||||||
|
progress={upgradeProgress[node.id]}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
</TableCell>
|
</TableCell>
|
||||||
<TableCell>
|
<TableCell>
|
||||||
<div className="flex flex-wrap items-center gap-1.5 min-w-max">
|
<div className="flex flex-wrap items-center gap-1.5 min-w-max">
|
||||||
@@ -2261,19 +2283,9 @@ export default function NodePage() {
|
|||||||
{node.version || "未知"}
|
{node.version || "未知"}
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
{upgradeProgress[node.id] &&
|
<NodeUpgradeProgress
|
||||||
upgradeProgress[node.id].percent < 100 && (
|
progress={upgradeProgress[node.id]}
|
||||||
<div className="mt-1">
|
|
||||||
<Progress
|
|
||||||
showValueLabel
|
|
||||||
aria-label="升级进度"
|
|
||||||
color="warning"
|
|
||||||
label={upgradeProgress[node.id].message}
|
|
||||||
size="sm"
|
|
||||||
value={upgradeProgress[node.id].percent}
|
|
||||||
/>
|
/>
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
<div className="flex justify-between text-sm">
|
<div className="flex justify-between text-sm">
|
||||||
<span className="text-default-600">
|
<span className="text-default-600">
|
||||||
开机时间
|
开机时间
|
||||||
|
|||||||
@@ -0,0 +1,46 @@
|
|||||||
|
import { Progress } from "@/shadcn-bridge/heroui/progress";
|
||||||
|
|
||||||
|
export interface NodeUpgradeProgressState {
|
||||||
|
stage: string;
|
||||||
|
percent: number;
|
||||||
|
message: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const STAGE_LABELS: Record<string, string> = {
|
||||||
|
downloading: "下载",
|
||||||
|
verifying: "校验",
|
||||||
|
installing: "安装 / 重启",
|
||||||
|
};
|
||||||
|
|
||||||
|
export function NodeUpgradeProgress({
|
||||||
|
progress,
|
||||||
|
}: {
|
||||||
|
progress?: NodeUpgradeProgressState;
|
||||||
|
}) {
|
||||||
|
// The final installing event means the agent is restarting, not a durable
|
||||||
|
// job state. Do not leave a stale "重启中 100%" banner after it reconnects.
|
||||||
|
if (
|
||||||
|
!progress ||
|
||||||
|
(progress.stage === "installing" && progress.percent >= 100)
|
||||||
|
) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const percent = Number.isFinite(progress.percent)
|
||||||
|
? Math.min(100, Math.max(0, progress.percent))
|
||||||
|
: 0;
|
||||||
|
const stage = STAGE_LABELS[progress.stage];
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="mt-2 min-w-40 max-w-xs break-words" role="status">
|
||||||
|
<Progress
|
||||||
|
showValueLabel
|
||||||
|
aria-label={stage ? `升级${stage}进度` : "升级进度"}
|
||||||
|
color="warning"
|
||||||
|
label={progress.message || (stage ? `正在${stage}` : "正在升级")}
|
||||||
|
size="sm"
|
||||||
|
value={percent}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -64,7 +64,6 @@ import {
|
|||||||
getNodeList,
|
getNodeList,
|
||||||
diagnoseTunnel,
|
diagnoseTunnel,
|
||||||
updateTunnelOrder,
|
updateTunnelOrder,
|
||||||
batchRedeployTunnels,
|
|
||||||
previewBatchTunnelDelete,
|
previewBatchTunnelDelete,
|
||||||
previewTunnelDelete,
|
previewTunnelDelete,
|
||||||
} from "@/api";
|
} from "@/api";
|
||||||
@@ -74,6 +73,11 @@ import {
|
|||||||
getDiagnosisQualityDisplay,
|
getDiagnosisQualityDisplay,
|
||||||
type DiagnosisResult,
|
type DiagnosisResult,
|
||||||
} from "@/pages/tunnel/diagnosis";
|
} from "@/pages/tunnel/diagnosis";
|
||||||
|
import { useTunnelRedeploy } from "@/pages/tunnel/use-tunnel-redeploy";
|
||||||
|
import {
|
||||||
|
TunnelRedeployStatus,
|
||||||
|
TunnelRedeploySummary,
|
||||||
|
} from "@/pages/tunnel/redeploy-progress";
|
||||||
import { diagnoseTunnelStream } from "@/api/diagnosis-stream";
|
import { diagnoseTunnelStream } from "@/api/diagnosis-stream";
|
||||||
import {
|
import {
|
||||||
createTunnelFormDefaults,
|
createTunnelFormDefaults,
|
||||||
@@ -455,6 +459,7 @@ export default function TunnelPage() {
|
|||||||
number | null
|
number | null
|
||||||
>(null);
|
>(null);
|
||||||
const [batchLoading, setBatchLoading] = useState(false);
|
const [batchLoading, setBatchLoading] = useState(false);
|
||||||
|
const redeploy = useTunnelRedeploy();
|
||||||
const [batchProgress, setBatchProgress] = useState<BatchProgressState>({
|
const [batchProgress, setBatchProgress] = useState<BatchProgressState>({
|
||||||
active: false,
|
active: false,
|
||||||
label: "",
|
label: "",
|
||||||
@@ -1351,19 +1356,12 @@ export default function TunnelPage() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handleBatchRedeploy = async () => {
|
const handleBatchRedeploy = async () => {
|
||||||
if (selectedIds.size === 0) return;
|
if (selectedIds.size === 0 || batchLoading || redeploy.state?.running)
|
||||||
setBatchLoading(true);
|
return;
|
||||||
setBatchProgress({
|
|
||||||
active: true,
|
|
||||||
label: `正在重新下发 ${selectedIds.size} 条隧道...`,
|
|
||||||
percent: 30,
|
|
||||||
});
|
|
||||||
try {
|
try {
|
||||||
const res = await batchRedeployTunnels(Array.from(selectedIds));
|
const result = await redeploy.run(Array.from(selectedIds));
|
||||||
|
|
||||||
if (res.code === 0) {
|
|
||||||
const result = res.data;
|
|
||||||
|
|
||||||
|
if (!result) return;
|
||||||
if (result.failCount === 0) {
|
if (result.failCount === 0) {
|
||||||
toast.success(`成功重新下发 ${result.successCount} 项`);
|
toast.success(`成功重新下发 ${result.successCount} 项`);
|
||||||
} else {
|
} else {
|
||||||
@@ -1386,20 +1384,11 @@ export default function TunnelPage() {
|
|||||||
}
|
}
|
||||||
setSelectedIds(new Set());
|
setSelectedIds(new Set());
|
||||||
setSelectMode(false);
|
setSelectMode(false);
|
||||||
setBatchProgress({
|
|
||||||
active: true,
|
|
||||||
label: `重新下发完成:成功 ${result.successCount} 项,正在刷新列表...`,
|
|
||||||
percent: 100,
|
|
||||||
});
|
|
||||||
await refreshTunnelList(false);
|
await refreshTunnelList(false);
|
||||||
} else {
|
|
||||||
toast.error(res.msg || "下发失败");
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
toast.error(extractApiErrorMessage(error, "下发失败"));
|
toast.error(
|
||||||
} finally {
|
extractApiErrorMessage(error, "下发进度中断,请检查结果后再重试"),
|
||||||
setBatchProgress({ active: false, label: "", percent: 0 });
|
);
|
||||||
setBatchLoading(false);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -1707,7 +1696,11 @@ export default function TunnelPage() {
|
|||||||
</Button>
|
</Button>
|
||||||
<Button
|
<Button
|
||||||
color="danger"
|
color="danger"
|
||||||
isDisabled={selectedIds.size === 0}
|
isDisabled={
|
||||||
|
selectedIds.size === 0 ||
|
||||||
|
batchLoading ||
|
||||||
|
redeploy.state?.running
|
||||||
|
}
|
||||||
size="sm"
|
size="sm"
|
||||||
variant="flat"
|
variant="flat"
|
||||||
onPress={handleOpenBatchDeleteModal}
|
onPress={handleOpenBatchDeleteModal}
|
||||||
@@ -1716,8 +1709,12 @@ export default function TunnelPage() {
|
|||||||
</Button>
|
</Button>
|
||||||
<Button
|
<Button
|
||||||
color="primary"
|
color="primary"
|
||||||
isDisabled={selectedIds.size === 0}
|
isDisabled={
|
||||||
isLoading={batchLoading}
|
selectedIds.size === 0 ||
|
||||||
|
batchLoading ||
|
||||||
|
redeploy.state?.running
|
||||||
|
}
|
||||||
|
isLoading={batchLoading || redeploy.state?.running}
|
||||||
size="sm"
|
size="sm"
|
||||||
variant="flat"
|
variant="flat"
|
||||||
onPress={handleBatchRedeploy}
|
onPress={handleBatchRedeploy}
|
||||||
@@ -1789,6 +1786,11 @@ export default function TunnelPage() {
|
|||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
<TunnelRedeploySummary
|
||||||
|
state={redeploy.state}
|
||||||
|
onDismiss={redeploy.clear}
|
||||||
|
/>
|
||||||
|
|
||||||
{/* 隧道卡片网格 */}
|
{/* 隧道卡片网格 */}
|
||||||
{tunnels.length > 0 ? (
|
{tunnels.length > 0 ? (
|
||||||
viewMode === "list" ? (
|
viewMode === "list" ? (
|
||||||
@@ -1860,6 +1862,9 @@ export default function TunnelPage() {
|
|||||||
{tunnel.name}
|
{tunnel.name}
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
|
<TunnelRedeployStatus
|
||||||
|
row={redeploy.state?.rows[tunnel.id]}
|
||||||
|
/>
|
||||||
</TableCell>
|
</TableCell>
|
||||||
<TableCell>
|
<TableCell>
|
||||||
<Chip
|
<Chip
|
||||||
@@ -1918,6 +1923,7 @@ export default function TunnelPage() {
|
|||||||
<div className="flex flex-wrap items-center gap-1.5 min-w-max">
|
<div className="flex flex-wrap items-center gap-1.5 min-w-max">
|
||||||
<Button
|
<Button
|
||||||
className="h-6 px-2 min-w-0 text-xs bg-indigo-50 text-indigo-600 hover:bg-indigo-100 dark:bg-indigo-950/30 dark:text-indigo-400"
|
className="h-6 px-2 min-w-0 text-xs bg-indigo-50 text-indigo-600 hover:bg-indigo-100 dark:bg-indigo-950/30 dark:text-indigo-400"
|
||||||
|
isDisabled={redeploy.state?.running}
|
||||||
size="sm"
|
size="sm"
|
||||||
variant="flat"
|
variant="flat"
|
||||||
onPress={() => handleEdit(tunnel)}
|
onPress={() => handleEdit(tunnel)}
|
||||||
@@ -1934,6 +1940,7 @@ export default function TunnelPage() {
|
|||||||
</Button>
|
</Button>
|
||||||
<Button
|
<Button
|
||||||
className="h-6 px-2 min-w-0 text-xs bg-rose-50 text-rose-600 hover:bg-rose-100 dark:bg-rose-950/30 dark:text-rose-400"
|
className="h-6 px-2 min-w-0 text-xs bg-rose-50 text-rose-600 hover:bg-rose-100 dark:bg-rose-950/30 dark:text-rose-400"
|
||||||
|
isDisabled={redeploy.state?.running}
|
||||||
size="sm"
|
size="sm"
|
||||||
variant="flat"
|
variant="flat"
|
||||||
onPress={() => handleDelete(tunnel)}
|
onPress={() => handleDelete(tunnel)}
|
||||||
@@ -2015,6 +2022,9 @@ export default function TunnelPage() {
|
|||||||
</CardHeader>
|
</CardHeader>
|
||||||
|
|
||||||
<CardBody className="pt-0 pb-3 md:pt-0 md:pb-3">
|
<CardBody className="pt-0 pb-3 md:pt-0 md:pb-3">
|
||||||
|
<TunnelRedeployStatus
|
||||||
|
row={redeploy.state?.rows[tunnel.id]}
|
||||||
|
/>
|
||||||
<div className="space-y-3">
|
<div className="space-y-3">
|
||||||
{/* 拓扑结构 */}
|
{/* 拓扑结构 */}
|
||||||
<div className="pt-2 border-t border-divider">
|
<div className="pt-2 border-t border-divider">
|
||||||
@@ -2160,6 +2170,7 @@ export default function TunnelPage() {
|
|||||||
<Button
|
<Button
|
||||||
className="flex-1 min-h-8"
|
className="flex-1 min-h-8"
|
||||||
color="primary"
|
color="primary"
|
||||||
|
isDisabled={redeploy.state?.running}
|
||||||
size="sm"
|
size="sm"
|
||||||
startContent={
|
startContent={
|
||||||
<svg
|
<svg
|
||||||
@@ -2202,6 +2213,7 @@ export default function TunnelPage() {
|
|||||||
<Button
|
<Button
|
||||||
className="flex-1 min-h-8"
|
className="flex-1 min-h-8"
|
||||||
color="danger"
|
color="danger"
|
||||||
|
isDisabled={redeploy.state?.running}
|
||||||
size="sm"
|
size="sm"
|
||||||
startContent={
|
startContent={
|
||||||
<svg
|
<svg
|
||||||
|
|||||||
@@ -0,0 +1,97 @@
|
|||||||
|
import type {
|
||||||
|
TunnelRedeployRow,
|
||||||
|
TunnelRedeployState,
|
||||||
|
} from "./use-tunnel-redeploy";
|
||||||
|
|
||||||
|
import { Button } from "@/shadcn-bridge/heroui/button";
|
||||||
|
import { Progress } from "@/shadcn-bridge/heroui/progress";
|
||||||
|
import { Spinner } from "@/shadcn-bridge/heroui/spinner";
|
||||||
|
|
||||||
|
export function TunnelRedeployStatus({ row }: { row?: TunnelRedeployRow }) {
|
||||||
|
if (!row) return null;
|
||||||
|
|
||||||
|
const text = {
|
||||||
|
queued: "等待下发",
|
||||||
|
running: "正在下发配置…",
|
||||||
|
success: "配置下发成功",
|
||||||
|
failed: "配置下发失败",
|
||||||
|
unknown: "下发结果未确认",
|
||||||
|
}[row.status];
|
||||||
|
const color =
|
||||||
|
row.status === "failed"
|
||||||
|
? "text-danger"
|
||||||
|
: row.status === "success"
|
||||||
|
? "text-success"
|
||||||
|
: row.status === "unknown"
|
||||||
|
? "text-warning"
|
||||||
|
: "text-default-500";
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className={`mt-1 flex items-start gap-1.5 text-xs ${color}`}>
|
||||||
|
{row.status === "running" && <Spinner size="sm" />}
|
||||||
|
<span className="max-w-xs break-words" title={row.reason}>
|
||||||
|
{text}
|
||||||
|
{row.reason ? `:${row.reason}` : ""}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function TunnelRedeploySummary({
|
||||||
|
state,
|
||||||
|
onDismiss,
|
||||||
|
}: {
|
||||||
|
state: TunnelRedeployState | null;
|
||||||
|
onDismiss: () => void;
|
||||||
|
}) {
|
||||||
|
if (!state) return null;
|
||||||
|
|
||||||
|
const { progress, running, error, currentName } = state;
|
||||||
|
const label = `${error ? "已确认" : "已完成"} ${progress.completed}/${progress.total} 条,成功 ${progress.success} 条,失败 ${progress.failed} 条`;
|
||||||
|
const percent =
|
||||||
|
progress.total > 0 ? (progress.completed / progress.total) * 100 : 0;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
className="mb-4 space-y-2 rounded-xl border border-divider bg-content1 p-3"
|
||||||
|
role="status"
|
||||||
|
>
|
||||||
|
<div className="flex items-start justify-between gap-3">
|
||||||
|
<div className="min-w-0 space-y-1 break-words text-sm">
|
||||||
|
<p className="font-medium">
|
||||||
|
{running
|
||||||
|
? "正在下发隧道配置"
|
||||||
|
: error
|
||||||
|
? "下发进度已中断"
|
||||||
|
: "隧道配置下发结束"}
|
||||||
|
</p>
|
||||||
|
<p className="text-default-500">{label}</p>
|
||||||
|
{currentName && <p>当前:{currentName}</p>}
|
||||||
|
{running && (
|
||||||
|
<p className="text-xs text-default-500">
|
||||||
|
进度按已完成的隧道统计,当前隧道内部步骤不计百分比。请保留此页面。
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
{error && <p className="text-warning">{error}</p>}
|
||||||
|
</div>
|
||||||
|
{!running && (
|
||||||
|
<Button
|
||||||
|
aria-label="关闭下发结果"
|
||||||
|
size="sm"
|
||||||
|
variant="light"
|
||||||
|
onPress={onDismiss}
|
||||||
|
>
|
||||||
|
关闭
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
<Progress
|
||||||
|
showValueLabel
|
||||||
|
aria-label="隧道配置下发进度"
|
||||||
|
color={error || progress.failed > 0 ? "warning" : "primary"}
|
||||||
|
size="sm"
|
||||||
|
value={percent}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,118 @@
|
|||||||
|
import { useEffect, useRef, useState } from "react";
|
||||||
|
|
||||||
|
import {
|
||||||
|
batchRedeployTunnelsStream,
|
||||||
|
type TunnelRedeployProgress,
|
||||||
|
} from "@/api/tunnel-redeploy-stream";
|
||||||
|
import { extractApiErrorMessage } from "@/api/error-message";
|
||||||
|
|
||||||
|
export interface TunnelRedeployRow {
|
||||||
|
status: "queued" | "running" | "success" | "failed" | "unknown";
|
||||||
|
reason?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface TunnelRedeployState {
|
||||||
|
running: boolean;
|
||||||
|
progress: TunnelRedeployProgress;
|
||||||
|
rows: Record<number, TunnelRedeployRow>;
|
||||||
|
currentName?: string;
|
||||||
|
error?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useTunnelRedeploy() {
|
||||||
|
const [state, setState] = useState<TunnelRedeployState | null>(null);
|
||||||
|
const abortRef = useRef<AbortController | null>(null);
|
||||||
|
|
||||||
|
useEffect(() => () => abortRef.current?.abort(), []);
|
||||||
|
|
||||||
|
const run = async (ids: number[]) => {
|
||||||
|
// Guard synchronously, before React has rendered a disabled button.
|
||||||
|
if (abortRef.current || ids.length === 0) return null;
|
||||||
|
|
||||||
|
const controller = new AbortController();
|
||||||
|
|
||||||
|
abortRef.current = controller;
|
||||||
|
setState({
|
||||||
|
running: true,
|
||||||
|
progress: { total: ids.length, completed: 0, success: 0, failed: 0 },
|
||||||
|
rows: Object.fromEntries(ids.map((id) => [id, { status: "queued" }])),
|
||||||
|
});
|
||||||
|
try {
|
||||||
|
const result = await batchRedeployTunnelsStream(
|
||||||
|
ids,
|
||||||
|
{
|
||||||
|
onStart: (progress) => {
|
||||||
|
setState((prev) => prev && { ...prev, progress });
|
||||||
|
},
|
||||||
|
onProcessing: ({ id, name, progress }) => {
|
||||||
|
setState(
|
||||||
|
(prev) =>
|
||||||
|
prev && {
|
||||||
|
...prev,
|
||||||
|
progress,
|
||||||
|
currentName: name || `隧道 #${id}`,
|
||||||
|
rows: { ...prev.rows, [id]: { status: "running" } },
|
||||||
|
},
|
||||||
|
);
|
||||||
|
},
|
||||||
|
onItem: ({ result: item, progress }) => {
|
||||||
|
setState(
|
||||||
|
(prev) =>
|
||||||
|
prev && {
|
||||||
|
...prev,
|
||||||
|
progress,
|
||||||
|
currentName: undefined,
|
||||||
|
rows: {
|
||||||
|
...prev.rows,
|
||||||
|
[item.id]: {
|
||||||
|
status: item.success ? "success" : "failed",
|
||||||
|
reason: item.reason,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
controller.signal,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (controller.signal.aborted) return null;
|
||||||
|
setState(
|
||||||
|
(prev) => prev && { ...prev, running: false, currentName: undefined },
|
||||||
|
);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
} catch (error) {
|
||||||
|
if (controller.signal.aborted) return null;
|
||||||
|
setState(
|
||||||
|
(prev) =>
|
||||||
|
prev && {
|
||||||
|
...prev,
|
||||||
|
running: false,
|
||||||
|
currentName: undefined,
|
||||||
|
error: extractApiErrorMessage(
|
||||||
|
error,
|
||||||
|
"下发进度中断,请检查未确认的结果后再重试",
|
||||||
|
),
|
||||||
|
rows: Object.fromEntries(
|
||||||
|
Object.entries(prev.rows).map(([id, row]) => [
|
||||||
|
id,
|
||||||
|
row.status === "queued" || row.status === "running"
|
||||||
|
? { status: "unknown" }
|
||||||
|
: row,
|
||||||
|
]),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
throw error;
|
||||||
|
} finally {
|
||||||
|
if (abortRef.current === controller) abortRef.current = null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const clear = () => {
|
||||||
|
if (!abortRef.current) setState(null);
|
||||||
|
};
|
||||||
|
|
||||||
|
return { state, run, clear };
|
||||||
|
}
|
||||||
@@ -1316,25 +1316,30 @@ export default function UserPage() {
|
|||||||
>
|
>
|
||||||
<Card className="overflow-hidden h-full">
|
<Card className="overflow-hidden h-full">
|
||||||
<CardHeader className="pb-2 md:pb-2">
|
<CardHeader className="pb-2 md:pb-2">
|
||||||
<OrderHandle />
|
<div className="flex flex-wrap items-start gap-2 w-full">
|
||||||
<div className="flex justify-between items-start w-full">
|
<div className="flex items-center gap-3 grow basis-44 min-w-0">
|
||||||
<div className="flex items-center gap-3 flex-1 min-w-0">
|
|
||||||
<div className="flex items-center justify-center shrink-0 w-10 h-10 rounded-full bg-primary text-white font-bold text-sm relative">
|
<div className="flex items-center justify-center shrink-0 w-10 h-10 rounded-full bg-primary text-white font-bold text-sm relative">
|
||||||
{(user.name || user.user).slice(0, 2).toUpperCase()}
|
{(user.name || user.user).slice(0, 2).toUpperCase()}
|
||||||
<span
|
<span
|
||||||
className={`absolute bottom-0 right-0 w-3 h-3 border-2 border-white dark:border-zinc-900 rounded-full ${userStatus.color === "success" ? "bg-success" : "bg-danger"}`}
|
className={`absolute bottom-0 right-0 w-3 h-3 border-2 border-white dark:border-zinc-900 rounded-full ${userStatus.color === "success" ? "bg-success" : "bg-danger"}`}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex flex-col min-w-0">
|
<div className="flex flex-col flex-1 min-w-0">
|
||||||
<h3 className="font-semibold text-foreground truncate text-sm">
|
<div className="flex items-center gap-1 min-w-0">
|
||||||
|
<OrderHandle />
|
||||||
|
<h3
|
||||||
|
className="font-semibold text-foreground truncate text-sm"
|
||||||
|
title={user.name || user.user}
|
||||||
|
>
|
||||||
{user.name || user.user}
|
{user.name || user.user}
|
||||||
</h3>
|
</h3>
|
||||||
|
</div>
|
||||||
<p className="text-xs text-default-500 truncate">
|
<p className="text-xs text-default-500 truncate">
|
||||||
@{user.user}
|
@{user.user}
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex items-center gap-1.5 ml-2">
|
<div className="flex flex-wrap items-center gap-1.5 ml-auto max-w-full shrink-0">
|
||||||
<Chip
|
<Chip
|
||||||
className="text-xs"
|
className="text-xs"
|
||||||
color={userStatus.color}
|
color={userStatus.color}
|
||||||
|
|||||||
Reference in New Issue
Block a user