mirror of
https://gitcode.com/JianFeeeee/webui4frpc.git
synced 2026-10-02 23:24:00 +00:00
从线上三节点(192.168.2.{30,106,60})的日志里挖出四个问题,本轮修三个。
## 1. 停用的转发会复活(功能性缺陷,实测仍在发生)
线上现象:`minecraft` 在 store 里 disabled=1,worker 却仍在跑,今天
09:46 还在刷 `connect to local service [192.168.2.60:25565]: connection
refused` —— 对着一个用户刻意没启的本地服务死刷。同时三台 logs 里躺着
13956 / 3769 条 `proxy [x] already exists`,每 33 秒一轮。
四处叠加导致:
- stopForward 先读 link,再 SetLinkDisabled(true),然后把**改之前**的
副本交给 RevokeTask ⇒ published task 带 disabled=false(实测 id/flag
都对不上:topology 里 link.id=223,store 里同一行是 199)
- ClaimFn **无条件** SetLinkDisabled(...,false)。原意是"重新认领时清掉
停用标记",但启动 reconcile 只要 worker 不在就重新 Claim ⇒ 每次重启
都是"先清标记再起 worker"
- RevokeFn 停了 worker 却**没删 topology 条目**,条目活过 worker
- 于是下次重启 reconcile 看到"owned 但 worker 不在"→ 再次 Claim → 死循环
修法(把 disabled 的所有权交回两个用户动作):
- Claim **只读** disabled 决定要不要起 worker;为 true 时连 topology
条目一起摘掉,绝不复活
- 启动 reconcile 先按 store 跳过 disabled 的条目(省掉无谓的
claim→skip 往返)
- RevokeFn 除停 worker 外,同时 RemoveTopologyEntry —— 撤销必须是
完整退役,不能只是"停一下"
- stopForward 把 Disabled=true 随 task 发布出去,让持有该转发的节点
即使本地 store 行陈旧也能判断这次停用是用户主动的
## 2. 令牌轮转日志零信息量却占满磁盘
每轮固定 3 行(OnToken cycle=N / forward cycle=N / token-send -> 200),
2 轮/秒,实测本机 **355 行/分钟、7 天 357 万行**,把真事件全淹了。
同一份信息(cycle / lastSync / roundDelayMs / 成员存活)本来就能从
GET /api/manager/cluster/ring 结构化拿到。
加 W4F_DEBUG 开关(沿用项目既有 W4F_ 前缀约定):稳态三行降级为 debug、
默认关闭;**失败路径一律保留** —— 发送失败、陈旧令牌、非 2xx 正是别人
grep 的对象,静音它们是坏交易。实测同样 12 秒:36 行 → 4 行。
## 3. Link.ID 在 ReplaceLinks 之后必然失效
ReplaceLinks 是 DELETE + 重新 INSERT,sqlite 给每行**新的自增 id**。
任何在改写前捕获的 Link(典型:随 token 环跑的 Link)手里的 id 要么查无
此行,要么命中另一条转发 —— 实测捕获 alpha id=1,改写后新表是 3/4/5,
GetLink(1) 直接落空。
新增 LinkByTriple(local, remote, port) 按自然键查(业务代码本来就一律用
这个三元组标识转发),并把 claim/reconcile 切过去。查无行返回
(Link{}, false, nil) 而非 error:新建的转发没有行,应当照常启动。
## 4. homeagent_device 孤儿(已澄清,非独立缺陷)
它 disabled=1 且从不在 topology 里,是缺陷 1 的另一面(停用标记没进
token),随本次修复覆盖,无需单独处理。
## 测试
新增 4 个测试文件,重点是**验证测试本身抓得住 bug**:
- 临时回退 `ln.Disabled = true` 这行 → TestStopForwardPublishesDisabled-
FlagInRevokeTask 如期变红,还原后变绿
- ⚠️ 第一版回归测试只断言 store 层,是**假绿**:newTestHandler 的 Ring
为 nil,RevokeTask 那条(真正坏掉的)路根本没执行。补了带 ring 的
newRingTestHandler,直接断言**发布出去的 task 上的 flag**
- LinkByTriple 在 ReplaceLinks 前后保持稳定;GetLink(id) 的失效被固化成
一个可见的说明性测试
- 停用/start 往返、per-forward 停用不误伤兄弟转发
- 错误路径不静音、W4F_DEBUG 各种取值
go build / go vet / go test ./... 全绿,gofmt 干净。
320 lines
10 KiB
Go
320 lines
10 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"webui4frpc/internal/process"
|
|
"webui4frpc/internal/store"
|
|
)
|
|
|
|
// forwardsReq is the {local,remote,remotePort} natural key identifying a single
|
|
// forward. The triple is stable across canvas re-saves (unlike Link.ID, which
|
|
// ReplaceLinks wholesale-replaces) and matches HasTask's keying.
|
|
type forwardsReq struct {
|
|
Local string `json:"local"`
|
|
Remote string `json:"remote"`
|
|
RemotePort int `json:"remotePort"`
|
|
}
|
|
|
|
// groupReq selects a group for one-click start/stop on the forwards page.
|
|
type groupReq struct {
|
|
Group string `json:"group"`
|
|
}
|
|
|
|
// findLinkByTriple returns the link matching the (local,remote,remotePort)
|
|
// natural key, or ok=false.
|
|
func findLinkByTriple(s *store.Store, local, remote string, port int) (store.Link, bool) {
|
|
links, err := s.ListLinks()
|
|
if err != nil {
|
|
return store.Link{}, false
|
|
}
|
|
for _, l := range links {
|
|
if l.Local == local && l.Remote == remote && l.RemotePort == port {
|
|
return l, true
|
|
}
|
|
}
|
|
return store.Link{}, false
|
|
}
|
|
|
|
// findLinkFromTopology looks up a forward's local/remote/link data from the
|
|
// ring topology when it's not in the local SQLite store (forward owned by
|
|
// another node). Returns the store.Local, store.Remote, store.Link, and ok.
|
|
func (h *Handler) findLinkFromTopology(local, remote string, port int) (store.Local, store.Remote, store.Link, bool) {
|
|
if h.Ring == nil {
|
|
return store.Local{}, store.Remote{}, store.Link{}, false
|
|
}
|
|
snap := h.Ring.Snapshot()
|
|
for _, t := range snap.Topology {
|
|
if t.Local.Name == local && t.Remote.Name == remote && t.Link.RemotePort == port {
|
|
return t.Local, t.Remote, t.Link, true
|
|
}
|
|
}
|
|
return store.Local{}, store.Remote{}, store.Link{}, false
|
|
}
|
|
|
|
// startForward flips a forward to enabled and brings it up. A local-only
|
|
// forward restarts/starts its local frpc worker so the re-enabled proxy is
|
|
// rendered back in; a cluster (non-localOnly) forward is submitted to the ring
|
|
// so the lowest-load member claims and spawns it (plan §令牌环协议). SubmitTask
|
|
// is idempotent via HasTask. Falls back to ring topology when the forward
|
|
// exists only in the cluster (owned by another node, not in local store).
|
|
func (h *Handler) startForward(local, remote string, port int) error {
|
|
ln, ok := findLinkByTriple(h.Store, local, remote, port)
|
|
loc, lok := h.Store.GetLocal(local)
|
|
rem, rok := h.Store.GetRemote(remote)
|
|
if !ok || !lok || !rok {
|
|
// Fall back to ring topology for forwards owned by other nodes.
|
|
tLoc, tRem, tLn, tok := h.findLinkFromTopology(local, remote, port)
|
|
if !tok {
|
|
return store.ErrNotFound
|
|
}
|
|
ln, loc, rem = tLn, tLoc, tRem
|
|
}
|
|
_ = h.Store.SetLinkDisabled(local, remote, port, false)
|
|
ln.Disabled = false
|
|
if loc.LocalOnly {
|
|
if h.Process != nil {
|
|
key := process.WorkerKey(local, remote, port)
|
|
if _, has := h.Process.Status(key); has {
|
|
_ = h.Process.Restart(key)
|
|
} else {
|
|
_ = h.Process.Start(key)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
if h.Ring != nil {
|
|
h.Ring.SubmitTask(loc, rem, ln)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// stopForward flips a forward to disabled and tears it down. A local-only
|
|
// forward restarts its worker so renderRemote omits the proxy (sibling forwards
|
|
// on the same remote keep running — true per-forward stop); a cluster forward
|
|
// is revoked so the owning node cancels its worker and drops it from the
|
|
// topology (plan §任务撤销). RevokeTask is idempotent. Falls back to ring
|
|
// topology when the forward exists only in the cluster (owned by another node).
|
|
func (h *Handler) stopForward(local, remote string, port int) error {
|
|
ln, ok := findLinkByTriple(h.Store, local, remote, port)
|
|
loc, lok := h.Store.GetLocal(local)
|
|
rem, rok := h.Store.GetRemote(remote)
|
|
if !ok || !lok || !rok {
|
|
tLoc, tRem, tLn, tok := h.findLinkFromTopology(local, remote, port)
|
|
if !tok {
|
|
return store.ErrNotFound
|
|
}
|
|
ln, loc, rem = tLn, tLoc, tRem
|
|
} else {
|
|
_ = h.Store.SetLinkDisabled(local, remote, port, true)
|
|
}
|
|
// The revoke task travels to whichever node OWNS the forward, and that node
|
|
// re-reads the disabled flag from its own store before starting a worker —
|
|
// so the flag has to be set on every node that has a copy of this link, not
|
|
// just the one handling this request. Propagating Disabled on the task lets
|
|
// the owner's RevokeFn stop the worker even if its own store row is stale.
|
|
//
|
|
// This also fixes a latent inconsistency: `ln` was read BEFORE the
|
|
// SetLinkDisabled(true) above, so the link published into the token still
|
|
// carried disabled=false and got copied into the topology entry verbatim.
|
|
ln.Disabled = true
|
|
if loc.LocalOnly {
|
|
if h.Process != nil {
|
|
key := process.WorkerKey(local, remote, port)
|
|
if _, has := h.Process.Status(key); has {
|
|
_ = h.Process.Restart(key)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
if h.Ring != nil {
|
|
h.Ring.RevokeTask(loc, rem, ln)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// handleForwardsStart toggles one forward on.
|
|
func (h *Handler) handleForwardsStart(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
methodNotAllowed(w)
|
|
return
|
|
}
|
|
var req forwardsReq
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
http.Error(w, "parse json: "+err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
if req.Local == "" || req.Remote == "" || req.RemotePort <= 0 {
|
|
http.Error(w, "local/remote/remotePort required", http.StatusBadRequest)
|
|
return
|
|
}
|
|
if err := h.startForward(req.Local, req.Remote, req.RemotePort); err != nil {
|
|
http.Error(w, err.Error(), http.StatusNotFound)
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, map[string]any{"ok": true})
|
|
}
|
|
|
|
// handleForwardsStop toggles one forward off.
|
|
func (h *Handler) handleForwardsStop(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
methodNotAllowed(w)
|
|
return
|
|
}
|
|
var req forwardsReq
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
http.Error(w, "parse json: "+err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
if req.Local == "" || req.Remote == "" || req.RemotePort <= 0 {
|
|
http.Error(w, "local/remote/remotePort required", http.StatusBadRequest)
|
|
return
|
|
}
|
|
if err := h.stopForward(req.Local, req.Remote, req.RemotePort); err != nil {
|
|
http.Error(w, err.Error(), http.StatusNotFound)
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, map[string]any{"ok": true})
|
|
}
|
|
|
|
// handleForwardsGroupStart starts every forward in a group with one click.
|
|
func (h *Handler) handleForwardsGroupStart(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
methodNotAllowed(w)
|
|
return
|
|
}
|
|
var req groupReq
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
http.Error(w, "parse json: "+err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
links, err := h.Store.ListLinks()
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
for _, ln := range links {
|
|
if ln.Group != req.Group {
|
|
continue
|
|
}
|
|
_ = h.startForward(ln.Local, ln.Remote, ln.RemotePort)
|
|
}
|
|
writeJSON(w, http.StatusOK, map[string]any{"ok": true})
|
|
}
|
|
|
|
// handleForwardsGroupStop stops every forward in a group with one click.
|
|
func (h *Handler) handleForwardsGroupStop(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
methodNotAllowed(w)
|
|
return
|
|
}
|
|
var req groupReq
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
http.Error(w, "parse json: "+err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
links, err := h.Store.ListLinks()
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
for _, ln := range links {
|
|
if ln.Group != req.Group {
|
|
continue
|
|
}
|
|
_ = h.stopForward(ln.Local, ln.Remote, ln.RemotePort)
|
|
}
|
|
writeJSON(w, http.StatusOK, map[string]any{"ok": true})
|
|
}
|
|
|
|
// assignReq carries the group label to assign to a single forward (status
|
|
// page group chip edit). Empty Group clears the assignment (移出分组).
|
|
type assignReq struct {
|
|
Local string `json:"local"`
|
|
Remote string `json:"remote"`
|
|
RemotePort int `json:"remotePort"`
|
|
Group string `json:"group"`
|
|
}
|
|
|
|
// handleForwardsAssign changes the group label of a single forward. The
|
|
// status page group chip is the quick entry; the canvas port editor also
|
|
// carries a group field (both persist via the same store column). The local
|
|
// DB row is updated if present; the ring topology entry is always updated so
|
|
// the group label propagates to all nodes via the next token cycle.
|
|
func (h *Handler) handleForwardsAssign(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
methodNotAllowed(w)
|
|
return
|
|
}
|
|
var req assignReq
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
http.Error(w, "parse json: "+err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
if req.Local == "" || req.Remote == "" || req.RemotePort <= 0 {
|
|
http.Error(w, "local/remote/remotePort required", http.StatusBadRequest)
|
|
return
|
|
}
|
|
found := false
|
|
if _, ok := findLinkByTriple(h.Store, req.Local, req.Remote, req.RemotePort); ok {
|
|
found = true
|
|
if err := h.Store.SetLinkGroup(req.Local, req.Remote, req.RemotePort, req.Group); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
}
|
|
if h.Ring != nil {
|
|
if h.Ring.UpdateTopologyGroup(req.Local, req.Remote, req.RemotePort, req.Group) {
|
|
found = true
|
|
}
|
|
}
|
|
if !found {
|
|
http.Error(w, store.ErrNotFound.Error(), http.StatusNotFound)
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, map[string]any{"ok": true})
|
|
}
|
|
|
|
// handleForwardsGroupDelete dissolves a group: every forward in the named
|
|
// group is moved to 未分组 (grp=""). The group itself is not a stored entity
|
|
// — it exists only as a label on links — so clearing all members is the
|
|
// complete "delete". Clears both local store links and ring topology entries
|
|
// so the change syncs to all nodes.
|
|
func (h *Handler) handleForwardsGroupDelete(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
methodNotAllowed(w)
|
|
return
|
|
}
|
|
var req groupReq
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
http.Error(w, "parse json: "+err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
if req.Group == "" {
|
|
http.Error(w, "group required", http.StatusBadRequest)
|
|
return
|
|
}
|
|
links, err := h.Store.ListLinks()
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
for _, ln := range links {
|
|
if ln.Group != req.Group {
|
|
continue
|
|
}
|
|
_ = h.Store.SetLinkGroup(ln.Local, ln.Remote, ln.RemotePort, "")
|
|
}
|
|
// Also clear group on ring topology entries (forwards owned by other nodes).
|
|
if h.Ring != nil {
|
|
snap := h.Ring.Snapshot()
|
|
for _, t := range snap.Topology {
|
|
if t.Link.Group == req.Group {
|
|
h.Ring.UpdateTopologyGroup(t.Local.Name, t.Remote.Name, t.Link.RemotePort, "")
|
|
}
|
|
}
|
|
}
|
|
writeJSON(w, http.StatusOK, map[string]any{"ok": true})
|
|
}
|