fakedns.go 1.5 KB

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