Skip to content

Commit 11f4904

Browse files
committed
fixed web interface, optimized location db
1 parent fb6ca09 commit 11f4904

File tree

3 files changed

+59
-56
lines changed

3 files changed

+59
-56
lines changed

models.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@
1212
**settings.DATABASE_CONNECTION_KWARGS,
1313
)
1414

15+
location_database_reader = None
16+
17+
def init_location_db_reader():
18+
global location_database_reader
19+
if os.path.isfile(settings.GEOLITE2_CITY_FILE_LOCATION):
20+
location_database_reader = geoip2.database.Reader(settings.GEOLITE2_CITY_FILE_LOCATION)
21+
else:
22+
# DB doesn`t exists
23+
log.warning("Public IP Database is not found. See GEOLITE2_CITY_FILE_LOCATION in settings.py")
24+
25+
init_location_db_reader()
26+
1527
class Proxy(peewee.Model):
1628
class Meta:
1729
database = raw_db
@@ -63,23 +75,18 @@ def get_raw_protocol(self):
6375

6476
@property
6577
def location(self):
66-
if os.path.isfile(settings.GEOLITE2_CITY_FILE_LOCATION):
67-
reader = geoip2.database.Reader(settings.GEOLITE2_CITY_FILE_LOCATION)
68-
response = reader.city(self.domain)
69-
70-
return {
71-
'latitude': response.location.latitude,
72-
'longitude': response.location.longitude,
73-
'country_code': response.country.iso_code,
74-
'country': response.country.name,
75-
'city': response.city.name,
76-
}
77-
else:
78-
'''
79-
DB doesn`t exists
80-
'''
81-
log.warning("Public IP Database has been absent")
78+
if location_database_reader is None:
8279
return None
80+
81+
response = location_database_reader.city(self.domain)
82+
83+
return {
84+
'latitude': response.location.latitude,
85+
'longitude': response.location.longitude,
86+
'country_code': response.country.iso_code,
87+
'country': response.country.name,
88+
'city': response.city.name,
89+
}
8390

8491
@property
8592
def address(self):

server/frontend/app.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,7 @@ async def get_proxies_html(self, request):
110110
"number_of_bad_checks": proxy.number_of_bad_checks,
111111
"bad_proxy": proxy.bad_proxy,
112112
"white_ipv4": proxy.white_ipv4,
113-
"city": proxy.city,
114-
"region": proxy.region,
115-
"country_code": proxy.country_code,
113+
"location": proxy.location,
116114
} for proxy in proxies]
117115
}
118116

server/templates/proxies.html

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,41 @@
11
{% extends "base.html" %}
22
{% block body %}
33
<table id=proxies>
4-
<thead>
5-
<tr>
6-
<td>address</td>
7-
<td>response_time</td>
8-
<td>uptime</td>
9-
<td>bad_uptime</td>
10-
<td>last_check_time</td>
11-
<td>checking_period</td>
12-
<td>number_of_bad_checks</td>
13-
<td>bad_proxy</td>
14-
<td>ip</td>
15-
<td>city</td>
16-
<td>region</td>
17-
<td>country</td>
18-
</tr>
19-
</thead>
20-
<tbody>
4+
<thead>
5+
<tr>
6+
<td>address</td>
7+
<td>response_time</td>
8+
<td>uptime</td>
9+
<td>bad_uptime</td>
10+
<td>last_check_time</td>
11+
<td>checking_period</td>
12+
<td>number_of_bad_checks</td>
13+
<td>bad_proxy</td>
14+
<td>ip</td>
15+
<td>city</td>
16+
<td>country</td>
17+
</tr>
18+
</thead>
19+
<tbody>
2120

22-
{% for proxy in proxies %}
23-
<tr>
24-
<td>{{ proxy.address }}</td>
25-
<td>{{ proxy.response_time }} ms</td>
26-
<td>{{ proxy.uptime }}</td>
27-
<td>{{ proxy.bad_uptime }}</td>
28-
<td id="proxy_{{ loop.index }}_last_check_time">{{ proxy.last_check_time }}</td>
29-
<td>{{ proxy.checking_period }}</td>
30-
<td>{{ proxy.number_of_bad_checks }}</td>
31-
<td>{{ proxy.bad_proxy }}</td>
32-
<td>{{ proxy.white_ipv4 }}</td>
33-
<td>{{ proxy.city }}</td>
34-
<td>{{ proxy.region }}</td>
35-
<td>{{ proxy.country_code }}</td>
36-
</tr>
37-
<script>
38-
proxy_{{ loop.index }}_last_check_time.textContent = new Date({{ proxy.last_check_time }} * 1000);
39-
</script>
40-
{% endfor %}
41-
</tbody>
21+
{% for proxy in proxies %}
22+
<tr>
23+
<td>{{ proxy.address }}</td>
24+
<td>{{ proxy.response_time }} ms</td>
25+
<td>{{ proxy.uptime }}</td>
26+
<td>{{ proxy.bad_uptime }}</td>
27+
<td id="proxy_{{ loop.index }}_last_check_time">{{ proxy.last_check_time }}</td>
28+
<td>{{ proxy.checking_period }}</td>
29+
<td>{{ proxy.number_of_bad_checks }}</td>
30+
<td>{{ proxy.bad_proxy }}</td>
31+
<td>{{ proxy.white_ipv4 }}</td>
32+
<td>{{ proxy.location['city'] }}</td>
33+
<td>{{ proxy.location['country_code'] }}</td>
34+
</tr>
35+
<script>
36+
proxy_{{ loop.index }}_last_check_time.textContent = new Date({{ proxy.last_check_time }} * 1000);
37+
</script>
38+
{% endfor %}
39+
</tbody>
4240
</table>
4341
{% endblock %}

0 commit comments

Comments
 (0)