2023-09-19 08:29:55 +00:00
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
import os
|
|
|
|
|
import logging
|
|
|
|
|
import socket
|
|
|
|
|
import socks
|
2023-09-26 01:19:33 -07:00
|
|
|
import logging
|
2023-09-19 08:29:55 +00:00
|
|
|
|
|
|
|
|
directory = os.path.dirname(__file__)
|
|
|
|
|
sys.path.append(directory + '/../../')
|
|
|
|
|
|
|
|
|
|
from aios_kernel import AIStorage
|
|
|
|
|
|
2023-09-26 01:19:33 -07:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
2023-09-19 08:29:55 +00:00
|
|
|
def apply_storage():
|
2023-09-20 08:45:53 +00:00
|
|
|
proxy_cfg = AIStorage.get_instance().get_user_config().get_config_item("proxy")
|
2023-09-19 08:29:55 +00:00
|
|
|
if proxy_cfg is None:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
host_url = proxy_cfg.value
|
2023-09-20 07:46:42 +00:00
|
|
|
if host_url is not None and len(host_url) > 3:
|
|
|
|
|
url_fields = host_url.split("@")
|
|
|
|
|
proxy_type, host, username, password = url_fields[0], None, None, None
|
|
|
|
|
if len(url_fields) > 1:
|
|
|
|
|
host = url_fields[1]
|
|
|
|
|
if len(url_fields) > 2:
|
|
|
|
|
username = url_fields[2]
|
|
|
|
|
if len(url_fields) > 3:
|
|
|
|
|
password = url_fields[3]
|
|
|
|
|
|
2023-09-19 08:29:55 +00:00
|
|
|
match proxy_type:
|
|
|
|
|
case "socks5":
|
|
|
|
|
(host, port) = host.split(":")
|
2023-09-20 07:46:42 +00:00
|
|
|
socks.set_default_proxy(socks.SOCKS5, host, int(port), username = username, password = password)
|
2023-09-19 08:29:55 +00:00
|
|
|
socket.socket = socks.socksocket
|
2023-09-26 01:19:33 -07:00
|
|
|
logger.info(f"proxy {host_url} will be used.")
|
2023-10-24 09:27:06 +00:00
|
|
|
case "http":
|
|
|
|
|
(host, port) = host.split(":")
|
|
|
|
|
socks.set_default_proxy(socks.HTTP, host, int(port), username = username, password = password)
|
|
|
|
|
socket.socket = socks.socksocket
|
|
|
|
|
logger.info(f"proxy {host_url} will be used.")
|
2023-09-19 08:29:55 +00:00
|
|
|
case _:
|
2023-09-26 01:19:33 -07:00
|
|
|
logger.error(f"the proxy type ({proxy_type}) has not support. proxy will not be used.")
|
|
|
|
|
|
2023-09-19 08:29:55 +00:00
|
|
|
|
|
|
|
|
def declare_user_config():
|
|
|
|
|
user_config = AIStorage.get_instance().get_user_config()
|
2023-10-24 09:27:06 +00:00
|
|
|
user_config.add_user_config("proxy", "set your proxy service as 'proxy_type@host:port@username@password', 'proxy_type' = 'socks5|http'", True, None)
|