Files
tg-ws-proxy/linux.py

306 lines
9.1 KiB
Python
Raw Normal View History

2026-03-19 06:55:55 +03:00
from __future__ import annotations
import os
import subprocess
import sys
import threading
import time
2026-03-29 15:21:56 +03:00
from typing import Optional
2026-03-19 06:55:55 +03:00
import customtkinter as ctk
import pyperclip
import pystray
2026-03-29 15:21:56 +03:00
from PIL import Image, ImageTk
2026-03-19 06:55:55 +03:00
2026-04-09 19:55:12 +03:00
from proxy import get_link_host
2026-03-28 15:45:08 +03:00
2026-03-29 15:21:56 +03:00
from utils.tray_common import (
APP_NAME, DEFAULT_CONFIG, FIRST_RUN_MARKER, LOG_FILE,
acquire_lock, bootstrap, check_ipv6_warning, ctk_run_dialog,
ensure_ctk_thread, ensure_dirs, load_config, load_icon, log,
maybe_notify_update, quit_ctk, release_lock, restart_proxy,
save_config, start_proxy, stop_proxy, tg_proxy_url,
)
from ui.ctk_tray_ui import (
2026-03-29 15:21:56 +03:00
install_tray_config_buttons, install_tray_config_form,
populate_first_run_window, tray_settings_scroll_and_footer,
validate_config_form,
)
from ui.ctk_theme import (
2026-03-29 15:21:56 +03:00
CONFIG_DIALOG_FRAME_PAD, CONFIG_DIALOG_SIZE, FIRST_RUN_SIZE,
create_ctk_toplevel, ctk_theme_for_platform, main_content_frame,
)
2026-03-19 06:55:55 +03:00
_tray_icon: Optional[object] = None
_config: dict = {}
2026-03-29 15:21:56 +03:00
_exiting = False
2026-03-19 06:55:55 +03:00
2026-03-29 15:21:56 +03:00
# dialogs (tkinter messagebox)
2026-03-19 06:55:55 +03:00
2026-03-29 15:21:56 +03:00
def _msgbox(kind: str, text: str, title: str, **kw):
import tkinter as _tk
from tkinter import messagebox as _mb
2026-03-19 06:55:55 +03:00
2026-03-29 15:21:56 +03:00
root = _tk.Tk()
root.withdraw()
2026-03-19 06:55:55 +03:00
try:
2026-03-29 15:21:56 +03:00
root.attributes("-topmost", True)
2026-03-19 06:55:55 +03:00
except Exception:
pass
2026-03-29 15:21:56 +03:00
result = getattr(_mb, kind)(title, text, parent=root, **kw)
root.destroy()
return result
2026-03-19 06:55:55 +03:00
2026-03-29 15:21:56 +03:00
def _show_error(text: str, title: str = "TG WS Proxy — Ошибка") -> None:
_msgbox("showerror", text, title)
2026-03-19 06:55:55 +03:00
2026-03-29 15:21:56 +03:00
def _show_info(text: str, title: str = "TG WS Proxy") -> None:
_msgbox("showinfo", text, title)
2026-03-19 06:55:55 +03:00
2026-03-29 15:21:56 +03:00
def _ask_yes_no(text: str, title: str = "TG WS Proxy") -> bool:
return bool(_msgbox("askyesno", text, title))
2026-03-19 06:55:55 +03:00
2026-03-29 15:21:56 +03:00
def _apply_window_icon(root) -> None:
icon_img = load_icon()
if icon_img:
root._ctk_icon_photo = ImageTk.PhotoImage(icon_img.resize((64, 64)))
root.iconphoto(False, root._ctk_icon_photo)
2026-03-29 15:21:56 +03:00
# tray callbacks
2026-03-28 15:45:08 +03:00
2026-03-19 06:55:55 +03:00
2026-03-29 15:21:56 +03:00
def _on_open_in_telegram(icon=None, item=None) -> None:
url = tg_proxy_url(_config)
log.info("Copying %s", url)
2026-03-19 06:55:55 +03:00
try:
2026-03-29 15:21:56 +03:00
pyperclip.copy(url)
_show_info(
f"Ссылка скопирована в буфер обмена, отправьте её в Telegram и нажмите по ней ЛКМ:\n{url}"
2026-03-19 06:55:55 +03:00
)
except Exception as exc:
2026-03-29 15:21:56 +03:00
log.error("Clipboard copy failed: %s", exc)
_show_error(f"Не удалось скопировать ссылку:\n{exc}")
def _on_copy_link(icon=None, item=None) -> None:
url = tg_proxy_url(_config)
log.info("Copying link: %s", url)
try:
pyperclip.copy(url)
except Exception as exc:
log.error("Clipboard copy failed: %s", exc)
_show_error(f"Не удалось скопировать ссылку:\n{exc}")
2026-03-29 15:21:56 +03:00
def _on_restart(icon=None, item=None) -> None:
threading.Thread(
target=lambda: restart_proxy(_config, _show_error), daemon=True
).start()
2026-03-29 15:21:56 +03:00
def _on_edit_config(icon=None, item=None) -> None:
threading.Thread(target=_edit_config_dialog, daemon=True).start()
2026-03-28 15:45:08 +03:00
2026-03-19 06:55:55 +03:00
2026-03-29 15:21:56 +03:00
def _on_open_logs(icon=None, item=None) -> None:
log.info("Opening log file: %s", LOG_FILE)
if LOG_FILE.exists():
env = {k: v for k, v in os.environ.items() if k not in ("VIRTUAL_ENV", "PYTHONPATH", "PYTHONHOME")}
subprocess.Popen(
["xdg-open", str(LOG_FILE)], env=env,
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
stdin=subprocess.DEVNULL, start_new_session=True,
2026-03-19 06:55:55 +03:00
)
2026-03-29 15:21:56 +03:00
else:
_show_info("Файл логов ещё не создан.")
2026-03-19 06:55:55 +03:00
2026-03-29 15:21:56 +03:00
def _on_exit(icon=None, item=None) -> None:
global _exiting
if _exiting:
os._exit(0)
return
_exiting = True
log.info("User requested exit")
quit_ctk()
threading.Thread(target=lambda: (time.sleep(3), os._exit(0)), daemon=True, name="force-exit").start()
if icon:
icon.stop()
2026-03-19 06:55:55 +03:00
2026-03-29 15:21:56 +03:00
# settings dialog
2026-03-19 06:55:55 +03:00
2026-03-29 15:21:56 +03:00
def _edit_config_dialog() -> None:
2026-04-09 20:10:48 +03:00
if not ensure_ctk_thread(ctk, _config.get("appearance", "auto")):
2026-03-19 06:55:55 +03:00
_show_error("customtkinter не установлен.")
return
cfg = dict(_config)
2026-03-29 15:21:56 +03:00
def _build(done: threading.Event) -> None:
2026-03-28 15:45:08 +03:00
theme = ctk_theme_for_platform()
w, h = CONFIG_DIALOG_SIZE
root = create_ctk_toplevel(
2026-03-29 15:21:56 +03:00
ctk, title="TG WS Proxy — Настройки", width=w, height=h, theme=theme,
after_create=_apply_window_icon,
2026-03-28 15:45:08 +03:00
)
fpx, fpy = CONFIG_DIALOG_FRAME_PAD
frame = main_content_frame(ctk, root, theme, padx=fpx, pady=fpy)
scroll, footer = tray_settings_scroll_and_footer(ctk, frame, theme)
2026-03-29 15:21:56 +03:00
widgets = install_tray_config_form(ctk, scroll, theme, cfg, DEFAULT_CONFIG, show_autostart=False)
2026-03-19 06:55:55 +03:00
_original_appearance = ctk.get_appearance_mode()
2026-03-29 15:21:56 +03:00
def _finish() -> None:
2026-03-28 15:45:08 +03:00
root.destroy()
done.set()
2026-03-19 06:55:55 +03:00
def _cancel() -> None:
ctk.set_appearance_mode(_original_appearance)
_finish()
2026-03-29 15:21:56 +03:00
def on_save() -> None:
2026-03-29 17:30:39 +03:00
from tkinter import messagebox
2026-03-29 15:21:56 +03:00
merged = validate_config_form(widgets, DEFAULT_CONFIG, include_autostart=False)
2026-03-28 15:45:08 +03:00
if isinstance(merged, str):
2026-03-29 17:30:39 +03:00
messagebox.showerror("TG WS Proxy — Ошибка", merged, parent=root)
2026-03-28 15:45:08 +03:00
return
_ui_only_keys = {"appearance", "check_updates"}
config_changed = any(merged.get(k) != cfg.get(k) for k in merged)
proxy_changed = any(merged.get(k) != cfg.get(k) for k in merged if k not in _ui_only_keys)
if not config_changed:
_finish()
return
2026-03-29 15:21:56 +03:00
save_config(merged)
_config.update(merged)
log.info("Config saved: %s", merged)
2026-03-28 15:45:08 +03:00
_tray_icon.menu = _build_menu()
2026-03-19 06:55:55 +03:00
if not proxy_changed:
_finish()
return
2026-03-28 15:45:08 +03:00
do_restart = messagebox.askyesno(
"Перезапустить?",
"Настройки сохранены.\n\nПерезапустить прокси сейчас?",
2026-03-29 15:21:56 +03:00
parent=root,
)
2026-03-28 15:45:08 +03:00
_finish()
if do_restart:
2026-03-29 15:21:56 +03:00
threading.Thread(target=lambda: restart_proxy(_config, _show_error), daemon=True).start()
2026-03-19 06:55:55 +03:00
root.protocol("WM_DELETE_WINDOW", _cancel)
install_tray_config_buttons(ctk, footer, theme, on_save=on_save, on_cancel=_cancel)
2026-03-19 06:55:55 +03:00
2026-03-29 15:21:56 +03:00
ctk_run_dialog(_build)
2026-03-19 06:55:55 +03:00
2026-03-29 15:21:56 +03:00
# first run
2026-03-19 06:55:55 +03:00
2026-03-29 15:21:56 +03:00
def _show_first_run() -> None:
ensure_dirs()
2026-03-19 06:55:55 +03:00
if FIRST_RUN_MARKER.exists():
return
2026-04-09 20:10:48 +03:00
if not ensure_ctk_thread(ctk, _config.get("appearance", "auto")):
2026-03-19 06:55:55 +03:00
FIRST_RUN_MARKER.touch()
return
2026-03-28 15:45:08 +03:00
host = _config.get("host", DEFAULT_CONFIG["host"])
port = _config.get("port", DEFAULT_CONFIG["port"])
secret = _config.get("secret", DEFAULT_CONFIG["secret"])
2026-03-29 15:21:56 +03:00
def _build(done: threading.Event) -> None:
2026-03-28 15:45:08 +03:00
theme = ctk_theme_for_platform()
w, h = FIRST_RUN_SIZE
root = create_ctk_toplevel(
2026-03-29 15:21:56 +03:00
ctk, title="TG WS Proxy", width=w, height=h, theme=theme,
after_create=_apply_window_icon,
2026-03-28 15:45:08 +03:00
)
2026-03-19 06:55:55 +03:00
2026-03-29 15:21:56 +03:00
def on_done(open_tg: bool) -> None:
2026-03-28 15:45:08 +03:00
FIRST_RUN_MARKER.touch()
root.destroy()
done.set()
if open_tg:
_on_open_in_telegram()
2026-03-19 06:55:55 +03:00
2026-03-29 15:21:56 +03:00
populate_first_run_window(ctk, root, theme, host=host, port=port, secret=secret, on_done=on_done)
2026-03-19 06:55:55 +03:00
2026-03-29 15:21:56 +03:00
ctk_run_dialog(_build)
2026-03-19 06:55:55 +03:00
2026-03-29 15:21:56 +03:00
# tray menu
2026-03-19 06:55:55 +03:00
def _build_menu():
host = _config.get("host", DEFAULT_CONFIG["host"])
port = _config.get("port", DEFAULT_CONFIG["port"])
2026-04-09 19:55:12 +03:00
link_host = get_link_host(host)
2026-03-19 06:55:55 +03:00
return pystray.Menu(
2026-03-29 15:21:56 +03:00
pystray.MenuItem(f"Открыть в Telegram ({link_host}:{port})", _on_open_in_telegram, default=True),
pystray.MenuItem("Скопировать ссылку", _on_copy_link),
2026-03-19 06:55:55 +03:00
pystray.Menu.SEPARATOR,
pystray.MenuItem("Перезапустить прокси", _on_restart),
pystray.MenuItem("Настройки...", _on_edit_config),
pystray.MenuItem("Открыть логи", _on_open_logs),
pystray.Menu.SEPARATOR,
pystray.MenuItem("Выход", _on_exit),
)
2026-03-29 15:21:56 +03:00
# entry point
2026-03-19 06:55:55 +03:00
2026-03-29 15:21:56 +03:00
def run_tray() -> None:
global _tray_icon, _config
2026-03-19 06:55:55 +03:00
2026-03-29 15:21:56 +03:00
_config = load_config()
bootstrap(_config)
2026-03-19 06:55:55 +03:00
if pystray is None or Image is None:
log.error("pystray or Pillow not installed; running in console mode")
2026-03-29 15:21:56 +03:00
start_proxy(_config, _show_error)
2026-03-19 06:55:55 +03:00
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
stop_proxy()
return
2026-03-29 15:21:56 +03:00
start_proxy(_config, _show_error)
maybe_notify_update(_config, lambda: _exiting, _ask_yes_no)
2026-03-19 06:55:55 +03:00
_show_first_run()
2026-03-29 15:21:56 +03:00
check_ipv6_warning(_show_info)
2026-03-19 06:55:55 +03:00
2026-03-29 15:21:56 +03:00
_tray_icon = pystray.Icon(APP_NAME, load_icon(), "TG WS Proxy", menu=_build_menu())
2026-03-19 06:55:55 +03:00
log.info("Tray icon running")
_tray_icon.run()
stop_proxy()
log.info("Tray app exited")
2026-03-29 15:21:56 +03:00
def main() -> None:
2026-04-14 00:27:06 +03:00
if not acquire_lock():
2026-03-19 06:55:55 +03:00
_show_info("Приложение уже запущено.", os.path.basename(sys.argv[0]))
return
try:
run_tray()
finally:
2026-03-29 15:21:56 +03:00
release_lock()
2026-03-19 06:55:55 +03:00
if __name__ == "__main__":
main()