mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-11-25 20:15:01 +03:00
b39ac90f1e
Since 3.11.0 the panel asks outside IP-echo services for its public address every five minutes, and on a server with no IPv6 it retried the IPv6 services forever. Operators with static addresses had no way to stop it (#7176). Add four General settings: the refresh interval (0 keeps the first answer), a switch for IPv6 detection, and a fixed IPv4 and IPv6 address that replace the lookup. The defaults keep today's behaviour. Clearing an override forgets the earlier success, so the next lookup is not timed from before the pin.
177 lines
6.2 KiB
Go
177 lines
6.2 KiB
Go
package entity
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestCheckValidSmtpFrom(t *testing.T) {
|
|
base := func() *AllSetting {
|
|
return &AllSetting{WebPort: 2053, SubPort: 2096}
|
|
}
|
|
|
|
for _, v := range []string{"", "panel@example.com"} {
|
|
s := base()
|
|
s.SmtpFrom = v
|
|
if err := s.CheckValid(); err != nil {
|
|
t.Errorf("CheckValid with smtpFrom=%q: unexpected error %v", v, err)
|
|
}
|
|
}
|
|
|
|
for _, v := range []string{
|
|
"not-an-address",
|
|
"panel@example.com\r\nBcc: evil@example.com",
|
|
"a@b\nSubject: injected",
|
|
} {
|
|
s := base()
|
|
s.SmtpFrom = v
|
|
if err := s.CheckValid(); err == nil {
|
|
t.Errorf("CheckValid with smtpFrom=%q: want error, got nil", v)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCheckValidWildcardListenPortConflict(t *testing.T) {
|
|
s := &AllSetting{WebPort: 2053, SubPort: 2053, WebListen: "0.0.0.0", SubListen: ""}
|
|
if err := s.CheckValid(); err == nil {
|
|
t.Error("CheckValid must reject the same port bound on 0.0.0.0 and \"\" (both wildcard)")
|
|
}
|
|
|
|
ok := &AllSetting{WebPort: 2053, SubPort: 2053, WebListen: "127.0.0.1", SubListen: "192.168.1.1"}
|
|
if err := ok.CheckValid(); err != nil {
|
|
t.Errorf("distinct specific listens on the same port should be allowed: %v", err)
|
|
}
|
|
}
|
|
|
|
// The allowlist and the trusted-proxy list share one validator, so this also
|
|
// pins that each list still reports its own message (#5378).
|
|
func TestCheckValidIPOrCIDRLists(t *testing.T) {
|
|
base := func() *AllSetting {
|
|
return &AllSetting{WebPort: 2053, SubPort: 2096}
|
|
}
|
|
|
|
for _, v := range []string{"", "203.0.113.10", "198.51.100.0/24", " 203.0.113.10 , 2001:db8::/32 ", "203.0.113.10,,"} {
|
|
s := base()
|
|
s.IpLimitAllowlist = v
|
|
if err := s.CheckValid(); err != nil {
|
|
t.Errorf("ipLimitAllowlist=%q: unexpected error %v", v, err)
|
|
}
|
|
}
|
|
|
|
for _, v := range []string{"nonsense", "203.0.113.10/33", "203.0.113.10, oops"} {
|
|
s := base()
|
|
s.IpLimitAllowlist = v
|
|
err := s.CheckValid()
|
|
if err == nil {
|
|
t.Errorf("ipLimitAllowlist=%q: want error, got nil", v)
|
|
continue
|
|
}
|
|
if !strings.Contains(err.Error(), "IP limit allowlist entry is not valid:") {
|
|
t.Errorf("ipLimitAllowlist=%q: error %q does not name the setting", v, err)
|
|
}
|
|
}
|
|
|
|
s := base()
|
|
s.TrustedProxyCIDRs = "127.0.0.1/32, bogus"
|
|
err := s.CheckValid()
|
|
if err == nil || !strings.Contains(err.Error(), "trusted proxy CIDR is not valid: bogus") {
|
|
t.Errorf("trustedProxyCIDRs error = %v, want it to name the trusted-proxy list and the bad entry", err)
|
|
}
|
|
}
|
|
|
|
// A schedule the scheduler cannot parse used to be saved anyway, and the report
|
|
// or sync job then silently never ran.
|
|
func TestCheckValidSchedules(t *testing.T) {
|
|
set := map[string]func(*AllSetting, string){
|
|
"tgRunTime": func(s *AllSetting, v string) { s.TgRunTime = v },
|
|
"discordRunTime": func(s *AllSetting, v string) { s.DiscordRunTime = v },
|
|
"ldapSyncCron": func(s *AllSetting, v string) { s.LdapSyncCron = v },
|
|
}
|
|
for name, apply := range set {
|
|
for _, v := range []string{"", "@daily", "30 8 * * *", "0 30 8 * * *", "@every 1m"} {
|
|
s := &AllSetting{WebPort: 2053, SubPort: 2096}
|
|
apply(s, v)
|
|
if err := s.CheckValid(); err != nil {
|
|
t.Errorf("%s=%q: unexpected error %v", name, v, err)
|
|
}
|
|
}
|
|
s := &AllSetting{WebPort: 2053, SubPort: 2096}
|
|
apply(s, "every morning")
|
|
err := s.CheckValid()
|
|
if err == nil || !strings.Contains(err.Error(), name) {
|
|
t.Errorf("%s=%q: want an error naming %s, got %v", name, "every morning", name, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// The middleware only strips the port, so a saved scheme, port or path could
|
|
// never match the Host header and locked the panel behind 403.
|
|
func TestCheckValidNormalizesPanelAndSubDomains(t *testing.T) {
|
|
tests := []struct {
|
|
name, in, want, wantErr string
|
|
}{
|
|
{"empty", "", "", ""},
|
|
{"capitals, spaces and root dot", " Panel.Example.COM. ", "panel.example.com", ""},
|
|
{"IPv6 address", "2001:DB8::1", "2001:db8::1", ""},
|
|
{"scheme", "https://panel.example.com", "", "must be a bare host name without scheme, port or path: https://panel.example.com"},
|
|
{"port", "panel.example.com:8443", "", "must be a bare host name without scheme, port or path: panel.example.com:8443"},
|
|
{"path", "panel.example.com/x", "", "must be a bare host name without scheme, port or path: panel.example.com/x"},
|
|
{"inner space", "panel example.com", "", "must be a bare host name without scheme, port or path: panel example.com"},
|
|
{"bracketed IPv6", "[2001:db8::1]", "", "must be a bare host name without scheme, port or path: [2001:db8::1]"},
|
|
}
|
|
for _, tc := range tests {
|
|
for _, field := range []string{"web domain", "subscription domain"} {
|
|
t.Run(field+"/"+tc.name, func(t *testing.T) {
|
|
s := &AllSetting{WebPort: 2053, SubPort: 2096}
|
|
target := &s.WebDomain
|
|
if field == "subscription domain" {
|
|
target = &s.SubDomain
|
|
}
|
|
*target = tc.in
|
|
err := s.CheckValid()
|
|
if tc.wantErr != "" {
|
|
if err == nil || err.Error() != field+" "+tc.wantErr {
|
|
t.Fatalf("CheckValid(%q) error = %v, want %q", tc.in, err, field+" "+tc.wantErr)
|
|
}
|
|
return
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("CheckValid(%q): unexpected error %v", tc.in, err)
|
|
}
|
|
if *target != tc.want {
|
|
t.Errorf("CheckValid(%q) saved %q, want %q", tc.in, *target, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
// The override is displayed on the overview and used in backup filenames, so a
|
|
// hostname, wrong-family or zoned address must be rejected at save time.
|
|
func TestCheckValidPublicIPOverride(t *testing.T) {
|
|
cases := []struct {
|
|
v4, v6, want string
|
|
}{
|
|
{"", "", ""},
|
|
{"203.0.113.9", "2001:db8::1", ""},
|
|
{"example.com", "", "public IPv4 override is not a valid IPv4 address: example.com"},
|
|
{"2001:db8::1", "", "public IPv4 override is not a valid IPv4 address: 2001:db8::1"},
|
|
{"", "203.0.113.9", "public IPv6 override is not a valid IPv6 address: 203.0.113.9"},
|
|
{"", "fe80::1%eth0", "public IPv6 override is not a valid IPv6 address: fe80::1%eth0"},
|
|
{"", "::ffff:203.0.113.9", "public IPv6 override is not a valid IPv6 address: ::ffff:203.0.113.9"},
|
|
}
|
|
for _, c := range cases {
|
|
s := &AllSetting{WebPort: 2053, SubPort: 2096, PublicIpv4Override: c.v4, PublicIpv6Override: c.v6}
|
|
err := s.CheckValid()
|
|
if c.want == "" {
|
|
if err != nil {
|
|
t.Errorf("v4=%q v6=%q: unexpected error %v", c.v4, c.v6, err)
|
|
}
|
|
continue
|
|
}
|
|
if err == nil || err.Error() != c.want {
|
|
t.Errorf("v4=%q v6=%q: err=%v, want %q", c.v4, c.v6, err, c.want)
|
|
}
|
|
}
|
|
}
|