github.com/v2fly/v2ray-core/v4@v4.45.2/infra/conf/fakedns.go (about)

     1  package conf
     2  
     3  import (
     4  	"encoding/json"
     5  	"strings"
     6  
     7  	"github.com/v2fly/v2ray-core/v4/app/dns/fakedns"
     8  )
     9  
    10  type FakeDNSPoolElementConfig struct {
    11  	IPPool  string `json:"ipPool"`
    12  	LRUSize int64  `json:"poolSize"`
    13  }
    14  
    15  type FakeDNSConfig struct {
    16  	pool  *FakeDNSPoolElementConfig
    17  	pools []*FakeDNSPoolElementConfig
    18  }
    19  
    20  // UnmarshalJSON implements encoding/json.Unmarshaler.UnmarshalJSON
    21  func (f *FakeDNSConfig) UnmarshalJSON(data []byte) error {
    22  	var pool FakeDNSPoolElementConfig
    23  	var pools []*FakeDNSPoolElementConfig
    24  	switch {
    25  	case json.Unmarshal(data, &pool) == nil:
    26  		f.pool = &pool
    27  	case json.Unmarshal(data, &pools) == nil:
    28  		f.pools = pools
    29  	default:
    30  		return newError("invalid fakedns config")
    31  	}
    32  	return nil
    33  }
    34  
    35  func (f *FakeDNSConfig) Build() (*fakedns.FakeDnsPoolMulti, error) {
    36  	fakeDNSPool := fakedns.FakeDnsPoolMulti{}
    37  
    38  	if f.pool != nil {
    39  		fakeDNSPool.Pools = append(fakeDNSPool.Pools, &fakedns.FakeDnsPool{
    40  			IpPool:  f.pool.IPPool,
    41  			LruSize: f.pool.LRUSize,
    42  		})
    43  		return &fakeDNSPool, nil
    44  	}
    45  
    46  	if f.pools != nil {
    47  		for _, v := range f.pools {
    48  			fakeDNSPool.Pools = append(fakeDNSPool.Pools, &fakedns.FakeDnsPool{IpPool: v.IPPool, LruSize: v.LRUSize})
    49  		}
    50  		return &fakeDNSPool, nil
    51  	}
    52  
    53  	return nil, newError("no valid FakeDNS config")
    54  }
    55  
    56  type FakeDNSPostProcessingStage struct{}
    57  
    58  func (FakeDNSPostProcessingStage) Process(config *Config) error {
    59  	fakeDNSInUse := false
    60  	isIPv4Enable, isIPv6Enable := true, true
    61  
    62  	if config.DNSConfig != nil {
    63  		for _, v := range config.DNSConfig.Servers {
    64  			if v.Address.Family().IsDomain() && strings.EqualFold(v.Address.Domain(), "fakedns") {
    65  				fakeDNSInUse = true
    66  			}
    67  		}
    68  
    69  		switch strings.ToLower(config.DNSConfig.QueryStrategy) {
    70  		case "useip4", "useipv4", "use_ip4", "use_ipv4", "use_ip_v4", "use-ip4", "use-ipv4", "use-ip-v4":
    71  			isIPv4Enable, isIPv6Enable = true, false
    72  		case "useip6", "useipv6", "use_ip6", "use_ipv6", "use_ip_v6", "use-ip6", "use-ipv6", "use-ip-v6":
    73  			isIPv4Enable, isIPv6Enable = false, true
    74  		}
    75  	}
    76  
    77  	if fakeDNSInUse {
    78  		// Add a Fake DNS Config if there is none
    79  		if config.FakeDNS == nil {
    80  			config.FakeDNS = &FakeDNSConfig{}
    81  			switch {
    82  			case isIPv4Enable && isIPv6Enable:
    83  				config.FakeDNS.pools = []*FakeDNSPoolElementConfig{
    84  					{
    85  						IPPool:  "198.18.0.0/15",
    86  						LRUSize: 32768,
    87  					},
    88  					{
    89  						IPPool:  "fc00::/18",
    90  						LRUSize: 32768,
    91  					},
    92  				}
    93  			case !isIPv4Enable && isIPv6Enable:
    94  				config.FakeDNS.pool = &FakeDNSPoolElementConfig{
    95  					IPPool:  "fc00::/18",
    96  					LRUSize: 65535,
    97  				}
    98  			case isIPv4Enable && !isIPv6Enable:
    99  				config.FakeDNS.pool = &FakeDNSPoolElementConfig{
   100  					IPPool:  "198.18.0.0/15",
   101  					LRUSize: 65535,
   102  				}
   103  			}
   104  		}
   105  
   106  		found := false
   107  		// Check if there is a Outbound with necessary sniffer on
   108  		var inbounds []InboundDetourConfig
   109  
   110  		if len(config.InboundConfigs) > 0 {
   111  			inbounds = append(inbounds, config.InboundConfigs...)
   112  		}
   113  		for _, v := range inbounds {
   114  			if v.SniffingConfig != nil && v.SniffingConfig.Enabled && v.SniffingConfig.DestOverride != nil {
   115  				for _, dov := range *v.SniffingConfig.DestOverride {
   116  					if strings.EqualFold(dov, "fakedns") || strings.EqualFold(dov, "fakedns+others") {
   117  						found = true
   118  						break
   119  					}
   120  				}
   121  			}
   122  		}
   123  		if !found {
   124  			newError("Defined FakeDNS but haven't enabled FakeDNS destOverride at any inbound.").AtWarning().WriteToLog()
   125  		}
   126  	}
   127  
   128  	return nil
   129  }