mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-04-29 16:58:34 +00:00
Fakedns improvements (#731)
Co-authored-by: Shelikhoo <xiaokangwang@outlook.com> Co-authored-by: sixg0000d <sixg0000d@gmail.com> Co-authored-by: Loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com>
This commit is contained in:
parent
e286cdcaa8
commit
6b6974c804
12 changed files with 478 additions and 59 deletions
|
@ -1,65 +1,128 @@
|
|||
package conf
|
||||
|
||||
import (
|
||||
"github.com/golang/protobuf/proto"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
|
||||
"github.com/xtls/xray-core/app/dns/fakedns"
|
||||
"github.com/xtls/xray-core/features/dns"
|
||||
)
|
||||
|
||||
type FakeDNSConfig struct {
|
||||
type FakeDNSPoolElementConfig struct {
|
||||
IPPool string `json:"ipPool"`
|
||||
LruSize int64 `json:"poolSize"`
|
||||
LRUSize int64 `json:"poolSize"`
|
||||
}
|
||||
|
||||
func (f FakeDNSConfig) Build() (proto.Message, error) {
|
||||
return &fakedns.FakeDnsPool{
|
||||
IpPool: f.IPPool,
|
||||
LruSize: f.LruSize,
|
||||
}, nil
|
||||
type FakeDNSConfig struct {
|
||||
pool *FakeDNSPoolElementConfig
|
||||
pools []*FakeDNSPoolElementConfig
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements encoding/json.Unmarshaler.UnmarshalJSON
|
||||
func (f *FakeDNSConfig) UnmarshalJSON(data []byte) error {
|
||||
var pool FakeDNSPoolElementConfig
|
||||
var pools []*FakeDNSPoolElementConfig
|
||||
switch {
|
||||
case json.Unmarshal(data, &pool) == nil:
|
||||
f.pool = &pool
|
||||
case json.Unmarshal(data, &pools) == nil:
|
||||
f.pools = pools
|
||||
default:
|
||||
return newError("invalid fakedns config")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *FakeDNSConfig) Build() (*fakedns.FakeDnsPoolMulti, error) {
|
||||
fakeDNSPool := fakedns.FakeDnsPoolMulti{}
|
||||
|
||||
if f.pool != nil {
|
||||
fakeDNSPool.Pools = append(fakeDNSPool.Pools, &fakedns.FakeDnsPool{
|
||||
IpPool: f.pool.IPPool,
|
||||
LruSize: f.pool.LRUSize,
|
||||
})
|
||||
return &fakeDNSPool, nil
|
||||
}
|
||||
|
||||
if f.pools != nil {
|
||||
for _, v := range f.pools {
|
||||
fakeDNSPool.Pools = append(fakeDNSPool.Pools, &fakedns.FakeDnsPool{IpPool: v.IPPool, LruSize: v.LRUSize})
|
||||
}
|
||||
return &fakeDNSPool, nil
|
||||
}
|
||||
|
||||
return nil, newError("no valid FakeDNS config")
|
||||
}
|
||||
|
||||
type FakeDNSPostProcessingStage struct{}
|
||||
|
||||
func (FakeDNSPostProcessingStage) Process(conf *Config) error {
|
||||
var fakeDNSInUse bool
|
||||
func (FakeDNSPostProcessingStage) Process(config *Config) error {
|
||||
fakeDNSInUse := false
|
||||
isIPv4Enable, isIPv6Enable := true, true
|
||||
|
||||
if conf.DNSConfig != nil {
|
||||
for _, v := range conf.DNSConfig.Servers {
|
||||
if v.Address.Family().IsDomain() {
|
||||
if v.Address.Domain() == "fakedns" {
|
||||
fakeDNSInUse = true
|
||||
}
|
||||
if config.DNSConfig != nil {
|
||||
for _, v := range config.DNSConfig.Servers {
|
||||
if v.Address.Family().IsDomain() && strings.EqualFold(v.Address.Domain(), "fakedns") {
|
||||
fakeDNSInUse = true
|
||||
}
|
||||
}
|
||||
|
||||
switch strings.ToLower(config.DNSConfig.QueryStrategy) {
|
||||
case "useip4", "useipv4", "use_ip4", "use_ipv4", "use_ip_v4", "use-ip4", "use-ipv4", "use-ip-v4":
|
||||
isIPv4Enable, isIPv6Enable = true, false
|
||||
case "useip6", "useipv6", "use_ip6", "use_ipv6", "use_ip_v6", "use-ip6", "use-ipv6", "use-ip-v6":
|
||||
isIPv4Enable, isIPv6Enable = false, true
|
||||
}
|
||||
}
|
||||
|
||||
if fakeDNSInUse {
|
||||
if conf.FakeDNS == nil {
|
||||
// Add a Fake DNS Config if there is none
|
||||
conf.FakeDNS = &FakeDNSConfig{
|
||||
IPPool: dns.FakeIPPool,
|
||||
LruSize: 65535,
|
||||
// Add a Fake DNS Config if there is none
|
||||
if config.FakeDNS == nil {
|
||||
config.FakeDNS = &FakeDNSConfig{}
|
||||
switch {
|
||||
case isIPv4Enable && isIPv6Enable:
|
||||
config.FakeDNS.pools = []*FakeDNSPoolElementConfig{
|
||||
{
|
||||
IPPool: dns.FakeIPv4Pool,
|
||||
LRUSize: 32768,
|
||||
},
|
||||
{
|
||||
IPPool: dns.FakeIPv6Pool,
|
||||
LRUSize: 32768,
|
||||
},
|
||||
}
|
||||
case !isIPv4Enable && isIPv6Enable:
|
||||
config.FakeDNS.pool = &FakeDNSPoolElementConfig{
|
||||
IPPool: dns.FakeIPv6Pool,
|
||||
LRUSize: 65535,
|
||||
}
|
||||
case isIPv4Enable && !isIPv6Enable:
|
||||
config.FakeDNS.pool = &FakeDNSPoolElementConfig{
|
||||
IPPool: dns.FakeIPv4Pool,
|
||||
LRUSize: 65535,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
found := false
|
||||
// Check if there is a Outbound with necessary sniffer on
|
||||
var inbounds []InboundDetourConfig
|
||||
|
||||
if len(conf.InboundConfigs) > 0 {
|
||||
inbounds = append(inbounds, conf.InboundConfigs...)
|
||||
if len(config.InboundConfigs) > 0 {
|
||||
inbounds = append(inbounds, config.InboundConfigs...)
|
||||
}
|
||||
for _, v := range inbounds {
|
||||
if v.SniffingConfig != nil && v.SniffingConfig.Enabled && v.SniffingConfig.DestOverride != nil {
|
||||
for _, dov := range *v.SniffingConfig.DestOverride {
|
||||
if dov == "fakedns" {
|
||||
if strings.EqualFold(dov, "fakedns") || strings.EqualFold(dov, "fakedns+others") {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
newError("Defined Fake DNS but haven't enabled fake dns sniffing at any inbound.").AtWarning().WriteToLog()
|
||||
newError("Defined FakeDNS but haven't enabled FakeDNS destOverride at any inbound.").AtWarning().WriteToLog()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -78,6 +78,8 @@ func (c *SniffingConfig) Build() (*proxyman.SniffingConfig, error) {
|
|||
p = append(p, "tls")
|
||||
case "fakedns":
|
||||
p = append(p, "fakedns")
|
||||
case "fakedns+others":
|
||||
p = append(p, "fakedns+others")
|
||||
default:
|
||||
return nil, newError("unknown protocol: ", protocol)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue