Skip to content
This repository has been archived by the owner on Jan 4, 2022. It is now read-only.

Commit

Permalink
Remove dns resolve, use DNS alias instead.
Browse files Browse the repository at this point in the history
  • Loading branch information
derekhe committed Nov 28, 2021
1 parent f4ede4a commit a3e536a
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 85 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ build/
.vscode/
nginx/logs/
nginx/temp/
nginx/conf/nginx.conf
.idea/
Python39/*
2 changes: 1 addition & 1 deletion config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ threads = 200
server = khms1.google.com

[general]
warning = disabled
warning = enabled

95 changes: 95 additions & 0 deletions nginx/conf/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
worker_processes 8;

error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;

events {
worker_connections 1024;
}

http {
server_names_hash_bucket_size 64;

include mime.types;
default_type application/octet-stream;

keepalive_timeout 65;

server {
listen 443 ssl;
server_name khstorelive.azureedge.net;

ssl_certificate cert.pem;
ssl_certificate_key key.pem;

ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;

access_log logs/khstore-access.log;

location ~* /.*?tsom_cc_activation_masks.* {
return 404;
}

location ~* /.*?/coverage_maps/.* {
return 404;
}

location ~* /.*?texture_synthesis_online_map_high_res.* {
return 404;
}

location ~* /.*?color_corrected_images.* {
return 404;
}

location / {
proxy_set_header Host khstorelive.azureedge.net;
proxy_pass https://khstorelive.afd.azureedge.net;
proxy_pass_request_headers on;
proxy_ssl_verify off;
}
}

server {
listen 443 ssl;
server_name kh.ssl.ak.tiles.virtualearth.net;

ssl_certificate cert.pem;
ssl_certificate_key key.pem;

ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;

access_log logs/virtualearth-access.log;

location /health {
proxy_pass http://localhost:39871/health;
proxy_ssl_verify off;
access_log logs/health-check.log;
}

location ~* /tiles/akh.*? {
proxy_pass http://localhost:39871;
proxy_pass_request_headers on;
proxy_ssl_verify off;
access_log logs/localgoogle-access.log;
}

location ~* /tiles/mtx.*? {
proxy_pass http://localhost:39871;
proxy_pass_request_headers on;
proxy_set_header Host kh.ssl.ak.tiles.virtualearth.net;
proxy_ssl_verify off;
access_log logs/localgoogle-access.log;
}

location / {
proxy_set_header Host kh.ssl.ak.tiles.virtualearth.net;
proxy_pass https://kh.ssl.ak.tiles.virtualearth.net.edgekey.net;
proxy_pass_request_headers on;
proxy_ssl_verify off;
}
}
}
41 changes: 2 additions & 39 deletions run.bat
Original file line number Diff line number Diff line change
@@ -1,43 +1,6 @@
@setlocal enableextensions
@cd /d "%~dp0"
@echo off

:: BatchGotAdmin
:-------------------------------------
REM --> Check for permissions
IF "%PROCESSOR_ARCHITECTURE%" EQU "amd64" (
>nul 2>&1 "%SYSTEMROOT%\SysWOW64\cacls.exe" "%SYSTEMROOT%\SysWOW64\config\system"
) ELSE (
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
)
.\Python39\python.exe src\app.py

REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
set params= %*
echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params:"=""%", "", "runas", 1 >> "%temp%\getadmin.vbs"

"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B

:gotAdmin
pushd "%CD%"
CD /D "%~dp0"
:--------------------------------------

if exist .\Python39\python.exe (
echo "Use embedded python"
.\Python39\python.exe src\app.py
) else (
echo "Use user python"
py -m pip install -r requirements.txt
py src\app.py
)

pause
pause
24 changes: 2 additions & 22 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import webbrowser
from diskcache import Cache
from multiprocessing import Process
from runner import add_cert, override_hosts, restore_hosts, get_hosts_origin_ips, del_cert
from runner import add_cert, override_hosts, restore_hosts, del_cert
from server import run_server, clear_cache, url_mapping
from settings import Settings
from threading import Thread
Expand Down Expand Up @@ -233,12 +233,6 @@ def enable_features(template: str):
out = out.replace(f"#{feature}#", "")
return out

@staticmethod
def config_dns(template: str):
for k, v in get_hosts_origin_ips().items():
template = template.replace(f"#{k}#", v)
return template

def run(self):
if self.is_warning_enabled():
messagebox.showwarning(title="IMPORTANT",
Expand All @@ -259,19 +253,6 @@ def run(self):
messagebox.showerror(message=f"Add certificate failed: {traceback.format_exc()}")
return

try:
with open("./src/nginx.conf.template", "rt") as nginx:
template = nginx.read()
output = self.enable_features(template)
output = self.config_dns(output)

with open("./nginx/conf/nginx.conf", "wt") as out:
out.write(output)
except Exception:
traceback.print_exc()
messagebox.showerror(message=f"Generate nginx file failed:\n {traceback.format_exc()}")
return

try:
override_hosts()
except:
Expand All @@ -289,8 +270,7 @@ def run(self):
self.server_process = Process(
target=run_server,
args=(
self.settings.cache_size, self.settings.proxy_url, self.settings.google_server,
get_hosts_origin_ips()))
self.settings.cache_size, self.settings.proxy_url, self.settings.google_server))
self.server_process.start()
self.nginx_process = subprocess.Popen(
"nginx.exe", shell=True, cwd="./nginx")
Expand Down
20 changes: 2 additions & 18 deletions src/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
urllib3.disable_warnings()

__domains = ['kh.ssl.ak.tiles.virtualearth.net', 'khstorelive.azureedge.net']
__default_ip = {
'kh.ssl.ak.tiles.virtualearth.net': '104.85.242.213',
'khstorelive.azureedge.net': '104.212.68.114'
}
host_path = "C:\\Windows\\System32\\drivers\\etc\\hosts"
host_entries = [f"\n127.0.0.1 {domain}\n" for domain in __domains]

Expand All @@ -21,24 +17,12 @@ def add_cert():
subprocess.run(["certutil", "-addstore", "-f", "root",
".\\certs\\cert.crt"], shell=True, check=True)


def del_cert():
subprocess.run(["certutil", "-delstore", "-f", "root",
".\\certs\\cert.crt"], shell=True, check=True)


def get_hosts_origin_ips():
try:
origin_ips = {}
dns_resolver = dns.resolver.Resolver()
for d in __domains:
origin_ips[d] = dns_resolver.resolve(d)[0].to_text()
print(origin_ips)
return origin_ips
except:
traceback.print_exc()
return __default_ip


def override_hosts():
print("Overriding hosts")
os.chmod(host_path, stat.S_IWRITE)
Expand All @@ -57,4 +41,4 @@ def restore_hosts():
f.seek(0)
f.write(host)
f.truncate()
print("Restored hosts")
print("Restored hosts")
6 changes: 2 additions & 4 deletions src/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
__cache: Cache = None
__proxies = None
__google_server = "mt1.google.com"
__original_ips = None
app = Flask(__name__)


Expand Down Expand Up @@ -125,12 +124,11 @@ def tiles(path):
return response


def run_server(cache_size, proxies, google_server, original_ips):
global __cache, __proxies, __google_server, __original_ips
def run_server(cache_size, proxies, google_server):
global __cache, __proxies, __google_server
__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)

0 comments on commit a3e536a

Please sign in to comment.