-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfallback.py
More file actions
executable file
·137 lines (98 loc) · 3.51 KB
/
fallback.py
File metadata and controls
executable file
·137 lines (98 loc) · 3.51 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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# (c) 2019, Vlad Glagolev <stealth@sourcemage.org>
import os
import stat
import sys
from random import random
import redis
import requests
import yaml
from bottle import route, default_app, redirect, abort, request
from flup.server.fcgi import WSGIServer
_DEFAULT_CONFIG = os.path.join(sys.path[0], "fallback.yml")
_CONFIG_FORMAT = {
'redis': ('password', 'expiration'),
'servers': ('name', 'url')
}
def check_mode(f):
st = os.stat(f)
if st.st_mode & (stat.S_IRGRP | stat.S_IROTH):
raise Exception("configuration file is group/world-readable")
def configure(f):
""" Check configuration file syntax. """
try:
fp = open(f)
except IOError as e:
raise Exception("unable to open configuration file '%s': %s" % (f, e))
try:
check_mode(fp.name)
config = yaml.safe_load(fp)
except yaml.YAMLError as e:
raise Exception("syntax mismatch in configuration file '%s': %s" % (fp.name, e))
except Exception:
raise
finally:
fp.close()
for section in _CONFIG_FORMAT:
if section not in config:
raise Exception("missing section: %s" % section)
if section in ('servers',):
if type(config[section]) != list:
raise Exception("section '%s' must be a list" % section)
for key in _CONFIG_FORMAT[section]:
for item in config[section]:
if key not in item:
raise Exception("missing key in section's '%s' item: %s" % (section, key))
if type(item[key]) != str:
raise Exception("value for '%s' must be a string" % key)
if section in ('redis',):
if type(config[section]) != dict:
raise Exception("redis item must be a dictionary")
for key in _CONFIG_FORMAT[section]:
if key not in config[section]:
raise Exception("missing key in section '%s': %s" % (section, key))
if key == "password" and type(config[section][key]) != str:
raise Exception("value for '%s' must be a string" % key)
elif key == "expiration" and type(config[section][key]) != int:
raise Exception("value for '%s' must be an integer" % key)
return config
try:
conf = configure(_DEFAULT_CONFIG)
except Exception as e:
print("configuration error: %s" % e)
sys.exit(1)
else:
red = redis.Redis(password=conf['redis']['password'])
servers = {s['name']: s['url'] for s in conf['servers']}
@route('/')
@route('/<args:path>')
def index(args=''):
src = args[args.rfind('/') + 1:]
try:
no_redis = False
res = red.get(src)
except Exception:
no_redis = True
res = None
if res:
redirect(servers[res] + src)
for mirror_id, mirror_url in sorted(servers.items(), key=lambda x: random()):
url = mirror_url + src
try:
req = requests.head(url, allow_redirects=True, timeout=5)
if req.status_code == 405:
req = requests.get(url, allow_redirects=True, timeout=5)
if req.ok:
if not no_redis:
red.set(src, mirror_id, ex=conf['redis']['expiration'])
break
except Exception:
req = None
continue
if req and req.ok:
redirect(url)
else:
abort(404, "File not found")
WSGIServer(default_app()).run()