mirror of
https://github.com/bunny-lab-io/Borealis.git
synced 2025-12-17 17:15:48 -07:00
Overhaul of Reverse Tunnel Code
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
"""Placeholder Bash channel (Agent side)."""
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
MSG_DATA = 0x05
|
||||
MSG_CONTROL = 0x09
|
||||
MSG_CLOSE = 0x08
|
||||
CLOSE_PROTOCOL_ERROR = 3
|
||||
CLOSE_AGENT_SHUTDOWN = 6
|
||||
|
||||
|
||||
class BashChannel:
|
||||
"""Stub Bash handler that immediately reports unsupported."""
|
||||
|
||||
def __init__(self, role, tunnel, channel_id: int, metadata: Optional[Dict[str, Any]]):
|
||||
self.role = role
|
||||
self.tunnel = tunnel
|
||||
self.channel_id = channel_id
|
||||
self.metadata = metadata or {}
|
||||
self.loop = getattr(role, "loop", None) or asyncio.get_event_loop()
|
||||
self._closed = False
|
||||
|
||||
async def start(self) -> None:
|
||||
# Until Bash support is implemented, close the channel to free resources.
|
||||
await self.stop(code=CLOSE_PROTOCOL_ERROR, reason="bash_unsupported")
|
||||
|
||||
async def on_frame(self, frame) -> None:
|
||||
if self._closed:
|
||||
return
|
||||
if frame.msg_type in (MSG_DATA, MSG_CONTROL):
|
||||
# Ignore payloads but acknowledge by stopping the channel to avoid leaks.
|
||||
await self.stop(code=CLOSE_PROTOCOL_ERROR, reason="bash_unsupported")
|
||||
elif frame.msg_type == MSG_CLOSE:
|
||||
await self.stop(code=CLOSE_AGENT_SHUTDOWN, reason="operator_close")
|
||||
|
||||
async def stop(self, code: int = CLOSE_PROTOCOL_ERROR, reason: str = "") -> None:
|
||||
if self._closed:
|
||||
return
|
||||
self._closed = True
|
||||
try:
|
||||
await self.role._send_frame(self.tunnel, self.role.close_frame(self.channel_id, code, reason or "bash_closed"))
|
||||
except Exception:
|
||||
pass
|
||||
self.role._log(f"reverse_tunnel bash channel stopped channel={self.channel_id} reason={reason or 'closed'}")
|
||||
|
||||
|
||||
__all__ = ["BashChannel"]
|
||||
@@ -0,0 +1,35 @@
|
||||
"""Expose the PowerShell channel under the domain path, with file-based import fallback."""
|
||||
from __future__ import annotations
|
||||
|
||||
import importlib.util
|
||||
from pathlib import Path
|
||||
|
||||
powershell_module = None
|
||||
|
||||
# Attempt package-relative import first
|
||||
try: # pragma: no cover - best effort
|
||||
from ....ReverseTunnel import tunnel_Powershell as powershell_module # type: ignore
|
||||
except Exception:
|
||||
powershell_module = None
|
||||
|
||||
# Fallback: load directly from file path to survive non-package runtimes
|
||||
if powershell_module is None:
|
||||
try:
|
||||
base = Path(__file__).resolve().parents[3] / "ReverseTunnel" / "tunnel_Powershell.py"
|
||||
spec = importlib.util.spec_from_file_location("tunnel_Powershell", base)
|
||||
if spec and spec.loader:
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(module) # type: ignore
|
||||
powershell_module = module
|
||||
except Exception:
|
||||
powershell_module = None
|
||||
|
||||
if powershell_module and hasattr(powershell_module, "PowershellChannel"):
|
||||
PowershellChannel = powershell_module.PowershellChannel # type: ignore
|
||||
else: # pragma: no cover - safety guard
|
||||
class PowershellChannel: # type: ignore
|
||||
def __init__(self, *args, **kwargs):
|
||||
raise ImportError("PowerShell channel unavailable")
|
||||
|
||||
|
||||
__all__ = ["PowershellChannel"]
|
||||
@@ -0,0 +1,6 @@
|
||||
"""Protocol handlers for interactive shell tunnels (Agent side)."""
|
||||
|
||||
from .Powershell import PowershellChannel
|
||||
from .Bash import BashChannel
|
||||
|
||||
__all__ = ["PowershellChannel", "BashChannel"]
|
||||
@@ -0,0 +1,3 @@
|
||||
"""Interactive shell domain (PowerShell/Bash) handlers."""
|
||||
|
||||
__all__ = ["tunnel", "Protocols"]
|
||||
@@ -0,0 +1,5 @@
|
||||
"""Placeholder module for remote interactive shell tunnel domain (Agent side)."""
|
||||
|
||||
DOMAIN_NAME = "remote-interactive-shell"
|
||||
|
||||
__all__ = ["DOMAIN_NAME"]
|
||||
Reference in New Issue
Block a user