Fix: Handle untrusted global proxy environment on deploy

This commit is contained in:
LmeSzinc 2022-12-28 01:41:38 +08:00
parent ebba35b7c9
commit 352a219571
2 changed files with 41 additions and 0 deletions

View File

@ -1,3 +1,7 @@
from deploy.patch import patch_trust_env
patch_trust_env()
from deploy.adb import AdbManager
from deploy.alas import AlasManager
from deploy.app import AppManager

37
deploy/patch.py Normal file
View File

@ -0,0 +1,37 @@
import os
import re
def _patch_trust_env(file):
"""
People use proxies, but they never realize that proxy software leaves a
global proxy pointing to itself even when the software is not running.
In most situations we set `session.trust_env = False` in requests, but this
does not effect the `pip` command.
To handle untrusted user environment for good. We patch the code file in
requests directly. Of course, the patch only effect the python env inside
Alas.
Returns:
bool: If patched.
"""
if os.path.exists(file):
with open(file, 'r', encoding='utf-8') as f:
content = f.read()
if re.search('self.trust_env = True', content):
content = re.sub('self.trust_env = True', 'self.trust_env = False', content)
with open(file, 'w', encoding='utf-8') as f:
f.write(content)
print(f'{file} trust_env patched')
elif re.search('self.trust_env = False', content):
print(f'{file} trust_env already patched')
else:
print(f'{file} trust_env not found')
else:
print(f'{file} trust_env no need to patch')
def patch_trust_env():
_patch_trust_env('./toolkit/Lib/site-packages/requests/sessions.py')
_patch_trust_env('./toolkit/Lib/site-packages/pip/_vendor/requests/sessions.py')