Files
mirror-3x-ui/internal/web/service/setting_factory_defaults_test.go
T
Hamed a9d22498c2 feat(traffic): separate depletion restarts from client disables (#7267)
One switch decided two things: whether Xray restarts when a client is disabled or deleted by hand, and whether it restarts when a client runs out of quota or expires. A panel that takes many API deletes had to choose between a restart on each of them and leaving depleted clients' open connections alive.

Add a switch for the quota and expiry case. It inherits the old switch until it is saved, and older settings clients may omit it. A depletion asks for one restart per affected running core per committed batch: the local core when a local inbound served the client, and each affected node through the node API. A master whose nodes alone served the client is not restarted. With the new switch off, a depleted client is still removed from the running core and from its config snapshot, so a later reconcile does not restart for it either.

Also add a node traffic fetch timeout setting (1 to 30 seconds, default 4).

Run on a panel with real cores: with the old switch off and the new one on, a stream through a client that crossed its quota was cut within one poll and the lifecycle log names client_depletion; with them swapped, the stream and the core survived. A depletion a node detects itself follows that node's own switches.

Refs #7145
2026-11-29 18:25:19 +03:30

89 lines
2.0 KiB
Go

package service
import (
"reflect"
"strings"
"testing"
"github.com/mhsanaei/3x-ui/v3/internal/util/reflect_util"
"github.com/mhsanaei/3x-ui/v3/internal/web/entity"
)
func allSettingJSONTags(t *testing.T) map[string]bool {
t.Helper()
tags := make(map[string]bool)
for _, field := range reflect_util.GetFields(reflect.TypeFor[entity.AllSetting]()) {
if tag, _, _ := strings.Cut(field.Tag.Get("json"), ","); tag != "" {
tags[tag] = true
}
}
return tags
}
func TestGetFactoryDefaultsExposesBrowserSafeKeys(t *testing.T) {
defaults := (&SettingService{}).GetFactoryDefaults()
tests := []struct {
key string
want string
}{
{key: "webPort", want: "2053"},
{key: "subPort", want: "2096"},
{key: restartOnDepletedKey, want: "true"},
{key: nodeFetchTimeoutKey, want: "4"},
}
for _, tc := range tests {
t.Run(tc.key, func(t *testing.T) {
got, ok := defaults[tc.key]
if !ok {
t.Fatalf("expected key %q in factory defaults", tc.key)
}
if got != tc.want {
t.Errorf("factory default for %q = %q, want %q", tc.key, got, tc.want)
}
})
}
}
func TestGetFactoryDefaultsOmitsSensitiveMaterial(t *testing.T) {
defaults := (&SettingService{}).GetFactoryDefaults()
for _, key := range []string{
"secret",
"panelGuid",
"nodeMtlsCaCertPem",
"nodeMtlsCaKeyPem",
"nodeMtlsClientCertPem",
"nodeMtlsClientKeyPem",
"nodeMtlsClientCertSha256",
"xrayTemplateConfig",
"tgBotToken",
"twoFactorToken",
"ldapPassword",
"smtpPassword",
"discordBotToken",
} {
t.Run(key, func(t *testing.T) {
if _, ok := defaults[key]; ok {
t.Errorf("factory defaults must not expose %q", key)
}
})
}
}
func TestGetFactoryDefaultsInvariant(t *testing.T) {
defaults := (&SettingService{}).GetFactoryDefaults()
tags := allSettingJSONTags(t)
for key := range defaults {
t.Run(key, func(t *testing.T) {
if !tags[key] {
t.Errorf("key %q is not an entity.AllSetting json tag", key)
}
if factoryDefaultSecretKeys[key] {
t.Errorf("key %q is in the credential deny-list and must not be returned", key)
}
})
}
}