Skip to content

Commit

Permalink
🐎 Improve platform_check.py & adjust emoji export
Browse files Browse the repository at this point in the history
  • Loading branch information
Oreomeow committed Sep 13, 2022
1 parent d4d0337 commit 254000c
Show file tree
Hide file tree
Showing 15 changed files with 48 additions and 47 deletions.
2 changes: 1 addition & 1 deletion data/ssrspeed.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"addition": "OreosLab ©",
"uploadResult": false,
"hide_max_speed": false,
"font": "SourceHanSansCN-Medium.otf",
"font": "LXGWBright-Medium.otf",
"colors": [
{
"name": "origin",
Expand Down
5 changes: 2 additions & 3 deletions ssrspeed/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion ssrspeed/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
16 changes: 8 additions & 8 deletions ssrspeed/launchers/base_client.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions ssrspeed/launchers/ss_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
[
Expand All @@ -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(
[
Expand Down
4 changes: 2 additions & 2 deletions ssrspeed/launchers/ssr_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
[
Expand Down
4 changes: 2 additions & 2 deletions ssrspeed/launchers/trojan_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
[
Expand All @@ -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(
[
Expand Down
4 changes: 2 additions & 2 deletions ssrspeed/launchers/v2ray_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
[
Expand All @@ -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(
[
Expand Down
14 changes: 7 additions & 7 deletions ssrspeed/result/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

"""
Expand Down Expand Up @@ -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(
# (
Expand Down
7 changes: 3 additions & 4 deletions ssrspeed/speedtest/speed_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import socket

import geoip2.database
import pynat
import socks
from geoip2.errors import AddressNotFoundError
from loguru import logger
Expand All @@ -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"])
Expand Down Expand Up @@ -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}"

Expand Down Expand Up @@ -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}"

Expand Down
6 changes: 3 additions & 3 deletions ssrspeed/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -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__ = [
Expand All @@ -11,7 +11,7 @@
"ip_loc",
"parse_location",
"get_ip_info",
"check_platform",
"PLATFORM",
"async_check_port",
"sync_check_port",
"RequirementsCheck",
Expand Down
6 changes: 6 additions & 0 deletions ssrspeed/utils/platform_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from loguru import logger

PLATFORM = None


def check_platform() -> str:
tmp = platform.platform()
Expand All @@ -14,3 +16,7 @@ def check_platform() -> str:
return "MacOS"
else:
return "Unknown"


if not PLATFORM:
PLATFORM = check_platform()
9 changes: 4 additions & 5 deletions ssrspeed/utils/pynat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 3 additions & 4 deletions ssrspeed/utils/reqs_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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:
Expand Down
5 changes: 2 additions & 3 deletions ssrspeed/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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."
)
Expand Down

0 comments on commit 254000c

Please sign in to comment.