diff --git a/data/ssrspeed.example.json b/data/ssrspeed.example.json index a312302..e60db19 100644 --- a/data/ssrspeed.example.json +++ b/data/ssrspeed.example.json @@ -41,7 +41,7 @@ "addition": "OreosLab ©", "uploadResult": false, "hide_max_speed": false, - "font": "SourceHanSansCN-Medium.otf", + "font": "LXGWBright-Medium.otf", "colors": [ { "name": "origin", diff --git a/ssrspeed/__main__.py b/ssrspeed/__main__.py index b8ba0ef..63b212b 100644 --- a/ssrspeed/__main__.py +++ b/ssrspeed/__main__.py @@ -9,7 +9,7 @@ from ssrspeed.core import SSRSpeedCore from ssrspeed.paths import KEY_PATH, ROOT_PATH from ssrspeed.shell import cli as cli_cfg -from ssrspeed.utils import RequirementsCheck, check_platform +from ssrspeed.utils import PLATFORM, RequirementsCheck LOGS_DIR = KEY_PATH["logs"] RESULTS_DIR = KEY_PATH["results"] @@ -48,8 +48,7 @@ VERSION = ssrconfig["VERSION"] if __name__ == "__main__": - pf = check_platform() - if pf == "Unknown": + if PLATFORM == "Unknown": logger.critical("Your system is not supported. Please contact the developer.") sys.exit(1) diff --git a/ssrspeed/config/config.py b/ssrspeed/config/config.py index a0c8808..7eb578f 100644 --- a/ssrspeed/config/config.py +++ b/ssrspeed/config/config.py @@ -5,7 +5,7 @@ from ssrspeed.paths import KEY_PATH -__version__ = "1.3.5" +__version__ = "1.3.6" __web_api_version__ = "0.5.2" CONFIG_FILE = KEY_PATH["ssrspeed.json"] diff --git a/ssrspeed/launchers/base_client.py b/ssrspeed/launchers/base_client.py index 88c7967..d7895ac 100644 --- a/ssrspeed/launchers/base_client.py +++ b/ssrspeed/launchers/base_client.py @@ -1,28 +1,28 @@ import signal +from abc import ABCMeta, abstractmethod from typing import Any, Dict, List from loguru import logger from ssrspeed.config import ssrconfig -from ssrspeed.utils import check_platform +from ssrspeed.utils import PLATFORM -class BaseClient(object): +class BaseClient(metaclass=ABCMeta): + + _platform = PLATFORM + def __init__(self): self._localAddress: str = ssrconfig.get("localAddress", "127.0.0.1") self._localPort: int = ssrconfig.get("localPort", 1087) self._config_list: List[Dict[str, Any]] = [] self._config: Dict[str, Any] = {} - self._platform: str = self._check_platform() self._process = None - @staticmethod - def _check_platform() -> str: - return check_platform() - def _before_stop_client(self): pass + @abstractmethod def start_client(self, config: Dict[str, Any]): pass @@ -36,7 +36,7 @@ def test_process_terminate(self): def stop_client(self): self._before_stop_client() if self._process is not None: - if self._check_platform() == "Windows": + if BaseClient._platform == "Windows": self._process.terminate() # self._process.send_signal(signal.SIGINT) else: diff --git a/ssrspeed/launchers/ss_client.py b/ssrspeed/launchers/ss_client.py index 1a58891..d6dbc64 100644 --- a/ssrspeed/launchers/ss_client.py +++ b/ssrspeed/launchers/ss_client.py @@ -30,7 +30,7 @@ async def start_client(self, config: Dict[str, Any], debug=False): if self._process is None: - if self._platform == "Windows": + if Shadowsocks._platform == "Windows": if debug: self._process = subprocess.Popen( [ @@ -56,7 +56,7 @@ async def start_client(self, config: Dict[str, Any], debug=False): f'Starting shadowsocks-libev with server {config["server"]}:{config["server_port"]}' ) - elif self._platform == "Linux" or self._platform == "MacOS": + elif Shadowsocks._platform == "Linux" or Shadowsocks._platform == "MacOS": if debug: self._process = subprocess.Popen( [ diff --git a/ssrspeed/launchers/ssr_client.py b/ssrspeed/launchers/ssr_client.py index 55d4170..b8730a5 100644 --- a/ssrspeed/launchers/ssr_client.py +++ b/ssrspeed/launchers/ssr_client.py @@ -54,7 +54,7 @@ async def start_client(self, config: Dict[str, Any], debug=False): if self._process is None: - if self._platform == "Windows": + if ShadowsocksR._platform == "Windows": if self.useSsrCSharp: self.__ssr_csharp_conf(config) self._process = subprocess.Popen( @@ -90,7 +90,7 @@ async def start_client(self, config: Dict[str, Any], debug=False): f'Starting shadowsocksr-libev with server {config["server"]}:{config["server_port"]}' ) - elif self._platform == "Linux" or self._platform == "MacOS": + elif ShadowsocksR._platform == "Linux" or ShadowsocksR._platform == "MacOS": if debug: self._process = subprocess.Popen( [ diff --git a/ssrspeed/launchers/trojan_client.py b/ssrspeed/launchers/trojan_client.py index d9dc4f4..7e8b0cb 100644 --- a/ssrspeed/launchers/trojan_client.py +++ b/ssrspeed/launchers/trojan_client.py @@ -27,7 +27,7 @@ async def start_client(self, config: Dict[str, Any], debug=False): if self._process is None: - if self._platform == "Windows": + if Trojan._platform == "Windows": if debug: self._process = subprocess.Popen( [ @@ -50,7 +50,7 @@ async def start_client(self, config: Dict[str, Any], debug=False): f'Starting trojan with server {config["server"]}:{config["server_port"]}' ) - elif self._platform == "Linux" or self._platform == "MacOS": + elif Trojan._platform == "Linux" or Trojan._platform == "MacOS": if debug: self._process = subprocess.Popen( [ diff --git a/ssrspeed/launchers/v2ray_client.py b/ssrspeed/launchers/v2ray_client.py index f0d64b2..be5f6c3 100644 --- a/ssrspeed/launchers/v2ray_client.py +++ b/ssrspeed/launchers/v2ray_client.py @@ -27,7 +27,7 @@ async def start_client(self, config: Dict[str, Any], debug=False): if self._process is None: - if self._platform == "Windows": + if V2Ray._platform == "Windows": if debug: self._process = subprocess.Popen( [ @@ -50,7 +50,7 @@ async def start_client(self, config: Dict[str, Any], debug=False): f'Starting v2ray-core with server {config["server"]}:{config["server_port"]}' ) - elif self._platform == "Linux" or self._platform == "MacOS": + elif V2Ray._platform == "Linux" or V2Ray._platform == "MacOS": if debug: self._process = subprocess.Popen( [ diff --git a/ssrspeed/result/exporter.py b/ssrspeed/result/exporter.py index 8c8438c..a8072f3 100644 --- a/ssrspeed/result/exporter.py +++ b/ssrspeed/result/exporter.py @@ -4,8 +4,8 @@ import requests from loguru import logger -from pilmoji import Pilmoji from PIL import Image, ImageDraw, ImageFont +from pilmoji import Pilmoji from ssrspeed.config import ssrconfig from ssrspeed.paths import KEY_PATH @@ -118,7 +118,7 @@ def export_wps_result(self, result, export_type: int = 0): def __get_max_width(self, result: dict) -> tuple: font = self.__font - draw = ImageDraw.Draw(Image.new("RGB", (1, 1), (255, 255, 255))) + pilmoji = Pilmoji(Image.new("RGB", (1, 1), (255, 255, 255))) max_group_width = 0 max_remark_width = 0 len_in = 0 @@ -128,12 +128,12 @@ def __get_max_width(self, result: dict) -> tuple: remark = item["remarks"] inres = item["InRes"] outres = item["OutRes"] - max_group_width = max(max_group_width, draw.textsize(group, font=font)[0]) + max_group_width = max(max_group_width, pilmoji.getsize(group, font=font)[0]) max_remark_width = max( - max_remark_width, draw.textsize(remark, font=font)[0] + max_remark_width, pilmoji.getsize(remark, font=font)[0] ) - len_in = max(len_in, draw.textsize(inres, font=font)[0]) - len_out = max(len_out, draw.textsize(outres, font=font)[0]) + len_in = max(len_in, pilmoji.getsize(inres, font=font)[0]) + len_out = max(len_out, pilmoji.getsize(outres, font=font)[0]) return max_group_width + 10, max_remark_width + 10, len_in + 20, len_out + 20 """ @@ -266,7 +266,7 @@ def __export_as_png(self, result: dict): "RGB", (image_right_position, new_image_height), (255, 255, 255) ) draw = ImageDraw.Draw(result_img) - pilmoji = Pilmoji(result_img) + pilmoji = Pilmoji(result_img, emoji_position_offset=(0, 3)) # draw.line( # ( diff --git a/ssrspeed/speedtest/speed_test.py b/ssrspeed/speedtest/speed_test.py index 459db12..cb08c45 100644 --- a/ssrspeed/speedtest/speed_test.py +++ b/ssrspeed/speedtest/speed_test.py @@ -4,7 +4,6 @@ import socket import geoip2.database -import pynat import socks from geoip2.errors import AddressNotFoundError from loguru import logger @@ -18,7 +17,7 @@ ) from ssrspeed.paths import KEY_PATH from ssrspeed.speedtest.methodology import SpeedTestMethods -from ssrspeed.utils import async_check_port, domain2ip, ip_loc, get_ip_info +from ssrspeed.utils import async_check_port, domain2ip, get_ip_info, ip_loc LOCAL_ADDRESS = ssrconfig["localAddress"] LOCAL_PORT = int(ssrconfig["localPort"]) @@ -319,7 +318,7 @@ async def __fast_start_test( _item["ntt"]["public_port"] = eport if t: nat_info += " - NAT Type: " + t - if t != pynat.BLOCKED: + if t != "Blocked": nat_info += f" - Internal End: {sip}:{sport}" nat_info += f" - Public End: {eip}:{eport}" @@ -495,7 +494,7 @@ async def __base_start_test( _item["ntt"]["public_port"] = eport if t: nat_info += " - NAT Type: " + t - if t != pynat.BLOCKED: + if t != "Blocked": nat_info += f" - Internal End: {sip}:{sport}" nat_info += f" - Public End: {eip}:{eport}" diff --git a/ssrspeed/utils/__init__.py b/ssrspeed/utils/__init__.py index 2768178..c411aa2 100644 --- a/ssrspeed/utils/__init__.py +++ b/ssrspeed/utils/__init__.py @@ -1,8 +1,8 @@ from . import b64plus from .geoip import domain2ip, ip_loc, parse_location -from .pynat import get_ip_info -from .platform_check import check_platform +from .platform_check import PLATFORM from .port_check import async_check_port, sync_check_port +from .pynat import get_ip_info from .reqs_check import RequirementsCheck __all__ = [ @@ -11,7 +11,7 @@ "ip_loc", "parse_location", "get_ip_info", - "check_platform", + "PLATFORM", "async_check_port", "sync_check_port", "RequirementsCheck", diff --git a/ssrspeed/utils/platform_check.py b/ssrspeed/utils/platform_check.py index 504ee3d..d12ee50 100644 --- a/ssrspeed/utils/platform_check.py +++ b/ssrspeed/utils/platform_check.py @@ -2,6 +2,8 @@ from loguru import logger +PLATFORM = None + def check_platform() -> str: tmp = platform.platform() @@ -14,3 +16,7 @@ def check_platform() -> str: return "MacOS" else: return "Unknown" + + +if not PLATFORM: + PLATFORM = check_platform() diff --git a/ssrspeed/utils/pynat.py b/ssrspeed/utils/pynat.py index 1927e69..8ef4682 100644 --- a/ssrspeed/utils/pynat.py +++ b/ssrspeed/utils/pynat.py @@ -28,14 +28,13 @@ Drop Python 2 support by Oreomeow, 2022-09-12. """ -import sys -import socket -import random -import codecs import argparse +import codecs import ipaddress +import random import secrets - +import socket +import sys __version__ = "0.7.0" url = "https://github.com/aarant/pynat" diff --git a/ssrspeed/utils/reqs_check.py b/ssrspeed/utils/reqs_check.py index 0dcb264..7ae1a20 100644 --- a/ssrspeed/utils/reqs_check.py +++ b/ssrspeed/utils/reqs_check.py @@ -6,7 +6,7 @@ from loguru import logger from ssrspeed.paths import KEY_PATH -from ssrspeed.utils import check_platform +from ssrspeed.utils import PLATFORM CLIENTS_DIR = KEY_PATH["clients"] DATABASES_DIR = KEY_PATH["databases"] @@ -60,10 +60,9 @@ def __init__(self): } def check(self): - platform = check_platform() - if platform == "Windows": + if PLATFORM == "Windows": self.__checks(self.__win_require) - elif platform == "Linux" or platform == "MacOS": + elif PLATFORM == "Linux" or PLATFORM == "MacOS": self.__linux_check() self.__checks(self.__linux_require) else: diff --git a/ssrspeed/web.py b/ssrspeed/web.py index b64b8ac..a546463 100644 --- a/ssrspeed/web.py +++ b/ssrspeed/web.py @@ -14,7 +14,7 @@ from ssrspeed.paths import KEY_PATH, ROOT_PATH from ssrspeed.shell import web_cli as console_cfg from ssrspeed.type.errors.webapi import FileNotAllowed, WebFileCommonError -from ssrspeed.utils import RequirementsCheck, check_platform +from ssrspeed.utils import PLATFORM, RequirementsCheck from ssrspeed.utils.web import get_post_data WEB_API_VERSION = ssrconfig["WEB_API_VERSION"] @@ -191,8 +191,7 @@ def get_results(): if __name__ == "__main__": - pfInfo = check_platform() - if pfInfo == "Unknown": + if PLATFORM == "Unknown": logger.critical( "Your system does not support it. Please contact the developer." )