Files
ARTEX/traffic/proxy_test.go
BaaRai fd9e601026 Mask the global proxy in GET /api/traffic
With traffic capture off the proxy field returned the saved global egress
proxy URL, credentials included, although GET /api/settings masks it. Show
the masked placeholder instead (a round trip through PUT /api/settings keeps
the stored proxy) and keep the recording proxy address with capture on.
Proxy validation errors no longer quote the submitted address.
2026-12-04 01:29:19 +01:00

104 lines
3.3 KiB
Go

package traffic
import (
"strings"
"testing"
)
func TestValidateProxyURL(t *testing.T) {
ok := []string{
"http://127.0.0.1:8080",
"https://proxy.example.com:3128",
"socks5://10.0.0.1:1080",
"socks5://user:pass@10.0.0.1:1080",
}
for _, raw := range ok {
if _, err := ValidateProxyURL(raw); err != nil {
t.Errorf("ValidateProxyURL(%q) unexpected error: %v", raw, err)
}
}
bad := []string{
"127.0.0.1:8080", // no scheme
"ftp://host:21", // unsupported scheme
"http://", // no host
"socks4://10.0.0.1:1080", // unsupported scheme
}
for _, raw := range bad {
if _, err := ValidateProxyURL(raw); err == nil {
t.Errorf("ValidateProxyURL(%q) expected error, got nil", raw)
}
}
}
// Validation errors reach the PUT /api/settings response and logs: they never
// quote the address, which may carry a password.
func TestValidateProxyURLErrorsOmitAddress(t *testing.T) {
for _, raw := range []string{
"http://user:pw-secret@", // no host
"user:pw-secret@proxy.example:3128", // no scheme, parsed as scheme "user"
"http://user:pw-secret@bad host:3128", // url.Parse error, which quotes its input
"ftp://user:pw-secret@proxy.example:21", // unsupported scheme
"//user:pw-secret@proxy.example:3128", // no scheme
} {
_, err := ValidateProxyURL(raw)
if err == nil {
t.Fatalf("ValidateProxyURL(%q) expected error", raw)
}
if strings.Contains(err.Error(), "pw-secret") || strings.Contains(err.Error(), "user") {
t.Fatalf("ValidateProxyURL(%q) error quotes the address: %v", raw, err)
}
}
}
func TestSetUpstreamProxyStoreClear(t *testing.T) {
tr := &Traffic{}
if got := tr.upstream.Load(); got != nil {
t.Fatalf("initial upstream = %v, want nil", got)
}
if err := tr.SetUpstreamProxy("socks5://user:pass@10.0.0.1:1080"); err != nil {
t.Fatalf("SetUpstreamProxy: %v", err)
}
u := tr.upstream.Load()
if u == nil || u.Scheme != "socks5" || u.Host != "10.0.0.1:1080" {
t.Fatalf("stored upstream = %v, want socks5://10.0.0.1:1080", u)
}
if pw, _ := u.User.Password(); u.User.Username() != "user" || pw != "pass" {
t.Fatalf("stored upstream lost credentials: %v", u)
}
// Empty clears back to direct.
if err := tr.SetUpstreamProxy(" "); err != nil {
t.Fatalf("SetUpstreamProxy(clear): %v", err)
}
if got := tr.upstream.Load(); got != nil {
t.Fatalf("after clear upstream = %v, want nil", got)
}
// Invalid value is rejected and does not mutate current state.
if err := tr.SetUpstreamProxy("nope://x"); err == nil {
t.Fatal("SetUpstreamProxy(invalid) expected error")
}
if got := tr.upstream.Load(); got != nil {
t.Fatalf("invalid set mutated upstream to %v, want nil", got)
}
}
func TestProxyAddr(t *testing.T) {
cases := []struct {
addr string
want string
}{
// Bare :port means "bind all interfaces" — the legacy default. The URL
// agents consume must still point at loopback so they reach the local proxy.
{":8788", "http://127.0.0.1:8788"},
// Explicit loopback — the current default since #129 (open proxy exposure).
{"127.0.0.1:8788", "http://127.0.0.1:8788"},
// Explicit all-interface bind is still supported (remote capture via SSH).
{"0.0.0.0:8788", "http://0.0.0.0:8788"},
}
for _, c := range cases {
got := (&Traffic{addr: c.addr}).ProxyAddr()
if got != c.want {
t.Errorf("ProxyAddr(%q) = %q, want %q", c.addr, got, c.want)
}
}
}