#924 improvements

This commit is contained in:
Flowseal
2026-06-17 09:43:06 +03:00
parent 784a7f659b
commit b0010af130
3 changed files with 14 additions and 10 deletions

View File

@@ -185,7 +185,7 @@ def _run_proxy_thread() -> None:
loop.run_until_complete(_run(stop_event=stop_ev))
except Exception as exc:
log.error("Proxy thread crashed: %s", exc)
msg = diagnose_listen_error(exc)
msg, _ = diagnose_listen_error(exc)
if msg:
_show_error(msg)
finally:

View File

@@ -1,8 +1,9 @@
from __future__ import annotations
import errno
import webbrowser
from typing import Optional
from typing import Optional, Tuple, Callable
MSG_PORT_BUSY = (
@@ -23,6 +24,7 @@ MSG_PERMISSION = (
MSG_BAD_ADDRESS = (
"Не удалось запустить прокси:\n"
"Некорректный или недоступный адрес для прослушивания.\n\n"
"Проверьте решение по открывшейся в браузере ссылке.\n"
"Проверьте host и порт в настройках прокси и перезапустите."
)
@@ -33,7 +35,7 @@ _WSA_EADDRINUSE = 10048
_WSA_EADDRNOTAVAIL = 10049
def diagnose_listen_error(exc: BaseException) -> Optional[str]:
def diagnose_listen_error(exc: BaseException) -> Tuple[Optional[str], Optional[Callable]]:
"""Map a listen-socket bind failure to a user-facing message.
Returns None when the exception is not a recognizable bind failure,
@@ -46,10 +48,10 @@ def diagnose_listen_error(exc: BaseException) -> Optional[str]:
winerror = getattr(exc, "winerror", None)
if err == errno.EADDRINUSE or winerror == _WSA_EADDRINUSE:
return MSG_PORT_BUSY
return MSG_PORT_BUSY, None
if err == errno.EACCES or winerror == _WSA_EACCES:
return MSG_PERMISSION
return MSG_PERMISSION, None
if (winerror in (_WSA_EFAULT, _WSA_EADDRNOTAVAIL)
or err in (errno.EADDRNOTAVAIL, errno.EFAULT)):
return MSG_BAD_ADDRESS
return None
return MSG_BAD_ADDRESS, lambda : webbrowser.open("https://github.com/Flowseal/tg-ws-proxy/issues/903#issuecomment-4726752103")
return None, None

View File

@@ -228,7 +228,7 @@ _proxy_thread: Optional[threading.Thread] = None
_async_stop: Optional[Tuple[asyncio.AbstractEventLoop, asyncio.Event]] = None
def _run_proxy_thread(on_port_busy: Callable[[str], None]) -> None:
def _run_proxy_thread(show_error: Callable[[str], None]) -> None:
global _async_stop
loop = asyncio.new_event_loop()
@@ -240,9 +240,11 @@ def _run_proxy_thread(on_port_busy: Callable[[str], None]) -> None:
loop.run_until_complete(_run(stop_event=stop_ev))
except Exception as exc:
log.error("Proxy thread crashed: %s", repr(exc))
msg = diagnose_listen_error(exc)
msg, diagnose_called = diagnose_listen_error(exc)
if msg:
on_port_busy(msg)
show_error(msg)
if diagnose_called:
diagnose_called()
finally:
loop.close()
_async_stop = None