From: bert hubert Date: Sat, 9 Dec 2017 22:43:17 +0000 (+0100) Subject: implement 'weak' geobackend dependency, abstract out functionality X-Git-Tag: dnsdist-1.3.1~136^2~51 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4a913a28cd0d7bccce6d558454999a779f64eb4e;p=thirdparty%2Fpdns.git implement 'weak' geobackend dependency, abstract out functionality --- diff --git a/modules/geoipbackend/geoipbackend.cc b/modules/geoipbackend/geoipbackend.cc index 7bbdd42364..ab299da01e 100644 --- a/modules/geoipbackend/geoipbackend.cc +++ b/modules/geoipbackend/geoipbackend.cc @@ -1098,6 +1098,27 @@ bool GeoIPBackend::hasDNSSECkey(const DNSName& name) { return false; } +string getGeo(const std::string& ip, GeoIPBackend::GeoIPQueryAttribute qa) +{ + try { + GeoIPBackend gib; + GeoIPLookup gl; + string res=gib.queryGeoIP(ip, false, qa, &gl); + // cout<<"Result for "< d_result; }; +std::string getGeo(const std::string& ip, GeoIPBackend::GeoIPQueryAttribute qa); + #endif /* PDNS_GEOIPBACKEND_HH */ diff --git a/pdns/lua-record.cc b/pdns/lua-record.cc index 4b22e1930c..f7fff7d107 100644 --- a/pdns/lua-record.cc +++ b/pdns/lua-record.cc @@ -191,26 +191,28 @@ bool doCompare(const T& var, const std::string& res, const C& cmp) return false; } } -static string getGeo(const std::string& ip, GeoIPBackend::GeoIPQueryAttribute qa) + + +std::string getGeo(const std::string& ip, GeoIPBackend::GeoIPQueryAttribute qa) __attribute__((weak)); + +std::string getGeo(const std::string& ip, GeoIPBackend::GeoIPQueryAttribute qa) { - try { - GeoIPBackend gib; - GeoIPLookup gl; - string res=gib.queryGeoIP(ip, false, qa, &gl); - // cout<<"Result for "<& ips) +{ + return ips[random() % ips.size()]; +} + +static ComboAddress hashed(const ComboAddress& who, vector& ips) +{ + ComboAddress::addressOnlyHash aoh; + return ips[aoh(who) % ips.size()]; +} + + static ComboAddress wrandom(vector >& wips) { int sum=0; @@ -376,7 +378,7 @@ std::vector> luaSynth(const std::string& code, cons }); - lua.writeFunction("ifportup", [&bestwho](int port, const vector >& ips, const std::unordered_map& options) { + lua.writeFunction("ifportup", [&bestwho](int port, const vector >& ips, const boost::optional> options) { vector candidates; for(const auto& i : ips) { ComboAddress rem(i.second, port); @@ -390,7 +392,22 @@ std::vector> luaSynth(const std::string& code, cons ret.push_back(i.second); } else { - auto res=closest(bestwho, candidates); + ComboAddress res; + string selector="random"; + if(options) { + if(options->count("selector")) + selector=options->find("selector")->second; + } + if(selector=="random") + res=pickrandom(candidates); + else if(selector=="closest") + res=closest(bestwho, candidates); + else if(selector=="hashed") + res=hashed(bestwho, candidates); + else { + L<