forked from tosterkamp/random-redirect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
119 lines (105 loc) · 3.83 KB
/
server.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
import random
import requests
from datetime import datetime, timedelta
import threading
import time
import logging
import uwsgi
def application(env, start_response):
if(env['HTTP_HOST'].startswith('jitsi')):
redirect_server = jitsi.get_random()
elif(env['HTTP_HOST'].startswith('poll')):
redirect_server = poll.get_random()
elif(env['HTTP_HOST'].startswith('pad')):
redirect_server = pad.get_random()
elif(env['HTTP_HOST'].startswith('codimd')):
redirect_server = codimd.get_random()
elif(env['HTTP_HOST'].startswith('cryptpad')):
redirect_server = cryptpad.get_random()
elif(env['HTTP_HOST'].startswith('etherpad')):
redirect_server = etherpad.get_random()
elif(env['HTTP_HOST'].startswith('ethercalc')):
redirect_server = ethercalc.get_random()
else:
redirect_server = 'https://github.com/tosterkamp/random-redirect/'
print('redirect from ' + env['HTTP_HOST'] + ' to ' + redirect_server)
start_response('302', [('Location', redirect_server)])
return [b"redirected"]
class ServerList:
def __init__(self, filename, t_request):
"""
@param filename: name of the file with the serverlist in the folder res
@param t_request: file, which is located on the searched servers, which can be used to measure the speed of the server
"""
self.lock = threading.Lock()
self.file = 'res/' + filename
self.test_request = t_request;
self.servers = []
self.offline_servers = []
self.renew()
def get_random(self):
"""
return a random server from self.list
"""
self.lock.acquire()
tmp = random.choice(self.servers)
self.lock.release()
return tmp
def renew(self):
"""
get current list from filesystem and check if the servers are online
"""
if self.test_request is None:
with open(self.file, 'r') as f:
self.servers = f.readlines()
else:
online = []
offline = []
with open(self.file, 'r') as f:
all_servers = f.readlines()
for x in all_servers:
try:
r = requests.get(str.rstrip(x) + self.test_request, timeout=0.5)
if (r.status_code == requests.codes.ok):
#print(str.rstrip(x) + " is online")
online.append(x)
else:
#print(str.rstrip(x) + " returns status code: " + str(r.status_code))
offline.append(x)
except Exception as inst:
#print(str.rstrip(x) + " Error: " + str(inst))
offline.append(x)
#print("offline: ")
#print(offline)
self.lock.acquire()
self.servers = online
self.offline_servers = offline
self.lock.release()
#print("online: ")
#print(self.servers)
jitsi = ServerList('jitsi_servers.lst', 'sounds/outgoingRinging.wav')
poll = ServerList('poll_servers.lst', 'images/date.png')
pad = ServerList('pad_servers.lst', None)
codimd = ServerList('codimd_servers.lst', 'screenshot.png')
cryptpad = ServerList('cryptpad_servers.lst', 'customize/images/AGPL.png')
etherpad = ServerList('etherpad_servers.lst', 'locales.json')
ethercalc = ServerList('ethercalc_servers.lst', 'static/img/davy/bg/home2.png')
def reload(signum):
print("start reload")
global jitsi
global poll
global pad
global codimd
global cryptpad
global etherpad
global ethercalc
jitsi.renew()
poll.renew()
pad.renew()
codimd.renew()
cryptpad.renew()
etherpad.renew()
ethercalc.renew()
print("finish reload")
uwsgi.register_signal(99, "", reload)
uwsgi.add_timer(99, 600)