Skip to content

Commit d718f18

Browse files
committed
Add IPInfo mmdb Support
1 parent 4d48f12 commit d718f18

File tree

3 files changed

+30
-33
lines changed

3 files changed

+30
-33
lines changed

etc/server.conf.sample

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[global]
22
mmdb_file = db/GeoOpen-Country.mmdb,db/GeoOpen-Country-ASN.mmdb
3+
#mmdb_file = db/ipinfo_country_asn.mmdb,db/ipinfo_country.mmdb
34
country_file = db/country.json
45
lookup_pubsub = no
56
port = 8000

mmdb_server/mmdb_server.py

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import falcon
1616
import maxminddb
1717

18-
version = "0.5"
18+
version = "0.6"
1919
config = configparser.ConfigParser()
2020
config.read('etc/server.conf')
2121
mmdb_file = config['global'].get('mmdb_file')
@@ -30,6 +30,7 @@
3030

3131
if pubsub:
3232
import redis
33+
3334
rdb = redis.Redis(host='127.0.0.1')
3435

3536
mmdbs = []
@@ -70,62 +71,57 @@ def countryLookup(country: str) -> dict:
7071
return {}
7172

7273

74+
def _process_lookup(ip):
75+
"""Helper function to process IP lookup and format response."""
76+
ret = []
77+
for mmdb in mmdbs:
78+
georesult = mmdb['reader'].get(ip) or {} # Ensure dictionary, prevent NoneType errors
79+
m = mmdb.copy()
80+
del m['reader']
81+
georesult['meta'] = m
82+
georesult['ip'] = ip
83+
84+
# Determine country code from old or new format
85+
country_code = None
86+
if isinstance(georesult.get('country'), dict):
87+
country_code = georesult['country'].get('iso_code')
88+
elif isinstance(georesult.get('country'), str):
89+
country_code = georesult['country']
90+
91+
georesult['country_info'] = countryLookup(country_code) if country_code else {}
92+
ret.append(georesult)
93+
return ret
94+
95+
7396
class GeoLookup:
7497
def on_get(self, req, resp, value):
75-
ret = []
7698
ua = req.get_header('User-Agent')
7799
ips = req.access_route
78100
if not validIPAddress(value):
79101
resp.status = falcon.HTTP_422
80102
resp.media = "IPv4 or IPv6 address is in an incorrect format. Dotted decimal for IPv4 or textual representation for IPv6 are required."
81103
return
82104
pubLookup(value=f'{value} via {ips} using {ua}')
83-
for mmdb in mmdbs:
84-
m = {}
85-
georesult = mmdb['reader'].get(value)
86-
m = mmdb.copy()
87-
del m['reader']
88-
georesult['meta'] = m
89-
georesult['ip'] = value
90-
if georesult['country']['iso_code'] != 'None':
91-
georesult['country_info'] = countryLookup(country=georesult['country']['iso_code'])
92-
else:
93-
georesult['country_info'] = {}
94-
ret.append(georesult)
95-
resp.media = ret
96-
return
105+
resp.media = _process_lookup(value)
97106

98107

99108
class MyGeoLookup:
100109
def on_get(self, req, resp):
101-
ret = []
102110
ips = req.access_route
103-
for mmdb in mmdbs:
104-
georesult = mmdb['reader'].get(ips[0])
105-
m = mmdb.copy()
106-
del m['reader']
107-
georesult['meta'] = m
108-
georesult['ip'] = ips[0]
109-
if georesult['country']['iso_code'] != 'None':
110-
georesult['country_info'] = countryLookup(country=georesult['country']['iso_code'])
111-
else:
112-
georesult['country_info'] = {}
113-
ret.append(georesult)
114-
resp.media = ret
115-
return
111+
resp.media = _process_lookup(ips[0])
116112

117113

118114
app = falcon.App()
119115

120116
app.add_route('/geolookup/{value}', GeoLookup())
121117
app.add_route('/', MyGeoLookup())
122118

119+
123120
def main():
124121
with make_server('', port, app) as httpd:
125122
print(f'Serving on port {port}...')
126123
httpd.serve_forever()
127124

128125

129126
if __name__ == '__main__':
130-
main()
131-
127+
main()

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "mmdb-server"
3-
version = "0.5.0"
3+
version = "0.6.0"
44
description = ""
55
authors = ["Alexandre Dulaunoy"]
66
readme = "README.md"

0 commit comments

Comments
 (0)