mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2026-01-31 10:01:53 +08:00
feat: 支持 turn 中转,可以远程访问 h264/webrtc #197
This commit is contained in:
@@ -81,6 +81,7 @@ from ..validators.net import valid_port
|
||||
from ..validators.net import valid_ports_list
|
||||
from ..validators.net import valid_mac
|
||||
from ..validators.net import valid_ssl_ciphers
|
||||
from ..validators.net import valid_ice_servers
|
||||
|
||||
from ..validators.hid import valid_hid_key
|
||||
from ..validators.hid import valid_hid_mouse_output
|
||||
@@ -860,6 +861,7 @@ def _get_config_scheme() -> dict:
|
||||
], type=valid_command),
|
||||
"cmd_remove": Option([], type=valid_options),
|
||||
"cmd_append": Option([], type=valid_options),
|
||||
"local_ice_servers": Option([], type=valid_ice_servers, unpack_as="ice_servers"),
|
||||
},
|
||||
|
||||
"watchdog": {
|
||||
|
||||
@@ -2,6 +2,8 @@ import asyncio
|
||||
import asyncio.subprocess
|
||||
import socket
|
||||
import dataclasses
|
||||
import json
|
||||
from typing import Any
|
||||
|
||||
import netifaces
|
||||
|
||||
@@ -43,6 +45,7 @@ class JanusRunner: # pylint: disable=too-many-instance-attributes
|
||||
cmd: list[str],
|
||||
cmd_remove: list[str],
|
||||
cmd_append: list[str],
|
||||
ice_servers: list[dict[str, Any]],
|
||||
) -> None:
|
||||
|
||||
self.__stun = Stun(stun_host, stun_port, stun_timeout, stun_retries, stun_retries_delay)
|
||||
@@ -52,6 +55,7 @@ class JanusRunner: # pylint: disable=too-many-instance-attributes
|
||||
self.__check_retries_delay = check_retries_delay
|
||||
|
||||
self.__cmd = tools.build_cmd(cmd, cmd_remove, cmd_append)
|
||||
self.__ice_servers = ice_servers
|
||||
|
||||
self.__janus_task: (asyncio.Task | None) = None
|
||||
self.__janus_proc: (asyncio.subprocess.Process | None) = None # pylint: disable=no-member
|
||||
@@ -173,13 +177,25 @@ class JanusRunner: # pylint: disable=too-many-instance-attributes
|
||||
part.format(**placeholders)
|
||||
for part in cmd
|
||||
]
|
||||
self.__janus_proc = await aioproc.run_process(
|
||||
cmd=cmd,
|
||||
env={"JANUS_USTREAMER_WEB_ICE_URL": f"stun:{netcfg.stun_host}:{netcfg.stun_port}"},
|
||||
)
|
||||
env = {}
|
||||
ice_payload = self.__build_ice_payload(netcfg)
|
||||
if ice_payload:
|
||||
env["JANUS_USTREAMER_WEB_ICE_URL"] = ice_payload
|
||||
self.__janus_proc = await aioproc.run_process(cmd=cmd, env=env or None)
|
||||
get_logger(0).info("Started Janus pid=%d: %s", self.__janus_proc.pid, tools.cmdfmt(cmd))
|
||||
|
||||
async def __kill_janus_proc(self) -> None:
|
||||
if self.__janus_proc:
|
||||
await aioproc.kill_process(self.__janus_proc, 5, get_logger(0))
|
||||
self.__janus_proc = None
|
||||
|
||||
def __build_ice_payload(self, netcfg: _Netcfg) -> (str | None):
|
||||
if self.__ice_servers:
|
||||
try:
|
||||
return f"json:{json.dumps(self.__ice_servers, ensure_ascii=False)}"
|
||||
except Exception as ex: # pragma: no cover
|
||||
get_logger(0).error("Can't encode ICE servers: %s", tools.efmt(ex))
|
||||
return None
|
||||
if netcfg.stun_host and netcfg.stun_port:
|
||||
return f"stun:{netcfg.stun_host}:{netcfg.stun_port}"
|
||||
return None
|
||||
|
||||
@@ -136,7 +136,12 @@ class Stun:
|
||||
return (StunNatType.FULL_CONE_NAT, resp)
|
||||
|
||||
if first.changed is None:
|
||||
raise RuntimeError(f"Changed addr is None: {first}")
|
||||
get_logger(0).warning(
|
||||
"STUN server %s:%d responded without CHANGED-ADDRESS; skipping NAT type detection",
|
||||
self.__host,
|
||||
self.__port,
|
||||
)
|
||||
return (StunNatType.ERROR, first)
|
||||
resp = await self.__make_request("Change request [ext_ip != src_ip]", first.changed, b"")
|
||||
if not resp.ok:
|
||||
return (StunNatType.CHANGED_ADDR_ERROR, resp)
|
||||
|
||||
Reference in New Issue
Block a user