Files
mirror-3x-ui/internal/web/service/inbound_xhttp_test.go
T
Gleb Kochergin 6f162d03a1 feat(xhttp): configure a separate download endpoint and preserve it in subscriptions (#7187)
An operator who wanted XHTTP downloads to go through another endpoint, such as a relay or a CDN, had no field for it, and downloadSettings typed into the advanced JSON editor was dropped the next time the inbound was saved (#6307).

Add a separate-download switch to the inbound and outbound XHTTP forms, keep the download stream through save, share links, JSON and Clash output, and validate it before it is stored: transport, address, port, REALITY key and short id, and a uTLS fingerprint the core accepts. Saving an inbound re-applies its own session, sequence and padding rules to the download leg, since the listener checks the download request against them.
2026-11-24 21:04:55 +03:30

60 lines
2.7 KiB
Go

package service
import (
"encoding/json"
"reflect"
"strings"
"testing"
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
)
func TestValidateXhttpDownload(t *testing.T) {
const valid = `{"network":"xhttp","xhttpSettings":{"mode":"packet-up","extra":{"downloadSettings":{"network":"xhttp","address":"down.test","port":443,"security":"reality","realitySettings":{"password":"AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE","shortId":"abcd"}}}}}`
for _, tc := range []struct{ from, to, field string }{
{"packet-up", "packet-up", ""},
{"packet-up", "stream-one", "stream-one"},
{`"network":"xhttp","address"`, `"network":"tcp","address"`, "network"},
{"AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE", "abc", "password"},
{"abcd", "f", "shortId"},
{`"shortId":"abcd"`, `"shortId":"abcd","fingerprint":"Chrome"`, ""},
{`"shortId":"abcd"`, `"shortId":"abcd","fingerprint":"chorme"`, "fingerprint"},
{`"shortId":"abcd"`, `"shortId":"abcd","fingerprint":"unsafe"`, "fingerprint"},
{`"security":"reality","realitySettings"`, `"security":"tls","tlsSettings":{"fingerprint":"chorme"},"realitySettings"`, "fingerprint"},
{`"security":"reality","realitySettings"`, `"security":"tls","tlsSettings":{"fingerprint":"firefox"},"realitySettings"`, ""},
} {
err := validateXhttpDownload(strings.Replace(valid, tc.from, tc.to, 1))
if tc.field == "" && err != nil || tc.field != "" && (err == nil || !strings.Contains(err.Error(), tc.field)) {
t.Fatalf("%s: %v", tc.to, err)
}
}
}
// The listener matches a download GET against its own session and padding rules, so a leg
// saved before the inbound's rules changed must not keep the old ones.
func TestNormalizeStreamSettingsSyncsXhttpDownloadRequestShape(t *testing.T) {
inbound := &model.Inbound{Protocol: model.VLESS, StreamSettings: `{"network":"xhttp","xhttpSettings":{
"path":"/up","sessionIDPlacement":"query","sessionIDKey":"sid","xPaddingBytes":"100-200",
"downloadSettings":{"network":"xhttp","address":"down.test","port":443,"security":"tls",
"xhttpSettings":{"path":"/down","host":"cdn.test","sessionIDPlacement":"path","seqKey":"stale"}}}}`}
(&InboundService{}).normalizeStreamSettings(inbound)
var stream struct {
XhttpSettings struct {
DownloadSettings struct {
XhttpSettings map[string]any `json:"xhttpSettings"`
} `json:"downloadSettings"`
} `json:"xhttpSettings"`
}
if err := json.Unmarshal([]byte(inbound.StreamSettings), &stream); err != nil {
t.Fatal(err)
}
want := map[string]any{
"path": "/down", "host": "cdn.test",
"sessionIDPlacement": "query", "sessionIDKey": "sid", "xPaddingBytes": "100-200",
}
if got := stream.XhttpSettings.DownloadSettings.XhttpSettings; !reflect.DeepEqual(got, want) {
t.Fatalf("download leg xhttpSettings = %v, want %v", got, want)
}
}