s-ui/sub/jsonService_test.go
Amir Hakimnejad f7f5fc6667
sub: fall back to the SNI for the ws Host in links and JSON (#1334)
* sub: fall back to the SNI for the ws Host in links and JSON

With no Host set, sing-box sends the server address. Through a CDN clean IP
domain that Host matches no site, so the CDN answers 403. Clash already used
the SNI; links and the JSON subscription now do the same.

* sub: skip the default Host when TLS is off

Links and Clash already check it; the JSON path did not.

* sub: don't emit an empty host param in links
2026-11-05 18:11:02 +01:00

238 lines
8.9 KiB
Go

package sub
import (
"encoding/json"
"testing"
"github.com/alireza0/s-ui/database/model"
)
func runAddHTTPClients(t *testing.T, template string) (map[string]interface{}, map[string]interface{}) {
t.Helper()
var othersJson map[string]interface{}
if err := json.Unmarshal([]byte(template), &othersJson); err != nil {
t.Fatal(err)
}
route := map[string]interface{}{"final": "proxy"}
if ruleSet, ok := othersJson["rule_set"]; ok {
route["rule_set"] = ruleSet
}
jsonConfig := map[string]interface{}{}
(&JsonService{}).addHTTPClients(&jsonConfig, route, othersJson)
return jsonConfig, route
}
// A remote rule-set with no client of its own would use the implicit default,
// which sing-box 1.14 reports as deprecated in the client's log.
func TestAddHTTPClientsDeclaresDefault(t *testing.T) {
jsonConfig, route := runAddHTTPClients(t, `{
"rule_set": [
{"tag": "geosite-ir", "type": "remote", "format": "binary", "url": "https://e.com/a.srs"}
]
}`)
clients, ok := jsonConfig["http_clients"].([]interface{})
if !ok || len(clients) != 1 {
t.Fatalf("expected one declared http client, got %v", jsonConfig["http_clients"])
}
client, _ := clients[0].(map[string]interface{})
if client["tag"] != defaultHTTPClientTag {
t.Errorf("unexpected client: %v", client)
}
// A detour would be wrong here: sing-box refuses a detour to a plain direct
// outbound, and downloading over the default outbound is the intent.
if _, hasDetour := client["detour"]; hasDetour {
t.Errorf("the default download client must carry no detour, got %v", client)
}
if route["default_http_client"] != defaultHTTPClientTag {
t.Errorf("the route must point at it, got %v", route["default_http_client"])
}
}
// Rule-sets that name their own client need no default, and a config with no
// remote rule-set at all must not grow one.
func TestAddHTTPClientsLeavesConfigsAlone(t *testing.T) {
for name, template := range map[string]string{
"every rule-set has a client": `{
"rule_set": [
{"tag": "a", "type": "remote", "format": "binary", "url": "https://e.com/a.srs",
"http_client": {"detour": "proxy"}}
]
}`,
"local rule-set only": `{
"rule_set": [{"tag": "a", "type": "local", "format": "binary", "path": "/a.srs"}]
}`,
"no rule-set": `{}`,
} {
t.Run(name, func(t *testing.T) {
jsonConfig, route := runAddHTTPClients(t, template)
if _, ok := jsonConfig["http_clients"]; ok {
t.Errorf("no client should have been declared, got %v", jsonConfig["http_clients"])
}
if _, ok := route["default_http_client"]; ok {
t.Errorf("no default should have been set, got %v", route["default_http_client"])
}
})
}
}
// A template that declares its own clients decides for itself.
func TestAddHTTPClientsKeepsTemplateClients(t *testing.T) {
jsonConfig, route := runAddHTTPClients(t, `{
"http_clients": [{"tag": "over-proxy", "detour": "proxy"}],
"default_http_client": "over-proxy",
"rule_set": [
{"tag": "a", "type": "remote", "format": "binary", "url": "https://e.com/a.srs"}
]
}`)
clients, ok := jsonConfig["http_clients"].([]interface{})
if !ok || len(clients) != 1 {
t.Fatalf("the template's clients must be carried over, got %v", jsonConfig["http_clients"])
}
client, _ := clients[0].(map[string]interface{})
if client["tag"] != "over-proxy" || client["detour"] != "proxy" {
t.Errorf("the template's client must be unchanged, got %v", client)
}
if route["default_http_client"] != "over-proxy" {
t.Errorf("the template's default must win, got %v", route["default_http_client"])
}
}
func runAddOthersRoute(t *testing.T, template string) map[string]interface{} {
t.Helper()
var othersJson map[string]interface{}
if err := json.Unmarshal([]byte(template), &othersJson); err != nil {
t.Fatal(err)
}
route := map[string]interface{}{"final": "proxy"}
if defaultDomainResolver, ok := othersJson["default_domain_resolver"].(string); ok && defaultDomainResolver != "" {
route["default_domain_resolver"] = defaultDomainResolver
} else if fallback := fallbackDomainResolver(othersJson); fallback != "" {
route["default_domain_resolver"] = fallback
}
return route
}
// With two or more DNS servers and nothing naming the resolver for dial
// fields, sing-box guesses and reports the guess as deprecated.
func TestFallbackDomainResolver(t *testing.T) {
for name, expect := range map[string]struct {
template string
want string
}{
"final server wins": {`{"dns": {"servers": [
{"tag": "proxy-dns", "type": "tcp", "server": "8.8.8.8"},
{"tag": "local-dns", "type": "local"}
], "final": "local-dns"}}`, "local-dns"},
"first server without a final": {`{"dns": {"servers": [
{"tag": "a", "type": "local"}, {"tag": "b", "type": "local"}
]}}`, "a"},
"single server needs no choice": {`{"dns": {"servers": [
{"tag": "a", "type": "local"}
], "final": "a"}}`, ""},
"no dns section": {`{}`, ""},
} {
t.Run(name, func(t *testing.T) {
var othersJson map[string]interface{}
if err := json.Unmarshal([]byte(expect.template), &othersJson); err != nil {
t.Fatal(err)
}
if got := fallbackDomainResolver(othersJson); got != expect.want {
t.Errorf("want %q, got %q", expect.want, got)
}
})
}
}
// An explicit choice in the template must not be second-guessed.
func TestDefaultDomainResolverFromTemplate(t *testing.T) {
route := runAddOthersRoute(t, `{
"default_domain_resolver": "direct-dns",
"dns": {"servers": [
{"tag": "proxy-dns", "type": "tcp", "server": "8.8.8.8"},
{"tag": "direct-dns", "type": "local"}
], "final": "proxy-dns"}
}`)
if route["default_domain_resolver"] != "direct-dns" {
t.Errorf("the template's choice must win, got %v", route["default_domain_resolver"])
}
}
func TestGetOutboundsRealmIgnoresAddrs(t *testing.T) {
inbound := &model.Inbound{
Type: "hysteria2",
Tag: "hy2",
OutJson: json.RawMessage(`{"type": "hysteria2", "tag": "hy2", "realm": {"server_url": "https://realm.example.com", "realm_id": "home"}}`),
Addrs: json.RawMessage(`[{"server": "a.example.com", "server_port": 443}, {"server": "b.example.com", "server_port": 443}]`),
}
clientConfig := json.RawMessage(`{"hysteria2": {"name": "user", "password": "secret"}}`)
outbounds, _, err := (&JsonService{}).getOutbounds(clientConfig, []*model.Inbound{inbound}, "")
if err != nil {
t.Fatal(err)
}
if len(*outbounds) != 1 {
t.Fatalf("got %d outbounds, want one realm outbound", len(*outbounds))
}
out := (*outbounds)[0]
if _, ok := out["server"]; ok {
t.Errorf("server conflicts with realm, got %v", out["server"])
}
if out["password"] != "secret" {
t.Errorf("password = %v, want the client's", out["password"])
}
}
func TestGetOutboundsKeepsPerAddressTLS(t *testing.T) {
inbound := &model.Inbound{
Type: "vless",
Tag: "vless",
OutJson: json.RawMessage(`{"type": "vless", "tag": "vless", "tls": {"enabled": true, "server_name": "origin.example.com"}}`),
Addrs: json.RawMessage(`[{"server": "a.example.com", "server_port": 443, "tls": {"server_name": "one.example.com"}}, {"server": "b.example.com", "server_port": 443, "tls": {"server_name": "two.example.com"}}, {"server": "c.example.com", "server_port": 443}]`),
}
clientConfig := json.RawMessage(`{"vless": {"name": "user", "uuid": "00000000-0000-0000-0000-000000000000"}}`)
outbounds, _, err := (&JsonService{}).getOutbounds(clientConfig, []*model.Inbound{inbound}, "")
if err != nil {
t.Fatal(err)
}
want := []string{"one.example.com", "two.example.com", "origin.example.com"}
if len(*outbounds) != len(want) {
t.Fatalf("got %d outbounds, want %d", len(*outbounds), len(want))
}
for i, out := range *outbounds {
tls, _ := out["tls"].(map[string]interface{})
if tls["server_name"] != want[i] {
t.Errorf("outbound %d server_name = %v, want %s", i, tls["server_name"], want[i])
}
if tls["enabled"] != true {
t.Errorf("outbound %d lost the inbound's tls.enabled", i)
}
}
}
func TestGetOutboundsFallsBackToSniForWsHost(t *testing.T) {
inbound := &model.Inbound{
Type: "vless",
Tag: "vless",
OutJson: json.RawMessage(`{"type": "vless", "tag": "vless", "tls": {"enabled": true, "server_name": "origin.example.com"}, "transport": {"type": "ws", "path": "/ws"}}`),
Addrs: json.RawMessage(`[{"server": "a.example.com", "server_port": 443, "tls": {"server_name": "one.example.com"}}, {"server": "b.example.com", "server_port": 443, "tls": {"server_name": "two.example.com"}}]`),
}
clientConfig := json.RawMessage(`{"vless": {"name": "user", "uuid": "00000000-0000-0000-0000-000000000000"}}`)
outbounds, _, err := (&JsonService{}).getOutbounds(clientConfig, []*model.Inbound{inbound}, "")
if err != nil {
t.Fatal(err)
}
if len(*outbounds) != 2 {
t.Fatalf("got %d outbounds, want 2", len(*outbounds))
}
for i, want := range []string{"one.example.com", "two.example.com"} {
transport, _ := (*outbounds)[i]["transport"].(map[string]interface{})
headers, _ := transport["headers"].(map[string]interface{})
if headers["Host"] != want {
t.Errorf("outbound %d Host = %v, want %s", i, headers["Host"], want)
}
}
}