15
15
import falcon
16
16
import maxminddb
17
17
18
- version = "0.5 "
18
+ version = "0.6 "
19
19
config = configparser .ConfigParser ()
20
20
config .read ('etc/server.conf' )
21
21
mmdb_file = config ['global' ].get ('mmdb_file' )
30
30
31
31
if pubsub :
32
32
import redis
33
+
33
34
rdb = redis .Redis (host = '127.0.0.1' )
34
35
35
36
mmdbs = []
@@ -70,62 +71,57 @@ def countryLookup(country: str) -> dict:
70
71
return {}
71
72
72
73
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
+
73
96
class GeoLookup :
74
97
def on_get (self , req , resp , value ):
75
- ret = []
76
98
ua = req .get_header ('User-Agent' )
77
99
ips = req .access_route
78
100
if not validIPAddress (value ):
79
101
resp .status = falcon .HTTP_422
80
102
resp .media = "IPv4 or IPv6 address is in an incorrect format. Dotted decimal for IPv4 or textual representation for IPv6 are required."
81
103
return
82
104
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 )
97
106
98
107
99
108
class MyGeoLookup :
100
109
def on_get (self , req , resp ):
101
- ret = []
102
110
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 ])
116
112
117
113
118
114
app = falcon .App ()
119
115
120
116
app .add_route ('/geolookup/{value}' , GeoLookup ())
121
117
app .add_route ('/' , MyGeoLookup ())
122
118
119
+
123
120
def main ():
124
121
with make_server ('' , port , app ) as httpd :
125
122
print (f'Serving on port { port } ...' )
126
123
httpd .serve_forever ()
127
124
128
125
129
126
if __name__ == '__main__' :
130
- main ()
131
-
127
+ main ()
0 commit comments