fix(sub): keep the salamander warning state one entry per inbound

genHysteriaLink remembered which salamander settings it had warned
about in a set keyed by the setting names themselves, which a node
chooses with no bound on length or number and the set never forgot,
and it logged the names verbatim into the in-memory log buffer. A node
renaming a setting on every poll grew the master's memory by twice the
name's size per subscription fetch until the process ran out.

The set is keyed by inbound id and holds a digest of the names, the
warning repeats only when that set changes, and it logs at most eight
names cut to 32 characters.
This commit is contained in:
MHSanaei
2026-11-01 00:45:21 +01:00
parent bebe1b911d
commit c2250dbe57
2 changed files with 46 additions and 3 deletions
+40
View File
@@ -1,6 +1,7 @@
package sub
import (
"fmt"
"reflect"
"strconv"
"strings"
@@ -86,3 +87,42 @@ func TestGenHysteriaLinkGeckoStillWarnsOnExtraKeys(t *testing.T) {
t.Fatal("leftover salamander key did not warn alongside the gecko export")
}
}
// The names may be a node's: 50 renames of one oversized setting must leave
// one fixed-size entry for the inbound and warning lines of bounded length.
func TestGenHysteriaLinkSalamanderWarningStateIsBounded(t *testing.T) {
const id = 910004
for i := range 50 {
name := strconv.Itoa(i) + strings.Repeat("k", 4096)
in := &model.Inbound{
Id: id, Listen: "203.0.113.1", Port: 443, Protocol: model.Hysteria,
Settings: `{"version":2,"clients":[{"auth":"secret","email":"user"}]}`,
StreamSettings: `{"security":"tls","finalmask":{"udp":[{"type":"salamander","settings":{"password":"pw","` + name + `":1}}]}}`,
}
(&SubService{}).genHysteriaLink(in, "user")
}
entries := 0
salamanderWarningSeen.Range(func(key, _ any) bool {
if strings.HasPrefix(fmt.Sprint(key), strconv.Itoa(id)) {
entries++
}
return true
})
if entries != 1 {
t.Fatalf("salamanderWarningSeen holds %d entries for one inbound, want 1", entries)
}
needle := "inbound " + strconv.Itoa(id) + ": salamander settings"
found := 0
for _, line := range logger.GetLogs(100, "warning") {
if !strings.Contains(line, needle) {
continue
}
found++
if len(line) > 512 {
t.Fatalf("warning line is %d bytes long, want at most 512", len(line))
}
}
if found == 0 {
t.Fatal("renamed salamander settings did not warn at all")
}
}