Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 66 additions & 57 deletions app/dns/config.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/dns/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ message NameServer {
repeated OriginalRule original_rules = 4;
QueryStrategy query_strategy = 7;
bool allowUnexpectedIPs = 8;
string tag = 9;
}

enum DomainMatchingType {
Expand Down
10 changes: 10 additions & 0 deletions app/dns/nameserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/xtls/xray-core/app/router"
"github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/common/session"
"github.com/xtls/xray-core/common/strmatcher"
"github.com/xtls/xray-core/core"
"github.com/xtls/xray-core/features/dns"
Expand All @@ -31,6 +32,7 @@ type Client struct {
domains []string
expectIPs []*router.GeoIPMatcher
allowUnexpectedIPs bool
tag string
}

var errExpectedIPNonMatch = errors.New("expectIPs not match")
Expand Down Expand Up @@ -168,6 +170,7 @@ func NewClient(
client.domains = rules
client.expectIPs = matchers
client.allowUnexpectedIPs = ns.AllowUnexpectedIPs
client.tag = ns.Tag
return nil
})
return client, err
Expand All @@ -181,6 +184,13 @@ func (c *Client) Name() string {
// QueryIP sends DNS query to the name server with the client's IP.
func (c *Client) QueryIP(ctx context.Context, domain string, option dns.IPOption, disableCache bool) ([]net.IP, error) {
ctx, cancel := context.WithTimeout(ctx, 4*time.Second)
if len(c.tag) != 0 {
content := session.InboundFromContext(ctx)
errors.LogDebug(ctx, "DNS: client override tag from ", content.Tag, " to ", c.tag)
// create a new context to override the tag
// do not direct set *content.Tag, it might be used by other clients
ctx = session.ContextWithInbound(ctx, &session.Inbound{Tag: c.tag})
}
ips, err := c.server.QueryIP(ctx, domain, c.clientIP, option, disableCache)
cancel()

Expand Down
4 changes: 4 additions & 0 deletions infra/conf/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type NameServerConfig struct {
ExpectIPs StringList `json:"expectIps"`
QueryStrategy string `json:"queryStrategy"`
AllowUnexpectedIPs bool `json:"allowUnexpectedIps"`
Tag string `json:"tag"`
}

func (c *NameServerConfig) UnmarshalJSON(data []byte) error {
Expand All @@ -38,6 +39,7 @@ func (c *NameServerConfig) UnmarshalJSON(data []byte) error {
ExpectIPs StringList `json:"expectIps"`
QueryStrategy string `json:"queryStrategy"`
AllowUnexpectedIPs bool `json:"allowUnexpectedIps"`
Tag string `json:"tag"`
}
if err := json.Unmarshal(data, &advanced); err == nil {
c.Address = advanced.Address
Expand All @@ -48,6 +50,7 @@ func (c *NameServerConfig) UnmarshalJSON(data []byte) error {
c.ExpectIPs = advanced.ExpectIPs
c.QueryStrategy = advanced.QueryStrategy
c.AllowUnexpectedIPs = advanced.AllowUnexpectedIPs
c.Tag = advanced.Tag
return nil
}

Expand Down Expand Up @@ -121,6 +124,7 @@ func (c *NameServerConfig) Build() (*dns.NameServer, error) {
OriginalRules: originalRules,
QueryStrategy: resolveQueryStrategy(c.QueryStrategy),
AllowUnexpectedIPs: c.AllowUnexpectedIPs,
Tag: c.Tag,
}, nil
}

Expand Down