mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-02 15:36:41 -05:00
Fix: FakeDNS IPv6 & refine JSON unmarshal (#925)
This commit is contained in:
parent
bad6cdfb88
commit
a21e4a7deb
@ -1,78 +1,127 @@
|
|||||||
package conf
|
package conf
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/golang/protobuf/proto"
|
"encoding/json"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/v2fly/v2ray-core/v4/app/dns/fakedns"
|
"github.com/v2fly/v2ray-core/v4/app/dns/fakedns"
|
||||||
)
|
)
|
||||||
|
|
||||||
type FakeDNSPoolElementConfig struct {
|
type FakeDNSPoolElementConfig struct {
|
||||||
IPPool string `json:"ipPool"`
|
IPPool string `json:"ipPool"`
|
||||||
LruSize int64 `json:"poolSize"`
|
LRUSize int64 `json:"poolSize"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type FakeDNSConfig struct {
|
type FakeDNSConfig struct {
|
||||||
IPPool string `json:"ipPool"`
|
pool *FakeDNSPoolElementConfig
|
||||||
LruSize int64 `json:"poolSize"`
|
pools []*FakeDNSPoolElementConfig
|
||||||
Pools *[]FakeDNSPoolElementConfig `json:"pools,omitempty"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f FakeDNSConfig) Build() (proto.Message, error) {
|
// UnmarshalJSON implements encoding/json.Unmarshaler.UnmarshalJSON
|
||||||
if f.Pools != nil {
|
func (f *FakeDNSConfig) UnmarshalJSON(data []byte) error {
|
||||||
fakeDNSPool := &fakedns.FakeDnsPoolMulti{}
|
var pool FakeDNSPoolElementConfig
|
||||||
for _, v := range *f.Pools {
|
var pools []*FakeDNSPoolElementConfig
|
||||||
fakeDNSPool.Pools = append(fakeDNSPool.Pools, &fakedns.FakeDnsPool{IpPool: v.IPPool, LruSize: v.LruSize})
|
switch {
|
||||||
}
|
case json.Unmarshal(data, &pool) == nil:
|
||||||
return fakeDNSPool, 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
|
||||||
}
|
}
|
||||||
|
|
||||||
return &fakedns.FakeDnsPool{
|
if f.pools != nil {
|
||||||
IpPool: f.IPPool,
|
for _, v := range f.pools {
|
||||||
LruSize: f.LruSize,
|
fakeDNSPool.Pools = append(fakeDNSPool.Pools, &fakedns.FakeDnsPool{IpPool: v.IPPool, LruSize: v.LRUSize})
|
||||||
}, nil
|
}
|
||||||
|
return &fakeDNSPool, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, newError("no valid FakeDNS config")
|
||||||
}
|
}
|
||||||
|
|
||||||
type FakeDNSPostProcessingStage struct{}
|
type FakeDNSPostProcessingStage struct{}
|
||||||
|
|
||||||
func (FakeDNSPostProcessingStage) Process(conf *Config) error {
|
func (FakeDNSPostProcessingStage) Process(config *Config) error {
|
||||||
var fakeDNSInUse bool
|
fakeDNSInUse := false
|
||||||
|
isIPv4Enable, isIPv6Enable := true, true
|
||||||
|
|
||||||
if conf.DNSConfig != nil {
|
if config.DNSConfig != nil {
|
||||||
for _, v := range conf.DNSConfig.Servers {
|
for _, v := range config.DNSConfig.Servers {
|
||||||
if v.Address.Family().IsDomain() {
|
if v.Address.Family().IsDomain() && strings.EqualFold(v.Address.Domain(), "fakedns") {
|
||||||
if v.Address.Domain() == "fakedns" {
|
fakeDNSInUse = true
|
||||||
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 fakeDNSInUse {
|
||||||
if conf.FakeDNS == nil {
|
// Add a Fake DNS Config if there is none
|
||||||
// Add a Fake DNS Config if there is none
|
if config.FakeDNS == nil {
|
||||||
conf.FakeDNS = &FakeDNSConfig{
|
config.FakeDNS = &FakeDNSConfig{}
|
||||||
IPPool: "198.18.0.0/15",
|
switch {
|
||||||
LruSize: 65535,
|
case isIPv4Enable && isIPv6Enable:
|
||||||
|
config.FakeDNS.pools = []*FakeDNSPoolElementConfig{
|
||||||
|
{
|
||||||
|
IPPool: "198.18.0.0/15",
|
||||||
|
LRUSize: 32768,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
IPPool: "fc00::/18",
|
||||||
|
LRUSize: 32768,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
case !isIPv4Enable && isIPv6Enable:
|
||||||
|
config.FakeDNS.pool = &FakeDNSPoolElementConfig{
|
||||||
|
IPPool: "fc00::/18",
|
||||||
|
LRUSize: 65535,
|
||||||
|
}
|
||||||
|
case isIPv4Enable && !isIPv6Enable:
|
||||||
|
config.FakeDNS.pool = &FakeDNSPoolElementConfig{
|
||||||
|
IPPool: "198.18.0.0/15",
|
||||||
|
LRUSize: 65535,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
found := false
|
found := false
|
||||||
// Check if there is a Outbound with necessary sniffer on
|
// Check if there is a Outbound with necessary sniffer on
|
||||||
var inbounds []InboundDetourConfig
|
var inbounds []InboundDetourConfig
|
||||||
|
|
||||||
if len(conf.InboundConfigs) > 0 {
|
if len(config.InboundConfigs) > 0 {
|
||||||
inbounds = append(inbounds, conf.InboundConfigs...)
|
inbounds = append(inbounds, config.InboundConfigs...)
|
||||||
}
|
}
|
||||||
for _, v := range inbounds {
|
for _, v := range inbounds {
|
||||||
if v.SniffingConfig != nil && v.SniffingConfig.Enabled && v.SniffingConfig.DestOverride != nil {
|
if v.SniffingConfig != nil && v.SniffingConfig.Enabled && v.SniffingConfig.DestOverride != nil {
|
||||||
for _, dov := range *v.SniffingConfig.DestOverride {
|
for _, dov := range *v.SniffingConfig.DestOverride {
|
||||||
if dov == "fakedns" {
|
if strings.EqualFold(dov, "fakedns") || strings.EqualFold(dov, "fakedns+others") {
|
||||||
found = true
|
found = true
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !found {
|
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()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user