fakedns.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package conf
  2. import (
  3. "github.com/golang/protobuf/proto"
  4. "github.com/v2fly/v2ray-core/v4/app/dns/fakedns"
  5. )
  6. type FakeDNSConfig struct {
  7. IPPool string `json:"ipPool"`
  8. LruSize int64 `json:"poolSize"`
  9. }
  10. func (f FakeDNSConfig) Build() (proto.Message, error) {
  11. return &fakedns.FakeDnsPool{
  12. IpPool: f.IPPool,
  13. LruSize: f.LruSize,
  14. }, nil
  15. }
  16. type FakeDNSPostProcessingStage struct{}
  17. func (FakeDNSPostProcessingStage) Process(conf *Config) error {
  18. var fakeDNSInUse bool
  19. if conf.DNSConfig != nil {
  20. for _, v := range conf.DNSConfig.Servers {
  21. if v.Address.Family().IsDomain() {
  22. if v.Address.Domain() == "fakedns" {
  23. fakeDNSInUse = true
  24. }
  25. }
  26. }
  27. }
  28. if fakeDNSInUse {
  29. if conf.FakeDNS == nil {
  30. // Add a Fake DNS Config if there is none
  31. conf.FakeDNS = &FakeDNSConfig{
  32. IPPool: "198.18.0.0/15",
  33. LruSize: 65535,
  34. }
  35. }
  36. found := false
  37. // Check if there is a Outbound with necessary sniffer on
  38. var inbounds []InboundDetourConfig
  39. if len(conf.InboundConfigs) > 0 {
  40. inbounds = append(inbounds, conf.InboundConfigs...)
  41. }
  42. for _, v := range inbounds {
  43. if v.SniffingConfig != nil && v.SniffingConfig.Enabled && v.SniffingConfig.DestOverride != nil {
  44. for _, dov := range *v.SniffingConfig.DestOverride {
  45. if dov == "fakedns" {
  46. found = true
  47. }
  48. }
  49. }
  50. }
  51. if !found {
  52. newError("Defined Fake DNS but haven't enabled fake dns sniffing at any inbound.").AtWarning().WriteToLog()
  53. }
  54. }
  55. return nil
  56. }