-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
179 lines (147 loc) · 5.66 KB
/
app.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
#!/usr/bin/python3
# SPDX-FileCopyrightText: 2023 FC Stegerman <flx@obfusk.net>
# SPDX-License-Identifier: AGPL-3.0-or-later
import os
import re
from typing import Any, Union
import requests
from flask import Flask, abort, make_response, redirect, request
from flask_limiter import Limiter
HOMEPAGE = "https://github.com/obfusk/fdroid-release-redirector"
CODEBERG = "codeberg.org"
GITLAB = "gitlab.com"
NOTABUG = "notabug.org"
# host, namespace, project, release_tag
GITEA_RELEASE = "https://{}/api/v1/repos/{}/{}/releases/tags/{}"
# host, namespace, project, release
GITLAB_RELEASE = "https://{}/api/v4/projects/{}%2F{}/releases/{}"
# host, namespace, project, upload
GITLAB_UPLOAD = "https://{}/{}/{}/uploads/{}"
# hash/filename
GITLAB_UPLOAD_RX = re.compile(r"\(/uploads/([0-9a-f]{32}/[^/)]+)\)")
# NB: HTML, not JSON
# host, namespace, project
NOTABUG_RELEASES = "https://{}/{}/{}/releases"
# namespace, project, release
NOTABUG_RELEASE_ZIP = 'href="/{}/{}/archive/{}.zip"'
# host, attachment
NOTABUG_ATTACHMENT = "https://{}/attachments/{}"
# uuid, filename
NOTABUG_ATTACHMENT_RX = re.compile(r'href="/attachments/([0-9a-f-]+)"[^>]*>([^<]+)<')
NOTABUG_DOWNLOADS_RX = re.compile(r'<div class="download">(.*?)</div>', re.S)
ENV_PREFIX = "FDROID_RELEASE_REDIRECTOR"
RATELIMIT = os.environ.get(f"{ENV_PREFIX}_RATELIMIT") in ("1", "yes", "true")
FORWARDED = os.environ.get(f"{ENV_PREFIX}_FORWARDED") in ("1", "yes", "true")
if FORWARDED:
def get_remote_address() -> str:
return request.access_route[-1]
else:
from flask_limiter.util import get_remote_address
app = Flask(__name__)
# NB: in-memory storage may require WEB_CONCURRENCY=1
limiter = Limiter(
get_remote_address,
app=app,
default_limits=["60/hour"],
enabled=RATELIMIT,
storage_uri="memory://",
strategy="fixed-window",
)
# FIXME: too strict?
def validate(*args: str) -> bool:
for arg in args:
if not re.fullmatch(r"[a-zA-Z0-9._-]+", arg):
return False
return True
def gitea_release(host: str, namespace: str, project: str, release_tag: str,
asset: str) -> Union[int, str]:
if not validate(namespace, project, release_tag, asset):
return 400
url = GITEA_RELEASE.format(host, namespace, project, release_tag)
try:
r = requests.get(url, timeout=3)
if r.status_code == 404:
return 404
r.raise_for_status()
data = r.json()
for asset_data in data["assets"]:
if asset_data["name"] == asset:
asset_url: str = asset_data["browser_download_url"]
return asset_url
return 404
except (IndexError, KeyError, requests.RequestException):
return 400
def gitlab_release(host: str, namespace: str, project: str, release: str,
asset: str) -> Union[int, str]:
if not validate(namespace, project, release, asset):
return 400
url = GITLAB_RELEASE.format(host, namespace, project, release)
try:
r = requests.get(url, timeout=3)
if r.status_code == 404:
return 404
r.raise_for_status()
data = r.json()
asset_url: str
for asset_url in (a["url"] for a in data["assets"]["links"]):
if asset_url.endswith("/" + asset):
return asset_url
for upload in GITLAB_UPLOAD_RX.findall(data["description"]):
if upload.endswith("/" + asset):
asset_url = GITLAB_UPLOAD.format(host, namespace, project, upload)
return asset_url
return 404
except (IndexError, KeyError, requests.RequestException):
return 400
# FIXME: scraping HTML; no API, no pagination
def notabug_release(host: str, namespace: str, project: str, release: str,
asset: str) -> Union[int, str]:
if not validate(namespace, project, release, asset):
return 400
url = NOTABUG_RELEASES.format(host, namespace, project)
release_zip = NOTABUG_RELEASE_ZIP.format(namespace, project, release)
try:
r = requests.get(url, timeout=3)
if r.status_code == 404:
return 404
r.raise_for_status()
for dl_m in NOTABUG_DOWNLOADS_RX.finditer(r.text):
if release_zip in dl_m[1]:
for url_m in NOTABUG_ATTACHMENT_RX.finditer(dl_m[1]):
if url_m[2] == asset:
return NOTABUG_ATTACHMENT.format(host, url_m[1])
return 404
except (IndexError, KeyError, requests.RequestException):
return 400
@app.route("/codeberg/<namespace>/<project>/<release>/<asset>")
def r_codeberg(namespace: str, project: str, release: str, asset: str) -> Any:
result = gitea_release(CODEBERG, namespace, project, release, asset)
if isinstance(result, int):
abort(result)
else:
return redirect(result)
@app.route("/gitlab/<namespace>/<project>/<release>/<asset>")
@limiter.limit("120/hour")
def r_gitlab(namespace: str, project: str, release: str, asset: str) -> Any:
result = gitlab_release(GITLAB, namespace, project, release, asset)
if isinstance(result, int):
abort(result)
else:
return redirect(result)
@app.route("/notabug/<namespace>/<project>/<release>/<asset>")
def r_notabug(namespace: str, project: str, release: str, asset: str) -> Any:
result = notabug_release(NOTABUG, namespace, project, release, asset)
if isinstance(result, int):
abort(result)
else:
return redirect(result)
@app.route("/")
@limiter.exempt
def r_root() -> Any:
return redirect(HOMEPAGE)
@app.route("/robots.txt")
@limiter.exempt
def r_robots() -> Any:
resp = make_response("User-agent: *\nDisallow: /\n")
resp.mimetype = "text/plain"
return resp