Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document geoiplookup and expose GeoIPQueryAttribute to Lua #14972

Merged
merged 3 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/actions/spell-check/expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,7 @@ luarule
luawrapper
Lutter
Luuk
lvlt
Machard
Maik
Maikel
Expand Down
29 changes: 29 additions & 0 deletions docs/lua-records/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,35 @@ Reverse DNS functions
Helper functions
~~~~~~~~~~~~~~~~

.. function:: geoiplookup(address, attr)

Retrieve specific attributes related to an IP address.

:param string address: The IP address to lookup.
:param int attr: The attribute identifier for the lookup.

You can use the following constants as the attribute:

- `GeoIPQueryAttribute.ASn`
- `GeoIPQueryAttribute.City`
- `GeoIPQueryAttribute.Continent`
- `GeoIPQueryAttribute.Country`
- `GeoIPQueryAttribute.Country2`
- `GeoIPQueryAttribute.Name`
- `GeoIPQueryAttribute.Region`
- `GeoIPQueryAttribute.Location`

Example::

asn.example.com IN LUA TXT "geoiplookup(bestwho:toString(), GeoIPQueryAttribute.ASn)" ; 1
city.example.com IN LUA TXT "geoiplookup(bestwho:toString(), GeoIPQueryAttribute.City)" ; auckland
continent.example.com IN LUA TXT "geoiplookup(bestwho:toString(), GeoIPQueryAttribute.Continent)" ; oc
country.example.com IN LUA TXT "geoiplookup(bestwho:toString(), GeoIPQueryAttribute.Country)" ; nz
country2.example.com IN LUA TXT "geoiplookup(bestwho:toString(), GeoIPQueryAttribute.Country2)" ; nz
name.example.com IN LUA TXT "geoiplookup(bestwho:toString(), GeoIPQueryAttribute.Name)" ; lvlt-1
region.example.com IN LUA TXT "geoiplookup(bestwho:toString(), GeoIPQueryAttribute.Region)" ; auk
location.example.com IN LUA TXT "geoiplookup(bestwho:toString(), GeoIPQueryAttribute.Location)" ; -36.000000 174.000000

.. function:: asnum(number)
asnum(numbers)

Expand Down
1 change: 1 addition & 0 deletions pdns/lua-record.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1370,6 +1370,7 @@ static void setupLuaRecords(LuaContext& lua) // NOLINT(readability-function-cogn
lua.writeFunction("geoiplookup", [](const string &ip, const GeoIPInterface::GeoIPQueryAttribute attr) {
return getGeo(ip, attr);
});
lua.writeVariable("GeoIPQueryAttribute", std::unordered_map<std::string, int>{{"ASn", GeoIPInterface::GeoIPQueryAttribute::ASn}, {"City", GeoIPInterface::GeoIPQueryAttribute::City}, {"Continent", GeoIPInterface::GeoIPQueryAttribute::Continent}, {"Country", GeoIPInterface::GeoIPQueryAttribute::Country}, {"Country2", GeoIPInterface::GeoIPQueryAttribute::Country2}, {"Name", GeoIPInterface::GeoIPQueryAttribute::Name}, {"Region", GeoIPInterface::GeoIPQueryAttribute::Region}, {"Location", GeoIPInterface::GeoIPQueryAttribute::Location}});
BozhanL marked this conversation as resolved.
Show resolved Hide resolved

typedef const boost::variant<string,vector<pair<int,string> > > combovar_t;

Expand Down
20 changes: 20 additions & 0 deletions regression-tests.auth-py/test_LuaRecords.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ class BaseLuaTest(AuthTest):
dblookup IN LUA A "dblookup('lookmeup.example.org', pdns.A)[1]"

whitespace IN LUA TXT "'foo" "bar'"
geoipqueryattribute IN LUA TXT ("string.format('%d %d %d %d %d %d %d',"
"GeoIPQueryAttribute.ASn, GeoIPQueryAttribute.City, GeoIPQueryAttribute.Continent,"
"GeoIPQueryAttribute.Country, GeoIPQueryAttribute.Country2, GeoIPQueryAttribute.Name,"
"GeoIPQueryAttribute.Region, GeoIPQueryAttribute.Location)")
"""
}
_web_rrsets = []
Expand Down Expand Up @@ -1177,6 +1181,22 @@ def testWhitespace(self, expectws=True):
self.assertRcodeEqual(res, dns.rcode.NOERROR)
self.assertEqual(res.answer, response.answer)

def testGeoIPQueryAttribute(self):
"""
Test GeoIPQueryAttribute enum
"""
name = 'geoipqueryattribute.example.org.'

query = dns.message.make_query(name, 'TXT')

response = dns.message.make_response(query)

response.answer.append(dns.rrset.from_text(name, 0, dns.rdataclass.IN, dns.rdatatype.TXT, '"0 1 2 3 4 5 6"'))

res = self.sendUDPQuery(query)
self.assertRcodeEqual(res, dns.rcode.NOERROR)
self.assertEqual(res.answer, response.answer)


class TestLuaRecordsShared(TestLuaRecords):
_config_template = """
Expand Down
Loading