Commit e13f65da authored by shadMod's avatar shadMod 💬

replaced old 'self.config[releases]' with new 'self.config[distros][distro][archs][arch]'

to get determinate dict with all the required keys due to the latest changes in downloads.toml

replaced md5 with sha256 and http with https

Fixes: #16
parent 73dfc935
# Source code of the Ubuntu-it website # Source code of the Ubuntu-it website
# Copyright (C) 2016 Pietro Albini <pietroalbini@ubuntu.com> # Copyright (C) 2016 Pietro Albini <pietroalbini@ubuntu.com>
# Copyright (C) 2023 shadMod
# #
# This program is free software: you can redistribute it and/or modify # This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published # it under the terms of the GNU Affero General Public License as published
...@@ -75,6 +76,8 @@ class Downloads: ...@@ -75,6 +76,8 @@ class Downloads:
found_mirrors = list(sorted(launchpad.get_cdimage_mirrors( found_mirrors = list(sorted(launchpad.get_cdimage_mirrors(
distro, self.config["mirrors"]["country"] distro, self.config["mirrors"]["country"]
))) )))
if "it1.mirror.vhosting-it.com" in found_mirrors[0]:
found_mirrors = ["https://releases.ubuntu.com/"]
if found_mirrors: if found_mirrors:
self._mirrors[distro] = found_mirrors self._mirrors[distro] = found_mirrors
if not self._mirrors: if not self._mirrors:
...@@ -85,14 +88,14 @@ class Downloads: ...@@ -85,14 +88,14 @@ class Downloads:
return self._mirrors return self._mirrors
@property @property
def md5sums(self): def sha256sums(self):
"""Get a list of all the MD5SUMS""" """Get a list of all the SHA256SUMS"""
if not hasattr(self, "_md5sums"): if getattr(self, "_sha256sums") is None:
if self._cache is not None: if self._cache is not None:
self._md5sums = self._cache["md5sums"] self._sha256sums = self._cache["sha256sums"]
else: else:
self._md5sums = self._fetch_md5sums() self._sha256sums = self._fetch_sha256sums()
return self._md5sums return self._sha256sums
def _strip_non_lts_releases(self): def _strip_non_lts_releases(self):
"""Process the lts-only distro configuration""" """Process the lts-only distro configuration"""
...@@ -104,14 +107,14 @@ class Downloads: ...@@ -104,14 +107,14 @@ class Downloads:
if not self.config["releases"][release]["lts"]: if not self.config["releases"][release]["lts"]:
config["releases"].remove(release) config["releases"].remove(release)
def _fetch_md5sums(self): def _fetch_sha256sums(self):
"""Fetch all the needed MD5SUMS""" """Fetch all the needed SHA256SUMS"""
result = {} result = {}
files_content = {} files_content = {}
for distro, config in self.config["distros"].items(): for distro, config in self.config["distros"].items():
for release in config["releases"]: for release in config["releases"]:
if config["lts-only"] and not self.config["releases"][release]["lts"]: if config["lts-only"]:
continue continue
for arch in config["archs"].keys(): for arch in config["archs"].keys():
...@@ -120,7 +123,7 @@ class Downloads: ...@@ -120,7 +123,7 @@ class Downloads:
# Fetch the file from the remote if it wasn't done already # Fetch the file from the remote if it wasn't done already
if path not in files_content: if path not in files_content:
md5s = {} sha_hash = {}
response = requests.get("%s/SHA256SUMS" % path) response = requests.get("%s/SHA256SUMS" % path)
if response.status_code == 404: if response.status_code == 404:
...@@ -132,19 +135,19 @@ class Downloads: ...@@ -132,19 +135,19 @@ class Downloads:
if line.strip() == "": if line.strip() == "":
continue continue
hash, name = line.split(" ", 1) _hash, name = line.split(" ", 1)
if name.startswith("*"): if name.startswith("*"):
name = name[1:] name = name[1:]
md5s[name] = hash sha_hash[name] = _hash
files_content[path] = md5s files_content[path] = sha_hash
# Add the MD5 to the result if it was found # Add the SHA256 to the result if it was found
if file in files_content[path]: if file in files_content[path]:
key = "%s:%s:%s" % (distro, release, arch) key = "%s:%s:%s" % (distro, release, arch)
result[key] = files_content[path][file] result[key] = files_content[path][file]
else: else:
print(files_content[path]) print(files_content[path])
raise RuntimeError("Missing MD5 for {}".format(file)) raise RuntimeError("Missing SHA256SUMS for {}".format(file))
return result return result
...@@ -178,7 +181,7 @@ class Downloads: ...@@ -178,7 +181,7 @@ class Downloads:
"version": CACHE_FILE_VERSION, "version": CACHE_FILE_VERSION,
"content": { "content": {
"mirrors": self.mirrors, "mirrors": self.mirrors,
"md5sums": self.md5sums, "sha256sums": self.sha256sums,
}, },
}, f, separators=(",", ":")) }, f, separators=(",", ":"))
...@@ -188,12 +191,13 @@ class Downloads: ...@@ -188,12 +191,13 @@ class Downloads:
def url_for(self, distro, release, arch, torrent=False, use_random=True): def url_for(self, distro, release, arch, torrent=False, use_random=True):
"""Get the URL of a download file""" """Get the URL of a download file"""
# Build the dict of keys to replace # Build the dict of keys to replace
data_rel = self.config["distros"][distro]["archs"][arch][release]
replaces = { replaces = {
"distro": distro, "distro": distro,
"release": release, "release": release,
"arch": arch, "arch": arch,
"version": self.config["releases"][release]["version"], "version": data_rel["version"],
"codename": self.config["releases"][release]["codename"], "codename": data_rel["codename"],
} }
# Add mirrors to the replaces key # Add mirrors to the replaces key
...@@ -204,14 +208,14 @@ class Downloads: ...@@ -204,14 +208,14 @@ class Downloads:
replaces["mirror:%s" % name] = choices[0] replaces["mirror:%s" % name] = choices[0]
# Get the right source # Get the right source
source_name = self.config["distros"][distro]["archs"][arch] host = data_rel["host"]
source = self.config["sources"][source_name] source = self.config["sources"][host]
# Download URLs are different for torrent and HTTP # Download URLs are different for torrent and HTTP
if torrent: if torrent:
url = source["torrent"] url = source["torrent"]
else: else:
url = source["http"] url = source["https"]
for key, value in replaces.items(): for key, value in replaces.items():
url = url.replace("{%s}" % key, value) url = url.replace("{%s}" % key, value)
...@@ -238,11 +242,12 @@ class Downloads: ...@@ -238,11 +242,12 @@ class Downloads:
if distro not in self.config["distros"]: if distro not in self.config["distros"]:
flask.abort(404) flask.abort(404)
data = self.config["distros"][distro]
return flask.render_template( return flask.render_template(
"download/landing.html", "download/landing.html",
distro_name=distro, distro_name=distro,
distro=self.config["distros"][distro], distro=data,
releases=self.config["releases"], releases=data["archs"]["amd64"],
archs=self.config["archs"], archs=self.config["archs"],
) )
...@@ -266,16 +271,19 @@ class Downloads: ...@@ -266,16 +271,19 @@ class Downloads:
torrent = "torrent" in flask.request.form torrent = "torrent" in flask.request.form
payload = signer.dumps({ data_rel = self.config["distros"][distro]["archs"][arch][release]
payload = signer.dumps(
{
"url": self.url_for(distro, release, arch, torrent), "url": self.url_for(distro, release, arch, torrent),
"md5": self.md5sums["%s:%s:%s" % (distro, release, arch)], "sha256": self.sha256sums[f"{distro}:{release}:{arch}"],
"name": "%s %s%s" % ( "name": "%s %s%s" % (
self.config["distros"][distro]["name"], self.config["distros"][distro]["name"],
self.config["releases"][release]["version"], data_rel["version"],
" LTS" if self.config["releases"][release]["lts"] else "", " LTS" if data_rel["lts"] else "",
), ),
"theme": distro, "theme": distro,
}) }
)
return flask.redirect(flask.url_for(".thanks", payload=payload)) return flask.redirect(flask.url_for(".thanks", payload=payload))
@bp.route("/grazie/<payload>") @bp.route("/grazie/<payload>")
...@@ -293,10 +301,11 @@ class Downloads: ...@@ -293,10 +301,11 @@ class Downloads:
"""Generate the sub-navbar for /download""" """Generate the sub-navbar for /download"""
result = [] result = []
for name, distro in self.config["distros"].items(): for name, distro in self.config["distros"].items():
result.append({ result.append(
{
"name": distro["name"], "name": distro["name"],
"endpoint": "download.landing", "endpoint": "download.landing",
"endpoint-args": {"distro": name}, "endpoint-args": {"distro": name},
}) }
)
return result return result
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment