feat: use SystemProfile path for sing-box config storage
This commit is contained in:
parent
a3cf476af4
commit
89705bbab7
1 changed files with 3 additions and 11 deletions
|
|
@ -1,20 +1,18 @@
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from core.Constants import Constants
|
||||||
from core.utils.encrypted_proxy.net import get_real_ip, verify_ip
|
from core.utils.encrypted_proxy.net import get_real_ip, verify_ip
|
||||||
from core.utils.encrypted_proxy.singbox import SingboxRunner
|
from core.utils.encrypted_proxy.singbox import SingboxRunner
|
||||||
import socket
|
import socket
|
||||||
|
|
||||||
|
|
||||||
def _resolve(host: str) -> str:
|
def _resolve(host: str) -> str:
|
||||||
return socket.gethostbyname(host)
|
return socket.gethostbyname(host)
|
||||||
|
|
||||||
|
|
||||||
def _get_hy2_domain(server_host: str) -> str:
|
def _get_hy2_domain(server_host: str) -> str:
|
||||||
parts = server_host.split(".", 1)
|
parts = server_host.split(".", 1)
|
||||||
if len(parts) == 2:
|
if len(parts) == 2:
|
||||||
return f"hy2.{parts[1]}"
|
return f"hy2.{parts[1]}"
|
||||||
return server_host
|
return server_host
|
||||||
|
|
||||||
|
|
||||||
def build_hysteria_config(username: str, password: str,
|
def build_hysteria_config(username: str, password: str,
|
||||||
server_host: str, socks5_port: int) -> dict:
|
server_host: str, socks5_port: int) -> dict:
|
||||||
hy2_domain = _get_hy2_domain(server_host)
|
hy2_domain = _get_hy2_domain(server_host)
|
||||||
|
|
@ -73,17 +71,14 @@ def build_hysteria_config(username: str, password: str,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def enable_hysteria(username: str, password: str, server_host: str,
|
def enable_hysteria(username: str, password: str, server_host: str,
|
||||||
config_dir: Path, socks5_port: int,
|
config_dir: Path, socks5_port: int,
|
||||||
observer=None) -> bool:
|
observer=None) -> bool:
|
||||||
real_ip = get_real_ip()
|
real_ip = get_real_ip()
|
||||||
runner = SingboxRunner()
|
runner = SingboxRunner()
|
||||||
runner.stop()
|
runner.stop()
|
||||||
|
config_path = Path(Constants.HV_DATA_HOME) / f"{username}-sing-box.json"
|
||||||
config_path = config_dir / f"{username}-sing-box.json"
|
|
||||||
config = build_hysteria_config(username, password, server_host, socks5_port)
|
config = build_hysteria_config(username, password, server_host, socks5_port)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
runner.write_config(config_path, config)
|
runner.write_config(config_path, config)
|
||||||
ok = runner.start(config_path)
|
ok = runner.start(config_path)
|
||||||
|
|
@ -91,12 +86,10 @@ def enable_hysteria(username: str, password: str, server_host: str,
|
||||||
if observer:
|
if observer:
|
||||||
observer.notify("error", str(e))
|
observer.notify("error", str(e))
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if not ok:
|
if not ok:
|
||||||
if observer:
|
if observer:
|
||||||
observer.notify("error", "sing-box not active after start")
|
observer.notify("error", "sing-box not active after start")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
proxy_ip = verify_ip(socks5_port)
|
proxy_ip = verify_ip(socks5_port)
|
||||||
if observer:
|
if observer:
|
||||||
observer.notify("connected", {
|
observer.notify("connected", {
|
||||||
|
|
@ -106,9 +99,8 @@ def enable_hysteria(username: str, password: str, server_host: str,
|
||||||
})
|
})
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def disable_hysteria(observer=None) -> bool:
|
def disable_hysteria(observer=None) -> bool:
|
||||||
SingboxRunner().stop()
|
SingboxRunner().stop()
|
||||||
if observer:
|
if observer:
|
||||||
observer.notify("disconnected", {})
|
observer.notify("disconnected", {})
|
||||||
return True
|
return True
|
||||||
Loading…
Reference in a new issue