-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxymanager.py
56 lines (45 loc) · 1.52 KB
/
proxymanager.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
import database
import traceback
import time
import random
from multiprocessing import Lock
PROXYS = {}
EXCLUDE = ["packet"]
class ProxyManager():
@staticmethod
def updateProxys():
"""
Fetch newest proxys from the database
"""
global PROXYS
try:
newProxys = database.getProxys()
except Exception as e:
print(f"[DATABASE] Exception found: {traceback.format_exc()}")
time.sleep(10)
database.Connect()
return ProxyManager.updateProxys()
if newProxys != PROXYS:
PROXYS = newProxys
return True
else:
return False
def __init__(self, proxygroups=[]):
self.proxygroups = proxygroups
self.proxys = []
self.lock = Lock()
for group in PROXYS:
if (self.proxygroups and group in self.proxygroups) or (not self.proxygroups and group not in EXCLUDE):
self.proxys.append(PROXYS[group])
self.proxys = sum(self.proxys, [])
self.currentProxy = random.randint(0, len(self.proxys)-1) if self.proxys else 0
def next(self):
"""
Get the next Proxy
"""
with self.lock:
self.currentProxy = 0 if self.currentProxy >= (len(self.proxys) - 1) or not self.proxys else self.currentProxy + 1
return {
"http":f"http://{self.proxys[self.currentProxy]}",
"https":f"http://{self.proxys[self.currentProxy]}"
} if self.proxys else {}