neruton 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- neruton/__init__.py +107 -0
- neruton/__main__.py +10 -0
- neruton/_version.py +11 -0
- neruton/cli.py +258 -0
- neruton/client.py +1017 -0
- neruton/config.py +188 -0
- neruton/errors.py +64 -0
- neruton/forwarder.py +810 -0
- neruton/protocol.py +358 -0
- neruton/py.typed +0 -0
- neruton/ui.py +127 -0
- neruton/yamux.py +974 -0
- neruton-0.2.0.dist-info/METADATA +136 -0
- neruton-0.2.0.dist-info/RECORD +18 -0
- neruton-0.2.0.dist-info/WHEEL +5 -0
- neruton-0.2.0.dist-info/entry_points.txt +2 -0
- neruton-0.2.0.dist-info/licenses/LICENSE +21 -0
- neruton-0.2.0.dist-info/top_level.txt +1 -0
neruton/__init__.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
"""Native pure-Python tunnel client & SDK for Neruton (WSS + Yamux)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from ._version import (
|
|
6
|
+
DEFAULT_BASE_DOMAIN,
|
|
7
|
+
DEFAULT_PANEL_HOST,
|
|
8
|
+
DEFAULT_PUBLIC_SCHEME,
|
|
9
|
+
DEFAULT_SERVER_URL,
|
|
10
|
+
TUNNEL_WS_PATH,
|
|
11
|
+
__version__,
|
|
12
|
+
)
|
|
13
|
+
from .client import SyncTunnel, Tunnel, connect, forward, http
|
|
14
|
+
from .client import open_tunnel as open # noqa: A001
|
|
15
|
+
from .config import Config, config_path, load_config, resolve_server_and_token, save_config
|
|
16
|
+
from .errors import (
|
|
17
|
+
AuthError,
|
|
18
|
+
NerutonError,
|
|
19
|
+
PrefixError,
|
|
20
|
+
RegistrationError,
|
|
21
|
+
TunnelClosedError,
|
|
22
|
+
TunnelConnectionError,
|
|
23
|
+
YamuxClosedError,
|
|
24
|
+
YamuxError,
|
|
25
|
+
YamuxProtocolError,
|
|
26
|
+
YamuxStreamResetError,
|
|
27
|
+
)
|
|
28
|
+
from .forwarder import HostHeaderKind, HostHeaderMode, RequestEvent, TargetConfig
|
|
29
|
+
from .protocol import (
|
|
30
|
+
RESERVED_PREFIXES,
|
|
31
|
+
RegisterRequest,
|
|
32
|
+
RegisterResponse,
|
|
33
|
+
build_tunnel_ws_url,
|
|
34
|
+
generate_uuid_prefix,
|
|
35
|
+
normalize_target_url,
|
|
36
|
+
validate_prefix,
|
|
37
|
+
validate_prefix_format,
|
|
38
|
+
)
|
|
39
|
+
from .yamux import (
|
|
40
|
+
DEFAULT_CREDIT,
|
|
41
|
+
FrameFlags,
|
|
42
|
+
FrameHeader,
|
|
43
|
+
FrameType,
|
|
44
|
+
GoAwayCode,
|
|
45
|
+
Mode,
|
|
46
|
+
StreamState,
|
|
47
|
+
WsByteTransport,
|
|
48
|
+
YamuxSession,
|
|
49
|
+
YamuxStream,
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
__all__ = [
|
|
53
|
+
"__version__",
|
|
54
|
+
# High-level async & sync APIs
|
|
55
|
+
"connect",
|
|
56
|
+
"forward",
|
|
57
|
+
"http",
|
|
58
|
+
"open",
|
|
59
|
+
"Tunnel",
|
|
60
|
+
"SyncTunnel",
|
|
61
|
+
# Config helpers
|
|
62
|
+
"Config",
|
|
63
|
+
"config_path",
|
|
64
|
+
"load_config",
|
|
65
|
+
"save_config",
|
|
66
|
+
"resolve_server_and_token",
|
|
67
|
+
# Protocol & forwarder types
|
|
68
|
+
"DEFAULT_BASE_DOMAIN",
|
|
69
|
+
"DEFAULT_PANEL_HOST",
|
|
70
|
+
"DEFAULT_PUBLIC_SCHEME",
|
|
71
|
+
"DEFAULT_SERVER_URL",
|
|
72
|
+
"TUNNEL_WS_PATH",
|
|
73
|
+
"RESERVED_PREFIXES",
|
|
74
|
+
"RegisterRequest",
|
|
75
|
+
"RegisterResponse",
|
|
76
|
+
"RequestEvent",
|
|
77
|
+
"TargetConfig",
|
|
78
|
+
"HostHeaderKind",
|
|
79
|
+
"HostHeaderMode",
|
|
80
|
+
"build_tunnel_ws_url",
|
|
81
|
+
"normalize_target_url",
|
|
82
|
+
"generate_uuid_prefix",
|
|
83
|
+
"validate_prefix",
|
|
84
|
+
"validate_prefix_format",
|
|
85
|
+
# Low-level Yamux exports
|
|
86
|
+
"DEFAULT_CREDIT",
|
|
87
|
+
"FrameFlags",
|
|
88
|
+
"FrameHeader",
|
|
89
|
+
"FrameType",
|
|
90
|
+
"GoAwayCode",
|
|
91
|
+
"Mode",
|
|
92
|
+
"StreamState",
|
|
93
|
+
"WsByteTransport",
|
|
94
|
+
"YamuxSession",
|
|
95
|
+
"YamuxStream",
|
|
96
|
+
# Exceptions
|
|
97
|
+
"NerutonError",
|
|
98
|
+
"AuthError",
|
|
99
|
+
"PrefixError",
|
|
100
|
+
"RegistrationError",
|
|
101
|
+
"TunnelClosedError",
|
|
102
|
+
"TunnelConnectionError",
|
|
103
|
+
"YamuxError",
|
|
104
|
+
"YamuxProtocolError",
|
|
105
|
+
"YamuxClosedError",
|
|
106
|
+
"YamuxStreamResetError",
|
|
107
|
+
]
|
neruton/__main__.py
ADDED
neruton/_version.py
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"""Version and protocol constants for Neruton."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
__version__ = "0.2.0"
|
|
6
|
+
|
|
7
|
+
DEFAULT_BASE_DOMAIN = "moer.eu.org"
|
|
8
|
+
DEFAULT_PANEL_HOST = "neruton.moer.eu.org"
|
|
9
|
+
DEFAULT_SERVER_URL = "https://neruton.moer.eu.org"
|
|
10
|
+
DEFAULT_PUBLIC_SCHEME = "https"
|
|
11
|
+
TUNNEL_WS_PATH = "/_neruton/tunnel"
|
neruton/cli.py
ADDED
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
"""CLI entrypoint (`neruton` and `python -m neruton`) mirroring Rust `neruton-cli`."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import asyncio
|
|
7
|
+
import os
|
|
8
|
+
import signal
|
|
9
|
+
import sys
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
from typing import List, Optional, Sequence
|
|
12
|
+
|
|
13
|
+
from ._version import DEFAULT_SERVER_URL, __version__
|
|
14
|
+
from .client import Tunnel
|
|
15
|
+
from .config import config_path, load_config, resolve_server_and_token, save_config
|
|
16
|
+
from .errors import NerutonError
|
|
17
|
+
from .forwarder import RequestEvent
|
|
18
|
+
from .protocol import RegisterResponse, detect_client_os
|
|
19
|
+
from . import ui
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _add_common_tunnel_flags(parser: argparse.ArgumentParser) -> None:
|
|
23
|
+
parser.add_argument(
|
|
24
|
+
"--server",
|
|
25
|
+
default=None,
|
|
26
|
+
help="Neruton gateway server URL (env: NERUTON_SERVER, default: https://neruton.moer.eu.org)",
|
|
27
|
+
)
|
|
28
|
+
parser.add_argument(
|
|
29
|
+
"-s",
|
|
30
|
+
"--subdomain",
|
|
31
|
+
"--prefix",
|
|
32
|
+
dest="subdomain",
|
|
33
|
+
default=None,
|
|
34
|
+
help="Custom subdomain prefix (auto-generates a UUID v4 if omitted)",
|
|
35
|
+
)
|
|
36
|
+
parser.add_argument(
|
|
37
|
+
"--token",
|
|
38
|
+
default=None,
|
|
39
|
+
help="Optional Bearer auth token for protected servers (env: NERUTON_TOKEN)",
|
|
40
|
+
)
|
|
41
|
+
parser.add_argument(
|
|
42
|
+
"--no-tls-verify",
|
|
43
|
+
action="store_true",
|
|
44
|
+
default=False,
|
|
45
|
+
help="Disable TLS certificate verification for self-signed servers or local HTTPS targets",
|
|
46
|
+
)
|
|
47
|
+
parser.add_argument(
|
|
48
|
+
"--host-header",
|
|
49
|
+
default=None,
|
|
50
|
+
help="Control the Host header sent to the local service (`preserve`, `rewrite`, or custom value)",
|
|
51
|
+
)
|
|
52
|
+
parser.add_argument(
|
|
53
|
+
"--config",
|
|
54
|
+
type=Path,
|
|
55
|
+
default=None,
|
|
56
|
+
help="Path to config file (default: ~/.config/neruton/config.toml)",
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
61
|
+
parser = argparse.ArgumentParser(
|
|
62
|
+
prog="neruton",
|
|
63
|
+
description="Self-hosted intranet penetration & wildcard tunnel client inspired by cloudflared",
|
|
64
|
+
)
|
|
65
|
+
parser.add_argument(
|
|
66
|
+
"-V",
|
|
67
|
+
"--version",
|
|
68
|
+
action="version",
|
|
69
|
+
version=f"neruton {__version__}",
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
subparsers = parser.add_subparsers(dest="command", required=True)
|
|
73
|
+
|
|
74
|
+
# `neruton tunnel --url <url>`
|
|
75
|
+
p_tunnel = subparsers.add_parser(
|
|
76
|
+
"tunnel",
|
|
77
|
+
help="Start an HTTP/WebSocket tunnel forwarding to a local service",
|
|
78
|
+
)
|
|
79
|
+
p_tunnel.add_argument(
|
|
80
|
+
"-u",
|
|
81
|
+
"--url",
|
|
82
|
+
required=True,
|
|
83
|
+
help="Local target URL or port to expose (e.g. http://localhost:3000 or 3000)",
|
|
84
|
+
)
|
|
85
|
+
_add_common_tunnel_flags(p_tunnel)
|
|
86
|
+
|
|
87
|
+
# `neruton http <target>`
|
|
88
|
+
p_http = subparsers.add_parser(
|
|
89
|
+
"http",
|
|
90
|
+
help="Shorthand for `neruton tunnel --url <PORT_OR_URL>` (e.g. `neruton http 3000`)",
|
|
91
|
+
)
|
|
92
|
+
p_http.add_argument(
|
|
93
|
+
"target",
|
|
94
|
+
help="Local port or URL (e.g. `3000`, `localhost:8080`, or `http://127.0.0.1:3000`)",
|
|
95
|
+
)
|
|
96
|
+
_add_common_tunnel_flags(p_http)
|
|
97
|
+
|
|
98
|
+
# `neruton config <set|show|path>`
|
|
99
|
+
p_config = subparsers.add_parser(
|
|
100
|
+
"config",
|
|
101
|
+
help="Manage persistent CLI configuration (~/.config/neruton/config.toml)",
|
|
102
|
+
)
|
|
103
|
+
cfg_sub = p_config.add_subparsers(dest="config_action", required=True)
|
|
104
|
+
|
|
105
|
+
p_cfg_set = cfg_sub.add_parser(
|
|
106
|
+
"set",
|
|
107
|
+
help="Save default server URL and/or auth token to config.toml",
|
|
108
|
+
)
|
|
109
|
+
p_cfg_set.add_argument("--server", default=None, help="Default server URL (empty to unset)")
|
|
110
|
+
p_cfg_set.add_argument("--token", default=None, help="Default auth token (empty to unset)")
|
|
111
|
+
p_cfg_set.add_argument("--config", type=Path, default=None, help="Path to config file")
|
|
112
|
+
|
|
113
|
+
p_cfg_show = cfg_sub.add_parser(
|
|
114
|
+
"show",
|
|
115
|
+
help="Show the current resolved configuration",
|
|
116
|
+
)
|
|
117
|
+
p_cfg_show.add_argument("--config", type=Path, default=None, help="Path to config file")
|
|
118
|
+
|
|
119
|
+
p_cfg_path = cfg_sub.add_parser(
|
|
120
|
+
"path",
|
|
121
|
+
help="Print the resolved path to config.toml",
|
|
122
|
+
)
|
|
123
|
+
p_cfg_path.add_argument("--config", type=Path, default=None, help="Path to config file")
|
|
124
|
+
|
|
125
|
+
# `neruton version`
|
|
126
|
+
subparsers.add_parser("version", help="Print version and build metadata")
|
|
127
|
+
|
|
128
|
+
return parser
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
async def _run_tunnel_cli(args: argparse.Namespace, target_url: str) -> int:
|
|
132
|
+
server, token = resolve_server_and_token(
|
|
133
|
+
server=args.server,
|
|
134
|
+
token=args.token,
|
|
135
|
+
config_file=args.config,
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
def _on_connect(reg: RegisterResponse) -> None:
|
|
139
|
+
ui.print_banner(reg, tunnel.target_url)
|
|
140
|
+
|
|
141
|
+
def _on_reconnect(_attempt: int, backoff: float, reason: str) -> None:
|
|
142
|
+
ui.log_warn(f"Disconnected ({reason}). Reconnecting in {int(backoff)}s...")
|
|
143
|
+
|
|
144
|
+
def _on_request(ev: RequestEvent) -> None:
|
|
145
|
+
ui.log_request(ev.method, ev.path, ev.status, ev.duration_ms)
|
|
146
|
+
|
|
147
|
+
tunnel = Tunnel(
|
|
148
|
+
target_url=target_url,
|
|
149
|
+
server=server,
|
|
150
|
+
token=token,
|
|
151
|
+
subdomain=args.subdomain,
|
|
152
|
+
no_tls_verify=args.no_tls_verify,
|
|
153
|
+
host_header=args.host_header,
|
|
154
|
+
reconnect=True,
|
|
155
|
+
retry_initial=True,
|
|
156
|
+
on_connect=_on_connect,
|
|
157
|
+
on_reconnect=_on_reconnect,
|
|
158
|
+
on_request=_on_request,
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
ui.log_info(f"Connecting to {tunnel.ws_url} (target: {tunnel.target_url})...")
|
|
162
|
+
|
|
163
|
+
loop = asyncio.get_running_loop()
|
|
164
|
+
stop_triggered = False
|
|
165
|
+
|
|
166
|
+
def _request_stop() -> None:
|
|
167
|
+
nonlocal stop_triggered
|
|
168
|
+
if not stop_triggered:
|
|
169
|
+
stop_triggered = True
|
|
170
|
+
loop.create_task(tunnel.close())
|
|
171
|
+
|
|
172
|
+
for sig in (signal.SIGINT, signal.SIGTERM):
|
|
173
|
+
try:
|
|
174
|
+
loop.add_signal_handler(sig, _request_stop)
|
|
175
|
+
except (NotImplementedError, RuntimeError):
|
|
176
|
+
pass
|
|
177
|
+
|
|
178
|
+
try:
|
|
179
|
+
await tunnel.start()
|
|
180
|
+
if not tunnel.closed:
|
|
181
|
+
await tunnel.wait()
|
|
182
|
+
if stop_triggered:
|
|
183
|
+
ui.log_info("Tunnel shut down cleanly.")
|
|
184
|
+
return 0
|
|
185
|
+
except KeyboardInterrupt:
|
|
186
|
+
await tunnel.close()
|
|
187
|
+
ui.log_info("Tunnel shut down cleanly.")
|
|
188
|
+
return 0
|
|
189
|
+
finally:
|
|
190
|
+
for sig in (signal.SIGINT, signal.SIGTERM):
|
|
191
|
+
try:
|
|
192
|
+
loop.remove_signal_handler(sig)
|
|
193
|
+
except (NotImplementedError, RuntimeError):
|
|
194
|
+
pass
|
|
195
|
+
if not tunnel.closed:
|
|
196
|
+
await tunnel.close()
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
def _cmd_config(args: argparse.Namespace) -> int:
|
|
200
|
+
action = args.config_action
|
|
201
|
+
if action == "set":
|
|
202
|
+
if args.server is None and args.token is None:
|
|
203
|
+
raise NerutonError("provide at least one of --server or --token")
|
|
204
|
+
cfg = load_config(args.config)
|
|
205
|
+
if args.server is not None:
|
|
206
|
+
cfg.server = args.server if args.server.strip() else None
|
|
207
|
+
if args.token is not None:
|
|
208
|
+
cfg.token = args.token if args.token.strip() else None
|
|
209
|
+
saved = save_config(cfg, args.config)
|
|
210
|
+
print(f"Saved configuration to {saved}")
|
|
211
|
+
return 0
|
|
212
|
+
|
|
213
|
+
if action == "show":
|
|
214
|
+
path = config_path(args.config)
|
|
215
|
+
cfg = load_config(args.config)
|
|
216
|
+
env_server = os.environ.get("NERUTON_SERVER", "").strip()
|
|
217
|
+
effective_server = env_server or cfg.server or DEFAULT_SERVER_URL
|
|
218
|
+
token_status = "<configured>" if cfg.token else "<not set>"
|
|
219
|
+
print(f"Config file: {path}")
|
|
220
|
+
print(f"Effective server: {effective_server}")
|
|
221
|
+
print(f"Saved server: {cfg.server or '<default>'}")
|
|
222
|
+
print(f"Saved token: {token_status}")
|
|
223
|
+
return 0
|
|
224
|
+
|
|
225
|
+
if action == "path":
|
|
226
|
+
print(config_path(args.config))
|
|
227
|
+
return 0
|
|
228
|
+
|
|
229
|
+
raise NerutonError(f"unknown config action: {action}")
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
def main(argv: Optional[Sequence[str]] = None) -> int:
|
|
233
|
+
"""Entry point for the `neruton` CLI script."""
|
|
234
|
+
parser = build_parser()
|
|
235
|
+
args = parser.parse_args(list(argv) if argv is not None else None)
|
|
236
|
+
|
|
237
|
+
try:
|
|
238
|
+
if args.command == "version":
|
|
239
|
+
print(f"neruton {__version__} ({detect_client_os()})")
|
|
240
|
+
return 0
|
|
241
|
+
if args.command == "config":
|
|
242
|
+
return _cmd_config(args)
|
|
243
|
+
if args.command == "tunnel":
|
|
244
|
+
return asyncio.run(_run_tunnel_cli(args, args.url))
|
|
245
|
+
if args.command == "http":
|
|
246
|
+
return asyncio.run(_run_tunnel_cli(args, args.target))
|
|
247
|
+
parser.error(f"unknown command: {args.command}")
|
|
248
|
+
return 2
|
|
249
|
+
except KeyboardInterrupt:
|
|
250
|
+
ui.log_info("Tunnel shut down cleanly.")
|
|
251
|
+
return 0
|
|
252
|
+
except Exception as exc:
|
|
253
|
+
ui.log_error(str(exc))
|
|
254
|
+
return 1
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
if __name__ == "__main__": # pragma: no cover
|
|
258
|
+
sys.exit(main())
|