mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-08-20 18:11:00 +00:00
380aff4d82
* Add remote routing URL support * Harden remote routing refresh * fix(sub): harden remote routing fetch and accept Mihomo src rule flag Remote routing bytes reach the YAML/JSON parsers from goroutines that run outside Gin's recovery, so a parser panic on crafted input would take down the whole panel. Contain it in fetch() (a panic now degrades to a failed refresh that keeps the last-good value and releases the in-flight slot) and start the refresh, cache-load and startup-warm goroutines through common.GoRecover like the other background workers. The route-graph validator only skipped a trailing no-resolve flag, so a valid Mihomo rule like IP-CIDR,x,DIRECT,no-resolve,src was rejected as an unknown target; skip both option flags. Also deduplicate the HTTPS-source classification into common.ParseRemoteRoutingURL so the save-time validator and the resolver can never drift (internal/sub imports internal/web/service, so the copy existed only to avoid the import cycle), move the test-only mergeRemoteClashRulesYAML helper into the test file, and trim oversized comment blocks. --------- Co-authored-by: Duxxie <yelloduxx@users.noreply.github.com> Co-authored-by: Sanaei <ho3ein.sanaei@gmail.com>
56 lines
2.1 KiB
Go
56 lines
2.1 KiB
Go
package common
|
|
|
|
import "testing"
|
|
|
|
func TestEnsureURLScheme(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
in string
|
|
want string
|
|
}{
|
|
{"empty", "", ""},
|
|
{"whitespace only", " ", ""},
|
|
{"bare telegram handle", "t.me/xui_support", "https://t.me/xui_support"},
|
|
{"bare domain with path", "example.com/help", "https://example.com/help"},
|
|
{"already https", "https://t.me/xui_support", "https://t.me/xui_support"},
|
|
{"already http", "http://example.com", "http://example.com"},
|
|
{"telegram deep link", "tg://resolve?domain=xui_support", "tg://resolve?domain=xui_support"},
|
|
{"mailto", "mailto:support@example.com", "mailto:support@example.com"},
|
|
{"tel", "tel:+1234567890", "tel:+1234567890"},
|
|
{"trims whitespace", " t.me/xui_support ", "https://t.me/xui_support"},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if got := EnsureURLScheme(tt.in); got != tt.want {
|
|
t.Errorf("EnsureURLScheme(%q) = %q, want %q", tt.in, got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestParseRemoteRoutingURLKeepsInlineCompatibility(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
wantSource string
|
|
wantRemote bool
|
|
wantErr bool
|
|
}{
|
|
{name: "deeplink stays inline", input: "happ://routing/onadd/abc"},
|
|
{name: "plain HTTP stays inline", input: "http://example.com/rules"},
|
|
{name: "multiline stays inline", input: "https://example.com/rules\nMATCH,PROXY"},
|
|
{name: "HTTPS source", input: " https://example.com/rules#ignored ", wantSource: "https://example.com/rules", wantRemote: true},
|
|
{name: "uppercase scheme", input: "HTTPS://example.com/rules", wantSource: "https://example.com/rules", wantRemote: true},
|
|
{name: "credentials rejected", input: "https://user:pass@example.com/rules", wantRemote: true, wantErr: true},
|
|
{name: "missing host rejected", input: "https:///rules", wantRemote: true, wantErr: true},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, remote, err := ParseRemoteRoutingURL(tt.input)
|
|
if got != tt.wantSource || remote != tt.wantRemote || (err != nil) != tt.wantErr {
|
|
t.Fatalf("got=%q remote=%v err=%v", got, remote, err)
|
|
}
|
|
})
|
|
}
|
|
}
|