Files
i2p.www/i2p2www/downloads.py

114 lines
3.1 KiB
Python
Raw Normal View History

2015-06-22 22:35:08 +00:00
from flask import abort, redirect, render_template, request
2012-12-19 12:52:05 +00:00
try:
import json
except ImportError:
import simplejson as json
from random import randint
from i2p2www import CURRENT_I2P_VERSION, MIRRORS_FILE
DEFAULT_MIRROR = {
2015-06-24 12:19:51 +00:00
'net': 'clearnet',
'protocol': 'https',
'domain': 'download.i2p2.de',
2014-03-24 21:27:56 +00:00
'org': 'sigterm.no',
2014-03-24 21:25:31 +00:00
'country': 'no',
}
2015-06-22 22:35:08 +00:00
DEFAULT_I2P_MIRROR = {
2015-06-24 12:19:51 +00:00
'net': 'i2p',
2015-06-22 22:35:08 +00:00
'protocol': 'http',
'domain': 'whnxvjwjhzsske5yevyokhskllvtisv5ueokw6yvh6t7zqrpra2q.b32.i2p',
'org': 'sigterm.no',
'country': 'i2p',
}
2012-12-19 12:52:05 +00:00
###################
# Download handlers
# Read in mirrors from file
def read_mirrors():
file = open(MIRRORS_FILE, 'r')
dat = file.read()
file.close()
lines=dat.split('\n')
ret={}
for line in lines:
try:
obj=json.loads(line)
except ValueError:
continue
if 'protocol' not in obj or 'domain' not in obj or 'path' not in obj:
2012-12-19 12:52:05 +00:00
continue
2015-06-24 12:19:51 +00:00
net=obj['net']
2012-12-19 12:52:05 +00:00
protocol=obj['protocol']
domain=obj['domain']
path=obj['path']
obj['url']='%s://%s%s' % (protocol, domain, path)
2015-06-24 12:19:51 +00:00
if net not in ret:
ret[net]={}
if protocol not in ret[net]:
ret[net][protocol]={}
ret[net][protocol][domain]=obj
2012-12-19 12:52:05 +00:00
return ret
# List of downloads
def downloads_list():
# TODO: read mirror list or list of available files
2015-06-22 22:35:08 +00:00
if request.headers.get('X-I2P-Desthash') and not request.headers.get('X-Forwarded-Server'):
def_mirror = DEFAULT_I2P_MIRROR
else:
def_mirror = DEFAULT_MIRROR
return render_template('downloads/list.html', def_mirror=def_mirror)
2012-12-19 12:52:05 +00:00
2013-07-18 04:40:31 +00:00
# Debian-specific page
def downloads_debian():
return render_template('downloads/debian.html')
2019-01-13 19:37:44 +00:00
# Firefox-specific page
def downloads_firefox():
return render_template('downloads/firefox.html')
2012-12-19 12:52:05 +00:00
# Specific file downloader
def downloads_select(version, file):
2012-12-19 12:52:05 +00:00
mirrors=read_mirrors()
obj=[]
2015-06-24 12:19:51 +00:00
for net in mirrors.keys():
2012-12-19 12:52:05 +00:00
a={}
2015-06-24 12:19:51 +00:00
a['key']=net
a['name']=net
a['protocols']=[]
for protocol in mirrors[net].keys():
b={}
b['key']=protocol
b['name']=protocol
b['domains']=mirrors[net][protocol]
a['protocols'].append(b)
2012-12-19 12:52:05 +00:00
obj.append(a)
return render_template('downloads/select.html', mirrors=obj, version=version, file=file)
2012-12-19 12:52:05 +00:00
2015-06-24 12:19:51 +00:00
def downloads_redirect(version, net, protocol, domain, file):
2012-12-19 12:52:05 +00:00
mirrors=read_mirrors()
2015-06-24 12:19:51 +00:00
if not net in mirrors:
2012-12-19 12:52:05 +00:00
abort(404)
2015-06-24 12:19:51 +00:00
mirrors=mirrors[net]
2012-12-19 12:52:05 +00:00
data = {
'version': version,
2012-12-19 12:52:05 +00:00
'file': file,
}
2015-06-24 12:19:51 +00:00
if not protocol:
2015-06-24 12:20:50 +00:00
protocol = mirrors.keys()[randint(0, len(mirrors) - 1)]
2015-06-24 12:19:51 +00:00
if not protocol in mirrors:
abort(404)
mirrors=mirrors[protocol]
if not domain:
domain = mirrors.keys()[randint(0, len(mirrors) - 1)]
if not domain in mirrors:
abort(404)
return render_template('downloads/redirect.html',
version=version, protocol=protocol, domain=domain, file=file,
2015-06-24 12:19:51 +00:00
url=mirrors[domain]['url'] % data)