38 lines
1.2 KiB
Python
38 lines
1.2 KiB
Python
import GeoIP
|
|
import MySQLdb as sql
|
|
import config
|
|
|
|
gdb = '/usr/share/GeoIP/GeoLiteCity.dat'
|
|
|
|
def add_host(c, ip):
|
|
print 'adding host for', ip
|
|
gi = GeoIP.open(gdb, GeoIP.GEOIP_STANDARD)
|
|
rec = gi.record_by_addr(ip) or {}
|
|
city = rec.get('city')
|
|
country = rec.get('country_name')
|
|
if city and country:
|
|
geo = city + ', ' + country
|
|
else:
|
|
geo = city or country or ''
|
|
c.execute('insert into f2s_hosts (ip, longitude, latitude, country_code, country, geo)\
|
|
values (%s, %s, %s, %s, %s, %s)', [ip, rec.get('longitude'), rec.get('latitude'),
|
|
rec.get('country_code'), country, geo])
|
|
return c.lastrowid
|
|
|
|
def banned(name, protocol, port, ip, *a):
|
|
conn = sql.connect(config.db_host, config.db_user, config.db_pass, config.db)
|
|
c = conn.cursor(sql.cursors.DictCursor)
|
|
n = c.execute('select id from f2s_hosts where ip = %s', [ip])
|
|
if n == 0:
|
|
hid = add_host(c, ip)
|
|
else:
|
|
hid = c.fetchone()['id']
|
|
print 'logging ban for host', ip
|
|
c.execute('insert into f2s_bans (id, name, protocol, port) values\
|
|
(%s, %s, %s, %s)', [hid, name, protocol, port])
|
|
|
|
if __name__ == '__main__':
|
|
from sys import argv
|
|
argv[3] = int(argv[3])
|
|
banned(*argv[1:])
|