59 lines
1.6 KiB
Python
59 lines
1.6 KiB
Python
|
|
import asyncio
|
||
|
|
import logging
|
||
|
|
|
||
|
|
logger = logging.getLogger("yuxi.channel.xmpp.status")
|
||
|
|
|
||
|
|
|
||
|
|
async def probe_xmpp(
|
||
|
|
jid: str,
|
||
|
|
host: str = "",
|
||
|
|
port: int = 5222,
|
||
|
|
use_ssl: bool = False,
|
||
|
|
timeout: float = 10.0,
|
||
|
|
) -> bool:
|
||
|
|
if not jid:
|
||
|
|
return False
|
||
|
|
|
||
|
|
from slixmpp import ClientXMPP
|
||
|
|
|
||
|
|
class ProbeBot(ClientXMPP):
|
||
|
|
def __init__(self, j, h, p, ssl):
|
||
|
|
super().__init__(j, "probe-only")
|
||
|
|
self.probe_ok = False
|
||
|
|
self._host = h
|
||
|
|
self._port = p
|
||
|
|
self._use_ssl = ssl
|
||
|
|
|
||
|
|
async def run_probe(self):
|
||
|
|
self.register_plugin("xep_0199")
|
||
|
|
try:
|
||
|
|
if self._host:
|
||
|
|
self.connect(address=(self._host, self._port), use_ssl=self._use_ssl)
|
||
|
|
else:
|
||
|
|
self.connect()
|
||
|
|
fut = asyncio.ensure_future(self.process(forever=False))
|
||
|
|
await asyncio.sleep(3.0)
|
||
|
|
self.disconnect()
|
||
|
|
try:
|
||
|
|
await asyncio.wait_for(fut, timeout=2.0)
|
||
|
|
except (TimeoutError, asyncio.CancelledError):
|
||
|
|
pass
|
||
|
|
self.probe_ok = True
|
||
|
|
except Exception:
|
||
|
|
self.probe_ok = False
|
||
|
|
|
||
|
|
bot = ProbeBot(jid, host, port, use_ssl)
|
||
|
|
try:
|
||
|
|
await asyncio.wait_for(bot.run_probe(), timeout=timeout)
|
||
|
|
except (TimeoutError, Exception):
|
||
|
|
return False
|
||
|
|
return bot.probe_ok
|
||
|
|
|
||
|
|
|
||
|
|
def build_xmpp_status_summary(snapshot) -> dict:
|
||
|
|
return {
|
||
|
|
"connected": getattr(snapshot, "connected", False),
|
||
|
|
"jid": getattr(snapshot, "jid", ""),
|
||
|
|
"rooms": getattr(snapshot, "rooms", []),
|
||
|
|
}
|