Skip to content

Commit 2850d6f

Browse files
committed
fix: Catching exceptions and results properly depending on the cases
1 parent 5d4a0bf commit 2850d6f

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

misp_modules/modules/expansion/countrycode.py

+14-16
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,22 @@
2020
"gov": "Government (USA)"
2121
}
2222

23-
codes = False
23+
24+
def parse_country_code(extension):
25+
# Retrieve a json full of country info
26+
try:
27+
codes = requests.get("http://www.geognos.com/api/en/countries/info/all.json").json()
28+
except Exception:
29+
return "http://www.geognos.com/api/en/countries/info/all.json not reachable"
30+
if not codes.get('StatusMsg') or not codes["StatusMsg"] == "OK":
31+
return 'Not able to get the countrycode references from http://www.geognos.com/api/en/countries/info/all.json'
32+
for country in codes['Results'].values():
33+
if country['CountryCodes']['tld'] == extension:
34+
return country['Name']
35+
return "Unknown"
2436

2537

2638
def handler(q=False):
27-
global codes
28-
if not codes:
29-
codes = requests.get("http://www.geognos.com/api/en/countries/info/all.json").json()
3039
if q is False:
3140
return False
3241
request = json.loads(q)
@@ -36,18 +45,7 @@ def handler(q=False):
3645
ext = domain.split(".")[-1]
3746

3847
# Check if it's a common, non country one
39-
if ext in common_tlds.keys():
40-
val = common_tlds[ext]
41-
else:
42-
# Retrieve a json full of country info
43-
if not codes["StatusMsg"] == "OK":
44-
val = "Unknown"
45-
else:
46-
# Find our code based on TLD
47-
codes = codes["Results"]
48-
for code in codes.keys():
49-
if codes[code]["CountryCodes"]["tld"] == ext:
50-
val = codes[code]["Name"]
48+
val = common_tlds[ext] if ext in common_tlds.keys() else parse_country_code(ext)
5149
r = {'results': [{'types': ['text'], 'values':[val]}]}
5250
return r
5351

0 commit comments

Comments
 (0)