-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathip_checker.py
More file actions
54 lines (42 loc) · 1.58 KB
/
Copy pathip_checker.py
File metadata and controls
54 lines (42 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# -*- coding:utf-8 -*-
import datetime
import pygeoip
from config import config
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
gic = pygeoip.GeoIP(config['current_path'] + '/GeoLiteCity.dat')
def get_country_and_city(ip_address):
global gic
# noinspection PyBroadException
try:
res = gic.record_by_addr(ip_address)['country_name'] + ', ' + gic.record_by_addr(ip_address)['city']
except:
res = 'Unknown'
return res
# Read the white list
ip_white_list = set()
with open(config['current_path'] + "/ip_white_list.csv") as f:
for line in f:
ip_white_list.add(get_country_and_city(str(line).split('\r')[0]))
# check all the ip
ip_pool = set()
with open(config['shadowsocks_log_path'] + "/shadowsocks.log") as f:
for line in reversed(list(f)):
list_of_content = str(line).split(' ')
# noinspection PyBroadException
try:
content_datetime = datetime.datetime.strptime(list_of_content[0] + list_of_content[1], '%Y-%m-%d%H:%M:%S')
except:
continue
if (datetime.datetime.now() - content_datetime).days >= 1:
break
if list_of_content[2] == 'INFO' and list_of_content[-2] == 'from':
ip = list_of_content[-1].split(':')[0]
ip_pool.add(ip)
# check for unusual ip
for ip in list(ip_pool):
if get_country_and_city(ip) not in ip_white_list:
print 'Unusual access within 1 day: ' + ip + ' from ' + get_country_and_city(ip) + '\r\n'
print 'Whitelist of cities: ', list(ip_white_list), '\r\n'
print 'Access within 1 day: ', list(ip_pool), '\r\n'