+#include "version.hh"
#include "ext/luawrapper/include/LuaContext.hpp"
#include "lua-auth4.hh"
#include <thread>
#include "ueberbackend.hh"
#include <boost/format.hpp>
#include "dnsrecords.hh"
+#include "dns_random.hh"
#include "../modules/geoipbackend/geoipinterface.hh" // only for the enum
setDown(rem, url, opts);
for(bool first=true;;first=false) {
try {
- MiniCurl mc;
+ string useragent = productName();
+ if (opts.count("useragent")) {
+ useragent = opts.at("useragent");
+ }
+ MiniCurl mc(useragent);
string content;
if(opts.count("source")) {
if (ips.empty()) {
throw std::invalid_argument("The IP list cannot be empty");
}
- return ips[random() % ips.size()];
+ return ips[dns_random(ips.size())];
}
static ComboAddress hashed(const ComboAddress& who, const vector<ComboAddress>& ips)
sum += i.first;
pick.push_back({sum, i.second});
}
- int r = random() % sum;
+ int r = dns_random(sum);
auto p = upper_bound(pick.begin(), pick.end(),r, [](int r, const decltype(pick)::value_type& a) { return r < a.first;});
return p->second;
}
// cout<<" distance: "<<sqrt(dist2) * 40000.0/360<<" km"<<endl; // length of a degree
ranked[dist2].push_back(c);
}
- return ranked.begin()->second[random() % ranked.begin()->second.size()];
+ return ranked.begin()->second[dns_random(ranked.begin()->second.size())];
}
static std::vector<DNSZoneRecord> lookup(const DNSName& name, uint16_t qtype, int zoneid)
return ret;
}
+static thread_local unique_ptr<AuthLua4> s_LUA;
+bool g_LuaRecordSharedState;
+
std::vector<shared_ptr<DNSRecordContent>> luaSynth(const std::string& code, const DNSName& query, const DNSName& zone, int zoneid, const DNSPacket& dnsp, uint16_t qtype)
{
- AuthLua4 alua;
+ if(!s_LUA || // we don't have a Lua state yet
+ !g_LuaRecordSharedState) { // or we want a new one even if we had one
+ s_LUA = make_unique<AuthLua4>();
+ }
std::vector<shared_ptr<DNSRecordContent>> ret;
- LuaContext& lua = *alua.getLua();
+ LuaContext& lua = *s_LUA->getLua();
lua.writeVariable("qname", query);
lua.writeVariable("who", dnsp.getRemote());
lua.writeVariable("dh", (dnsheader*)&dnsp.d);
for(const auto& nmpair : netmasks) {
Netmask nm(nmpair.second);
if(nm.match(bestwho)) {
- return destinations[random() % destinations.size()].second;
+ return destinations[dns_random(destinations.size())].second;
}
}
}
ret.push_back(DNSRecordContent::mastermake(qtype, QClass::IN, content ));
}
} catch(std::exception &e) {
- g_log<<Logger::Error<<"Lua record reported: "<<e.what()<<endl;
+ g_log<<Logger::Error<<"Lua record reported: "<<e.what();
+ try {
+ std::rethrow_if_nested(e);
+ g_log<<endl;
+ } catch(const std::exception& ne) {
+ g_log << ": " << ne.what() << std::endl;
+ }
+ catch(const PDNSException& ne) {
+ g_log << ": " << ne.reason << std::endl;
+ }
throw ;
}