fix(outbound): let the probe resolve with the panel dns config (#7144)

The HTTP-mode outbound test builds a throwaway Xray instance whose
config carried no dns section, so an outbound whose server name only the
panel's configured resolvers know (a split-DNS name, a hosts entry)
failed the test with a lookup against the OS resolver, while the same
outbound worked through the running core.

The test endpoints now pass the stored template's dns section to the
probe instance. Its tag, which names routing the probe config does not
have, and its script are left out; every resolution setting rides along.

Fixes #7131
This commit is contained in:
SepehrSDQ
2026-11-24 03:48:17 +03:30
committed by GitHub
parent 0444c1f4f7
commit d04dbbe834
6 changed files with 117 additions and 29 deletions
+19 -2
View File
@@ -10,6 +10,7 @@ import (
piaprotocol "github.com/mhsanaei/3x-ui/v3/internal/pia"
"github.com/mhsanaei/3x-ui/v3/internal/util/common"
"github.com/mhsanaei/3x-ui/v3/internal/util/json_util"
"github.com/mhsanaei/3x-ui/v3/internal/web/service"
"github.com/mhsanaei/3x-ui/v3/internal/web/service/integration"
"github.com/mhsanaei/3x-ui/v3/internal/web/service/outbound"
@@ -324,7 +325,7 @@ func (a *XraySettingController) testOutbound(c *gin.Context) {
return
}
result, err := a.OutboundService.TestOutbound(outboundJSON, testURL, allOutboundsJSON, mode)
result, err := a.OutboundService.TestOutbound(outboundJSON, testURL, allOutboundsJSON, mode, a.panelDNS())
if err != nil {
jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
return
@@ -357,7 +358,7 @@ func (a *XraySettingController) testOutbounds(c *gin.Context) {
return
}
results, err := a.OutboundService.TestOutbounds(outboundsJSON, testURL, allOutboundsJSON, mode)
results, err := a.OutboundService.TestOutbounds(outboundsJSON, testURL, allOutboundsJSON, mode, a.panelDNS())
if err != nil {
jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
return
@@ -366,6 +367,22 @@ func (a *XraySettingController) testOutbounds(c *gin.Context) {
jsonObj(c, results, nil)
}
// panelDNS is the stored template's dns section; the outbound test resolves with it, as the
// running core does, or a name only the panel's resolvers know fails the test (#7131).
func (a *XraySettingController) panelDNS() json_util.RawMessage {
template, err := a.SettingService.GetXrayConfigTemplate()
if err != nil {
return nil
}
var cfg struct {
DNS json_util.RawMessage `json:"dns"`
}
if json.Unmarshal([]byte(template), &cfg) != nil {
return nil
}
return cfg.DNS
}
// balancerStatus reports the live state (override + strategy picks) of the
// balancer tags given as a comma-separated "tags" form field.
func (a *XraySettingController) balancerStatus(c *gin.Context) {
@@ -152,7 +152,7 @@ func TestOutboundTCPIPv6ReachesListener(t *testing.T) {
svc := &OutboundService{}
for _, ob := range items {
t.Run(ob.(map[string]any)["tag"].(string), func(t *testing.T) {
r, err := svc.TestOutbound(mustJSON(t, ob), "", "", "tcp")
r, err := svc.TestOutbound(mustJSON(t, ob), "", "", "tcp", nil)
if err != nil {
t.Fatal(err)
}
@@ -160,7 +160,7 @@ func TestOutboundTCPIPv6ReachesListener(t *testing.T) {
})
}
t.Run("batch", func(t *testing.T) {
results, err := svc.TestOutbounds(mustJSON(t, items), "", "", "tcp")
results, err := svc.TestOutbounds(mustJSON(t, items), "", "", "tcp", nil)
if err != nil {
t.Fatal(err)
}
@@ -197,7 +197,7 @@ func TestOutboundTCPReportsRefusedIPv6Connection(t *testing.T) {
for _, host := range []string{"::1", "[::1]"} {
t.Run(host, func(t *testing.T) {
ob := endpointOutbound("closed", "vless", host, port, false)
r, err := (&OutboundService{}).TestOutbound(mustJSON(t, ob), "", "", "tcp")
r, err := (&OutboundService{}).TestOutbound(mustJSON(t, ob), "", "", "tcp", nil)
if err != nil {
t.Fatal(err)
}
+29 -8
View File
@@ -103,17 +103,36 @@ func probeModeLabel(mode string) string {
}
}
// probeDNS is the panel's dns section as the throwaway instance can load it: "tag" names
// routing the probe config does not have, and the core refuses a "script" beside servers.
func probeDNS(panelDNS json_util.RawMessage) json_util.RawMessage {
var dns map[string]any
if len(panelDNS) == 0 || json.Unmarshal(panelDNS, &dns) != nil {
return nil
}
delete(dns, "tag")
delete(dns, "script")
if len(dns) == 0 {
return nil
}
cleaned, err := json.Marshal(dns)
if err != nil {
return nil
}
return cleaned
}
// TestOutbound probes a single outbound; legacy single-test API kept for the
// /testOutbound endpoint. Dispatch matches TestOutbounds: mode "tcp" dials
// the outbound's endpoints directly, anything else routes a real HTTP request
// through a temp xray instance (UDP-transport outbounds are always forced to
// the HTTP probe — a raw dial can't measure them).
func (s *OutboundService) TestOutbound(outboundJSON string, testURL string, allOutboundsJSON string, mode string) (*TestOutboundResult, error) {
func (s *OutboundService) TestOutbound(outboundJSON string, testURL string, allOutboundsJSON string, mode string, panelDNS json_util.RawMessage) (*TestOutboundResult, error) {
var ob map[string]any
if err := json.Unmarshal([]byte(outboundJSON), &ob); err != nil {
return &TestOutboundResult{Mode: probeModeLabel(mode), Success: false, Error: fmt.Sprintf("Invalid outbound JSON: %v", err)}, nil
}
results := s.testOutboundsParsed([]map[string]any{ob}, testURL, allOutboundsJSON, mode)
results := s.testOutboundsParsed([]map[string]any{ob}, testURL, allOutboundsJSON, mode, panelDNS)
return results[0], nil
}
@@ -121,7 +140,7 @@ func (s *OutboundService) TestOutbound(outboundJSON string, testURL string, allO
// input, in input order, each carrying the outbound's tag. allOutboundsJSON
// supplies the config context (sockopt.dialerProxy chains); testURL falls
// back to the default probe URL when empty.
func (s *OutboundService) TestOutbounds(outboundsJSON string, testURL string, allOutboundsJSON string, mode string) ([]*TestOutboundResult, error) {
func (s *OutboundService) TestOutbounds(outboundsJSON string, testURL string, allOutboundsJSON string, mode string, panelDNS json_util.RawMessage) ([]*TestOutboundResult, error) {
var raw []json.RawMessage
if err := json.Unmarshal([]byte(outboundsJSON), &raw); err != nil {
return nil, fmt.Errorf("invalid outbounds JSON: %w", err)
@@ -136,13 +155,13 @@ func (s *OutboundService) TestOutbounds(outboundsJSON string, testURL string, al
items[i] = ob
}
}
return s.testOutboundsParsed(items, testURL, allOutboundsJSON, mode), nil
return s.testOutboundsParsed(items, testURL, allOutboundsJSON, mode, panelDNS), nil
}
// testOutboundsParsed splits items into the TCP lane (direct dials, bounded
// worker pool) and the HTTP lane (one shared temp xray instance), runs both,
// and returns results aligned with items. A nil item marks unparseable input.
func (s *OutboundService) testOutboundsParsed(items []map[string]any, testURL string, allOutboundsJSON string, mode string) []*TestOutboundResult {
func (s *OutboundService) testOutboundsParsed(items []map[string]any, testURL string, allOutboundsJSON string, mode string, panelDNS json_util.RawMessage) []*TestOutboundResult {
results := make([]*TestOutboundResult, len(items))
modeLabel := probeModeLabel(mode)
@@ -250,7 +269,8 @@ func (s *OutboundService) testOutboundsParsed(items []map[string]any, testURL st
}
defer httpTestSemaphore.Unlock()
retryPerItem, err := runHTTPProbeBatch(httpItems, allOutbounds, testURL, realDelay)
dns := probeDNS(panelDNS)
retryPerItem, err := runHTTPProbeBatch(httpItems, allOutbounds, testURL, realDelay, dns)
if err == nil {
return results
}
@@ -263,7 +283,7 @@ func (s *OutboundService) testOutboundsParsed(items []map[string]any, testURL st
// instance so the broken outbound reports xray's real error and the
// rest still get tested. Serial: the poisoned case fails fast (~1s).
for _, it := range httpItems {
if _, ferr := runHTTPProbeBatch([]*httpBatchItem{it}, allOutbounds, testURL, realDelay); ferr != nil {
if _, ferr := runHTTPProbeBatch([]*httpBatchItem{it}, allOutbounds, testURL, realDelay, dns); ferr != nil {
it.result.Success = false
it.result.Error = ferr.Error()
}
@@ -277,7 +297,7 @@ func (s *OutboundService) testOutboundsParsed(items []map[string]any, testURL st
// whether splitting the batch into per-item instances could help (true for
// start failures / early exits that a poisoned config would explain, false
// for environmental failures like a missing binary or no free ports).
func runHTTPProbeBatch(items []*httpBatchItem, allOutbounds []any, testURL string, realDelay bool) (retryPerItem bool, err error) {
func runHTTPProbeBatch(items []*httpBatchItem, allOutbounds []any, testURL string, realDelay bool, dns json_util.RawMessage) (retryPerItem bool, err error) {
ports, release, err := reserveLoopbackPorts(len(items))
if err != nil {
return false, fmt.Errorf("Failed to reserve test ports: %w", err)
@@ -285,6 +305,7 @@ func runHTTPProbeBatch(items []*httpBatchItem, allOutbounds []any, testURL strin
defer release()
cfg := buildBatchTestConfig(items, allOutbounds, ports)
cfg.DNSConfig = dns
configPath, err := createTestConfigPath()
if err != nil {
@@ -10,12 +10,14 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"strconv"
"strings"
"sync"
"testing"
"time"
"github.com/mhsanaei/3x-ui/v3/internal/util/json_util"
"github.com/mhsanaei/3x-ui/v3/internal/xray"
)
@@ -254,7 +256,7 @@ func TestTestOutboundsPrevalidationAndOrdering(t *testing.T) {
map[string]any{"tag": "a", "protocol": "socks"}, // valid
map[string]any{"tag": "a", "protocol": "vless"}, // duplicate
})
results, err := (&OutboundService{}).TestOutbounds(batch, "http://example.invalid/gen", "", "http")
results, err := (&OutboundService{}).TestOutbounds(batch, "http://example.invalid/gen", "", "http", nil)
if err != nil {
t.Fatalf("TestOutbounds: %v", err)
}
@@ -296,7 +298,7 @@ func TestTestOutboundsFallbackOnStartFailure(t *testing.T) {
map[string]any{"tag": "a", "protocol": "socks"},
map[string]any{"tag": "b", "protocol": "vless"},
})
results, err := (&OutboundService{}).TestOutbounds(batch, "http://example.invalid/gen", "", "http")
results, err := (&OutboundService{}).TestOutbounds(batch, "http://example.invalid/gen", "", "http", nil)
if err != nil {
t.Fatalf("TestOutbounds: %v", err)
}
@@ -322,7 +324,7 @@ func TestTestOutboundsNoFallbackWhenBinaryMissing(t *testing.T) {
map[string]any{"tag": "a", "protocol": "socks"},
map[string]any{"tag": "b", "protocol": "vless"},
})
results, err := (&OutboundService{}).TestOutbounds(batch, "http://example.invalid/gen", "", "http")
results, err := (&OutboundService{}).TestOutbounds(batch, "http://example.invalid/gen", "", "http", nil)
if err != nil {
t.Fatalf("TestOutbounds: %v", err)
}
@@ -346,7 +348,7 @@ func TestTestOutboundsSemaphoreBusy(t *testing.T) {
defer httpTestSemaphore.Unlock()
batch := mustJSON(t, []any{map[string]any{"tag": "a", "protocol": "socks"}})
results, err := (&OutboundService{}).TestOutbounds(batch, "", "", "http")
results, err := (&OutboundService{}).TestOutbounds(batch, "", "", "http", nil)
if err != nil {
t.Fatalf("TestOutbounds: %v", err)
}
@@ -357,7 +359,7 @@ func TestTestOutboundsSemaphoreBusy(t *testing.T) {
func TestTestOutboundsInputValidation(t *testing.T) {
s := &OutboundService{}
if _, err := s.TestOutbounds("not json", "", "", "tcp"); err == nil {
if _, err := s.TestOutbounds("not json", "", "", "tcp", nil); err == nil {
t.Error("expected error for invalid JSON")
}
@@ -365,11 +367,11 @@ func TestTestOutboundsInputValidation(t *testing.T) {
for i := range big {
big[i] = map[string]any{"tag": fmt.Sprintf("t%d", i), "protocol": "socks"}
}
if _, err := s.TestOutbounds(mustJSON(t, big), "", "", "tcp"); err == nil {
if _, err := s.TestOutbounds(mustJSON(t, big), "", "", "tcp", nil); err == nil {
t.Error("expected error for oversized batch")
}
results, err := s.TestOutbounds("[]", "", "", "tcp")
results, err := s.TestOutbounds("[]", "", "", "tcp", nil)
if err != nil || len(results) != 0 {
t.Errorf("empty batch: results=%v err=%v", results, err)
}
@@ -397,7 +399,7 @@ func TestTestOutboundsTCPLane(t *testing.T) {
"protocol": "socks",
"settings": map[string]any{"servers": []any{map[string]any{"address": "127.0.0.1", "port": port}}},
}})
results, err := (&OutboundService{}).TestOutbounds(batch, "", "", "tcp")
results, err := (&OutboundService{}).TestOutbounds(batch, "", "", "tcp", nil)
if err != nil {
t.Fatalf("TestOutbounds: %v", err)
}
@@ -433,7 +435,7 @@ func TestTestOutboundsHTTPBatchThroughStubSocks(t *testing.T) {
map[string]any{"tag": "a", "protocol": "vless"},
map[string]any{"tag": "b", "protocol": "trojan"},
})
results, err := (&OutboundService{}).TestOutbounds(batch, srv.URL, "", "http")
results, err := (&OutboundService{}).TestOutbounds(batch, srv.URL, "", "http", nil)
if err != nil {
t.Fatalf("TestOutbounds: %v", err)
}
@@ -497,7 +499,7 @@ func TestTestOutboundsRealDelayBatchThroughStubSocks(t *testing.T) {
map[string]any{"tag": "a", "protocol": "vless"},
map[string]any{"tag": "wg", "protocol": "wireguard"},
})
results, err := (&OutboundService{}).TestOutbounds(batch, srv.URL, "", "real")
results, err := (&OutboundService{}).TestOutbounds(batch, srv.URL, "", "real", nil)
if err != nil {
t.Fatalf("TestOutbounds: %v", err)
}
@@ -544,7 +546,7 @@ func TestTestOutboundsTCPModeForcesUDPToHTTPProbe(t *testing.T) {
})
batch := mustJSON(t, []any{map[string]any{"tag": "wg", "protocol": "wireguard"}})
results, err := (&OutboundService{}).TestOutbounds(batch, srv.URL, "", "tcp")
results, err := (&OutboundService{}).TestOutbounds(batch, srv.URL, "", "tcp", nil)
if err != nil {
t.Fatalf("TestOutbounds: %v", err)
}
@@ -571,7 +573,7 @@ func TestTestOutboundsTCPModeForcesAmneziaWGToHTTPProbe(t *testing.T) {
})
batch := mustJSON(t, []any{map[string]any{"tag": "awg", "protocol": "amneziawg"}})
results, err := (&OutboundService{}).TestOutbounds(batch, srv.URL, "", "tcp")
results, err := (&OutboundService{}).TestOutbounds(batch, srv.URL, "", "tcp", nil)
if err != nil {
t.Fatalf("TestOutbounds: %v", err)
}
@@ -622,3 +624,51 @@ func TestProbeThroughSocksTransportFailure(t *testing.T) {
t.Errorf("expected transport failure, got %+v", result)
}
}
// The probe instance had no dns section, so an outbound whose address only the panel's
// resolvers know failed the test while it worked through the running core (#7131).
func TestTestOutbounds_ProbeInstanceResolvesWithThePanelDNS(t *testing.T) {
for _, tc := range []struct {
name string
panelDNS string
want map[string]any
}{
{
name: "resolution settings ride along, routing tag and script do not",
panelDNS: `{"servers":["https+local://198.51.100.10/dns-query"],"hosts":{"node1.example.internal":"198.51.100.20"},"useSystemHosts":true,"tag":"dns_inbound","script":"return nil"}`,
want: map[string]any{
"servers": []any{"https+local://198.51.100.10/dns-query"},
"hosts": map[string]any{"node1.example.internal": "198.51.100.20"},
"useSystemHosts": true,
},
},
{name: "no panel dns leaves the instance on the OS resolver", panelDNS: ""},
{name: "a section with nothing but its tag is dropped", panelDNS: `{"tag":"dns_inbound"}`},
{name: "an unreadable section is dropped", panelDNS: `not json`},
} {
t.Run(tc.name, func(t *testing.T) {
var probeConfig *xray.Config
withStubProcess(t, func(cfg *xray.Config, configPath string) batchProcess {
probeConfig = cfg
return &stubProcess{cfg: cfg, startErr: errors.New("stop before dialing")}
})
batch := mustJSON(t, []any{map[string]any{"tag": "a", "protocol": "vless"}})
if _, err := (&OutboundService{}).TestOutbounds(batch, "http://example.invalid/gen", "", "http", json_util.RawMessage(tc.panelDNS)); err != nil {
t.Fatalf("TestOutbounds: %v", err)
}
if probeConfig == nil {
t.Fatal("no probe instance was built")
}
var got map[string]any
if len(probeConfig.DNSConfig) > 0 {
if err := json.Unmarshal(probeConfig.DNSConfig, &got); err != nil {
t.Fatalf("probe dns is not JSON: %v", err)
}
}
if !reflect.DeepEqual(got, tc.want) {
t.Fatalf("probe dns = %v, want %v", got, tc.want)
}
})
}
}
@@ -28,7 +28,7 @@ func TestTestOutboundsTCPModeForcesCoreSpelledUDPToHTTPProbe(t *testing.T) {
})
batch := mustJSON(t, []any{map[string]any{"tag": "wg", "protocol": "WireGuard"}})
results, err := (&OutboundService{}).TestOutbounds(batch, srv.URL, "", "tcp")
results, err := (&OutboundService{}).TestOutbounds(batch, srv.URL, "", "tcp", nil)
if err != nil {
t.Fatalf("TestOutbounds: %v", err)
}
@@ -129,7 +129,7 @@ func TestTestOutboundsRejectsUntestableIDsInAnyCase(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
batch := mustJSON(t, []any{map[string]any{"tag": "t1", "protocol": tt.protocol}})
results, err := (&OutboundService{}).TestOutbounds(batch, "", "", "tcp")
results, err := (&OutboundService{}).TestOutbounds(batch, "", "", "tcp", nil)
if err != nil {
t.Fatalf("TestOutbounds: %v", err)
}
@@ -166,7 +166,7 @@ func TestTestOutboundsTCPLaneReadsProtocolIDCaseInsensitively(t *testing.T) {
"protocol": "SOCKS",
"settings": map[string]any{"servers": []any{map[string]any{"address": "127.0.0.1", "port": port}}},
}})
results, err := (&OutboundService{}).TestOutbounds(batch, "", "", "tcp")
results, err := (&OutboundService{}).TestOutbounds(batch, "", "", "tcp", nil)
if err != nil {
t.Fatalf("TestOutbounds: %v", err)
}
@@ -40,7 +40,7 @@ func TestTestOutboundsRejectsCaseVariantUntestableIDs(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
batch := mustJSON(t, []any{map[string]any{"tag": "probe", "protocol": tt.protocol}})
results, err := (&OutboundService{}).TestOutbounds(batch, srv.URL, "", "http")
results, err := (&OutboundService{}).TestOutbounds(batch, srv.URL, "", "http", nil)
if err != nil {
t.Fatalf("TestOutbounds: %v", err)
}