2021-03-07 04:39:50 +00:00
|
|
|
package conf
|
|
|
|
|
|
|
|
import (
|
2024-06-29 18:32:57 +00:00
|
|
|
"context"
|
2021-10-20 05:15:49 +00:00
|
|
|
"encoding/json"
|
|
|
|
"strings"
|
2021-09-20 13:00:55 +00:00
|
|
|
|
2021-03-07 04:39:50 +00:00
|
|
|
"github.com/xtls/xray-core/app/dns/fakedns"
|
2024-06-29 18:32:57 +00:00
|
|
|
"github.com/xtls/xray-core/common/errors"
|
2021-03-14 15:58:27 +00:00
|
|
|
"github.com/xtls/xray-core/features/dns"
|
2021-03-07 04:39:50 +00:00
|
|
|
)
|
|
|
|
|
2021-10-20 05:15:49 +00:00
|
|
|
type FakeDNSPoolElementConfig struct {
|
2021-03-07 04:39:50 +00:00
|
|
|
IPPool string `json:"ipPool"`
|
2021-10-20 05:15:49 +00:00
|
|
|
LRUSize int64 `json:"poolSize"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type FakeDNSConfig struct {
|
|
|
|
pool *FakeDNSPoolElementConfig
|
|
|
|
pools []*FakeDNSPoolElementConfig
|
2021-03-07 04:39:50 +00:00
|
|
|
}
|
|
|
|
|
2021-10-20 05:15:49 +00:00
|
|
|
// 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:
|
2024-06-29 18:32:57 +00:00
|
|
|
return errors.New("invalid fakedns config")
|
2021-10-20 05:15:49 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-06-29 18:32:57 +00:00
|
|
|
return nil, errors.New("no valid FakeDNS config")
|
2021-03-07 04:39:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type FakeDNSPostProcessingStage struct{}
|
|
|
|
|
2021-10-20 05:15:49 +00:00
|
|
|
func (FakeDNSPostProcessingStage) Process(config *Config) error {
|
|
|
|
fakeDNSInUse := false
|
|
|
|
isIPv4Enable, isIPv6Enable := true, true
|
2021-03-07 04:39:50 +00:00
|
|
|
|
2021-10-20 05:15:49 +00:00
|
|
|
if config.DNSConfig != nil {
|
|
|
|
for _, v := range config.DNSConfig.Servers {
|
|
|
|
if v.Address.Family().IsDomain() && strings.EqualFold(v.Address.Domain(), "fakedns") {
|
|
|
|
fakeDNSInUse = true
|
2021-03-07 04:39:50 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-20 05:15:49 +00:00
|
|
|
|
|
|
|
switch strings.ToLower(config.DNSConfig.QueryStrategy) {
|
2021-12-15 00:28:47 +00:00
|
|
|
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
|
2021-10-20 05:15:49 +00:00
|
|
|
}
|
2021-03-07 04:39:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if fakeDNSInUse {
|
2021-10-20 05:15:49 +00:00
|
|
|
// 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,
|
|
|
|
}
|
2021-03-07 04:39:50 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-20 05:15:49 +00:00
|
|
|
|
2021-03-07 04:39:50 +00:00
|
|
|
found := false
|
|
|
|
// Check if there is a Outbound with necessary sniffer on
|
|
|
|
var inbounds []InboundDetourConfig
|
|
|
|
|
2021-10-20 05:15:49 +00:00
|
|
|
if len(config.InboundConfigs) > 0 {
|
|
|
|
inbounds = append(inbounds, config.InboundConfigs...)
|
2021-03-07 04:39:50 +00:00
|
|
|
}
|
|
|
|
for _, v := range inbounds {
|
|
|
|
if v.SniffingConfig != nil && v.SniffingConfig.Enabled && v.SniffingConfig.DestOverride != nil {
|
|
|
|
for _, dov := range *v.SniffingConfig.DestOverride {
|
2021-10-20 05:15:49 +00:00
|
|
|
if strings.EqualFold(dov, "fakedns") || strings.EqualFold(dov, "fakedns+others") {
|
2021-03-07 04:39:50 +00:00
|
|
|
found = true
|
2021-10-20 05:15:49 +00:00
|
|
|
break
|
2021-03-07 04:39:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2024-06-29 18:32:57 +00:00
|
|
|
errors.LogWarning(context.Background(), "Defined FakeDNS but haven't enabled FakeDNS destOverride at any inbound.")
|
2021-03-07 04:39:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|