abh / geodns

DNS server with per-client targeted responses
Apache License 2.0
1.37k stars 191 forks source link

Don't resolve record directly #142

Open ghost opened 2 months ago

ghost commented 2 months ago

Hello there,

I wanted to know is there any way with this library to prevent direct lookup results for de.example.com, europe.example.com, ... and only allow resolves for example.com ?

Thanks in advance.

ghost commented 2 months ago

@abh

I can add this code to the serve.go and it will resolve the issue, do you think this is the right way? Should I open pull request?

if len(labelMatches) == 1 {
  var countryCode, continentCode string

  if len(targets) == 3 {
      countryCode, continentCode = targets[1], targets[2]
  } else if len(targets) == 2 {
      if countries.CountryContinent[targets[1]] != "" {
          countryCode = targets[1]
      } else {
          continentCode = targets[1]
      }
  }

  cc := strings.SplitN(labelMatches[0].Label.Label, ".", 2)

  if len(cc) == 2 && (countries.CountryContinent[cc[1]] != "" || len(countries.ContinentCountries[cc[1]]) > 0) {
      newTargets := targets

      if cc[1] != countryCode && cc[1] != continentCode {
          newTargets = append(newTargets, cc[1])
      }

      parentLabelMatches := z.FindLabels(cc[0], newTargets, []uint16{dns.TypeMF, dns.TypeCNAME, qtype})

      if len(parentLabelMatches) > 1 {
          // empty labelMatches
          labelMatches = []zones.LabelMatch{}
      }
  }
}

Also this must be added to imports on top:

"github.com/abh/geodns/v3/countries"
abh commented 2 months ago

Something like that could work with an option to enable the feature.

Though I think it'd make more sense to "hide" the targeting rules in some way in a separate part of the data structure (and then if the data comes that way then they can't be queried directly).

Many years ago @miekg suggested having the zone format be a regular "RFC 1035 zone" with extra metadata for the targeting information; that might be worth revisiting again.

ghost commented 2 months ago

@abh I have managed to code it somehow, but I don't know where to put for configuration or if its the right decision, take a look:

https://github.com/abh/geodns/commit/6047cb9567c651e41e1d1cbe1e3ac2ff0bc736f3