-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathmodels.py
227 lines (174 loc) · 6.23 KB
/
models.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import logging
import os.path
import geoip2.database
import peewee
import peewee_async
from proxy_py import settings
log = logging.getLogger("proxy_py/main")
raw_db = peewee_async.PooledPostgresqlDatabase(
*settings.DATABASE_CONNECTION_ARGS,
**settings.DATABASE_CONNECTION_KWARGS,
)
location_database_reader = None
def init_location_db_reader():
global location_database_reader
if os.path.isfile(settings.GEOLITE2_CITY_FILE_LOCATION):
location_database_reader = geoip2.database.Reader(
settings.GEOLITE2_CITY_FILE_LOCATION
)
else:
# DB doesn`t exists
log.warning(
"Public IP Database is not found. See GEOLITE2_CITY_FILE_LOCATION in settings.py"
)
init_location_db_reader()
class Proxy(peewee.Model):
class Meta:
database = raw_db
db_table = "proxies"
indexes = (
(("raw_protocol", "auth_data", "domain", "port"), True),
(("auth_data", "domain", "port"), False), # important!
(("raw_protocol",), False),
(("auth_data",), False),
(("domain",), False),
(("port",), False),
(("number_of_bad_checks",), False),
(("next_check_time",), False),
(("last_check_time",), False),
(("checking_period",), False),
(("uptime",), False),
(("bad_uptime",), False),
(("response_time",), False),
(("_white_ipv4",), False),
(("_white_ipv6",), False),
)
PROTOCOLS = (
"http",
"socks4",
"socks5",
)
raw_protocol = peewee.SmallIntegerField(null=False)
domain = peewee.CharField(settings.DB_MAX_DOMAIN_LENGTH, null=False)
port = peewee.IntegerField(null=False)
auth_data = peewee.CharField(
settings.DB_AUTH_DATA_MAX_LENGTH, default="", null=False
)
checking_period = peewee.IntegerField(
default=settings.MIN_PROXY_CHECKING_PERIOD, null=False
)
last_check_time = peewee.IntegerField(default=0, null=False)
next_check_time = peewee.IntegerField(default=0, null=False)
number_of_bad_checks = peewee.IntegerField(default=0, null=False)
uptime = peewee.IntegerField(default=None, null=True)
bad_uptime = peewee.IntegerField(default=None, null=True)
# in microseconds
response_time = peewee.IntegerField(default=None, null=True)
# TODO: consider storing as binary
_white_ipv4 = peewee.CharField(16, null=True)
_white_ipv6 = peewee.CharField(45, null=True)
def get_raw_protocol(self):
return self.raw_protocol
@property
def location(self):
if location_database_reader is None:
return None
response = location_database_reader.city(self.domain)
return {
"latitude": response.location.latitude,
"longitude": response.location.longitude,
"country_code": response.country.iso_code,
"country": response.country.name,
"city": response.city.name,
}
@property
def address(self):
return self.to_url()
@property
def protocol(self):
return self.PROTOCOLS[int(self.raw_protocol)]
@protocol.setter
def protocol(self, protocol):
self.raw_protocol = self.PROTOCOLS.index(protocol)
@property
def bad_proxy(self):
return self.number_of_bad_checks > 0
@property
def white_ipv4(self):
return self._white_ipv4
@white_ipv4.setter
def white_ipv4(self, value):
self._white_ipv4 = value
@property
def white_ipv6(self):
return self._white_ipv6
@white_ipv6.setter
def white_ipv6(self, value):
self._white_ipv6 = value
def to_url(self, protocol=None):
address = (
protocol if protocol is not None else self.PROTOCOLS[int(self.raw_protocol)]
)
address += "://"
if self.auth_data:
address += self.auth_data + "@"
address += "{}:{}".format(self.domain, self.port)
return address
def __str__(self):
return self.to_url()
__repr__ = __str__
class ProxyCountItem(peewee.Model):
class Meta:
database = raw_db
db_table = "proxy_count_items"
timestamp = peewee.IntegerField(primary_key=True)
good_proxies_count = peewee.IntegerField(null=False)
bad_proxies_count = peewee.IntegerField(null=False)
dead_proxies_count = peewee.IntegerField(null=False)
class CollectorState(peewee.Model):
class Meta:
database = raw_db
db_table = "collector_states"
indexes = (
(("processing_period",), False),
(("last_processing_time",), False),
)
# python module name
identifier = peewee.TextField(unique=True)
processing_period = peewee.IntegerField(null=False)
last_processing_time = peewee.IntegerField(null=False)
last_processing_proxies_count = peewee.IntegerField(default=0, null=False)
# TODO: add new proxies
last_processing_new_proxies_count = peewee.IntegerField(default=0, null=False)
data = peewee.TextField(default=None, null=True)
class StatBaseModel(peewee.Model):
class Meta:
database = raw_db
timestamp = peewee.BigIntegerField(primary_key=True)
class NumberOfProxiesToProcess(StatBaseModel):
class Meta:
db_table = "number_of_proxies_to_process"
good_proxies = peewee.IntegerField(null=False)
bad_proxies = peewee.IntegerField(null=False)
dead_proxies = peewee.IntegerField(null=False)
class NumberOfCollectorsToProcess(StatBaseModel):
class Meta:
db_table = "number_of_collectors_to_process"
value = peewee.IntegerField(null=False)
class ProcessorProxiesQueueSize(StatBaseModel):
class Meta:
db_table = "processor_proxies_queue_size"
value = peewee.IntegerField(null=False)
_silent = True
Proxy.create_table(_silent)
ProxyCountItem.create_table(_silent)
CollectorState.create_table(_silent)
NumberOfProxiesToProcess.create_table(_silent)
NumberOfCollectorsToProcess.create_table(_silent)
ProcessorProxiesQueueSize.create_table(_silent)
db = peewee_async.Manager(raw_db)
raw_db.execute_sql(
"""CREATE MATERIALIZED VIEW IF NOT EXISTS working_proxies
AS SELECT * FROM proxies WHERE number_of_bad_checks = 0;"""
)
db.allow_sync()