From 06b7ebeff8444b7127d25e3ac11ee4aa37e0a38f Mon Sep 17 00:00:00 2001 From: derekhe Date: Wed, 24 Nov 2021 22:04:48 +0800 Subject: [PATCH] Photogrammetry use proxy server to speed up loading for who use proxies --- config.ini | 2 +- src/app.py | 3 ++- src/nginx.conf.template | 10 +++++++++- src/server.py | 28 +++++++++++++++++++++++++--- 4 files changed, 37 insertions(+), 6 deletions(-) diff --git a/config.ini b/config.ini index c4646f18..1018e8d2 100644 --- a/config.ini +++ b/config.ini @@ -12,5 +12,5 @@ threads = 200 server = mt1.google.com [general] -warning = enabled +warning = disabled diff --git a/src/app.py b/src/app.py index dd4166eb..c581343c 100644 --- a/src/app.py +++ b/src/app.py @@ -288,7 +288,8 @@ def run(self): try: self.server_process = Process( target=run_server, - args=(self.settings.cache_size, self.settings.proxy_url, self.settings.google_server)) + args=( + self.settings.cache_size, self.settings.proxy_url, self.settings.google_server, get_hosts_origin_ips())) self.server_process.start() self.nginx_process = subprocess.Popen( "nginx.exe", shell=True, cwd="./nginx") diff --git a/src/nginx.conf.template b/src/nginx.conf.template index 0d3a3d78..6dda5f1f 100644 --- a/src/nginx.conf.template +++ b/src/nginx.conf.template @@ -1,4 +1,4 @@ -worker_processes 1; +worker_processes 8; error_log logs/error.log; error_log logs/error.log notice; @@ -75,6 +75,14 @@ http { #bing_aerial# proxy_pass_request_headers on; #bing_aerial# proxy_ssl_verify off; #bing_aerial# access_log logs/localgoogle-access.log; +#bing_aerial# } + +#bing_aerial# location ~* /tiles/mtx.*? { +#bing_aerial# proxy_pass http://localhost:39871; +#bing_aerial# proxy_pass_request_headers on; +#bing_aerial# proxy_set_header Host kh.ssl.ak.tiles.virtualearth.net; +#bing_aerial# proxy_ssl_verify off; +#bing_aerial# access_log logs/localgoogle-access.log; #bing_aerial# } location / { diff --git a/src/server.py b/src/server.py index 5131fa3a..a80bb13c 100644 --- a/src/server.py +++ b/src/server.py @@ -8,13 +8,14 @@ import urllib3 from PIL import Image, ImageEnhance, ImageStat from diskcache import Cache -from flask import Flask, make_response, Response +from flask import Flask, make_response, Response, request urllib3.disable_warnings() __cache: Cache = None __proxies = None __google_server = "mt1.google.com" +__original_ips = None app = Flask(__name__) @@ -50,6 +51,26 @@ def calc_brightness(im): return stat.rms[0] +@app.route('/tiles/mtx') +def mtx(dummy=None): + print("Handing request to", request.url) + request_header = {} + for k, v in request.headers: + request_header[k] = v + + print("Downloading from:", request.url) + + url = request.url.replace(request.host, "kh.ssl.ak.tiles.virtualearth.net.edgekey.net").replace("http://","https://") + + remote_response = requests.get( + url, proxies=__proxies, timeout=30, verify=False, headers=request_header) + + response = make_response(remote_response.content) + for k, v in remote_response.headers.items(): + response.headers[k] = v + return response + + @app.route("/tiles/akh") def tiles(path): quadkey = re.findall(r"(\d+).jpeg", path)[0] @@ -98,11 +119,12 @@ def tiles(path): return response -def run_server(cache_size, proxies, google_server): - global __cache, __proxies, __google_server +def run_server(cache_size, proxies, google_server, original_ips): + global __cache, __proxies, __google_server, __original_ips __cache = Cache( "./cache", size_limit=int(cache_size) * 1024 * 1024 * 1024, shards=10) __proxies = {"https": proxies} __google_server = google_server + __original_ips = original_ips app.run(port=39871, host="0.0.0.0", threaded=True)