mirror of
https://github.com/armbian/dl-router.git
synced 2026-01-06 10:32:39 -08:00
woohoo it works
This commit is contained in:
62
app/main.py
62
app/main.py
@@ -2,6 +2,7 @@
|
||||
|
||||
import uwsgi
|
||||
import json
|
||||
import logging
|
||||
|
||||
from flask import (
|
||||
Flask,
|
||||
@@ -22,23 +23,26 @@ def load_mirrors():
|
||||
""" open mirrors file and return contents """
|
||||
# with open('mirrors.conf', 'r') as row:
|
||||
# all_mirrors=row.read().splitlines()
|
||||
global mode
|
||||
yaml = YAML()
|
||||
yaml.indent(mapping=2, sequence=4, offset=2)
|
||||
yaml.preserve_quotes = True
|
||||
|
||||
with open('mirrors.yaml', 'r') as f:
|
||||
config = yaml.load(f)
|
||||
return config['apt']
|
||||
mode = config['mode']
|
||||
print("using mode: {}".format(mode))
|
||||
return config['mirrors']
|
||||
|
||||
|
||||
def reload_all():
|
||||
""" reload mirror and redirect map files """
|
||||
load_mirrors()
|
||||
global mirror
|
||||
global mode
|
||||
mirror = Mirror(load_mirrors())
|
||||
global dl_map
|
||||
dl_map = parser.reload()
|
||||
return dl_map
|
||||
if mode == "dl_map":
|
||||
global dl_map
|
||||
dl_map = parser.reload()
|
||||
return mirror
|
||||
|
||||
|
||||
def get_ip():
|
||||
@@ -48,22 +52,37 @@ def get_ip():
|
||||
return request.environ['HTTP_X_FORWARDED_FOR']
|
||||
|
||||
|
||||
def get_redirect(path, IP, region=None):
|
||||
def get_region(IP):
|
||||
""" this is where we geoip and return region code """
|
||||
return None
|
||||
|
||||
|
||||
def get_redirect(path, IP):
|
||||
""" get redirect based on path and IP(future) """
|
||||
global mode
|
||||
global dl_map
|
||||
region = get_region(IP)
|
||||
split_path = path.split('/')
|
||||
if len(split_path) == 2:
|
||||
if split_path[0] == "region":
|
||||
if split_path[1] in mirror.all_regions():
|
||||
region = split_path[1]
|
||||
del split_path[0:2]
|
||||
path = "/{}".format("/".join(split_path))
|
||||
print("path: {}".format(path))
|
||||
if mode == "dl_map" and len(split_path) == 2:
|
||||
key = "{}/{}".format(split_path[0], split_path[1])
|
||||
new_path = dl_map.get(key, path)
|
||||
return "{}{}".format(mirror.next(), new_path)
|
||||
return "{}{}".format(mirror.next(region), new_path)
|
||||
if path == '':
|
||||
return mirror.next()
|
||||
return mirror.next(region)
|
||||
print("path: {}".format(path))
|
||||
return "{}{}".format(mirror.next(), path)
|
||||
return "{}{}".format(mirror.next(region), path)
|
||||
|
||||
|
||||
mirror = Mirror(load_mirrors())
|
||||
parser = Parser('userdata.csv')
|
||||
dl_map = parser.parsed_data
|
||||
if mode == "dl_map":
|
||||
parser = Parser('userdata.csv')
|
||||
dl_map = parser.parsed_data
|
||||
|
||||
|
||||
app = Flask(__name__)
|
||||
@@ -79,7 +98,7 @@ def status():
|
||||
def signal_reload():
|
||||
""" trigger graceful reload via uWSGI """
|
||||
uwsgi.reload()
|
||||
return dl_map
|
||||
return "reloded"
|
||||
|
||||
|
||||
@ app.route('/mirrors')
|
||||
@@ -88,6 +107,21 @@ def show_mirrors():
|
||||
return json.dumps(mirror.all_mirrors())
|
||||
|
||||
|
||||
@ app.route('/regions')
|
||||
def show_regions():
|
||||
""" return all_regions in json format to requestor """
|
||||
return json.dumps(mirror.all_regions())
|
||||
|
||||
|
||||
@ app.route('/dl_map')
|
||||
def show_dl_map():
|
||||
global mode
|
||||
global dl_map
|
||||
if mode == "dl_map":
|
||||
return json.dumps(dl_map)
|
||||
return "no map. in direct mode"
|
||||
|
||||
|
||||
@ app.route('/', defaults={'path': ''})
|
||||
@ app.route('/<path:path>')
|
||||
def catch_all(path):
|
||||
|
||||
@@ -4,31 +4,36 @@
|
||||
class Mirror():
|
||||
|
||||
def __init__(self, mirror_list):
|
||||
self.mirror_list = dict()
|
||||
self.mirror_list = mirror_list
|
||||
self._list_position = dict()
|
||||
self._list_max = dict()
|
||||
self.mirror_list['default'] = list(
|
||||
mirror_list['NA'] + mirror_list['EU'])
|
||||
self.mirror_list = mirror_list
|
||||
for region in list(mirror_list.keys()):
|
||||
self._list_position[region] = 0
|
||||
self._list_max[region] = len(mirror_list[region]) - 1
|
||||
|
||||
def increment(self, region='default'):
|
||||
# set defaults to None in param list, then actually set inside
|
||||
# body to avoid scope change
|
||||
def increment(self, region=None):
|
||||
""" move to next element regions mirror list and return list position"""
|
||||
if region is None:
|
||||
region = 'default'
|
||||
if self._list_position[region] == self._list_max[region]:
|
||||
self._list_position[region] = 0
|
||||
else:
|
||||
self._list_position[region] = self._list_position[region] + 1
|
||||
|
||||
def next(self, region='default'):
|
||||
def next(self, region=None):
|
||||
""" return next mirror in rotation """
|
||||
if region is None:
|
||||
region = 'default'
|
||||
self.increment(region)
|
||||
return self.mirror_list[region][self._list_position[region]]
|
||||
|
||||
def all_mirrors(self):
|
||||
""" return all mirrrors configured """
|
||||
self.mirror_list
|
||||
return self.mirror_list
|
||||
|
||||
def all_regions(self):
|
||||
""" return list of regions configured """
|
||||
|
||||
@@ -1,18 +1,20 @@
|
||||
---
|
||||
apt:
|
||||
AS:
|
||||
- https://mirrors.tuna.tsinghua.edu.cn/armbian/
|
||||
NA:
|
||||
- https://armbian.tnahosting.net/dl/
|
||||
- https://us.mirrors.fossho.st/armbian/dl
|
||||
EU:
|
||||
# - https://minio.k-space.ee/armbian/dl/
|
||||
- https://uk.mirrors.fossho.st/armbian/dl/
|
||||
- https://armbian.systemonachip.net/dl/
|
||||
- http://mirrors.netix.net/armbian/dl/
|
||||
- https://mirrors.dotsrc.org/armbian-dl/
|
||||
#mode: redirect
|
||||
#mirrors:
|
||||
# AS:
|
||||
# - https://mirrors.tuna.tsinghua.edu.cn/armbian/
|
||||
# NA:
|
||||
# - https://armbian.tnahosting.net/apt/
|
||||
# - https://us.mirrors.fossho.st/armbian/armbian/
|
||||
# EU:
|
||||
# # - https://minio.k-space.ee/armbian/apt/
|
||||
# - https://uk.mirrors.fossho.st/armbian/apt/
|
||||
# - https://armbian.systemonachip.net/apt/
|
||||
# - http://mirrors.netix.net/armbian/apt/
|
||||
# - https://mirrors.dotsrc.org/armbian-apt/
|
||||
|
||||
dl:
|
||||
mode: dl_map
|
||||
mirrors:
|
||||
AS:
|
||||
- https://mirrors.tuna.tsinghua.edu.cn/armbian/
|
||||
NA:
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
rm -rf __pycache__
|
||||
pip install -r requirements.txt
|
||||
|
||||
15
run_local_dev.sh
Executable file
15
run_local_dev.sh
Executable file
@@ -0,0 +1,15 @@
|
||||
#export FLASK_APP=main.py
|
||||
#python -m flask run --host 0.0.0.0 --port 5000
|
||||
|
||||
|
||||
|
||||
APP_PATH=$(pwd)/app
|
||||
USERDATA_PATH=$(pwd)/userdata.csv
|
||||
MIRRORS_CONF_PATH=$(pwd)/mirrors.yaml
|
||||
LISTEN_PORT=5000
|
||||
CONTAINER_NAME=redirect
|
||||
DETACH=false
|
||||
|
||||
##FIXME CHANGE CONFIG MAP TO YAML WHEN DONE
|
||||
|
||||
sudo docker run --rm $([[ ${DETACH} == "true" ]] && echo "-d") -v ${APP_PATH}:/app -v ${USERDATA_PATH}:/app/userdata.csv -v ${MIRRORS_CONF_PATH}:/app/mirrors.yaml -p ${LISTEN_PORT}:80 --name ${CONTAINER_NAME} quay.io/lanefu/nginx-uwsgi-flask:arm64
|
||||
574
testurls.txt
Normal file
574
testurls.txt
Normal file
File diff suppressed because it is too large
Load Diff
3915
userdata.csv
3915
userdata.csv
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user