fix: 保留用户/规则高级设置并增强节点运行时恢复 (#468)

* fix: 用户编辑时 maxConn 字段未正确回填

- User 接口添加 maxConn 类型定义
- handleEdit 中回填 maxConn 值
- normalizeUserItem 添加 maxConn 字段处理

解决编辑用户时最大连接数显示为 0 的问题

* fix: 保留转发设置并增强节点运行时恢复
This commit is contained in:
sagit
2026-04-25 18:12:13 +08:00
committed by GitHub
parent 9b923a2d0b
commit 3f374df724
30 changed files with 744 additions and 123 deletions
@@ -96,6 +96,7 @@ func TestMaxConnLimit(t *testing.T) {
"remoteAddr": "1.1.1.1:443",
"strategy": "fifo",
"maxConn": 42,
"proxyProtocol": 2,
}
body, err := json.Marshal(payload)
if err != nil {
@@ -120,6 +121,47 @@ func TestMaxConnLimit(t *testing.T) {
t.Fatalf("get forward ID: %v", err)
}
listOut := requestContractEnvelope(t, router, adminToken, "/api/v1/forward/list", nil)
if listOut.Code != 0 {
t.Fatalf("expected /forward/list success, got code=%d msg=%s", listOut.Code, listOut.Msg)
}
rows := mustContractSlice(t, listOut.Data, "forward list")
var target map[string]interface{}
for _, row := range rows {
item, ok := row.(map[string]interface{})
if !ok {
t.Fatalf("expected forward item to be object, got %T", row)
}
idVal, ok := item["id"].(float64)
if !ok {
t.Fatalf("expected forward id to be float64, got %T", item["id"])
}
if int64(idVal) == forwardID {
target = item
break
}
}
if target == nil {
t.Fatalf("forward %d not found in /forward/list response", forwardID)
}
maxConnVal, ok := target["maxConn"].(float64)
if !ok {
t.Fatalf("expected maxConn to be float64, got %T (%v)", target["maxConn"], target["maxConn"])
}
if int(maxConnVal) != 42 {
t.Fatalf("expected maxConn 42 in /forward/list, got %v", maxConnVal)
}
proxyProtocolVal, ok := target["proxyProtocol"].(float64)
if !ok {
t.Fatalf("expected proxyProtocol to be float64, got %T (%v)", target["proxyProtocol"], target["proxyProtocol"])
}
if int(proxyProtocolVal) != 2 {
t.Fatalf("expected proxyProtocol 2 in /forward/list, got %v", proxyProtocolVal)
}
commandMu.Lock()
defer commandMu.Unlock()
@@ -349,9 +349,9 @@ func TestBackupExportImportRestoreContracts(t *testing.T) {
tunnelID := mustLastInsertID(t, r, "backup-forward-tunnel")
if err := r.DB().Exec(`
INSERT INTO forward(user_id, user_name, name, tunnel_id, remote_addr, strategy, in_flow, out_flow, created_time, updated_time, status, inx)
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`, 1, "admin_user", "backup-forward", tunnelID, "127.0.0.1:9000", "fifo", 0, 0, now, now, 1, 88).Error; err != nil {
INSERT INTO forward(user_id, user_name, name, tunnel_id, remote_addr, strategy, in_flow, out_flow, created_time, updated_time, status, inx, proxy_protocol)
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`, 1, "admin_user", "backup-forward", tunnelID, "127.0.0.1:9000", "fifo", 0, 0, now, now, 1, 88, 2).Error; err != nil {
t.Fatalf("seed forward for backup: %v", err)
}
forwardID := mustLastInsertID(t, r, "backup-forward")
@@ -412,6 +412,9 @@ func TestBackupExportImportRestoreContracts(t *testing.T) {
if !ok {
t.Fatalf("expected forwardPorts for forward %d in payload", forwardID)
}
if proxyProtocol, ok := forwardMap["proxyProtocol"].(float64); !ok || int(proxyProtocol) != 2 {
t.Fatalf("expected exported proxyProtocol 2 for forward %d, got %v", forwardID, forwardMap["proxyProtocol"])
}
for _, p := range portsRaw {
portMap, ok := p.(map[string]interface{})
if !ok {
@@ -475,6 +478,14 @@ func TestBackupExportImportRestoreContracts(t *testing.T) {
t.Fatalf("expected forward_port node=%d port=%d after import, got %v", nodeID, port, after)
}
}
var proxyProtocol int
if err := r.DB().Raw(`SELECT proxy_protocol FROM forward WHERE id = ?`, forwardID).Row().Scan(&proxyProtocol); err != nil {
t.Fatalf("query proxy_protocol after import: %v", err)
}
if proxyProtocol != 2 {
t.Fatalf("expected proxy_protocol 2 after import, got %d", proxyProtocol)
}
})
t.Run("backup export tolerates nullable legacy tunnel chain fields", func(t *testing.T) {
@@ -0,0 +1,59 @@
package contract_test
import (
"testing"
"time"
"go-backend/internal/auth"
)
func TestUserListReturnsMaxConn(t *testing.T) {
secret := "contract-jwt-secret"
router, repo := setupContractRouter(t, secret)
now := time.Now().UnixMilli()
if err := repo.DB().Exec(`
INSERT INTO user(id, user, pwd, role_id, exp_time, flow, in_flow, out_flow, flow_reset_time, num, max_conn, created_time, updated_time, status)
VALUES(2, 'max_conn_user', 'pwd', 1, 2727251700000, 99999, 0, 0, 1, 10, 37, ?, ?, 1)
`, now, now).Error; err != nil {
t.Fatalf("insert user: %v", err)
}
adminToken, err := auth.GenerateToken(1, "admin_user", 0, secret)
if err != nil {
t.Fatalf("generate admin token: %v", err)
}
out := requestContractEnvelope(t, router, adminToken, "/api/v1/user/list", map[string]interface{}{})
if out.Code != 0 {
t.Fatalf("expected /user/list success, got code=%d msg=%s", out.Code, out.Msg)
}
rows := mustContractSlice(t, out.Data, "user list")
var target map[string]interface{}
for _, row := range rows {
item, ok := row.(map[string]interface{})
if !ok {
t.Fatalf("expected user item to be object, got %T", row)
}
idVal, ok := item["id"].(float64)
if !ok {
t.Fatalf("expected user id to be float64, got %T", item["id"])
}
if int64(idVal) == 2 {
target = item
break
}
}
if target == nil {
t.Fatalf("user 2 not found in /user/list response")
}
maxConnVal, ok := target["maxConn"].(float64)
if !ok {
t.Fatalf("expected maxConn to be float64, got %T (%v)", target["maxConn"], target["maxConn"])
}
if int(maxConnVal) != 37 {
t.Fatalf("expected maxConn 37 in /user/list, got %v", maxConnVal)
}
}