Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#130 : maxmind for ip import instead of whois.
- Loading branch information
1 parent
6aa34f7
commit 75df165
Showing
6 changed files
with
122 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import geoip2.database | ||
import os | ||
import pyzipcode | ||
|
||
from ipaddr import IPAddress | ||
from app import app | ||
from app.models import cfg_settings | ||
|
||
app.config["GEOIP_ASN_DATABASE_FILE"] = cfg_settings.Cfg_settings.get_private_setting("GEOIP_ASN_DATABASE_FILE") | ||
if not app.config["GEOIP_ASN_DATABASE_FILE"]: | ||
app.config["GEOIP_ASN_DATABASE_FILE"] = os.getenv('GEOIP_ASN_DATABASE_FILE', | ||
'/usr/local/ThreatKB/MaxMind/GeoLite2-ASN.mmdb') | ||
|
||
app.config["GEOIP_CITY_DATABASE_FILE"] = cfg_settings.Cfg_settings.get_private_setting("GEOIP_CITY_DATABASE_FILE") | ||
if not app.config["GEOIP_CITY_DATABASE_FILE"]: | ||
app.config["GEOIP_CITY_DATABASE_FILE"] = os.getenv('GEOIP_CITY_DATABASE_FILE', | ||
'/usr/local/ThreatKB/MaxMind/GeoLite2-City.mmdb') | ||
|
||
GEOIP_ASN_DATABASE_FILE = app.config["GEOIP_ASN_DATABASE_FILE"] | ||
GEOIP_CITY_DATABASE_FILE = app.config["GEOIP_CITY_DATABASE_FILE"] | ||
ASN_READER = geoip2.database.Reader(GEOIP_ASN_DATABASE_FILE) | ||
CITY_READER = geoip2.database.Reader(GEOIP_CITY_DATABASE_FILE) | ||
ZIPCODE_DB = pyzipcode.ZipCodeDatabase() | ||
|
||
|
||
def get_geo_for_ip(ip_address): | ||
try: | ||
ip_address = IPAddress(ip_address) | ||
if not ip_address.is_private: | ||
asn_info = ASN_READER.asn(ip_address) | ||
city_info = CITY_READER.city(ip_address) | ||
zip_code = city_info.postal.code | ||
return dict( | ||
ip=ip_address, | ||
asn=asn_info.autonomous_system_organization, | ||
country_code=city_info.country.iso_code, | ||
city=city_info.city.names["en"], | ||
state=ZIPCODE_DB[zip_code].state, | ||
zip_code=zip_code, | ||
country=city_info.country.names["en"], | ||
continent=city_info.continent.names["en"] | ||
) | ||
return None | ||
except Exception as e: | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
migrations/versions/665baa4d3f57_geoip_database_config_settings.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
"""GEOIP database config settings | ||
Revision ID: 665baa4d3f57 | ||
Revises: 34d5b6b940a7 | ||
Create Date: 2017-11-12 16:20:53.697870 | ||
""" | ||
import datetime | ||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
from app.models import cfg_settings | ||
|
||
revision = '665baa4d3f57' | ||
down_revision = '34d5b6b940a7' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
date_created = datetime.datetime.now().isoformat() | ||
date_modified = datetime.datetime.now().isoformat() | ||
|
||
op.bulk_insert( | ||
cfg_settings.Cfg_settings.__table__, | ||
[ | ||
{"key": "GEOIP_ASN_DATABASE_FILE", | ||
"value": '/usr/local/ThreatKB/MaxMind/GeoLite2-ASN.mmdb', | ||
"public": True, | ||
"date_created": date_created, | ||
"date_modified": date_modified}, | ||
{"key": "GEOIP_CITY_DATABASE_FILE", | ||
"value": '/usr/local/ThreatKB/MaxMind/GeoLite2-City.mmdb', | ||
"public": True, | ||
"date_created": date_created, | ||
"date_modified": date_modified} | ||
] | ||
) | ||
|
||
|
||
def downgrade(): | ||
keys = ["GEOIP_ASN_DATABASE_FILE", "GEOIP_CITY_DATABASE_FILE"] | ||
for key in keys: | ||
op.execute("""DELETE from cfg_settings where `key`='%s';""" % key) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters