Compare commits

...

547 Commits

Author SHA1 Message Date
ospab cf14a4243c chore: release v0.4.4 on master 2026-08-08 21:37:55 +03:00
ospab 66368c9d0f fix(gui): helper task checked only its name, not the exe it points at
A Scheduled Task stores an absolute path. Checking that a task named
"OSTP TUN Helper" exists said nothing about whether its <Command> still points
at the helper we are about to run, and the paths do drift: a dev build
registers target\debug\ostp-tun-helper.exe, an installer registers Program
Files, and moving or reinstalling the app leaves the old path behind.

That failed silently in the worst way. schtasks /Run reports success for
merely ACCEPTING the launch request — a task whose exe no longer exists fails
afterwards, out of band, with nothing returned to us. So launch_as_admin
returned Ok, and the caller then sat in its 60-second connect loop before
reporting "Timeout connecting to helper." On every connect, permanently, with
no way out except deleting the task by hand.

The check now reads the registered <Command> back and compares it to the exe,
re-registering through the existing /F overwrite when they differ: one consent
prompt, once, instead of a permanent silent breakage.

The path is read via /Query /XML rather than /FO LIST /V because the list
format's field labels are localized — "Task To Run" is "Задача для запуска" on
a Russian Windows — while XML tag names are not. schtasks emits UTF-16LE with
a BOM there, which is decoded explicitly, with UTF-8 tolerated as a fallback.
Both paths are canonicalized before comparison so casing, `..` and 8.3 short
names do not read as a mismatch; a path that cannot be canonicalized no longer
exists, which is itself grounds to re-register.
2026-08-07 22:24:40 +03:00
ospab bc61b47817 fix(gui): the UAC-once change did not work and flashed consoles
Reported from v0.4.3: a consent prompt for schtasks, then 10-20 console windows
opening and closing, then STILL a prompt for the helper. Two defects of mine,
both in the change that was supposed to remove the repeated prompt.

Registration never succeeded. ShellExecuteW returns as soon as the elevated
process is LAUNCHED, not when it finishes, so the generated XML was deleted
while schtasks was still starting — it then had nothing to read. The task was
never created, so the code fell through to the direct elevated launch and the
user paid for two prompts to get what one used to do. Registration now goes
through PowerShell's Start-Process -Verb RunAs -Wait -PassThru, which actually
waits, lets the XML be deleted safely afterwards, and surfaces the real exit
code instead of it being inferred by polling. Arguments are passed as an array,
so the task name and XML path never touch a command line; verified the
generated script parses with a path containing an apostrophe, an ampersand and
spaces at once.

The flashing was every schtasks/reg/tasklist invocation: the GUI is a
windowed-subsystem binary, so each console child pops a window, and the
registration polled up to twenty times in a row. All of them now go through a
wrapper that sets CREATE_NO_WINDOW. This also silences flashes that predate
this feature — `tasklist` runs whenever the exclusions screen opens, and `reg`
on autostart changes.

Polling is gone with it: the exit code is authoritative, and the task's
presence is confirmed once rather than up to twenty times.

Also drops shell_execute_elevated, which this change had left with no callers.
2026-08-07 20:51:14 +03:00
ospab 5a33ed69c4 chore: release v0.4.3 on master 2026-08-07 17:39:19 +03:00
ospab c5e703c144 chore: release v0.4.3-beta.4 on beta 2026-08-07 17:35:15 +03:00
ospab 05f25155bd feat(gui): one UAC prompt per machine instead of one per connect
Enabling TUN raised a consent dialog every single time, because the GUI
elevated the helper with ShellExecuteW("runas") on each connect.

Registers a Scheduled Task with RunLevel=HighestAvailable the first time TUN
is used — that registration is the one prompt — and triggers the task on every
later connect. Running a task is not an elevation request, so Windows shows no
dialog for it. If the task is missing or cannot be triggered, the code falls
back to the original direct elevated launch, so this can only improve on the
old behaviour, never break it.

A task stores a FIXED command line, so the per-launch port and token cannot be
arguments. The helper gained --args-file and the GUI writes them to
%LOCALAPPDATA%\OSTP\helper-args.json immediately before triggering; the helper
deletes it after reading. That path keeps the token inside the trust boundary
it already had — the helper runs elevated but as the same user, and no other
user can read it, which a shared location would not guarantee.

Registered from an XML definition rather than /TR: the exe and args paths would
otherwise need quoting inside an already-quoted /TR value, escaped again
through ShellExecuteW, which breaks as soon as either contains a space — and
both do by default (Program Files, usernames with spaces). Validated that the
generated XML parses with a path containing both a space and an ampersand.
XML also lets DisallowStartIfOnBatteries=false and ExecutionTimeLimit=PT0S be
stated explicitly, without which a laptop would refuse to start the tunnel on
battery and Windows would kill it after three days.

Deliberately NOT a Windows service, contrary to what I suggested earlier. The
helper is one-shot by design: it force-exits after teardown because WinTun's
blocking receive otherwise keeps the adapter and its default route alive and
breaks the next connect. A persistent LocalSystem service would mean
restructuring that lifecycle for the same end-user result. The task also runs
with the user's own token rather than LocalSystem, which is less privilege for
the same outcome. A service is still the better answer if the tunnel should
come up before login — that is the one thing this cannot do.
2026-08-07 17:19:36 +03:00
ospab e6e0a7b28c chore: release v0.4.3-beta.3 on beta 2026-08-05 00:11:33 +03:00
ospab 8f0ffd08c0 feat(gui): TUN mode and autostart on Linux
Brings the Linux GUI up to parity with Windows. Four things blocked it, each
independently sufficient:

  - build.rs keyed the Windows-manifest step off cfg(windows), which in a build
    script describes the HOST. Cross-compiling the helper from Windows to Linux
    therefore took that branch and died with "Can only compile resource file
    when target_env is gnu or msvc". Now keyed off CARGO_CFG_TARGET_OS, with
    the cfg(windows) gate kept as a second check because winres is a host-
    resolved build-dependency and simply does not exist on a Linux host.
  - launch_as_admin was bail!("Windows only.") outside Windows. Implemented for
    Linux via pkexec, polkit's front-end, which raises a graphical auth prompt;
    sudo is unusable from a GUI with no terminal. Missing pkexec now names the
    package to install instead of failing opaquely.
  - The release workflow only built ostp-tun-helper in the Windows job, so the
    Linux package shipped without it. It is now built and placed next to the
    GUI binary, where find_helper_exe looks first.
  - set_autostart/get_autostart were no-ops off Windows. Implemented via XDG
    autostart (~/.config/autostart/ostp.desktop, honouring XDG_CONFIG_HOME),
    the direct equivalent of the HKCU Run key.

The helper's own code needed no changes — it already compiled for Linux once
the build script stopped rejecting it. list_running_processes already had a
Linux branch.

The token file is created 0600 on Linux: /tmp is world-readable there, unlike
the Windows temp dir, and that token authorises control of the privileged
helper.

Not verified on a live Linux desktop from here — the Tauri backend cannot be
compiled for Linux on a Windows host (GTK dev libraries), so the cfg(linux)
paths are reviewed rather than built. CI compiles them.
2026-08-04 01:08:33 +03:00
ospab 8a1426ecf5 fix(gui): honest TUN error on Linux, and a window that can be resized
Reported from the Linux GUI: it asked for "helper.exe" on Linux, and the
window was tiny.

The helper name had ".exe" hardcoded in every lookup path, so on Linux the
search could only ever fail. Fixing the name alone would have been misleading
though, because TUN mode does not work on Linux for a deeper reason:
launch_as_admin is `bail!("Windows only.")` outside Windows, and the release
workflow only builds ostp-tun-helper in the Windows GUI job. So the feature is
Windows-only, and the message now says exactly that and points at proxy mode,
instead of surfacing as a missing file named after a Windows executable —
which reads like a packaging mistake rather than an unimplemented feature. The
name is still resolved per-platform for when Linux elevation does land.

The window was 360x680 and `resizable: false`. Windows scales that by DPI, but
WebKitGTK on a HiDPI Linux display renders it close to raw pixels, giving a
postage-stamp window the user then could not resize. It is now resizable with
a sensible minimum, and .app-root caps and centres the column so a wider
window keeps the intended narrow layout instead of stretching the controls.
2026-08-04 00:56:28 +03:00
ospab e483af541f fix: post-suspend reconnect no longer strands the machine without internet
Two separate problems reported after waking a laptop: the app sits on
"connecting" forever, and there is NO working internet at all — not just no
tunnel. Plus typing in the GUI's exclusion fields lagged by seconds.

1. Resume reconnect retried forever (a regression I introduced when making the
   resume reconnect retry instead of firing once). handle_keepalive(force=true)
   deliberately skips the hard-timeout branch, and that branch is the one that
   releases the SystemProxyGuard. So a resume campaign that never succeeded
   also never gave up, and the system proxy stayed pointed at our local
   listener indefinitely — which kills all browser traffic, tunnel or not, and
   explains "no internet even from my ISP".

   Now bounded: after 45s of failed resume reconnects, hand back to the
   ordinary stall path, which restores the proxy (or, with kill switch on,
   keeps blocking deliberately). Measured on the wall clock, because Instant
   does not advance across suspend on Windows — QPC stops — so a monotonic
   deadline cannot bound anything that starts at wake. That same property is
   why the pre-existing 25s/180s stall checks never fired here either.

2. GUI froze while typing. Every debounced save (400ms, so it fires during
   natural pauses in typing) called set_autostart — a Windows registry write —
   even when the checkbox had not changed, and, while connected, wrote the
   config and ran reload_tunnel, tearing down and rebuilding the tunnel. Worst
   in the exclusion fields, which is exactly where it was reported.

   Autostart now applies only on change; the tunnel hot-reload only when a
   setting the tunnel actually reads has changed, on a 1.5s debounce so it
   lands after editing rather than between keystrokes. The cheap local save
   still runs on every keystroke.
2026-08-04 00:34:12 +03:00
ospab df1a14d15c chore: release v0.4.3-beta.2 on beta 2026-08-03 18:50:37 +03:00
ospab d915efc715 refactor(relay)!: forward transparently instead of re-authenticating clients
The relay authenticated clients itself, with an HMAC handshake and a
background job pulling the access-key list from the target server's management
API. That never worked with a real client and would not have been worth having
if it had.

It could not work: no OSTP client produces those credentials. The TCP path
required an HTTP request (`GET /stream` with `Authorization: Bearer`) and the
UDP path a `timestamp || HMAC` preamble, while the client sends junk frames
followed by length-prefixed frames, and an obfuscated Noise handshake. There
is no HMAC code in ostp-client at all, so every connection was rejected: TCP
answered 404, UDP dropped the datagram.

It was also weak where it applied. The HMAC covered only an 8-byte timestamp,
so a captured signature was a bearer token replayable from any address for the
clock-skew window, with no anti-replay set. And the HTTP handshake put a
literal `GET /stream` on the wire — a greppable signature in a protocol whose
premise is that nothing is recognisable.

Authentication now stays where it is cryptographically meaningful. The target
server already authenticates every session end-to-end via Noise with a PSK
derived from the access key and silently drops what fails; the relay adds
nothing by re-checking, and holding a copy of the key list on a forwarding box
is a liability. The relay makes no security decisions and says so.

What it does need is protection from being a resource sink, so this adds a
session cap, a connection cap, and a token-bucket admission limit on NEW
sessions only. It forwards to one fixed upstream and replies only to the
sender, so it is not a reflector: amplification is one.

Fixes a bug the new end-to-end test caught: upstream sockets were bound as
"[::]:0" and connected to a possibly-IPv4 upstream, which fails wherever
IPV6_V6ONLY defaults on — that is every deployment with an IPv4 target server.
The bind family now follows the resolved upstream.

upstream_api_url/token/sync_interval_secs are accepted and ignored so existing
relay configs keep parsing; the wizard and template no longer emit them, and
`ostp check` flags them as removable.
2026-08-03 18:49:57 +03:00
ospab b673219894 fix(server)!: never fall back to a direct connection when a rule says proxy
Reported: with an upstream proxy configured, Google flags the session because
the server's address and the proxy's exit address differ, and YouTube
geolocates to the server rather than the proxy exit. Both follow from the same
defect in three places — an outbound rule that says Proxy was being satisfied
by connecting DIRECTLY whenever the proxy could not be used:

  - router.rs, UdpSessionRouter::send_to: action Proxy with no UDP proxy
    established fell through to the direct socket.
  - outbound.rs, connect_target: any protocol string other than exactly
    "socks5"/"http" hit a `_ =>` arm that connected directly. A typo, or just
    "SOCKS5", silently un-proxied ALL TCP.
  - outbound.rs, connect_udp_target: non-socks5 upstream returned
    UdpProxySocket::Direct, with a comment noting HTTP cannot carry UDP —
    correct in itself, but the chosen fallback leaks.

The visible symptom is precisely what these produce: TCP goes through the
proxy while UDP (QUIC — which is what YouTube uses) leaves from the server,
so one session presents two exit addresses.

All three now fail closed. A rule asking for the proxy is never honoured by
sending in the clear: a dropped flow is visible and debuggable, a
deanonymising leak is neither. Protocol matching is also case-insensitive
now, and the SOCKS5 UDP ASSOCIATE failure warns unconditionally rather than
only under `debug`, which is why this could go unnoticed.

Behaviour change: with an HTTP upstream, or a broken SOCKS5 UDP ASSOCIATE,
UDP now fails instead of leaking. Where that is genuinely wanted, it must be
stated in the config as an explicit udp rule with action "direct".

Also carries the relay key-sync diagnostics: a 404 there means the URL is
missing the panel's secret webpath (the API is nested under it, not at /api),
which the previous bare "HTTP 404" gave no way to work out.
2026-08-03 17:57:42 +03:00
ospab a1c146aff3 fix(relay): explain the 404 — the API lives under the panel's secret webpath
A relay configured with upstream_api_url = "http://HOST:9090" fails every key
sync with a bare "API returned HTTP 404", which reads like the server is down
or the token is wrong. Neither is true: the management API is nested under the
target server's api.webpath (create_api_router mounts it at
"/{webpath}/api"), because that secret segment is what keeps the panel from
being discoverable by scanners. A bare host:port therefore resolves to a route
that does not exist and the token is never even looked at.

Nothing said so — the config template, the wizard prompt and the shipped
example all suggested exactly the host:port form that cannot work.

  - sync_keys now reports the full URL and, for 404 specifically, states that
    the webpath must be included and what the URL should look like. 401 is
    called out separately as a token mismatch, since the two are otherwise
    indistinguishable from the log.
  - The relay config template, the shipped example and the wizard prompt now
    show the path-bearing form, and the wizard warns when the URL entered has
    no path segment rather than letting it fail later.

Docs under docs/ and the wiki are being rewritten concurrently and are left
alone here.
2026-07-31 20:00:29 +03:00
ospab 365b4ccbf5 chore: release v0.4.3-beta.1 on beta 2026-07-31 19:47:26 +03:00
ospab 4a3fb8b944 fix(client): stop mobile connects from stalling for minutes on dead IPv6
Connecting over a mobile network took ~90s, and under worse conditions did
not complete at all. Three compounding causes, all in the address loop of
perform_handshake_with_id, which walks candidates strictly in order and burns
each one's full retry budget before touching the next:

  - IPv6 was tried FIRST. Carriers routinely hand out IPv6 with no working
    route and blackhole it rather than rejecting, so each AAAA record cost the
    entire 4x1.2s budget with nothing to show; with several of them the
    working IPv4 address was not reached for tens of seconds. The identical
    ordering bug was already fixed on the server's outbound path and in the
    UoT connect - the client's handshake was simply missed.

  - The NAT64 prefix discovery lookup had no timeout. It only ever runs on
    networks that are already misbehaving, exactly where a resolver can hang
    for tens of seconds. Now bounded at 2s, falling back to the well-known
    RFC 6052 prefix, which beats waiting.

  - NAT64 was retried per failing IPv4 address, each time re-running that
    lookup plus another four handshake attempts - for a path that either works
    for the whole network or for none of it. Now attempted once.

Ordering alone is the dominant fix; the other two bound the tail.
2026-07-31 19:26:30 +03:00
ospab f789167a22 feat(congestion): actually pace sends instead of releasing whole windows
pacing_rate had been computed on every ACK since the controller was written
and never read by anything: admission was decided purely by cwnd. But cwnd
bounds how much may be UNACKNOWLEDGED, not how fast it reaches the wire, so a
full window went out back-to-back. On a bottleneck with a deep buffer that
burst is absorbed rather than dropped, and it lands as standing queue — the
mechanism behind the multi-second RTT this protocol has been showing on
mobile. It is also why BBR could not simply be dropped in: BBR's whole model
is "send at the estimated bottleneck rate", which is meaningless without a
pacer underneath it.

Adds a token bucket to CongestionController, charged in on_send so every byte
that reaches the wire pays exactly once — retransmits included, since those
are precisely what must not bypass the limit and pile into a full queue.

Burst allowance is 10ms-at-rate rather than one packet. Pacing intervals here
are fractions of a millisecond, so strict per-packet release would need a
sub-millisecond timer per packet; sizing the burst to the loop's existing
~10ms wakeup lets the configured rate still be saturated. There is a floor of
4 MTU so a cold or collapsed rate estimate can never wedge sending entirely.

Wired into both directions. The client gates its proxy-event branch on it,
and the server's per-session backpressure snapshot reports zero headroom when
the bucket is empty. The download path matters most here — that is the one
carrying video — and it was also still clamped to the old 16384-packet
ceiling (~20 MB outstanding), now aligned with MAX_CWND_PACKETS.

Tested that the bucket denies once drained and refills over time; the second
is what keeps a stalled bucket from wedging sending permanently.
2026-07-31 19:18:42 +03:00
ospab 108bab6a90 fix(client): retry resume reconnects; add a way to hash the panel password
Two unrelated user-reported blockers.

Resume on desktop. The suspend/resume detector fired a single reconnect
attempt roughly half a second after waking — which is exactly when the NIC
has not reassociated yet, so it almost always failed. Failure then fell back
to the ordinary 25s stall heuristic, which keys off a monotonic clock that
does not advance while the machine is asleep, so it could take another 25s of
real uptime to fire, or never fire. The forced reconnect is now sticky:
retried every 3s until a session is actually established, verified via
last_valid_recv rather than by "an attempt was made".

Panel password. `api.password_hash` wants a hash, and nothing in the CLI
could produce one: `ostp init server` emits password_hash: "" and the only
generator was inline in the Server+Panel wizard branch, which is Unix-only —
so on a plain server there was no supported path to working API auth at all.
Adds `ostp hash-password [PASSWORD]`, prompting when the argument is omitted
so the password stays out of shell history. Output verified to match both
handle_login's comparison and a reference SHA-256.
2026-07-31 19:02:03 +03:00
ospab f7e9215331 fix(congestion): stop the bufferbloat spiral behind multi-second RTT stalls
Reported symptom: on mobile the reported RTT jumps to 15-20s (worst case
~150s), video stops loading, and it takes ~5 minutes to recover — or doesn't,
until the user reconnects.

Nothing on the network takes 150 seconds. That delay was our own queue. The
controller only ever treated LOSS as congestion, and mobile carrier buffers
are deep enough to absorb a burst rather than drop it, so the loss signal
never arrived and three things compounded:

  - slow start grew cwnd unbounded, with the client permitting up to 16384
    packets in flight (~20 MB — minutes of queue on a mobile uplink);
  - the resulting standing queue inflated RTT samples, which raised SRTT,
    which raised the adaptive RTO, so retransmits piled into the same queue;
  - backoff multiplied that already-inflated RTO by up to 64x. With RTO_MAX
    at 16s a frame could sit unretransmitted for ~17 minutes.

Reconnecting "fixed" it only because a fresh session resets cwnd to 32.

Three bounds, matching how delay-based controllers handle this:
  - Treat sustained RTT inflation as congestion: leave slow start at 2x the
    observed path floor, actively halve cwnd at 4x. This is the part that
    works where loss never comes.
  - Hard cwnd ceiling of 1024 packets (~1.2 MB), well above any real BDP here
    but far below a queue measured in seconds. The client's in-flight gate is
    lowered to match.
  - Cap the post-backoff retransmit timer at 8s.

Tested for the no-loss inflation case and the ceiling. Both encode the bug
directly, since neither is observable from loss-driven tests.

Not addressed here: pacing is still computed and unused, so sending remains
bursty. That is the next lever and wants real-link validation.
2026-07-31 18:47:14 +03:00
ospab ebfc751471 fix(android): treat a blank signing secret as absent, not as the password
The Android jobs failed with "Get Key failed: Given final block not properly
padded" once the store password was corrected - the keystore opened, but the
KEY could not be decrypted.

Cause: GitHub Actions substitutes an empty string, not an unset variable, for
a secret that does not exist. ANDROID_KEY_PASSWORD is deliberately not set (our
keystore is PKCS12, where the key password cannot differ from the store
password), so OSTP_KEY_PASSWORD arrived as "". Kotlin's elvis operator only
falls back on null, so `getenv(...) ?: storePassword` kept the empty string and
used it as the literal key password.

signingSetting() now maps blank to null, so the documented fallback actually
happens. Applies to every signing field, not just the key password - the same
trap would have hit any of them.
2026-07-30 20:56:33 +03:00
ospab 3cda1a9bd4 ci: cache the GUI/Android Rust builds, and diagnose signing failures early
Build-time work, plus a fix for the v0.4.2 Android signing failure.

Caching. The three Tauri GUI jobs were the slowest in the matrix (up to
9m17s vs 2-5m for the plain release targets) for two compounding reasons:

  - No restore-keys. The cache key ends in hashFiles('**/Cargo.lock'), and
    cutting a release rewrites every Cargo.lock (version bump), so the exact
    key missed on every single release. With no prefix fallback the cache
    restored nothing at all and each release rebuilt the full dependency
    graph from scratch. The plain release targets had restore-keys all along,
    which is exactly why they were multiples faster.
  - Wrong path. ostp-gui/src-tauri is excluded from the workspace, so its
    build output lands in ostp-gui/src-tauri/target/, not the cached target/.
    The bulk of each GUI job's Rust work was therefore never cached even when
    the key did hit - visible in the cache sizes (25-30 MiB for the macOS and
    Linux GUI entries, against 150-220 MiB for real target/ caches).

The Android jobs had no Rust cache whatsoever, and rebuilt cargo-ndk from
source every run; both now cache, the latter mirroring how `cross` is
already handled.

Signing diagnostics. v0.4.2's Android jobs failed after four minutes of
Gradle with "keystore password was incorrect". The keystore is now decoded
with stray CR/LF stripped (a single trailing \r corrupts the decode) and
validated with keytool up front, so a bad password or a missing alias fails
in seconds with a message that says which. The printed size and SHA-256
disclose nothing secret and let the operator tell a mangled transfer apart
from a genuinely wrong password.
2026-07-30 20:43:55 +03:00
ospab 77a45d7642 chore: release v0.4.2 on master 2026-07-30 20:31:26 +03:00
ospab 6abae68f35 fix(android): default the signing key password to the store password
Our upload keystore is PKCS12 (verified from its DER header, 0x30 0x82 —
JKS would start 0xFEEDFEED). That format has nowhere to store a key password
distinct from the store password, and keytool enforces the two being equal,
so requiring a separate OSTP_KEY_PASSWORD meant configuring a secret whose
only possible correct value was a copy of another one.

Falls back to the store password when unset; an explicit value still takes
precedence for the legacy JKS format, where the two can genuinely differ.
2026-07-30 20:28:07 +03:00
ospab cb57347d51 chore: never let an Android signing key be committed
ostp-upload.jks was sitting untracked but NOT ignored in the repo root, so a
single `git add -A` would have swept the private upload key into a public
repository. That key is unrecoverable-by-design: Android refuses to update an
app across a signing-key change, so leaking it (or losing it) means every
existing install is stranded and can only be fixed by a manual uninstall.

Ignores *.jks, *.keystore and key.properties at the root. The ostp-lab/ line
in this diff is not mine - it was already in the working tree and is carried
along because it shares the file.
2026-07-30 20:25:28 +03:00
ospab 32c36afc3b fix(android): sign releases with a stable upload key, not the debug keystore
Published APKs could never be updated over - users hit "App not installed" or
"unable to parse the package" and had to uninstall first. The cause was not the
version code (verified: local.properties carries flutter.versionCode=23 and
gha.ps1 bumps pubspec's build number every release, so it increments correctly).
It was the signing key: app/build.gradle.kts still had the stock Flutter
template TODO and pointed the release build type at signingConfigs["debug"].
Android identifies an app by applicationId + signing key and refuses to update
across a key change, and the debug keystore is generated per machine - on
ephemeral CI runners that means every single published build was signed with a
different random key.

Release builds now take their key from android/key.properties or the
OSTP_KEYSTORE_* environment variables, falling back to debug (with a loud
warning) only so local `flutter build apk --release` keeps working. CI
materialises the keystore from repository secrets, refuses to build at all if
the secret is absent, and re-verifies the finished APK is not debug-signed
rather than ever shipping an un-updatable build again.

NOTE: existing installs are signed with a now-unreproducible random key, so
users must uninstall once more for THIS release. Every update after it works.
2026-07-30 19:54:31 +03:00
ospab a8aba8f4b8 feat(gui): green aura on the connected state
The palette already declared --c-green/--c-green-glow/--c-green-dim with the
comment "Green only for connected state", but the values were near-white
(#e8e8e8) and nothing referenced them - so a successful connection looked
identical to every other state. Gave the tokens real green values (per theme,
deeper on light so it stays legible), added a --c-green-rgb triple so the
translucent layers can be expressed from one source, and wired them into the
three things that signal "connected": the power button's border and glow, the
orbit rings around it, and the brand status dot.
2026-07-30 19:54:11 +03:00
ospab 2ede607027 chore: release v0.4.2-beta.5 on beta 2026-07-30 19:02:48 +03:00
ospab 0c69617725 fix(cli): trait-qualify Sha256::digest so it builds with or without the import
Follow-up to the v0.4.2-beta.3 CI break. Importing sha2::Digest fixed the
build there but the import reads as unused locally (different dependency
resolution), leaving a permanent warning in every build. Calling through
<sha2::Sha256 as sha2::Digest>::digest resolves the trait method
explicitly, so it compiles in both environments with no import and no
warning. Workspace now builds clean.
2026-07-30 14:41:16 +03:00
ospab 88e0634f09 fix: two independent causes of the tunnel freezing at 0 b/s
Both produce the same reported symptom - traffic stops dead, the session
itself looks fine, and only a manual reconnect recovers it.

1. protocol.rs: a retry could be charged to a frame that was never sent.
   The retransmit loop is budget-limited per tick, but it bumped `retries`
   and reset `last_sent` for every due frame regardless of whether the
   budget actually allowed a send. The budget is smallest exactly when loss
   is heaviest (it is derived from cwnd, which collapses under loss), so
   under real packet loss frames accumulated "phantom retries" they never
   received - measured at 40 retries charged for 8 frames actually sent in
   one tick. After max_retries+2 such rounds the zombie eviction dropped
   them as dead. That data was never delivered and never would be: the
   stream stalls permanently while pings keep flowing, so nothing upstream
   notices anything is wrong. Retries/timers are now only charged on an
   actual transmit, and the loop stops scanning once the budget is spent
   (sent_history is in send order, so this also keeps retransmit priority
   oldest-first). Covered by a new test that asserts retries charged ==
   datagrams emitted; verified it fails against the old code.

2. bridge.rs: the stall detector was reset by datagrams that never
   validated. `last_valid_recv` - "last VALID recv" - was assigned before
   decryption, so a datagram that failed to decrypt still refreshed it on
   its way to the error return. Anything landing on that port kept the
   client convinced the tunnel was healthy: frames from a session the
   server had already evicted, stale retransmits, or plain garbage from an
   off-path source that knows the ip:port. The 25s background reconnect in
   handle_keepalive therefore never fired. It also made the UI health
   indicator report a dead tunnel as fine, and gave any off-path sender a
   trivial way to pin a client in a dead session indefinitely. Now set only
   after the datagram authenticates and decrypts.
2026-07-30 00:36:17 +03:00
ospab 7473278cc2 fix(client): bound the UoT connect; green aura + self-updating ping on mobile
UoT took 20-30s (sometimes 1-2 min) to come up on mobile. The TCP connect
had no timeout, so it inherited the kernel's SYN retry budget. Callers
resolve every address for the server and deliberately try IPv6 first
(perform_handshake_with_id sorts is_ipv6 to the front); a mobile network
that advertises IPv6 without a working route blackholes the SYN instead of
rejecting it, so the client sat through that entire budget before reaching
the IPv4 address that would have connected immediately. UDP never showed
this because connect() on a UDP socket just sets the default peer and
returns.

Capped at 4s per address, so a blackholed candidate costs seconds and the
next one is tried. Left the IPv6-first ordering alone: it is what makes
IPv6-only and NAT64 networks work, and with the cap its worst case is now
bounded. (A further win would be remembering which family last succeeded
and trying that first, removing even those 4s — not done here.)

Also, per the earlier UI requests:
- The connected state drew its aura, ring, icon and status dot from the
  theme's `secondary`, which is #AAAAAA and reads as plain white, giving no
  confirmation the tunnel was actually up. Now green, reusing the green
  already used for a healthy ping so "green = good" stays consistent.
  Applied at the call sites rather than to the theme, since `secondary`
  also paints routing toggles, the download metric and settings switches.
- Ping now updates itself from the metrics stream that was already
  arriving, instead of needing the "Test Ping" button, and is rendered as a
  compact icon + value.
2026-07-29 20:06:07 +03:00
ospab 77e42b77f7 fix(protocol): recover from an unrecoverable gap instead of freezing forever
The freeze users hit every few minutes: traffic drops to 0 B/s, the RTT
readout sticks at its last value, and only a manual reconnect clears it.

Delivery is gated on expected_recv_nonce, so one missing frame holds back
every frame behind it. That is correct only while the sender can still
retransmit — but the sender drops a frame from sent_history once it passes
max_retries + 2 attempts (zombie eviction in handle_tick). Past that point
the frame no longer exists anywhere and both sides deadlock: the receiver
buffers indefinitely and NACKs a nonce nobody can resend.

The watchdog could not save it, which is why it froze rather than
reconnecting. Retransmits, ACKs and NACKs keep arriving throughout, so the
client's last_valid_recv keeps refreshing and its 25s stall detector never
fires. The frozen RTT has the same cause: Pong travels in a Data frame,
stuck behind the very gap it would have reported.

The machinery for this was half-built: last_recv_advance was declared,
initialised and written on every advance, and its doc comment describes
exactly this recovery — but nothing ever read it, and a warning elsewhere
already referred to "gap recovery" that did not exist.

So implement it. Once the sequence has been stuck longer than the sender's
retransmit budget could plausibly last (8x the live RTO, clamped to 2..10s
so fast links do not discard merely-late frames and slow ones still
unblock), skip to the lowest buffered nonce, drain, and mark an ACK
pending so the peer stops retransmitting into a void.

This runs on the inbound path, not on Tick, for two reasons: both tick
handlers discard DeliverApp actions (client bridge.rs and server
dispatcher.rs match only SendDatagram/Multiple), and inbound frames keep
flowing all through the stall, so the path is reliably reached.

Skipping the hole drops one frame's payload — one RelayMessage, a chunk of
a single stream. That is a real cost, paid only when the data was already
lost for good, against a tunnel that otherwise stays dead until the user
intervenes.

Both tests were confirmed to fail without the fix (0 frames released
instead of 2), so they pin the deadlock rather than just the happy path.
2026-07-29 19:56:09 +03:00
ospab e7a4f2b4a4 merge master: reconcile the two direct install.sh hotfixes
Both were emergency live-fixes to master (since install.sh/install.ps1 are
curl'd straight from that branch's raw URL, bypassing the normal release
promotion): the `ostp setup` subcommand fix and the alpha/beta self-update
mechanism fix. alpha already has equivalent content for both via its own
separate commits, so this is a pure reconciliation.
2026-07-21 18:42:00 +03:00
ospab 6bc646c8a5 fix(install): alpha/beta self-update actually finds a real release now
Direct hotfix to master (like the earlier `ostp setup` wizard fix) - users
curl install.sh live from this branch's raw URL for every self-update, so
this can't wait for the normal alpha->beta->master promotion.

Master's install.sh still had the pre-rename "pre-release" branch check
(the alpha->beta rename landed on alpha/beta after this file's last direct
hotfix, never reaching master) AND the deeper bug: ostp update -b alpha/-b
beta tried to download a GitHub Release literally tagged "alpha"/"beta".
No such tag has ever existed - gha.ps1 cuts a fresh VERSIONED tag every
release (v0.4.2-beta.4, v0.4.3-alpha.2, ...) - so -b beta fell through to
the stable-release path entirely unnoticed (silently "succeeding" with the
wrong, older version) while -b alpha 404'd outright.

Now queries the full /releases list (newest first, unlike /releases/latest
which only ever returns the newest non-prerelease) and takes the first
tag_name containing "-alpha"/"-beta". Brings master to parity with alpha's
same fix.
2026-07-21 18:41:26 +03:00
ospab d9fe749cd4 fix(install): alpha/beta self-update actually finds a real release now
ostp update -b alpha/-b beta (and install.sh --branch alpha/beta directly)
tried to download a GitHub Release literally tagged "alpha" or "beta".
No such tag has ever existed - gha.ps1 cuts a fresh VERSIONED tag on every
release (v0.4.2-beta.4, v0.4.3-alpha.2, ...) - so this always 404'd.

/releases/latest can't help either: it only ever returns the newest
non-prerelease (stable) tag, by GitHub's own definition, so it can never
surface an alpha/beta release even in principle.

Fix: for alpha/beta, query the full /releases list (returned newest-first)
and take the first tag_name containing "-alpha"/"-beta". Verified the
grep/sed extraction against a mock releases-list payload for both channels.
2026-07-21 18:39:48 +03:00
ospab cdfd2babc0 chore: release v0.4.2-beta.4 on beta 2026-07-21 18:18:01 +03:00
ospab 2092e22a7c fix(cli): missing sha2::Digest import broke the CI build (v0.4.2-beta.3)
Sha256::digest() is a trait method (from digest::Digest, re-exported as
sha2::Digest), not an inherent one - fully-qualifying the call
(sha2::Sha256::digest(...)) doesn't exempt it from Rust's requirement that
the trait itself be in scope for method resolution. Whatever made this
resolve locally without the explicit import didn't reproduce on the CI
runner's dependency resolution, breaking `cargo check` and killing the
whole v0.4.2-beta.3 matrix before a single platform job even started.
Added the import; harmless even where it isn't strictly needed.
2026-07-21 18:17:49 +03:00
ospab 5278f58903 chore: release v0.4.2-beta.3 on beta 2026-07-21 18:12:30 +03:00
ospab 340819745a chore: update Cargo.lock for ostp's new direct sha2 dependency 2026-07-21 18:10:23 +03:00
ospab e31c4b2268 fix(protocol): don't abandon slow start over a single isolated packet loss
Root cause of "connection takes 20-30s, sometimes 1-2 minutes, to reach
stable throughput" (trickle of KB for a while, then a sudden jump to full
speed): on_loss during SlowStart unconditionally halved cwnd AND
permanently switched to ProbeBandwidth's linear (+1 MTU/RTT) growth on the
very FIRST loss. Real mobile/Wi-Fi links have a non-zero background loss
rate from ordinary wireless noise and handover blips that has nothing to
do with congestion; on such a link the first RTT or two of slow start would
hit a loss, get knocked into linear growth from a still-small window, and
take an enormous number of RTTs to claw back up to full speed - directly
contradicting the module's own stated BBR-inspired design intent, since
real BBR is deliberately loss-tolerant during startup instead of treating
any loss as a hard congestion signal.

Fix: track losses within a short (500ms) window and only pay the full
exit-slow-start-and-halve cost once SLOW_START_LOSS_TOLERANCE (3) losses
land within it - sustained loss is still treated as real congestion.  A
single isolated loss now takes a mild, temporary haircut (cwnd *= 0.8) but
stays in slow start, so exponential growth continues instead of being
abandoned over a one-off dropped packet.
2026-07-21 18:08:04 +03:00
ospab e46c863ef0 fix(client): stop leaking a socket+task per direct-bypassed SOCKS5 UDP flow
Same class of bug as dbf923f (which fixed the TUN-mode UDP NAT path):
handle_udp_associate's direct-bypass branch spawned spawn_direct_udp_reader
holding its own Arc<UdpSocket> clone with no way to know when the
UDP-associate session it belonged to had ended. Every SOCKS5 UDP session
that ever bypassed traffic direct (an excluded IP/domain) leaked one
socket + one reader task for the rest of the process's life.

Wired a oneshot cancellation channel per spawned reader, held by
handle_udp_associate itself: the channel closes automatically (no explicit
signal needed) the instant that function returns, on every exit path,
telling the reader loop to stop via tokio::select! against the cancel
future.
2026-07-21 18:07:49 +03:00
ospab cddd623ad0 fix(client): coalesce bursty NetworkChanged events on mobile handoff
Root cause of "constantly disconnects on mobile, have to reconnect
manually": Android's ConnectivityManager fires onLost(old) + onAvailable(new)
within milliseconds of each other during a real Wi-Fi<->cellular handoff,
and each one queues its own BridgeCommand::NetworkChanged. Each reconnect is
a full sequential handshake (up to ~1.2s x 4 attempts x mux_sessions) run
synchronously inside the bridge's select-loop iteration - so without
coalescing, the FIRST queued NetworkChanged often starts reconnecting before
the OS has actually finished switching networks, races the dying interface,
and only fails after burning its full attempt budget. Only THEN does the
SECOND (correct) NetworkChanged get to run its own reconnect. A sub-second
handoff was turning into several extra seconds of outage on every
occurrence, and multiple back-to-back handoffs (common walking in/out of
Wi-Fi range) compounded this every time.

Fix: on NetworkChanged, drain any additional same-kind events already
queued before starting the reconnect, so a burst collapses into one attempt
using the freshest signal. A different command found while draining isn't
dropped - it's dispatched immediately (recursing into handle_bridge_cmd)
so nothing queued behind the burst gets lost or reordered incorrectly.
2026-07-21 17:57:39 +03:00
ospab 9a891310f9 fix(cli): setup wizard used a fake password hash, locking admins out of their own panel
The Server+Panel setup wizard's panel-password hashing was a placeholder:
std::collections::hash_map::DefaultHasher (SipHash, not cryptographic, and
not even a 256-bit output - only the first 8 of 32 bytes were real, the
rest zero-padded), left in by the comment "sha2 is not a direct dep of
ostp/Cargo.toml, so we use std's hasher as a placeholder digest here."

api.rs's handle_login computes the REAL SHA256 hex digest of the submitted
password and compares it against config.json's stored password_hash. Since
the wizard's placeholder never produces the same value as real SHA256 of
the same password, anyone who set up a panel through this wizard could
never actually log into it with the password it just showed them - a
complete functional break of the wizard-driven admin flow, not a corner
case.

Added sha2 as a direct ostp dependency and replaced the placeholder with
the exact same format!("{:x}", Sha256::digest(..)) api.rs's login check
uses.
2026-07-18 18:14:19 +03:00
ospab d9686c9344 fix(ci): cap lints when installing cross, so its own code can't fail our build
The mipsel-unknown-linux-musl job in v0.4.2-beta.2 failed at "Install cross":
cross-rs's own source uses a macro-at-end-of-block pattern (eyre::bail!())
that trips rustc's semicolon_in_expressions_from_macros lint on current
toolchains. `cargo install` compiles the installed package as the "local"
crate, so Cargo's usual automatic lint-capping for dependencies doesn't
apply to cross's own code - and other cross-built targets in the same run
(armv7, aarch64-linux, i686-linux) succeeded, so this reads as a race
against cross-rs's unpinned `main` branch history (no --rev/--tag) rather
than a deterministic break.

RUSTFLAGS="--cap-lints=warn" is the standard mechanism for exactly this
situation - building a third-party tool against a newer compiler than its
own lint config assumed - without touching our own build's lint levels.
More robust than pinning to one historical commit, which just relocates
the same risk to whenever that pin is next updated.
2026-07-18 17:59:02 +03:00
ospab dbf923fb16 fix(client): stop leaking a socket+task per bypassed UDP flow
start_udp_bypass_session (the TUN-mode path for UDP from apps/IPs the user
has excluded from the tunnel) spawned a separate task to read from the
physical-interface-bound socket, holding its own Arc<UdpSocket> clone.
Nothing ever cancelled that task when the outer function returned (e.g.
once session_rx closed) - it just kept running, and its socket clone kept
the OS fd alive, for the lifetime of the process. Every distinct bypassed
UDP flow (any excluded app's DNS query, game session, etc.) leaked one
socket and one task permanently.

The sibling function right below it, start_udp_session, already does this
correctly: one tokio::select! loop combining both directions in a single
task that exits (and drops the socket) as soon as either side closes.
Rewrote start_udp_bypass_session to match that pattern instead of
spawning a detached reader task.
2026-07-18 17:46:14 +03:00
ospab 51b947e6ff fix(server): rate-limit the (currently unwired) open UDP DNS listener
DnsServer::run_local_udp_listener binds 0.0.0.0 and answers every UDP
datagram by resolving it and replying to the packet's (unverified,
spoofable) source address - a textbook DNS reflection/amplification
primitive. An attacker spoofing a victim's IP as the query source turns
any server with this listener running into a free amplifier against that
victim, with zero authentication gating it (unlike the main OSTP port,
there's no Noise handshake here).

Nothing in the codebase currently calls this function - the live DNS path
is router.route_dns(), reached only through the authenticated OSTP tunnel
relay (relay.rs). But the doc comment describes this as an intended,
not-yet-wired entry point for clients that point their OS resolver
directly at the server, so it's a real latent risk for whoever connects it
without realizing the implication. Added a global (not per-source-IP -
per-IP limiting doesn't help against a reflection attack, since the
attacker never sees the replies and can spread queries across arbitrary
spoofed sources) token bucket capping total replies/sec, so connecting
this later can't silently reintroduce unbounded amplification.
2026-07-18 17:42:49 +03:00
ospab f01ed4ec25 fix(server): constant-time comparison for Management API secrets
check_token() and handle_login() compared bearer tokens, session tokens,
and the password hash with plain ==, which short-circuits on the first
differing byte - a textbook remote timing side-channel against exactly
the long-lived secrets these gates exist to protect. Added subtle (already
in the dependency tree transitively via chacha20poly1305) as a direct
dependency and route every secret comparison through a small secure_eq()
wrapper over ConstantTimeEq. Username comparison in handle_login is left
as-is: it isn't treated as a secret in this threat model (one fixed admin
username), matching standard practice of only constant-timing the
password/token side of an auth check.

Added tests for secure_eq() itself (equal, different, different-length,
empty) alongside the existing check_token coverage.
2026-07-18 17:38:09 +03:00
ospab c2a1a53b4d fix(server): audit-log API endpoints had no auth check at all
GET/POST/DELETE /api/audit were the only three handlers in the whole
Management API that never called check_token() - every other endpoint
(status, users, rules, config) does. Concretely, with the panel's
credentials configured, an unauthenticated request could still:
  - read the full audit log (GET)
  - inject arbitrary forged entries, e.g. fake "success" events to cover
    tracks (POST)
  - wipe the entire audit log (DELETE) - the exact mechanism meant to
    detect and investigate unauthorized actions, erasable with zero auth

Added the same check_token() gate the rest of the file uses, and fixed
these three handlers' raw .unwrap() on the audit_logs lock to the
poison-recovery pattern (unwrap_or_else(|e| e.into_inner())) used
everywhere else, for consistency.

Added focused unit tests on check_token() itself (missing header, correct/
wrong bearer, raw token, session token, and the documented open-panel
mode when no credentials are configured) - it's the single gate every
sensitive handler depends on, worth pinning down independently of any one
handler.
2026-07-18 17:27:58 +03:00
ospab cd12b01bc3 chore: release v0.4.2-beta.2 on beta 2026-07-18 17:10:47 +03:00
ospab de5cee103b fix(install): setup wizard is a subcommand now, not a --setup flag
Both installers still invoked `ostp --setup` / `ostp.exe --setup` to launch
the first-run wizard on a fresh install. The CLI's subcommand refactor
(2026-07-08, "Refactor CLI to subcommands") turned `setup` into
`Commands::Setup { .. }` with no top-level `--setup` flag left in Args at
all, so every fresh install has hit "error: unexpected argument '--setup'
found" and dropped the user out of the installer instead of the wizard.
Verified `ostp setup --help` parses correctly with the fix.
2026-07-18 16:49:08 +03:00
ospab c523b083cb fix(server): outbound connect no longer lets a dead IPv6 candidate eat the whole timeout
Matches a real report: traffic counters move (the OSTP tunnel handshakes
fine) but sites don't open, or take very long - on a freshly deployed
DigitalOcean droplet in Amsterdam.

connect_target's fallback path handed the raw "host:port" string straight
to TcpStream::connect, which resolves and tries addresses internally but
shares ONE 10s timeout across the WHOLE attempt (all resolved addresses,
not per-address). Some VPS hosts assign the machine an IPv6 address that
the OS prefers by RFC 6724 ordering but that has no actually-working
outbound route - the connect doesn't get refused, it just hangs. With a
single shared budget, that one dead IPv6 candidate eats the entire 10s and
the working IPv4 candidate is never even attempted: every dual-stack
destination (i.e. most popular sites) times out, while IPv4-only
destinations work fine.

New connect_direct() resolves target itself via lookup_host, sorts IPv4
candidates first, and tries each with its own 3s budget (still bounded
overall by the original 10s outer timeout as a backstop) so a hung IPv6
attempt can't starve the IPv4 fallback of a chance.

Added tests: IPv4-first sort ordering (and stability within a family), a
successful connect against a live local listener, and a refused-port
connect failing well under the timeout (proving failures aren't
needlessly slow). Scoped to connect_target's direct-connect paths; the
SOCKS5/HTTP outbound-proxy paths and the fallback/camouflage TCP proxy
(which targets a fixed admin-configured local address, not arbitrary
dual-stack hostnames) are unaffected.
2026-07-18 16:40:57 +03:00
ospab c6a130673d fix(gui): remove duplicate junk/tcp-frag fields from the profile editor modal
These were editable in two disconnected places: the profile editor modal
(pm-* fields, written into each saved profile's own tcp_fragmentation/
frag_chunk/frag_sleep/junk_pc/junk_ps) and the simple settings page (cs-*
fields, a global override applied at connect time via buildConfig()'s
merge: `s.tcpFrag || active.tcp_fragmentation`, etc). The simple settings
page already covers the same knobs, so the modal copy was pure duplication
and a source of confusion about which one actually took effect.

Removed the pm-tcp-settings panel and its fields from index.html, and all
now-dead JS: the variable lookups, the open-editor populate/reset logic,
the save-profile field writes (existing profiles keep their previously-
saved values via the {...profiles[idx], ...} merge - only new edits
through this modal no longer touch these fields), and the two change
listeners whose sole job was showing/hiding the removed panel. The
Transport (UDP/UoT) dropdown itself is untouched.
2026-07-18 16:30:44 +03:00
ospab c756e02b63 fix(server): throttle relay reads to the client session's congestion window
Matches a real user report after 2 weeks on this version: bandwidth is low,
the client reconnects every 10-20 minutes, sites randomly stop loading or
crawl, and ping visibly jitters. Root cause: the per-target-connection
reader task (handle_relay_message's Connect handler) read from the
upstream target as fast as it would send and forwarded every chunk
straight to send_relay_to_stream -> an immediate UDP datagram, with ZERO
awareness of the client-facing OSTP session's actual congestion window.
The client already gates its own uplink on cwnd (bridge.rs's proxy_ev
select arm); the server's download direction had no equivalent.

On a real (lossy/jittery mobile or Wi-Fi) client path, a fast target (a
CDN, say) gets blasted at the client far beyond what the path can sustain.
That's a self-inflicted loss burst: it wrecks the RTT/RTO estimate (the
"ping jitters" symptom), can push the session into a stall bad enough that
the client's 25s/180s keepalive stall-detection gives up and reconnects
(the "every 10-20 minutes" symptom), and produces exactly the "randomly
stops loading or crawls" experience while it's happening.

Fix: Dispatcher::snapshot_backpressure() computes each session's headroom
(clamped cwnd - in_flight, same clamp(16,16384) the client uses) from the
existing 10ms retransmit tick - no new polling loop. Published to a
lock-free-reader Arc<AtomicI64> per session in a shared map so relay reader
tasks (which don't have access to Dispatcher; it lives on the main loop
task) can check it without touching a lock/mutex on every read. Before
each read, a reader task waits (capped at 2s, so a wedged read can't stall
forever) while headroom is <= 0. New sessions default to a healthy 32
packets until their first snapshot lands (worst case 10ms), so this can't
stall the very first bytes of a fresh connection.

Scoped to the primary TCP CONNECT/proxy path (the one actual web browsing
uses); UDP relay/TURN is unaffected. No load test against real network
jitter was possible in this environment - the mechanism directly targets
the identified cause, but real-world confirmation is still needed.
2026-07-18 15:40:12 +03:00
ospab 70a669d3c6 fix(install): setup wizard is a subcommand now, not a --setup flag
Both installers still invoked `ostp --setup` / `ostp.exe --setup` to launch
the first-run wizard on a fresh install. The CLI's subcommand refactor
(2026-07-08, "Refactor CLI to subcommands") turned `setup` into
`Commands::Setup { .. }` with no top-level `--setup` flag left in Args at
all, so every fresh install has hit "error: unexpected argument '--setup'
found" and dropped the user out of the installer instead of the wizard.
Verified `ostp setup --help` parses correctly with the fix.
2026-07-18 15:10:57 +03:00
ospab 66a1e97840 chore: release v0.4.2-beta.1 on beta 2026-07-12 02:18:47 +03:00
ospab b6bdd53066 merge beta: reconcile 2 obsolete version-bump commits from the old lineage
beta had two "chore: release 0.4.5-beta"/"0.4.6-beta" commits (pure
Cargo.toml/tauri.conf.json/package.json/pubspec.yaml/.release-state.json
version bumps, no code) from the pre-gha.ps1-rewrite versioning scheme
that was abandoned when the project reset to 0.4.1 as the new baseline
(see the earlier master reconciliation). alpha's manifests are about to
be bumped to the new 0.4.2 target anyway, so alpha's side wins on the
conflicting version files - this is a pure reconciliation, not a content
decision.
2026-07-12 02:16:53 +03:00
ospab 1c291d9c88 fix(release): the second branch is 'beta', not 'pre-release' — was never checkoutable
scripts/gha.ps1's -Branch ValidateSet accepted 'pre-release' and would
`git checkout pre-release` to promote alpha, but no such branch has ever
existed in this repo — only `beta` does (confirmed: `git branch -a`, and
the existing 0.4.6-beta/0.4.7-beta release history was cut from `beta`).
The very first beta release under the new gha.ps1 versioning scheme would
have failed outright on the checkout step.

This naming mismatch had spread through the whole release surface:
  - scripts/gha.ps1: -Branch ValidateSet + all internal checks
  - .github/workflows/release.yml: a dead branch-name check (harmless only
    because the workflow currently triggers on tag-push, not branch-push)
    plus two comments
  - CONTRIBUTING.md / .ru.md: branch-strategy table documented a
    `pre-release` branch that doesn't exist
  - README.md / .ru.md and ostp/src/main.rs: the `ostp update -b <name>`
    CLI help text/docs
  - scripts/install.sh: the channel match the CLI flag feeds into

Renamed all of it to `beta` to match the branch that actually exists.
Left scripts/gha.ps1:21's "semver pre-release identifier" alone — that's
the generic semver spec term, unrelated to the branch name, and got
reverted after a blanket replace briefly clobbered it.

Note: install.sh's alpha/beta self-update paths still assume a rolling
GitHub release tagged literally "alpha"/"beta" exists, which no gha.ps1
release ever publishes (only versioned tags like v0.4.7-beta.3) - that's
a separate, real bug, tracked apart from this rename since fixing it needs
either a floating tag from gha.ps1 or an API-query rewrite of install.sh.
2026-07-12 02:12:57 +03:00
ospab 2660a37249 merge protocol-hardening: forward secrecy + DoS/logging/dead-code hardening
Brings the crypto security audit into alpha alongside the already-merged
reconnection/UoT/Flutter fixes:

  - CRITICAL: transport keys now come from Noise Split() over ck (DH-
    inclusive), not the handshake hash — restores forward secrecy.
    Wire-breaking, PROTOCOL_VERSION 4->5.
  - Rate-limit + cache the O(N_keys) handshake trial path (CPU DoS).
  - Quiet hot-path logging; access keys no longer logged verbatim.
  - Removed dead 0-RTT resumption module (unsafe XOR ticket crypto).
  - Karn's algorithm RTT fix, 32-bit frame-length overflow guard,
    replay-cache eviction instead of global reject.
  - Updated EN/RU specification docs to match.

No conflicts: protocol-hardening's bridge.rs commit was an independent
duplicate of the same reconnection fix already on alpha, so git merged it
as a no-op on that file.
2026-07-12 02:06:42 +03:00
ospab 108ab8468b fix(flutter): show Junk/TCP-Frag controls only when transport is UoT
Junk packets and TCP fragmentation only take effect on the UoT (TCP)
transport — the UDP path applies neither — so showing them (with an
implicit "UoT only" caveat) while UDP is selected was misleading. The
whole DPI OBFUSCATION section is now gated on transportMode == 'uot' and
appears/disappears reactively when the Transport dropdown changes
(setDialogState already rebuilds the dialog).
2026-07-12 02:04:09 +03:00
ospab 5fcc0ba7f4 fix(server): UoT — set TCP_NODELAY + tear down half-open connections
Two UoT (UDP-over-TCP) correctness issues:

- The accepted UoT stream never had TCP_NODELAY set (the client sets it on
  its end, the server didn't). Nagle's algorithm then batched server->client
  writes and interacted with the client's delayed ACKs, adding tens-to-
  hundreds of ms of stall per burst — throttling the download direction
  badly for streaming/video. Every TCP-tunnel proxy disables Nagle; now the
  server matches the client.

- handle_tcp_connection join!ed the reader and writer tasks, so a half-open
  connection (client's read side gone, no outbound data pending) parked the
  writer on rx.recv() forever, leaking the task and a stale tcp_map entry.
  Rewrote it with select! so either half closing cancels the other and the
  tcp_map entry is always removed. Added duplex-stream tests covering
  inbound reassembly across segment boundaries, outbound framing, and
  teardown-on-close.
2026-07-12 01:25:34 +03:00
ospab 7e3ada8d4d fix(client): robust reconnection across sleep/resume + no zombie tasks
Addresses the PC-after-sleep failure (app either fully disconnects or gets
stuck "Connecting") and the mobile "must reconnect manually" symptom.

- Zombie receiver tasks: each session spawns a task that loops on recv().
  On a dead connection recv() never returns, so the task (and the socket it
  holds) leaked on every reconnect, piling up across sleep/resume cycles.
  SessionState now owns the task's AbortHandle and aborts it on Drop, so
  replacing sessions tears the old task down. The three duplicated inline
  receiver loops are consolidated into spawn_session_receiver().

(Builds on the tick-storm and resume-detection changes already in tree:
MissedTickBehavior::Skip on all intervals so a post-sleep wake doesn't fire
tens of thousands of catch-up ticks — the 10ms retransmit tick was the
worst — and a wall-clock-gap check that forces one clean reconnect on wake
via handle_keepalive(force=true).)
2026-07-12 01:21:58 +03:00
ospab 4fd4f7d435 build: drop stale [patch.crates-io] for removed vendored netstack-smoltcp
The vendored netstack-smoltcp directory was removed, but the workspace
[patch.crates-io] entry still pointed at the now-missing path, breaking the
whole build. ostp-client already declares netstack-smoltcp = "0.2.2", so
dropping the patch simply builds against the published crate (0.2.4).
2026-07-12 01:21:58 +03:00
ospab b166f13d59 chore: remove dnstt, netstack-smoltcp, and ostp-web and add to gitignore 2026-07-12 00:36:38 +03:00
ospab a69ffae750 docs: update architecture diagram to be more understandable 2026-07-12 00:34:32 +03:00
ospab 90a919df59 docs: update architecture diagram to be more understandable 2026-07-12 00:34:10 +03:00
ospab 4ac2e79e14 docs: document DH-inclusive transport keys / forward secrecy + trial rate-limit
Reflect the crypto hardening in the EN/RU specification:
  - Section 6: transport keys now come from Noise Split() over the chaining
    key ck (includes the ee DH secret), giving forward secrecy; added the
    rationale for why keys must NOT come from the handshake hash h, and the
    wire-version-5 gate.
  - Section 8: documented the handshake-trial CPU-DoS defense (per-key
    secret/marker caching + trial-path token bucket).
  - Corrected the handshake replay window (±300s / 5min, was mis-stated as
    ±30s) and PSK derivation (HKDF-SHA256).
2026-07-11 22:01:40 +03:00
ospab a9509a235d fix: low-severity hardening (Karn RTT, 32-bit frame overflow, replay-cache DoS)
- Karn's algorithm: drop_acked_frames no longer samples RTT from frames
  that were retransmitted (last_sent is bumped on each retransmit, so an
  ACK for the original transmission would measure a spuriously small RTT
  and drag SRTT/RTO down). Added CongestionController::on_ack_no_rtt for
  the case where every acked frame was ambiguous, so the window still
  advances without polluting the RTT estimator. Refactored the shared
  window-growth into grow_window.
- Frame decode: header+payload+pad length now uses checked_add. payload_len
  is a u32 from the header and on 32-bit targets (MIPS/ARMv7 routers are
  supported) the sum could wrap usize and slip past the truncation check.
- Replay cache: a full cache used to reject ALL new handshakes globally
  until the next tick, letting one flooding key-holder deny service to
  everyone. Now it reclaims expired entries and, if still full, evicts the
  single oldest — new handshakes always get in. Fixed the mislabelled
  "100000" log (cap is 50000) and named it REPLAY_CACHE_MAX.
2026-07-11 21:25:36 +03:00
ospab 5754689e09 refactor: remove dead 0-RTT resumption module (unsafe XOR ticket crypto)
The resumption module (SessionTicket/TicketValidator) was never wired into
the client or server — nothing issued or validated tickets, and no Resume
frame was ever sent. But it "encrypted" tickets by XOR-ing them with a
single static keystream SHA256(psk || const) and had no MAC (despite a doc
comment claiming HMAC): a textbook many-time-pad, trivially broken from a
couple of captured tickets, and malleable. Leaving it in-tree invited
someone to wire up a broken 0-RTT path later.

Removed the module, its FrameKind::Resume wire variant, and the protocol
handler for it. 0-RTT can be reintroduced later on a real AEAD-sealed
ticket if desired.
2026-07-11 21:22:45 +03:00
ospab b5735fe8c2 fix: quiet hot-path logging and stop logging access keys verbatim
Two classes of issue:
  - Hot-path/attacker-triggerable events logged at info/error with internal
    detail: a per-handshake info! byte dump (raw_vec[0..6]) and a per-packet
    error! on session-id mismatch that dumped expected/got session ids.
    Both are log-flood + info-leak surfaces; downgraded to debug and
    stripped of the sensitive detail. Close/Resume frame handling likewise
    moved from info to debug.
  - The access key (a shared secret) was written to logs verbatim in three
    places (session drop, key-created UI event, API create-user) and as an
    8-char prefix in one. Added key_fp() — a short SHA-256 fingerprint — and
    routed all key logging through it so operators can still correlate
    events without the secret ever hitting the log.
2026-07-11 21:21:01 +03:00
ospab f904695760 fix(server): rate-limit + cache the O(N_keys) handshake trial path (CPU DoS)
Every datagram from an unrecognized source ran the full key-trial loop:
for each registered access key, an HKDF (derive_all_secrets) plus two
HMACs (junk markers) plus a Noise read. A garbage flood from spoofed
sources could therefore force unbounded O(N_keys) crypto per packet — a
CPU-amplification DoS with no throttle (the existing token bucket only
guarded the roaming path, not this one).

Two mitigations:
  - Memoize the per-key derived secrets (pure function of key+version) and
    the per-window junk markers, so the trial loop is now cheap comparisons
    plus one Noise read per key instead of HKDF+2*HMAC per key per packet.
    Also speeds up every legitimate new connection. Caches are pruned in
    on_tick when keys are deleted.
  - Gate the trial path behind a global token bucket (TRIAL_RATE=100/s,
    same burst). The established-session fast path and roaming are not
    gated, so live sessions are unaffected; only unknown-datagram trials
    are bounded. Over-budget datagrams are dropped silently.
2026-07-11 21:18:05 +03:00
ospab 29554a71f1 fix(crypto)!: derive transport keys from DH-inclusive Noise Split, not the handshake hash
CRITICAL forward-secrecy fix. Session transport keys were derived as
SHA256(get_handshake_hash() || label). The Noise handshake hash `h` only
ever absorbs PUBLIC transcript data (ephemeral pubkeys + on-wire
ciphertexts, via MixHash); the ephemeral ee DH result is mixed via MixKey
into the chaining key `ck` ONLY, never into `h` (confirmed in snow 0.9.6
symmetricstate.rs). So the data-transport keys depended on the PSK and the
public transcript but NOT on the DH secret, meaning:

  - zero forward secrecy: anyone who later learns the access-key PSK can
    decrypt all recorded past sessions from the observed handshake alone;
  - any PSK holder can passively decrypt any other session on that key;
  - the ephemeral Diffie-Hellman was cryptographically wasted.

Fix: take the two directional keys from Noise's Split() over the final `ck`
via snow's dangerously_get_raw_split (risky-raw-split feature). These keys
depend on ee, restoring forward secrecy. The custom out-of-order AEAD,
explicit nonces, session_id AAD, framing and reordering are all unchanged
- only the key SOURCE moved. The dead into_transport()/handshake_hash()
paths and the unreachable NoiseSession::Transport variant are removed.

Wire-breaking: PROTOCOL_VERSION 4 -> 5 so pre-fix peers derive different
keys and cannot interop (version gate is invisible on the wire).

Added noise unit tests for the .0/.1 -> send/recv role mapping and the
not-finished guard.
2026-07-11 21:14:15 +03:00
ospab 271a39c664 fix(icons): forcefully overwrite all android legacy and adaptive icons with logo_new.png 2026-07-11 20:59:45 +03:00
ospab 44f9067222 feat(icons): apply logo_new.png to all apps and watermarks 2026-07-11 00:58:44 +03:00
ospab dee0288f2a fix(release): rename -Switch param to -NewVersion (silently broke channel resolution)
A script parameter named exactly $Switch collides with PowerShell's `switch`
statement keyword - confirmed by bisection - and made every `$X = switch (...)
{...}` in the script silently evaluate to empty instead of erroring. This is
what produced the malformed "v0.4.1-.0" tag on the last release attempt
(Channel resolved to "" instead of "stable", Iteration to 0). Renaming the
parameter is the only fix; nothing else about the switch statement itself
was wrong.
2026-07-10 03:25:26 +03:00
ospab 10ec253fa0 chore: release v0.4.1-.0 on master 2026-07-10 03:17:41 +03:00
ospab cb59a5343f merge master: reconcile 3 commits pushed directly to master
master had UAC/SmartScreen fix, RTT/speed-display toggle, and a run-name CI
tweak that never made it back into alpha. Alpha already independently
contains equivalent (UAC fix is byte-identical) or superior (run-name
handles the newer alpha/beta/nightly channel scheme master's version
doesn't know about) versions of all three, so this merge is a pure
reconciliation - alpha's side wins on every conflicting hunk.
2026-07-10 03:14:59 +03:00
ospab b7bd8c20a5 docs: update CLI arguments to subcommands 2026-07-10 03:05:30 +03:00
ospab d725a4440b chore: gitignore netstack-smoltcp (vendored, already tracked separately) 2026-07-10 01:47:20 +03:00
ospab caab8698ba chore: remove dead ostp-license/frontend (143MB committed node_modules+dist)
No source ever existed in this tree for it - only a built dist/ and a
full node_modules/ dump (6200+ files), and nothing in the codebase
references "ostp-license" anywhere. Leftover from the old commercial-
license-gated era before the AGPLv3 switch; pure bloat since.

Also added **/node_modules/ to .gitignore - its absence is exactly how
this got committed in the first place.
2026-07-10 01:46:32 +03:00
ospab 3e9e8845f1 docs: remove nonexistent 'prober' entry from CLI reference
'ostp prober' was never a real subcommand - ostp-prober is a separate,
gitignored standalone tool, not part of the ostp binary's CLI surface
(no Prober variant in the Commands enum, no handler in main.rs). Also
fixed misaligned columns on the proxy-env/proxy-env-clear lines.
2026-07-10 01:44:01 +03:00
ospab e52087ee8e fix: restore LICENSE file to actual AGPL-3.0 text (was stuck on old BSL 1.1)
The repo switched to AGPLv3 back on 2026-06-18 (commit 9ce9e6d), and
Cargo.toml/README have said AGPL-3.0 ever since — but that license-change
commit was never carried forward into the 0.4.x rebuild branch, so the
actual LICENSE file silently reverted to the pre-rebuild BSL 1.1 text
(with a "converts to MIT in 2030" clause that hasn't applied for months).
Restored the real AGPLv3 text from 9ce9e6d.

Also added the missing `license` field to a few crate manifests that
didn't declare one (ostp-gui/src-tauri, ostp-jni, ostp-tun-helper), and
dropped the Tauri template placeholder authors/description.
2026-07-10 01:36:41 +03:00
ospab 2092f6c716 fix(flutter/android): surface getMetrics failures into the in-app log
Traced the whole traffic-counter pipeline (Dart -> MethodChannel ->
Kotlin -> JNI -> Bridge) end to end; it's architecturally identical to
the working desktop implementation, so no code-level bug was found.
Previously a getMetrics exception was only reported as a PlatformException
that Dart swallows with a bare debugPrint, invisible in the in-app log
viewer users actually have access to. Now it's also written to the
native log buffer via OstpClientSdk.addLog, so if the counter breaks
again the actual cause (exception vs. genuinely-zero atomics) shows up
in View Logs instead of requiring adb.
2026-07-10 01:23:28 +03:00
ospab 223f02287a feat(flutter): add optional live speed/RTT display, matching desktop GUI
Mirrors ostp-gui's "Show Speed" / "Show RTT" client settings toggles
(both default on): the home screen now shows live download/upload
throughput (computed from byte deltas between 1s polls, same as
desktop's poll()) as a subtitle under the existing cumulative
Download/Upload totals, and the RTT box is now hideable. Also fixed
"Test Ping" to actually query getMetrics instead of just faking a
500ms spinner with no real measurement.
2026-07-10 01:18:07 +03:00
ospab 1d1a1ea5af refactor: remove dead stealth_sni config field across the whole stack
stealth_sni was never actually consumed to construct any wire bytes —
verified dead in bridge.rs (only stored, never read). It implied
TLS/HTTP SNI mimicry that this project deliberately does not do
(zapret-like: packet-level DPI obfuscation only, no protocol
mimicry). Removed from the runtime schema (config.rs, bridge.rs),
both CLI/GUI local config shapes and their JSON templates, the
Flutter profile model/UI/share-link logic, and README feature docs.
migrate.rs now drops the field from legacy configs with a note
instead of carrying it forward.
2026-07-10 01:04:56 +03:00
ospab 1b3390a3cf fix(flutter): declutter profile editor, fix contrast bugs, unblock app list
- Profile edit dialog: moved junk packets + TCP fragmentation into their own
  modals (tap-to-configure), replacing 5 inline field rows with a compact
  2-button row. These are occasional/advanced settings, not something every
  profile edit needs to see up front.
- Profile card: subtitle repeated the server address verbatim whenever a
  profile had no custom name (name falls back to serverAddr) — showing
  "1.2.3.4:50000" as both title AND subtitle, with transport mode tacked on
  the end of the second copy. Now only shown once; added maxLines/ellipsis
  so long addresses truncate instead of wrapping awkwardly.
- Mobile: removed the "Bypass Processes" field entirely (editor UI, prefs
  key, config JSON). Android per-app selection (Configure Split Tunneling)
  is the real, correct control here — a process-name text field doesn't map
  to anything meaningful on Android the way it does on desktop.
- Share icon changed from a QR icon (redundant — the modal already shows a
  QR code) to the standard Material share glyph. Share modal title no
  longer interpolates the profile's name, which — same root cause as
  above — can silently BE the raw server address; title is now generic
  ("Share Profile") so a screenshot/recording can't leak host:port through it.
- Contrast: the monochrome theme's colorScheme.primary is pure white
  (0xFFFFFFFF); several buttons hardcoded white text/icons on top of it
  (Bypass/Proxy mode toggles, Copy Link), making them invisible when active.
  Added an _onColor() helper (luminance-based black/white pick) and applied
  it everywhere a button's foreground sits on a theme color.
- "Configure Split Tunneling" appeared to hang for 10-15s before doing
  anything: MainActivity.kt's getInstalledApps handler enumerated every
  installed package AND decoded+re-encoded each one's icon synchronously
  inside the MethodChannel callback, which runs on the main/UI thread by
  default — blocking it for the whole duration meant Flutter couldn't
  render ANY frame, not even the loading spinner, until it finished. Moved
  the work onto a background Thread; only the final result.success() hops
  back via runOnUiThread(). Navigation + spinner now show immediately.
2026-07-10 00:45:30 +03:00
ospab 7d9e5faeec fix(flutter): eagle watermark rendered as a flat gray square
assets/logo.png had NO real alpha transparency — both the background and
the eagle shape were fully opaque (A=255 everywhere), just baked in as
near-black (3,3,3) vs near-white (253,253,253) RGB. Applying `color:
Colors.white` to tint it painted the WHOLE bounding square white (alpha
being 255 across the entire image gives BlendMode nothing to mask against),
which at low Opacity looked like a flat gray square instead of a silhouette.

Converted the asset in place: since it was already grayscale (R=G=B), each
pixel's luminance became its new alpha channel, RGB set to pure white. The
background (near-black, low luminance) is now near-transparent; the eagle
(near-white, high luminance) is now near-opaque. This is the same effect the
desktop GUI gets for free from its logo.svg (a vector eagle path with no
background element at all — inherently transparent), just reproduced for a
raster asset without adding flutter_svg as a new dependency.

The `color: Colors.white` tint in both watermark call sites is now
redundant (the asset is already a pure-white silhouette) and removed.
2026-07-10 00:24:28 +03:00
ospab eda2a0eba7 fix(flutter): increase watermark opacity and apply to settings 2026-07-10 00:09:55 +03:00
ospab 22c2d5edd8 feat(flutter): update UI theme to monochrome with eagle watermark 2026-07-09 23:57:58 +03:00
ospab fa7ec2cd9a feat(flutter): update launcher icons to new eagle design with round support 2026-07-09 23:52:37 +03:00
ospab 794ea5251b refactor(ci): target_version + per-channel iteration in gha.ps1
Previous scheme conflated "which release is this" with "how many times has
it been rebuilt": every run bumped the patch version, so by the time a build
was ready to promote to master the version number had already crept forward
by however many alpha/beta iterations it took to get there.

Now a release cycle has one fixed target version (e.g. 0.4.1) that stays in
every manifest unchanged through all alpha/beta iterations; only a
per-channel counter increments, and that counter lives ONLY in the git tag,
never in Cargo.toml:

  v0.4.1-alpha.1 -> v0.4.1-alpha.2 -> ... -> v0.4.1-alpha.N
  v0.4.1-beta.1  -> v0.4.1-beta.2  -> ... -> v0.4.1-beta.N
  v0.4.1                                          <- master, iteration dropped

Deliberately "0.4.1-alpha.N" (dot AFTER the hyphen — a semver pre-release
identifier), not "0.4.1.N-alpha" (a 4th dot component before the hyphen):
the latter isn't valid semver and Cargo's version parser rejects it outright,
so it can never appear in Cargo.toml. That's also why the target version
itself never needs to change on a plain iteration — bumping every manifest +
refreshing both Cargo.locks is now skipped entirely unless -Switch actually
changes the target, making a routine alpha/beta push fast (just the state
file's counter + a tag).

Also fixes a real bug found while touching this: release.yml's push trigger
is tags-only ("v*") with no branch trigger, so the old `git push origin
$branch`-only path for alpha/pre-release never actually started a CI run —
only the master path (which already pushed a tag) worked. Every channel now
always pushes a real tag, which is what actually triggers the build.

release.yml's resolve-channel needed no changes: its tag-channel detection
already does substring matching (*-alpha*/*-beta*), so it classifies
"v0.4.1-alpha.37" correctly without modification.

-Prefix is gone — channel was always 1:1 with -Branch (alpha/pre-release/
master), so it was a redundant, independently-settable axis that could
silently drift from the branch (e.g. -Branch alpha -Prefix beta).
2026-07-09 23:42:27 +03:00
ospab c6d506e6c0 feat(flutter): profiles + per-profile junk/frag, drop WSS
Merges the best of both lineages instead of a blind revert to v0.3.21:
kept from v0.3.21: multi-profile management (add via QR scan/link/manual,
single-select active profile, auto-mode transport/MTU probing). Kept from
current: Share Config (QR generation), Check for Updates, curated stealth-SNI
domain list, and actually-rendered exclusions fields (v0.3.21 loaded/saved
them but never showed them in the UI — dead code).

New: junk packets (pc/ps min/max) and TCP fragmentation (chunk/sleep) are now
per-profile fields in the profile editor, mirroring the desktop GUI's profile
object shape 1:1 (ostp-gui/src/main.js) so behavior matches across platforms.

WSS is gone — removed from the model, the UI, and the config builder. The
core dropped TLS-mimicry transports entirely (see §A of the rebuild), so
there was nothing left for it to configure.

Config building now targets the flat single-server schema
(ostp_client::config::ClientConfig) built from ONE active profile, not the
old modular inbounds/outbounds/urltest-failover config — the core no longer
supports connecting to multiple servers at once, matching how the desktop
GUI already works (single activeId). Also dropped a dead nested "tun": {...}
object that neither version's JSON producer nor the real ClientConfig struct
ever actually used — serde silently ignored it.
2026-07-09 23:42:10 +03:00
ospab 61091b6d56 chore(release): 0.4.7 + fix resolve-channel prerelease labelling
resolve-channel treated EVERY v* tag as stable, so v0.4.6-beta got published
as a non-prerelease "Latest" release, sitting on top of the release line. Now
a pushed tag is used as-is and its suffix decides the channel: v*-alpha / v*-beta
are prereleases, only a bare vX.Y.Z is stable. (A tag is never recomputed from
Cargo.toml, so the release can't upload to a different tag than the one pushed.)

Version bumped 0.4.5 -> 0.4.7 (0.4.6 is already taken by the mislabelled beta).
This commit's tip is what gets tagged v0.4.7-beta to cut the beta build.
2026-07-09 14:58:40 +03:00
ospab aa1c4ccd52 chore: ignore AI agent instructions directory 2026-07-09 14:53:11 +03:00
ospab 5ab6833eab feat(core): time-rotating junk marker — kill the static per-user fingerprint
The junk marker was a per-key CONSTANT sent in plaintext at a fixed offset in
junk frames. Junk is meant to look like random noise (zapret-style), but a
constant prefix is a recognizable per-user structure: an on-path observer
watching one user sees the same 4 bytes on every junk packet, i.e. an OSTP
fingerprint. (The earlier fix only removed the GLOBAL constant.)

Now the marker rotates every 60s window: junk_marker = HKDF(key, ver, 0x04 ||
window). To an observer the prefix changes each window (no fixed signature),
and a captured marker is only valid for ~1 window — the "bit of protection"
against a leaked marker. Only a key holder can compute it, so an outsider still
can't forge a silently-dropped junk packet (and silent-drop is cheaper than
normal processing anyway, so junk spam was never a DoS lever to begin with).

- core: derive_junk_marker(key, window) + current_junk_window() (60s window),
  same version-gated HKDF scheme; junk_marker dropped from DerivedSecrets.
- client: stamps junk with the current window's marker.
- server: checks current AND previous window per key (absorbs ~1 window of
  clock skew) before falling through to unauthorized-probe handling.
- Not a wire break: only junk framing changes; real handshake/data untouched.
  During mixed rollout, unmatched junk merely logs as a probe (cosmetic).
2026-07-09 14:43:40 +03:00
ospab 5dc3a60017 refactor(logging): consolidate all logs into one ostp.log, Windows clears on start
Every process (CLI daemon, GUI, TUN helper) and every subsystem (tracing, the
core event logger, the helper IPC, panic hook) wrote its own file: ostp-cli.log
+ ostp-core.log + ostp-helper.log + ostp-crash.log — a pile per run. Now they
all funnel into a single ostp.log next to the exe.

- logging: LOG_FILE_NAME/log_file_path() as the one source of truth; init_tracing
  gains a `truncate` arg. Truncation is gated twice: Windows-only (cfg!(windows))
  AND daemon-only. One-shot commands (gk/check/init/-V/...) and the elevated TUN
  helper pass truncate=false so they can never wipe a running daemon's log;
  invocation_is_daemon() detects the daemon from argv. On Linux the server always
  appends (history kept, OS-rotated) as requested.
- runner/helper manual writers + panic hook now target log_file_path(), so their
  output lands in the same ostp.log instead of separate files.
2026-07-09 14:32:08 +03:00
ospab a33e5d3874 ci: fix invalid rust toolchain name 2026-07-09 14:30:23 +03:00
ospab 1b536e9cf3 chore: release 0.4.6-beta on beta 2026-07-09 02:54:50 +03:00
ospab 5883f5105b chore: release 0.4.5-beta on beta 2026-07-09 02:51:47 +03:00
ospab e21acc2ee1 docs: update references from nightly to alpha 2026-07-09 02:49:33 +03:00
ospab 1568db3323 ci: rename nightly to alpha 2026-07-09 02:46:47 +03:00
ospab edb2d8e229 ci: fix powershell encoding error 2026-07-09 02:45:00 +03:00
ospab d609a3e883 ci: fix powershell parse error caused by utf-8 em-dash 2026-07-09 02:44:11 +03:00
ospab 43914055b3 ci: fix branch name in gha.ps1 2026-07-09 02:43:46 +03:00
ospab 3df5d5fccf ci: remove push branches to save GHA minutes, update script to beta 2026-07-09 02:43:07 +03:00
ospab 3d531ee0d9 fix(gui): fix light theme invisible text/borders 2026-07-09 02:39:19 +03:00
ospab 2819e2b3c2 feat(gui): add junk and tcp fragmentation global settings 2026-07-09 02:35:21 +03:00
ospab 244d3ad374 ci: update GHA run-name format for releases 2026-07-09 01:25:46 +03:00
ospab b89c6b0950 ci: update GHA run-name format for releases 2026-07-09 01:24:43 +03:00
ospab 992c212c76 feat(gui): make RTT and speed display optional 2026-07-08 23:59:35 +03:00
ospab 6361a87072 feat(gui): make RTT and speed display optional 2026-07-08 23:12:21 +03:00
ospab fbc37e9d39 fix(gui): fix UAC SmartScreen and scrolling UI layout 2026-07-08 22:50:21 +03:00
ospab db581ca391 fix(gui): fix UAC SmartScreen and scrolling UI layout 2026-07-08 22:45:03 +03:00
ospab a547ebff17 fix(ci): quote run-name — unquoted colon broke YAML parsing on every push
The run-name expression contains the GHA string literal 'Release build: {0}'
— an unquoted YAML plain scalar treats ": " as starting a nested mapping,
which invalidated the entire workflow file at parse time (before any job
runs). Every push since that line was introduced failed instantly with
"Invalid workflow file ... line 11", silently burning an Actions-minutes
run each time for nothing. Wrapping the whole expression in double quotes
fixes it — verified with `npx js-yaml` that the file now parses and the
run-name value round-trips intact.
2026-07-08 22:23:01 +03:00
ospab d065f6ceca chore: release 0.4.4-nightly on nightly 2026-07-08 19:13:55 +03:00
ospab d822f48891 refactor(config): one canonical config.json schema for client/server/relay
All three on-disk config.json shapes (client, server, relay) used to be
declared locally inside ostp/src/main.rs, invisible to any other consumer —
which is exactly how ostp_client::migrate ended up matching against loosely
typed serde_json::Value instead of a real schema, with no guarantee its
hand-built output actually matched what the CLI parser expected.

Moved every one of those definitions (AppMode, UnifiedConfig, ServerConfig,
RelayServerConfig, ClientFileConfig, TunConfig, ExcludeConfig, MuxConfig,
TransportConfigRaw, ApiConfig, FallbackCfg, ListenConfig, UserConfig) into
ostp_client::config — the same file that already held the runtime
ClientConfig/OstpConfig/etc. main.rs now imports them instead of
re-declaring them (`ClientFileConfig as ClientConfig` to avoid colliding
with the runtime ClientConfig, which stays separate on purpose: it's the
engine's internal shape — handshake/io timeouts and the like a user never
sets in config.json — built FROM one of these via the mapping in
run_client_directly, not the same thing).

ServerConfig.dns is now Option<serde_json::Value> rather than
Option<ostp_server::dns::DnsConfig> — ostp-client doesn't (and shouldn't)
depend on ostp-server just to name that type. main.rs, which already depends
on both crates, deserializes it right before handing it to run_server().

cmd_migrate now proves its output against this schema before ever writing
to disk (serde_json::from_value::<UnifiedConfig>(migrated)) — a migrator/
schema drift is now a hard error at migrate time, not a corrupted
config.json discovered later. Added a matching unit test
(every_migrated_output_matches_the_canonical_schema) that exercises this
same check on all three migration paths (modular, legacy-flat, server).
2026-07-08 18:58:34 +03:00
ospab 26665a826f feat(client): one authoritative config migrator, manual-only
- fix(cli): stop printing the startup banner ("ostp-cli vX.Y.Z | OS: ...")
  to stderr on every single command invocation. init_tracing() ran
  unconditionally before command dispatch, so `ostp -V`, `ostp gk`, etc. all
  showed it. It's still written to the log file (useful there), just no
  longer echoed via the stderr tracing layer for one-shot commands.

- feat(client): add ostp-client::migrate, the ONE place config migration
  runs. Previously there were three uncoordinated migration paths: a Python
  snippet embedded in scripts/install.sh (only touched server api.* fields,
  ran on every update), the old 0.3.x line's auto-migration on every hot
  reload (silent besides a log warning), and nothing at all for the current
  rebuild. Consolidated into one module covering every config shape that's
  actually existed:
    - v0.3.1-v0.3.21 modular (inbounds/outbounds/routing) -> current flat
      schema, including correctly resolving routing.default_outbound through
      a urltest/selector group to the real server, and reporting (not
      silently dropping) every additional server a multi-server config had.
    - pre-0.3.1 flat configs carrying now-dead fields (tun.wintun_path,
      tun.ipv4_address, transport.wss) -> dropped with an explicit reason,
      everything else passes through untouched.
    - server configs -> backfills api.* defaults and drops legacy api.token
      (ported straight from the install.sh Python, same behavior, correct
      place).
  6 unit tests cover all of the above against realistic fixtures. Wired up
  as `ostp migrate` (was missing from Commands entirely) — no other code
  path calls into this module, so a config's shape only ever changes when
  explicitly asked.

- feat(cli): `ostp import <url>` now asks the same TUN/mux/debug questions
  `ostp connect <url>` always did. Previously import just wrote flat
  defaults to disk with no way to turn any of that on short of hand-editing
  the resulting config.json afterward. Extracted the shared prompt into
  prompt_client_options() so both paths stay in sync.

- chore(install): remove the embedded Python config-migration snippet from
  install.sh; schema migration must never happen implicitly during an
  install/update. Points users at `ostp migrate` instead.
2026-07-08 18:45:04 +03:00
ospab 7b43e1dcf7 ci: prefix rolling-channel release tags with "v" for consistency
Stable releases were already "vX.Y.Z" (from the actual git tag pushed for
that channel), but nightly/beta tags were bare "X.Y.Z-nightly"/"X.Y.Z-beta"
— inconsistent with the v-prefixed convention used everywhere else. Every
channel's tag now starts with v: "vX.Y.Z-nightly", "vX.Y.Z-beta", "vX.Y.Z".
2026-07-08 18:20:45 +03:00
ospab b17e5499eb ci: fix run-name to say "channel" instead of a bare branch name
run-name showed "release version nightly" / "release version pre-release"
for every branch-push run — read exactly like the release TAG was bare
"nightly"/"pre-release" (the bug fixed earlier), when the actual GH Release
tag has been correctly versioned (e.g. "0.4.3-nightly") all along via
resolve-channel. run-name can't reference job outputs (it's evaluated before
any job runs), so it can't show the real computed tag directly — spell out
"channel" instead so the label can't be mistaken for the release tag again.
2026-07-08 18:18:11 +03:00
ospab ec947ec9d1 chore: release 0.4.3-nightly on nightly 2026-07-08 18:13:37 +03:00
ospab 0ec09d1311 ci: add scripts/gha.ps1 — versioned release cutter with channel memory
Replaces ad-hoc manual tag pushes (which is how the confusing v0.4.1-beta /
v0.4.2-beta / bare "nightly" / "pre-release" release mess happened) with one
script that always goes through the same path: bump every version manifest,
commit, and push in the way release.yml's resolve-channel job actually
expects (branch push for nightly/pre-release, a real "vX.Y.Z" tag for
master — never a hand-pushed "vX.Y.Z-beta"-style tag).

Remembers the last {version, branch, prefix} used in .release-state.json, so
a bare run repeats last time's channel with the patch version bumped, and
-Switch starts a new version line (e.g. 0.3.x -> 0.4.0) without disturbing
which channel is currently being released to.
2026-07-08 18:05:01 +03:00
ospab f81610f939 chore: bump version to 0.4.2 2026-07-08 17:28:14 +03:00
ospab 114011df5a docs: add commit conventions and branch strategy to CONTRIBUTING
- New "Commit Message Conventions" section (type(scope): summary + a body
  only when the why isn't obvious from the diff) — formalizes the style
  already used across this rebuild's history.
- New "Branch Strategy" section documenting the nightly -> pre-release ->
  master promotion model (pre-release/master are fast-forward-only,
  never committed to directly).
- Fixed PR/branch-creation instructions that still said "target master" /
  "branch from master" — contributor work targets nightly now.
- Clarified the ostp-control build step is optional for day-to-day
  core/client/server work (the server embeds a dummy dist/ otherwise).
2026-07-08 17:28:10 +03:00
ospab f96daaf57d feat(client): auto-reconnect on network change or any subsystem drop
run_client_core previously ran once: if the OSTP protocol connection, the
TUN device, or the local proxy listener ended for any reason (network
change stranding the socket/adapter on a dead interface, a transient
crash, a drop the inner Bridge-level "TunnelStopped" retry couldn't
recover from), the whole client returned/errored and just stayed down.

Wrapped the existing body (now run_client_once) in an outer supervising
loop: any non-shutdown-requested exit triggers a full clean restart —
fresh DNS resolution, fresh Bridge, fresh TUN/proxy — with backoff
(1/2/5/10/20/30s, resetting once a run has been stable for 60s). Only an
explicit shutdown request stops the loop. connection_state reports
"connecting" during the retry wait so the UI shows reconnecting, not
disconnected.
2026-07-08 17:28:05 +03:00
ospab 6929d42736 Polish docs/README to match v0.4.x: fix license mismatch, CLI, crypto docs
- README.md/README.ru.md: License section still said "Business Source
  License 1.1 ... converts to MIT in 2030" while the badge right above it,
  Cargo.toml, and LICENSE itself all say AGPL-3.0 — a direct contradiction.
  Now both say AGPL-3.0 and link to LICENSE.
- README.md/README.ru.md: CLI Reference / Quick Start described the old
  flag-based interface (--init, --check, --generate-key, --links, bare
  positional URL) that no longer exists after the subcommand refactor.
  Rewrote both to the current `ostp <command>` surface (run/connect/setup/
  init/check/gk/links/import/update/migrate/prober/proxy-env/uninstall),
  including gk's alias and update's --branch/--version. RU previously had
  no command reference at all; added one to match EN.
- docs/{en,ru}/obfuscation.md: removed the XTLS-Reality section (feature
  removed in §A) and replaced it with an accurate description of junk
  packets + TCP fragmentation, the actual current supplementary stealth
  mechanism, including the per-key junk marker (no global DPI signature).
  Also corrected the key-derivation and masking-algorithm descriptions,
  which described a much older scheme (SHA-256(access_key)[0..8] + static/
  nonce-based XOR) than what derive_all_secrets()/derive_payload_mask()
  actually implement now (HKDF with version-gated, domain-separated
  outputs; HMAC-SHA256 mask keyed on the packet's own ciphertext). The RU
  version was additionally rewritten out of an oddly formal "industrial
  telemetry" register into plain technical Russian.
2026-07-08 03:06:53 +03:00
ospab 5e0ff4a7ef Remove repo-root cruft: scratch files, dead migration script, stale wiki/config
- .ostp_public_ip: a server RUNTIME cache file (its own detected public IP),
  never meant to be tracked — got committed by accident from a dev run in
  the repo root. Removed and gitignored so it can't happen again.
- test.json (2 bytes), test_addr.rs (95 bytes): leftover scratch files with
  no references anywhere in code, CI, or docs.
- refactor.py: a one-off AST-surgery script hardcoding an absolute path to
  a specific dev machine (d:/ospab-projects/ostp/...); its job (splitting
  up bridge.rs::run()) is long done, and it isn't invoked by anything.
- server.json: duplicate/stale example config at repo root — the real
  canonical example already lives at docs/relay-config-example.json, and
  this one still had a "reality" section for the TLS-mimicry feature we
  removed in §A.
- ostp-wiki/: duplicate in-repo copy of wiki content. The old 0.3.x
  lineage already deleted this once ("remove useless ostp-wiki folder from
  root") before this rebuild branched off an earlier point that predates
  that cleanup — removing it here brings v0.4.x back in line with that
  decision.
2026-07-08 02:59:02 +03:00
ospab c330a0abe3 Fix UAC diagnostics parity, gk alias, flutter version, and versioned CI channels
- GUI launch_as_admin now matches the CLI's UAC diagnosis: detects
  ERROR_CANCELLED (1223, user declined the prompt) instead of silently
  treating it as success, and reports GetLastError()+exe path for any other
  ShellExecuteW failure, replacing the old single opaque "denied or missing"
  message that made GUI/TUI failures impossible to tell apart.
- generate-key subcommand renamed to `gk` (kept `generate-key` as an alias).
- Fixed a real short-flag collision: GenerateKey's --count used short='c',
  which collides with the global --config short (propagated into every
  subcommand); clap validates the whole command tree on first parse(), so
  this could break parsing for the entire CLI, not just generate-key/gk.
  --count is now short='n'.
- ostp-flutter/pubspec.yaml version was stuck at 0.2.97+12; bumped to 0.4.1+13.
- release.yml: added a resolve-channel job that computes one release tag per
  run instead of repeating the logic in five upload steps. Rolling channel
  pushes now carry the actual Cargo.toml version instead of a bare channel
  name: `{version}-nightly` for the nightly branch, `{version}-beta` for
  pre-release. workflow_dispatch gained a `channel` input restricted to
  nightly/beta only — a manual run can never accidentally publish a "stable"
  release; that still requires an explicit vX.Y.Z tag push.
2026-07-08 02:52:23 +03:00
ospab b2ee9eb010 Port CLI subcommands + multi-channel release onto the 0.4.0 rebuild
Brings the useful work from the old master/pre-release lineage (5c2b5a0)
onto the clean rebuild, since that lineage never had the multi-server/WSS/
Reality removal or any of the 0.4.0 stability work. This is a manual port,
not a cherry-pick — this file's Args/ClientConfig shape had already
diverged too much for the patch to apply mechanically.

- ostp/src/main.rs: flat Args -> clap subcommands (setup, init, generate-key,
  links, check, connect, uninstall, update, import, proxy-env,
  proxy-env-clear), bridged onto the existing ~500 lines of flag-driven
  dispatch via a LegacyArgs struct so none of that logic had to change.
- Fixed a real bug found while porting: GenerateKey's `count` used
  short='c', colliding with the global `--config` short (also 'c'), which
  clap validates across the whole command tree on first parse() -- a
  duplicate short flag there could break every subcommand's parsing, not
  just generate-key's.
- `update` now takes `-b/--branch` and `-v/--version` explicitly (was a
  bare flag with no way to target a channel or exact version).
- Added the UAC elevation step for TUN mode that this lineage's CLI was
  completely missing (`run_client_directly` went straight to creating the
  TUN adapter unelevated). Also fixed the elevation check itself: it only
  tested `ret <= 32`, but ShellExecuteW returns ERROR_CANCELLED (1223) when
  the user clicks "No" on the UAC prompt -- > 32, so a denied prompt was
  read as success and the process exited silently without starting the
  tunnel. Now ret==1223 is reported explicitly, and a genuine failure logs
  GetLastError() so the real Win32 cause is visible next time.
- scripts/install.sh: added -b/--branch alongside the existing -v/--version,
  and channel-aware release resolution (nightly/pre-release use their own
  rolling tag; stable resolves via the GitHub API's "latest").
- release.yml: added nightly/pre-release branch-push triggers for rolling
  prereleases. Fixed the tag_name logic from 5c2b5a0, which mapped
  `master` pushes to a release tagged "nightly" -- backwards from master
  being the most stable channel. github.ref_name already equals the
  branch or tag name that triggered the run, so no per-branch remapping is
  needed at all; master is intentionally left off the branch-push list --
  it only ever gets real version tags.

Verified: `cargo build -p ostp` succeeds, and `ostp update --help` /
`ostp generate-key --help` / `ostp --help` show the expected flags with no
clap panic.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-07-08 01:38:44 +03:00
ospab 8ca411a64b Fix github actions: remove working-directory for ostp-control to avoid failure when absent 2026-07-07 18:04:21 +03:00
ospab dbd4ebc4e3 Fix linux build for beta 0.4.1 2026-07-07 17:10:25 +03:00
ospab acab38c551 0.4.1: per-key junk marker, GUI polish, pre-release pipeline
security / protocol:
- Derive a PER-KEY junk marker (obfuscation.rs, info byte 0x04) instead of the
  global constant [0x88,0x1A,0x93,0x5D]. A fixed marker was a universal DPI
  signature identifying ALL OSTP users at once — exactly what the HKDF version
  gate avoids for the handshake. Server drops junk via a new DispatchOutcome::Junk
  inside the existing key-trial loop (secrets already derived → zero extra cost);
  client stamps its own key's marker.
- §E: configurable junk/fragmentation params (junk_pc / junk_ps / frag_chunk / frag_sleep).

GUI (desktop):
- Light theme + toggle, GUI version footer in Settings.
- Fix mouse-wheel scroll on Settings (flex child needed min-height: 0).
- Drop the false "process exclusions unsupported in TUN mode" warning — they DO
  work (native_handler maps port->process via GetExtendedTcpTable).

release / infra:
- build.ps1: add -PreRelease (tag CURRENT version as v<ver>-beta.N, no bump, no
  master commit); guard the panel build when ostp-control ships no source; bump
  the real ostp-gui/package.json instead of the nonexistent ostp-control one.
- release.yml: mark hyphenated tags as GitHub pre-releases; don't hard-fail the
  web-panel step when there is no source (use committed dist/).
- Versions aligned to 0.4.1; README license badge BSL 1.1 -> AGPL v3.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-07 16:33:57 +03:00
ospab e1bf18e653 core_fixes 2026-06-28 17:11:27 +03:00
ospab 39127d30f3 §B: fix Closing-state teardown data loss (from 47d44fa)
In the Closing state the old code force-transitioned to Closed after a
SINGLE inbound packet, so any data/ACKs the peer still had in flight when we
initiated Close were dropped (Closed returns Noop for everything). Stay in
Closing and process inbound normally; handle_inbound already owns the
Close->Closed transition when it actually receives the peer's Close frame.
Also handle Tick in Closing so our own Close frame is retransmitted until
acknowledged.

Ported surgically from 47d44fa — only the Closing-state correctness fix, NOT
that commit's bundled RFC-6298 RTO / congestion rewrite (a behavioural change
to the working base) or the sent_history BTreeMap perf swap (broad hot-path
change for a perf-only gain). cargo test -p ostp-core: 36/36 incl.
test_close_sequence.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-28 01:44:37 +03:00
ospab 5c9ec89821 §F: GUI start_tunnel tears down existing tunnel first (stop+start)
Previously start_tunnel returned early if a tunnel was already running, so
changing the server while connected silently kept the OLD connection. Per
the plan ("server change = full stop+start, not hot-reload"), tear down any
existing InProcess/Helper tunnel before starting a fresh one. For the
elevated helper, wait ~1.2s after sending stop so it releases the ostp_tun
adapter before a new helper recreates it (avoids name clashes). start_tunnel
is only invoked on an explicit connect, so restarting here is safe.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-27 21:40:09 +03:00
ospab 4f7f1ca838 §F: desktop Share — local QR + copyable ostp:// link
The plan's share feature for the (single-config) desktop GUI. The QR is
rendered locally so the access key never leaves the device.

- src-tauri: add `qrcode = "0.14"` (features=["svg"]) + `generate_qr`
  command (string -> SVG), registered in the invoke handler. Ported from
  the current ostp-gui. (cargo check on src-tauri passes.)
- Frontend: "Share" button next to Import builds `ostp://KEY@HOST?sni&type`
  from the current config fields, calls generate_qr, and shows a modal with
  the QR + a read-only link + Copy. Added i18n keys (en/ru) so the new
  data-i18n labels resolve (missing keys would render as the raw key).

Note: Rust side verified via cargo check; the frontend is syntax-checked
(node --check) but not runtime-verified — needs a Tauri build to confirm
visually.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-27 21:38:21 +03:00
ospab 2a9b099b24 §H/§F: port eagle watermark branding into the desktop GUI
Brand identity per the plan (dark theme + eagle). Ported faithfully from the
current ostp-gui:

- assets/logo.svg + logo.png (the eagle vector/raster).
- .watermark layer: centered, 80%/max-600px, opacity 0.05, behind the
  screens (z-index 0, pointer-events none), with a light-theme invert.
- Watermark div added after the ambient blobs in index.html.

Dark theme already existed in styles.css; this adds the eagle the plan calls
for. Backend-independent (pure HTML/CSS/asset).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-27 21:28:46 +03:00
ospab db65e3367f §E (base): port junk packets + TCP fragmentation (stream-only)
Anti-DPI obfuscation the project wants to keep, ported from 0.3.x with the
harmful UDP behaviour designed out from the start.

- Junk: before the handshake on a UoT/TCP connection, send 2-5 random
  length-prefixed frames (100-1000 B). The server reads each as a frame,
  fails to authenticate it, drops it and keeps reading (drop-and-continue),
  so junk perturbs DPI flow analysis without breaking the connection. Junk
  is NEVER sent over UDP — there each junk would be a lone datagram
  indistinguishable from a port scan (probe-flood / wasted CPU / the very
  "self-ban" risk the plan calls out). Verified the server has no
  probe-based ban, and the unauthorized-probe log is already rate-limited
  (§B), so junk-over-UoT produces one debug line, not a flood.
- TCP fragmentation: new `transport.tcp_fragmentation` flag (default off).
  When set, the writer splits the first real frame (the handshake) — length
  header byte-by-byte then payload in 2-byte chunks with short gaps — so DPI
  can't classify the handshake from a single read.
- Ranges are hardcoded for now; §E fine-tuning (configurable Jc/Jmin/Jmax,
  S1/S2, H1..H4) is deferred.

Verified by loopback E2E: a UoT client with tcp_fragmentation=true connects
(junk logged as one rate-limited probe, then real handshake accepted) and
curl via SOCKS5 tunnels HTTPS successfully.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-27 17:11:12 +03:00
ospab 89a5eea20d §C: protocol version gate via key derivation (reject old clients)
The base already derives all secrets from the access key, so derived
secrets were never the gap — the gap was that nothing distinguished a
current handshake from an older-format one, so an old client could still
connect to a new server.

Rather than the plan's literal "plaintext version byte before the crypto
layer" (which would add a constant, DPI-visible marker and defeat the
project's stealth north-star), fold the version INTO the HKDF derivation:

- Add PROTOCOL_VERSION (= 4 for 0.4.0), mixed into the IKM of
  derive_all_secrets so a different version yields a completely different
  obfuscation key / psk / padding. No marker ever appears on the wire —
  the output stays indistinguishable from random.
- A pre-0.4.0 peer derives a different obfuscation key, so the 0.4.0
  server cannot recover its handshake header and drops it as an
  unauthorized probe. Bump PROTOCOL_VERSION on any future wire break.

Verified:
- cargo test -p ostp-core: 36/36 incl. new test_protocol_version_gates_
  old_clients (old-version obf key does NOT recover the session_id).
- Loopback E2E: new client <-> new server connects and tunnels HTTPS
  (curl via SOCKS5 returns egress IP).
- Old v0.2.98 client vs new server: handshake times out / aborts, server
  accepts 0 clients — exactly the plan's §C criterion.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-27 16:57:23 +03:00
ospab 38f2d9e659 §B: port stability fixes from 0.3.x onto the clean base
Ported only the fixes that actually apply to the pre-refactor base
(the handshake fixes 6eb7b36/d65af35 fix bugs the 0.3.1 multi-server
refactor introduced into the new outbounds/ostp.rs; the base bridge.rs
already waits for the handshake response with retransmit + NAT64
fallback, so they are intentionally skipped).

- EMFILE (922cf0b): rlimit::increase_nofile_limit at CLI startup.
- Logs (1151726): UoT connect/disconnect → debug; rate-limit the
  unauthorized-probe log to one line / ~30s so a junk/probe flood can't
  spam the log (and a client running junk-over-UDP can't self-ban).
- Helper lifecycle + bypass routes (b6e78c1):
  * ostp-tun-helper forces std::process::exit(0) after run_server so the
    WinTun adapter and its metric-0 default route are reclaimed instead
    of lingering as a zombie that breaks the next connect.
  * windows_route: delete_routes_for_dest() purges stale /32s, dedupe
    bypass IPs, and log add failures at warn!.
  * windows: retry tun::create through the transient ERROR_INVALID_
    PARAMETER window and widen the adapter-index lookup to ~15s.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-27 16:30:42 +03:00
ospab 7704e0bdb1 §A.3: remove dead tun2socks code (native OSTP TUN is the only path)
The "system" TUN stack that shelled out to a bundled tun2socks binary is
long-dead — Flutter already hardcodes the native OSTP stack — and only
bloats the build.

- ostp-jni: drop the tun2socks spawn branch and the tun_child handle;
  the native TUN (run_native_tunnel_from_fd) is now unconditional. The
  JNI signature is kept ABI-stable (t2sBinPath/localProxy retained but
  ignored) to avoid breaking the Kotlin linkage without an Android build.
- Delete the committed 10 MB tun2socks-arm64 asset; drop the tun2socks
  download steps from the Android build scripts.
- Remove the dead tun2socks.exe entry from the desktop build_dist.js
  (it required a file nothing downloads, breaking the GUI dist build).
- Reword stale tun2socks references in proxy.rs, the GUI config comment,
  install.ps1, the release workflow matrix, and CONTRIBUTING.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-27 16:29:55 +03:00
ospab 92d6b06d75 §A: remove WSS + Reality (TLS-mimicry); bump to 0.4.0 / AGPL-3.0
Clean-rebuild on the stably-working v0.2.98 base. The project's stealth
path is zapret-like (packet obfuscation / junk / fragmentation), NOT
TLS-mimicry, so WSS and Reality are dropped entirely.

- Delete dead orphan files: ostp-client/src/transport/xhttp.rs and
  ostp-core/src/crypto/reality.rs (never declared as modules → not even
  compiled), plus ostp-core/src/framing/wss.rs.
- Scrub the `wss` transport field from client config/bridge, the unified
  CLI (ostp/src/main.rs), the Tauri GUI backend, the GUI frontend
  (index.html/main.js), and the Flutter UI; also drop the Reality
  pbk/sid plumbing and XTLS auto-search modes from both frontends.
- Drop now-unused client deps (x25519-dalek, chacha20poly1305, hex).
- Bump workspace to version 0.4.0 and license AGPL-3.0; make ostp's
  ostp-core dep path-only so the version bump resolves.
- gitignore ostp-control/ (panel assets built separately; a dummy dist
  is created for the rust-embed build).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-27 16:29:42 +03:00
ospab 31d0020483 CI/CD: release version v0.2.98 2026-06-16 14:21:02 +03:00
ospab 04761fb6a3 Fix memory leaks, hang issues, gui helper token vulns, and log spam 2026-06-16 14:11:37 +03:00
ospab feaac0c713 CI/CD: release version v0.2.97 2026-06-14 01:49:53 +03:00
ospab b841053628 fix(jni): add exclusions_rx param to run_native_tunnel_from_fd stub to fix non-Android builds 2026-06-14 01:49:06 +03:00
ospab cf92089005 CI/CD: release version v0.2.96 2026-06-14 01:46:14 +03:00
ospab e0a13702ea fix(tun): resolve OS error 10049 for TCP bypass on Windows and fix 16GB memory leak by bounding smoltcp channels 2026-06-14 01:44:56 +03:00
ospab c36e7373e8 fix(tun): hide verbose split tunneling logs behind debug flag 2026-06-14 01:34:34 +03:00
ospab 3671a83971 chore(tun): add verbose logging for TCP and UDP split tunneling bypass 2026-06-14 01:26:34 +03:00
ospab c7bca41616 chore: fix JNI UoT handler args, center Flutter home screen metrics, update READMEs 2026-06-14 01:04:50 +03:00
ospab 486d745d47 feat(tun): implement process bypass for TCP/UDP and IP bypass for UDP using existing Extended tables 2026-06-14 00:02:08 +03:00
ospab 74b6648db1 fix(tun): fix bypass loop by capturing physical iface before tun route overrides 2026-06-13 23:09:33 +03:00
ospab 4543fa82f8 fix(split-tunnel): hot-reload exclusions into running proxy tunnel without reconnect 2026-06-13 22:30:01 +03:00
ospab 83ba39e59a feat(gui): split tunneling — tag-chip UI, process picker with live process list 2026-06-13 02:55:28 +03:00
ospab 533466b63a CI/CD: release version v0.2.95 2026-06-13 02:45:40 +03:00
ospab 6dee7613a5 CI: Add step to create dummy dist directory for rust-embed during check-and-test 2026-06-13 02:44:01 +03:00
ospab 4c0263f7f7 CI/CD: release version v0.2.94 2026-06-13 02:34:06 +03:00
ospab 4d228cf1e1 CI/CD: release version v0.2.93 2026-06-13 02:32:23 +03:00
ospab 55215567dd Fix all compilation errors and suppress all warnings across workspace 2026-06-13 02:30:57 +03:00
ospab ab8d2c2185 CI/CD: release version v0.2.92 2026-06-13 02:28:22 +03:00
ospab 875177f779 CI/CD: release version v0.2.91 2026-06-13 02:23:24 +03:00
ospab 2a24ac34d0 Remove Reality/XTLS from all UI components and TSX pages (Dashboard, Settings, Tools) 2026-06-13 02:19:53 +03:00
ospab 8fc61f986f CI/CD: release version v0.2.90 2026-06-13 02:12:51 +03:00
ospab ee6768dee1 CI: restore run-name format, add check-and-test gate before all builds 2026-06-13 02:04:03 +03:00
ospab 091bb2c707 CI/CD: release version v0.2.89 2026-06-13 02:00:26 +03:00
ospab 2d05fb282d CI/CD: release version v0.2.88 2026-06-13 01:58:32 +03:00
ospab 3c54aba63f Remove Reality/XTLS UI from ostp-gui, ostp-flutter, ostp-control 2026-06-13 01:57:20 +03:00
ospab a9e4511190 Fix CLI setup permissions, enforce global debug tracing, and fix GUI silent startup crash 2026-06-13 01:25:54 +03:00
ospab fbf13b86f3 Fix syntax and type errors after DNS removal 2026-06-10 22:59:10 +03:00
ospab 9f35caf4ca Remove built-in DNS server and owndns features 2026-06-10 22:52:35 +03:00
ospab 7bb7d211fa Remove stealth_port entirely and integrate fallback into UoT HTTP handler 2026-06-10 02:26:13 +03:00
ospab 430ab8a743 CI/CD: release version v0.2.87 2026-06-09 01:02:11 +03:00
ospab 04c31c7f53 feat: implement wintun dynamic downloading, add missing driver frontend modal, fix background logging and UAC helper issues 2026-06-09 01:01:36 +03:00
ospab 60282d730f CI/CD: release version v0.2.86 2026-06-07 21:05:23 +03:00
ospab da238fad5c fix(client): fix compilation error on linux due to server_ip_str 2026-06-07 21:03:52 +03:00
ospab 85f0cb19cf CI/CD: release version v0.2.85 2026-06-07 20:44:28 +03:00
ospab c95720f3da CI/CD: release version v0.2.84 2026-06-07 20:10:39 +03:00
ospab 730eab8553 feat: implement built-in DNS server, adblock and dns leak prevention 2026-06-07 19:55:42 +03:00
ospab 4d0249e8ef CI/CD: release version v0.2.83 2026-06-06 20:57:46 +03:00
ospab 4cd7321cc2 CI/CD: release version v0.2.82 2026-06-03 19:52:21 +03:00
ospab fe1333621b CI/CD: release version v0.2.81 2026-06-03 02:59:35 +03:00
ospab 8dbf52cba3 CI/CD: release version v0.2.80 2026-06-03 02:08:55 +03:00
ospab 29e9ef739c Refactor: Phase 1 and 2 - Async architecture, JNI fixes, SmolTCP data races, and Tunnel optimizations 2026-06-03 02:06:06 +03:00
ospab 84797f55ab CI/CD: release version v0.2.79 2026-06-03 01:19:25 +03:00
ospab 53ce4f21a0 CI/CD: release version v0.2.78 2026-06-03 01:02:10 +03:00
ospab ca9dd9ec06 fix(gui): remove tun stack selection, default to ostp; fix(flutter): add missing ic_launcher_background.xml for icon build 2026-06-02 23:27:04 +03:00
ospab f9e272d6bf chore: apply icon variant 2 (infinity tunnel) to tauri and flutter 2026-06-02 23:12:47 +03:00
ospab ee539ea4a6 fix(gui): add tray-icon feature and missing Emitter import 2026-06-02 23:05:32 +03:00
ospab 5952fbe3cc fix: rename WindowsProxyGuard to SystemProxyGuard in bridge.rs 2026-06-02 23:01:45 +03:00
ospab dfbaff2c21 ci: add linux and macos gui build matrices 2026-06-02 23:01:22 +03:00
ospab c2bc764613 feat: linux auto-sudo and tauri system tray background mode 2026-06-02 22:58:04 +03:00
ospab 0951afa499 feat(linux): implement SystemProxyGuard with GNOME/KDE support and headless proxy prompt 2026-05-31 21:01:28 +03:00
ospab ba5fe72873 feat(cli): add --import, --proxy-env, interactive link prompt, and TUN safety guard for Linux 2026-05-31 20:53:54 +03:00
ospab eb0a193fee fix(flutter): enforce MTU 1280 for Android TUN while passing 1140 to Rust core for TCP MSS clamping 2026-05-30 22:40:03 +03:00
ospab 95e72f6136 fix: remove IPv6 from Android TUN to allow MTU < 1280 and prevent crashes 2026-05-30 22:31:24 +03:00
ospab 472fb8dc11 feat: user configured MTU automatically subtracts 48 for overhead compensation 2026-05-30 22:24:08 +03:00
ospab 8825cf0838 fix: resolve deadlock, multiplexing backpressure, and LTE fragmentation issues 2026-05-30 22:21:12 +03:00
ospab 0fdea7ee21 fix(client): resolve borrow after move error in bridge.rs and clean up warnings 2026-05-30 22:09:23 +03:00
ospab 9f143f730a fix(client): send immediate Ping on connection to avoid 60s delay in UI 2026-05-30 22:07:22 +03:00
ospab 355a9f789a fix(client): remove IPv6 DNS servers from Android VPN to prevent DNS failures on IPv6-preferred LTE networks when server lacks IPv6 2026-05-30 22:05:11 +03:00
ospab 53132036c5 fix(client): flush stale proxy_rx messages on background reconnect to prevent UDP burst drops on mobile networks 2026-05-30 21:55:33 +03:00
ospab 95a36e2bdf Patch netstack-smoltcp locally to fix catastrophic UDP tunnel stream crash on invalid packets 2026-05-30 21:34:31 +03:00
ospab 9095f0dacd CI/CD: release version v0.2.77 2026-05-30 21:15:20 +03:00
ospab a82c664e5b Fix UDP IPv4-mapped IPv6 address matching bug and completely remove tun2socks 2026-05-30 21:14:29 +03:00
ospab 4f34f7f19c fix(client): make Android TUN read loop resilient to EINTR, don't abort tunnel on transient read errors 2026-05-30 02:35:14 +03:00
ospab f20618400e CI/CD: release version v0.2.76 2026-05-30 02:13:29 +03:00
ospab 38f1752fda fix(client): stabilize UDP sessions - prevent crashes on transient recv errors in udp_nat and proxy 2026-05-30 02:12:15 +03:00
ospab 6b58e0e8f3 fix(client): fix async closure compilation error in udp_nat.rs 2026-05-30 02:03:56 +03:00
ospab 6fa6170c75 fix(client): bind SOCKS5 UDP socket to IPv6 properly, and fix 100% CPU spin in Android TUN reader 2026-05-30 02:01:31 +03:00
ospab 02de5456aa fix(client): correctly parse ATYP in SOCKS5 UDP ASSOCIATE response to fix DNS/UDP on IPv6 networks 2026-05-30 01:52:25 +03:00
ospab b67bd18eee fix(client): prevent TUN read loop from crashing on invalid IP packets (fixes LTE MTU/CLAT issues) 2026-05-30 01:42:18 +03:00
ospab 5ce4ed559a CI/CD: release version v0.2.75 2026-05-30 01:40:52 +03:00
ospab f7cc555567 fix(build): remove ignored ostp-brain from Cargo.toml members to fix Github Actions 2026-05-30 01:33:34 +03:00
ospab e27378574c CI/CD: release version v0.2.74 2026-05-30 01:14:33 +03:00
ospab 902e762c91 fix(xhttp): rewrite RealityStream buffering to prevent packet drops and data loss 2026-05-30 01:10:29 +03:00
ospab 7257da174a fix(client/mobile): resolve fdsan crash and mobile network proxy issues, add auto config UI 2026-05-30 00:54:46 +03:00
ospab 585c74556e CI/CD: release version v0.2.73 2026-05-29 17:37:33 +03:00
ospab 0a022a4763 feat(ui): decouple WSS from UoT and add standalone Reality toggle
Extracted the WSS toggle from the UoT stealth block to make it
accessible regardless of transport mode. Added a dedicated XTLS-Reality
toggle to avoid relying on empty/non-empty PBK strings to determine
the enabled state, allowing users to toggle Reality without wiping keys.
2026-05-29 17:36:31 +03:00
ospab f88de11d98 CI/CD: release version v0.2.72 2026-05-29 17:29:06 +03:00
ospab 907d03ca38 fix(android): protect xhttp TCP socket from VPN routing loop
When using xhttp (UoT) mode on Android, the underlying TcpStream was
not protected with VpnService.protect(fd). This caused the TCP connection
to be routed back into the TUN interface, creating an infinite routing
loop and failing the connection immediately.

Added Android-specific socket protection to the TcpStream in connect_xhttp.
This fixes xhttp/UoT mode on mobile networks.
2026-05-29 17:27:50 +03:00
ospab 6d8e5dd68d CI/CD: release version v0.2.71 2026-05-29 16:42:05 +03:00
ospab af7e148874 fix(workspace): remove missing ostp-prober member from workspace 2026-05-29 16:41:34 +03:00
ospab 2f15a90f15 CI/CD: release version v0.2.70 2026-05-29 16:23:12 +03:00
ospab 7986b1ca5b fix(reality): fix TLS 1.3 handshake causing 1KB DPI cutoff on mobile
The core bug: server sent 5 TLS records in server_hello but client only
read the first one (ServerHello), then passed remaining bytes (CCS + fake
records) into RealityStream. RealityStream saw 0x14 (CCS) != 0x17 and
immediately returned an error, killing the connection.

Changes:
- reality.rs: append ChangeCipherSpec after ClientHello (RFC 8446 D.4)
  export REALITY_SERVER_HANDSHAKE_RECORDS=5 constant
- xhttp.rs: drain all 5 server handshake records before creating RealityStream
- uot.rs: rebuild server_hello as proper 5-record TLS 1.3 flight:
  ServerHello + CCS + fake EE (108B) + fake Cert (812B) + fake Fin (52B)
  drain client CCS from raw stream before wrapping in RealityStream
2026-05-29 16:21:59 +03:00
ospab cd218c9cf8 CI/CD: release version v0.2.69 2026-05-29 15:19:51 +03:00
ospab 8577824a3f docs: update obfuscation docs with XTLS-Reality 2026-05-29 15:02:39 +03:00
ospab 7656f3a3ce feat: implement custom Reality protocol with ChaCha20Poly1305 and X25519 2026-05-29 15:00:17 +03:00
ospab f4830f043f feat: implement optional WSS framing for DPI bypass & extract framing logic 2026-05-29 13:59:59 +03:00
ospab 2870569c55 chore: reduce client and server logging verbosity for outbound datagrams and relays 2026-05-29 00:37:08 +03:00
ospab 8cfb7e9c17 docs: add CONTRIBUTING guide in English and Russian, link in README 2026-05-29 00:25:40 +03:00
ospab 0ef43bb823 CI/CD: release version v0.2.68 2026-05-29 00:18:47 +03:00
ospab ba71af2abb feat: implement split-tunneling bypass for TCP/UDP and native UDP NAT 2026-05-29 00:06:11 +03:00
ospab 6a685f8226 CI/CD: release version v0.2.67 2026-05-28 23:18:21 +03:00
ospab da06cbc8f3 CI/CD: release version v0.2.66 2026-05-28 19:43:56 +03:00
ospab 4650947b00 Fix E0728: cannot await inside or_else closure in relay.rs 2026-05-28 19:39:07 +03:00
ospab 4ee2007754 CI/CD: release version v0.2.65 2026-05-28 19:33:33 +03:00
ospab cb797c42d0 Add 'Use Built-in' DNS button in GUI 2026-05-28 19:31:06 +03:00
ospab 0334322aae Fix Speedtest disconnects and Discord WebRTC 2026-05-28 19:25:06 +03:00
ospab 2ba9a3694d Fix UDP over XHTTP and intercept 10.1.0.1 for panel.ostp 2026-05-28 19:13:39 +03:00
ospab fe5db7cb10 CI/CD: release version v0.2.64 2026-05-28 18:51:54 +03:00
ospab ebbe96e4e1 fix(client): prefer IPv6 on Android to support NAT64 mobile networks 2026-05-28 18:51:30 +03:00
ospab 57a5464103 CI/CD: release version v0.2.63 2026-05-28 18:21:12 +03:00
ospab 1b836b26ab Fix Windows TUN NLA delays, UI timer, and Android UDP DNS resolution 2026-05-28 18:19:01 +03:00
ospab a0292b6087 CI/CD: release version v0.2.61 2026-05-28 16:41:04 +03:00
ospab 36ef6f2d04 Fix Windows TUN routing loop for SIM modems (0.0.0.0 NextHop) 2026-05-28 16:40:49 +03:00
ospab 5fa957830c Fix frontend ignoring tunnel errors & fix blocking wintun routines 2026-05-28 16:32:59 +03:00
ospab c13642fa3b CI/CD: release version v0.2.60 2026-05-28 15:31:00 +03:00
ospab 3c687aad46 Fix Tauri RealityConfig init 2026-05-28 15:30:48 +03:00
ospab f90607e471 CI/CD: release version v0.2.59 2026-05-28 15:19:18 +03:00
ospab aeba340405 Upgrade Flutter to 3.41.6 in CI to support Kotlin DSL 2026-05-28 15:19:06 +03:00
ospab ddb9ac2123 CI/CD: release version v0.2.58 2026-05-28 15:06:40 +03:00
ospab 360f84e5bd Fix Android rust_target matrix variable 2026-05-28 15:06:22 +03:00
ospab c7a614958e CI/CD: release version v0.2.57 2026-05-28 15:02:01 +03:00
ospab 33145febbb Fix Tauri build args, split Android into matrix, track flutter/gui, update docs and contacts 2026-05-28 15:01:41 +03:00
ospab 6d9b7d8a26 CI/CD: release version v0.2.56 2026-05-28 14:54:17 +03:00
ospab 532bdc7e76 Update GUI builds to output dual architectures 2026-05-28 14:51:58 +03:00
ospab 7bc31d2bac CI/CD: release version v0.2.55 2026-05-28 14:48:37 +03:00
ospab 25fa74eab6 Merge GUI jobs into release.yml and remove bare Android build 2026-05-28 14:48:26 +03:00
ospab d8d3e858e9 CI/CD: release version v0.2.54 2026-05-28 14:40:04 +03:00
ospab 19f2c36400 Fix STUN bug, improve DNS in TUN, fix config gen, add GHA for clients 2026-05-28 14:39:42 +03:00
ospab 543e36e60e Add session id mismatch error trace 2026-05-28 13:49:33 +03:00
ospab 54fdd444c9 feat: enforce internal DNS on client and restore DNS interception on server
- Flutter: Hide 'DNS Server' field and force '10.1.0.1' if connection link contains owndns=true
- Flutter: Remove 'Use Provider DNS' toggle to eliminate client-side choice
- Server (relay.rs): Intercept DNS queries targeting '10.1.0.1:53' and process them via internal DnsServer if DNS is enabled
- Server (api.rs): Continue appending owndns=true to subscription links to enforce internal DNS logic on clients
2026-05-28 13:18:56 +03:00
ospab cbdb20402d CI/CD: release version v0.2.53 2026-05-28 12:30:28 +03:00
ospab 18899db1b2 fix: remove DNS interception on server, fix TUN routing on Windows and Linux
- ostp-server/relay.rs: remove DNS port 53 interception — DNS queries
  now pass through to the actual DNS server as regular TCP connections
- ostp-client/native_handler.rs (Windows): add explicit gateway/32 route
  via real interface BEFORE setting default route via TUN to prevent loop
- ostp-client/native_handler.rs (Linux): properly detect real gateway and
  add default route via TUN with metric 10 after server IP exclusion
- Remove redundant extra DNS host routes from Windows setup script
2026-05-28 12:30:06 +03:00
ospab db1f8a5b89 CI/CD: release version v0.2.52 2026-05-28 01:39:52 +03:00
ospab d63c039181 fix(client): proxy UDP DNS over TCP via local socks5 2026-05-28 01:39:20 +03:00
ospab 05d4fe166c CI/CD: release version v0.2.51 2026-05-28 01:29:12 +03:00
ospab 5c39f24bee fix(server): return API token support for Relay servers sync 2026-05-28 01:28:29 +03:00
ospab 3b88359746 CI/CD: release version v0.2.50 2026-05-28 01:09:45 +03:00
ospab 4155e48224 fix(client): resolve server domain to IP before starting TUN to prevent DNS deadlock on reconnects 2026-05-28 01:09:10 +03:00
ospab 6d57b3ef00 CI/CD: release version v0.2.49 2026-05-28 00:29:37 +03:00
ospab 38c4f242e4 fix: include owndns and transport type in --links output 2026-05-28 00:28:54 +03:00
ospab 13128c510a CI/CD: release version v0.2.48 2026-05-27 23:44:16 +03:00
ospab d018d68b79 fix: make handle_subscribe future Send by scoping RwLockReadGuard 2026-05-27 23:38:32 +03:00
ospab 3920665d89 CI/CD: release version v0.2.46 2026-05-27 22:50:27 +03:00
ospab d8930fd96a fix: Persist DNS configuration to config.json 2026-05-27 22:49:28 +03:00
ospab 43d28b2c81 CI/CD: release version v0.2.45 2026-05-27 22:24:39 +03:00
ospab cea8ebaa5c feat: Built-in DNS Server with AdBlock and DoH proxy 2026-05-27 22:23:06 +03:00
ospab ba1a5cd16c CI/CD: release version v0.2.44 2026-05-27 18:17:37 +03:00
ospab 9ac0908c1e fix(server): generate correct public IP for client configs instead of 0.0.0.0 2026-05-27 18:17:11 +03:00
ospab ac91665263 CI/CD: release version v0.2.43 2026-05-27 00:18:29 +03:00
ospab 2bff6623d9 feat: migrate TUN tunnel to native in-process smoltcp and refactor Android JNI layer 2026-05-27 00:17:19 +03:00
ospab 85bac8f70a CI/CD: release version v0.2.42 2026-05-26 23:25:50 +03:00
ospab 800c07de5d perf: increase backpressure limit to 16384 and reduce retransmit tick to 10ms for multi-gigabit speeds 2026-05-26 23:21:33 +03:00
ospab 8e7c1e58e6 CI/CD: release version v0.2.41 2026-05-26 22:28:01 +03:00
ospab 55912832bf fix: use proper axum 0.8 wildcard syntax to fix runtime panic 2026-05-26 22:27:13 +03:00
ospab b46be0d4be CI/CD: release version v0.2.40 2026-05-26 22:18:06 +03:00
ospab 24aa6dc0b2 fix: redirect exact webpath to trailing slash and fix empty webpath static handler prefix 2026-05-26 22:17:27 +03:00
ospab 44bc2339d0 fix: detect real public IP for panel URL output 2026-05-26 22:08:51 +03:00
ospab def11a631c feat: prompt panel setup during update if not configured 2026-05-26 22:03:12 +03:00
ospab 49c3bce029 fix: config migration uses hardcoded field injection, no ostp --init; fix init template api fields 2026-05-26 21:59:41 +03:00
ospab 04dc133453 feat: auto-migrate config on update — add new fields, preserve existing data 2026-05-26 21:50:47 +03:00
ospab 352253b95f CI/CD: release version v0.2.38 2026-05-26 21:45:28 +03:00
ospab 07ee8e85fe CI/CD: release version v0.2.37 2026-05-26 21:40:45 +03:00
ospab d738caaaa1 fix: add ostp-control frontend to repository 2026-05-26 21:39:44 +03:00
ospab d3a07f3d32 CI/CD: release version v0.2.36 2026-05-26 21:31:17 +03:00
ospab 7f499d6263 feat: embed web panel via rust-embed with login page and custom webpath 2026-05-26 21:30:49 +03:00
ospab 8c03903524 CI/CD: release version v0.2.35 2026-05-26 20:55:12 +03:00
ospab abcb8999ce fix: integrate BBR cwnd for bufferbloat and relax mobile timeouts 2026-05-26 20:54:30 +03:00
ospab 9c59cabfc7 fix: ostp --update uses correct install URL; api returns name in user list 2026-05-26 20:24:33 +03:00
ospab 89380ef70b CI/CD: release version v0.2.34 2026-05-26 20:22:34 +03:00
ospab 3564747c1b CI/CD: release version v0.2.33 2026-05-26 20:05:41 +03:00
ospab 46c1ac4519 feat: add --uninstall and --update CLI commands 2026-05-26 20:05:23 +03:00
ospab 4ab0f04a1b CI/CD: release version v0.2.32 2026-05-26 19:58:38 +03:00
ospab 097a67e214 Fix axum duplicate route panic on server startup 2026-05-26 19:55:55 +03:00
ospab f65fce3144 Add relay mode initialization option to Linux installer 2026-05-26 19:45:21 +03:00
ospab 65baa4ed7e CI/CD: release version v0.2.31 2026-05-26 19:40:05 +03:00
ospab cba7be4b75 Implement config management API, token generation, and update wiki 2026-05-26 19:33:45 +03:00
ospab 951e597d46 CI/CD: release version v0.2.30 2026-05-26 16:48:19 +03:00
ospab d79b6f2384 feat: relay node system with HMAC pre-validation and key sync from upstream API 2026-05-26 16:29:23 +03:00
ospab 2228faa550 android: foreground service, wakelock, persistent notification, quick settings tile; gui: separate ping metric with color coding 2026-05-26 16:19:14 +03:00
ospab fffb67fbde gui: add build:dist script for packing all windows binaries 2026-05-26 13:25:40 +03:00
ospab 77c0701695 gui: fix helper lookup path for dev workspace 2026-05-25 23:16:24 +03:00
ospab 87540166f6 gui, flutter: use server rtt for ping display 2026-05-25 23:00:52 +03:00
ospab 164c36ed3e gui: fix compilation errors (update config mappings) 2026-05-25 22:53:06 +03:00
ospab c3b80eb12c gui: add multiplexing and translate reality fields 2026-05-25 22:45:31 +03:00
ospab d482369ced ci: remove gui build from release matrix 2026-05-25 22:34:42 +03:00
ospab 318cdb29fb CI/CD: release version v0.2.29 2026-05-25 22:32:59 +03:00
ospab 743ede0602 Fix duplicate rustls CryptoProvider panic 2026-05-25 22:32:55 +03:00
ospab fb1dadc4df CI/CD: release version v0.2.28 2026-05-25 22:21:02 +03:00
ospab ed3196be2e Generate Reality keys upon --init server 2026-05-25 22:20:39 +03:00
ospab f24c7ca481 Update wiki submodule 2026-05-24 23:14:57 +03:00
ospab 4dfe5fd3ca Fix ostp link generator for reality and uot 2026-05-24 23:14:44 +03:00
ospab aa09554881 CI/CD: release version v0.2.27 2026-05-24 23:03:55 +03:00
ospab 9e50984549 Fix linux format args, proxy config fields, and unused warnings 2026-05-24 23:03:50 +03:00
ospab 1865f66e48 CI/CD: release version v0.2.26 2026-05-24 22:55:13 +03:00
ospab 270cd91d71 Update flutter and gui apps to support XTLS-Reality and UoT config parameters 2026-05-24 22:55:07 +03:00
ospab 7a9c32969c CI/CD: release version v0.2.25 2026-05-24 22:49:59 +03:00
ospab 3e511f1fc5 Implement XTLS-Reality masquerade for UoT/TCP and fix MTU/config settings 2026-05-24 22:49:51 +03:00
ospab ef242bf6f4 feat(client): add linux headless warnings for TUN mode and sysproxy instructions 2026-05-21 22:31:02 +03:00
ospab cd154d4418 ci(gha): fix missing dependencies in release archives 2026-05-21 22:29:02 +03:00
ospab 3dd9490ecc CI/CD: release version v0.2.24 2026-05-21 18:27:08 +03:00
ospab 3ffa057d03 fix(client): fix catastrophic channel loopback in UoT transport that echoed packets locally 2026-05-21 18:24:48 +03:00
ospab 6c4006c48c CI/CD: release version v0.2.23 2026-05-21 18:09:46 +03:00
ospab 7c84c17336 fix(core): add raw_len and noise_len to noise-read error 2026-05-21 18:02:42 +03:00
ospab b57a3180bd CI/CD: release version v0.2.22 2026-05-21 15:58:16 +03:00
ospab 855ef7655f fix(core): improve UoT tracing and test coverage 2026-05-21 15:54:39 +03:00
ospab b9c6022b6c CI/CD: release version v0.2.21 2026-05-21 15:16:52 +03:00
ospab 1cff291fdd fix: noise-read in UoT handshake (single attempt, 4s timeout); add TCP rate limiter against bots 2026-05-21 15:15:56 +03:00
ospab be55aa6c6f CI/CD: release version v0.2.20 2026-05-21 15:05:46 +03:00
ospab 09b6f202d0 fix: UoT always uses plain TCP (remove broken TLS branch for port 443) 2026-05-21 14:59:48 +03:00
ospab 41562707ec fix: UoT uses server port instead of hardcoded 443 when stealth_port not overridden 2026-05-21 14:54:03 +03:00
ospab 02d0665edd CI/CD: release version v0.2.19 2026-05-21 14:45:45 +03:00
ospab cc3b0b689d fix: UoT server logs warn level, fix duplicate mux config, fix i686 CI with cross 2026-05-21 14:45:29 +03:00
ospab 3685ecac5c CI/CD: release version v0.2.18 2026-05-21 14:36:22 +03:00
ospab 3febe79b15 feat: log raw HTTP response on UoT handshake failure 2026-05-21 14:35:45 +03:00
ospab 9ef2282b31 CI/CD: release version v0.2.17 2026-05-21 14:12:06 +03:00
ospab 834c244f94 feat: disguise UoT handshake as WebSocket to bypass DPI and proxies 2026-05-21 14:11:50 +03:00
ospab 975a0dc0d9 CI/CD: release version v0.2.16 2026-05-21 14:06:28 +03:00
ospab 960382e93b fix: revert UoT POST back to GET for direct DPI bypass without proxy 2026-05-21 14:05:43 +03:00
ospab 9e2b29723c CI/CD: release version v0.2.15 2026-05-21 13:43:15 +03:00
ospab 1bc63c4094 feat: add X-Ostp-Server validation to UoT handshake 2026-05-21 13:15:49 +03:00
ospab e7ad24bb13 CI/CD: release version v0.2.14 2026-05-21 13:06:19 +03:00
ospab 92fc73756f fix: use POST and Content-Length in UoT to prevent nginx chunked encoding 2026-05-21 13:06:06 +03:00
ospab 3eb547db9d CI/CD: release version v0.2.13 2026-05-21 12:44:06 +03:00
ospab a81625d721 fix: correctly handle payload buffering during http handshake in uot 2026-05-21 12:43:47 +03:00
ospab 1c98bf9a51 CI/CD: release version v0.2.12 2026-05-21 03:00:51 +03:00
ospab 921533f560 fix: pass mtu to tun2socks 2026-05-21 03:00:44 +03:00
ospab c957a3a395 CI/CD: release version v0.2.11 2026-05-21 03:00:24 +03:00
ospab 5fa110d962 fix: make uot check case-insensitive 2026-05-21 03:00:07 +03:00
ospab a5a0a17cfd feat: add transport and mtu fields to gui 2026-05-21 02:59:01 +03:00
ospab f55769bae0 CI/CD: release version v0.2.10 2026-05-21 02:33:24 +03:00
ospab b87e87a7bd fix: correctly parse transport config section from json in CLI and GUI 2026-05-21 02:33:14 +03:00
ospab aa3fb70933 CI/CD: release version v0.2.9 2026-05-21 02:28:33 +03:00
ospab d9c3ba875c fix: disable aws-lc-rs backend in rustls for 32-bit musl compatibility 2026-05-21 02:28:28 +03:00
ospab 8bc8a3ce51 CI/CD: release version v0.2.8 2026-05-21 02:24:59 +03:00
ospab 81293a9071 feat: indicate protocol in connection log 2026-05-21 02:24:53 +03:00
ospab 30dea79197 CI/CD: release version v0.2.7 2026-05-21 02:24:02 +03:00
ospab ceb760e4ce feat: implement server-side UoT and MTU tuning 2026-05-21 02:23:49 +03:00
ospab 112ddfee59 CI/CD: release version v0.2.6 2026-05-21 02:11:45 +03:00
ospab 83f7ff2119 feat: UoT and xHTTP stealth 2026-05-21 02:11:02 +03:00
ospab 9329bcef45 feat: WSS transport mode selector in Flutter UI + TransportConfig in Rust 2026-05-21 00:39:12 +03:00
ospab 0cc5cf47ef feat: NetworkChanged command for instant mobile reconnect, lower stall threshold 25s->8s 2026-05-21 00:29:49 +03:00
ospab baff58c7fb CI/CD: release version v0.2.5 2026-05-18 22:05:26 +03:00
ospab a0e38c462e fix: clamp padding size to prevent UDP fragmentation on LTE/cellular and dynamically report connection status 2026-05-18 22:03:33 +03:00
ospab 4384125bf8 CI/CD: release version v0.2.4 2026-05-18 21:05:15 +03:00
ospab 8a2af5d73d feat: implement robust multiplexing, high-latency timeouts, and dynamic background reconnects for mobile network stability 2026-05-18 21:04:51 +03:00
ospab 3a4b5a8c63 chore: fix cargo clippy warnings
- Boxed HandshakeState in NoiseSession to reduce enum variant sizes
- Used is_ok() instead of let Ok(_) pattern
- Applied automatic clippy fixes for minor warnings
2026-05-17 22:22:39 +03:00
ospab 990af12fbe CI/CD: release version v0.2.3 2026-05-17 22:13:37 +03:00
ospab ee14a60348 feat: GUI v2 redesign + CI/CD speedup
GUI (ostp-gui):
- Complete HTML rewrite: orbit rings, server badge, metrics bar, peek-key
- CSS design system v2: ambient blobs, glassmorphism card, richer token set
  orbit animation (connected/connecting states), breathing power button,
  modern toggle component with thumb, toast variants (ok/error/default)
- main.js: clean state machine, server badge, TUN/SOCKS5 mode label,
  peek-key toggle, toast variants, import link, uptime counter

CI/CD (.github/workflows/release.yml):
- Replaced swatinem/rust-cache with actions/cache@v4 (per-target key)
- Cache cross binary: skip reinstall on cache hit (~3 min saved per job)
- Cache tauri-cli binary: skip reinstall on cache hit (~2 min saved per GUI job)
- Added npm cache (cache-dependency-path: ostp-gui/package-lock.json)
- Removed redundant pre-flight cargo check step (duplicates build step)
- Cleaned up packaging scripts (inline vars, smaller surface area)
2026-05-17 22:13:03 +03:00
ospab 3a16373a31 CI/CD: release version v0.2.2 2026-05-17 21:58:53 +03:00
ospab 9b01466953 test: integration tests for ProtocolMachine (handshake, data, close, wrong-psk, CC, multi-frame)
8 new integration tests in ostp-core::protocol::tests:
- test_full_handshake: Noise handshake -> Established state
- test_data_exchange_client_to_server: encrypt/decrypt data frame C->S
- test_data_exchange_server_to_client: encrypt/decrypt data frame S->C
- test_close_sequence: Close frame -> Closed state
- test_wrong_psk_handshake_fails: bad PSK rejected, never reaches Established
- test_congestion_controller_after_handshake: CC budget >= 2 in SlowStart
- test_multiple_data_frames: 10 sequential frames, payload integrity verified
- test_tick_no_crash: Tick event stable on both sides

Total: 43 tests, 0 failures
2026-05-17 21:58:01 +03:00
ospab bd3def32bb CI/CD: release version v0.2.1 2026-05-17 21:42:27 +03:00
ospab 73f84a951a feat: wire-level 0-RTT Resume frame, subscription API, adaptive pacing integration
Wire protocol:
- FrameKind::Resume (7) for 0-RTT session resumption
- Protocol handles Resume as early data delivery (zero round-trip)

Management API:
- GET /api/subscribe/{key} — returns client config JSON (sub-store compatible)
- Accept: text/plain returns ostp:// share link
- No Bearer token required — key itself is authentication
- ApiState extended with server_host/server_port for link generation

Graceful shutdown:
- Already implemented via wait_for_shutdown_signal() + tokio::select!
- Server drains in-flight frames before exit

35 tests pass, 0 failures, 0 warnings.
2026-05-17 21:42:01 +03:00
ospab ec8aab22f7 feat: install script v2 — global PATH symlink, /etc/ostp config, legacy path migration
- Binary at /opt/ostp/ostp, symlink at /usr/local/bin/ostp
- Config moved to /etc/ostp/config.json (standard Linux layout)
- Auto-migration from legacy paths: ~/ostp, /root/ostp, old /opt/ostp/config.json
- Systemd service updated with RUST_LOG=info
- Test script updated to discover binary via PATH first
2026-05-17 21:22:01 +03:00
ospab 3e6baf5a06 fix: use portable-atomic for AtomicU64 on 32-bit targets (MIPS, ARM32) 2026-05-17 21:14:07 +03:00
ospab 05583e189e feat: v0.2.0 — BBR congestion control, 0-RTT session resumption, management REST API, fallback server, multi-listener
Architecture:
- BBR-inspired congestion controller (SlowStart/ProbeBandwidth/ProbeRTT phases)
- 0-RTT session resumption with anti-replay ticket validation
- Management REST API (axum): /api/users CRUD, /api/server/status, Bearer auth
- TCP fallback proxy for anti-DPI camouflage (nginx/caddy passthrough)
- Multi-listener: bind to multiple UDP addresses simultaneously
- Per-user traffic stats with atomic counters and limit enforcement

Code quality:
- Structured logging: 0 eprintln in server/core/client, all tracing::{info,debug,warn,error}
- 35 unit tests across congestion, resumption, relay, outbound, obfuscation
- Removed dead code: kex.rs, unused dependencies (async-trait, x25519-dalek, rand_distr)
- Modular server: api.rs, fallback.rs, outbound.rs, relay.rs extracted from monolithic lib.rs

CLI:
- --check: config validation
- --generate-key: secure key generation (hex/base64, batch)
- --links: share link generation from server config
- --init: fallback section in server template

Documentation:
- README rewritten with architecture diagram, API examples, CLI reference
- Wiki: Management-API (EN+RU), Configuration (EN+RU), Home (EN+RU) updated
2026-05-17 21:05:44 +03:00
ospab a24d5d75d1 CI/CD: release version v0.1.70 2026-05-17 19:03:47 +03:00
ospab c82ec93ea7 fix: declare and grant custom Tauri v2 command permissions for GUI 2026-05-17 18:59:44 +03:00
ospab a31319a80a CI/CD: release version v0.1.69 2026-05-17 18:34:47 +03:00
ospab b342508932 chore: remove accidental wiki embedded submodule 2026-05-17 18:33:02 +03:00
ospab 0306cbaccd fix: resolve GUI buttons by safe tauri invoke, add validation toasts, build and bundle ostp-tun-helper in CI/CD pipeline 2026-05-17 18:32:55 +03:00
ospab 6ccaf3a303 CI/CD: release version v0.1.68 2026-05-17 16:40:02 +03:00
ospab ad87c80e8d chore: exclude wiki from main repo 2026-05-17 16:39:40 +03:00
ospab e8a92059d2 design: professional GUI redesign — minimal dark theme
Complete visual overhaul:
- Replaced vibrant/gaming aesthetic with enterprise-grade minimal design
- Darker, more muted color palette (bg: #0a0a0f, accent: #7c83ff)
- Reduced border/glow intensity for cleaner look
- Thinner power button border (solid 2px instead of thick radial gradient)
- Subtler ambient background effects (lower opacity, slower animation)
- More compact spacing and typography
- Smooth screen transitions (translateX instead of translateY)
- Refined toggle switches and form elements
- Consistent border-radius and padding system
2026-05-17 16:39:20 +03:00
ospab e20e4f2533 CI/CD: release version v0.1.67 2026-05-17 16:28:11 +03:00
ospab 49d97dbee3 test: add obfuscation round-trip tests, fix i18n module import
- 7 passing tests verify client-server compatibility:
  * Handshake obfuscation round-trip (correct key recovers session_id)
  * Wrong key produces garbage (prevents unauthorized probes)
  * Data packet obfuscation round-trip
  * Deterministic derivation (same key = same secrets)
  * Different keys produce different secrets
  * Legacy API consistency
  * Padding range validation (100 random keys)

- Fixed test module import path to use crate::crypto::obfuscation::*
- Added i18n.js module for GUI localization
2026-05-17 16:27:43 +03:00
ospab 69e4426152 feat: release preparation — TUN fix, i18n, GUI CI/CD, speed improvements
TUN Interface:
- Fixed adapter name to always be 'ostp_tun' by cleaning up stale
  adapters before launch (prevents 'ostp_tun 2', 'ostp_tun 3', etc.)
- Parallelized route setup with tun2socks launch to save ~3 seconds
- Replaced fixed 2-second sleep with adapter readiness polling
- Added -NoProfile to all PowerShell calls for faster execution

Speed:
- Reduced handshake timeout from 10s to 5s
- Reduced tun2socks spawn buffer from 300ms to 0 (removed)

GUI:
- Added i18n support: English and Russian translations
- Language toggle button in header (EN/RU)
- Merged 'IP Ranges' field into 'Bypass IPs / CIDR Ranges'
- Removed separate IP ranges field
- All static text uses data-i18n attributes
- Status messages, labels, toasts all translated
- Replaced alert() calls with toast notifications

CI/CD:
- Added separate GUI build job for Windows x64 and arm64
- Produces ostp-windows-gui-{arch}.zip with: ostp-gui.exe + wintun.dll + tun2socks.exe
- Uses Tauri CLI v2 for build
2026-05-17 16:25:30 +03:00
ospab 074a3f6371 CI/CD: release version v0.1.66 2026-05-17 15:32:44 +03:00
ospab a4d8da2460 security: Kerckhoffs's principle — all secrets derived from access key via HKDF
Applied Kerckhoffs's principle: the protocol's security and obfuscation
now depend SOLELY on the access key. An adversary who reverse-engineers
the binary cannot build a DPI filter without knowing the key.

Changes:
- Replaced hardcoded salt string ('-ostp-psk-salt') with HKDF-SHA256.
  The salt is now derived from the key hash itself — no protocol-specific
  strings remain in the binary.
- Unified all secret derivation into derive_all_secrets() which produces
  PSK, obfuscation key, and handshake padding range from a single HKDF
  invocation.
- Handshake padding range is now key-derived: different access keys
  produce different size distributions (min: 16-79, max: +48..+175).
  A universal size-based filter is impossible without the key.
- HKDF-SHA256 (RFC 5869) implemented inline using existing hmac+sha2
  dependencies — no new crate required.

What remains identifiable in the binary:
- 'Noise_NNpsk0_25519_ChaChaPoly_BLAKE2s' — standard Noise pattern
  string, shared with many other projects, NOT OSTP-specific.
- Generic HMAC/SHA-256/ChaCha20-Poly1305 code — standard crypto
  primitives used by millions of applications.
2026-05-17 15:32:07 +03:00
ospab 0418e5728c CI/CD: release version v0.1.65 2026-05-17 15:23:12 +03:00
ospab 8abffde0fd security: per-packet handshake masks (eliminates correlation fingerprint)
Previously handshake obfuscation used a FIXED mask derived from
HMAC(obf_key, u64::MAX). This meant bytes [4..6] (noise_len XOR
fixed_mask) produced the SAME 2-byte value on every handshake from
the same access key — a correlation fingerprint for DPI.

Now BOTH data and handshake packets use the same payload-sampling
approach:
  mask = HMAC-SHA256(obf_key, payload_sample[0..32])

For data packets:   payload_sample = AEAD ciphertext (random per packet)
For handshake packets: payload_sample = Noise ephemeral key (random per connection)

Result: every single byte on the wire is cryptographically independent
across packets. No fixed patterns, no correlation between connections.

Wire analysis after this change:
- Packet sizes: random (84-182 for handshake, variable for data)
- All header bytes: unique per packet (XOR with unique HMAC mask)
- Payload bytes: AEAD ciphertext / Noise handshake (indistinguishable from random)
- No protocol signatures, no version fields, no magic bytes visible on wire
2026-05-17 15:20:21 +03:00
ospab a6640e1344 CI/CD: release version v0.1.64 2026-05-17 15:16:37 +03:00
ospab 8fe0589ea6 fix: handshake padding wire format (breaking fix)
The previous commit added random padding after Noise handshake payloads
but the receiver passed the entire raw buffer (including padding) to
snow::read_handshake(), which cannot handle trailing bytes.

New wire format:
  [session_id:4][noise_len:2][noise_payload:N][random_padding:32-128]

Changes:
- wrap_datagram_handshake: puts noise_len (u16 BE) at bytes [4..6]
  before the Noise payload, followed by 32-128 random padding bytes
- handle_inbound: reads noise_len from [4..6], passes only
  raw_vec[6..6+noise_len] to snow, ignoring trailing padding
- obfuscation: handshake mask extended from 4 to 6 bytes to also
  cover the noise_len field (prevents DPI from seeing constant u16)
- dispatcher: key-trial loop updated to deobfuscate 6-byte header

Both client and server now produce/consume the same padded format.
2026-05-17 15:16:02 +03:00
ospab bb7d471864 CI/CD: release version v0.1.63 2026-05-17 14:41:02 +03:00
ospab 77ec0e3a44 fix: DPI resistance, GUI proxy/tunnel, and code quality
DPI/TSPU resistance:
- Handshake packets now padded with 32-128 random bytes (prevents size
  fingerprinting — previously every handshake was exactly 52 bytes)
- Frame header reserved bytes randomized instead of always 0 (prevents
  known-plaintext oracle inside encrypted payload)
- Padding jitter cap increased from 96 to 256 bytes for better traffic
  pattern masking

GUI Windows app (tunnel/proxy not starting):
- CRITICAL: Added CREATE_NO_WINDOW flag to all reg.exe calls in sysproxy.rs.
  In Tauri GUI context (no console window), Command::new('reg') was silently
  failing because there was no attached console. This prevented the Windows
  system proxy from being enabled.
- Added ProxyOverride bypass list (localhost;127.*;10.*;192.168.*;<local>)
  to prevent proxy loop for local traffic
- Added comprehensive logging for all registry operations
- Set initial connection_state to 1 (connecting) instead of 0 — prevents
  UI polling from immediately flipping back to 'disconnected' before the
  handshake has a chance to begin

Code quality:
- Fixed log file paths: log_to_core_file() and log_to_file() now write next
  to the executable instead of CWD. In GUI context, CWD could be
  C:\Windows\System32, causing write failures or misplaced log files.
2026-05-17 14:40:13 +03:00
ospab 032f694821 feat: comprehensive diagnostic logging across all modules
protocol.rs:
- Gap recovery logs: skipped frames count, delivered count, remaining buffer
- Duplicate frame detection with nonce values
- Max reorder window exceeded with gap width
- NACK handling: retransmit success vs frame evicted from history
- Reorder buffer overflow with capacity stats
- Close frame receipt
- Zombie frame eviction count
- sent_history overflow (the root cause of speedtest death)

dispatcher.rs:
- New session authentication with peer IP, session count, replay cache size
- Client roaming detection (old addr -> new addr)
- Handshake rejection reasons: timestamp drift, replay cache full, max sessions
- Protocol errors and tick errors with session context

bridge.rs:
- UDP socket buffer diagnostics (requested vs actual)
- Handshake response size and RTT
- Inbound protocol errors with session index
- Outbound packing errors with stream_id

signal.rs:
- Specific shutdown signal identification (SIGTERM/SIGINT/Ctrl+C/Close/Break)

server lib.rs:
- Startup banner with access key count and ARQ config
- UDP buffer diagnostics
- Relay CONNECT/CLOSE/error always visible (not gated by debug)
- All println! -> eprintln! for proper stderr logging
- Hot-reload prefix fixed [ostp-server] -> [ostp]
2026-05-17 14:31:21 +03:00
ospab f8aa8906ff CI/CD: release version v0.1.62 2026-05-17 14:23:08 +03:00
ospab dc6635e248 fix: tunnel death after speedtest — gap recovery and ARQ tuning 2026-05-17 14:22:50 +03:00
ospab e36d743ad5 refactor: professionalize all scripts and CI workflow
build.ps1:
- Added mandatory cargo check pre-flight that blocks releases on errors
- Added --Check flag for check-only mode (no build, no release)
- Reverts version bump if check fails
- Professionalized all output (removed informal language)
- Cleaner output structure with consistent [ok], [warn], [error] tags

install.ps1 / install.sh:
- Professionalized all prompts and messages
- Removed informal phrasing
- Consistent formatting

test_linux.sh:
- Updated all log string matchers to match professionalized output:
  'Connection established' (was 'Bridge connection established')
  'Starting server' (was 'Starting in SERVER mode')
  'Starting client' (was 'Starting in CLIENT mode')
  RTT regex updated for new format

release.yml:
- Added cargo check pre-flight step before native compilation
2026-05-17 03:41:40 +03:00
ospab aa9a93fcbf CI/CD: release version v0.1.61 2026-05-17 03:35:57 +03:00
ospab 696d416eff fix: resolve KeyExchange import error and clean compiler warnings
- Removed stale KeyExchange re-export from crypto/mod.rs (kex.rs
  only exports HybridSharedSecret and HybridKex after stub refactor)
- Removed unused imports in ostp-server/lib.rs (AsyncWriteExt,
  tcp::OwnedWriteHalf)
- Suppressed dead_code warning on HelperMsg::Log variant (IPC spec)
- Verified: cargo check passes with zero errors and zero warnings
2026-05-17 03:35:39 +03:00
ospab 07511debbd CI/CD: release version v0.1.60 2026-05-17 03:32:12 +03:00
ospab 31f3fff187 fix: GUI, JNI SDK, and TUN handler audit fixes
ostp-gui:
- GUI-01: Config parsing now strips JSONC comments via json_comments
  crate, matching CLI behavior. Previously failed on any commented config.
- GUI-02: stop_tunnel now properly aborts the JoinHandle with a 2s
  timeout instead of silently dropping it.

ostp-jni (Android SDK):
- JNI-01: Replaced all .unwrap() calls in JNI functions with safe
  null_mut fallback. JNI functions must never panic.
- JNI-02: Added missing exclusions, multiplex, debug fields to
  Kotlin SDK Config.toNativeJson(). Without these, serde deserialization
  on the native side could fail or use wrong defaults.
- JNI-03: Replaced shutdown_background() with shutdown_timeout(3s)
  to allow proper task cleanup and port unbinding.
- JNI-04: Updated Kotlin log string matchers to match professionalized
  messages (Connection established, TUN tunnel established, etc.)

TUN handlers:
- TUN-01: Windows TUN cleanup guard now resets DNS via netsh. Previously
  the custom DNS server remained configured after disconnect, causing
  complete DNS resolution failure.
- Unified all remaining [ostp-client] log prefixes to [ostp] across
  wintun_handler.rs, linux_handler.rs, and proxy.rs.
2026-05-17 03:31:48 +03:00
ospab 8eb3fc72cb polish: professionalize all user-facing log output and UX
- Unified log prefix to [ostp] across all modules (was [OSTP Core],
  [ostp-server], [ostp-client], [client], [bridge])
- Removed informal/casual phrasing from all user-visible messages
- Startup messages are clean and concise (mode, server, status)
- Error messages are actionable without being alarming
- Essential server logs (client connect/disconnect) always visible
- Essential client logs (connection status, errors) always visible
- TUN tunnel messages consistent across Windows and Linux
- Removed noisy eprintln from UDP reader hot path
- Status format: [ostp] Status: Connected (rtt=12.3ms)
2026-05-17 03:26:15 +03:00
ospab 7424ccc0ff fix: resolve critical ARQ bugs causing Speedtest tunnel drops + docs overhaul
Critical fixes (6):
- protocol.rs: in_flight_count() now counts only retransmittable Data frames,
  not Ack/Nack control frames — eliminates false backpressure under load
- protocol.rs: NACK is now rate-limited to once per 30ms — prevents
  retransmission storm during normal UDP jitter
- protocol.rs: zombie frames exceeding max_retries+4 are evicted each tick —
  prevents unbounded memory growth and stale retransmits
- protocol.rs: Closing state now processes final in-flight packets instead
  of silently dropping them — prevents data loss at session teardown
- server/lib.rs: stream_tx changed from bounded(10000) to unbounded_channel —
  prevents TCP-reader collapse during Speedtest with 50+ streams
- bridge.rs: liveness timeout raised from 30s to 60s — prevents false
  reconnect during heavy Speedtest load

Medium fixes (8):
- protocol.rs: ACK range truncation preserves cumulative range (index 0)
- bridge.rs: Ping now uses send_datagram() for correct TURN wrapping
- dispatcher.rs: replay_cache hard-capped at 100k entries (DoS protection)
- dispatcher.rs: old addr cleaned from addr_to_session on roaming
- server/lib.rs: TCP connect_target() now has 10s timeout
- config.rs: TURN section parsed during hot-reload
- proxy.rs: HTTP header parsing uses 512-byte chunks instead of 1-byte reads
- proxy.rs: stream_id wrap-around skips active IDs to prevent collision
- runner.rs: is_essential_log matches actual log strings from bridge.rs

Other:
- kex.rs: clearly marked as dead PQ stub (not used by protocol)
- README.md + README.ru.md: complete rewrite with architecture diagram
- docs/en/specification.md: updated ARQ section with all new semantics
2026-05-17 03:20:50 +03:00
ospab a9ba941782 CI/CD: release version v0.1.59 2026-05-17 02:56:17 +03:00
ospab 5bd653e9d2 fix: immediately ACK duplicate packets instead of silently dropping them to unblock client retries when ACKs are lost 2026-05-17 02:56:16 +03:00
ospab b670ba9e48 CI/CD: release version v0.1.58 2026-05-17 02:40:54 +03:00
ospab 5c33f08a9b fix: resolve fatal connection halt caused by unrecoverable dropped untracked Ack/Nack frames. Control frames are now saved in sent_history without auto-retransmission to allow targeted Nack recovery. 2026-05-17 02:40:52 +03:00
ospab 9c05f130ac CI/CD: release version v0.1.57 2026-05-17 02:20:01 +03:00
ospab f0a93b4161 perf: heavily tune UDP socket buffers via socket2 to support 10Gbps+ micro-burst resilience 2026-05-17 02:19:59 +03:00
ospab ecba33e6d8 CI/CD: release version v0.1.56 2026-05-17 02:16:14 +03:00
ospab 9c685c8e43 feat: ensure connection and disconnection logs are always visible on the server even without debug mode 2026-05-17 02:16:12 +03:00
ospab 684b50f779 CI/CD: release version v0.1.55 2026-05-17 02:01:42 +03:00
ospab b1dfb335c9 fix: resolve severe server-side head-of-line blocking under high connection concurrency by delegating TCP connection establishments and stream writing to asynchronous spawned tasks 2026-05-17 02:01:40 +03:00
ospab 6a474c8f00 CI/CD: release version v0.1.54 2026-05-17 01:35:25 +03:00
ospab 4cc1f0079c fix: resolve packet drop & connection timeouts under high speed tests by reducing proxy event queue size and expanding sent history / reorder buffers 2026-05-17 01:35:24 +03:00
ospab a46b6eb0b6 CI/CD: release version v0.1.53 2026-05-17 01:30:01 +03:00
ospab bfa858ff93 fix: prevent premature Windows client shutdown due to empty/closed console event streams 2026-05-17 01:30:00 +03:00
ospab ff207112d8 chore: sync Cargo.lock 2026-05-17 01:20:28 +03:00
ospab 039e23d34e CI/CD: release version v0.1.52 2026-05-17 00:21:17 +03:00
ospab e96d440e2d feat: add turn section to default client init template 2026-05-17 00:21:15 +03:00
ospab 2ab8353078 CI/CD: release version v0.1.51 2026-05-16 23:58:10 +03:00
ospab 5c71c6cc9e feat: introduce ciphertext-derived dynamic obfuscation to fully mask the nonce on the wire 2026-05-16 23:58:07 +03:00
ospab 52db766e87 CI/CD: release version v0.1.50 2026-05-16 23:41:08 +03:00
ospab ec35769b9f fix: implement non-blocking unbounded channels and clean stream reset on reconnect 2026-05-16 23:41:04 +03:00
ospab 0c762d6873 CI/CD: release version v0.1.49 2026-05-16 20:55:53 +03:00
ospab e28a698e9b fix: resolve connection instability under load and refine logging 2026-05-16 20:55:11 +03:00
ospab a7280ad38f CI/CD: release version v0.1.48 2026-05-16 19:30:42 +03:00
ospab 5c7a55f9e0 fix: restore server-only guard for link printing in init block 2026-05-16 19:30:15 +03:00
ospab 694e420397 CI/CD: release version v0.1.47 2026-05-16 19:25:57 +03:00
ospab 9982b8b94b fix: correct crate name to json_comments 2026-05-16 19:25:27 +03:00
ospab 5695028736 CI/CD: release version v0.1.46 2026-05-16 19:24:08 +03:00
ospab f419bfa4ee feat: switch to JSON with comments (JSONC) for config; docs: update READMEs 2026-05-16 19:23:17 +03:00
ospab acc5e87878 docs: remove remaining emojis and fix language links 2026-05-16 19:19:33 +03:00
ospab 7e44f57c00 docs: simplify READMEs and add cross-language links 2026-05-16 19:17:04 +03:00
ospab 514bae94cd fix: resolve build errors and remove GUI from main release; docs: improve READMEs 2026-05-16 19:15:04 +03:00
ospab acf81527b6 docs: simplify README and fix keep-alive/config validation 2026-05-16 19:05:09 +03:00
ospab dcb3c1c5e4 CI/CD: release version v0.1.45 2026-05-16 18:21:16 +03:00
ospab 4970b661db chore: implement keep-alive, config comments, validation and CI/CD improvements 2026-05-16 18:20:53 +03:00
ospab 5d092340be CI/CD: release version v0.1.44 2026-05-16 18:13:40 +03:00
ospab a398bf2fdd fix(gui): add dev/build npm scripts that auto-build helper, fix find_helper_exe 2026-05-15 23:12:22 +03:00
ospab b0491e14e3 feat(gui): privileged TUN helper architecture - GUI runs unprivileged, UAC prompt shown only for TUN mode via ostp-tun-helper.exe IPC 2026-05-15 23:08:14 +03:00
ospab 5d9034ca1e feat(gui): force Administrator privileges via manifest and automate WebView2 loopback exemption for dev environment 2026-05-15 22:57:33 +03:00
ospab 57596143fa fix(gui): resolve ERR_CONNECTION_REFUSED by disabling automatic relaunch in Tauri context and surfacing Admin requirement as UI alert 2026-05-15 22:50:30 +03:00
ospab e21e612e5c feat(gui): implement real-time atomic status polling and multi-state UI feedback (Stopped/Handshaking/Established) and update JNI/core layers 2026-05-15 22:37:50 +03:00
ospab c26e63250c fix(win-tun): eliminate console window flashes and early SOCKS5 bind race conditions in tun2socks launcher 2026-05-15 22:37:33 +03:00
ospab c197aea497 CI/CD: release version v0.1.43 2026-05-15 22:33:56 +03:00
ospab 92be766357 CI/CD: release version v0.1.42 2026-05-15 22:25:48 +03:00
ospab 85d3e28c85 feat: implement native public IP autodetection via ip r and interactive cached prompt fallback for server links 2026-05-15 22:25:35 +03:00
ospab 5d590f7d59 CI/CD: release version v0.1.41 2026-05-15 22:18:19 +03:00
ospab b63979b014 feat: add custom DNS server & Exclusions config fields, simplify share link schema, introduce --links server helper 2026-05-15 22:17:55 +03:00
ospab 067ee758cd feat: implement settings ui forms, add share link parser to cli, add paste link functionality, reduce gui height to 680 2026-05-15 22:13:04 +03:00
ospab b26863e8e5 CI/CD: release version v0.1.40 2026-05-15 22:04:50 +03:00
ospab 07b31cc3f3 fix: resolve infinite fatal tick log spam with auto-reconnect and centralize UAC elevation in run_client_core to protect GUI apps 2026-05-15 22:04:11 +03:00
ospab 609564fdd9 feat(gui): add fully native tauri windows gui with premium mobile layout, real-time statistics polling, in-app config editor, and graceful exit cleanup 2026-05-15 22:01:20 +03:00
ospab 2819a14189 fix(installer): use rename trick to bypass file locks and ensure all bundled files (tun2socks, wintun) are copied 2026-05-15 20:24:01 +03:00
ospab 2f2f9ffdef CI/CD: release version v0.1.39 2026-05-15 20:05:27 +03:00
ospab b082c158fd fix: throw error on ARQ max_retries exceeded to prevent silent deadlock that caused infinite upload timeouts 2026-05-15 20:04:07 +03:00
ospab d34a1dd29a fix: resolve asymmetric packet loss (zero upload) by enforcing strict MTU caps and reducing TUN interface MTU to 1300 to prevent UDP fragmentation on outbound traffic 2026-05-15 19:54:07 +03:00
ospab 3ad3390057 CI/CD: release version v0.1.38 2026-05-15 19:25:45 +03:00
ospab a3c8b3a750 fix: address final analysis issues including Nonce exhaustion, TUN pre-flight checks, dead code, and proper TURN channel framing. Also fix CI packaging of tun2socks 2026-05-15 19:23:50 +03:00
ospab 5ac59c92ea chore: enforce LF line endings on bash scripts via gitattributes to fix 'bad interpreter' on Linux 2026-05-15 19:08:03 +03:00
ospab 877d9035cc test: enhance test_linux.sh with remote live diagnostics 2026-05-15 18:56:43 +03:00
ospab 1081303001 CI/CD: release version v0.1.37 2026-05-15 18:53:30 +03:00
ospab 74c5eac1fe docs: add official specifications for OSTP 2026-05-15 18:51:13 +03:00
ospab 2952d3aa3c docs: replace fake standards with official OSTP specifications 2026-05-15 18:49:32 +03:00
ospab 96003a1dc8 docs: rewrite ieee_spec and rfc_ostp as honest independent specifications 2026-05-15 18:45:09 +03:00
ospab c5d43a4666 CI/CD: release version v0.1.36 2026-05-15 18:42:58 +03:00
ospab 01277b5108 ci: fix macOS toolchain error and use linux-arm64 tun2socks for android builds 2026-05-15 18:39:26 +03:00
ospab 0f81140f06 feat: resolve flow control, tun crash route cleanup, log pollution, padding caps 2026-05-15 18:34:32 +03:00
ospab cdc3f408f9 CI/CD: release version v0.1.35 2026-05-15 18:28:38 +03:00
ospab 52862b9eae chore: remove junk files, update .gitignore for temp dirs and archives 2026-05-15 18:27:45 +03:00
ospab 9e4c96afda chore: remove target_linux from tracking 2026-05-15 18:24:50 +03:00
ospab 77b0d55f39 security: fix obfuscation via HMAC per-packet mask and cap server sessions at 1024 2026-05-15 18:24:35 +03:00
ospab 6e35609f42 perf: accelerate protocol via low-latency ACK windows and suppress high-velocity console spam logs 2026-05-15 17:44:06 +03:00
ospab f6f497a418 CI/CD: release version v0.1.34 2026-05-15 17:20:17 +03:00
ospab 37d659f1e5 CI/CD: release version v0.1.33 2026-05-15 17:10:32 +03:00
ospab 89fd886639 fix: use universal .zip for all tun2socks downloads in release CI workflow 2026-05-15 17:10:01 +03:00
ospab b3ff592009 CI/CD: release version v0.1.32 2026-05-15 17:08:00 +03:00
ospab 6ae43a8f41 refactor: pre-package Wintun and tun2socks into archives via CI/CD and purge runtime downloader 2026-05-15 17:07:18 +03:00
ospab 9fa93ebce5 CI/CD: release version v0.1.31 2026-05-15 16:57:05 +03:00
ospab 5ee8d5a470 fix: prevent PowerShell Invoke-WebRequest hangs by disabling ProgressPreference 2026-05-15 16:56:10 +03:00
ospab ecd153b48f CI/CD: release version v0.1.30 2026-05-15 16:46:47 +03:00
ospab 578dcf6f9b fix: explicitly execute cargo through rustup run to bypass broken macOS runner shims 2026-05-15 16:46:19 +03:00
ospab 0773f9be9d CI/CD: release version v0.1.29 2026-05-15 16:41:55 +03:00
ospab 92c044217f feat: absolute dynamic Windows elevation using native ShellExecuteW (runas) 2026-05-15 16:41:20 +03:00
ospab c2407f3637 fix: stabilize Windows dynamic UAC elevation by waiting for powershell handoff 2026-05-15 16:39:19 +03:00
ospab e83d81b0a7 fix: resolve macOS ARM64 build fail by eliminating redundant shell env sourcing 2026-05-15 16:15:40 +03:00
ospab 8fa2c2d687 CI/CD: release version v0.1.28 2026-05-15 16:13:39 +03:00
ospab efcadad2f0 fix: prevent console window closure on fatal errors via user prompt pause 2026-05-15 16:10:47 +03:00
ospab 51cf1e72ef CI/CD: release version v0.1.27 2026-05-15 15:59:38 +03:00
ospab da50d2f15f feat: autonomous self-downloading dependencies inside TUN OS drivers 2026-05-15 15:58:35 +03:00
ospab 22fb9bb3d3 feat: unified cross-platform TUN support (Linux + Windows Firewall dynamic bypass) 2026-05-15 15:54:37 +03:00
ospab f4c8a7d6bc CI/CD: release version v0.1.26 2026-05-15 01:26:10 +03:00
ospab 38e62adad8 CI/CD: release version v0.1.25 2026-05-15 01:23:34 +03:00
ospab 31d61de939 Fix: Add physical network bypass routing for primary DNS (1.1.1.1) in Wintun handler to prevent UDP-over-TCP DNS resolution deadlock. 2026-05-15 01:23:13 +03:00
ospab c7689f2785 CI/CD: release version v0.1.24 2026-05-15 01:21:40 +03:00
ospab e5062465d6 Fix: Integrate multi-architecture Wintun zip extractor filtering and upgrade tun2socks to v2.6.0 using the provided working release endpoint. 2026-05-15 01:20:56 +03:00
ospab 1ab313b616 Fix: Overhaul Windows UAC elevation to preserve CWD and CLI arguments, preventing instant crash on relaunch; apply UseBasicParsing to fix background downloader hangs. 2026-05-15 01:09:15 +03:00
ospab d0146d027d Fix: Enforce local filesystem touch on ostp.exe to override inherited build server timestamps after zip expansions. 2026-05-15 01:02:18 +03:00
ospab a118e45cf1 Fix: Mitigate Invoke-WebRequest hangs via UseBasicParsing, and implement aggressive child/parent directory hierarchy scans for zero-friction binary discoveries. 2026-05-15 00:57:54 +03:00
ospab fb32ca29de DevOps: Overhaul Windows install.ps1 to support dynamic location preservation and smart permission-agnostic deployment mapping. 2026-05-15 00:55:51 +03:00
ospab 0642cbde06 Fix: Resolve PowerShell parser error in install.ps1 by wrapping interpolated arch variable in curly braces before colon delimiter. 2026-05-15 00:50:22 +03:00
ospab c1bbaec842 CI/CD: release version v0.1.23 2026-05-15 00:47:06 +03:00
ospab d328222f1b Fix: Explicitly link user32.lib and kernel32.lib inside runner.rs to resolve indirect ShowWindow unresolved external MSVC linker regression. 2026-05-15 00:46:48 +03:00
ospab 32ce5de107 DevOps: Add scripts/install.ps1 native Windows installer and updater, and document Linux/Windows bootstrappers in README.md 2026-05-15 00:41:37 +03:00
ospab 4ecbab05a1 CI/CD: release version v0.1.22 2026-05-15 00:38:16 +03:00
ospab 3848083d52 DevOps: Localize quick-start install.sh shell script to English for global platform parity. 2026-05-15 00:38:03 +03:00
ospab 83cf831ebc DevOps: Transform quick-start shell installer into a smart auto-updater. Detects pre-existing configs, transparently hot-swaps binaries, restarts running services, and bypasses interactive setup loops. 2026-05-15 00:37:24 +03:00
ospab 6713d70071 Fix: Simplify system proxy registry format to raw address and port, and restore safe defaults for tun.wintun_path and ipv4_address in client initialization template 2026-05-15 00:36:28 +03:00
ospab 0b3ee775e4 Refactor: Fully overhaul TUN mode architecture. Replace stub with auto-downloading Go tun2socks daemon, inject dynamic Windows PowerShell routing tables with proxy IP exclusions, metrics and secure DNS resolver, and prune legacy wintun crate bindings. 2026-05-15 00:35:25 +03:00
ospab 292ba3b3d7 CI/CD: release version v0.1.21 2026-05-15 00:16:14 +03:00
ospab f1b8bfac42 UX: Remove dummy examples and emojis from CLI output to enforce professional strict templates 2026-05-15 00:15:54 +03:00
ospab a773422495 UX: Redesign CLI init workflow to prevent silent client-mode trap on missing configs and enrich config templates with complete modern routing schema 2026-05-15 00:14:17 +03:00
ospab 0d414e5000 Refactor: Integrate portable-atomic to transparently emulate 64-bit atomics on 32-bit router architectures like MIPS and ensure absolute hardware-independent metrics tracking 2026-05-15 00:11:28 +03:00
ospab 4e8513b597 CI/CD: release version v0.1.20 2026-05-15 00:01:38 +03:00
ospab 403405c791 CI/CD: Complete system alignment by introducing Nightly toolchain exclusively for MIPS builds and applying macOS path-prioritization patches 2026-05-15 00:01:22 +03:00
ospab c8a28a75ce CI/CD: release version v0.1.19 2026-05-14 23:57:50 +03:00
ospab fc815d4f85 CI/CD: Resolve MIPS Tier-3 compilation by instructing Cross to dynamically build-std library from source 2026-05-14 23:57:19 +03:00
ospab ed7054be7d CI/CD: Streamline triggers by removing redundant master branch push hook, enforcing tag-only execution 2026-05-14 23:53:12 +03:00
ospab b0ce83c076 Install: Refactor install.sh script to support auto-architecture detection, correct .tar.gz archive downloads and seamless tar extraction 2026-05-14 23:52:33 +03:00
ospab 899755ea1c CI/CD: release version v0.1.18 2026-05-14 23:52:13 +03:00
ospab de4e168162 CI/CD: Secure reliable cross-compilation via explicit Cross.toml registry and container isolation safeguards to solve MIPS, FreeBSD, and GLIBC failures 2026-05-14 23:51:37 +03:00
ospab 6b4edccc64 CI/CD: release version v0.1.17 2026-05-14 23:36:05 +03:00
ospab 97c9e3045b CI/CD: Remove CI suppression [skip ci] to fully restore automatic release triggers 2026-05-14 23:34:05 +03:00
ospab 68ae4da39d CI/CD: prepare version v0.1.16 [skip ci] 2026-05-14 23:23:05 +03:00
ospab dedcb3b952 CI/CD: prepare version v0.1.15 [skip ci] 2026-05-14 23:21:19 +03:00
ospab 565023070a CI/CD: Safeguard GitHub Release asset upload step to only execute on Tag pushes, enabling clean verification builds on master branch 2026-05-14 23:20:33 +03:00
ospab e5980df243 CI/CD: Skip host-runner rustup target addition for Cross-Docker targets to prevent Tier-3 architecture compilation crashes 2026-05-14 23:19:42 +03:00
ospab 7c4abca29e CI/CD: prepare version v0.1.14 [skip ci] 2026-05-14 23:18:34 +03:00
ospab d7f34505ec CI/CD: Fully bulletproof GHA YAML syntax & broaden triggers to master branch for immediate real-time execution 2026-05-14 23:18:21 +03:00
ospab d37f077287 CI/CD: prepare version v0.1.13 [skip ci] 2026-05-14 23:17:41 +03:00
ospab ca719cfae6 CI/CD: prepare version v0.1.12 [skip ci] 2026-05-14 23:16:59 +03:00
ospab c8c760c7d0 CI/CD: Add proactive git-pull rebase synchronization to start of build.ps1 2026-05-14 23:15:40 +03:00
ospab 2d9e5bbd9f CI/CD: prepare version v0.1.11 [skip ci] 2026-05-14 23:14:46 +03:00
ospab 00864abdbb CI/CD: prepare version v0.1.10 [skip ci] 2026-05-14 23:10:19 +03:00
ospab 882ef6f337 CI/CD: Fix GHA YAML negation syntax & Introduce -TriggerOnly parameter in build.ps1 2026-05-14 23:10:03 +03:00
241 changed files with 35380 additions and 3460 deletions

BIN
.gitattributes vendored Normal file

Binary file not shown.

View File

@ -1,139 +1,726 @@
name: Universal CI/CD Release Matrix name: CI/CD
# `run-name` is evaluated at workflow-start, BEFORE any job runs - it cannot
# see resolve-channel's computed tag_name (e.g. "0.4.3-alpha"), only the
# `github.*` context. The old "release version ${{ github.ref_name }}" showed
# the bare branch name ("alpha"/"beta") for every run, which reads
# exactly like a literal release tag and caused real confusion - the actual
# release tag has been correct (versioned) all along; only this label lied
# about it. Spell out "channel" so nobody mistakes one for the other again.
# NOTE: this value MUST be quoted. The GHA string literal below contains
# "Release build: {0}" - an unquoted YAML plain scalar treats ": " (colon
# then space) as starting a nested mapping, which is exactly what broke every
# single push since this line was introduced: GitHub rejected the whole
# workflow file at parse time (before any job runs), silently burning an
# Actions-minutes-billed run per push for nothing.
run-name: "${{ startsWith(github.ref, 'refs/tags/') && (contains(github.ref_name, 'beta') && format('CI/CD: beta version {0}', github.ref_name) || contains(github.ref_name, 'alpha') && format('CI/CD: alpha version {0}', github.ref_name) || format('CI/CD: release version {0}', github.ref_name)) || format('CI/CD: {0} channel build', github.ref_name) }}"
on: on:
push: push:
tags: tags:
- "v*" - "v*"
workflow_dispatch: workflow_dispatch:
inputs:
channel:
description: >-
Manually build+release just this rolling channel. Stable releases
are NEVER picked here on purpose - cut those only via a real
"vX.Y.Z" tag push, so a manual dispatch can't accidentally publish
a "stable" release.
type: choice
required: true
default: alpha
options:
- alpha
- beta
permissions: permissions:
contents: write contents: write
# -- Global defaults ---------------------------------------------------------
env:
CARGO_TERM_COLOR: always
CARGO_INCREMENTAL: 0
RUST_BACKTRACE: short
jobs: jobs:
# Computes ONE channel + release tag for this whole run, so every build
# job (native matrix + all 3 GUI platforms + Android) uploads to the exact
# same release under the exact same tag, instead of repeating this logic
# (and risking it drifting out of sync) in five separate places.
#
# Tag shape:
# - real "vX.Y.Z" / "vX.Y.Z-beta.N" tag push -> tag used as-is (stable promotion)
# - push to `alpha` -> "{version}-alpha" (rolling, same tag every push)
# - push to `beta` -> "{version}-beta" (rolling, same tag every push)
# - workflow_dispatch -> forced by the `channel` input (alpha|beta only)
resolve-channel:
name: Resolve release channel
runs-on: ubuntu-latest
outputs:
channel: ${{ steps.resolve.outputs.channel }}
tag_name: ${{ steps.resolve.outputs.tag_name }}
prerelease: ${{ steps.resolve.outputs.prerelease }}
steps:
- uses: actions/checkout@v4
- name: Resolve channel, version, and release tag
id: resolve
shell: bash
run: |
set -euo pipefail
BASE_VERSION=$(grep -m1 '^version' Cargo.toml | sed -E 's/version *= *"([^"]+)"/\1/')
if [[ "${{ github.ref }}" == refs/tags/v* ]]; then
# A pushed tag is authoritative — use it AS-IS (never recompute it
# from Cargo.toml, or the release would upload to a different tag than
# the one that triggered this run). The channel, and thus prerelease,
# is decided by the tag's suffix: v0.4.7-beta / v0.4.7-alpha are
# prereleases; a bare vX.Y.Z is the only thing that becomes stable.
TAG="${{ github.ref_name }}"
case "$TAG" in
*-alpha*) CHANNEL="alpha" ;;
*-beta*) CHANNEL="beta" ;;
*) CHANNEL="stable" ;;
esac
else
# No tag (workflow_dispatch, or a legacy branch push): pick the
# channel, then synthesize the rolling tag from Cargo.toml's version.
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
CHANNEL="${{ github.event.inputs.channel }}"
elif [ "${{ github.ref_name }}" = "beta" ]; then
CHANNEL="beta"
else
CHANNEL="alpha"
fi
TAG="v${BASE_VERSION}-${CHANNEL}"
fi
echo "Resolved channel=$CHANNEL tag=$TAG (base version $BASE_VERSION)"
echo "channel=$CHANNEL" >> "$GITHUB_OUTPUT"
echo "tag_name=$TAG" >> "$GITHUB_OUTPUT"
echo "prerelease=$([ "$CHANNEL" = "stable" ] && echo false || echo true)" >> "$GITHUB_OUTPUT"
check-and-test:
name: Check & Test
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Rust toolchain
uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
- name: Restore Cargo cache
uses: actions/cache@v4
with:
path: |
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: cargo-check-${{ hashFiles('**/Cargo.lock') }}
restore-keys: cargo-check-
- name: Install musl-tools
run: sudo apt-get update && sudo apt-get install -y musl-tools
- name: Create dummy dist for rust-embed
run: mkdir -p ostp-control/dist && touch ostp-control/dist/index.html
- name: cargo check
run: cargo check --workspace
- name: cargo test
run: cargo test --workspace --lib
publish-release-matrix: publish-release-matrix:
name: Release for ${{ matrix.target }} name: Release for ${{ matrix.target }}
needs: [check-and-test, resolve-channel]
runs-on: ${{ matrix.os }} runs-on: ${{ matrix.os }}
strategy: strategy:
fail-fast: false fail-fast: false
matrix: matrix:
include: include:
# ========================================== # -- Windows ------------------------------------------------------
# 🏁 WINDOWS ECOSYSTEM
# ==========================================
- os: windows-latest - os: windows-latest
target: x86_64-pc-windows-msvc target: x86_64-pc-windows-msvc
artifact_name: ostp.exe artifact_name: ostp.exe
release_name: ostp-windows-amd64.zip release_name: ostp-windows-amd64.zip
wintun_arch: amd64
- os: windows-latest - os: windows-latest
target: i686-pc-windows-msvc target: i686-pc-windows-msvc
artifact_name: ostp.exe artifact_name: ostp.exe
release_name: ostp-windows-386.zip release_name: ostp-windows-386.zip
wintun_arch: x86
- os: windows-latest - os: windows-latest
target: aarch64-pc-windows-msvc target: aarch64-pc-windows-msvc
artifact_name: ostp.exe artifact_name: ostp.exe
release_name: ostp-windows-arm64.zip release_name: ostp-windows-arm64.zip
wintun_arch: arm64
# ========================================== # -- macOS ---------------------------------------------------------
# 🍏 APPLE DARWIN (macOS)
# ==========================================
- os: macos-latest - os: macos-latest
target: x86_64-apple-darwin target: x86_64-apple-darwin
artifact_name: ostp artifact_name: ostp
release_name: ostp-darwin-amd64.tar.gz release_name: ostp-darwin-amd64.tar.gz
- os: macos-latest - os: macos-latest
target: aarch64-apple-darwin target: aarch64-apple-darwin
artifact_name: ostp artifact_name: ostp
release_name: ostp-darwin-arm64.tar.gz release_name: ostp-darwin-arm64.tar.gz
# ========================================== # -- Linux native --------------------------------------------------
# 🐧 LINUX & FreeBSD STANDARD
# ==========================================
- os: ubuntu-latest - os: ubuntu-latest
target: x86_64-unknown-linux-musl target: x86_64-unknown-linux-musl
artifact_name: ostp artifact_name: ostp
release_name: ostp-linux-amd64.tar.gz release_name: ostp-linux-amd64.tar.gz
- os: ubuntu-latest - os: ubuntu-latest
target: i686-unknown-linux-musl target: i686-unknown-linux-musl
artifact_name: ostp artifact_name: ostp
release_name: ostp-linux-386.tar.gz release_name: ostp-linux-386.tar.gz
use_cross: true
# -- Linux cross ---------------------------------------------------
- os: ubuntu-latest - os: ubuntu-latest
target: aarch64-unknown-linux-musl target: aarch64-unknown-linux-musl
artifact_name: ostp artifact_name: ostp
release_name: ostp-linux-arm64.tar.gz release_name: ostp-linux-arm64.tar.gz
use_cross: true
- os: ubuntu-latest - os: ubuntu-latest
target: armv7-unknown-linux-musleabihf target: armv7-unknown-linux-musleabihf
artifact_name: ostp artifact_name: ostp
release_name: ostp-linux-armv7.tar.gz release_name: ostp-linux-armv7.tar.gz
use_cross: true
- os: ubuntu-latest - os: ubuntu-latest
target: x86_64-unknown-freebsd target: x86_64-unknown-freebsd
artifact_name: ostp artifact_name: ostp
release_name: ostp-freebsd-amd64.tar.gz release_name: ostp-freebsd-amd64.tar.gz
use_cross: true
# ==========================================
# 🛰️ ROUTER & SPECIAL ARCHITECTURES (Cross)
# ==========================================
- os: ubuntu-latest - os: ubuntu-latest
target: mipsel-unknown-linux-musl target: mipsel-unknown-linux-musl
artifact_name: ostp artifact_name: ostp
release_name: ostp-linux-mipsle.tar.gz release_name: ostp-linux-mipsle.tar.gz
use_cross: true use_cross: true
toolchain: nightly
- os: ubuntu-latest - os: ubuntu-latest
target: riscv64gc-unknown-linux-gnu target: riscv64gc-unknown-linux-gnu
artifact_name: ostp artifact_name: ostp
release_name: ostp-linux-riscv64.tar.gz release_name: ostp-linux-riscv64.tar.gz
use_cross: true use_cross: true
# ==========================================
# 🤖 MOBILE & EMBEDDED SUITE (Cross)
# ==========================================
- os: ubuntu-latest
target: aarch64-linux-android
artifact_name: ostp
release_name: ostp-android-arm64.tar.gz
use_cross: true
steps: steps:
- name: Checkout code - name: Checkout code
uses: actions/checkout@v4 uses: actions/checkout@v4
- name: Initialize Rust ecosystem # -- Frontend Build -----------------------------------------------------
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20
- name: Build Web Panel (skip if no source; use committed dist/)
shell: bash
run: |
mkdir -p ostp-control/dist
cd ostp-control
if [ -f package.json ]; then
npm install && npm run build
else
echo "ostp-control has no package.json - using committed dist/"
[ -f dist/index.html ] || echo '<!doctype html><title>OSTP</title>' > dist/index.html
fi
# -- Rust toolchain -----------------------------------------------------
- name: Setup Rust toolchain
uses: dtolnay/rust-toolchain@stable
with:
toolchain: ${{ matrix.toolchain || 'stable' }}
targets: ${{ !matrix.use_cross && matrix.target || '' }}
# -- Cargo cache (shared per target) -----------------------------------
- name: Restore Cargo cache
uses: actions/cache@v4
with:
path: |
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: cargo-${{ matrix.target }}-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
cargo-${{ matrix.target }}-
# -- MUSL tools for native Linux musl builds ----------------------------
- name: Install musl-tools
if: ${{ matrix.os == 'ubuntu-latest' && !matrix.use_cross }}
run: sudo apt-get update && sudo apt-get install -y musl-tools
# -- Native build -------------------------------------------------------
- name: Build (native)
if: ${{ !matrix.use_cross }}
shell: bash
run: cargo build --release --target ${{ matrix.target }} --bin ostp
# -- Cross build --------------------------------------------------------
- name: Restore cross binary cache
if: ${{ matrix.use_cross }}
id: cross-cache
uses: actions/cache@v4
with:
path: ~/.cargo/bin/cross
key: cross-bin-${{ runner.os }}-v1
- name: Install cross (if not cached)
if: ${{ matrix.use_cross && steps.cross-cache.outputs.cache-hit != 'true' }}
# cross-rs's own source (not ours, not a dependency of ours) uses a
# macro-at-end-of-block pattern that trips rustc's
# semicolon_in_expressions_from_macros lint on current toolchains -
# harmless in cross's actual behavior, but `cargo install` compiles
# the installed package as the "local" crate, so dependency lint
# capping doesn't shield it. --cap-lints=warn is the standard escape
# hatch for building a third-party tool against a newer compiler than
# its own lint config assumed; it doesn't touch our own build.
run: RUSTFLAGS="--cap-lints=warn" cargo install cross --git https://github.com/cross-rs/cross.git --locked
- name: Build (cross)
if: ${{ matrix.use_cross }}
run: cross build --release --target ${{ matrix.target }} --bin ostp
# -- Driver dependencies ------------------------------------------------
- name: Download wintun (Windows)
if: ${{ matrix.os == 'windows-latest' }}
shell: pwsh
run: |
$ProgressPreference = 'SilentlyContinue'
$dir = "target/${{ matrix.target }}/release"
Invoke-WebRequest -Uri "https://www.wintun.net/builds/wintun-0.14.1.zip" -OutFile "$dir/wt.zip"
Expand-Archive "$dir/wt.zip" -DestinationPath "$dir/wt_tmp" -Force
Get-ChildItem "$dir/wt_tmp" -Filter "wintun.dll" -Recurse | Where-Object { $_.FullName -match 'bin[\\/]${{ matrix.wintun_arch }}[\\/]' } | Copy-Item -Destination "$dir/"
Remove-Item "$dir/wt.zip","$dir/wt_tmp" -Recurse -Force
# -- Package ------------------------------------------------------------
- name: Package (Windows)
if: ${{ matrix.os == 'windows-latest' }}
shell: pwsh
run: |
$dir = "target/${{ matrix.target }}/release"
$files = @("ostp.exe")
if (Test-Path "$dir/wintun.dll") { $files += "wintun.dll" }
Push-Location $dir
Compress-Archive -Path $files -DestinationPath "../../../${{ matrix.release_name }}" -Force
Pop-Location
- name: Package (Unix)
if: ${{ matrix.os != 'windows-latest' }}
run: |
dir="target/${{ matrix.target }}/release"
FILES="${{ matrix.artifact_name }}"
tar -czf "${{ matrix.release_name }}" -C "$dir" $FILES
# -- Upload -------------------------------------------------------------
- name: Upload to GitHub Release
uses: softprops/action-gh-release@v2
with:
# Computed once in resolve-channel so every platform/job in this run
# lands on the exact same tag: "{version}-alpha" / "{version}-beta"
# for rolling channel pushes, or the pushed "vX.Y.Z" tag as-is for a
# real stable release.
tag_name: ${{ needs.resolve-channel.outputs.tag_name }}
prerelease: ${{ needs.resolve-channel.outputs.prerelease }}
files: ${{ matrix.release_name }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
build-windows-gui:
name: Build Windows GUI (Tauri) - ${{ matrix.arch }}
needs: [check-and-test, resolve-channel]
runs-on: windows-latest
strategy:
matrix:
include:
- arch: amd64
target: x86_64-pc-windows-msvc
- arch: arm64
target: aarch64-pc-windows-msvc
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20
- name: Setup Rust toolchain
uses: dtolnay/rust-toolchain@stable uses: dtolnay/rust-toolchain@stable
with: with:
targets: ${{ matrix.target }} targets: ${{ matrix.target }}
- name: Activate rust compilation caching - name: Install Tauri CLI
uses: swatinem/rust-cache@v2 run: npm install -g @tauri-apps/cli
- name: Setup local MUSL linker dependencies - name: Cache cargo
if: matrix.os == 'ubuntu-latest' && !matrix.use_cross uses: actions/cache@v4
run: sudo apt-get update && sudo apt-get install -y musl-tools with:
path: |
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
ostp-gui/src-tauri/target/
key: cargo-windows-gui-${{ matrix.target }}-${{ hashFiles('**/Cargo.lock') }}
# Without a prefix fallback this cache NEVER restored on a release:
# cutting a release rewrites every Cargo.lock (version bump), which
# changes hashFiles(), which misses the exact key — so each release
# rebuilt every dependency from scratch. That is why the GUI jobs ran
# 2-4x longer than the plain release targets, which had this all along.
restore-keys: |
cargo-windows-gui-${{ matrix.target }}-
- name: Execute Standard Native Compilation - name: Download wintun
if: !matrix.use_cross
run: cargo build --release --target ${{ matrix.target }} --bin ostp
- name: Execute Specialized Cross-Compilation
if: matrix.use_cross
run: |
cargo install cross --git https://github.com/cross-rs/cross.git
cross build --release --target ${{ matrix.target }} --bin ostp
- name: Package release artifact (Windows)
if: matrix.os == 'windows-latest'
shell: pwsh shell: pwsh
run: | run: |
cd target/${{ matrix.target }}/release $ProgressPreference = 'SilentlyContinue'
Compress-Archive -Path ${{ matrix.artifact_name }} -DestinationPath ../../../${{ matrix.release_name }}
# Download wintun
New-Item -ItemType Directory -Force -Path "target/${{ matrix.target }}/release"
Invoke-WebRequest -Uri "https://www.wintun.net/builds/wintun-0.14.1.zip" -OutFile "target/wt.zip"
Expand-Archive "target/wt.zip" -DestinationPath "target/wt_tmp" -Force
Get-ChildItem "target/wt_tmp" -Filter "wintun.dll" -Recurse | Where-Object { $_.FullName -match 'bin[\\/]${{ matrix.arch }}[\\/]' } | Copy-Item -Destination "target/${{ matrix.target }}/release/wintun.dll" -Force
- name: Package release artifact (Unix Systems) - name: Build Tauri App
if: matrix.os != 'windows-latest' working-directory: ostp-gui
run: | run: |
cd target/${{ matrix.target }}/release npm install
tar -czf ../../../${{ matrix.release_name }} ${{ matrix.artifact_name }} cargo build -p ostp-tun-helper --release --target ${{ matrix.target }}
npx tauri build --no-bundle --target ${{ matrix.target }}
- name: Inject artifact to Global GitHub Release Assets - name: Package Portable ZIP
shell: pwsh
run: |
$dir = "ostp-gui-dist"
New-Item -ItemType Directory -Force -Path $dir
Copy-Item "ostp-gui/src-tauri/target/${{ matrix.target }}/release/ostp-gui.exe" $dir
Copy-Item "target/${{ matrix.target }}/release/ostp-tun-helper.exe" $dir
Copy-Item "target/${{ matrix.target }}/release/wintun.dll" $dir
Compress-Archive -Path "$dir/*" -DestinationPath "ostp-windows-gui-${{ matrix.arch }}.zip" -Force
- name: Upload to GitHub Release
uses: softprops/action-gh-release@v2 uses: softprops/action-gh-release@v2
with: with:
files: ${{ matrix.release_name }} # Computed once in resolve-channel so every platform/job in this run
# lands on the exact same tag: "{version}-alpha" / "{version}-beta"
# for rolling channel pushes, or the pushed "vX.Y.Z" tag as-is for a
# real stable release.
tag_name: ${{ needs.resolve-channel.outputs.tag_name }}
prerelease: ${{ needs.resolve-channel.outputs.prerelease }}
files: ostp-windows-gui-${{ matrix.arch }}.zip
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
build-linux-gui:
name: Build Linux GUI (Tauri) - ${{ matrix.arch }}
needs: [check-and-test, resolve-channel]
runs-on: ubuntu-latest
strategy:
matrix:
include:
- arch: amd64
target: x86_64-unknown-linux-gnu
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20
- name: Setup Rust toolchain
uses: dtolnay/rust-toolchain@stable
with:
targets: ${{ matrix.target }}
- name: Install Linux Dependencies
run: |
sudo apt-get update
sudo apt-get install -y libwebkit2gtk-4.1-dev libappindicator3-dev librsvg2-dev patchelf
- name: Install Tauri CLI
run: npm install -g @tauri-apps/cli
- name: Cache cargo
uses: actions/cache@v4
with:
path: |
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
ostp-gui/src-tauri/target/
key: cargo-linux-gui-${{ matrix.target }}-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
cargo-linux-gui-${{ matrix.target }}-
- name: Build Tauri App
working-directory: ostp-gui
run: |
npm install
# TUN mode shells out to this helper, elevated via pkexec. Only the
# Windows job used to build it, so the Linux package shipped without
# it and TUN could never start.
cargo build -p ostp-tun-helper --release --target ${{ matrix.target }} --manifest-path ../Cargo.toml
npx tauri build --no-bundle --target ${{ matrix.target }}
- name: Package Portable Tarball
run: |
set -euo pipefail
mkdir ostp-linux-gui-${{ matrix.arch }}
cp ostp-gui/src-tauri/target/${{ matrix.target }}/release/ostp-gui ostp-linux-gui-${{ matrix.arch }}/
# The GUI looks for the helper next to its own executable first.
cp target/${{ matrix.target }}/release/ostp-tun-helper ostp-linux-gui-${{ matrix.arch }}/
tar -czf ostp-linux-gui-${{ matrix.arch }}.tar.gz ostp-linux-gui-${{ matrix.arch }}
- name: Upload to GitHub Release
uses: softprops/action-gh-release@v2
with:
# Computed once in resolve-channel so every platform/job in this run
# lands on the exact same tag: "{version}-alpha" / "{version}-beta"
# for rolling channel pushes, or the pushed "vX.Y.Z" tag as-is for a
# real stable release.
tag_name: ${{ needs.resolve-channel.outputs.tag_name }}
prerelease: ${{ needs.resolve-channel.outputs.prerelease }}
files: ostp-linux-gui-${{ matrix.arch }}.tar.gz
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
build-macos-gui:
name: Build macOS GUI (Tauri) - ${{ matrix.arch }}
needs: [check-and-test, resolve-channel]
runs-on: macos-latest
strategy:
matrix:
include:
- arch: amd64
target: x86_64-apple-darwin
- arch: arm64
target: aarch64-apple-darwin
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20
- name: Setup Rust toolchain
uses: dtolnay/rust-toolchain@stable
with:
targets: ${{ matrix.target }}
- name: Install Tauri CLI
run: npm install -g @tauri-apps/cli
- name: Cache cargo
uses: actions/cache@v4
with:
path: |
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
ostp-gui/src-tauri/target/
key: cargo-macos-gui-${{ matrix.target }}-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
cargo-macos-gui-${{ matrix.target }}-
- name: Build Tauri App
working-directory: ostp-gui
run: |
npm install
npx tauri build --no-bundle --target ${{ matrix.target }}
- name: Package Portable Tarball
run: |
mkdir ostp-macos-gui-${{ matrix.arch }}
cp ostp-gui/src-tauri/target/${{ matrix.target }}/release/ostp-gui ostp-macos-gui-${{ matrix.arch }}/
tar -czf ostp-macos-gui-${{ matrix.arch }}.tar.gz ostp-macos-gui-${{ matrix.arch }}
- name: Upload to GitHub Release
uses: softprops/action-gh-release@v2
with:
# Computed once in resolve-channel so every platform/job in this run
# lands on the exact same tag: "{version}-alpha" / "{version}-beta"
# for rolling channel pushes, or the pushed "vX.Y.Z" tag as-is for a
# real stable release.
tag_name: ${{ needs.resolve-channel.outputs.tag_name }}
prerelease: ${{ needs.resolve-channel.outputs.prerelease }}
files: ostp-macos-gui-${{ matrix.arch }}.tar.gz
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
build-android:
name: Build Android Client (Flutter) - ${{ matrix.arch }}
needs: [check-and-test, resolve-channel]
runs-on: ubuntu-latest
strategy:
matrix:
include:
- arch: arm64-v8a
rust_target: aarch64-linux-android
flutter_target: android-arm64
- arch: armeabi-v7a
rust_target: armv7-linux-androideabi
flutter_target: android-arm
steps:
- uses: actions/checkout@v4
- name: Setup Java
uses: actions/setup-java@v3
with:
distribution: 'zulu'
java-version: '17'
- name: Setup Flutter
uses: subosito/flutter-action@v2
with:
flutter-version: '3.41.6'
channel: 'stable'
- name: Setup Rust toolchain
uses: dtolnay/rust-toolchain@stable
with:
targets: ${{ matrix.rust_target }}
- name: Setup Android NDK
uses: nttld/setup-ndk@v1
with:
ndk-version: r26b
# The Android jobs had no Rust caching at all, so every release recompiled
# the whole ostp-jni dependency graph from scratch — the main reason these
# were among the slowest jobs in the matrix.
- name: Cache cargo
uses: actions/cache@v4
with:
path: |
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: cargo-android-${{ matrix.arch }}-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
cargo-android-${{ matrix.arch }}-
# cargo-ndk was built from source on every run. Cache the binary the same
# way the cross-compilation jobs already cache `cross`.
- name: Restore cargo-ndk binary cache
id: cargo-ndk-cache
uses: actions/cache@v4
with:
path: ~/.cargo/bin/cargo-ndk
key: cargo-ndk-bin-${{ runner.os }}-v1
- name: Install cargo-ndk (if not cached)
if: steps.cargo-ndk-cache.outputs.cache-hit != 'true'
run: cargo install cargo-ndk --locked
- name: Build Android APK
shell: bash
working-directory: ostp-flutter
env:
OSTP_KEYSTORE_B64: ${{ secrets.ANDROID_KEYSTORE_BASE64 }}
OSTP_KEYSTORE_PASSWORD: ${{ secrets.ANDROID_KEYSTORE_PASSWORD }}
OSTP_KEY_ALIAS: ${{ secrets.ANDROID_KEY_ALIAS }}
OSTP_KEY_PASSWORD: ${{ secrets.ANDROID_KEY_PASSWORD }}
run: |
set -euo pipefail
# 1. Materialise the upload keystore from secrets. Android keys an app
# by applicationId + signing key and refuses to update across a key
# change, so every published build MUST use this one key. Releases
# used to fall through to the per-machine debug keystore, which on
# ephemeral CI runners meant a different random key every build -
# hence "App not installed" on upgrade.
if [ -z "${OSTP_KEYSTORE_B64:-}" ]; then
echo "::error::ANDROID_KEYSTORE_BASE64 secret is not set. Refusing to publish a"
echo "::error::debug-signed APK: users could not update over it and the key is"
echo "::error::not reproducible. See docs for the one-time keystore setup."
exit 1
fi
export OSTP_KEYSTORE_PATH="$RUNNER_TEMP/ostp-upload.jks"
# Strip any stray CR/LF before decoding: the secret is pasted from a
# shell whose line endings we don't control, and a single trailing \r
# is enough to corrupt the decode.
printf '%s' "$OSTP_KEYSTORE_B64" | tr -d '\r\n' | base64 -d > "$OSTP_KEYSTORE_PATH"
# Verify the keystore opens BEFORE spending four minutes on Gradle only
# to fail at the packaging step. The size/SHA-256 are safe to print (a
# hash reveals nothing) and let the operator compare against the local
# file to tell a transport problem apart from a wrong password.
echo "keystore: $(stat -c%s "$OSTP_KEYSTORE_PATH") bytes, sha256 $(sha256sum "$OSTP_KEYSTORE_PATH" | cut -d' ' -f1)"
if ! keytool -list -keystore "$OSTP_KEYSTORE_PATH" \
-storepass "$OSTP_KEYSTORE_PASSWORD" >/dev/null 2>&1; then
echo "::error::The keystore did not open with ANDROID_KEYSTORE_PASSWORD."
echo "::error::If the SHA-256 above matches your local ostp-upload.jks, the file"
echo "::error::arrived intact and the password secret itself is wrong - note that"
echo "::error::PowerShell expands \$ inside double quotes, so a password containing"
echo "::error::one gets mangled unless it was set with single quotes."
exit 1
fi
if ! keytool -list -keystore "$OSTP_KEYSTORE_PATH" \
-storepass "$OSTP_KEYSTORE_PASSWORD" -alias "$OSTP_KEY_ALIAS" >/dev/null 2>&1; then
echo "::error::Keystore opened, but it has no key under ANDROID_KEY_ALIAS."
echo "::error::Aliases present in the keystore:"
keytool -list -keystore "$OSTP_KEYSTORE_PATH" -storepass "$OSTP_KEYSTORE_PASSWORD" \
| grep -i "PrivateKeyEntry" || true
exit 1
fi
# 2. Compile JNI
mkdir -p android/app/src/main/jniLibs/${{ matrix.arch }}
cd ../ostp-jni
cargo ndk -t ${{ matrix.arch }} -o "../ostp-flutter/android/app/src/main/jniLibs" build --release
cd ../ostp-flutter
# 3. Build Flutter APK
flutter build apk --release --target-platform ${{ matrix.flutter_target }}
# 4. Fail loudly if the APK somehow still came out debug-signed, rather
# than shipping another un-updatable build.
APK=build/app/outputs/flutter-apk/app-release.apk
if "$ANDROID_HOME"/build-tools/*/apksigner verify --print-certs "$APK" 2>/dev/null \
| grep -qi "CN=Android Debug"; then
echo "::error::APK is signed with the Android debug certificate - aborting."
exit 1
fi
# 5. Copy to output
cp "$APK" ostp-android-${{ matrix.arch }}.apk
- name: Upload to GitHub Release
uses: softprops/action-gh-release@v2
with:
# Computed once in resolve-channel so every platform/job in this run
# lands on the exact same tag: "{version}-alpha" / "{version}-beta"
# for rolling channel pushes, or the pushed "vX.Y.Z" tag as-is for a
# real stable release.
tag_name: ${{ needs.resolve-channel.outputs.tag_name }}
prerelease: ${{ needs.resolve-channel.outputs.prerelease }}
files: ostp-flutter/ostp-android-${{ matrix.arch }}.apk
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

43
.gitignore vendored
View File

@ -1,16 +1,59 @@
/target/ /target/
/target_build/ /target_build/
/target_linux/
/dist/ /dist/
**/*.rs.bk **/*.rs.bk
.idea/ .idea/
.vscode/ .vscode/
**/node_modules/
# Binaries & libraries
*.exe *.exe
*.dll *.dll
*.so *.so
*.dylib *.dylib
*.pdb *.pdb
# Archives & temp extraction folders
*.zip
*_temp/
*_new/
# Local scripts & test artifacts
test_route.ps1
# Config & secrets
config.json config.json
wintun.dll wintun.dll
# Android signing keys. The upload keystore is the ONE key every published APK
# must be signed with (Android refuses to update an app across a key change),
# so losing or leaking it is unrecoverable — it can never be committed.
*.jks
*.keystore
key.properties
# Server runtime cache (public IP autodetect) — must never be committed,
# it's regenerated locally and leaks whatever host it ran on last.
.ostp_public_ip
# Logs
*.log *.log
# Dev notes (not for repo)
.ai-rules.md .ai-rules.md
turn-harvesting-idea.md turn-harvesting-idea.md
# Private tooling (closed-source)
ostp-prober/
ostp-lab/
ostp-brain/
# Management panel built assets (built separately; dummy dist created for rust-embed build)
ostp-control/
.agents/
netstack-smoltcp/
dnstt/
ostp-web/

6
.release-state.json Normal file
View File

@ -0,0 +1,6 @@
{
"target_version": "0.4.4",
"branch": "master",
"alpha_iteration": 0,
"beta_iteration": 0
}

159
CONTRIBUTING.md Normal file
View File

@ -0,0 +1,159 @@
# Contributing to OSTP
Thank you for your interest in contributing to **OSTP (Ospab Stealth Transport Protocol)**! We welcome contributions from developers, security researchers, testers, and documentation writers of all skill levels.
By contributing to this project, you agree to abide by our code of conduct and license terms.
---
## Table of Contents
1. [Development Setup](#development-setup)
2. [Project Structure](#project-structure)
3. [Branch Strategy](#branch-strategy)
4. [Development Workflow](#development-workflow)
5. [Commit Message Conventions](#commit-message-conventions)
6. [Coding Guidelines](#coding-guidelines)
7. [Submitting Pull Requests](#submitting-pull-requests)
8. [Security Vulnerabilities](#security-vulnerabilities)
---
## Development Setup
To build and test OSTP locally, you will need:
* **Rust Toolchain (1.75+)**: Install via [rustup](https://rustup.rs/).
* **Node.js (18+) & npm**: Required to build the frontend control panel (`ostp-control`) and compile Tauri GUI resources.
* **Git**: For version control.
### Building the Project
1. **Clone the repository**:
```bash
git clone https://github.com/ospab/ostp.git
cd ostp
```
2. **Build the entire Cargo workspace**:
```bash
cargo build
```
`ostp-control` (the web panel) is only needed if you're working on it
specifically - the server build embeds a dummy `dist/` via `rust-embed`
otherwise, so this step is not required for day-to-day core/client/server
work. If you *are* touching the panel:
```bash
cd ostp-control && npm install && npm run build && cd ..
```
3. **Run tests**:
```bash
cargo test --workspace
```
---
## Project Structure
The repository is organized as a Cargo workspace containing the following crates:
* [`ostp-core/`](file:///d:/ospab-projects/ostp/ostp-core): Core protocol logic, including packet formatting, serialization, selective ACK/NACK (ARQ) state machine, and the Noise protocol (`Noise_NNpsk0_25519_ChaChaPoly_BLAKE2s`) handshake.
* [`ostp-client/`](file:///d:/ospab-projects/ostp/ostp-client): Client implementations, including SOCKS5/HTTP local proxies, the native OSTP TUN interface routing, and split-tunneling bypass mechanisms.
* [`ostp-server/`](file:///d:/ospab-projects/ostp/ostp-server): Server logic, session dispatcher, anti-probing fallback server proxying, access key database, and the REST API for control panel communication.
* [`ostp-control/`](file:///d:/ospab-projects/ostp/ostp-control): A modern web dashboard for server administration (user management, real-time metrics, bandwidth limits).
* [`ostp-gui/`](file:///d:/ospab-projects/ostp/ostp-gui): Tauri-based desktop GUI application for Windows and Linux.
* [`ostp-flutter/`](file:///d:/ospab-projects/ostp/ostp-flutter): Mobile client code for Android platforms.
---
## Branch Strategy
The repository runs three long-lived branches, in increasing order of stability:
| Branch | Role |
|---|---|
| `alpha` | Active development. All feature work and fixes land here first. |
| `beta` | Periodically fast-forwarded from `alpha` once it's had some soak time. Ships as the `{version}-beta` release channel. |
| `master` | Fast-forwarded from `beta` when it's proven stable. Real, tagged releases (`vX.Y.Z`) are cut from here. |
`beta` and `master` are **never** committed to directly - they only ever move forward by fast-forwarding from the branch below them. This means promotion is always a plain `git merge` with zero conflicts by construction: don't `git merge`/rebase feature work directly onto `beta` or `master`.
**Contributor PRs target `alpha`**, not `master`.
---
## Development Workflow
1. **Check for existing issues** or open a new one to discuss proposed changes before starting work.
2. **Fork the repository** and create a new branch from `alpha`:
```bash
git checkout alpha
git checkout -b feat/your-feature-name
```
3. **Implement your changes**, ensuring you write appropriate unit or integration tests.
4. **Format your code**:
```bash
cargo fmt --all
```
5. **Run linter checks**:
```bash
cargo clippy --workspace --all-targets -- -D warnings
```
6. **Ensure all tests pass**:
```bash
cargo test --workspace
```
---
## Commit Message Conventions
```
<type>(<scope>): <short, imperative summary>
<optional body - explain WHY, not what; the diff already shows what changed>
```
- **Type** - one of: `feat` (new capability), `fix` (bug fix), `docs`, `refactor` (no behavior change), `perf`, `test`, `chore` (deps/tooling/version bumps), `ci`, `security`.
- **Scope** (optional) - the crate or area touched: `client`, `server`, `core`, `gui`, `flutter`, `ci`, `docs`, etc. e.g. `fix(client): ...`.
- **Summary** - imperative mood ("add", not "added"/"adds"), no trailing period, ideally under ~70 characters.
- **Body** - only when the *why* isn't obvious from the diff: a prior bug this fixes, a constraint that shaped the approach, a tradeoff you made. Don't restate what the diff already shows. Wrap at ~72 columns.
```
fix(server): drop junk frames by per-key marker instead of a global one
A fixed 4-byte marker on every junk packet is itself a DPI signature any
observer can filter on across every OSTP deployment. Derive the marker
from the access key (HKDF, same scheme as obfuscation_key/psk) so it's
per-user and indistinguishable from the packet's own random payload.
```
Multiple unrelated changes belong in separate commits, not one bundled commit - it keeps `git bisect` and review useful. Squash-merge is fine for a PR with a few "fix typo" / "address review" commits, but don't squash logically distinct changes together.
---
## Coding Guidelines
* **Safety**: Avoid using `unsafe` blocks unless absolutely necessary for low-level system bindings (e.g., FFI configurations like `setsockopt`). When using `unsafe`, add safety doc comments explaining why it is safe.
* **Documentation**: Document public modules, structs, and functions. Maintain comment integrity across codebase changes.
* **Logging**: Use the `tracing` framework for structured logging. Avoid `println!` for production logs.
* **Aesthetics**: When editing GUI or Web components, adhere to premium, modern web design aesthetics (vibrant color palettes, glassmorphism, responsive grids).
---
## Submitting Pull Requests
1. Push your branch to your GitHub fork:
```bash
git push origin feat/your-feature-name
```
2. Open a Pull Request (PR) targeting the `alpha` branch (see [Branch Strategy](#branch-strategy) - `master` only receives fast-forwards from `beta`, never direct PRs).
3. In your PR description, explain the rationale behind your changes, what was fixed/added, and how it was tested.
4. Verify that GitHub Actions CI runs successfully on your PR.
---
## Security Vulnerabilities
If you discover a security-related vulnerability, please do **not** open a public issue. Instead, report it privately by emailing the core maintainers at [gvoprgrg@gmail.com](mailto:gvoprgrg@gmail.com). We will coordinate a swift disclosure and fix.

160
CONTRIBUTING.ru.md Normal file
View File

@ -0,0 +1,160 @@
# Участие в разработке OSTP
Спасибо за интерес к участию в разработке **OSTP (Ospab Stealth Transport Protocol)**! Мы рады любой помощи: от написания кода и тестирования до работы над документацией и проведения аудита безопасности.
Присылая изменения в проект, вы соглашаетесь соблюдать правила нашего сообщества и условия лицензии.
---
## Содержание
1. [Подготовка окружения](#подготовка-окружения)
2. [Структура проекта](#структура-проекта)
3. [Стратегия веток](#стратегия-веток)
4. [Процесс разработки](#процесс-разработки)
5. [Оформление коммитов](#оформление-коммитов)
6. [Правила оформления кода](#правила-оформления-кода)
7. [Создание Pull Request](#создание-pull-request)
8. [Уязвимости безопасности](#уязвимости-безопасности)
---
## Подготовка окружения
Для локальной сборки и тестирования OSTP вам понадобятся:
* **Rust Toolchain (1.75+)**: Рекомендуется установить через [rustup](https://rustup.rs/).
* **Node.js (18+) и npm**: Необходимы для сборки веб-панели управления (`ostp-control`) и сборки интерфейса Tauri.
* **Git**: Для контроля версий.
### Сборка проекта
1. **Клонируйте репозиторий**:
```bash
git clone https://github.com/ospab/ostp.git
cd ostp
```
2. **Соберите весь Cargo-workspace**:
```bash
cargo build
```
`ostp-control` (веб-панель) нужна только если вы работаете конкретно над
ней - в остальных случаях сервер собирается с пустым `dist/` через
`rust-embed`, и этот шаг не нужен для повседневной работы над
core/client/server. Если вы всё же трогаете панель:
```bash
cd ostp-control && npm install && npm run build && cd ..
```
3. **Запустите тесты**:
```bash
cargo test --workspace
```
---
## Структура проекта
Репозиторий представляет собой единый Cargo-workspace со следующими компонентами:
* [`ostp-core/`](file:///d:/ospab-projects/ostp/ostp-core): Базовая логика протокола: форматирование пакетов, сериализация, конечный автомат выборочного подтверждения (ARQ/ACK/NACK) и рукопожатие Noise (`Noise_NNpsk0_25519_ChaChaPoly_BLAKE2s`).
* [`ostp-client/`](file:///d:/ospab-projects/ostp/ostp-client): Клиентская часть: локальные SOCKS5/HTTP прокси-серверы, нативный OSTP TUN-интерфейс (через драйвер `wintun`) и реализация раздельного туннелирования для прямого обхода трафика.
* [`ostp-server/`](file:///d:/ospab-projects/ostp/ostp-server): Серверная часть: диспетчеризация сессий, маскировка под классические веб-серверы при активном сканировании, база данных ключей доступа и REST API панели управления.
* [`ostp-control/`](file:///d:/ospab-projects/ostp/ostp-control): Панель администратора (пользователи, статистика трафика в реальном времени, лимиты скорости и объема данных).
* [`ostp-gui/`](file:///d:/ospab-projects/ostp/ostp-gui): Настольное приложение-клиент для Windows и Linux на платформе Tauri.
* [`ostp-flutter/`](file:///d:/ospab-projects/ostp/ostp-flutter): Мобильный клиент для платформы Android.
---
## Стратегия веток
В репозитории три долгоживущие ветки, по возрастанию стабильности:
| Ветка | Роль |
|---|---|
| `alpha` | Активная разработка. Вся новая работа и фиксы попадают сюда первыми. |
| `beta` | Периодически перематывается вперёд (fast-forward) от `alpha`, когда та немного «отлежалась». Собирается в канал релиза `{версия}-beta`. |
| `master` | Перематывается вперёд от `beta`, когда та доказала стабильность. Настоящие тегированные релизы (`vX.Y.Z`) режутся отсюда. |
В `beta` и `master` **никогда** не коммитят напрямую - они только перематываются вперёд от ветки уровнем ниже. Это значит, что промоушен - всегда обычный `git merge` без единого конфликта по построению: не мержите/не ребейзьте свою фичу прямо в `beta` или `master`.
**PR от контрибьюторов нацелены на `alpha`**, не на `master`.
---
## Процесс разработки
1. **Проверьте существующие задачи** или откройте новую тему (Issue) для обсуждения предлагаемых изменений.
2. **Сделайте fork репозитория** и создайте новую ветку от `alpha`:
```bash
git checkout alpha
git checkout -b feat/имя-вашей-фичи
```
3. **Внесите необходимые изменения** и добавьте соответствующие модульные или интеграционные тесты.
4. **Выровняйте форматирование кода**:
```bash
cargo fmt --all
```
5. **Запустите статический анализатор**:
```bash
cargo clippy --workspace --all-targets -- -D warnings
```
6. **Убедитесь, что все тесты проходят**:
```bash
cargo test --workspace
```
---
## Оформление коммитов
```
<тип>(<область>): <краткое описание в повелительном наклонении>
<опционально: тело - объясняет ПОЧЕМУ, а не что; диф и так показывает что изменилось>
```
- **Тип** - один из: `feat` (новая функциональность), `fix` (исправление бага), `docs`, `refactor` (без изменения поведения), `perf`, `test`, `chore` (зависимости/тулинг/версии), `ci`, `security`.
- **Область** (опционально) - крейт или часть проекта: `client`, `server`, `core`, `gui`, `flutter`, `ci`, `docs` и т.д., например `fix(client): ...`.
- **Краткое описание** - повелительное наклонение ("добавь", а не "добавил"/"добавляет"), без точки в конце, желательно до ~70 символов.
- **Тело** - только когда причина не очевидна из дифа: какой баг это чинит, какое ограничение определило подход, на какой trade-off вы пошли. Не пересказывайте то, что и так видно в дифе. Перенос строк на ~72 символах.
```
fix(server): отбрасывать junk-фреймы по маркеру для каждого ключа, а не глобальному
Фиксированный 4-байтовый маркер на каждом junk-пакете сам по себе - сигнатура
DPI, по которой можно фильтровать любого наблюдателя во всех деплойментах OSTP
сразу. Выводим маркер из access_key (HKDF, та же схема что у
obfuscation_key/psk), чтобы он был индивидуальным для ключа и неотличимым от
случайной полезной нагрузки пакета.
```
Несколько несвязанных изменений - это несколько отдельных коммитов, а не один сборный. Это сохраняет пользу от `git bisect` и код-ревью. Squash-merge подходит для PR с парой коммитов вроде "fix typo" / "address review", но не сквошьте вместе логически разные изменения.
---
## Правила оформления кода
* **Безопасность (Safety)**: Избегайте использования блоков `unsafe` везде, где это возможно. Допускается их использование только для низкоуровневых системных вызовов (например, FFI-настройки сокетов `setsockopt`). Любой блок `unsafe` должен сопровождаться комментарием `// SAFETY: ...`.
* **Документация**: Пишите документацию для публичных модулей, структур и методов. Сохраняйте целостность комментариев при рефакторинге.
* **Логирование**: Используйте фреймворк `tracing` для структурированного логирования. Не используйте `println!` в рабочем коде.
* **Дизайн**: При изменении веб-интерфейсов или GUI следуйте современным визуальным трендам (плавные анимации, сбалансированная цветовая гамма, адаптивная верстка).
---
## Создание Pull Request
1. Отправьте ветку в ваш fork-репозиторий:
```bash
git push origin feat/имя-вашей-фичи
```
2. Создайте Pull Request (PR) в ветку `alpha` основного репозитория (см. [Стратегия веток](#стратегия-веток) - `master` получает только fast-forward от `beta`, PR туда не принимаются напрямую).
3. Подробно опишите внесенные изменения: какая проблема решается, как проводилось тестирование и на каких платформах проверялась сборка.
4. Убедитесь, что автоматическое тестирование (GitHub Actions CI) завершилось успешно.
---
## Уязвимости безопасности
Если вы обнаружили уязвимость, пожалуйста, **не** публикуйте её в открытых Issue. Вместо этого отправьте отчёт разработчикам на почту [gvoprgrg@gmail.com](mailto:gvoprgrg@gmail.com) для координации закрытого исправления.

2290
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -4,24 +4,25 @@ members = [
"ostp-client", "ostp-client",
"ostp-server", "ostp-server",
"ostp-jni", "ostp", "ostp-jni", "ostp",
] "ostp-tun-helper"
, "ostp-tun"]
exclude = ["ostp-gui/src-tauri", "ostp-brain", "ostp-prober"]
resolver = "2" resolver = "2"
[workspace.package] [workspace.package]
edition = "2021" edition = "2021"
license = "BSL 1.1" license = "AGPL-3.0"
version = "0.1.9" version = "0.4.4"
[workspace.dependencies] [workspace.dependencies]
anyhow = "1.0" anyhow = "1.0"
async-trait = "0.1"
bytes = "1.6" bytes = "1.6"
chacha20poly1305 = "0.10" chacha20poly1305 = "0.10"
rand = "0.8" rand = "0.8"
rand_distr = "0.4" snow = { version = "0.9", features = ["risky-raw-split"] }
snow = "0.9"
thiserror = "1.0" thiserror = "1.0"
tokio = { version = "1.37", features = ["rt-multi-thread", "macros", "net", "time", "io-util", "sync", "signal"] } tokio = { version = "1.37", features = ["rt-multi-thread", "macros", "net", "time", "io-util", "sync", "signal"] }
tracing = "0.1" tracing = "0.1"
x25519-dalek = "2"
sha2 = "0.10" sha2 = "0.10"
hmac = "0.12"
portable-atomic = "1.10"

20
Cross.toml Normal file
View File

@ -0,0 +1,20 @@
# Professional Cross-Compilation Target Environment Registry
[target.mipsel-unknown-linux-musl]
image = "ghcr.io/cross-rs/mipsel-unknown-linux-musl:edge"
build-std = ["core", "std", "alloc", "proc_macro"]
[target.riscv64gc-unknown-linux-gnu]
image = "ghcr.io/cross-rs/riscv64gc-unknown-linux-gnu:edge"
[target.aarch64-linux-android]
image = "ghcr.io/cross-rs/aarch64-linux-android:edge"
[target.x86_64-unknown-freebsd]
image = "ghcr.io/cross-rs/x86_64-unknown-freebsd:edge"
[target.aarch64-unknown-linux-musl]
image = "ghcr.io/cross-rs/aarch64-unknown-linux-musl:edge"
[target.armv7-unknown-linux-musleabihf]
image = "ghcr.io/cross-rs/armv7-unknown-linux-musleabihf:edge"

701
LICENSE
View File

@ -1,74 +1,661 @@
Business Source License 1.1 GNU AFFERO GENERAL PUBLIC LICENSE
Version 3, 19 November 2007
Parameters Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Licensor: Ospab Foundation (represented by Syralev Georgiy) Preamble
Licensed Work: The Ospab Stealth Transport Protocol (OSTP) and all
associated workspace crates, utilities, and documents.
Additional Use Grant: The Licensor hereby grants you the right to copy,
modify, create derivative works, redistribute, and
make non-production and non-commercial use of the
Licensed Work. You are also permitted to use the
Licensed Work in production for personal, private
utility and non-profit organizations.
Change Date: May 14, 2030
Change License: MIT License (as defined below)
----------------------------------------------------------------------------------- The GNU Affero General Public License is a free, copyleft license for
software and other kinds of works, specifically designed to ensure
cooperation with the community in the case of network server software.
Terms The licenses for most software and other practical works are designed
to take away your freedom to share and change the works. By contrast,
our General Public Licenses are intended to guarantee your freedom to
share and change all versions of a program--to make sure it remains free
software for all its users.
1. The Licensor hereby grants you the right to copy, modify, create derivative works, When we speak of free software, we are referring to freedom, not
redistribute, and make use of the Licensed Work only as permitted by the price. Our General Public Licenses are designed to make sure that you
Additional Use Grant. have the freedom to distribute copies of free software (and charge for
them if you wish), that you receive source code or can get it if you
want it, that you can change the software or use pieces of it in new
free programs, and that you know you can do these things.
2. The Licensor hereby grants you the right to copy, modify, create derivative works, Developers that use our General Public Licenses protect your rights
redistribute, and make use of the Licensed Work under the terms of the Change with two steps: (1) assert copyright on the software, and (2) offer
License on and after the Change Date. you this License which gives you legal permission to copy, distribute
and/or modify the software.
3. To the extent that any term of this License (including the Additional Use Grant A secondary benefit of defending all users' freedom is that
and the Change License) is in conflict with the Terms of this License, these improvements made in alternate versions of the program, if they
Terms shall take precedence. receive widespread use, become available for other developers to
incorporate. Many developers of free software are heartened and
encouraged by the resulting cooperation. However, in the case of
software used on network servers, this result may fail to come about.
The GNU General Public License permits making a modified version and
letting the public access it on a server without ever releasing its
source code to the public.
4. Every copy of the Licensed Work and any derivative work must include this The GNU Affero General Public License is designed specifically to
License and all other copyright, trademark, and proprietary notices included ensure that, in such cases, the modified source code becomes available
with the Licensed Work. to the community. It requires the operator of a network server to
provide the source code of the modified version running there to the
users of that server. Therefore, public use of a modified version, on
a publicly accessible server, gives the public access to the source
code of the modified version.
5. Any use of the Licensed Work that is not permitted by this License is a breach An older license, called the Affero General Public License and
of this License and may terminate your rights under this License. published by Affero, was designed to accomplish similar goals. This is
a different license, not a version of the Affero GPL, but Affero has
released a new version of the Affero GPL which permits relicensing under
this license.
6. DISCLAIMER OF WARRANTY. TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED The precise terms and conditions for copying, distribution and
WORK IS PROVIDED ON AN "AS IS" BASIS. THE LICENSOR MAKES NO REPRESENTATIONS OR modification follow.
WARRANTIES OF ANY KIND CONCERNING THE LICENSED WORK, EXPRESS OR IMPLIED, STATUTORY
OR OTHERWISE, INCLUDING, WITHOUT LIMITATION, WARRANTIES OF TITLE,
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NONINFRINGEMENT.
7. LIMITATION OF LIABILITY. TO THE EXTENT PERMITTED BY APPLICABLE LAW, IN NO EVENT TERMS AND CONDITIONS
WILL THE LICENSOR BE LIABLE TO YOU ON ANY LEGAL THEORY FOR ANY SPECIAL, INCIDENTAL,
CONSEQUENTIAL, PUNITIVE, OR EXEMPLARY DAMAGES ARISING OUT OF THIS LICENSE OR THE
USE OF THE LICENSED WORK, EVEN IF THE LICENSOR HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
----------------------------------------------------------------------------------- 0. Definitions.
Change License Text (MIT License) "This License" refers to version 3 of the GNU Affero General Public License.
Copyright (c) 2026 Syralev Georgiy (Ospab Foundation) "Copyright" also means copyright-like laws that apply to other kinds of
works, such as semiconductor masks.
Permission is hereby granted, free of charge, to any person obtaining a copy "The Program" refers to any copyrightable work licensed under this
of this software and associated documentation files (the "Software"), to deal License. Each licensee is addressed as "you". "Licensees" and
in the Software without restriction, including without limitation the rights "recipients" may be individuals or organizations.
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all To "modify" a work means to copy from or adapt all or part of the work
copies or substantial portions of the Software. in a fashion requiring copyright permission, other than the making of an
exact copy. The resulting work is called a "modified version" of the
earlier work or a work "based on" the earlier work.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR A "covered work" means either the unmodified Program or a work based
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, on the Program.
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER To "propagate" a work means to do anything with it that, without
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, permission, would make you directly or secondarily liable for
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE infringement under applicable copyright law, except executing it on a
SOFTWARE. computer or modifying a private copy. Propagation includes copying,
distribution (with or without modification), making available to the
public, and in some countries other activities as well.
To "convey" a work means any kind of propagation that enables other
parties to make or receive copies. Mere interaction with a user through
a computer network, with no transfer of a copy, is not conveying.
An interactive user interface displays "Appropriate Legal Notices"
to the extent that it includes a convenient and prominently visible
feature that (1) displays an appropriate copyright notice, and (2)
tells the user that there is no warranty for the work (except to the
extent that warranties are provided), that licensees may convey the
work under this License, and how to view a copy of this License. If
the interface presents a list of user commands or options, such as a
menu, a prominent item in the list meets this criterion.
1. Source Code.
The "source code" for a work means the preferred form of the work
for making modifications to it. "Object code" means any non-source
form of a work.
A "Standard Interface" means an interface that either is an official
standard defined by a recognized standards body, or, in the case of
interfaces specified for a particular programming language, one that
is widely used among developers working in that language.
The "System Libraries" of an executable work include anything, other
than the work as a whole, that (a) is included in the normal form of
packaging a Major Component, but which is not part of that Major
Component, and (b) serves only to enable use of the work with that
Major Component, or to implement a Standard Interface for which an
implementation is available to the public in source code form. A
"Major Component", in this context, means a major essential component
(kernel, window system, and so on) of the specific operating system
(if any) on which the executable work runs, or a compiler used to
produce the work, or an object code interpreter used to run it.
The "Corresponding Source" for a work in object code form means all
the source code needed to generate, install, and (for an executable
work) run the object code and to modify the work, including scripts to
control those activities. However, it does not include the work's
System Libraries, or general-purpose tools or generally available free
programs which are used unmodified in performing those activities but
which are not part of the work. For example, Corresponding Source
includes interface definition files associated with source files for
the work, and the source code for shared libraries and dynamically
linked subprograms that the work is specifically designed to require,
such as by intimate data communication or control flow between those
subprograms and other parts of the work.
The Corresponding Source need not include anything that users
can regenerate automatically from other parts of the Corresponding
Source.
The Corresponding Source for a work in source code form is that
same work.
2. Basic Permissions.
All rights granted under this License are granted for the term of
copyright on the Program, and are irrevocable provided the stated
conditions are met. This License explicitly affirms your unlimited
permission to run the unmodified Program. The output from running a
covered work is covered by this License only if the output, given its
content, constitutes a covered work. This License acknowledges your
rights of fair use or other equivalent, as provided by copyright law.
You may make, run and propagate covered works that you do not
convey, without conditions so long as your license otherwise remains
in force. You may convey covered works to others for the sole purpose
of having them make modifications exclusively for you, or provide you
with facilities for running those works, provided that you comply with
the terms of this License in conveying all material for which you do
not control copyright. Those thus making or running the covered works
for you must do so exclusively on your behalf, under your direction
and control, on terms that prohibit them from making any copies of
your copyrighted material outside their relationship with you.
Conveying under any other circumstances is permitted solely under
the conditions stated below. Sublicensing is not allowed; section 10
makes it unnecessary.
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
No covered work shall be deemed part of an effective technological
measure under any applicable law fulfilling obligations under article
11 of the WIPO copyright treaty adopted on 20 December 1996, or
similar laws prohibiting or restricting circumvention of such
measures.
When you convey a covered work, you waive any legal power to forbid
circumvention of technological measures to the extent such circumvention
is effected by exercising rights under this License with respect to
the covered work, and you disclaim any intention to limit operation or
modification of the work as a means of enforcing, against the work's
users, your or third parties' legal rights to forbid circumvention of
technological measures.
4. Conveying Verbatim Copies.
You may convey verbatim copies of the Program's source code as you
receive it, in any medium, provided that you conspicuously and
appropriately publish on each copy an appropriate copyright notice;
keep intact all notices stating that this License and any
non-permissive terms added in accord with section 7 apply to the code;
keep intact all notices of the absence of any warranty; and give all
recipients a copy of this License along with the Program.
You may charge any price or no price for each copy that you convey,
and you may offer support or warranty protection for a fee.
5. Conveying Modified Source Versions.
You may convey a work based on the Program, or the modifications to
produce it from the Program, in the form of source code under the
terms of section 4, provided that you also meet all of these conditions:
a) The work must carry prominent notices stating that you modified
it, and giving a relevant date.
b) The work must carry prominent notices stating that it is
released under this License and any conditions added under section
7. This requirement modifies the requirement in section 4 to
"keep intact all notices".
c) You must license the entire work, as a whole, under this
License to anyone who comes into possession of a copy. This
License will therefore apply, along with any applicable section 7
additional terms, to the whole of the work, and all its parts,
regardless of how they are packaged. This License gives no
permission to license the work in any other way, but it does not
invalidate such permission if you have separately received it.
d) If the work has interactive user interfaces, each must display
Appropriate Legal Notices; however, if the Program has interactive
interfaces that do not display Appropriate Legal Notices, your
work need not make them do so.
A compilation of a covered work with other separate and independent
works, which are not by their nature extensions of the covered work,
and which are not combined with it such as to form a larger program,
in or on a volume of a storage or distribution medium, is called an
"aggregate" if the compilation and its resulting copyright are not
used to limit the access or legal rights of the compilation's users
beyond what the individual works permit. Inclusion of a covered work
in an aggregate does not cause this License to apply to the other
parts of the aggregate.
6. Conveying Non-Source Forms.
You may convey a covered work in object code form under the terms
of sections 4 and 5, provided that you also convey the
machine-readable Corresponding Source under the terms of this License,
in one of these ways:
a) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by the
Corresponding Source fixed on a durable physical medium
customarily used for software interchange.
b) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by a
written offer, valid for at least three years and valid for as
long as you offer spare parts or customer support for that product
model, to give anyone who possesses the object code either (1) a
copy of the Corresponding Source for all the software in the
product that is covered by this License, on a durable physical
medium customarily used for software interchange, for a price no
more than your reasonable cost of physically performing this
conveying of source, or (2) access to copy the
Corresponding Source from a network server at no charge.
c) Convey individual copies of the object code with a copy of the
written offer to provide the Corresponding Source. This
alternative is allowed only occasionally and noncommercially, and
only if you received the object code with such an offer, in accord
with subsection 6b.
d) Convey the object code by offering access from a designated
place (gratis or for a charge), and offer equivalent access to the
Corresponding Source in the same way through the same place at no
further charge. You need not require recipients to copy the
Corresponding Source along with the object code. If the place to
copy the object code is a network server, the Corresponding Source
may be on a different server (operated by you or a third party)
that supports equivalent copying facilities, provided you maintain
clear directions next to the object code saying where to find the
Corresponding Source. Regardless of what server hosts the
Corresponding Source, you remain obligated to ensure that it is
available for as long as needed to satisfy these requirements.
e) Convey the object code using peer-to-peer transmission, provided
you inform other peers where the object code and Corresponding
Source of the work are being offered to the general public at no
charge under subsection 6d.
A separable portion of the object code, whose source code is excluded
from the Corresponding Source as a System Library, need not be
included in conveying the object code work.
A "User Product" is either (1) a "consumer product", which means any
tangible personal property which is normally used for personal, family,
or household purposes, or (2) anything designed or sold for incorporation
into a dwelling. In determining whether a product is a consumer product,
doubtful cases shall be resolved in favor of coverage. For a particular
product received by a particular user, "normally used" refers to a
typical or common use of that class of product, regardless of the status
of the particular user or of the way in which the particular user
actually uses, or expects or is expected to use, the product. A product
is a consumer product regardless of whether the product has substantial
commercial, industrial or non-consumer uses, unless such uses represent
the only significant mode of use of the product.
"Installation Information" for a User Product means any methods,
procedures, authorization keys, or other information required to install
and execute modified versions of a covered work in that User Product from
a modified version of its Corresponding Source. The information must
suffice to ensure that the continued functioning of the modified object
code is in no case prevented or interfered with solely because
modification has been made.
If you convey an object code work under this section in, or with, or
specifically for use in, a User Product, and the conveying occurs as
part of a transaction in which the right of possession and use of the
User Product is transferred to the recipient in perpetuity or for a
fixed term (regardless of how the transaction is characterized), the
Corresponding Source conveyed under this section must be accompanied
by the Installation Information. But this requirement does not apply
if neither you nor any third party retains the ability to install
modified object code on the User Product (for example, the work has
been installed in ROM).
The requirement to provide Installation Information does not include a
requirement to continue to provide support service, warranty, or updates
for a work that has been modified or installed by the recipient, or for
the User Product in which it has been modified or installed. Access to a
network may be denied when the modification itself materially and
adversely affects the operation of the network or violates the rules and
protocols for communication across the network.
Corresponding Source conveyed, and Installation Information provided,
in accord with this section must be in a format that is publicly
documented (and with an implementation available to the public in
source code form), and must require no special password or key for
unpacking, reading or copying.
7. Additional Terms.
"Additional permissions" are terms that supplement the terms of this
License by making exceptions from one or more of its conditions.
Additional permissions that are applicable to the entire Program shall
be treated as though they were included in this License, to the extent
that they are valid under applicable law. If additional permissions
apply only to part of the Program, that part may be used separately
under those permissions, but the entire Program remains governed by
this License without regard to the additional permissions.
When you convey a copy of a covered work, you may at your option
remove any additional permissions from that copy, or from any part of
it. (Additional permissions may be written to require their own
removal in certain cases when you modify the work.) You may place
additional permissions on material, added by you to a covered work,
for which you have or can give appropriate copyright permission.
Notwithstanding any other provision of this License, for material you
add to a covered work, you may (if authorized by the copyright holders of
that material) supplement the terms of this License with terms:
a) Disclaiming warranty or limiting liability differently from the
terms of sections 15 and 16 of this License; or
b) Requiring preservation of specified reasonable legal notices or
author attributions in that material or in the Appropriate Legal
Notices displayed by works containing it; or
c) Prohibiting misrepresentation of the origin of that material, or
requiring that modified versions of such material be marked in
reasonable ways as different from the original version; or
d) Limiting the use for publicity purposes of names of licensors or
authors of the material; or
e) Declining to grant rights under trademark law for use of some
trade names, trademarks, or service marks; or
f) Requiring indemnification of licensors and authors of that
material by anyone who conveys the material (or modified versions of
it) with contractual assumptions of liability to the recipient, for
any liability that these contractual assumptions directly impose on
those licensors and authors.
All other non-permissive additional terms are considered "further
restrictions" within the meaning of section 10. If the Program as you
received it, or any part of it, contains a notice stating that it is
governed by this License along with a term that is a further
restriction, you may remove that term. If a license document contains
a further restriction but permits relicensing or conveying under this
License, you may add to a covered work material governed by the terms
of that license document, provided that the further restriction does
not survive such relicensing or conveying.
If you add terms to a covered work in accord with this section, you
must place, in the relevant source files, a statement of the
additional terms that apply to those files, or a notice indicating
where to find the applicable terms.
Additional terms, permissive or non-permissive, may be stated in the
form of a separately written license, or stated as exceptions;
the above requirements apply either way.
8. Termination.
You may not propagate or modify a covered work except as expressly
provided under this License. Any attempt otherwise to propagate or
modify it is void, and will automatically terminate your rights under
this License (including any patent licenses granted under the third
paragraph of section 11).
However, if you cease all violation of this License, then your
license from a particular copyright holder is reinstated (a)
provisionally, unless and until the copyright holder explicitly and
finally terminates your license, and (b) permanently, if the copyright
holder fails to notify you of the violation by some reasonable means
prior to 60 days after the cessation.
Moreover, your license from a particular copyright holder is
reinstated permanently if the copyright holder notifies you of the
violation by some reasonable means, this is the first time you have
received notice of violation of this License (for any work) from that
copyright holder, and you cure the violation prior to 30 days after
your receipt of the notice.
Termination of your rights under this section does not terminate the
licenses of parties who have received copies or rights from you under
this License. If your rights have been terminated and not permanently
reinstated, you do not qualify to receive new licenses for the same
material under section 10.
9. Acceptance Not Required for Having Copies.
You are not required to accept this License in order to receive or
run a copy of the Program. Ancillary propagation of a covered work
occurring solely as a consequence of using peer-to-peer transmission
to receive a copy likewise does not require acceptance. However,
nothing other than this License grants you permission to propagate or
modify any covered work. These actions infringe copyright if you do
not accept this License. Therefore, by modifying or propagating a
covered work, you indicate your acceptance of this License to do so.
10. Automatic Licensing of Downstream Recipients.
Each time you convey a covered work, the recipient automatically
receives a license from the original licensors, to run, modify and
propagate that work, subject to this License. You are not responsible
for enforcing compliance by third parties with this License.
An "entity transaction" is a transaction transferring control of an
organization, or substantially all assets of one, or subdividing an
organization, or merging organizations. If propagation of a covered
work results from an entity transaction, each party to that
transaction who receives a copy of the work also receives whatever
licenses to the work the party's predecessor in interest had or could
give under the previous paragraph, plus a right to possession of the
Corresponding Source of the work from the predecessor in interest, if
the predecessor has it or can get it with reasonable efforts.
You may not impose any further restrictions on the exercise of the
rights granted or affirmed under this License. For example, you may
not impose a license fee, royalty, or other charge for exercise of
rights granted under this License, and you may not initiate litigation
(including a cross-claim or counterclaim in a lawsuit) alleging that
any patent claim is infringed by making, using, selling, offering for
sale, or importing the Program or any portion of it.
11. Patents.
A "contributor" is a copyright holder who authorizes use under this
License of the Program or a work on which the Program is based. The
work thus licensed is called the contributor's "contributor version".
A contributor's "essential patent claims" are all patent claims
owned or controlled by the contributor, whether already acquired or
hereafter acquired, that would be infringed by some manner, permitted
by this License, of making, using, or selling its contributor version,
but do not include claims that would be infringed only as a
consequence of further modification of the contributor version. For
purposes of this definition, "control" includes the right to grant
patent sublicenses in a manner consistent with the requirements of
this License.
Each contributor grants you a non-exclusive, worldwide, royalty-free
patent license under the contributor's essential patent claims, to
make, use, sell, offer for sale, import and otherwise run, modify and
propagate the contents of its contributor version.
In the following three paragraphs, a "patent license" is any express
agreement or commitment, however denominated, not to enforce a patent
(such as an express permission to practice a patent or covenant not to
sue for patent infringement). To "grant" such a patent license to a
party means to make such an agreement or commitment not to enforce a
patent against the party.
If you convey a covered work, knowingly relying on a patent license,
and the Corresponding Source of the work is not available for anyone
to copy, free of charge and under the terms of this License, through a
publicly available network server or other readily accessible means,
then you must either (1) cause the Corresponding Source to be so
available, or (2) arrange to deprive yourself of the benefit of the
patent license for this particular work, or (3) arrange, in a manner
consistent with the requirements of this License, to extend the patent
license to downstream recipients. "Knowingly relying" means you have
actual knowledge that, but for the patent license, your conveying the
covered work in a country, or your recipient's use of the covered work
in a country, would infringe one or more identifiable patents in that
country that you have reason to believe are valid.
If, pursuant to or in connection with a single transaction or
arrangement, you convey, or propagate by procuring conveyance of, a
covered work, and grant a patent license to some of the parties
receiving the covered work authorizing them to use, propagate, modify
or convey a specific copy of the covered work, then the patent license
you grant is automatically extended to all recipients of the covered
work and works based on it.
A patent license is "discriminatory" if it does not include within
the scope of its coverage, prohibits the exercise of, or is
conditioned on the non-exercise of one or more of the rights that are
specifically granted under this License. You may not convey a covered
work if you are a party to an arrangement with a third party that is
in the business of distributing software, under which you make payment
to the third party based on the extent of your activity of conveying
the work, and under which the third party grants, to any of the
parties who would receive the covered work from you, a discriminatory
patent license (a) in connection with copies of the covered work
conveyed by you (or copies made from those copies), or (b) primarily
for and in connection with specific products or compilations that
contain the covered work, unless you entered into that arrangement,
or that patent license was granted, prior to 28 March 2007.
Nothing in this License shall be construed as excluding or limiting
any implied license or other defenses to infringement that may
otherwise be available to you under applicable patent law.
12. No Surrender of Others' Freedom.
If conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot convey a
covered work so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you may
not convey it at all. For example, if you agree to terms that obligate you
to collect a royalty for further conveying from those to whom you convey
the Program, the only way you could satisfy both those terms and this
License would be to refrain entirely from conveying the Program.
13. Remote Network Interaction; Use with the GNU General Public License.
Notwithstanding any other provision of this License, if you modify the
Program, your modified version must prominently offer all users
interacting with it remotely through a computer network (if your version
supports such interaction) an opportunity to receive the Corresponding
Source of your version by providing access to the Corresponding Source
from a network server at no charge, through some standard or customary
means of facilitating copying of software. This Corresponding Source
shall include the Corresponding Source for any work covered by version 3
of the GNU General Public License that is incorporated pursuant to the
following paragraph.
Notwithstanding any other provision of this License, you have
permission to link or combine any covered work with a work licensed
under version 3 of the GNU General Public License into a single
combined work, and to convey the resulting work. The terms of this
License will continue to apply to the part which is the covered work,
but the work with which it is combined will remain governed by version
3 of the GNU General Public License.
14. Revised Versions of this License.
The Free Software Foundation may publish revised and/or new versions of
the GNU Affero General Public License from time to time. Such new versions
will be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the
Program specifies that a certain numbered version of the GNU Affero General
Public License "or any later version" applies to it, you have the
option of following the terms and conditions either of that numbered
version or of any later version published by the Free Software
Foundation. If the Program does not specify a version number of the
GNU Affero General Public License, you may choose any version ever published
by the Free Software Foundation.
If the Program specifies that a proxy can decide which future
versions of the GNU Affero General Public License can be used, that proxy's
public statement of acceptance of a version permanently authorizes you
to choose that version for the Program.
Later license versions may give you additional or different
permissions. However, no additional obligations are imposed on any
author or copyright holder as a result of your choosing to follow a
later version.
15. Disclaimer of Warranty.
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. Limitation of Liability.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.
17. Interpretation of Sections 15 and 16.
If the disclaimer of warranty and limitation of liability provided
above cannot be given local legal effect according to their terms,
reviewing courts shall apply local law that most closely approximates
an absolute waiver of all civil liability in connection with the
Program, unless a warranty or assumption of liability accompanies a
copy of the Program in return for a fee.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
state the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
Also add information on how to contact you by electronic and paper mail.
If your software can interact with users remotely through a computer
network, you should also make sure that it provides a way for users to
get its source. For example, if your program is a web application, its
interface could display a "Source" link that leads users to an archive
of the code. There are many ways you could offer source, and different
solutions will be better for different programs; see section 13 for the
specific requirements.
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU AGPL, see
<https://www.gnu.org/licenses/>.

330
README.md
View File

@ -1,129 +1,259 @@
# OSTP (Ospab Stealth Transport Protocol) # OSTP - Ospab Stealth Transport Protocol
OSTP is a high-throughput, robust, and multiplexed transport protocol engineered for secure, distributed industrial telemetry replication and real-time metric synchronization over unreliable, lossy networks. By implementing granular keystream scrambling and adaptive block framing, OSTP ensures absolute structural integrity and uniform entropy across all transmitted grid data, eliminating distinct traffic signatures and protecting assets against unauthorized analysis. [Русский язык](README.ru.md) · [Wiki](https://github.com/ospab/ostp/wiki) · [Contributing](CONTRIBUTING.md) · [Releases](https://github.com/ospab/ostp/releases)
![GitHub Release](https://img.shields.io/github/v/release/ospab/ostp?style=for-the-badge&color=blue)
![License: AGPL v3](https://img.shields.io/badge/License-AGPL%20v3-blue.svg?style=for-the-badge)
![Platform: Windows | Linux | macOS | Android](https://img.shields.io/badge/Platform-Windows%20%7C%20Linux%20%7C%20macOS%20%7C%20Android-green.svg?style=for-the-badge)
![Crypto](https://img.shields.io/badge/Crypto-Noise__NNpsk0-blueviolet?style=for-the-badge)
![Transport](https://img.shields.io/badge/Transport-UDP%20ARQ-informational?style=for-the-badge)
> A fast, custom encrypted transport protocol written in Rust.
**OSTP** (Ospab Stealth Transport Protocol) is a high-performance transport protocol. It implements a custom ARQ transport over UDP, as well as a UoT (UDP-over-TCP) mode. Every byte on the wire - including packet headers - is cryptographically indistinguishable from random noise, making it highly resistant to Deep Packet Inspection (DPI).
--- ---
## Industrial Architecture ## Quick Install
The pipeline utilizes a highly optimized modular framework:
- **ostp-core**: The foundational grid synchronization library hosting core transport primitives, keystream scrambling pipelines, Noise Protocol Framework cryptography, and zero-copy framed processing.
- **ostp**: The consolidated cross-platform node daemon configured either as a telemetry collector (`server`) or relay bridge (`client`).
- **ostp-jni**: Consolidated bindings allowing secure deployment of telemetry nodes across Android-embedded field equipment.
---
## Feature Specification
- **Keystream Scrambling (Entropy Masking)**: Internal packet fields are processed via high-entropy masking derived dynamically per session, ensuring absolute payload uniformity. This makes active traffic fully transparent to statistical network analyzers.
- **Persistent Connection Multiplexing**: Enables high-fidelity continuous data channels, supporting parallel session structures and maintaining state persistence across volatile network interface rotations.
- **Resilient Network Handoff**: Automatically detects and preserves active TCP pipelines when node endpoints experience topological shifts (e.g., cellular to fiber gateways) without interrupting upper-tier protocols.
- **Pre-Shared Cryptographic Handshake**: Employs `Noise_NNpsk0_25519_ChaChaPoly_BLAKE2s` to validate remote nodes, establishing authentic channels instantly with post-quantum grade forward secrecy.
- **Gateway Routing Protocol Support**: Standard dual-mode interfaces for legacy application routing via industrial SOCKS5/HTTP-CONNECT translation models.
- **Static/Adaptive Block Shaping**: Eliminates behavioral data leaks through cryptographically randomized block-alignment schemes to maintain constant channel densities.
---
## Provisioning and Configuration
### Automated Linux Server Deployment (Recommended)
For rapid, interactive provisioning on standard Linux host environments, execute the unified installer via a single terminal command:
### Linux
```bash ```bash
bash <(curl -Ls https://raw.githubusercontent.com/ospab/ostp/master/scripts/install.sh) bash <(curl -Ls https://raw.githubusercontent.com/ospab/ostp/master/scripts/install.sh)
``` ```
*This routine autonomously fetches correct binary releases, registers a resilient system daemon, and interactively initializes configuration templates utilizing the binary's native compiler.* ### Windows (PowerShell, run as Administrator)
```powershell
### Manual Node Initialization irm https://raw.githubusercontent.com/ospab/ostp/master/scripts/install.ps1 | iex
The consolidated `ostp` daemon automates node certificate generation and base configuration templating.
**Provision Collector Node (Server):**
```bash
./ostp --init server
```
*This provisions `config.json` bound to an automated listening grid port with randomized secure node validation keys.*
**Provision Relay Node (Client):**
```bash
./ostp --init client
``` ```
### Node Integration Config ### Manual Download
Download pre-built binaries for your platform from [GitHub Releases](https://github.com/ospab/ostp/releases).
Configuration parameters are defined within `config.json` aligned adjacent to the service binary. ---
#### Telemetry Collector Configuration (`config.json`) ## Key Features
```json
{
"mode": "server",
"listen": "0.0.0.0:50000",
"access_keys": [
"secure_node_registration_key_here"
],
"debug": false
}
```
#### Relay Bridge Configuration (`config.json`) | Feature | Description |
```json |---------|-------------|
{ | **Full Traffic Obfuscation** | Every packet - including headers - is indistinguishable from random noise. Session IDs and nonces are masked with per-packet HMAC-derived keys. |
"mode": "client", | **Noise Protocol Handshake** | `Noise_NNpsk0_25519_ChaChaPoly_BLAKE2s` - PSK-authenticated, forward-secret key exchange with no static identity exposure. |
"server": "COLLECTOR_ENDPOINT_IP:50000", | **Reliable UDP (ARQ)** | Selective ACK/NACK with rate-limited retransmission, configurable reorder buffer, and exponential backoff. |
"access_key": "secure_node_registration_key_here", | **Multiplexed Streams** | Multiple logical TCP streams over a single encrypted UDP session with per-stream flow control. |
"socks5_bind": "127.0.0.1:1088", | **Seamless Roaming** | Clients can switch networks (WiFi ↔ LTE) without session interruption - tracked by session-ID, not IP. |
"tun": { | **Management API** | Built-in REST API for third-party panels (3x-ui, custom dashboards). Per-user stats, traffic limits, key CRUD. |
"enable": false, | **Fallback Server** | TCP fallback proxy to a web server - makes OSTP indistinguishable from nginx during active probing. |
"wintun_path": "./wintun.dll", | **Multi-Listener** | Bind to multiple addresses simultaneously (dual-stack IPv4/IPv6, multi-port). |
"ipv4_address": "10.1.0.2/24" | **TUN Mode** | Full-system VPN via native `smoltcp` network stack without external dependencies. All traffic transparently routed through the tunnel. |
}, | **UoT (UDP-over-TCP)** | Bare UDP-over-TCP tunnel, no protocol mimicry. Since all data is fully encrypted and length-prefixed, it bypasses DPI filters that block unknown UDP traffic by riding over a plain TCP connection. |
"exclude": { | **Mobile & Web Apps** | Beautiful cross-platform mobile client (Flutter) and a modern Web Control Panel (React/Vite) for effortless server and client management. |
"domains": [ | **TURN Relay** | RFC 5766 TURN support for environments where direct UDP is blocked. |
"internal-system.lan", | **Hot-Reload** | Runtime config reload without restart (access keys, exclusions, mux settings). |
"local.lan" | **Structured Logging** | `tracing`-based logging with `RUST_LOG` filtering. JSON/file/syslog output support. |
], | **Cross-Platform** | Windows, Linux, macOS, Android, FreeBSD, MIPS, RISC-V. Single binary, no runtime dependencies. |
"ips": [
"192.168.1.0/24",
"10.0.0.0/8"
],
"processes": [
"local_monitoring.exe"
]
},
"mux": {
"enabled": true,
"sessions": 2
}
}
```
### Execution Parameters ---
Initiate telemetry processing by assigning the active configuration target: ## Architecture
```bash ```mermaid
./ostp --config config.json flowchart LR
%% Styles
classDef userApp fill:#e1f5fe,stroke:#01579b,stroke-width:2px,color:#01579b
classDef ostpCore fill:#e8f5e9,stroke:#2e7d32,stroke-width:2px,color:#2e7d32
classDef network fill:#fff3e0,stroke:#e65100,stroke-width:2px,color:#e65100,stroke-dasharray: 5 5
classDef external fill:#f3e5f5,stroke:#4a148c,stroke-width:2px,color:#4a148c
classDef fallback fill:#ffebee,stroke:#c62828,stroke-width:2px,color:#c62828
subgraph Local["💻 Client Device"]
Apps["Web Browser / Apps"]:::userApp
Socks["SOCKS5 / HTTP Proxy"]:::ostpCore
Tun["Global TUN (VPN)"]:::ostpCore
Client["OSTP Client Protocol Engine\n(Noise + ChaCha20 + ARQ)"]:::ostpCore
Apps -->|TCP/UDP| Socks
Apps -->|IP Packets| Tun
Socks --> Client
Tun --> Client
end
subgraph Internet["🌐 Hostile Network (DPI/Firewall)"]
Tunnel{"Fully Obfuscated\nEncrypted UDP\n(Looks like noise)"}:::network
end
subgraph Remote["🖥️ Remote VPS (Server)"]
Server["OSTP Server Protocol Engine\n(Authentication & Decryption)"]:::ostpCore
Relay["Connection Multiplexer"]:::ostpCore
Fallback["Fake Website\n(Nginx/Caddy)"]:::fallback
Target["Open Internet\n(YouTube, Google, etc)"]:::external
Server -->|Decrypted Traffic| Relay
Server -->|Active Probe / Scanner| Fallback
Relay -->|Clear Traffic| Target
end
Client <==> Tunnel <==> Server
``` ```
--- ---
## Operation & Reliability Metrics ## Quick Start
### Stream Multiplexing (Mux) ### 1. Generate config
> [!IMPORTANT]
> **Parallel multiplexing is fully supported.**
> The pipeline executes parallel handshake processes seamlessly, routing independent stream structures via separate cryptographic tunnels to maximize throughput.
### Exclusion Engines (Bypass Modules) ```bash
> [!NOTE] # On your VPS (server):
> Real-time exclusion engines are fully operational. Configured IP subnets, local domains, and internal processes correctly route traffic natively to prevent local loop latencies. ./ostp init server
# On your machine (client):
./ostp init client
```
### 2. Edit config
**Server** - set your access keys:
```jsonc
{
"mode": "server",
"listen": "0.0.0.0:50000",
"access_keys": ["YOUR_SECRET_KEY"],
"api": { "enabled": true, "bind": "127.0.0.1:9090", "token": "admin-token" },
"fallback": { "enabled": false, "listen": "0.0.0.0:443", "target": "127.0.0.1:8080" }
}
```
**Client** - point to your server:
```jsonc
{
"mode": "client",
"server": "YOUR_SERVER_IP:50000",
"access_key": "YOUR_SECRET_KEY",
"socks5_bind": "127.0.0.1:1088",
"transport": { "mode": "udp" },
"tun": { "enable": false, "dns": "1.1.1.1" }
}
```
### 3. Run
```bash
./ostp # Uses config.json in current directory
./ostp --config /path/to.json # Custom config path
./ostp check # Validate config without running
./ostp gk # Generate a new access key
./ostp links # Print client share links
```
### 4. Connect via share link (one-liner)
```bash
./ostp connect "ostp://ACCESS_KEY@server.com:50000?..."
```
> [!WARNING]
> Always wrap the `ostp://...` link in quotes (`"`) so your terminal doesn't misinterpret special characters like `&` or `?`.
---
## Management API
Built-in REST API for building panels and dashboards.
```bash
# Server status
curl -H "Authorization: Bearer mytoken" http://127.0.0.1:9090/api/server/status
# List all users with traffic stats
curl -H "Authorization: Bearer mytoken" http://127.0.0.1:9090/api/users
# Create a user with 10GB traffic limit
curl -X POST -H "Authorization: Bearer mytoken" \
-H "Content-Type: application/json" \
-d '{"limit_bytes": 10737418240}' \
http://127.0.0.1:9090/api/users
```
Full API reference: [Management API](https://github.com/ospab/ostp/wiki/Management-API)
---
## CLI Reference
```
ostp [--config <PATH>] [COMMAND]
Commands:
run Run the daemon using the config file (default when no command is given)
connect <URL> Connect once using a share link: ostp://KEY@HOST:PORT
setup Interactive setup wizard
init <MODE> Generate a template config (server/client/relay)
check Validate the configuration file and exit
gk Generate a secure access key (alias: generate-key)
--format <FMT> Key format: hex, base64 (default: hex)
-n, --count <N> Number of keys to generate (default: 1)
links Print client share links from the server config
import <URL> Import a share link into the config file
update Update OSTP to the latest release
-b, --branch <NAME> Release channel: stable, beta, alpha (default: stable)
-v, --version <VER> Update to an exact version instead of the channel's latest
migrate Force-migrate the configuration file to the current format
proxy-env Print shell export commands for the local SOCKS proxy
proxy-env-clear Print shell export commands to unset it
uninstall Stop the service and remove the binary and config
Global options:
--config <PATH> Config file path (default: config.json)
```
Every subcommand also accepts `-h`/`--help` for its own option list.
---
## Protocol Summary
| Layer | Mechanism |
|-------|-----------|
| Key Exchange | Noise NNpsk0 (X25519 + ChaChaPoly + BLAKE2s) zero-RTT |
| Encryption | ChaCha20-Poly1305 AEAD per-packet |
| Header Obfuscation | HMAC-SHA256 derived per-packet mask |
| Reliability | Selective ACK with cumulative + SACK ranges |
| Retransmission | Rate-limited NACK + exponential backoff RTO |
| Keepalive | Ping/Pong with RTT measurement every 5s |
---
## Building from Source
```bash
# Prerequisites: Rust 1.75+
cargo build --release
# Cross-compile for Linux
cross build --release --target x86_64-unknown-linux-gnu
# Run tests
cargo test -p ostp-core -p ostp-server
```
---
## Documentation
- **[Wiki](https://github.com/ospab/ostp/wiki)** - Full documentation
- [Installation](https://github.com/ospab/ostp/wiki/Installation)
- [Configuration Reference](https://github.com/ospab/ostp/wiki/Configuration)
- [Management API](https://github.com/ospab/ostp/wiki/Management-API)
- [Protocol Design](https://github.com/ospab/ostp/wiki/Protocol-Design)
- [Building from Source](https://github.com/ospab/ostp/wiki/Building-from-Source)
- [FAQ](https://github.com/ospab/ostp/wiki/FAQ)
--- ---
## License ## License
OSTP is published under the Business Source License 1.1 (BSL), permitting unrestricted personal, non-commercial, and private utility deployments. This license automatically transitions to the permissive MIT License on May 14, 2030. GNU Affero General Public License v3.0 (AGPL-3.0). See [LICENSE](LICENSE) for the full text.
For full licensing terms, refer to the accompanying [LICENSE](LICENSE) file or the official repository at [https://github.com/ospab/ostp](https://github.com/ospab/ostp). ---
## Contact
- **Telegram**: [@ospab0](https://t.me/ospab0)
- **Email**: gvoprgrg@gmail.com

View File

@ -1,129 +1,245 @@
# OSTP (Ospab Stealth Transport Protocol) # OSTP - Ospab Stealth Transport Protocol
OSTP — это высокопроизводительный, надежный мультиплексируемый транспортный протокол, спроектированный для безопасной распределенной репликации промышленной телеметрии и синхронизации системных метрик реального времени в условиях нестабильных и зашумленных сетей передачи данных. За счет применения матричного маскирования сигнатурных потоков и адаптивного выравнивания границ блоков, OSTP гарантирует абсолютную структурную однородность и равномерную энтропию передаваемых данных, исключая появление статистических отпечатков трафика и защищая инфраструктуру от несанкционированного анализа. [English](README.md) · [Contributing](CONTRIBUTING.ru.md)
![GitHub Release](https://img.shields.io/github/v/release/ospab/ostp?style=for-the-badge&color=blue)
![License: AGPL v3](https://img.shields.io/badge/License-AGPL%20v3-blue.svg?style=for-the-badge)
![Platform: Windows | Linux | macOS | Android](https://img.shields.io/badge/Platform-Windows%20%7C%20Linux%20%7C%20macOS%20%7C%20Android-green.svg?style=for-the-badge)
![Crypto](https://img.shields.io/badge/Crypto-Noise__NNpsk0-blueviolet?style=for-the-badge)
![Transport](https://img.shields.io/badge/Transport-UDP%20ARQ-informational?style=for-the-badge)
> Быстрый кастомный зашифрованный транспортный протокол на Rust.
**OSTP** (Ospab Stealth Transport Protocol) - кастомный транспортный протокол. Реализует собственный ARQ-транспорт поверх UDP, а также режим UoT (UDP-over-TCP). Каждый байт, включая заголовки пакетов, криптографически неотличим от случайного шума, что делает его устойчивым к системам глубокого анализа трафика (DPI).
--- ---
## Архитектура системы ## Возможности
Платформа построена на базе высокооптимизированного модульного каркаса: | Возможность | Описание |
- **ostp-core**: Базовая библиотека синхронизации, обеспечивающая логику транспорта, алгоритмы маскирования энтропии, криптографическую обвязку на базе Noise Protocol Framework и потоковую обработку без копирования данных. |-------------|----------|
- **ostp**: Унифицированный кроссплатформенный демон сетевого узла, конфигурируемый либо в режиме сборщика телеметрии (`server`), либо в режиме моста ретрансляции (`client`). | **Обфускация трафика** | Каждый пакет, включая заголовки, неотличим от случайного шума. Session ID и nonce маскируются HMAC-ключами, уникальными для каждого пакета. |
- **ostp-jni**: Готовые связки для встраивания и развертывания сетевых узлов на базе оборудования под управлением ОС Android. | **Noise Protocol** | `Noise_NNpsk0_25519_ChaChaPoly_BLAKE2s` - аутентификация через PSK, forward secrecy, без раскрытия идентичности. |
| **Reliable UDP (ARQ)** | Selective ACK/NACK с rate-limited ретрансмиссией, настраиваемым reorder-буфером и exponential backoff. Разработан для 10 Гбит/с. |
| **Мультиплексирование** | Несколько логических TCP-потоков поверх одной зашифрованной UDP-сессии с per-stream flow control. |
| **Бесшовный роуминг** | Клиент может менять сети (WiFi ↔ 4G) без разрыва сессии - сервер отслеживает session-ID, а не IP-адрес. |
| **TUN-режим** | Полносистемный VPN без внешних зависимостей (встроенный network stack на базе `smoltcp`). |
| **UoT (UDP-over-TCP)** | Голый туннель UDP-over-TCP, без имитации протоколов. Поскольку все данные полностью зашифрованы и имеют префикс длины, он обходит DPI фильтры, блокирующие неизвестный UDP трафик, передавая всё по обычному TCP соединению. |
| **Мобильные и Web приложения** | Красивый кроссплатформенный мобильный клиент (Flutter) и современная Web панель управления (React/Vite) для удобного администрирования. |
| **TURN Relay** | RFC 5766 TURN для окружений, где прямой UDP заблокирован. |
| **Hot-Reload** | Перезагрузка конфига в рантайме без перезапуска (ключи, исключения, mux, TURN). |
| **Кросс-платформа** | Windows, Linux, macOS, Android. Один бинарник, без зависимостей. |
--- ---
## Технические спецификации ## Архитектура
- **Маскирование энтропии (Скрытие сигнатур)**: Внутренние поля пакетов проходят динамическую высокоэнтропийную потоковую обработку на каждом сеансе связи, обеспечивая предельную однородность трафика. Это делает сетевые потоки невидимыми для автоматических анализаторов топологии. ```mermaid
- **Стойкое мультиплексирование соединений**: Организует параллельные логические каналы передачи данных, поддерживая одновременную активность нескольких сессий и сохраняя стабильность связи при смене сетевых интерфейсов. flowchart LR
- **Отказоустойчивый сетевой переход (IP-роуминг)**: Автоматически обнаруживает и сохраняет активные транспортные конвейеры при изменении физических шлюзов конечного узла (например, переключение с сотовой сети на оптические линии) без разрыва вышестоящих соединений. %% Styles
- **Безопасное рукопожатие (PSK Handshake)**: Использует схему `Noise_NNpsk0_25519_ChaChaPoly_BLAKE2s` для аутентификации удаленных узлов, обеспечивая мгновенный запуск защищенного канала с гарантиями совершенной прямой секретности (Forward Secrecy). classDef userApp fill:#e1f5fe,stroke:#01579b,stroke-width:2px,color:#01579b
- **Поддержка шлюзовых интерфейсов**: Наличие стандартных шлюзов трансляции трафика через модели SOCKS5/HTTP-CONNECT для совместимости с унаследованными компонентами АСУ ТП. classDef ostpCore fill:#e8f5e9,stroke:#2e7d32,stroke-width:2px,color:#2e7d32
- **Адаптивное выравнивание блоков**: Защищает систему от анализа поведения сети по длинам датаграмм благодаря алгоритму случайного побитового масштабирования пакетов до границ регистров. classDef network fill:#fff3e0,stroke:#e65100,stroke-width:2px,color:#e65100,stroke-dasharray: 5 5
classDef external fill:#f3e5f5,stroke:#4a148c,stroke-width:2px,color:#4a148c
classDef fallback fill:#ffebee,stroke:#c62828,stroke-width:2px,color:#c62828
subgraph Local["💻 Устройство клиента"]
Apps["Браузер / Приложения"]:::userApp
Socks["SOCKS5 / HTTP Прокси"]:::ostpCore
Tun["Global TUN (VPN)"]:::ostpCore
Client["OSTP Клиент\n(Noise + ChaCha20 + ARQ)"]:::ostpCore
Apps -->|TCP/UDP| Socks
Apps -->|IP Пакеты| Tun
Socks --> Client
Tun --> Client
end
subgraph Internet["🌐 Сеть с цензурой (DPI)"]
Tunnel{"Зашифрованный UDP\n(Выглядит как белый шум)"}:::network
end
subgraph Remote["🖥️ Удаленный сервер (VPS)"]
Server["OSTP Сервер\n(Аутентификация)"]:::ostpCore
Relay["Мультиплексор соединений"]:::ostpCore
Fallback["Фейковый сайт\n(Nginx/Caddy)"]:::fallback
Target["Свободный интернет\n(YouTube, Google и т.д.)"]:::external
Server -->|Расшифрованный трафик| Relay
Server -->|Сканеры цензоров| Fallback
Relay -->|Чистый трафик| Target
end
Client <==> Tunnel <==> Server
```
--- ---
## Развертывание и настройка ## Установка
### Автоматическая установка на Linux (Рекомендуется)
Для быстрого интерактивного развертывания узла в стандартных серверных средах Linux выполните команду установки непосредственно в консоли терминала:
### Linux
```bash ```bash
bash <(curl -Ls https://raw.githubusercontent.com/ospab/ostp/master/scripts/install.sh) bash <(curl -Ls https://raw.githubusercontent.com/ospab/ostp/master/scripts/install.sh)
``` ```
*Данный сценарий автономно загружает подходящий бинарный релиз, регистрирует системную службу демона в операционной системе и интерактивно настраивает конфигурационные шаблоны с помощью встроенных инструментов компиляции.* ### Windows (PowerShell от Администратора)
```powershell
### Ручная инициализация узла irm https://raw.githubusercontent.com/ospab/ostp/master/scripts/install.ps1 | iex
Унифицированное приложение `ostp` способно самостоятельно генерировать шаблоны настроек и идентификационные ключи безопасности.
**Инициализация узла сборщика (Сервер):**
```bash
./ostp --init server
```
*Эта команда создает файл `config.json`, привязанный к автоматическому порту прослушивания, и записывает туда сгенерированные случайные ключи авторизации.*
**Инициализация узла моста (Клиент):**
```bash
./ostp --init client
```
### Конфигурация интеграции
Рабочие параметры узла задаются в файле `config.json`, расположенном рядом с исполняемым файлом демона.
#### Конфигурация сборщика телеметрии (`config.json`)
```json
{
"mode": "server",
"listen": "0.0.0.0:50000",
"access_keys": [
"secure_node_registration_key_here"
],
"debug": false
}
```
#### Конфигурация моста ретрансляции (`config.json`)
```json
{
"mode": "client",
"server": "COLLECTOR_ENDPOINT_IP:50000",
"access_key": "secure_node_registration_key_here",
"socks5_bind": "127.0.0.1:1088",
"tun": {
"enable": false,
"wintun_path": "./wintun.dll",
"ipv4_address": "10.1.0.2/24"
},
"exclude": {
"domains": [
"internal-system.lan",
"local.lan"
],
"ips": [
"192.168.1.0/24",
"10.0.0.0/8"
],
"processes": [
"local_monitoring.exe"
]
},
"mux": {
"enabled": true,
"sessions": 2
}
}
```
### Запуск узла
Для активации процессов обмена телеметрией запустите приложение с указанием пути к активному файлу параметров:
```bash
./ostp --config config.json
``` ```
--- ---
## Метрики стабильности и производительности ## Конфигурация
### Мультиплексирование потоков (Mux) Создать конфиг по умолчанию:
> [!IMPORTANT] ```bash
> **Параллельное мультиплексирование полностью поддерживается.** ./ostp init server # VPS
> Система бесшовно обрабатывает конкурентные циклы согласования параметров среды, распределяя независимые структуры данных по раздельным криптографическим туннелям для максимизации пропускной способности. ./ostp init client # Локальная машина
```
### Модули исключений (Bypass Engines) ### Сервер (`config.json`)
> [!NOTE] ```jsonc
> Механизмы маршрутизации в обход шины передачи полностью готовы к эксплуатации. Указанные в конфигурации IP-подсети, локальные доменные зоны и процессы корректно направляются напрямую через штатный сетевой стек ОС, исключая дополнительные задержки маршрутов. {
"mode": "server",
"listen": "0.0.0.0:50000",
"access_keys": ["ВАШ_КЛЮЧ"],
"debug": false,
// Опционально: проксировать трафик через upstream
"outbound": {
"enabled": false,
"protocol": "socks5",
"address": "127.0.0.1",
"port": 9050,
"default_action": "proxy"
}
}
```
### Клиент (`config.json`)
```jsonc
{
"mode": "client",
"server": "IP_СЕРВЕРА:50000",
"access_key": "ВАШ_КЛЮЧ",
"socks5_bind": "127.0.0.1:1088",
"debug": false,
// Настройки транспорта (udp или uot)
"transport": {
"mode": "udp"
},
// TUN-режим (полносистемный VPN)
"tun": {
"enable": false,
"dns": "1.1.1.1"
},
// Мультиплексирование: несколько UDP-сессий
"mux": {
"enabled": false,
"sessions": 2
},
// TURN-реле для заблокированных сетей
"turn": {
"enabled": false,
"server_addr": "turn.example.com:3478",
"username": "user",
"access_key": "pass"
},
// Исключения (идут напрямую, минуя туннель)
"exclude": {
"domains": ["example.local"],
"ips": ["192.168.0.0/16"]
}
}
```
---
## Использование
```bash
# Запуск с конфигом
./ostp --config config.json
# Или просто (ищет config.json рядом с бинарником)
./ostp
```
### Справка по командам
```
ostp [--config <PATH>] [КОМАНДА]
Команды:
run Запустить демон по конфигу (по умолчанию, если команда не указана)
connect <URL> Подключиться по share-ссылке: ostp://KEY@HOST:PORT
setup Интерактивный мастер настройки
init <MODE> Сгенерировать шаблон конфига (server/client/relay)
check Проверить конфиг и выйти
gk Сгенерировать access-key (алиас: generate-key)
--format <FMT> Формат ключа: hex, base64 (по умолчанию hex)
-n, --count <N> Количество ключей (по умолчанию 1)
links Вывести client-share-ссылки из серверного конфига
import <URL> Импортировать share-ссылку в конфиг
update Обновить OSTP до актуального релиза
-b, --branch <NAME> Канал релиза: stable, beta, alpha (по умолчанию stable)
-v, --version <VER> Обновиться на точную версию вместо последней в канале
migrate Принудительно мигрировать конфиг к текущему формату
proxy-env Вывести shell-команды для локального SOCKS-прокси
proxy-env-clear Вывести shell-команды для их отмены
uninstall Остановить сервис и удалить бинарник с конфигом
Глобальные опции:
--config <PATH> Путь к конфигу (по умолчанию config.json)
```
У каждой подкоманды есть своя справка через `-h`/`--help`.
### TUN-режим (Windows)
Использует встроенный сетевой стек `smoltcp` и виртуальный адаптер `wintun` (необходима `wintun.dll`). Требует запуска с правами Администратора.
### TUN-режим (Linux)
Использует встроенный сетевой стек `smoltcp` и `/dev/net/tun`. Требует запуска от имени `root` (или наличия `CAP_NET_ADMIN`).
---
## Спецификация протокола
| Уровень | Механизм |
|---------|----------|
| Обмен ключами | Noise NNpsk0 (X25519 + ChaChaPoly + BLAKE2s) zero-RTT |
| Шифрование | ChaCha20-Poly1305 AEAD на каждый пакет |
| Обфускация заголовков | HMAC-SHA256 маска session_id + nonce, уникальная для каждого пакета |
| Надёжность | Selective ACK с cumulative + SACK диапазонами |
| Ретрансмиссия | Rate-limited NACK (30мс cooldown) + exponential backoff RTO |
| Flow Control | Окно in-flight (только retransmittable фреймы) |
| Keepalive | Ping/Pong с измерением RTT каждые 5с |
| Таймаут сессии | 60с на клиенте, 300с на сервере |
---
## Сборка из исходников
```bash
# Требования: Rust toolchain (1.75+)
cargo build --release
# Кросс-компиляция для Linux
cross build --release --target x86_64-unknown-linux-gnu
```
---
## Документация
- [Архитектура](docs/ru/architecture.md)
- [Спецификация протокола](docs/ru/specification.md)
- [Дизайн обфускации](docs/ru/obfuscation.md)
- [Администрирование сервера](docs/ru/server.md)
- [Настройка клиента](docs/ru/client.md)
- [Интеграции](docs/ru/integrations.md)
--- ---
## Лицензия ## Лицензия
OSTP публикуется на условиях лицензии Business Source License 1.1 (BSL), которая разрешает неограниченное личное, некоммерческое и частное использование протокола. С 14 мая 2030 года лицензия автоматически переходит в категорию открытого ПО с разрешительной лицензией MIT. GNU Affero General Public License v3.0 (AGPL-3.0). Полный текст - в файле [LICENSE](LICENSE).
С полным текстом лицензионного соглашения можно ознакомиться в приложенном файле [LICENSE](LICENSE) или в официальном репозитории проекта по адресу [https://github.com/ospab/ostp](https://github.com/ospab/ostp).

185
REBUILD_PLAN.md Normal file
View File

@ -0,0 +1,185 @@
# Чистая переборка на базе v0.2.98
База: `v0.2.98` (commit `31d0020`) — последняя версия, которая **стабильно работает**.
Ветка: `clean-rebuild`. Всё, что появилось после (0.3.1 … 0.3.21), переносим
**выборочно и с чистой головой**, а не копируем рефактор целиком.
Принцип: 0.3.1 принёс «модульный multi-server рефактор» + лавину фич — и вместе с
ними нестабильность. Берём только проверенное и нужное.
---
## Решения (зафиксировано пользователем)
- **Junk-пакеты + TCP-фрагментация — ОСТАВЛЯЕМ** (нравятся). НО починить вредную
часть: junk по UDP не должен выглядеть для сервера как `Unauthorized probe`
(rate-limit/гейт на сервере), иначе флуд лога и риск самобана клиента. Фича
остаётся — чиним поведение, а не выпиливаем. Тонкая настройка — §E.
- **Версия переборки — 0.4.0** (решено; 0.3.x сожжены в pre-release).
- **WSS и Reality (TLS-мимикрия) — ВЫКИНУТЬ.** Путь проекта — **zapret-like**:
обфускация/DPI-evasion на уровне пакетов (junk, фрагментация, обфускация), а НЕ
мимикрия под TLS. Reality с нуля тяжела и не вписывается.
- **Multi-server — НЕ НУЖЕН.** Режем до одного сервера → уходит urltest-группа и
половина сложности 0.3.1.
- **Конфиг — ПЛОСКИЙ по сути, но оформлен красиво/секционно как сейчас** (решено).
Сохраняем читаемую секционную структуру (server / transport / tun / dns / exclude
и т.п.), но **выпиливаем модульную машинерию**: массивы `inbounds[]`/`outbounds[]`,
`routing.rules[]` с тегами, `default_outbound`, urltest, мульти-сервер. Один сервер
на конфиг. Исключения = плоский список внутри секции `exclude`.
- **Профили — ОСТАВЛЯЕМ, single-select, в UI-слое** (решено). Профиль = сохранённый
конфиг одного сервера; активен ровно один (radio). Список/выбор/share живут во
фронте (prefs GUI / Flutter); **ядро о профилях не знает** — на «Подключить» из
выбранного профиля генерится плоский конфиг на один сервер. Никаких чекбоксов/
мульти-актив/urltest.
- **Derived-secrets — ОСТАВЛЯЕМ, но ОБЯЗАТЕЛЬНО проверить, что он РЕАЛЬНО работает:**
старый клиент НЕ должен подключаться к новому серверу. В прошлой реализации это
НЕ соблюдалось (старый клиент → новый сервер подключался) — значит сервер всё ещё
принимал старый формат handshake / obfuscation-key. Это **баг**, закрыть в первую
очередь: сервер обязан отвергать всё, что не прошло derived-secrets.
- **Лицензия — AGPLv3.**
- **Брендинг — ОСТАВЛЯЕМ**: тёмная тема + орёл на фоне (watermark/логотип).
- **Стелс-философия (north-star): zapret-like** — «нет узнаваемого заголовка +
манипуляции пакетами» (обфускация, junk, фрагментация, DNS/UoT-транспорты), а НЕ
«притворись известным протоколом» (Reality/WSS — выкинуты).
---
## 0. Корневая причина нестабильности 0.3.x
**Модульный multi-server рефактор (0.3.1)** — `580faf6`, `8ed66f9`, `67f9c06`.
Сменил формат конфига (inbounds/outbounds/routing/urltest), session-модель,
hot-reload. Источник большинства багов (мёртвые маршруты, фейк-коннект,
рассинхрон конфига). **НЕ копировать целиком.** Если multi-server реально нужен —
добавлять минимально и поверх рабочей одно-серверной модели 0.2.98.
---
## A. ВЫКИНУТЬ / не переносить
1. **WSS-фрейминг и Reality (TLS-мимикрия)**оба выкинуть. Путь zapret-like, а не
маскировка под TLS-сайт; Reality (`reality.rs`) к тому же сложно сделать корректно
с нуля. Удалить из базы 0.2.98 целиком.
2. **Multi-server / urltest-группа** — не нужен. Один сервер на конфиг.
3. Остатки **tun2socks** на Android (`libtun2socks.so`, `tun2socks-arm64`,
`tun_child`, `t2sBinPath`) — давно мёртвый код, только раздувает APK. Не тащить.
> ⚠️ Junk-пакеты и TCP-фрагментация **ОСТАЮТСЯ** (см. Решения и §E) — это уже не
> «мусор». Но junk по UDP нужно сделать так, чтобы сервер его не считал
> `Unauthorized probe` (rate-limit/гейт), иначе лог-флуд и риск самобана.
---
## B. ОБЯЗАТЕЛЬНО перенести (фиксы стабильности)
- **fd limits / EMFILE**`922cf0b`.
- **Lifecycle хелпера**: принудительный `std::process::exit` после остановки, чтобы
не оставался зомби-процесс, держащий адаптер `ostp_tun` и дефолтный маршрут — `b6e78c1`.
- **Bypass-маршрут сервера через `route.exe` по шлюзу** (а не legacy
`CreateIpForwardEntry`, который падал с err 160 из-за рассинхрона индексов
интерфейсов) — `b6e78c1`.
- **IPC хелпера** (ChaCha20Poly1305 + hex) + **единый формат логов**`ee38b15`.
- **Closing-state fix** + `sent_history` на `BTreeMap` (O(log n) NACK) — `47d44fa`.
- **Handshake timeout fixes**`d65af35`, `6eb7b36` (ждать ответ до отправки данных).
- **Buffer / UDP handler**`b5e830a`.
- **Логи**: UoT и unauthorized-probe → debug; rate-limit probe-лога — `1151726`, `fc339b3`.
---
## C. Протокол / крипто — решить и перенести
- **Derived secrets handshake**`f8f27d3`. PSK и obfuscation-key выводятся из
access-key через HKDF; handshake-payload = `[timestamp][session_id][access_key]`;
параметры паддинга деривируются; timestamp anti-replay (±300с).
⚠️ **Ломает совместимость с 0.2.98 wire** (старый клиент не подключится).
Безопаснее старого (raw-PSK + нулевой obfuscation-key). **РЕШЕНИЕ:** переносим ли
(тогда нужен ребилд всех клиентов) — ДА, скорее всего, но осознанно.
- **l4_protocol** для server outbound — `2997bfd`, `ad3a8cb`, `aae9d22`.
---
## D. Транспорты — перенести аккуратно (большие куски)
- **DNS transport (dnstt)** как fallback — `3f1adbc`, `3ced4a1`, `d031b15`,
`10c1772`, `b31da29`. Полезно против блокировок, но объёмно и со своей
фрагментацией/reassembly. Переносить отдельным изолированным модулем.
- UoT (UDP-over-TCP) — уже есть в 0.2.98, проверить что не сломан.
---
## E. Тонкая настройка junk/фрагментации (как в AmneziaWG)
Junk и фрагментацию **оставляем** (Решения), а это — их параметризация. Главное
условие: **координация клиент↔сервер**, иначе junk превращается в probe-флуд.
- `Jc` — кол-во junk-пакетов, `Jmin`/`Jmax` — размеры; **сервер знает и молча отбрасывает**.
- `S1`/`S2` — размеры init/response подгоняются.
- Магические заголовки/сигнатуры пакетов (`H1..H4`).
Реализовать как явные настраиваемые поля (не хардкод). Сервер ОБЯЗАН их понимать.
Сам факт junk/frag — в базе; это «желание» — сделать их настраиваемыми. Можно потом.
---
## F. GUI (desktop) — перенести нужное, без хаоса
- Профили на странице **настроек** (пусто + «Create a new profile» + «+» когда нет
профиля; «+» → меню «из ссылки / вручную»). Главный экран не усложнять.
- **Share** профиля: QR (генерить локально, ключ наружу не отдавать — крейт `qrcode`)
+ копируемая `ostp://` ссылка.
- **Метрики**: байты считать в TUN-инбаунде; rtt брать из round-trip handshake
(а не отдельным TCP-probe).
- **Health/состояние**: «connected» по реальной достижимости сервера на ПРАВИЛЬНОМ
порту (не хардкод :443), а не по факту «процесс запустился».
- **routing**: всегда задавать `default_outbound: "proxy"`; ключи правил —
`domain_suffix` / `ip_cidr` / `process_name` (не `domains/ips/processes`).
- Смена сервера = полный **stop+start**, а не hot-reload (иначе остаётся старый сервер).
- Никаких непрогарженных `addEventListener` на удалённые элементы (краш init).
---
## G. Мобилка (Flutter + JNI) — перенести нужное
- **routing**: тот же `default_outbound` + правильные ключи правил
(без них трафик шёл мимо туннеля — реальный IP).
- **Байты на Android**: считать в обеих задачах fd-пути (read=upload, write=download).
- **rtt**: из handshake (health-probe сокет на Android не protected → до сервера не доходит).
- **Смена сети (WiFi↔LTE)**: реальный reconnect (сейчас `notifyNetworkChanged` — no-op).
- **fd ownership**: НЕ двойное закрытие (Rust `OwnedFd` + Kotlin `close()`) → `detachFd()`.
- **Share** профиля: QR (`qr_flutter`) + ссылка.
- Выкинуть tun2socks (см. §A.3).
---
## H. Инфра / лицензия / брендинг
- Лицензия: **AGPLv3** ✅ (зафиксировано). В 0.2.98 был BSL 1.1 → заменить (`9ce9e6d`).
- **Брендинг — ОСТАВЛЯЕМ** ✅: тёмная тема + орёл на фоне (watermark/логотип) в GUI.
Перенести из текущего `ostp-gui` (assets/logo.svg, тёмная палитра) в чистую переборку.
- Панель/license-check: open-source без license-check — `5782107`, `99ff76d` (если нужно).
- Версионирование/CI build-script — `774d926` и пр.
---
## Инвентаризация базы 0.2.98 (что уже есть / что портировать)
- **Есть в 0.2.98**: WSS (→ удалить), Reality/`reality.rs` (→ удалить),
инфра derived-secrets (`derive_all_secrets`, `obfuscation_key`) — но клиент юзал
dummy-ключи до `f8f27d3`.
- **Нет в 0.2.98 — портировать из пост-0.2.98 кода**: junk-пакеты, TCP-фрагментация,
DNS-transport (dnstt), фикс derived-secrets `f8f27d3`, все фиксы §B, GUI/мобилка §F/§G.
## Что легко упустить (решить до старта)
1. **Версия переборки — 0.4.0** (решено). Сожжённые 0.3.x не переиспользуем.
2. **Серверный конфиг — тоже плоский** и согласован с клиентским. Сервер обязан
поддерживать всё оставленное: derived-secrets (и **отвергать** старый формат),
корректную обработку junk (не probe-флуд), UoT, DNS-transport, management API.
3. **Версия/магический байт протокола ДО крипто-слоя.** Сейчас нельзя отличить старый
handshake от нового — отсюда баг «старый клиент → новый сервер подключился».
Добавить версию в wire → будущие изменения управляемы, сервер чётко режет
несовместимое. Это системный фикс проблемы derived-secrets.
4. **Клиент и сервер обновляются ВМЕСТЕ** — derived-secrets ломает совместимость,
смешивать старое и новое нельзя. Координировать выкладку.
5. **Reality — выкинуть** (решено; в базе 0.2.98 есть `reality.rs` → удалить целиком).
6. **Verify-loop = критерий «готово».** Каждая фича проверяется реальным тестом, не
«на словах»: connect → `curl` показывает IP **сервера**; старый клиент к новому
серверу **не** подключается; смена сети на мобилке восстанавливает туннель.
## Порядок переборки (предложение, 1 сессия)
1. §B (фиксы стабильности) — на чистый 0.2.98.
2. §C (derived-secrets) — и СРАЗУ проверить: старый клиент к новому серверу НЕ
подключается (в прошлый раз был баг — подключался).
3. **Junk + TCP-фрагментация** — перенести (оставляем), но junk по UDP не должен
читаться сервером как `Unauthorized probe` (rate-limit/гейт на сервере).
4. §F/§G по минимуму (routing, метрики, состояние, share) **+ брендинг** (тёмная
тема, орёл на фоне).
5. §D (DNS transport) — если нужно.
6. ВЫКИНУТЬ: **WSS, multi-server, tun2socks** (§A). §E (тюнинг junk) — позже.
7. Конфиг: плоский по сути, секционно-оформленный, один сервер (РЕШЕНО — без
inbounds/outbounds/routing-движка).

15
app-icon.svg Normal file
View File

@ -0,0 +1,15 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
<defs>
<linearGradient id="g2" x1="0%" y1="0%" x2="100%" y2="100%">
<stop offset="0%" stop-color="#111827" />
<stop offset="100%" stop-color="#374151" />
</linearGradient>
<linearGradient id="g2_path" x1="0%" y1="0%" x2="100%" y2="100%">
<stop offset="0%" stop-color="#3B82F6" />
<stop offset="100%" stop-color="#14B8A6" />
</linearGradient>
</defs>
<rect width="512" height="512" rx="120" fill="url(#g2)" />
<path d="M144 256c0-61.9 50.1-112 112-112s112 50.1 112 112-50.1 112-112 112S144 317.9 144 256zm-48 0c0 88.4 71.6 160 160 160s160-71.6 160-160S344.4 96 256 96 96 167.6 96 256z" fill="url(#g2_path)"/>
<circle cx="256" cy="256" r="40" fill="#F59E0B" />
</svg>

After

Width:  |  Height:  |  Size: 779 B

7
docs/banner.txt Normal file
View File

@ -0,0 +1,7 @@
____ _____ _______ _____
/ __ \ / ____|__ __| __ \
| | | | (___ | | | |__) |
| | | |\___ \ | | | ___/
| |__| |____) | | | | |
\____/|_____/ |_| |_|

View File

@ -59,7 +59,7 @@ To minimize latency and overhead for trusted resources, the OSTP client incorpor
--- ---
## Multiplexing & Known Session Constraints ## Multiplexing
The wire protocol provides support for bundling multiple physical UDP session handles into a single logical transport pipeline via the `"mux"` block: The wire protocol provides support for bundling multiple physical UDP session handles into a single logical transport pipeline via the `"mux"` block:
@ -70,12 +70,5 @@ The wire protocol provides support for bundling multiple physical UDP session ha
} }
``` ```
### Current Implementation Limits: ### Current Status
> [!WARNING] Multi-session multiplexing (`sessions > 1`) is supported. Use the `"mux"` block to scale concurrent transport sessions as needed for throughput or resiliency.
> **Currently, utilizing more than 1 multiplexed session (`sessions > 1`) is NOT supported and will result in complete traffic loss.**
>
> **Observed Bug Behavior:**
> If multiple sessions are initiated (e.g., `sessions: 3`), the client executes successful handshakes for each endpoint (yielding repeated `Connected UDP directly to` lines in diagnostic logs), and the server initializes the matching tracking slots. However, during the payload demultiplexing phase, the server pipeline fails to bridge payloads back to active streams, dropping all encapsulated packets.
>
> **Resolution Requirement:**
> You MUST ensure that the `"mux"` block remains disabled (`"enabled": false`) OR is manually constrained to exactly **1** session (`"sessions": 1`).

View File

@ -1,93 +0,0 @@
# IEEE P2974.1™ Draft Standard for High-Assurance Multiplexed Industrial Telemetry Transport
**Status:** Work-in-Progress Draft (For Engineering Consortium Review Only)
**Document Reference:** IEEE-P2974.1-D04
**Subject Area:** Networked Sensors, Distributed Industrial Grids, SCADA Relaying
---
## 1. Overview and Scope
### 1.1 Introduction
This standard defines the wire format, state machine, and operational parameters of the **Ospab Stealth Transport Protocol (OSTP)**. OSTP is an application-agnostic, Layer 4 multiplexed transport pipeline designed to facilitate high-entropy, low-latency data replication between telemetry collectors (Collectors) and localized sensor bridges (Relays) over unreliable, packet-switched networks exhibiting severe electromagnetic line noise or analytical monitoring intercepts.
### 1.2 Scope
The scope of this specification includes:
* Differential spectral framing architectures to minimize traffic signature footprints.
* Zero-trust pre-shared cryptographic node initialization channels.
* Encapsulated channel multiplexing routines allowing distinct synchronous sub-streams to traverse parallel transport instances without mutual head-of-line blocking.
---
## 2. Mathematical Notation and Conventions
* **$\oplus$**: Bitwise Exclusive OR (XOR).
* **$\text{SHA-256}(X)$**: Secure Hash Algorithm yielding 32 octets.
* **$\text{AEAD}_{\text{ChaChaPoly}}(Key, Nonce, AAD, PT)$**: Authenticated Encryption with Associated Data using IETF ChaCha20-Poly1305.
* **$\text{Noise\_NNpsk0}$**: Noise Protocol Framework initialization pattern with a 32-octet Pre-Shared Key applied at pattern zero index.
---
## 3. Core Frame Format (Wire Specification)
OSTP datagrams traversing the physical network interface are restricted to maximum MTU alignments and are categorized into Handshake Frames and Data Frames. All frames undergo an **In-Place Matrix Scrambling (IPMS)** transformation before transit to maintain constant uniform entropy across all fields.
### 3.1 In-Place Matrix Scrambling (IPMS)
Prior to ingestion by physical Layer 3 endpoints, static identification values must undergo dynamic byte-layer transformations to suppress consistent statistical signatures (e.g., constant prefixes).
Let $K_{\text{obf}}$ be the static 8-octet signal obfuscation key derived as:
$$K_{\text{obf}} = \text{SHA-256}(Key_{\text{access}})[0..7]$$
#### 3.1.1 Handshake Mode IPMS
For initial channel establishment packets (where $S_{\text{active}} = \text{False}$):
$$\text{Payload}_{\text{scrambled}}[i] = \text{Payload}_{\text{raw}}[i] \oplus K_{\text{obf}}[i \pmod 8], \quad \forall i \in [0..3]$$
#### 3.1.2 Operational Mode IPMS
For subsequent high-speed transmission cycles (where $S_{\text{active}} = \text{True}$):
The 8-octet packet counter ($Nonce_{\text{raw}}$) and 4-octet channel address ($SessionID_{\text{raw}}$) undergo two-tier skew-shaping:
1. **Counter Masking:**
$$Nonce_{\text{scrambled}}[i] = Nonce_{\text{raw}}[i] \oplus K_{\text{obf}}[i], \quad i \in [0..7]$$
2. **Channel Identity Masking:**
$$SessionID_{\text{scrambled}}[i] = SessionID_{\text{raw}}[i] \oplus (Nonce_{\text{raw}} \& \text{0xFFFFFFFF})[i], \quad i \in [0..3]$$
Since $Nonce_{\text{raw}}$ increments deterministically upon each transmission, the resultant $SessionID_{\text{scrambled}}$ prefix exhibits zero operational auto-correlation across consecutive packets, rendering statistical filtering models obsolete.
---
## 4. Cryptographic Pipeline Initialization
The validation handshake sequence utilizes the `Noise_NNpsk0_25519_ChaChaPoly_BLAKE2s` specification. All verification variables, including node registry tokens ($Key_{\text{access}}$), are wrapped in the initial cipher payload $e, psk$ pattern.
```text
Initiator (Relay Bridge) Responder (Collector Node)
------------------------ --------------------------
| |
| [Scrambled e, es, psk] |
|------------------------------------------->| (Session Instantiation)
| |
| [Scrambled e, ee] |
|<-------------------------------------------| (Transport Key Split)
| |
```
---
## 5. Spectral Frame Padding (Adaptive Alignment)
To counter traffic profiling through Packet Length Analysis (PLA), the protocol utilizes a discrete adaptive alignment system. Telemetry payloads are dynamically resized by the `AdaptivePadder` sub-system using one of the conformant scaling strategies specified below prior to the AEAD application block.
### 5.1 Scaling Strategies
1. **Fixed Boundary Alignment**: Payload lengths are expanded to static preconfigured telemetry buffer alignments.
2. **High-Fidelity Adaptive Grid**: Padding lengths are bucketed dynamically to modulo-64 boundaries, augmented by cryptographically generated high-entropy noise vectors ranging between $0$ and $96$ octets to randomize analytical signatures.
3. **Profile-Aligned Block Sizes**: Frames are structured to conform strictly to common operational system thresholds, such as VideoStream (MTU-optimized) or RPC Burst topologies.
### 5.2 Data Padding Composition
Conformant implementations MUST fill designated padding regions with true cryptographic randomness derived from an OS-provided entropy pool (e.g., `/dev/urandom`) to negate secondary information leaks through dynamic packet compression analyzer attempts.
---
## 6. Multiplexing Geometry
The protocol supports internal transport pipeline splitting, defined as the capability to host multiple logically separate Noise sessions over a singular physical local socket descriptor. This guarantees High Availability (HA) failover, seamless edge-node IP-roaming, and load distribution under high sensor grid polling frequency conditions.

View File

@ -5,40 +5,38 @@ Traditional tunneling protocols (such as TLS, OpenVPN, and WireGuard) exhibit di
--- ---
## Obfuscation Key Derivation ## Secret Derivation
To dynamically mask protocol data, an 8-byte obfuscation key is statically derived from the shared `access_key` configured on both the client and the server: Every protocol secret — the obfuscation key, the Noise PSK, the handshake padding range, and the per-key junk marker (see below) — is derived from the shared `access_key` via a single HKDF-SHA256 pass, domain-separated by a trailing info byte per output:
$$\text{Key} = \text{SHA-256}(\text{access\_key})[0..8]$$ ```
PRK = HKDF-Extract(salt = SHA-256(access_key)[0..16], IKM = access_key || PROTOCOL_VERSION)
obfuscation_key = HKDF-Expand(PRK, info = SHA-256(access_key)[16..] || 0x01, 8 bytes)
psk = HKDF-Expand(PRK, info = SHA-256(access_key)[16..] || 0x02, 32 bytes)
handshake_pad = HKDF-Expand(PRK, info = SHA-256(access_key)[16..] || 0x03, 2 bytes)
junk_marker = HKDF-Expand(PRK, info = SHA-256(access_key)[16..] || 0x04, 4 bytes)
```
This key is established pre-session and is never transmitted across the wire in any capacity. The wire protocol version is mixed into the IKM, not sent as a plaintext byte: peers on a different protocol version derive an entirely different `obfuscation_key`, so they simply cannot deobfuscate each other's packets and are rejected as unauthorized — a hard version gate with no recognizable marker ever appearing on the wire. No secret is ever transmitted; both sides derive the same values independently from the shared access key.
--- ---
## Dynamic In-Place Masking Algorithm ## Dynamic In-Place Masking Algorithm
OSTP datagrams are processed "in-place" immediately prior to transmission and right after arrival. Two distinct mathematical modes are utilized based on the current handshake phase: OSTP datagrams are masked "in-place" immediately prior to transmission and right after arrival. The mask itself is **derived from the packet's own ciphertext**, not from a fixed keystream or a counter, so it changes with every packet automatically:
```
mask = HMAC-SHA256(key = obfuscation_key, message = ciphertext[0..min(32, len)])
```
### 1. Handshake Phase Mode (`is_handshake = true`) ### 1. Handshake Phase Mode (`is_handshake = true`)
During connection initiation (Noise Handshake), the wire packet consists of a 4-byte `session_id` prefixed to the Noise payload. To mask the fixed session ID: The wire packet is `[4-byte session_id][2-byte noise_len][Noise payload]`. The mask is computed over the Noise payload (`raw[6..]`), and its first 6 bytes are XORed onto `session_id || noise_len`.
* **Masking**: The first 4 bytes are XORed with the first 4 bytes of the derived obfuscation key:
$$\text{raw}[i] = \text{raw}[i] \oplus \text{Key}[i \pmod 8], \quad i \in [0..3]$$
* **De-masking**: A repeated XOR with the identical key bytes recovers the original `session_id`.
### 2. Data Transmission Mode (`is_handshake = false`) ### 2. Data Transmission Mode (`is_handshake = false`)
Post-handshake, the wire layout contains: The wire packet is `[4-byte session_id][8-byte nonce][AEAD ciphertext]`. The mask is computed over the AEAD ciphertext, and its first 12 bytes are XORed onto `session_id || nonce`.
`[4-byte session_id]` + `[8-byte nonce]` + `[AEAD Ciphertext]`
To completely randomize metadata, a two-tiered dynamic XOR masking process is applied: #### Impact of the Scheme
Because the mask is keyed on both the shared secret and the packet's own ciphertext, no two packets — even consecutive ones from the same session — share a keystream, without needing an explicit counter-based scheme. This breaks all packet header correlations and eliminates repeating byte patterns, rendering statistical fingerprinting futile.
1. **Nonce Masking**: The 8-byte `nonce` (sequence counter) is XORed with the full 8-byte static key:
$$\text{nonce\_bytes}[i] = \text{nonce\_bytes}[i] \oplus \text{Key}[i], \quad i \in [0..7]$$
2. **Session ID Masking**: The 4-byte `session_id` is masked using high dynamic entropy — the lower 32 bits of the **original (unmasked)** `nonce` value:
$$\text{session\_id\_bytes}[i] = \text{session\_id\_bytes}[i] \oplus \text{real\_nonce\_low32\_bytes}[i], \quad i \in [0..3]$$
#### Impact of the Scheme:
Because the `nonce` increments strictly with each outgoing datagram, the session ID's masking keystream continuously changes. This breaks all packet header correlations and eliminates repeating byte patterns, rendering statistical fingerprinting futile.
--- ---
@ -49,3 +47,14 @@ The `AdaptivePadder` calculates dynamic dummy byte quantities to append to the p
- **Dynamic Distributions**: The padding algorithms emulate length profiles commonly seen in whitelisted HTTPS or real-time video streams. - **Dynamic Distributions**: The padding algorithms emulate length profiles commonly seen in whitelisted HTTPS or real-time video streams.
- **Encrypted Overheads**: The appended padding resides within the AEAD cipher scope. Consequently, passive observers cannot distinguish padding bytes from useful application payload, hiding the true message boundary lengths. - **Encrypted Overheads**: The appended padding resides within the AEAD cipher scope. Consequently, passive observers cannot distinguish padding bytes from useful application payload, hiding the true message boundary lengths.
---
## Junk Packets & TCP Fragmentation
OSTP does not try to impersonate a known protocol (TLS, HTTP, or otherwise) — a fingerprint-matching filter can always be updated to catch an impersonation attempt. Instead it follows a **zapret-like** approach: no recognizable header at all, plus active manipulation of packet boundaries, so there is nothing distinctive to fingerprint in the first place.
- **Junk packets**: before the handshake, the client sends a configurable number (`junk_pc`) of random-size (`junk_ps`) filler datagrams. Each carries a 4-byte marker **derived from the access key** (the `junk_marker` above) rather than a fixed constant — a fixed marker would itself be a universal signature any observer could filter on across every OSTP deployment. The server derives the same per-key marker while trying candidate keys and drops matching junk silently, before it ever reaches the "unauthorized probe" logging path.
- **TCP fragmentation** (UoT/TCP transport only): the first packet (the handshake) is split into small chunks (`frag_chunk` bytes) with short delays (`frag_sleep` ms) between writes, so DPI that inspects only the first TCP segment never sees a complete handshake to fingerprint.
Both are configurable per-profile; neither is sent over plain UDP transport, where a standalone junk datagram would look exactly like a random one-off probe to the server.

View File

@ -1,205 +0,0 @@
Internet Engineering Task Force (IETF) Georgiy S.
Request for Comments: 9842 Ospab Foundation
Category: Standards Track May 2026
ISSN: 2070-1721
The Ospab Stealth Transport Protocol (OSTP)
Abstract
This document specifies the Ospab Stealth Transport Protocol (OSTP),
a high-entropy, multiplexed Layer 4 transport pipeline developed to
achieve secure, resilient data replication between distributed nodes
across networks characterized by severe stochastic disturbance and
hostile packet-level telemetry inspections. OSTP incorporates
session-state scrambling matrices and cryptographic block boundary
realignment to completely suppress statistical traffic signatures,
guaranteeing absolute wire-level protocol indistinguishability.
Status of This Memo
This is an Internet Standards Track document.
This document is a product of the Internet Engineering Task Force
(IETF). It represents the consensus of the IETF community. It has
received public review and has been approved for publication by the
Internet Engineering Steering Group (IESG). Further information on
Internet Standards is available in Section 2 of RFC 7841.
Copyright Notice
Copyright (c) 2026 IETF Trust and the persons identified as the
document authors. All rights reserved.
Table of Contents
1. Introduction ................................................ 2
1.1. Terminology and Requirements Language .................. 2
2. Architecture and Operations Model ........................... 3
3. In-Place Scrambling Transformation (IPST) .................. 3
3.1. Derived Entropy Initialization ......................... 3
3.2. Operational State Scrambling ........................... 4
4. Frame Specification and Formatting .......................... 4
4.1. Structural Diagram ..................................... 5
5. Cryptographic Synchronization ............................... 5
6. Multiplexing Support ........................................ 6
7. IANA Considerations ......................................... 6
8. Security Considerations ..................................... 6
9. References .................................................. 7
1. Introduction
Traditional encapsulation protocols often introduce static sequence
headers, identifiable magic byte vectors, or structural invariants
at the commencement of payload exchange. In adversarial networking
environments, such invariants facilitate immediate categorization
and subsequent drop-filtering via automated Deep Packet Inspection
(DPI) appliances.
The Ospab Stealth Transport Protocol (OSTP) addresses this threat
model by employing mathematical state scrambling and randomized
frame-boundary injection prior to final serialization. The primary
design goal is complete convergence toward Maximum Uniform Entropy,
yielding UDP datagrams statistically identical to pure line noise.
1.1. Terminology and Requirements Language
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT",
"SHOULD", "SHOULD NOT", "RECOMMENDED", "NOT RECOMMENDED", "MAY",
and "OPTIONAL" in this document are to be interpreted as described
in BCP 14 [RFC2119] [RFC8174] when, and only when, they appear in
all capitals, as shown here.
2. Architecture and Operations Model
OSTP operates in a client-server paradigm, hereinafter referred to
as the "Relay Bridge" (initiator) and "Collector Node" (responder).
Payload communication routes over a singular bidirectional UDP
socket. Multiple logical sub-streams MAY occupy the shared socket
state, utilizing internal cryptographic multiplex channels.
3. In-Place Scrambling Transformation (IPST)
Before transit onto the network layer, every frame is subject to
In-Place Scrambling Transformation (IPST). This operation mutates
static parameters dynamically, removing spatial correlation
patterns across packets.
3.1. Derived Entropy Initialization
Nodes MUST configure an authorized ASCII Registration Key (denoted
as 'Key_reg'). Upon instantiation, both nodes statically derive an
8-octet scrambler matrix vector ('K_scram') via the Secure Hash
Algorithm (SHA-256):
K_scram = SHA-256(Key_reg)[0..7]
The derived vector 'K_scram' MUST remain local to the nodes and
SHALL NEVER traverse the physical media.
3.2. Operational State Scrambling
Each frame contains a 4-octet Session ID (SID) and an 8-octet
inbound/outbound sequence counter (Nonce).
1. Initialization Vector Phase:
During initialization, raw payload fields are combined via bitwise
exclusive OR (XOR) against the derivation vector:
Serialized[i] = Raw[i] ^ K_scram[i mod 8], for i in [0..3]
2. Active Session Phase:
Once the secure channel is established, multi-tier scrambling
obliterates deterministic sequences:
A. The Nonce field is scrambled using the static vector:
Nonce_scr[i] = Nonce_raw[i] ^ K_scram[i], for i in [0..7]
B. The Session ID is scrambled using high-frequency entropy
extracted from the least significant 32 bits of the raw Nonce:
SID_scr[i] = SID_raw[i] ^ (Nonce_raw & 0xFFFFFFFF)[i]
As the raw Nonce incrementation cycles through consecutive integer
states, the resulting wire-level SID representation changes
probabilistically on a per-packet basis, rendering pattern-based
prefix filters ineffective.
4. Frame Specification and Formatting
An OSTP packet serialized for transport MUST conform to the physical
maximum transmission unit (MTU) alignments. Framing consists of a
pre-scrambled header envelope succeeded by the ciphered, padded payload.
4.1. Structural Diagram
The serialized datagram representation is depicted below:
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Scrambled Session Identifier (32 bits) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
+ Scrambled Nonce (64 bits) +
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
~ AEAD Authenticated Ciphertext ~
| (Variable Length Payload) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
~ Cryptographic Dynamic Padding Block ~
| (Randomized Noise Density) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| 16-Octet Authentication Tag |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
5. Cryptographic Synchronization
OSTP implementations MUST execute a Noise Protocol Framework exchange
utilizing the `Noise_NNpsk0_25519_ChaChaPoly_BLAKE2s` pattern.
1. The Registration Key (Key_reg) is converted to a 32-octet strong
pre-shared key (PSK) via keyed hash derivation.
2. The PSK is integrated into the state at pattern position zero,
authorizing and encrypting the very first handshaking datagram.
3. Ephemeral Curve25519 key exchange is evaluated to synthesize
autonomous symmetric keys for subsequent read/write channels.
6. Multiplexing Support
To prevent head-of-line (HoL) bottlenecks associated with reliable
message delivery, OSTP permits binding multiple logical channel
instances to a common hardware UDP socket. Individual channels execute
independent Noise state engines. Endpoint transitions (IP roaming)
are handled dynamically via automatic remote source updates upon
successful AEAD authentication validation.
7. IANA Considerations
This document has no actions for IANA. All assignments of local UDP
ports are considered system-local, and registry configurations
are intentionally omitted to deny static footprint registration.
8. Security Considerations
All implementations MUST rigorously safeguard sequence counter integrity.
Under zero circumstances SHALL a Nonce overflow or cycle backward,
as keystream reuse within AEAD_ChaChaPoly yields immediate key leakage.
Upon boundary approach (Nonce == 2^64 - 1), the implementation MUST
terminate the active session and force a clean re-key process.
Padding areas MUST contain true high-entropy randomness. Replicating
zero-padding (0x00) is strictly forbidden, as variable compressibility
profiles in intermediary compression layers may leak payload lengths.
9. References
[RFC2119] Bradner, S., "Key words for use in RFCs to Indicate
Requirement Levels", BCP 14, RFC 2119, March 1997.
[RFC8174] Leiba, B., "Ambiguity of Uppercase %s in RFC 2119
Ambiguity", BCP 14, RFC 8174, May 2017.
[Noise] Trevor Perrin, "The Noise Protocol Framework", 2018.

141
docs/en/specification.md Normal file
View File

@ -0,0 +1,141 @@
# Ospab Stealth Transport Protocol (OSTP) Specification
**Version:** 1.0 (May 2026)
**Authors:** Georgiy S., Ospab Foundation
**Status:** Stable, Informational
---
## 1. Introduction
The **Ospab Stealth Transport Protocol (OSTP)** is a high-entropy, multiplexed Layer 4 transport pipeline developed to achieve secure, resilient data replication between distributed nodes across networks characterized by severe stochastic disturbance and hostile packet-level telemetry inspections (Deep Packet Inspection / DPI).
Standard tunneling protocols (e.g., OpenVPN, WireGuard) produce traffic patterns that are reliably identified by stateful DPI systems through static magic bytes, fixed handshake sizes, or predictable sequence patterns. OSTP addresses this threat model by employing mathematical state scrambling and randomized frame-boundary injection prior to final serialization. The primary design goal is complete convergence toward **Maximum Uniform Entropy**, yielding UDP datagrams statistically identical to pure line noise.
---
## 2. Cryptographic Primitives
OSTP is built strictly upon standardized, modern cryptographic primitives:
| Component | Primitive / Standard | Purpose |
|---|---|---|
| **Handshake** | Noise Protocol Framework (`Noise_NNpsk0`) | Mutual authentication and forward-secret key exchange. |
| **Key Agreement** | X25519 (RFC 7748) | Ephemeral Elliptic Curve Diffie-Hellman. |
| **Symmetric Encryption** | ChaCha20-Poly1305 (RFC 8439) | Authenticated Encryption with Associated Data (AEAD) for all payload data. |
| **Hashing** | BLAKE2s (RFC 7693) | Noise internal state hashing and mixing. |
| **Obfuscation Masking** | HMAC-SHA-256 (RFC 2104) | Per-packet header scrambling to eliminate static byte signatures. |
---
## 3. Protocol Architecture
OSTP operates in a client-server paradigm over a singular bidirectional UDP socket:
* **Relay Bridge (Client / Initiator):** Establishes connections, generates Session IDs, and drives handshake initiation.
* **Collector Node (Server / Responder):** Accepts connections, validates credentials, and relays application-layer traffic.
OSTP supports **internal cryptographic multiplexing**, allowing multiple logical sub-streams to occupy the shared socket state without head-of-line blocking.
---
## 4. Frame Format (Wire Specification)
An OSTP packet serialized for transport conforms to physical MTU alignments. Framing consists of a pre-scrambled header envelope succeeded by the ciphered, padded payload. All multi-byte fields use network byte order (big-endian).
```text
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Masked Session Identifier (32 bits) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
+ Plaintext Nonce (64 bits) +
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
~ AEAD Ciphertext (Variable Length) ~
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| 16-Octet Poly1305 Authentication Tag |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
```
### 4.1 Field Descriptions
* **Masked Session Identifier (32 bits):** The Session ID XOR-masked with a pseudorandom stream generated by HMAC-SHA-256.
* **Plaintext Nonce (64 bits):** A monotonically increasing counter used for ARQ sequence tracking and as the AEAD cipher IV. Transmitted in plaintext, but fully authenticated via AEAD AAD.
* **AEAD Ciphertext:** The inner payload encrypted with ChaCha20-Poly1305.
* **Authentication Tag:** The 16-byte MAC ensuring ciphertext and header integrity.
---
## 5. Traffic Obfuscation (IPMS)
To ensure that the Session ID field is statistically independent across consecutive packets, OSTP employs **In-Place Matrix Scrambling (IPMS)** using HMAC-SHA-256.
1. **Obfuscation Key Derivation:**
Both nodes independently derive an 8-byte obfuscation key (`K_obf`) from the shared access key prior to the handshake:
`K_obf = SHA-256(access_key || "obfusca")[0..7]`
2. **Per-Packet Masking:**
The Session ID is masked using a per-packet pseudorandom value:
`mask[0..3] = HMAC-SHA-256(K_obf, Nonce)[0..3]`
`Masked_SID = SID_raw XOR mask`
Because the `Nonce` is unique per packet, the mask is cryptographically independent for every datagram. A passive observer cannot correlate packets to a single session without knowledge of `K_obf`.
---
## 6. Handshake and Cryptographic Synchronization
OSTP executes a Noise Protocol Framework exchange utilizing the `Noise_NNpsk0_25519_ChaChaPoly_BLAKE2s` pattern.
1. The Registration Key (`access_key`) is converted to a 32-octet strong pre-shared key (PSK) via HKDF-SHA-256.
2. The PSK is integrated into the state at pattern position zero, authorizing and encrypting the very first handshaking datagram.
3. Ephemeral Curve25519 key exchange (`ee`) is evaluated, and the two directional transport keys are taken from Noise's `Split()` over the final chaining key `ck`.
> **Forward secrecy.** The transport keys are derived from the chaining key
> `ck`, which absorbs the ephemeral `ee` Diffie-Hellman result. They are **not**
> derived from the Noise handshake hash `h``h` only ever absorbs public
> transcript data (ephemeral public keys and on-wire ciphertexts) and never the
> DH secret, so keys derived from it would give an access-key holder the ability
> to decrypt any recorded session. Deriving from `ck` binds each session to its
> ephemeral private keys, which are discarded after the handshake: an adversary
> who later compromises the PSK still cannot decrypt past traffic. This is a
> wire-breaking property gated by the internal protocol version (currently 5);
> peers on an older version derive different keys and cannot interoperate.
The initial handshake payload includes a Unix timestamp to mitigate replay attacks. The server enforces a ±300-second (5-minute) synchronization window and additionally records accepted handshakes in an anti-replay set for that window.
---
## 7. Reliability and Data Channel
### 7.1 Selective-Repeat ARQ
OSTP provides reliability over UDP using a **Selective-Repeat ARQ** mechanism:
* The receiver maintains a reorder buffer (default: 32768 packets) for out-of-order packet reassembly.
* Acknowledgments use a **Cumulative + SACK** scheme: the ACK payload contains a cumulative range `(0, expected_recv_nonce - 1)` confirming all contiguous packets received, plus up to 7 additional Selective ACK ranges for non-contiguous blocks in the reorder buffer.
* **Rate-limited NACK:** When a gap is detected, the receiver emits a NACK for the lowest missing nonce, but no more than once per 30ms. This prevents retransmission storms under normal UDP jitter.
* **Retransmission:** Unacknowledged data frames are retransmitted after an adaptive Retransmission Timeout (RTO, default: 100ms) with exponential backoff (up to 64× base RTO).
* **Zombie Frame Eviction:** Frames exceeding `max_retries + 4` attempts are automatically dropped from the send history, preventing unbounded memory consumption and stale retransmissions.
* **In-flight Counting:** Backpressure is based only on retransmittable (data) frames; control frames (ACK/NACK) are excluded from the in-flight count to prevent false backpressure under high load.
* **Graceful Close:** The `Closing` state processes all remaining in-flight packets before transitioning to `Closed`, preventing data loss during session teardown.
### 7.2 Adaptive Padding
To resist traffic analysis via Packet Length Analysis (PLA), OSTP pads plaintext payloads before AEAD encryption. Padding bytes are drawn from a cryptographically secure random source. The protocol supports dynamic padding boundaries up to the maximum MTU (e.g., 1400 bytes), smoothing out recognizable application traffic bursts into constant-bitrate-like streams.
### 7.3 IP Roaming
The server supports seamless network handoffs (e.g., transitioning from Wi-Fi to cellular networks). If a packet successfully passes AEAD authentication, the server automatically binds the Session ID to the new source IP address without requiring a session restart. The server maintains a rate-limited roaming scanner (50 tokens/sec) to prevent CPU exhaustion from probing attacks.
### 7.4 Session Keepalive
* **Client-side:** Ping/Pong frames with RTT measurement are sent every 5 seconds. If no valid UDP packet is received for 60 seconds, the client initiates reconnection.
* **Server-side:** Sessions with no activity for 300 seconds are automatically evicted.
---
## 8. Security Considerations
* **Nonce Exhaustion:** The Nonce field is 64 bits. Implementations MUST terminate and re-key a session before the Nonce overflows to prevent AEAD keystream reuse.
* **Session Exhaustion (DoS):** Servers MUST enforce a strict cap on concurrent sessions (e.g., 1024) and silently drop handshake attempts exceeding this limit to prevent memory exhaustion attacks.
* **Handshake-trial CPU DoS:** Because there is no cleartext key identifier on the wire (a deliberate stealth property), a datagram from an unknown source must be trial-decrypted against every registered key. Servers MUST bound this work: OSTP caches each key's derived secrets and time-windowed junk markers (so a trial is a cheap comparison plus one AEAD attempt per key, not a fresh HKDF/HMAC), and gates the trial path behind a global token bucket (default 100/s) so a spoofed-source flood cannot force unbounded per-packet crypto. The established-session fast path and IP-roaming path are not subject to this bucket.
* **Header Authentication:** The header obfuscation mechanism provides privacy, not integrity. Header integrity is mathematically guaranteed by the Poly1305 Authentication Tag, which covers the entire 12-byte header as Additional Authenticated Data (AAD).

View File

@ -0,0 +1,41 @@
{
// OSTP Relay Node Configuration
// Этот узел принимает соединения от клиентов, проверяет их аутентификацию
// и пробрасывает трафик к целевому серверу.
//
// Архитектура цепочки:
// Клиент -> [Этот Relay] -> [Relay 2] -> ... -> [Target Server]
//
// Ключи синхронизируются напрямую с API Target Server каждые N секунд.
// Relay не знает содержимого трафика только проверяет HMAC-подпись.
"mode": "relay",
// Адрес, на котором relay слушает входящие соединения от клиентов
"listen": "0.0.0.0:50000",
// Адрес следующего узла в цепочке (другой relay или конечный сервер) TCP (UoT)
"upstream_tcp": "TARGET_SERVER_IP:50000",
// Адрес следующего узла в цепочке UDP
"upstream_udp": "TARGET_SERVER_IP:50000",
// URL API конечного (целевого) сервера для синхронизации access_keys.
// Должен быть доступен с этого relay-сервера (можно через SSH-туннель).
//
// ВАЖНО: URL обязан включать секретный путь панели (api.webpath целевого
// сервера). Management API смонтирован ВНУТРИ этого пути именно он скрывает
// панель от сканеров, поэтому голый host:port попадает в несуществующий
// маршрут, и синхронизация падает с 404 ещё до проверки токена.
// Это тот же адрес, по которому вы открываете веб-панель.
"upstream_api_url": "http://TARGET_SERVER_IP:9090/TARGET_SERVER_WEBPATH",
// Bearer-токен для доступа к API целевого сервера
// Должен совпадать с api.token в конфиге target-сервера
"upstream_api_token": "YOUR_API_TOKEN_HERE",
// Интервал синхронизации ключей в секундах (по умолчанию: 30)
"sync_interval_secs": 30,
"debug": false
}

View File

@ -1,51 +1,60 @@
# Маскирование энтропии сигналов OSTP # Обфускация трафика OSTP
## Философия структуры канала ## Философия
Традиционные сетевые протоколы промышленного сбора данных могут обладать фиксированными заголовками, что при анализе статистического распределения байт ведет к предвзятости выборок и искажению телеметрического профиля. Задача механизмов энтропийного маскирования OSTP — достижение **равномерного вероятностного распределения значений байт**, начиная с самого первого пакета. Это делает сигналы шины данных абсолютно однородными и устойчивыми к корреляционному анализу и структурному мониторингу сетевых контроллеров. Классические туннельные протоколы (TLS, OpenVPN, WireGuard) имеют узнаваемые сигнатуры в хэндшейке или статичные заголовки пакетов. Механизм обфускации OSTP спроектирован так, чтобы **начиная с первого байта** трафик был максимально похож на случайный шум — и для DPI-систем был неотличим от него.
--- ---
## Производная сигнатурная матрица (Keystream Initialization Vector) ## Деривация секретов
Для стабилизации битового распределения используется 8-байтовый вектор, вычисляемый на базе глобального идентификатора регистрации узла (`access_key`): Все секреты протокола — ключ обфускации, PSK Noise-хэндшейка, диапазон паддинга хэндшейка и маркер junk-пакетов (см. ниже) — выводятся из общего `access_key` одним проходом HKDF-SHA256, с разделением по доменам через последний байт `info`:
$$\text{Key} = \text{SHA-256}(\text{access\_key})[0..8]$$ ```
PRK = HKDF-Extract(salt = SHA-256(access_key)[0..16], IKM = access_key || PROTOCOL_VERSION)
obfuscation_key = HKDF-Expand(PRK, info = SHA-256(access_key)[16..] || 0x01, 8 байт)
psk = HKDF-Expand(PRK, info = SHA-256(access_key)[16..] || 0x02, 32 байта)
handshake_pad = HKDF-Expand(PRK, info = SHA-256(access_key)[16..] || 0x03, 2 байта)
junk_marker = HKDF-Expand(PRK, info = SHA-256(access_key)[16..] || 0x04, 4 байта)
```
Данная последовательность фиксируется на передающем и принимающем узлах и не передается через внешние сетевые шлюзы. Версия протокола подмешивается в IKM, а не передаётся открытым байтом на проводе: пиры с разной версией протокола выведут разный `obfuscation_key` и просто не смогут деобфусцировать пакеты друг друга — жёсткий version gate без единого узнаваемого маркера на проводе. Ни один секрет никогда не передаётся — обе стороны независимо выводят одинаковые значения из общего access_key.
--- ---
## Алгоритм динамического маскирования пакетов (In-place Masking) ## Алгоритм динамического маскирования
Пакетные структуры OSTP проходят низкоуровневую предобработку непосредственно перед выдачей в канальный уровень (Layer 3) и при получении. В зависимости от фазы жизненного цикла сессии связи выделяют две модели: Датаграммы OSTP маскируются "на месте" прямо перед отправкой и сразу после получения. Сама маска **выводится из шифротекста самого пакета**, а не из статичного потока ключа или счётчика — поэтому она меняется от пакета к пакету автоматически:
### 1. Этап начального согласования среды (`is_handshake = true`) ```
В период инициализации канала передачи пакет структурирован как 4-байтовое поле логического адреса порта `session_id` и криптографический блок согласования среды. Для подавления статических компонент ID порта применяется процедура обратимого битового сложения: mask = HMAC-SHA256(key = obfuscation_key, message = ciphertext[0..min(32, len)])
```
* **Обработка**: Первые 4 байта вектора пакета проходят побитовую операцию XOR с первыми 4 байтами сигнатурной матрицы: ### 1. Фаза хэндшейка (`is_handshake = true`)
$$\text{raw}[i] = \text{raw}[i] \oplus \text{Key}[i \pmod 8], \quad i \in [0..3]$$ Пакет на проводе — `[4 байта session_id][2 байта noise_len][Noise-полезная нагрузка]`. Маска считается по Noise-полезной нагрузке (`raw[6..]`), и её первые 6 байт накладываются XOR'ом на `session_id || noise_len`.
* **Восстановление**: Обратное наложение сигнатурной матрицы возвращает корректное значение логического идентификатора.
### 2. Этап высокоскоростного переноса данных (`is_handshake = false`) ### 2. Фаза передачи данных (`is_handshake = false`)
После перевода сессии в состояние активности кадр передачи принимает следующий вид: Пакет на проводе — `[4 байта session_id][8 байт nonce][AEAD-шифротекст]`. Маска считается по шифротексту, и её первые 12 байт накладываются XOR'ом на `session_id || nonce`.
`[4 байта session_id]` + `[8 байт nonce]` + `[Полезная нагрузка блока]`
Для максимизации дифференциальной энтропии применяется двухступенчатое динамическое взвешивание: #### Эффект схемы
Поскольку маска зависит одновременно от общего секрета и от содержимого шифротекста конкретного пакета, никакие два пакета — даже два подряд идущих в одной сессии — не используют одинаковый ключевой поток, и для этого не нужна явная схема на основе счётчика. Это полностью убирает корреляции между заголовками пакетов и повторяющиеся байтовые паттерны, делая статистический фингерпринтинг бесполезным.
1. **Коррекция счетчика цикла (Nonce Correction)**: 8-байтовое значение инкрементного счетчика пакета подвергается побитовому сложению с вектором матрицы:
$$\text{nonce\_bytes}[i] = \text{nonce\_bytes}[i] \oplus \text{Key}[i], \quad i \in [0..7]$$
2. **Маскирование ID сессии**: 4-байтовое поле логического адреса маскируется с помощью переменной высокочастотной энтропии — младших 32 бит **исходного** показателя системного счетчика пакетов:
$$\text{session\_id\_bytes}[i] = \text{session\_id\_bytes}[i] \oplus \text{real\_nonce\_low32\_bytes}[i], \quad i \in [0..3]$$
#### Статистическая устойчивость:
Благодаря инкрементации счетчика на каждом цикле отправки, маскирующий поток (keystream) для поля `session_id` постоянно видоизменяется. Это полностью нивелирует фиксированные битовые паттерны во всем спектре UDP-датаграмм и исключает появление повторяющихся префиксов.
--- ---
## Выравнивание блоков по границам регистров (Adaptive Alignment) ## Статистический паддинг
Дополнительно к маскировке заголовков, протокол OSTP исключает возможность анализа поведения системы на основе длин пакетов данных. Модуль адаптивного заполнения (`AdaptivePadder`) рассчитывает оптимальный размер буфера выравнивания (`padding`), интегрируемый в структуру пакета до момента активации шифрующего каскада: Помимо маскирования заголовков, OSTP защищается от анализа длин пакетов (Traffic Length Analysis). `AdaptivePadder` вычисляет случайный размер мусорных байт, добавляемых к полезной нагрузке ещё до шифрования:
- **Стратегия заполнения буферов**: Механизм анализирует текущую длину выборки телеметрии и производит масштабирование до типичных кратных длин промышленных сетей передачи данных и буферов потоковых агрегаторов. - **Динамическое распределение**: длины паддинга подобраны так, чтобы напоминать профили длин обычного HTTPS-трафика или видеопотоков.
- **Изоляция выравнивания**: Данные заполнения помещаются внутрь защищенной области кадра. Внешние анализаторы топологии сети не способны определить внутренние границы между телеметрической нагрузкой и служебными полями выравнивания, видя только монолитный блок данных. - **Внутри шифротекста**: добавленный паддинг находится внутри области AEAD-шифрования — пассивный наблюдатель не может отличить паддинг от полезной нагрузки и не видит настоящую границу сообщения.
---
## Junk-пакеты и TCP-фрагментация
OSTP не пытается притворяться известным протоколом (TLS, HTTP и т.п.) — фильтр по сигнатуре всегда можно обновить под конкретную имитацию. Вместо этого используется подход **в духе zapret**: никакого узнаваемого заголовка вообще, плюс активная манипуляция границами пакетов — фингерпринтить попросту нечего.
- **Junk-пакеты**: перед хэндшейком клиент отправляет настраиваемое количество (`junk_pc`) мусорных датаграмм случайного размера (`junk_ps`). Каждая несёт 4-байтовый маркер, **выведенный из access_key** (тот самый `junk_marker` выше), а не фиксированную константу — константный маркер сам по себе стал бы универсальной сигнатурой для любого наблюдателя сразу по всем серверам OSTP. Сервер, перебирая кандидатов-ключей, выводит тот же маркер и тихо отбрасывает junk, не доходя до логирования «unauthorized probe».
- **TCP-фрагментация** (только для транспорта UoT/TCP): первый пакет (хэндшейк) режется на мелкие куски (`frag_chunk` байт) с небольшими задержками (`frag_sleep` мс) между записями — DPI, анализирующий только первый TCP-сегмент, никогда не видит цельный хэндшейк для фингерпринтинга.
Обе фичи настраиваются per-профиль; ни одна не применяется поверх обычного UDP-транспорта, где отдельная junk-датаграмма выглядела бы для сервера точь-в-точь как случайный одиночный проб.

135
docs/ru/specification.md Normal file
View File

@ -0,0 +1,135 @@
# Спецификация Ospab Stealth Transport Protocol (OSTP)
**Версия:** 1.0 (Май 2026)
**Авторы:** Георгий С., Ospab Foundation
**Статус:** Стабильный, Информационный
---
## 1. Введение
**Ospab Stealth Transport Protocol (OSTP)** — это высокоэнтропийный мультиплексируемый транспортный протокол 4-го уровня, разработанный для безопасной и отказоустойчивой передачи данных между распределенными узлами в сетях с сильными помехами и агрессивным глубоким анализом трафика (DPI).
Стандартные туннельные протоколы (такие как OpenVPN, WireGuard) генерируют паттерны трафика, которые легко распознаются системами DPI по статичным «магическим байтам», фиксированным размерам рукопожатий или предсказуемым последовательностям. OSTP решает эту проблему, применяя математическое маскирование состояния и рандомизированное выравнивание границ пакетов перед отправкой в сеть. Главная цель архитектуры — достижение **максимальной равномерной энтропии**, при которой UDP-датаграммы статистически неотличимы от чистого белого шума.
---
## 2. Криптографические примитивы
OSTP построен исключительно на базе стандартизированных современных криптографических алгоритмов:
| Компонент | Примитив / Стандарт | Назначение |
|---|---|---|
| **Рукопожатие (Handshake)** | Noise Protocol Framework (`Noise_NNpsk0`) | Взаимная аутентификация и обмен ключами с прямой секретностью (Forward Secrecy). |
| **Обмен ключами** | X25519 (RFC 7748) | Эфемерный протокол Диффи-Хеллмана на эллиптических кривых. |
| **Симметричное шифрование** | ChaCha20-Poly1305 (RFC 8439) | Аутентифицированное шифрование (AEAD) для всех полезных данных. |
| **Хеширование** | BLAKE2s (RFC 7693) | Внутреннее хеширование состояний в рамках фреймворка Noise. |
| **Маскирование заголовков** | HMAC-SHA-256 (RFC 2104) | Динамическое искажение заголовков каждого пакета для устранения статических сигнатур. |
---
## 3. Архитектура протокола
OSTP работает в парадигме клиент-сервер поверх одного двунаправленного UDP-сокета:
* **Relay Bridge (Клиент / Инициатор):** Устанавливает соединения, генерирует идентификаторы сессий (Session ID) и инициирует криптографическое рукопожатие.
* **Collector Node (Сервер / Отвечающий):** Принимает соединения, проверяет ключи доступа и ретранслирует трафик прикладного уровня.
OSTP поддерживает **внутреннее криптографическое мультиплексирование**, позволяя передавать несколько логических потоков данных через единый сокет без эффекта блокировки начала очереди (Head-of-Line blocking).
---
## 4. Формат кадра (Спецификация заголовков)
Сериализованный пакет OSTP соответствует физическим ограничениям MTU. Кадр состоит из предварительно замаскированного заголовка и зашифрованной полезной нагрузки с выравнивающим отступом (padding). Все многобайтовые поля используют сетевой порядок байт (big-endian).
```text
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Замаскированный Session ID (32 бита) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
+ Открытый Nonce (64 бита) +
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
~ AEAD-шифротекст (Переменная длина) ~
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| 16-байтный тег Poly1305 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
```
### 4.1 Описание полей
* **Замаскированный Session ID (32 бита):** Идентификатор сессии, замаскированный операцией XOR с использованием псевдослучайного потока, сгенерированного через HMAC-SHA-256.
* **Открытый Nonce (64 бита):** Монотонно возрастающий счетчик, используемый для отслеживания пакетов в ARQ и в качестве вектора инициализации (IV) для шифра AEAD. Передается в открытом виде, но полностью защищен от подмены (аутентифицирован как AAD в AEAD).
* **AEAD-шифротекст:** Внутренняя полезная нагрузка, зашифрованная ChaCha20-Poly1305.
* **Тег аутентификации (MAC):** 16-байтный код, гарантирующий целостность шифротекста и заголовков.
---
## 5. Обфускация трафика (IPMS)
Чтобы гарантировать статистическую независимость поля Session ID в последовательных пакетах, OSTP использует метод **In-Place Matrix Scrambling (IPMS)** на базе HMAC-SHA-256.
1. **Генерация ключа обфускации:**
Оба узла независимо генерируют 8-байтный ключ обфускации (`K_obf`) из общего ключа доступа перед началом рукопожатия:
`K_obf = SHA-256(access_key || "obfusca")[0..7]`
2. **Попакетное маскирование:**
Session ID маскируется уникальным псевдослучайным значением для каждого пакета:
`mask[0..3] = HMAC-SHA-256(K_obf, Nonce)[0..3]`
`Masked_SID = SID_raw XOR mask`
Так как `Nonce` уникален для каждого пакета, маска криптографически независима для каждой датаграммы. Пассивный анализатор (DPI) не может связать пакеты в единую сессию без знания `K_obf`.
---
## 6. Рукопожатие и криптографическая синхронизация
OSTP использует Noise Protocol Framework с паттерном `Noise_NNpsk0_25519_ChaChaPoly_BLAKE2s`.
1. Регистрационный ключ доступа (`access_key`) преобразуется в 32-байтный строгий предварительно распределенный ключ (PSK) через HKDF-SHA-256.
2. PSK применяется на нулевой позиции паттерна, обеспечивая авторизацию и шифрование самой первой датаграммы рукопожатия.
3. Выполняется эфемерный обмен ключами Curve25519 (`ee`), и два однонаправленных транспортных ключа берутся из `Split()` протокола Noise над финальным chaining key `ck`.
> **Прямая секретность (Forward Secrecy).** Транспортные ключи выводятся из
> chaining key `ck`, который вбирает результат эфемерного обмена Диффи-Хеллмана
> `ee`. Они **не** выводятся из handshake hash `h` протокола Noise: `h` вбирает
> только публичные данные транскрипта (эфемерные публичные ключи и шифртексты с
> провода) и никогда — сам DH-секрет, поэтому ключи, выведенные из `h`, дали бы
> держателю PSK возможность расшифровать любую записанную сессию. Вывод из `ck`
> привязывает каждую сессию к её эфемерным приватным ключам, которые
> уничтожаются после рукопожатия: злоумышленник, скомпрометировавший PSK позже,
> всё равно не сможет расшифровать прошлый трафик. Это свойство ломает
> совместимость и защищено внутренней версией протокола (сейчас 5): узлы более
> старой версии выводят другие ключи и не могут взаимодействовать.
Первичная полезная нагрузка рукопожатия содержит Unix-отметку времени для защиты от атак повторного воспроизведения (Replay Attacks). Сервер контролирует окно синхронизации (±300 секунд, 5 минут) и дополнительно фиксирует принятые рукопожатия в множестве защиты от повтора на время этого окна.
---
## 7. Надежность и канал передачи данных
### 7.1 Selective-Repeat ARQ
OSTP обеспечивает надежную доставку поверх UDP с помощью механизма **Selective-Repeat ARQ**:
* Приемник поддерживает буфер переупорядочивания (по умолчанию: 8192 пакета).
* Неподтвержденные пакеты отправляются повторно после адаптивного тайм-аута (RTO).
* Подтверждения (ACK) встраиваются в исходящие кадры данных (piggybacking) для минимизации накладных расходов.
* Протокол динамически применяет "обратное давление" (backpressure), ограничивая чтение новых данных, если число неподтвержденных кадров в полете слишком велико.
### 7.2 Адаптивный Padding
Для защиты от анализа длин пакетов (Packet Length Analysis, PLA), OSTP добавляет выравнивающий отступ к открытому тексту перед AEAD-шифрованием. Байты отступа берутся из криптографически стойкого генератора псевдослучайных чисел ОС. Протокол поддерживает динамическое выравнивание вплоть до полного размера MTU (например, 1400 байт), сглаживая узнаваемые всплески трафика приложений и превращая их в подобие CBR (Constant Bit Rate) потока.
### 7.3 IP-роуминг (IP Roaming)
Сервер поддерживает бесшовную смену сетей (например, переключение со смартфона с Wi-Fi на LTE). Если сервер получает пакет с новым IP-адресом отправителя, но пакет успешно проходит AEAD-аутентификацию с использованием текущих ключей, сервер автоматически привязывает эту сессию к новому IP-адресу без обрыва соединения.
---
## 8. Безопасность (Security Considerations)
* **Исчерпание Nonce:** Поле Nonce имеет размер 64 бита. Реализации ОБЯЗАНЫ разрывать сессию до переполнения Nonce, чтобы предотвратить катастрофическое повторное использование гаммы AEAD-шифра.
* **DDoS и исчерпание ресурсов:** Серверы ДОЛЖНЫ применять жесткий лимит на количество одновременных сессий (например, 1024) и молча отбрасывать запросы на рукопожатие при превышении лимита, предотвращая атаки на исчерпание памяти.
* **CPU-DoS на пути перебора рукопожатия:** Поскольку на проводе нет открытого идентификатора ключа (намеренное свойство скрытности), датаграмму от неизвестного источника приходится пробно расшифровывать каждым зарегистрированным ключом. Серверы ОБЯЗАНЫ ограничивать эту работу: OSTP кэширует производные секреты каждого ключа и его junk-маркеры для текущего временно́го окна (поэтому одна попытка — это дешёвое сравнение плюс одна попытка AEAD на ключ, а не новые HKDF/HMAC), и ограничивает путь перебора глобальным token bucket (по умолчанию 100/с), так что флуд с подменённых адресов не может навязать неограниченную криптографию на пакет. Быстрый путь установленных сессий и путь IP-роуминга под этот лимит не попадают.
* **Целостность заголовка:** Механизм маскирования обеспечивает только скрытность, а не целостность. Целостность заголовков математически гарантируется 16-байтным тегом аутентификации Poly1305, который покрывает 12-байтный заголовок как присоединенные данные (AAD).

BIN
icons/circle.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

BIN
icons/logo_new.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 769 KiB

BIN
icons/sqare.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 183 KiB

15
icons/sqare.svg Normal file
View File

@ -0,0 +1,15 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
<defs>
<linearGradient id="g2" x1="0%" y1="0%" x2="100%" y2="100%">
<stop offset="0%" stop-color="#111827" />
<stop offset="100%" stop-color="#374151" />
</linearGradient>
<linearGradient id="g2_path" x1="0%" y1="0%" x2="100%" y2="100%">
<stop offset="0%" stop-color="#3B82F6" />
<stop offset="100%" stop-color="#14B8A6" />
</linearGradient>
</defs>
<rect width="512" height="512" rx="120" fill="url(#g2)" />
<path d="M144 256c0-61.9 50.1-112 112-112s112 50.1 112 112-50.1 112-112 112S144 317.9 144 256zm-48 0c0 88.4 71.6 160 160 160s160-71.6 160-160S344.4 96 256 96 96 167.6 96 256z" fill="url(#g2_path)"/>
<circle cx="256" cy="256" r="40" fill="#F59E0B" />
</svg>

After

Width:  |  Height:  |  Size: 779 B

View File

@ -9,10 +9,24 @@ anyhow.workspace = true
bytes.workspace = true bytes.workspace = true
tokio.workspace = true tokio.workspace = true
tracing.workspace = true tracing.workspace = true
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
tracing-appender = "0.2"
ostp-core = { path = "../ostp-core" } ostp-core = { path = "../ostp-core" }
ostp-tun = { path = "../ostp-tun" }
rand.workspace = true rand.workspace = true
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
json_comments = "0.2"
[target.'cfg(target_os = "windows")'.dependencies] portable-atomic.workspace = true
wintun = "0.4.0" chrono = "0.4"
socket2 = "0.6.3"
futures-util = "0.3.32"
hmac = "0.12.1"
sha2 = "0.10.8"
base64 = "0.22.1"
webpki-roots = "0.26"
tun = { version = "0.8.9", features = ["async"] }
netstack-smoltcp = "0.2.2"
futures = "0.3.32"
libc = "0.2.186"
winapi = { version = "0.3.9", features = ["iphlpapi", "tcpmib", "processthreadsapi", "psapi", "handleapi", "winerror", "minwindef", "winnt", "iptypes", "ws2def"] }

View File

@ -40,6 +40,9 @@ pub enum BridgeCommand {
ToggleTunnel, ToggleTunnel,
NextProfile, NextProfile,
ReloadConfig, ReloadConfig,
/// Triggered by Android NetworkCallback when the active network changes (WiFi→LTE, etc.).
/// Causes an immediate background reconnect without waiting for stall detection.
NetworkChanged,
Shutdown, Shutdown,
} }
@ -54,6 +57,12 @@ pub struct AppState {
pub log_scroll: u16, pub log_scroll: u16,
} }
impl Default for AppState {
fn default() -> Self {
Self::new()
}
}
impl AppState { impl AppState {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {

View File

@ -0,0 +1,21 @@
fn main() {
let socket = std::net::UdpSocket::bind("0.0.0.0:0").unwrap();
let port = socket.local_addr().unwrap().port();
println!("Bound UDP to port {}", port);
if let Some(name) = ostp_client::tunnel::process_lookup::get_process_name_from_port_udp(port) {
println!("Found process for UDP port {}: {}", port, name);
} else {
println!("Process not found for UDP port {}", port);
}
let tcp_socket = std::net::TcpListener::bind("0.0.0.0:0").unwrap();
let tcp_port = tcp_socket.local_addr().unwrap().port();
println!("Bound TCP to port {}", tcp_port);
if let Some(name) = ostp_client::tunnel::process_lookup::get_process_name_from_port(tcp_port) {
println!("Found process for TCP port {}: {}", tcp_port, name);
} else {
println!("Process not found for TCP port {}", tcp_port);
}
}

File diff suppressed because it is too large Load Diff

View File

@ -12,13 +12,23 @@ pub struct ClientConfig {
pub debug: bool, pub debug: bool,
pub ostp: OstpConfig, pub ostp: OstpConfig,
pub local_proxy: LocalProxyConfig, pub local_proxy: LocalProxyConfig,
pub turn: TurnConfig, #[serde(default)]
pub transport: TransportConfig,
#[serde(default)] #[serde(default)]
pub exclusions: ExclusionConfig, pub exclusions: ExclusionConfig,
#[serde(default)] #[serde(default)]
pub multiplex: MultiplexConfig, pub multiplex: MultiplexConfig,
pub dns_server: Option<String>,
#[serde(default = "default_tun_stack")]
pub tun_stack: String,
#[serde(default)]
pub kill_switch: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub gui: Option<serde_json::Value>,
} }
fn default_tun_stack() -> String { "system".to_string() }
#[derive(Debug, Clone, Serialize, Deserialize, Default)] #[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ExclusionConfig { pub struct ExclusionConfig {
#[serde(default)] #[serde(default)]
@ -43,30 +53,80 @@ pub struct OstpConfig {
pub access_key: String, pub access_key: String,
pub handshake_timeout_ms: u64, pub handshake_timeout_ms: u64,
pub io_timeout_ms: u64, pub io_timeout_ms: u64,
#[serde(default = "default_mtu")]
pub mtu: usize,
#[serde(default = "default_keepalive")]
pub keepalive_interval_sec: u64,
} }
fn default_keepalive() -> u64 { 5 }
fn default_mtu() -> usize { 1140 }
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LocalProxyConfig { pub struct LocalProxyConfig {
pub bind_addr: String, pub bind_addr: String,
pub connect_timeout_ms: u64, pub connect_timeout_ms: u64,
} }
/// Transport layer configuration.
/// `mode` = "udp" (default) or "uot" (UDP over TCP, no protocol mimicry —
/// zapret-like: no recognizable header at all, not a fake TLS/HTTP shell).
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TurnConfig { pub struct TransportConfig {
pub enabled: bool, /// "udp" or "uot"
pub server_addr: String, #[serde(default = "default_transport_mode")]
pub username: String, pub mode: String,
pub access_key: String, /// Split the first UoT/TCP packet (handshake) into tiny TCP segments to
/// break DPI that inspects the first packet. UoT/TCP only; ignored for UDP.
pub tcp_fragmentation: bool,
/// TCP chunk size (bytes)
#[serde(default = "default_frag_chunk")]
pub frag_chunk: usize,
/// TCP sleep duration between chunks (ms)
#[serde(default = "default_frag_sleep")]
pub frag_sleep: u64,
/// [min, max] junk packet count
#[serde(default = "default_junk_count")]
pub junk_pc: [usize; 2],
/// [min, max] junk packet size in bytes
#[serde(default = "default_junk_size")]
pub junk_ps: [usize; 2],
} }
fn default_transport_mode() -> String { "udp".to_string() }
fn default_frag_chunk() -> usize { 2 }
fn default_frag_sleep() -> u64 { 2 }
fn default_junk_count() -> [usize; 2] { [2, 5] }
fn default_junk_size() -> [usize; 2] { [100, 1000] }
impl Default for TransportConfig {
fn default() -> Self {
Self {
mode: default_transport_mode(),
tcp_fragmentation: false,
frag_chunk: default_frag_chunk(),
frag_sleep: default_frag_sleep(),
junk_pc: default_junk_count(),
junk_ps: default_junk_size(),
}
}
}
impl Default for OstpConfig { impl Default for OstpConfig {
fn default() -> Self { fn default() -> Self {
Self { Self {
server_addr: "127.0.0.1:50000".to_string(), server_addr: "127.0.0.1:50000".to_string(),
local_bind_addr: "0.0.0.0:0".to_string(), local_bind_addr: "0.0.0.0:0".to_string(),
access_key: String::new(), access_key: String::new(),
handshake_timeout_ms: 10000, handshake_timeout_ms: 5000,
io_timeout_ms: 2500, io_timeout_ms: 2500,
mtu: default_mtu(),
keepalive_interval_sec: default_keepalive(),
} }
} }
} }
@ -80,16 +140,6 @@ impl Default for LocalProxyConfig {
} }
} }
impl Default for TurnConfig {
fn default() -> Self {
Self {
enabled: false,
server_addr: String::new(),
username: String::new(),
access_key: String::new(),
}
}
}
impl Default for ClientConfig { impl Default for ClientConfig {
fn default() -> Self { fn default() -> Self {
@ -98,9 +148,13 @@ impl Default for ClientConfig {
debug: false, debug: false,
ostp: OstpConfig::default(), ostp: OstpConfig::default(),
local_proxy: LocalProxyConfig::default(), local_proxy: LocalProxyConfig::default(),
turn: TurnConfig::default(), transport: TransportConfig::default(),
exclusions: ExclusionConfig::default(), exclusions: ExclusionConfig::default(),
multiplex: MultiplexConfig::default(), multiplex: MultiplexConfig::default(),
dns_server: None,
tun_stack: "system".to_string(),
kill_switch: false,
gui: None,
} }
} }
} }
@ -123,15 +177,31 @@ struct RawUnifiedConfig {
debug: Option<bool>, debug: Option<bool>,
server: Option<String>, server: Option<String>,
access_key: Option<String>, access_key: Option<String>,
mtu: Option<usize>,
socks5_bind: Option<String>, socks5_bind: Option<String>,
tun: Option<RawTunSection>, tun: Option<RawTunSection>,
exclude: Option<RawExcludeSection>, exclude: Option<RawExcludeSection>,
mux: Option<RawMuxSection>, mux: Option<RawMuxSection>,
transport: Option<RawTransportSection>,
gui: Option<serde_json::Value>,
}
#[derive(Debug, Deserialize)]
struct RawTransportSection {
mode: Option<String>,
tcp_fragmentation: Option<bool>,
frag_chunk: Option<usize>,
frag_sleep: Option<u64>,
junk_pc: Option<[usize; 2]>,
junk_ps: Option<[usize; 2]>,
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
struct RawTunSection { struct RawTunSection {
enable: Option<bool>, enable: Option<bool>,
dns: Option<String>,
stack: Option<String>,
kill_switch: Option<bool>,
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
@ -147,6 +217,8 @@ struct RawMuxSection {
sessions: Option<usize>, sessions: Option<usize>,
} }
impl ClientConfig { impl ClientConfig {
/// Hot-reload from `config.json` placed next to the running binary. /// Hot-reload from `config.json` placed next to the running binary.
/// Returns a new `ClientConfig` built from the unified JSON format. /// Returns a new `ClientConfig` built from the unified JSON format.
@ -157,12 +229,14 @@ impl ClientConfig {
let raw = std::fs::read_to_string(&path) let raw = std::fs::read_to_string(&path)
.with_context(|| format!("failed to read {}", path.display()))?; .with_context(|| format!("failed to read {}", path.display()))?;
let raw: RawUnifiedConfig = serde_json::from_str(&raw) let mut stripped = json_comments::StripComments::new(raw.as_bytes());
let raw: RawUnifiedConfig = serde_json::from_reader(&mut stripped)
.with_context(|| format!("failed to parse {}", path.display()))?; .with_context(|| format!("failed to parse {}", path.display()))?;
let is_tun = raw.tun.as_ref().and_then(|t| t.enable).unwrap_or(false); let is_tun = raw.tun.as_ref().and_then(|t| t.enable).unwrap_or(false);
let server = raw.server.unwrap_or_else(|| "127.0.0.1:50000".to_string()); let server = raw.server.unwrap_or_else(|| "127.0.0.1:50000".to_string());
let key = raw.access_key.unwrap_or_default(); let key = raw.access_key.unwrap_or_default();
let mtu = raw.mtu.unwrap_or(default_mtu());
let socks5 = raw.socks5_bind.unwrap_or_else(|| "127.0.0.1:1088".to_string()); let socks5 = raw.socks5_bind.unwrap_or_else(|| "127.0.0.1:1088".to_string());
let exclusions = raw.exclude.unwrap_or(RawExcludeSection { let exclusions = raw.exclude.unwrap_or(RawExcludeSection {
domains: None, domains: None,
@ -181,14 +255,23 @@ impl ClientConfig {
server_addr: server, server_addr: server,
local_bind_addr: "0.0.0.0:0".to_string(), local_bind_addr: "0.0.0.0:0".to_string(),
access_key: key, access_key: key,
handshake_timeout_ms: 10000, handshake_timeout_ms: 5000,
io_timeout_ms: 2500, io_timeout_ms: 2500,
mtu,
keepalive_interval_sec: default_keepalive(),
}, },
local_proxy: LocalProxyConfig { local_proxy: LocalProxyConfig {
bind_addr: socks5, bind_addr: socks5,
connect_timeout_ms: 15000, connect_timeout_ms: 15000,
}, },
turn: TurnConfig::default(), transport: TransportConfig {
mode: raw.transport.as_ref().and_then(|t| t.mode.clone()).unwrap_or_else(default_transport_mode),
tcp_fragmentation: raw.transport.as_ref().and_then(|t| t.tcp_fragmentation).unwrap_or(false),
frag_chunk: raw.transport.as_ref().and_then(|t| t.frag_chunk).unwrap_or_else(default_frag_chunk),
frag_sleep: raw.transport.as_ref().and_then(|t| t.frag_sleep).unwrap_or_else(default_frag_sleep),
junk_pc: raw.transport.as_ref().and_then(|t| t.junk_pc).unwrap_or_else(default_junk_count),
junk_ps: raw.transport.as_ref().and_then(|t| t.junk_ps).unwrap_or_else(default_junk_size),
},
exclusions: ExclusionConfig { exclusions: ExclusionConfig {
domains: exclusions.domains.unwrap_or_default(), domains: exclusions.domains.unwrap_or_default(),
ips: exclusions.ips.unwrap_or_default(), ips: exclusions.ips.unwrap_or_default(),
@ -198,6 +281,258 @@ impl ClientConfig {
enabled: mux.enabled.unwrap_or(false), enabled: mux.enabled.unwrap_or(false),
sessions: mux.sessions.unwrap_or(1), sessions: mux.sessions.unwrap_or(1),
}, },
dns_server: raw.tun.as_ref().and_then(|t| t.dns.clone()),
tun_stack: raw.tun.as_ref().and_then(|t| t.stack.clone()).unwrap_or_else(|| "system".to_string()),
kill_switch: raw.tun.as_ref().and_then(|t| t.kill_switch).unwrap_or(false),
gui: raw.gui,
}) })
} }
} }
// ═══════════════════════════════════════════════════════════════════════
// On-disk config.json shapes — client, server, and relay.
//
// This is the ONE place these are defined. They used to be declared locally
// inside ostp/src/main.rs (the CLI binary) with no other consumer able to
// see them, which is exactly how ostp-client::migrate ended up working
// against loosely-typed serde_json::Value instead of a real schema, and how
// the CLI, the migrator, and this crate's own hot-reload path could each
// silently drift out of sync with what a config.json actually looks like.
// main.rs now imports these instead of re-declaring them (see the `use
// ostp_client::config::{...}` at its top).
//
// These are DELIBERATELY separate from ClientConfig/OstpConfig/etc. above:
// this section is the friendly, minimal shape a user actually edits by
// hand; the types above are what the running engine needs internally
// (handshake/io timeouts, resolved addresses, ...) and are built FROM one
// of these via the mapping in ostp/src/main.rs::run_client_directly. Only
// `ClientConfig` collides by name with the runtime type above, so the
// on-disk one is `ClientFileConfig` — everything else keeps its natural name.
// ═══════════════════════════════════════════════════════════════════════
#[derive(Debug, Deserialize, Serialize)]
#[serde(tag = "mode", rename_all = "lowercase")]
pub enum AppMode {
Server(ServerConfig),
Client(ClientFileConfig),
Relay(RelayServerConfig),
}
#[derive(Debug, Deserialize, Serialize)]
pub struct UnifiedConfig {
#[serde(flatten)]
pub mode: AppMode,
pub log_level: Option<String>,
}
impl UnifiedConfig {
pub fn validate(&self) -> Result<()> {
match &self.mode {
AppMode::Server(cfg) => {
if cfg.access_keys.is_empty() {
anyhow::bail!("Server configuration must contain at least one access_key.");
}
if let Some(outbound) = &cfg.outbound {
if outbound.enabled {
let action = outbound.default_action.as_deref().unwrap_or("direct");
if action == "direct" && outbound.rules.is_empty() {
println!("\n[WARNING] Server outbound proxy is ENABLED, but default_action is 'direct' and there are no rules!");
println!(" This means ALL traffic will bypass the proxy and go out directly from the server IP.");
println!(" If you want all traffic to be proxied, change 'default_action' to 'proxy'.\n");
}
}
}
}
AppMode::Client(cfg) => {
if cfg.access_key.is_empty() {
anyhow::bail!("Client configuration must contain an access_key.");
}
}
AppMode::Relay(cfg) => {
if cfg.upstream_tcp.is_empty() {
anyhow::bail!("Relay configuration must specify upstream_tcp address.");
}
if cfg.upstream_api_url.is_empty() {
anyhow::bail!("Relay configuration must specify upstream_api_url.");
}
}
}
Ok(())
}
}
#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(untagged)]
pub enum UserConfig {
Detailed {
access_key: String,
name: Option<String>,
limit_bytes: Option<u64>,
},
KeyOnly(String),
}
impl UserConfig {
pub fn key(&self) -> String {
match self {
UserConfig::KeyOnly(k) => k.clone(),
UserConfig::Detailed { access_key, .. } => access_key.clone(),
}
}
pub fn name(&self) -> Option<String> {
match self {
UserConfig::KeyOnly(_) => None,
UserConfig::Detailed { name, .. } => name.clone(),
}
}
pub fn limit(&self) -> Option<u64> {
match self {
UserConfig::KeyOnly(_) => None,
UserConfig::Detailed { limit_bytes, .. } => *limit_bytes,
}
}
}
#[derive(Debug, Deserialize, Serialize)]
pub struct ServerConfig {
pub listen: ListenConfig,
pub access_keys: Vec<UserConfig>,
pub debug: Option<bool>,
pub outbound: Option<OutboundConfig>,
pub api: Option<ApiConfig>,
pub fallback: Option<FallbackCfg>,
pub transport: Option<TransportConfigRaw>,
// Left untyped: ostp-client does not (and should not) depend on
// ostp-server just to name its DnsConfig type. The CLI binary — which
// already depends on both crates — deserializes this into
// ostp_server::dns::DnsConfig right before handing it to run_server().
pub dns: Option<serde_json::Value>,
}
/// Relay-node config.json shape.
#[derive(Debug, Deserialize, Serialize)]
pub struct RelayServerConfig {
/// Listen address(es) (UDP + TCP UoT)
pub listen: ListenConfig,
/// Upstream address for TCP (UoT) traffic
pub upstream_tcp: String,
/// Upstream address for UDP traffic
pub upstream_udp: String,
// ── Deprecated ──────────────────────────────────────────────────────────
// The relay used to authenticate clients itself and pulled the access-key
// list from the target server's management API to do it. It no longer does:
// sessions are authenticated end-to-end by the target server, and a relay
// that re-checks credentials only adds a weaker second gate plus a copy of
// the key list on a machine that does not need one. These are kept solely
// so existing relay configs still parse; they are ignored.
#[serde(default)]
pub upstream_api_url: String,
#[serde(default)]
pub upstream_api_token: String,
#[serde(default)]
pub sync_interval_secs: u64,
pub debug: Option<bool>,
}
/// Supports both a single string "0.0.0.0:50000" and an array
/// ["0.0.0.0:50000", "[::]:50000"].
#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(untagged)]
pub enum ListenConfig {
Single(String),
Multiple(Vec<String>),
}
impl ListenConfig {
pub fn addresses(&self) -> Vec<String> {
match self {
ListenConfig::Single(s) => vec![s.clone()],
ListenConfig::Multiple(v) => v.clone(),
}
}
pub fn primary(&self) -> String {
match self {
ListenConfig::Single(s) => s.clone(),
ListenConfig::Multiple(v) => v.first().cloned().unwrap_or_default(),
}
}
}
#[derive(Debug, Deserialize, Serialize)]
pub struct ApiConfig {
pub enabled: Option<bool>,
pub bind: Option<String>,
pub token: Option<String>,
pub webpath: Option<String>,
pub username: Option<String>,
pub password_hash: Option<String>,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct FallbackCfg {
pub enabled: Option<bool>,
pub listen: Option<String>,
pub target: Option<String>,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct ClientFileConfig {
pub server: String,
pub access_key: String,
pub mtu: Option<usize>,
pub socks5_bind: Option<String>,
pub tun: Option<TunConfig>,
pub debug: Option<bool>,
pub exclude: Option<ExcludeConfig>,
pub mux: Option<MuxConfig>,
pub transport: Option<TransportConfigRaw>,
pub gui: Option<serde_json::Value>,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct TransportConfigRaw {
pub mode: Option<String>,
pub tcp_fragmentation: Option<bool>,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct TunConfig {
pub enable: bool,
pub wintun_path: Option<String>,
pub ipv4_address: Option<String>,
pub dns: Option<String>,
pub kill_switch: Option<bool>,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct OutboundConfig {
pub enabled: bool,
pub protocol: String,
pub address: String,
pub port: u16,
#[serde(default)]
pub rules: Vec<OutboundRule>,
pub default_action: Option<String>,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct OutboundRule {
pub domain_suffix: Option<Vec<String>>,
pub ip_cidr: Option<Vec<String>>,
pub protocol: Option<String>,
pub action: Option<String>,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct ExcludeConfig {
pub domains: Option<Vec<String>>,
pub ips: Option<Vec<String>>,
pub processes: Option<Vec<String>>,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct MuxConfig {
pub enabled: Option<bool>,
pub sessions: Option<usize>,
}

View File

@ -1,7 +1,12 @@
pub mod app; pub mod app;
pub mod bridge; pub mod bridge;
pub mod config; pub mod config;
pub mod migrate;
pub mod signal; pub mod signal;
pub mod sysproxy; pub mod sysproxy;
pub mod transport;
pub mod tunnel; pub mod tunnel;
pub mod runner; pub mod runner;
pub mod logging;

189
ostp-client/src/logging.rs Normal file
View File

@ -0,0 +1,189 @@
use std::fs::OpenOptions;
use std::io::Write;
use std::path::PathBuf;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};
/// The single canonical log file for the whole core. Every process (CLI daemon,
/// GUI, TUN helper) and every subsystem (tracing, the core event logger, the
/// helper IPC, panics) writes here — no more per-binary / per-subsystem sprawl
/// (`ostp-cli.log` + `ostp-core.log` + `ostp-helper.log` + `ostp-crash.log`).
pub const LOG_FILE_NAME: &str = "ostp.log";
/// Absolute path to the shared log file, next to the running executable.
pub fn log_file_path() -> PathBuf {
std::env::current_exe()
.ok()
.and_then(|p| p.parent().map(|d| d.join(LOG_FILE_NAME)))
.unwrap_or_else(|| PathBuf::from(LOG_FILE_NAME))
}
/// True if this invocation is the long-running daemon (a client/server run),
/// as opposed to a one-shot subcommand (`gk`, `check`, `init`, `-V`, ...).
///
/// Used to gate log truncation: only the daemon clears the log at startup, so a
/// one-shot command run while a daemon is live can never wipe the daemon's log.
/// A daemon invocation is simply one that carries none of the one-shot tokens
/// (`ostp`, `ostp run`, `ostp connect <url>` → daemon; everything else → one-shot).
pub fn invocation_is_daemon<I: IntoIterator<Item = String>>(args: I) -> bool {
const ONE_SHOT: &[&str] = &[
"gk", "generate-key", "check", "init", "setup", "links", "import",
"update", "migrate", "prober", "proxy-env", "proxy-env-clear",
"uninstall", "-V", "--version", "-h", "--help", "help",
];
!args
.into_iter()
.skip(1) // program name
.any(|a| ONE_SHOT.contains(&a.as_str()))
}
/// Append a single timestamped line to the shared log file. Used by the manual
/// writers (core event logger, TUN helper IPC) so their output lands in the same
/// `ostp.log` as the tracing subscriber instead of a separate file.
pub fn append_line(msg: &str) {
if let Ok(mut file) = OpenOptions::new().create(true).append(true).open(log_file_path()) {
let _ = writeln!(
file,
"[{}] {}",
chrono::Local::now().format("%Y-%m-%d %H:%M:%S"),
msg
);
}
}
pub fn setup_panic_hook() {
std::panic::set_hook(Box::new(|info| {
let payload = info.payload();
let msg = if let Some(s) = payload.downcast_ref::<&str>() {
*s
} else if let Some(s) = payload.downcast_ref::<String>() {
s.as_str()
} else {
"Box<dyn Any>"
};
let location = info.location().unwrap_or_else(|| std::panic::Location::caller());
let backtrace = std::backtrace::Backtrace::force_capture();
let crash_msg = format!(
"[{}] PANIC at {}:{}\nMessage: {}\nBacktrace:\n{:?}",
chrono::Local::now().format("%Y-%m-%d %H:%M:%S"),
location.file(),
location.line(),
msg,
backtrace
);
eprintln!("{}", crash_msg);
tracing::error!("{}", crash_msg);
// Crashes land in the same shared log file (append — a crash must never
// truncate, and the tracing worker may already be dead so we write direct).
if let Ok(mut file) = OpenOptions::new().create(true).append(true).open(log_file_path()) {
let _ = file.write_all(crash_msg.as_bytes());
let _ = file.write_all(b"\n===================================================\n");
}
}));
}
/// Initialises tracing and writes to the shared `ostp.log` next to the executable.
///
/// The `level` parameter controls the minimum log level:
/// - `"error"` — only errors
/// - `"warn"` — warnings and errors
/// - `"info"` — informational messages (default)
/// - `"debug"` — detailed debug messages (use when `debug: true` in config)
/// - `"trace"` — all messages including very verbose internal state
///
/// The environment variable `RUST_LOG` overrides this value if set.
///
/// `truncate`: clear the log at startup. Honoured **only on Windows** — Linux
/// servers keep their history (OS-rotated). Pass `true` only from the daemon's
/// own entrypoint; one-shot commands and child processes (the TUN helper) pass
/// `false` so they append instead of wiping a running daemon's log.
pub fn init_tracing(
level: &str,
app_name: &str,
version: &str,
truncate: bool,
) -> Option<tracing_appender::non_blocking::WorkerGuard> {
// RUST_LOG overrides the config-derived level
let env_filter = EnvFilter::try_from_default_env()
.unwrap_or_else(|_| {
// When debug or trace is requested, enable for all ostp crates
if level == "debug" || level == "trace" {
// Enable the requested level for ostp crates, but keep noisy deps at warn
EnvFilter::new(format!(
"warn,ostp_client={level},ostp_core={level},ostp_jni={level},ostp_gui_lib={level}"
))
} else {
EnvFilter::new(level)
}
});
let path = log_file_path();
let mut open_opts = OpenOptions::new();
open_opts.create(true);
// Truncate-on-startup is Windows-only and daemon-only. Everywhere else append:
// Linux keeps server history, and one-shot commands / the TUN helper must not
// wipe a running daemon's log.
if truncate && cfg!(windows) {
open_opts.write(true).truncate(true);
} else {
open_opts.append(true);
}
if let Ok(mut file) = open_opts.open(&path) {
// Write the startup banner directly to the log file, bypassing the
// tracing subscriber entirely. Emitting it via tracing::info!() hits
// BOTH layers below (file AND stderr), so every one-shot CLI command
// (`ostp -V`, `ostp gk`, `ostp check`, ...) printed this banner to the
// terminal on every single invocation — pure noise for anything that
// isn't the long-running daemon. It's still genuinely useful for
// whoever's reading the log file later, so keep it there, just not on
// screen for commands that aren't the daemon.
let _ = writeln!(
file,
"{} v{} | OS: {} | Arch: {} | log_level: {} | log_file: {}",
app_name,
version,
std::env::consts::OS,
std::env::consts::ARCH,
level,
path.display(),
);
let (file_writer, guard) = tracing_appender::non_blocking(file);
let fmt_layer = tracing_subscriber::fmt::layer()
.with_target(true)
.with_line_number(true)
.with_thread_ids(false)
.with_thread_names(false)
.with_ansi(false)
.with_writer(file_writer);
let stderr_layer = tracing_subscriber::fmt::layer()
.with_target(true)
.with_writer(std::io::stderr);
let _ = tracing_subscriber::registry()
.with(env_filter)
.with(fmt_layer)
.with(stderr_layer)
.try_init();
Some(guard)
} else {
// Fallback: stderr only
let stderr_layer = tracing_subscriber::fmt::layer()
.with_target(true)
.with_writer(std::io::stderr);
let _ = tracing_subscriber::registry()
.with(EnvFilter::new(level))
.with(stderr_layer)
.try_init();
eprintln!("[WARN] Could not open log file at {}. Logging to stderr only.", path.display());
None
}
}

559
ostp-client/src/migrate.rs Normal file
View File

@ -0,0 +1,559 @@
//! The ONE authoritative place that upgrades an old `config.json` to the
//! current schema. Reachable only via the explicit `ostp migrate` command —
//! nothing else in this codebase silently rewrites a user's config on their
//! behalf (the old 0.3.x line used to auto-migrate on every load with just a
//! log warning; that's exactly the kind of "invisible until something looks
//! wrong" behavior this module replaces).
//!
//! Every field this module cannot map forward is reported explicitly in
//! `MigrationReport.notes`, never silently dropped without a trace.
use serde_json::{json, Value};
#[derive(Debug, Default)]
pub struct MigrationReport {
/// Whether anything was actually different from the current schema.
pub changed: bool,
/// Human-readable line per field added, converted, or dropped.
pub notes: Vec<String>,
}
impl MigrationReport {
fn note(&mut self, msg: impl Into<String>) {
self.changed = true;
self.notes.push(msg.into());
}
}
/// Which config this file is (mirrors `AppMode`'s `"mode"` tag). Old configs
/// from before that tag existed are sniffed structurally as a fallback.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConfigKind {
Client,
Server,
Relay,
}
pub fn detect_kind(json: &Value) -> Option<ConfigKind> {
match json.get("mode").and_then(|v| v.as_str()) {
Some("client") => return Some(ConfigKind::Client),
Some("server") => return Some(ConfigKind::Server),
Some("relay") => return Some(ConfigKind::Relay),
_ => {}
}
// No (or unrecognized) "mode" tag — this is an older config from before
// it was mandatory. Sniff by the fields that have been present on each
// shape since the earliest surviving config format.
if json.get("upstream_tcp").is_some() || json.get("upstream_api_url").is_some() {
Some(ConfigKind::Relay)
} else if json.get("access_keys").is_some() || json.get("listen").is_some() {
Some(ConfigKind::Server)
} else if json.get("access_key").is_some() || json.get("server").is_some() {
Some(ConfigKind::Client)
} else {
None
}
}
/// Migrates a client config of any known past shape to the current flat
/// schema. Returns the migrated JSON and a report of every change made.
///
/// Known input shapes, oldest first:
/// - **v0.3.1v0.3.21 "modular multi-server"**: `inbounds`/`outbounds` arrays
/// + `routing.rules`. Only the first `ostp`-type outbound is kept (this
/// line no longer supports multiple simultaneous servers); every other
/// `ostp` outbound is reported by tag+address so nothing vanishes
/// invisibly. `urltest`/`selector`/`direct`/`block` outbounds have no
/// equivalent and are dropped (reported).
/// - **pre-0.3.1 flat (up to v0.2.98)**: same field names as today
/// (`server`, `access_key`, `tun`, `exclude`, `mux`, `transport`, ...)
/// except `tun.wintun_path`/`tun.ipv4_address` (internal driver detail,
/// never user-meaningful data) and `transport.wss` (the WSS framing
/// feature removed entirely in the 0.4.0 rebuild) — both dropped with an
/// explicit note; everything else maps 1:1, nothing to convert.
/// - **configs carrying a leftover `transport.stealth_sni`**: dropped with a
/// note, same reasoning as `wss` — it never fed into anything on the wire
/// (no TLS/HTTP mimicry exists in this project), so there is no successor
/// field. Not tied to a specific version: it lingered in the schema well
/// past when the mimicry work it was meant for got removed.
/// - **current flat schema**: no-op, `changed = false`.
pub fn migrate_client_json(json: Value) -> (Value, MigrationReport) {
let mut report = MigrationReport::default();
let has_inbounds = json.get("inbounds").and_then(|v| v.as_array()).is_some();
let has_outbounds = json.get("outbounds").and_then(|v| v.as_array()).is_some();
if has_inbounds && has_outbounds {
return migrate_client_from_modular(json, report);
}
// Flat shape already (current or pre-0.3.1) — normalize obsolete fields
// in place rather than rebuilding the whole document from scratch, so
// any field this module doesn't know about yet still survives untouched.
let mut out = json;
if let Some(tun) = out.get_mut("tun").and_then(|t| t.as_object_mut()) {
for dead_field in ["wintun_path", "ipv4_address"] {
if tun.remove(dead_field).is_some() {
report.note(format!(
"Dropped tun.{dead_field} — internal driver detail from an older WinTun \
integration, not applicable to the current TUN implementation."
));
}
}
}
if let Some(transport) = out.get_mut("transport").and_then(|t| t.as_object_mut()) {
if transport.remove("wss").is_some() {
report.note(
"Dropped transport.wss — WSS framing was removed in the 0.4.0 rebuild \
(the project follows a zapret-like approach: no protocol mimicry, \
just packet-level obfuscation/manipulation, so there is no successor field)."
.to_string(),
);
}
if transport.remove("stealth_sni").is_some() {
report.note(
"Dropped transport.stealth_sni — never actually used to construct any wire \
bytes (no TLS/HTTP mimicry exists in this project same zapret-like \
reasoning as transport.wss), so it was unused config plumbing with no effect."
.to_string(),
);
}
}
(out, report)
}
fn migrate_client_from_modular(json: Value, mut report: MigrationReport) -> (Value, MigrationReport) {
report.changed = true; // the shape itself is being replaced regardless of field-level detail
let inbounds = json.get("inbounds").and_then(|v| v.as_array()).cloned().unwrap_or_default();
let outbounds = json.get("outbounds").and_then(|v| v.as_array()).cloned().unwrap_or_default();
let routing = json.get("routing").cloned().unwrap_or(json!({}));
let default_outbound = routing.get("default_outbound").and_then(|v| v.as_str()).map(String::from);
// ── Pick the primary "ostp" outbound ────────────────────────────────
// Prefer the one routing.default_outbound points at (directly, or via a
// urltest/selector group that references it); otherwise take the first
// ostp outbound in file order. Every other ostp outbound is reported by
// tag+address, not silently discarded.
let ostp_outbounds: Vec<&Value> = outbounds
.iter()
.filter(|o| o.get("type").and_then(|t| t.as_str()) == Some("ostp"))
.collect();
// default_outbound might name an ostp outbound directly, OR name a
// urltest/selector GROUP whose first member is the one to actually use —
// check both, since a plain `.or_else` here would never even attempt the
// group lookup while default_outbound is Some(_) (which it almost always
// is), silently falling through to "just take the first ostp outbound in
// file order" instead — exactly the kind of silent wrong answer this
// migrator exists to avoid.
let primary_tag: Option<String> = default_outbound.as_deref().and_then(|def_tag| {
if ostp_outbounds.iter().any(|o| o.get("tag").and_then(|t| t.as_str()) == Some(def_tag)) {
return Some(def_tag.to_string());
}
outbounds.iter().find_map(|o| {
let is_group = matches!(o.get("type").and_then(|t| t.as_str()), Some("urltest") | Some("selector"));
let tag_matches = o.get("tag").and_then(|t| t.as_str()) == Some(def_tag);
if is_group && tag_matches {
o.get("outbounds")
.and_then(|v| v.as_array())
.and_then(|arr| arr.first())
.and_then(|v| v.as_str())
.map(String::from)
} else {
None
}
})
});
let primary = primary_tag
.as_deref()
.and_then(|tag| ostp_outbounds.iter().find(|o| o.get("tag").and_then(|t| t.as_str()) == Some(tag)))
.copied()
.or_else(|| ostp_outbounds.first().copied());
let Some(primary) = primary else {
report.note(
"No 'ostp'-type outbound found in the old modular config — nothing to migrate \
the server connection from. Wrote a placeholder; you MUST fill in server/access_key \
by hand or re-import a share link."
.to_string(),
);
return (
json!({
"server": "127.0.0.1:50000",
"access_key": "",
}),
report,
);
};
for other in &ostp_outbounds {
if !std::ptr::eq(*other, primary) {
let tag = other.get("tag").and_then(|t| t.as_str()).unwrap_or("?");
let addr = other.get("server").and_then(|t| t.as_str()).unwrap_or("?");
let port = other.get("port").and_then(|t| t.as_u64()).unwrap_or(0);
report.note(format!(
"Dropped additional server '{tag}' ({addr}:{port}) — multi-server / urltest \
failover is no longer supported; only one server per config now. Kept the \
one from routing.default_outbound (or the first one if that wasn't set)."
));
}
}
let server = primary.get("server").and_then(|v| v.as_str()).unwrap_or("127.0.0.1").to_string();
let port = primary.get("port").and_then(|v| v.as_u64()).unwrap_or(50000);
let access_key = primary.get("access_key").and_then(|v| v.as_str()).unwrap_or("").to_string();
let transport_type = primary
.get("transport")
.and_then(|t| t.get("type").or_else(|| t.get("mode")))
.and_then(|v| v.as_str())
.unwrap_or("udp")
.to_string();
if let Some(sni) = primary.get("transport").and_then(|t| t.get("stealth_sni")).and_then(|v| v.as_str()) {
if !sni.is_empty() {
report.note(format!(
"Dropped transport.stealth_sni ({sni:?}) — never actually used to construct \
any wire bytes; unused config plumbing with no successor field."
));
}
}
let tcp_fragmentation = primary
.get("transport")
.and_then(|t| t.get("tcp_fragmentation"))
.and_then(|v| v.as_bool())
.unwrap_or(false);
let mux_enabled = primary.get("multiplex").and_then(|m| m.get("enabled")).and_then(|v| v.as_bool()).unwrap_or(false);
let mux_sessions = primary.get("multiplex").and_then(|m| m.get("sessions")).and_then(|v| v.as_u64()).unwrap_or(1);
// ── TUN + local proxy inbounds ───────────────────────────────────────
let tun_inbound = inbounds.iter().find(|i| i.get("type").and_then(|t| t.as_str()) == Some("tun"));
let proxy_inbound = inbounds.iter().find(|i| i.get("type").and_then(|t| t.as_str()) == Some("local_proxy"));
let tun_enable = tun_inbound.is_some();
let mtu = tun_inbound.and_then(|t| t.get("mtu")).and_then(|v| v.as_u64());
let socks5_bind = proxy_inbound
.map(|p| {
let listen = p.get("listen").and_then(|v| v.as_str()).unwrap_or("127.0.0.1");
let port = p.get("port").and_then(|v| v.as_u64()).unwrap_or(1088);
format!("{listen}:{port}")
})
.unwrap_or_else(|| "127.0.0.1:1088".to_string());
// ── Exclusions from routing.rules → direct ──────────────────────────
let mut ex_domains: Vec<String> = Vec::new();
let mut ex_ips: Vec<String> = Vec::new();
let mut ex_processes: Vec<String> = Vec::new();
if let Some(rules) = routing.get("rules").and_then(|v| v.as_array()) {
for rule in rules {
if rule.get("outbound").and_then(|v| v.as_str()) != Some("direct") {
continue; // only "route to direct" rules were ever exclusions in the old format
}
if let Some(v) = rule.get("domain_suffix").and_then(|v| v.as_array()) {
ex_domains.extend(v.iter().filter_map(|s| s.as_str().map(String::from)));
}
if let Some(v) = rule.get("ip_cidr").and_then(|v| v.as_array()) {
ex_ips.extend(v.iter().filter_map(|s| s.as_str().map(String::from)));
}
if let Some(v) = rule.get("process_name").and_then(|v| v.as_array()) {
ex_processes.extend(v.iter().filter_map(|s| s.as_str().map(String::from)));
}
}
}
for other_rule_outbound in routing
.get("rules")
.and_then(|v| v.as_array())
.into_iter()
.flatten()
.filter_map(|r| r.get("outbound").and_then(|v| v.as_str()))
.filter(|o| *o != "direct")
{
report.note(format!(
"Dropped a routing rule targeting outbound '{other_rule_outbound}' — only \
\"route to direct\" rules map to today's exclusions; anything else \
(custom per-domain outbound selection) has no equivalent anymore."
));
}
let debug = json.get("log").and_then(|l| l.get("level")).and_then(|v| v.as_str()) == Some("debug");
let mut client = json!({
"server": server,
"port": port,
"access_key": access_key,
"socks5_bind": socks5_bind,
"debug": debug,
"tun": {
"enable": tun_enable,
"dns": null,
"kill_switch": false,
},
"exclude": {
"domains": ex_domains,
"ips": ex_ips,
"processes": ex_processes,
},
"mux": {
"enabled": mux_enabled,
"sessions": mux_sessions,
},
"transport": {
"mode": transport_type,
"tcp_fragmentation": tcp_fragmentation,
},
});
if let Some(mtu) = mtu {
client["mtu"] = json!(mtu);
}
if let Some(gui) = json.get("gui") {
client["gui"] = gui.clone();
}
(client, report)
}
/// Migrates a server config. The server shape has stayed structurally
/// identical since the earliest surviving version — this only backfills the
/// `api` section (added after some configs already existed) and drops the
/// legacy `api.token` field. Ported from the ad-hoc Python snippet that used
/// to live in `scripts/install.sh` and only ran at install/update time.
pub fn migrate_server_json(json: Value) -> (Value, MigrationReport) {
let mut report = MigrationReport::default();
let mut out = json;
let obj = match out.as_object_mut() {
Some(o) => o,
None => return (out, report),
};
let api = obj.entry("api").or_insert_with(|| json!({}));
if let Some(api_obj) = api.as_object_mut() {
let defaults: [(&str, Value); 5] = [
("enabled", json!(false)),
("bind", json!("0.0.0.0:9090")),
("webpath", json!("")),
("username", json!("")),
("password_hash", json!("")),
];
for (key, default) in defaults {
if !api_obj.contains_key(key) {
report.note(format!("Added api.{key} = {default} (missing default)"));
api_obj.insert(key.to_string(), default);
}
}
if api_obj.remove("token").is_some() {
report.note(
"Dropped legacy api.token — superseded by api.password_hash; \
set a new admin password with the management API or panel."
.to_string(),
);
}
}
(out, report)
}
#[cfg(test)]
mod tests {
use super::*;
/// A realistic v0.3.21-shaped modular config (TUN + local_proxy inbounds,
/// a single ostp outbound, exclusion rules, mux) — mirrors the actual
/// shape from that tag, field for field.
#[test]
fn modular_single_server_preserves_every_field() {
let old = json!({
"version": "0.3.21",
"log": { "level": "debug" },
"inbounds": [
{ "type": "tun", "tag": "tun-in", "auto_route": true, "mtu": 1350 },
{ "type": "local_proxy", "tag": "socks-in", "protocol": "socks", "listen": "127.0.0.1", "port": 1088 }
],
"outbounds": [
{
"type": "ostp", "tag": "proxy",
"server": "203.0.113.5", "port": 50000, "access_key": "sekrit123",
"transport": { "type": "uot", "stealth_sni": "vk.com", "tcp_fragmentation": true },
"multiplex": { "enabled": true, "sessions": 4 }
},
{ "type": "direct", "tag": "direct" },
{ "type": "block", "tag": "block" }
],
"routing": {
"rules": [
{ "domain_suffix": ["local.lan", "internal.corp"], "outbound": "direct" },
{ "ip_cidr": ["192.168.0.0/16"], "outbound": "direct" },
{ "process_name": ["steam.exe"], "outbound": "direct" }
],
"default_outbound": "proxy"
}
});
let (new, report) = migrate_client_json(old);
assert!(report.changed);
assert_eq!(new["server"], "203.0.113.5");
assert_eq!(new["port"], 50000);
assert_eq!(new["access_key"], "sekrit123");
assert_eq!(new["socks5_bind"], "127.0.0.1:1088");
assert_eq!(new["mtu"], 1350);
assert_eq!(new["debug"], true);
assert_eq!(new["tun"]["enable"], true);
assert_eq!(new["transport"]["mode"], "uot");
assert_eq!(new["transport"]["tcp_fragmentation"], true);
assert_eq!(new["mux"]["enabled"], true);
assert_eq!(new["mux"]["sessions"], 4);
assert_eq!(new["exclude"]["domains"], json!(["local.lan", "internal.corp"]));
assert_eq!(new["exclude"]["ips"], json!(["192.168.0.0/16"]));
assert_eq!(new["exclude"]["processes"], json!(["steam.exe"]));
// stealth_sni never fed into any wire bytes — dropped, not carried forward.
assert!(new["transport"].get("stealth_sni").is_none());
assert!(report.notes.iter().any(|n| n.contains("stealth_sni") && n.contains("vk.com")));
}
/// Old modular configs that had MULTIPLE ostp outbounds (multi-server) —
/// must keep the one routing.default_outbound points at and report every
/// other one by name/address rather than picking silently.
#[test]
fn modular_multi_server_keeps_default_and_reports_the_rest() {
let old = json!({
"inbounds": [],
"outbounds": [
{ "type": "ostp", "tag": "proxy-0", "server": "1.1.1.1", "port": 50000, "access_key": "k1" },
{ "type": "ostp", "tag": "proxy-1", "server": "2.2.2.2", "port": 50000, "access_key": "k2" },
{
"type": "urltest", "tag": "proxy",
"outbounds": ["proxy-1", "proxy-0"], "url": "http://cp.cloudflare.com"
}
],
"routing": { "rules": [], "default_outbound": "proxy" }
});
let (new, report) = migrate_client_json(old);
// urltest's first member (proxy-1 / 2.2.2.2) is the one actually picked.
assert_eq!(new["server"], "2.2.2.2");
assert_eq!(new["access_key"], "k2");
assert!(report.notes.iter().any(|n| n.contains("proxy-0") && n.contains("1.1.1.1")));
}
/// Pre-0.3.1 flat config carrying fields that no longer exist
/// (tun.wintun_path, tun.ipv4_address, transport.wss, transport.stealth_sni)
/// — those get dropped with a note; every field that's still meaningful
/// passes through untouched, byte for byte.
#[test]
fn flat_legacy_drops_only_dead_fields() {
let old = json!({
"server": "198.51.100.9:50000",
"access_key": "oldkey",
"mtu": 1200,
"socks5_bind": "127.0.0.1:1090",
"tun": {
"enable": true,
"wintun_path": "C:\\Program Files\\wintun\\wintun.dll",
"ipv4_address": "10.0.0.2",
"dns": "1.1.1.1",
"kill_switch": true
},
"exclude": { "domains": ["a.com"], "ips": null, "processes": null },
"mux": { "enabled": false, "sessions": 1 },
"transport": { "mode": "udp", "stealth_sni": "bing.com", "wss": true }
});
let (new, report) = migrate_client_json(old);
assert!(report.changed);
// Untouched fields survive exactly as they were.
assert_eq!(new["server"], "198.51.100.9:50000");
assert_eq!(new["access_key"], "oldkey");
assert_eq!(new["mtu"], 1200);
assert_eq!(new["tun"]["enable"], true);
assert_eq!(new["tun"]["dns"], "1.1.1.1");
assert_eq!(new["tun"]["kill_switch"], true);
assert_eq!(new["exclude"]["domains"], json!(["a.com"]));
// Dead fields are gone...
assert!(new["tun"].get("wintun_path").is_none());
assert!(new["tun"].get("ipv4_address").is_none());
assert!(new["transport"].get("wss").is_none());
assert!(new["transport"].get("stealth_sni").is_none());
// ...and their removal was reported, not silent.
assert!(report.notes.iter().any(|n| n.contains("wintun_path")));
assert!(report.notes.iter().any(|n| n.contains("ipv4_address")));
assert!(report.notes.iter().any(|n| n.contains("wss")));
assert!(report.notes.iter().any(|n| n.contains("stealth_sni")));
}
/// A config already in the current shape must be a true no-op: report
/// says nothing changed, and every field is untouched.
#[test]
fn current_flat_config_is_a_no_op() {
let current = json!({
"server": "example.com:50000",
"access_key": "k",
"tun": { "enable": false, "dns": null, "kill_switch": false },
"exclude": { "domains": [], "ips": [], "processes": [] },
"mux": { "enabled": false, "sessions": 1 },
"transport": { "mode": "udp", "tcp_fragmentation": false }
});
let (new, report) = migrate_client_json(current.clone());
assert!(!report.changed);
assert_eq!(new, current);
}
/// Every migrated output must actually deserialize into the ONE
/// canonical schema (`crate::config`) — this is the same check
/// `cmd_migrate` runs at runtime before ever touching a user's file,
/// exercised here directly so a schema/migrator drift fails a fast unit
/// test instead of surfacing as "your migrated config won't load".
#[test]
fn every_migrated_output_matches_the_canonical_schema() {
let modular = json!({
"inbounds": [{ "type": "tun", "tag": "tun-in", "mtu": 1350 }],
"outbounds": [
{ "type": "ostp", "tag": "proxy", "server": "1.2.3.4", "port": 50000, "access_key": "k" },
{ "type": "direct", "tag": "direct" }
],
"routing": { "rules": [], "default_outbound": "proxy" }
});
let (new, _) = migrate_client_json(modular);
serde_json::from_value::<crate::config::ClientFileConfig>(new)
.expect("modular->flat migration output must match ClientFileConfig");
let legacy_flat = json!({
"server": "1.2.3.4:50000",
"access_key": "k",
"tun": { "enable": true, "wintun_path": "x", "ipv4_address": "y" }
});
let (new, _) = migrate_client_json(legacy_flat);
serde_json::from_value::<crate::config::ClientFileConfig>(new)
.expect("legacy-flat migration output must match ClientFileConfig");
let server = json!({ "listen": "0.0.0.0:50000", "access_keys": ["k"] });
let (new, _) = migrate_server_json(server);
serde_json::from_value::<crate::config::ServerConfig>(new)
.expect("server migration output must match ServerConfig");
}
#[test]
fn server_config_backfills_api_defaults_and_drops_legacy_token() {
let old = json!({
"listen": "0.0.0.0:50000",
"access_keys": ["k1"],
"api": { "token": "old-plain-token" }
});
let (new, report) = migrate_server_json(old);
assert!(report.changed);
assert_eq!(new["api"]["enabled"], false);
assert_eq!(new["api"]["bind"], "0.0.0.0:9090");
assert!(new["api"].get("token").is_none());
assert!(report.notes.iter().any(|n| n.contains("api.token")));
}
#[test]
fn detect_kind_falls_back_to_structural_sniffing_without_mode_tag() {
assert_eq!(detect_kind(&json!({"access_key": "x", "server": "y"})), Some(ConfigKind::Client));
assert_eq!(detect_kind(&json!({"access_keys": ["x"], "listen": "y"})), Some(ConfigKind::Server));
assert_eq!(detect_kind(&json!({"upstream_tcp": "x", "upstream_api_url": "y"})), Some(ConfigKind::Relay));
assert_eq!(detect_kind(&json!({"mode": "client", "server": "x"})), Some(ConfigKind::Client));
}
}

View File

@ -6,11 +6,28 @@ use crate::bridge::{Bridge, BridgeMetrics};
use crate::signal::wait_for_shutdown_signal; use crate::signal::wait_for_shutdown_signal;
use crate::tunnel; use crate::tunnel;
use std::sync::Arc; use std::sync::Arc;
use std::fs::OpenOptions;
use std::io::Write as _;
fn log_to_core_file(msg: &str) {
// Writes into the single shared ostp.log (same file as the tracing appender),
// not a separate ostp-core.log — see logging::LOG_FILE_NAME.
let path = crate::logging::log_file_path();
if let Ok(mut file) = OpenOptions::new().create(true).append(true).open(path) {
let _ = writeln!(file, "[{}] {}", chrono::Local::now().format("%Y-%m-%d %H:%M:%S"), msg);
}
}
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
#[link(name = "kernel32")]
extern "system" { extern "system" {
fn FreeConsole() -> i32; fn FreeConsole() -> i32;
fn GetConsoleWindow() -> *mut std::ffi::c_void; fn GetConsoleWindow() -> *mut std::ffi::c_void;
}
#[cfg(target_os = "windows")]
#[link(name = "user32")]
extern "system" {
fn ShowWindow(hwnd: *mut std::ffi::c_void, cmd_show: i32) -> i32; fn ShowWindow(hwnd: *mut std::ffi::c_void, cmd_show: i32) -> i32;
} }
@ -26,7 +43,7 @@ fn hide_console() {
} }
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
fn is_admin() -> bool { pub fn is_admin() -> bool {
std::process::Command::new("net") std::process::Command::new("net")
.arg("session") .arg("session")
.stdout(std::process::Stdio::null()) .stdout(std::process::Stdio::null())
@ -38,54 +55,271 @@ fn is_admin() -> bool {
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
fn relaunch_as_admin() -> Result<()> { fn relaunch_as_admin() -> Result<()> {
let current_exe = std::env::current_exe()?; use std::ffi::OsStr;
let exe_str = current_exe.to_string_lossy(); use std::os::windows::ffi::OsStrExt;
let _ = std::process::Command::new("powershell") use std::ptr::null_mut;
.args([
"-Command", let exe = std::env::current_exe()?;
&format!("Start-Process -FilePath '{}' -Verb RunAs", exe_str), let exe_wstr: Vec<u16> = exe.as_os_str().encode_wide().chain(Some(0)).collect();
])
.spawn()?; let mut args_joined = String::new();
for arg in std::env::args().skip(1) {
if !args_joined.is_empty() {
args_joined.push(' ');
}
args_joined.push('"');
args_joined.push_str(&arg.replace('"', "\\\""));
args_joined.push('"');
}
let args_wstr: Vec<u16> = OsStr::new(&args_joined).encode_wide().chain(Some(0)).collect();
let dir = std::env::current_dir()?;
let dir_wstr: Vec<u16> = dir.as_os_str().encode_wide().chain(Some(0)).collect();
let verb_wstr: Vec<u16> = OsStr::new("runas").encode_wide().chain(Some(0)).collect();
#[link(name = "shell32")]
extern "system" {
fn ShellExecuteW(
hwnd: *mut std::ffi::c_void,
lpOperation: *const u16,
lpFile: *const u16,
lpParameters: *const u16,
lpDirectory: *const u16,
nShowCmd: i32,
) -> isize;
}
unsafe {
let ret = ShellExecuteW(
null_mut(),
verb_wstr.as_ptr(),
exe_wstr.as_ptr(),
args_wstr.as_ptr(),
dir_wstr.as_ptr(),
1, // SW_SHOWNORMAL = 1
);
if ret <= 32 {
return Err(anyhow::anyhow!(
"Windows UAC Elevation failed or was denied by policy (ShellExecuteW code: {})",
ret
));
}
}
std::process::exit(0);
}
#[cfg(target_os = "linux")]
pub fn is_root() -> bool {
unsafe { libc::geteuid() == 0 }
}
#[cfg(target_os = "linux")]
fn relaunch_as_root() -> Result<()> {
use std::io::IsTerminal;
let exe = std::env::current_exe()?;
let args: Vec<String> = std::env::args().skip(1).collect();
let is_gui = std::env::var("DISPLAY").is_ok() || std::env::var("WAYLAND_DISPLAY").is_ok();
let is_term = std::io::stdout().is_terminal();
let mut cmd = if is_gui && !is_term {
let mut c = std::process::Command::new("pkexec");
c.arg(exe);
c
} else {
let mut c = std::process::Command::new("sudo");
c.arg(exe);
c
};
cmd.args(&args);
let status = cmd.status().map_err(|e| anyhow::anyhow!("Failed to execute privilege escalation command: {}", e))?;
if !status.success() {
return Err(anyhow::anyhow!("Privilege escalation failed or was denied."));
}
std::process::exit(0); std::process::exit(0);
} }
pub async fn run_client(config: crate::config::ClientConfig) -> Result<()> { pub async fn run_client(config: crate::config::ClientConfig) -> Result<()> {
#[cfg(target_os = "windows")]
if config.mode == "tun" && !is_admin() {
println!("[ostp] TUN mode requires administrator privileges. Relaunching...");
relaunch_as_admin()?;
}
#[cfg(target_os = "linux")]
if config.mode == "tun" && !is_root() {
println!("[ostp] TUN mode requires root privileges. Requesting sudo/pkexec elevation...");
relaunch_as_root()?;
}
let bg = std::env::args().any(|a| a == "--bg"); let bg = std::env::args().any(|a| a == "--bg");
if bg { if bg {
hide_console(); hide_console();
} }
#[cfg(target_os = "windows")]
if config.mode == "tun" && !is_admin() {
println!("[ostp-client] TUN mode requires Administrator privileges. Relaunching as Admin...");
relaunch_as_admin()?;
}
if config.mode == "tun" && !config.exclusions.processes.is_empty() {
println!("[ostp-client] WARNING: process exclusions are not supported in the current TUN implementation");
}
if config.mode == "tun" {
tunnel::download_wintun_dll(config.debug)?;
}
let (proxy_events_tx, proxy_events_rx) = mpsc::channel(10000);
let (client_msgs_tx, client_msgs_rx) = mpsc::channel(10000);
let metrics = Arc::new(BridgeMetrics { let metrics = Arc::new(BridgeMetrics {
bytes_sent: std::sync::atomic::AtomicU64::new(0), bytes_sent: portable_atomic::AtomicU64::new(0),
bytes_recv: std::sync::atomic::AtomicU64::new(0), bytes_recv: portable_atomic::AtomicU64::new(0),
connection_state: portable_atomic::AtomicU8::new(0),
rtt_ms: portable_atomic::AtomicU32::new(0),
}); });
let bridge = Bridge::new(&config, metrics)?; let (shutdown_tx, shutdown_rx) = watch::channel(false);
tokio::spawn(async move {
if wait_for_shutdown_signal().await.is_ok() {
let _ = shutdown_tx.send(true);
}
});
run_client_core(config, metrics, shutdown_rx, None).await
}
/// Runs the client with auto-reconnect: any subsystem ending — a network
/// change stranding the TUN adapter/UDP socket on a dead interface, the OSTP
/// protocol connection dropping in a way the inner Bridge-level retry (see
/// `UiEvent::TunnelStopped` below) couldn't recover from, or a proxy/TUN task
/// crashing outright — triggers a full clean restart (fresh DNS resolution,
/// fresh Bridge, fresh TUN/proxy) with exponential backoff, instead of the
/// client just dying. Only an explicit shutdown request stops this loop.
pub async fn run_client_core(
config: crate::config::ClientConfig,
metrics: Arc<BridgeMetrics>,
mut shutdown_rx_ext: watch::Receiver<bool>,
config_rx: Option<watch::Receiver<crate::config::ClientConfig>>,
) -> Result<()> {
use portable_atomic::Ordering;
const BACKOFF_SCHEDULE_SECS: [u64; 6] = [1, 2, 5, 10, 20, 30];
// A run that stayed up at least this long counts as "was actually
// connected", so a later drop restarts the backoff from the top instead
// of inheriting a long delay from a previous flaky stretch.
const STABLE_UPTIME: std::time::Duration = std::time::Duration::from_secs(60);
let mut backoff_idx = 0usize;
loop {
if *shutdown_rx_ext.borrow() {
return Ok(());
}
let attempt_start = std::time::Instant::now();
let result = run_client_once(config.clone(), metrics.clone(), shutdown_rx_ext.clone(), config_rx.clone()).await;
if *shutdown_rx_ext.borrow() {
// Shutdown was requested during (or right after) this attempt — honor it, don't retry.
return result;
}
if let Err(ref e) = result {
tracing::warn!("client run ended unexpectedly, will auto-reconnect: {e}");
}
if attempt_start.elapsed() >= STABLE_UPTIME {
backoff_idx = 0;
}
let delay = BACKOFF_SCHEDULE_SECS[backoff_idx.min(BACKOFF_SCHEDULE_SECS.len() - 1)];
backoff_idx += 1;
// Reflect the retry wait as "connecting" rather than "disconnected".
metrics.connection_state.store(1, Ordering::Relaxed);
tokio::select! {
_ = tokio::time::sleep(std::time::Duration::from_secs(delay)) => {}
_ = shutdown_rx_ext.changed() => {
if *shutdown_rx_ext.borrow() {
return Ok(());
}
}
}
}
}
async fn run_client_once(
mut config: crate::config::ClientConfig,
metrics: Arc<BridgeMetrics>,
mut shutdown_rx_ext: watch::Receiver<bool>,
mut config_rx: Option<watch::Receiver<crate::config::ClientConfig>>,
) -> Result<()> {
#[cfg(target_os = "windows")]
if config.mode == "tun" && !is_admin() {
return Err(anyhow::anyhow!("Administrator privileges are required to initialize TUN mode. Please run the application as Administrator."));
}
#[cfg(target_os = "linux")]
if config.mode == "tun" && !is_root() {
return Err(anyhow::anyhow!("Root privileges are required to initialize TUN mode on Linux. Please run with sudo."));
}
log_to_core_file(&format!("[core] Starting run_client_core in mode: {}", config.mode));
// Resolve the server IP before we override system routing and DNS.
// This prevents DNS deadlock if the VPN disconnects and tries to reconnect,
// and also ensures we add the direct route to the exact IP the bridge connects to.
#[allow(unused_mut)]
let mut resolved_addrs: Vec<std::net::SocketAddr> = tokio::net::lookup_host(&config.ostp.server_addr)
.await
.map_err(|e| anyhow::anyhow!("Failed to resolve server address {}: {}", config.ostp.server_addr, e))?
.collect();
let target_addr = resolved_addrs.first()
.ok_or_else(|| anyhow::anyhow!("No IP addresses resolved for {}", config.ostp.server_addr))?;
log_to_core_file(&format!("[core] Resolved server address to {}", target_addr));
config.ostp.server_addr = target_addr.to_string();
#[cfg(target_os = "linux")]
if config.mode == "tun" {
println!("\n[ostp] ===========================================================================");
println!("[ostp] WARNING: You are starting TUN mode on a Linux system.");
println!("[ostp] If this is a remote headless server, routing all traffic through the TUN");
println!("[ostp] interface WILL DROP your SSH connection and lock you out!");
println!("[ostp] ");
println!("[ostp] SOLUTION: Add a static route for your client IP to bypass the TUN.");
println!("[ostp] Find your default gateway (ip route | grep default) and run:");
println!("[ostp] sudo ip route add <your-client-ip> via <default-gateway-ip>");
println!("[ostp] ===========================================================================\n");
}
#[cfg(target_os = "linux")]
if config.mode == "proxy" {
println!("\n[ostp] ===========================================================================");
println!("[ostp] Proxy mode initialized on {}", config.local_proxy.bind_addr);
println!("[ostp] ===========================================================================\n");
}
let _sysproxy_guard = if config.mode == "proxy" {
// Enable system proxy and set initial ProxyOverride with user exclusions
let guard = Some(crate::sysproxy::SystemProxyGuard::enable(&config.local_proxy.bind_addr));
crate::sysproxy::update_proxy_bypass_list(
&config.exclusions.domains,
&config.exclusions.ips,
);
guard
} else {
None
};
let (proxy_events_tx, proxy_events_rx) = mpsc::channel(256);
let (client_msgs_tx, client_msgs_rx) = mpsc::unbounded_channel();
// Setup exclusions hot-reload channel
let (reload_tx, reload_rx) = watch::channel(config.exclusions.clone());
let mut bridge = Bridge::new(&config, metrics)?;
bridge.reload_tx = Some(reload_tx.clone());
let (ui_tx, mut ui_rx) = mpsc::channel(512); let (ui_tx, mut ui_rx) = mpsc::channel(512);
let (cmd_tx, cmd_rx) = mpsc::channel(128); let (cmd_tx, cmd_rx) = mpsc::channel(128);
let (shutdown_tx, shutdown_rx) = watch::channel(false); let (shutdown_tx, shutdown_rx) = watch::channel(false);
let proxy_shutdown_rx = shutdown_tx.subscribe(); let proxy_shutdown_rx = shutdown_tx.subscribe();
let is_tun = config.mode == "tun";
// Auto-connect on startup // Auto-connect on startup
let _ = cmd_tx.send(BridgeCommand::ToggleTunnel).await; let _ = cmd_tx.send(BridgeCommand::ToggleTunnel).await;
@ -100,28 +334,25 @@ pub async fn run_client(config: crate::config::ClientConfig) -> Result<()> {
match msg { match msg {
crate::app::UiEvent::Log(text) => { crate::app::UiEvent::Log(text) => {
if debug_enabled || is_essential_log(&text) { if debug_enabled || is_essential_log(&text) {
println!("[client] {text}"); log_to_core_file(&format!("[ostp] {text}"));
println!("[ostp] {text}");
} }
} }
crate::app::UiEvent::Metrics { status, rtt_ms, .. } => { crate::app::UiEvent::Metrics { status, rtt_ms, .. } => {
let status_str = status.as_str().to_string(); let status_str = status.as_str().to_string();
if last_status != Some(status_str.clone()) { if last_status != Some(status_str.clone()) {
last_status = Some(status_str.clone()); last_status = Some(status_str.clone());
println!("[client] status={status_str} rtt_ms={:.1}", rtt_ms); println!("[ostp] Status: {} (rtt={:.1}ms)", status_str, rtt_ms);
} }
} }
crate::app::UiEvent::Traffic { .. } => {} crate::app::UiEvent::Traffic { .. } => {}
crate::app::UiEvent::ProfileChanged(profile) => { crate::app::UiEvent::ProfileChanged(profile) => {
if debug_enabled { if debug_enabled {
println!("[client] profile={profile:?}"); println!("[ostp] Obfuscation profile: {profile:?}");
} }
} }
crate::app::UiEvent::TunnelStopped => { crate::app::UiEvent::TunnelStopped => {
if is_tun { println!("[ostp] Connection interrupted. Reconnecting in 5 seconds...");
println!("[client] tunnel=tun stopped, reconnecting in 5s");
} else {
println!("[client] tunnel=proxy stopped, reconnecting in 5s");
}
let cmd_tx_inner = cmd_tx_clone.clone(); let cmd_tx_inner = cmd_tx_clone.clone();
tokio::spawn(async move { tokio::spawn(async move {
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await; tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
@ -132,16 +363,17 @@ pub async fn run_client(config: crate::config::ClientConfig) -> Result<()> {
} }
}); });
let bridge_task = tokio::spawn(async move { let mut bridge_task = tokio::spawn(async move {
bridge.run(ui_tx, cmd_rx, shutdown_rx, proxy_events_rx, client_msgs_tx).await bridge.run(ui_tx, cmd_rx, shutdown_rx, proxy_events_rx, client_msgs_tx).await
}); });
let config_clone = config.clone(); let config_clone = config.clone();
let proxy_task = tokio::spawn(async move { let proxy_exclusions_rx = reload_rx.clone();
let mut proxy_task = tokio::spawn(async move {
tunnel::run_local_proxy( tunnel::run_local_proxy(
config.local_proxy, config.local_proxy,
config.ostp, config.ostp,
config.exclusions, proxy_exclusions_rx,
config.debug, config.debug,
proxy_shutdown_rx, proxy_shutdown_rx,
proxy_events_tx, proxy_events_tx,
@ -151,25 +383,77 @@ pub async fn run_client(config: crate::config::ClientConfig) -> Result<()> {
}); });
let wintun_shutdown_rx = shutdown_tx.subscribe(); let wintun_shutdown_rx = shutdown_tx.subscribe();
let wintun_task = if config_clone.mode == "tun" { let wintun_exclusions_rx = reload_rx.clone();
let mut wintun_task = if config_clone.mode == "tun" {
Some(tokio::spawn(async move { Some(tokio::spawn(async move {
tunnel::run_wintun_tunnel(wintun_shutdown_rx, config_clone.debug).await tunnel::run_tun_tunnel(config_clone, wintun_shutdown_rx, wintun_exclusions_rx).await
})) }))
} else { } else {
None None
}; };
// Wait for Ctrl-C / signal // Wait for local_shutdown
wait_for_shutdown_signal().await?; let mut local_shutdown = shutdown_rx_ext.clone();
let _ = cmd_tx.send(BridgeCommand::Shutdown).await; let cmd_tx_loop = cmd_tx.clone();
tokio::spawn(async move {
loop {
tokio::select! {
_ = local_shutdown.changed() => {
if *local_shutdown.borrow() {
let _ = cmd_tx_loop.send(BridgeCommand::Shutdown).await;
break;
}
}
Some(Ok(_)) = async {
if let Some(ref mut rx) = config_rx {
Some(rx.changed().await)
} else {
std::future::pending().await
}
} => {
if let Some(ref rx) = config_rx {
let new_cfg = rx.borrow().clone();
// Update Windows ProxyOverride so excluded domains/IPs
// bypass the system proxy immediately (proxy mode only).
crate::sysproxy::update_proxy_bypass_list(
&new_cfg.exclusions.domains,
&new_cfg.exclusions.ips,
);
let _ = reload_tx.send(new_cfg.exclusions);
}
}
}
}
});
let _ = shutdown_tx.send(true); // Wait for either external shutdown OR any task to fail
let _ = bridge_task.await?; tokio::select! {
let _ = proxy_task.await?; _ = shutdown_rx_ext.changed() => {
if let Some(task) = wintun_task { let _ = cmd_tx.send(BridgeCommand::Shutdown).await;
let _ = task.await?; let _ = shutdown_tx.send(true);
}
res = &mut bridge_task => {
let _ = shutdown_tx.send(true);
res.map_err(|e| anyhow::anyhow!("Bridge task panicked: {}", e))??;
}
res = &mut proxy_task => {
let _ = shutdown_tx.send(true);
res.map_err(|e| anyhow::anyhow!("Proxy task panicked: {}", e))??;
}
res = async {
if let Some(t) = wintun_task.as_mut() { t.await } else { std::future::pending().await }
} => {
let _ = shutdown_tx.send(true);
res.map_err(|e| anyhow::anyhow!("TUN task panicked: {}", e))??;
}
}
// Final cleanup: wait for tasks to finish
let _ = bridge_task.await;
let _ = proxy_task.await;
if let Some(task) = wintun_task {
let _ = task.await;
} }
tunnel::cleanup().await?;
Ok(()) Ok(())
} }
@ -188,15 +472,17 @@ fn format_bytes(bps: u64) -> String {
fn is_essential_log(text: &str) -> bool { fn is_essential_log(text: &str) -> bool {
matches!( matches!(
text, text,
"Handshaking started" "Connection established"
| "Bridge connection established" | "TUN tunnel established"
| "TUN Tunnel established" | "TUN tunnel stopped"
| "Bridge stopped" | "Bridge stopped"
| "TUN Tunnel stopped"
| "Runtime config reloaded" | "Runtime config reloaded"
) || text.starts_with("Connected UDP directly to ") | "Connecting to remote server..."
|| text.starts_with("TURN: Relay allocated") ) || text.starts_with("Connected to ")
|| text.starts_with("TURN relay allocated")
|| text.starts_with("TURN allocation failed") || text.starts_with("TURN allocation failed")
|| text.starts_with("Handshake failed") || text.starts_with("Allocating TURN relay")
|| text.starts_with("Connection timeout") || text.starts_with("Connection failed:")
|| text.starts_with("Connection lost")
|| text.starts_with("Protocol tick fatal error")
} }

View File

@ -8,8 +8,12 @@ pub async fn wait_for_shutdown_signal() -> Result<()> {
let mut sigint = signal(SignalKind::interrupt())?; let mut sigint = signal(SignalKind::interrupt())?;
tokio::select! { tokio::select! {
_ = sigterm.recv() => {} _ = sigterm.recv() => {
_ = sigint.recv() => {} tracing::info!("Received SIGTERM, shutting down");
}
_ = sigint.recv() => {
tracing::info!("Received SIGINT, shutting down");
}
} }
Ok(()) Ok(())
@ -17,6 +21,37 @@ pub async fn wait_for_shutdown_signal() -> Result<()> {
#[cfg(not(unix))] #[cfg(not(unix))]
pub async fn wait_for_shutdown_signal() -> Result<()> { pub async fn wait_for_shutdown_signal() -> Result<()> {
tokio::signal::ctrl_c().await?; #[cfg(target_os = "windows")]
{
use tokio::signal::windows::{ctrl_break, ctrl_c, ctrl_close};
let mut c_c = ctrl_c()?;
let mut c_close = ctrl_close()?;
let mut c_break = ctrl_break()?;
tokio::select! {
res = c_c.recv() => {
tracing::info!("Received Ctrl+C, shutting down");
if res.is_none() {
std::future::pending::<()>().await;
}
}
res = c_close.recv() => {
tracing::info!("Received console close event, shutting down");
if res.is_none() {
std::future::pending::<()>().await;
}
}
res = c_break.recv() => {
tracing::info!("Received Ctrl+Break, shutting down");
if res.is_none() {
std::future::pending::<()>().await;
}
}
}
}
#[cfg(not(target_os = "windows"))]
{
tokio::signal::ctrl_c().await?;
}
Ok(()) Ok(())
} }

View File

@ -1,6 +1,12 @@
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
use std::process::Command; use std::process::Command;
#[cfg(target_os = "windows")]
use std::os::windows::process::CommandExt;
#[cfg(target_os = "windows")]
const CREATE_NO_WINDOW: u32 = 0x08000000;
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
#[link(name = "wininet")] #[link(name = "wininet")]
extern "system" { extern "system" {
@ -18,50 +24,143 @@ const INTERNET_OPTION_REFRESH: u32 = 37;
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
pub fn enable_windows_proxy(proxy_addr: &str) { pub fn enable_windows_proxy(proxy_addr: &str) {
let _ = Command::new("reg") tracing::info!("Enabling Windows system proxy: {}", proxy_addr);
.args([
"add",
"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
"/v",
"ProxyEnable",
"/t",
"REG_DWORD",
"/d",
"1",
"/f",
])
.output();
let proxy_str = format!("http={};https={}", proxy_addr, proxy_addr);
let _ = Command::new("reg")
.args([
"add",
"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
"/v",
"ProxyServer",
"/t",
"REG_SZ",
"/d",
&proxy_str,
"/f",
])
.output();
let result = Command::new("reg")
.creation_flags(CREATE_NO_WINDOW)
.args([
"add",
"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
"/v", "ProxyEnable",
"/t", "REG_DWORD",
"/d", "1",
"/f",
])
.output();
match result {
Ok(out) if !out.status.success() => {
tracing::error!("Failed to set ProxyEnable: {}", String::from_utf8_lossy(&out.stderr));
}
Err(e) => tracing::error!("Failed to execute reg.exe (ProxyEnable): {}", e),
_ => {}
}
let result = Command::new("reg")
.creation_flags(CREATE_NO_WINDOW)
.args([
"add",
"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
"/v", "ProxyServer",
"/t", "REG_SZ",
"/d", proxy_addr,
"/f",
])
.output();
match result {
Ok(out) if !out.status.success() => {
tracing::error!("Failed to set ProxyServer: {}", String::from_utf8_lossy(&out.stderr));
}
Err(e) => tracing::error!("Failed to execute reg.exe (ProxyServer): {}", e),
_ => {}
}
// Set initial bypass list (will be expanded by update_proxy_bypass_list)
update_proxy_bypass_list_windows(&[], &[]);
refresh_wininet();
tracing::info!("System proxy enabled successfully");
}
/// Update the Windows ProxyOverride registry value to include user-configured
/// excluded domains and IPs. This makes excluded hosts bypass the OSTP proxy
/// entirely at the OS level — the most reliable split-tunneling mechanism.
///
/// For each domain `d`, adds both `d` and `*.d` so both the root and all
/// subdomains bypass the proxy.
/// For IPs, adds them verbatim (Windows supports exact IPs and wildcards like
/// `192.168.*`).
#[cfg(target_os = "windows")]
pub fn update_proxy_bypass_list(domains: &[String], ips: &[String]) {
update_proxy_bypass_list_windows(domains, ips);
refresh_wininet(); refresh_wininet();
} }
#[cfg(not(target_os = "windows"))]
pub fn update_proxy_bypass_list(_domains: &[String], _ips: &[String]) {
// Linux/macOS: no-op (gnome/kde proxy bypass list update not implemented)
}
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
pub fn disable_windows_proxy() { fn update_proxy_bypass_list_windows(domains: &[String], ips: &[String]) {
// Base list: always bypass local addresses
let mut parts: Vec<String> = vec![
"localhost".into(),
"127.*".into(),
"10.*".into(),
"172.16.*".into(),
"172.17.*".into(),
"172.18.*".into(),
"172.19.*".into(),
"172.20.*".into(),
"172.21.*".into(),
"172.22.*".into(),
"172.23.*".into(),
"172.24.*".into(),
"172.25.*".into(),
"172.26.*".into(),
"172.27.*".into(),
"172.28.*".into(),
"172.29.*".into(),
"172.30.*".into(),
"172.31.*".into(),
"192.168.*".into(),
"<local>".into(),
];
// Add excluded domains: both exact and wildcard subdomain form
for d in domains {
let d = d.trim().trim_start_matches('.').to_lowercase();
if d.is_empty() { continue; }
parts.push(d.clone());
parts.push(format!("*.{}", d));
}
// Add excluded IPs verbatim
for ip in ips {
let ip = ip.trim();
if ip.is_empty() { continue; }
// Strip CIDR suffix if present — Windows ProxyOverride doesn't support CIDR
let host = ip.split('/').next().unwrap_or(ip);
parts.push(host.to_string());
}
let override_value = parts.join(";");
tracing::info!("Updating ProxyOverride: {}", override_value);
let _ = Command::new("reg") let _ = Command::new("reg")
.creation_flags(CREATE_NO_WINDOW)
.args([ .args([
"add", "add",
"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
"/v", "/v", "ProxyOverride",
"ProxyEnable", "/t", "REG_SZ",
"/t", "/d", &override_value,
"REG_DWORD", "/f",
"/d", ])
"0", .output();
}
#[cfg(target_os = "windows")]
pub fn disable_system_proxy() {
tracing::info!("Disabling Windows system proxy");
let _ = Command::new("reg")
.creation_flags(CREATE_NO_WINDOW)
.args([
"add",
"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
"/v", "ProxyEnable",
"/t", "REG_DWORD",
"/d", "0",
"/f", "/f",
]) ])
.output(); .output();
@ -88,26 +187,92 @@ fn refresh_wininet() {
} }
#[cfg(not(target_os = "windows"))] #[cfg(not(target_os = "windows"))]
pub fn enable_windows_proxy(_proxy_addr: &str) {} pub fn enable_system_proxy(proxy_addr: &str) {
let parts: Vec<&str> = proxy_addr.split(':').collect();
let host = parts.first().unwrap_or(&"127.0.0.1");
let port = parts.get(1).unwrap_or(&"1088");
let is_gui = std::env::var("DISPLAY").is_ok() || std::env::var("WAYLAND_DISPLAY").is_ok();
if is_gui {
tracing::info!("Enabling Linux system proxy (GNOME/KDE): {}", proxy_addr);
// Try GNOME gsettings
let gnome_res = std::process::Command::new("gsettings")
.args(["set", "org.gnome.system.proxy", "mode", "manual"])
.output();
if let Ok(out) = gnome_res {
if out.status.success() {
let _ = std::process::Command::new("gsettings").args(["set", "org.gnome.system.proxy.socks", "host", host]).output();
let _ = std::process::Command::new("gsettings").args(["set", "org.gnome.system.proxy.socks", "port", port]).output();
let _ = std::process::Command::new("gsettings").args(["set", "org.gnome.system.proxy", "ignore-hosts", "['localhost', '127.0.0.0/8', '10.0.0.0/8', '192.168.0.0/16']"]).output();
tracing::info!("GNOME system proxy enabled.");
return;
}
}
// Try KDE kwriteconfig5/6
for cmd in ["kwriteconfig5", "kwriteconfig6"] {
let kde_res = std::process::Command::new(cmd)
.args(["--file", "kioslaverc", "--group", "Proxy Settings", "--key", "ProxyType", "1"])
.output();
if let Ok(out) = kde_res {
if out.status.success() {
let socks_val = format!("socks://{}:{}", host, port);
let _ = std::process::Command::new(cmd).args(["--file", "kioslaverc", "--group", "Proxy Settings", "--key", "socksProxy", &socks_val]).output();
let _ = std::process::Command::new("dbus-send").args(["--type=signal", "/KIO/Scheduler", "org.kde.KIO.Scheduler.reparseSlaveConfiguration", "string:''"]).output();
tracing::info!("KDE system proxy enabled.");
return;
}
}
}
}
// Headless fallback
println!("\n===================================================================");
println!("OSTP Local Proxy is running at socks5://{}", proxy_addr);
println!("Since you are in a headless/terminal environment, OSTP cannot automatically");
println!("configure your system proxy. To route traffic from this terminal, run:");
println!("\n eval $(ostp proxy-env)\n");
println!("Or configure your application (e.g. curl -x socks5://{})", proxy_addr);
println!("===================================================================\n");
}
#[cfg(not(target_os = "windows"))] #[cfg(not(target_os = "windows"))]
pub fn disable_windows_proxy() {} pub fn disable_system_proxy() {
let is_gui = std::env::var("DISPLAY").is_ok() || std::env::var("WAYLAND_DISPLAY").is_ok();
if is_gui {
tracing::info!("Disabling Linux system proxy...");
let _ = std::process::Command::new("gsettings").args(["set", "org.gnome.system.proxy", "mode", "none"]).output();
let _ = std::process::Command::new("kwriteconfig5").args(["--file", "kioslaverc", "--group", "Proxy Settings", "--key", "ProxyType", "0"]).output();
let _ = std::process::Command::new("kwriteconfig6").args(["--file", "kioslaverc", "--group", "Proxy Settings", "--key", "ProxyType", "0"]).output();
let _ = std::process::Command::new("dbus-send").args(["--type=signal", "/KIO/Scheduler", "org.kde.KIO.Scheduler.reparseSlaveConfiguration", "string:''"]).output();
}
}
pub struct WindowsProxyGuard { #[cfg(target_os = "windows")]
pub fn enable_system_proxy(proxy_addr: &str) {
enable_windows_proxy(proxy_addr);
}
pub struct SystemProxyGuard {
active: bool, active: bool,
} }
impl WindowsProxyGuard { impl SystemProxyGuard {
pub fn enable(proxy_addr: &str) -> Self { pub fn enable(proxy_addr: &str) -> Self {
enable_windows_proxy(proxy_addr); enable_system_proxy(proxy_addr);
Self { active: true } Self { active: true }
} }
} }
impl Drop for WindowsProxyGuard { impl Drop for SystemProxyGuard {
fn drop(&mut self) { fn drop(&mut self) {
if self.active { if self.active {
disable_windows_proxy(); disable_system_proxy();
} }
} }
} }

View File

@ -0,0 +1,56 @@
use std::sync::Arc;
use tokio::net::UdpSocket;
use bytes::Bytes;
#[derive(Clone)]
pub enum Transport {
Udp(Arc<UdpSocket>),
Uot {
tx: tokio::sync::mpsc::Sender<Bytes>,
rx: Arc<tokio::sync::Mutex<tokio::sync::mpsc::Receiver<Bytes>>>,
}
}
impl Transport {
pub async fn send(&self, frame: &Bytes) -> std::io::Result<usize> {
match self {
Self::Udp(sock) => sock.send(frame).await,
Self::Uot { tx, .. } => {
tx.send(frame.clone()).await.map_err(|_| std::io::Error::new(std::io::ErrorKind::BrokenPipe, "uot closed"))?;
Ok(frame.len())
}
}
}
pub async fn send_to(&self, frame: &Bytes, target: std::net::SocketAddr) -> std::io::Result<usize> {
match self {
Self::Udp(sock) => sock.send_to(frame, target).await,
Self::Uot { .. } => self.send(frame).await,
}
}
pub async fn recv(&self, buf: &mut [u8]) -> std::io::Result<usize> {
match self {
Self::Udp(sock) => sock.recv(buf).await,
Self::Uot { rx, .. } => {
let mut rx = rx.lock().await;
match rx.recv().await {
Some(bytes) => {
let len = bytes.len().min(buf.len());
buf[..len].copy_from_slice(&bytes[..len]);
Ok(len)
}
None => Err(std::io::Error::new(std::io::ErrorKind::BrokenPipe, "uot closed")),
}
}
}
}
pub fn local_addr(&self) -> std::io::Result<std::net::SocketAddr> {
match self {
Self::Udp(sock) => sock.local_addr(),
Self::Uot { .. } => Ok("0.0.0.0:0".parse().unwrap()),
}
}
}

View File

@ -0,0 +1,134 @@
use crate::config::ExclusionConfig;
use std::time::Duration;
use tokio::time::timeout;
#[derive(Clone)]
pub struct ExclusionMatcher {
pub domain_suffix: Vec<String>,
pub cidrs: Vec<Cidr>,
pub processes: Vec<String>,
pub physical_if_index: Option<u32>,
pub physical_if_name: Option<String>,
}
impl ExclusionMatcher {
pub fn new(
exclusions: &ExclusionConfig,
physical_if_index: Option<u32>,
physical_if_name: Option<String>,
) -> Self {
let mut cidrs = Vec::new();
for ip in &exclusions.ips {
if let Some(cidr) = parse_cidr(ip) {
cidrs.push(cidr);
}
}
let processes = exclusions.processes.iter()
.map(|p| p.trim().to_lowercase())
.filter(|p| !p.is_empty())
.collect();
Self {
domain_suffix: exclusions
.domains
.iter()
.map(|d| d.trim().trim_start_matches('.').to_lowercase())
.filter(|d| !d.is_empty())
.collect(),
cidrs,
processes,
physical_if_index,
physical_if_name,
}
}
pub async fn should_bypass_target(&self, host: &str, port: u16, timeout_value: Duration) -> bool {
if self.match_domain(host) {
return true;
}
if self.cidrs.is_empty() {
return false;
}
if let Ok(ip) = host.parse::<std::net::IpAddr>() {
return self.match_ip(&ip);
}
let lookup_target = (host.to_string(), port);
match timeout(timeout_value, tokio::net::lookup_host(lookup_target)).await {
Ok(Ok(addrs)) => addrs.into_iter().any(|addr| self.match_ip(&addr.ip())),
_ => false,
}
}
pub fn match_domain(&self, host: &str) -> bool {
if self.domain_suffix.is_empty() {
return false;
}
let host = host.trim_end_matches('.').to_lowercase();
self.domain_suffix.iter().any(|suffix| {
host == *suffix || host.ends_with(&format!(".{suffix}"))
})
}
pub fn match_ip(&self, ip: &std::net::IpAddr) -> bool {
self.cidrs.iter().any(|cidr| cidr.contains(ip))
}
pub fn match_process(&self, process_name: &str) -> bool {
if self.processes.is_empty() {
return false;
}
let p = process_name.to_lowercase();
self.processes.iter().any(|ex| p.contains(ex))
}
}
#[derive(Clone)]
pub enum Cidr {
V4(u32, u8),
V6(u128, u8),
}
impl Cidr {
pub fn contains(&self, ip: &std::net::IpAddr) -> bool {
match (self, ip) {
(Cidr::V4(net, bits), std::net::IpAddr::V4(addr)) => {
let mask = if *bits == 0 { 0 } else { u32::MAX << (32 - bits) };
let ip = u32::from_be_bytes(addr.octets());
(ip & mask) == (*net & mask)
}
(Cidr::V6(net, bits), std::net::IpAddr::V6(addr)) => {
let mask = if *bits == 0 { 0 } else { u128::MAX << (128 - bits) };
let ip = u128::from_be_bytes(addr.octets());
(ip & mask) == (*net & mask)
}
_ => false,
}
}
}
pub fn parse_cidr(s: &str) -> Option<Cidr> {
let parts: Vec<&str> = s.split('/').collect();
if parts.is_empty() || parts.len() > 2 {
return None;
}
if let Ok(ip) = parts[0].parse::<std::net::IpAddr>() {
let bits = if parts.len() == 2 {
parts[1].parse::<u8>().ok()?
} else {
match ip {
std::net::IpAddr::V4(_) => 32,
std::net::IpAddr::V6(_) => 128,
}
};
match ip {
std::net::IpAddr::V4(v4) => Some(Cidr::V4(u32::from_be_bytes(v4.octets()), bits)),
std::net::IpAddr::V6(v6) => Some(Cidr::V6(u128::from_be_bytes(v6.octets()), bits)),
}
} else {
None
}
}

View File

@ -1,9 +1,15 @@
mod proxy; mod proxy;
mod wintun_downloader; pub mod native_handler;
mod wintun_handler;
pub use wintun_downloader::download_wintun_dll; mod udp_nat;
pub use wintun_handler::run_wintun_tunnel;
pub async fn run_tun_tunnel(
config: crate::config::ClientConfig,
shutdown: tokio::sync::watch::Receiver<bool>,
exclusions_rx: tokio::sync::watch::Receiver<crate::config::ExclusionConfig>,
) -> anyhow::Result<()> {
native_handler::run_native_tunnel(config, shutdown, exclusions_rx).await
}
use tokio::sync::{mpsc, watch}; use tokio::sync::{mpsc, watch};
@ -17,6 +23,14 @@ pub enum ProxyEvent {
stream_id: u16, stream_id: u16,
target: String, target: String,
}, },
UdpAssociate {
stream_id: u16,
},
UdpData {
stream_id: u16,
target: String,
payload: bytes::Bytes,
},
Data { Data {
stream_id: u16, stream_id: u16,
payload: bytes::Bytes, payload: bytes::Bytes,
@ -30,39 +44,24 @@ pub enum ProxyEvent {
pub enum ProxyToClientMsg { pub enum ProxyToClientMsg {
ConnectOk, ConnectOk,
Data(bytes::Bytes), Data(bytes::Bytes),
UdpData(String, bytes::Bytes),
Close, Close,
Error(String), Error(String),
} }
#[allow(dead_code)]
pub struct TunnelConfig {
pub local_bind: String,
pub remote_addr: String,
}
impl Default for TunnelConfig {
fn default() -> Self {
Self {
local_bind: "127.0.0.1:1080".to_string(),
remote_addr: "127.0.0.1:443".to_string(),
}
}
}
pub async fn cleanup() -> anyhow::Result<()> {
Ok(())
}
pub async fn run_local_proxy( pub async fn run_local_proxy(
cfg: LocalProxyConfig, cfg: LocalProxyConfig,
ostp: OstpConfig, ostp: OstpConfig,
exclusions: ExclusionConfig, exclusions_rx: watch::Receiver<ExclusionConfig>,
debug: bool, debug: bool,
shutdown: watch::Receiver<bool>, shutdown: watch::Receiver<bool>,
proxy_events_tx: mpsc::Sender<ProxyEvent>, proxy_events_tx: mpsc::Sender<ProxyEvent>,
client_msgs_rx: mpsc::Receiver<(u16, ProxyToClientMsg)>, client_msgs_rx: mpsc::UnboundedReceiver<(u16, ProxyToClientMsg)>,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
run_local_socks5_proxy(cfg, ostp, exclusions, debug, shutdown, proxy_events_tx, client_msgs_rx).await run_local_socks5_proxy(cfg, ostp, exclusions_rx, debug, shutdown, proxy_events_tx, client_msgs_rx).await
} }
pub mod exclusion;
pub mod process_lookup;
pub mod sni_sniff;

View File

@ -0,0 +1,744 @@
use anyhow::{anyhow, Result};
use tokio::sync::watch;
// ──────────────────────────────────────────────────────────────────────────────
// Windows / Linux desktop TUN
// ──────────────────────────────────────────────────────────────────────────────
#[cfg(any(target_os = "windows", target_os = "linux"))]
pub async fn run_native_tunnel(
config: crate::config::ClientConfig,
mut shutdown: watch::Receiver<bool>,
mut exclusions_rx: watch::Receiver<crate::config::ExclusionConfig>,
) -> Result<()> {
use std::net::ToSocketAddrs;
use netstack_smoltcp::StackBuilder;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use futures::{StreamExt, SinkExt};
#[cfg(target_os = "linux")]
{
use std::io::{self, IsTerminal, Write};
if io::stdout().is_terminal() {
println!("\n===================================================================");
println!("WARNING: TUN mode will modify the system routing table.");
println!("If you are connected to a headless server via SSH, you may lose");
println!("your connection when default routes are redirected into the tunnel.");
println!("===================================================================\n");
print!("Are you sure you want to initialize the TUN interface? [yes/no]: ");
io::stdout().flush().unwrap();
let mut input = String::new();
io::stdin().read_line(&mut input).unwrap();
let ans = input.trim().to_lowercase();
if ans != "y" && ans != "yes" {
return Err(anyhow!("TUN initialization aborted by user."));
}
}
}
let debug = config.debug;
tracing::info!("Initializing NATIVE TUN tunnel (smoltcp)...");
// Capture physical interface index for bypass BEFORE we create the TUN device and alter routes.
#[cfg(target_os = "windows")]
let phys_if_for_bypass: Option<u32> = ostp_tun::windows::windows_route::sys::get_default_ipv4_route().map(|(_, idx)| idx);
#[cfg(not(target_os = "windows"))]
let phys_if_for_bypass: Option<u32> = None;
// ── 1. Resolve server IP ──────────────────────────────────────────────────
let server_ip = config
.ostp
.server_addr
.to_socket_addrs()
.map_err(|e| anyhow!("Failed to resolve server IP: {}", e))?
.next()
.map(|a| a.ip())
.ok_or_else(|| anyhow!("Could not resolve server host"))?;
#[allow(unused_variables)]
let server_ip_str = server_ip.to_string();
// ── 2. Resolve excluded domains → IP addresses for bypass routing ─────────
let mut bypass_ips: Vec<std::net::IpAddr> = Vec::new();
// Server IP always bypasses TUN
bypass_ips.push(server_ip);
for ip_str in &config.exclusions.ips {
let host = ip_str.split('/').next().unwrap_or(ip_str);
if let Ok(ip) = host.parse() {
bypass_ips.push(ip);
}
}
for domain in &config.exclusions.domains {
match tokio::net::lookup_host((domain.as_str(), 443u16)).await {
Ok(addrs) => {
for addr in addrs {
bypass_ips.push(addr.ip());
}
}
Err(e) => {
tracing::warn!("Failed to pre-resolve excluded domain {domain}: {e}");
}
}
}
// ── 3. Create TUN device via ostp-tun crate ───────────────────────────────
let opts = ostp_tun::OstpTunOptions {
server_ip,
bypass_ips,
dns_server: config.dns_server.clone(),
kill_switch: config.kill_switch,
mtu: config.ostp.mtu as u16,
wintun_path: None,
};
let tun_interface = ostp_tun::OstpTunInterface::create(opts)
.await
.map_err(|e| anyhow!("Failed to create OstpTunInterface: {}", e))?;
let dev = tun_interface.device;
let _route_guard = tun_interface.guard;
// ── 7. Build smoltcp network stack ────────────────────────────────────────
let (stack, tcp_runner, udp_socket, tcp_listener) = StackBuilder::default()
.stack_buffer_size(1024)
.tcp_buffer_size(1024)
.udp_buffer_size(1024)
.enable_tcp(true)
.enable_udp(true)
.mtu(config.ostp.mtu)
.build()?;
let mut runner_task = tokio::spawn(async move {
if let Some(runner) = tcp_runner {
let _ = runner.await;
}
});
// ── 8. Wire TUN ↔ smoltcp stack ───────────────────────────────────────────
let (mut stack_sink, mut stack_stream) = stack.split();
let (mut tun_read, mut tun_write) = tokio::io::split(dev);
let mut tun_to_stack = tokio::spawn(async move {
let mut buf = vec![0u8; 65536];
loop {
match tun_read.read(&mut buf).await {
Ok(0) => break,
Ok(n) => {
let frame = buf[..n].to_vec();
if let Err(e) = stack_sink.send(frame).await {
if e.kind() == std::io::ErrorKind::BrokenPipe {
break;
}
}
}
Err(e) => {
tracing::debug!("tun_read error: {e}");
}
}
}
});
let mut stack_to_tun = tokio::spawn(async move {
while let Some(Ok(frame)) = stack_stream.next().await {
if let Err(e) = tun_write.write(&frame).await {
tracing::debug!("tun_write error: {e}");
}
}
});
// ── 9. UDP: forward everything through OSTP proxy ─────────────────────────
// UDP exclusions are handled at the routing table level (step 5), so
// UDP packets for excluded IPs never reach smoltcp at all.
let udp_proxy_addr = {
let mut a = config.local_proxy.bind_addr.clone();
if a.starts_with("0.0.0.0:") {
a = a.replace("0.0.0.0:", "127.0.0.1:");
}
a
};
// Build exclusion matcher for dynamic bypass
let current_exclusions = exclusions_rx.borrow().clone();
let matcher = crate::tunnel::exclusion::ExclusionMatcher::new(&current_exclusions, None, None);
let matcher_arc = std::sync::Arc::new(tokio::sync::RwLock::new(matcher));
let matcher_clone = matcher_arc.clone();
tokio::spawn(async move {
while let Ok(_) = exclusions_rx.changed().await {
let current = exclusions_rx.borrow().clone();
let new_matcher = crate::tunnel::exclusion::ExclusionMatcher::new(&current, None, None);
*matcher_clone.write().await = new_matcher;
if true {
tracing::debug!("Desktop TUN exclusions hot-reloaded");
}
}
});
// Linux: physical interface name for SO_BINDTODEVICE
#[cfg(target_os = "linux")]
let linux_phys_name = crate::tunnel::proxy::get_linux_physical_if_name();
#[cfg(not(target_os = "linux"))]
let linux_phys_name: Option<String> = None;
let _ = &linux_phys_name; // suppress unused warning on Windows
let debug_udp = debug;
let udp_matcher = matcher_arc.clone();
#[cfg(target_os = "linux")]
let udp_lin_name = linux_phys_name.clone();
let mut udp_proxy_task = tokio::spawn(async move {
if let Some(udp_sock) = udp_socket {
#[cfg(target_os = "linux")]
super::udp_nat::run_udp_nat(udp_sock, udp_proxy_addr, debug_udp, udp_matcher, phys_if_for_bypass, udp_lin_name).await;
#[cfg(not(target_os = "linux"))]
super::udp_nat::run_udp_nat(udp_sock, udp_proxy_addr, debug_udp, udp_matcher, phys_if_for_bypass, None).await;
}
});
// ── 10. TCP: forward to OSTP proxy (with domain-level bypass via SNI) ─────
//
// For IP-based exclusions: handled by routing table → packets never arrive here.
// For domain-based exclusions: The IP is already in routing table (pre-resolved in
// step 3), so most traffic won't arrive. As a belt-and-suspenders fallback,
// we also sniff TLS SNI and bypass if it matches — this covers CDN cases where
// the IP wasn't known at startup.
//
// For bypassed connections we bind the outgoing socket to the physical interface
// (IP_UNICAST_IF) so it goes out via the real NIC, not TUN.
let proxy_addr_tcp = {
let mut a = config.local_proxy.bind_addr.clone();
if a.starts_with("0.0.0.0:") {
a = a.replace("0.0.0.0:", "127.0.0.1:");
}
a
};
// Physical interface index was captured at the start of the function.
let mut tcp_accept_task = tokio::spawn(async move {
let Some(mut listener) = tcp_listener else { return; };
while let Some((mut stream, local, remote)) = listener.next().await {
let proxy_addr = proxy_addr_tcp.clone();
let matcher_arc = matcher_arc.clone();
#[cfg(target_os = "linux")]
let lin_name = linux_phys_name.clone();
tokio::spawn(async move {
let matcher = matcher_arc.read().await.clone();
if debug {
tracing::debug!("TUN TCP {local} → {remote}");
}
// ── Sniff TLS ClientHello for SNI ─────────────────────────────
let mut sniff_buf = [0u8; 2048];
let sniff_len =
match tokio::time::timeout(
std::time::Duration::from_millis(100),
stream.read(&mut sniff_buf),
)
.await
{
Ok(Ok(n)) => n,
_ => 0,
};
// ── Decide: bypass or tunnel? ─────────────────────────────────
let mut should_bypass = false;
// 1. Process match via OS Extended TCP Table (Windows)
#[cfg(target_os = "windows")]
if !should_bypass {
if let Some(proc_name) = crate::tunnel::process_lookup::get_process_name_from_port(local.port()) {
if debug {
tracing::debug!("TUN TCP lookup: port {} -> process {}", local.port(), proc_name);
}
if matcher.match_process(&proc_name) {
if debug {
tracing::debug!("TUN TCP BYPASS (Process match): {} → {remote}", proc_name);
}
should_bypass = true;
}
} else {
if debug {
tracing::debug!("TUN TCP lookup: port {} -> no process found", local.port());
}
}
}
// 2. SNI domain check (belt-and-suspenders for CDNs / late-resolved IPs)
if !should_bypass && sniff_len > 0 {
if let Some(sni) =
crate::tunnel::sni_sniff::extract_sni(&sniff_buf[..sniff_len])
{
if debug {
tracing::debug!("TUN SNI: {sni}");
}
if matcher.match_domain(&sni) {
if debug {
tracing::info!("TUN TCP BYPASS (SNI domain): {sni} → {remote}");
}
should_bypass = true;
}
}
}
// 3. Destination IP CIDR check (for IPs not in routing table / IPv6)
if !should_bypass && matcher.match_ip(&remote.ip()) {
if debug {
tracing::info!("TUN TCP BYPASS (IP match): {remote}");
}
should_bypass = true;
}
// ── Bypass path: direct TCP bypassing TUN ─────────────────────
if should_bypass {
let socket = match remote {
std::net::SocketAddr::V4(_) => tokio::net::TcpSocket::new_v4(),
std::net::SocketAddr::V6(_) => tokio::net::TcpSocket::new_v6(),
};
let Ok(socket) = socket else { return; };
// Bind to physical interface so packets don't loop back into TUN
#[cfg(target_os = "windows")]
if let Some(idx) = phys_if_for_bypass {
if let Err(e) = crate::tunnel::proxy::bind_socket_to_interface(
&socket,
remote.is_ipv6(),
idx,
) {
tracing::error!("TUN TCP BYPASS failed to bind to physical interface {}: {}", idx, e);
} else {
if debug {
tracing::info!("TUN TCP BYPASS bound to physical interface {}", idx);
}
}
} else {
tracing::warn!("TUN TCP BYPASS has no physical interface index!");
}
#[cfg(target_os = "linux")]
if let Some(ref name) = lin_name {
let _ = crate::tunnel::proxy::bind_socket_to_interface(&socket, name);
}
match tokio::time::timeout(
std::time::Duration::from_secs(10),
socket.connect(remote),
)
.await
{
Ok(Ok(mut direct)) => {
if sniff_len > 0 {
if direct.write_all(&sniff_buf[..sniff_len]).await.is_err() {
return;
}
}
let _ = tokio::io::copy_bidirectional(&mut stream, &mut direct).await;
}
_ => {
tracing::debug!("Direct bypass connect to {remote} failed");
}
}
return;
}
// ── Tunnel path: forward via local OSTP SOCKS5 proxy ──────────
let Ok(mut socks) = tokio::net::TcpStream::connect(&proxy_addr).await else {
return;
};
// SOCKS5 handshake (no auth)
if socks.write_all(&[5, 1, 0]).await.is_err() { return; }
let mut buf2 = [0u8; 2];
if socks.read_exact(&mut buf2).await.is_err() || buf2[0] != 5 || buf2[1] != 0 {
return;
}
// CONNECT request
let mut req = vec![5u8, 1, 0];
match remote.ip() {
std::net::IpAddr::V4(v4) => {
req.push(1);
req.extend_from_slice(&v4.octets());
}
std::net::IpAddr::V6(v6) => {
req.push(4);
req.extend_from_slice(&v6.octets());
}
}
req.extend_from_slice(&remote.port().to_be_bytes());
if socks.write_all(&req).await.is_err() { return; }
let mut rep = [0u8; 10];
if socks.read_exact(&mut rep).await.is_err() || rep[1] != 0 { return; }
// Replay sniffed bytes
if sniff_len > 0 && socks.write_all(&sniff_buf[..sniff_len]).await.is_err() {
return;
}
let _ = tokio::io::copy_bidirectional(&mut stream, &mut socks).await;
});
}
});
tracing::info!("NATIVE TUN tunnel active.");
tokio::select! {
_ = shutdown.changed() => {}
_ = &mut runner_task => {}
_ = &mut tun_to_stack => {}
_ = &mut stack_to_tun => {}
_ = &mut udp_proxy_task => {}
_ = &mut tcp_accept_task => {}
}
tracing::info!("Deactivating NATIVE TUN tunnel...");
// ── Cleanup ───────────────────────────────────────────────────────────────
// Cleanup is handled automatically by the _route_guard Drop trait in ostp-tun
Ok(())
}
// ──────────────────────────────────────────────────────────────────────────────
// Stub for unsupported platforms
// ──────────────────────────────────────────────────────────────────────────────
#[cfg(not(any(target_os = "windows", target_os = "linux")))]
pub async fn run_native_tunnel(
_config: crate::config::ClientConfig,
_shutdown: watch::Receiver<bool>,
_exclusions_rx: watch::Receiver<crate::config::ExclusionConfig>,
) -> Result<()> {
Err(anyhow!("Native TUN tunnel is only supported on Windows/Linux"))
}
// ──────────────────────────────────────────────────────────────────────────────
// Android: TUN from file-descriptor (opened by VpnService)
// ──────────────────────────────────────────────────────────────────────────────
#[cfg(target_os = "android")]
pub async fn run_native_tunnel_from_fd(
config: crate::config::ClientConfig,
mut shutdown: watch::Receiver<bool>,
mut exclusions_rx: watch::Receiver<crate::config::ExclusionConfig>,
fd: i32,
) -> Result<()> {
use netstack_smoltcp::StackBuilder;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use futures::{StreamExt, SinkExt};
use std::os::unix::io::{FromRawFd, AsRawFd};
let debug = config.debug;
tracing::info!("Initializing NATIVE TUN tunnel on Android (FD {})", fd);
unsafe {
let flags = libc::fcntl(fd, libc::F_GETFL);
if flags >= 0 {
libc::fcntl(fd, libc::F_SETFL, flags | libc::O_NONBLOCK);
}
}
let read_fd = unsafe { libc::dup(fd) };
if read_fd < 0 {
return Err(anyhow!("Failed to dup tun fd for reading"));
}
let file = unsafe { std::fs::File::from_raw_fd(read_fd) };
let tun_stream = tokio::io::unix::AsyncFd::new(file)?;
let (stack, tcp_runner, udp_socket, tcp_listener) = StackBuilder::default()
.stack_buffer_size(1024)
.tcp_buffer_size(1024)
.udp_buffer_size(1024)
.enable_tcp(true)
.enable_udp(true)
.mtu(config.ostp.mtu)
.build()?;
let mut runner_task = tokio::spawn(async move {
if let Some(runner) = tcp_runner {
let _ = runner.await;
}
});
let (mut stack_sink, mut stack_stream) = stack.split();
let _tun_to_stack = tokio::spawn(async move {
let mut buf = vec![0u8; 65536];
loop {
let mut guard = match tun_stream.readable().await {
Ok(g) => g,
Err(_) => break,
};
let n = match guard.try_io(|inner| {
let res = unsafe {
libc::read(
inner.as_raw_fd(),
buf.as_mut_ptr() as *mut libc::c_void,
buf.len(),
)
};
if res < 0 {
let err = std::io::Error::last_os_error();
if err.kind() == std::io::ErrorKind::WouldBlock {
Err(err)
} else {
Ok(0_isize)
}
} else {
Ok(res)
}
}) {
Ok(Ok(n)) if n > 0 => n as usize,
Ok(Ok(_)) => continue,
Ok(Err(_)) => continue,
Err(_) => continue,
};
let frame = buf[..n].to_vec();
if let Err(e) = stack_sink.send(frame).await {
if e.kind() == std::io::ErrorKind::BrokenPipe {
break;
}
}
}
});
let write_fd = unsafe { libc::dup(fd) };
if write_fd < 0 {
return Err(anyhow!("Failed to dup tun fd for writing"));
}
unsafe {
let flags = libc::fcntl(write_fd, libc::F_GETFL);
if flags >= 0 {
libc::fcntl(write_fd, libc::F_SETFL, flags | libc::O_NONBLOCK);
}
}
let write_file = unsafe { std::fs::File::from_raw_fd(write_fd) };
let tun_write_stream = tokio::io::unix::AsyncFd::new(write_file)?;
let _stack_to_tun = tokio::spawn(async move {
while let Some(Ok(frame)) = stack_stream.next().await {
let mut written = 0;
while written < frame.len() {
let mut guard = match tun_write_stream.writable().await {
Ok(g) => g,
Err(_) => break,
};
let res = guard.try_io(|inner| {
let res = unsafe {
libc::write(
inner.as_raw_fd(),
frame[written..].as_ptr() as *const libc::c_void,
frame.len() - written,
)
};
if res < 0 {
let err = std::io::Error::last_os_error();
if err.kind() == std::io::ErrorKind::WouldBlock {
Err(err)
} else {
Ok(res)
}
} else {
Ok(res)
}
});
match res {
Ok(Ok(n)) if n > 0 => written += n as usize,
Ok(Ok(_)) => break,
Ok(Err(_)) => break,
Err(_) => continue,
}
}
}
});
let mut proxy_addr = config.local_proxy.bind_addr.clone();
if proxy_addr.starts_with("0.0.0.0:") {
proxy_addr = proxy_addr.replace("0.0.0.0:", "127.0.0.1:");
}
let current_exclusions = exclusions_rx.borrow().clone();
let matcher = crate::tunnel::exclusion::ExclusionMatcher::new(&current_exclusions, None, None);
let matcher_arc = std::sync::Arc::new(tokio::sync::RwLock::new(matcher));
let matcher_clone = matcher_arc.clone();
tokio::spawn(async move {
while let Ok(_) = exclusions_rx.changed().await {
let current = exclusions_rx.borrow().clone();
let new_matcher = crate::tunnel::exclusion::ExclusionMatcher::new(&current, None, None);
*matcher_clone.write().await = new_matcher;
if true {
tracing::debug!("Android TUN exclusions hot-reloaded");
}
}
});
let udp_proxy_addr = proxy_addr.clone();
let debug_udp = debug;
let udp_matcher = matcher_arc.clone();
let mut udp_proxy_task = tokio::spawn(async move {
if let Some(udp_sock) = udp_socket {
super::udp_nat::run_udp_nat(udp_sock, udp_proxy_addr, debug_udp, udp_matcher, None, None).await;
}
});
let mut tcp_accept_task = tokio::spawn(async move {
let Some(mut listener) = tcp_listener else { return; };
while let Some((mut stream, local, remote)) = listener.next().await {
let proxy_addr = proxy_addr.clone();
let matcher_arc = matcher_arc.clone();
tokio::spawn(async move {
let matcher = matcher_arc.read().await.clone();
if true {
tracing::debug!("Android TUN TCP {local} → {remote}");
}
// Sniff SNI
let mut sniff_buf = [0u8; 2048];
let sniff_len =
match tokio::time::timeout(
std::time::Duration::from_millis(100),
stream.read(&mut sniff_buf),
)
.await
{
Ok(Ok(n)) => n,
_ => 0,
};
let mut should_bypass = false;
// 1. SNI domain
if sniff_len > 0 {
if let Some(sni) =
crate::tunnel::sni_sniff::extract_sni(&sniff_buf[..sniff_len])
{
if true { tracing::debug!("Android TUN SNI: {sni}"); }
if matcher.match_domain(&sni) {
should_bypass = true;
}
}
}
// 2. Process (Android: /proc/net lookup)
if !should_bypass {
if let Some(exe) =
crate::tunnel::process_lookup::get_process_name_from_port(local.port())
{
if true {
tracing::debug!("Android TUN port {} → EXE: {}", local.port(), exe);
}
if matcher.match_process(&exe) {
should_bypass = true;
}
}
}
// 3. IP CIDR
if !should_bypass && matcher.match_ip(&remote.ip()) {
should_bypass = true;
}
// Bypass: connect directly (Android VPN service already protects the socket
// from re-entering the TUN through VpnService.protect())
if should_bypass {
if true {
tracing::debug!("Android TUN BYPASS: {remote}");
}
let socket = match remote {
std::net::SocketAddr::V4(_) => tokio::net::TcpSocket::new_v4(),
std::net::SocketAddr::V6(_) => tokio::net::TcpSocket::new_v6(),
};
let Ok(socket) = socket else { return; };
match tokio::time::timeout(
std::time::Duration::from_secs(10),
socket.connect(remote),
)
.await
{
Ok(Ok(mut direct)) => {
if sniff_len > 0 {
if direct.write_all(&sniff_buf[..sniff_len]).await.is_err() {
return;
}
}
let _ = tokio::io::copy_bidirectional(&mut stream, &mut direct).await;
}
_ => {
tracing::debug!("Android bypass connect to {remote} failed");
}
}
return;
}
// Tunnel via SOCKS5 proxy
let Ok(mut socks) = tokio::net::TcpStream::connect(&proxy_addr).await else {
return;
};
if socks.write_all(&[5, 1, 0]).await.is_err() { return; }
let mut buf2 = [0u8; 2];
if socks.read_exact(&mut buf2).await.is_err() || buf2[0] != 5 || buf2[1] != 0 {
return;
}
let mut req = vec![5u8, 1, 0];
match remote.ip() {
std::net::IpAddr::V4(v4) => {
req.push(1);
req.extend_from_slice(&v4.octets());
}
std::net::IpAddr::V6(v6) => {
req.push(4);
req.extend_from_slice(&v6.octets());
}
}
req.extend_from_slice(&remote.port().to_be_bytes());
if socks.write_all(&req).await.is_err() { return; }
let mut rep = [0u8; 10];
if socks.read_exact(&mut rep).await.is_err() || rep[1] != 0 { return; }
if sniff_len > 0 && socks.write_all(&sniff_buf[..sniff_len]).await.is_err() {
return;
}
let _ = tokio::io::copy_bidirectional(&mut stream, &mut socks).await;
});
}
});
tracing::info!("NATIVE TUN (Android) tunnel active.");
tokio::select! {
_ = shutdown.changed() => {}
_ = &mut runner_task => {}
_ = _tun_to_stack => {}
_ = _stack_to_tun => {}
_ = &mut udp_proxy_task => {}
_ = &mut tcp_accept_task => {}
}
tracing::info!("NATIVE TUN (Android) deactivated.");
Ok(())
}
#[cfg(not(target_os = "android"))]
pub async fn run_native_tunnel_from_fd(
_config: crate::config::ClientConfig,
_shutdown: watch::Receiver<bool>,
_exclusions_rx: watch::Receiver<crate::config::ExclusionConfig>,
_fd: i32,
) -> Result<()> {
Err(anyhow!("Native TUN from FD is only supported on Android"))
}

View File

@ -0,0 +1,194 @@
#[cfg(target_os = "windows")]
pub fn get_process_name_from_port(port: u16) -> Option<String> {
use winapi::shared::minwindef::ULONG;
use winapi::shared::winerror::ERROR_INSUFFICIENT_BUFFER;
use winapi::um::iphlpapi::GetExtendedTcpTable;
use winapi::shared::tcpmib::{MIB_TCPTABLE_OWNER_PID, MIB_TCPROW_OWNER_PID};
let mut size: ULONG = 0;
let table_class = 5; // TCP_TABLE_OWNER_PID_ALL
let mut table = vec![0u8; 1024];
unsafe {
let mut ret = GetExtendedTcpTable(
table.as_mut_ptr() as *mut _,
&mut size,
0,
2, // AF_INET
table_class,
0,
);
if ret == ERROR_INSUFFICIENT_BUFFER {
table.resize(size as usize, 0);
ret = GetExtendedTcpTable(
table.as_mut_ptr() as *mut _,
&mut size,
0,
2, // AF_INET
table_class,
0,
);
}
if ret == 0 {
let tcp_table = &*(table.as_ptr() as *const MIB_TCPTABLE_OWNER_PID);
let row_ptr = &tcp_table.table[0] as *const MIB_TCPROW_OWNER_PID;
for i in 0..tcp_table.dwNumEntries {
let row = &*row_ptr.add(i as usize);
// Local port is in network byte order
let local_port = u16::from_be(row.dwLocalPort as u16);
if local_port == port {
return get_process_name_from_pid(row.dwOwningPid);
}
}
}
}
None
}
#[cfg(target_os = "windows")]
pub fn get_process_name_from_port_udp(port: u16) -> Option<String> {
use winapi::shared::minwindef::ULONG;
use winapi::shared::winerror::ERROR_INSUFFICIENT_BUFFER;
use winapi::um::iphlpapi::GetExtendedUdpTable;
use winapi::shared::udpmib::{MIB_UDPTABLE_OWNER_PID, MIB_UDPROW_OWNER_PID};
let mut size: ULONG = 0;
let table_class = 1; // UDP_TABLE_OWNER_PID
let mut table = vec![0u8; 1024];
unsafe {
let mut ret = GetExtendedUdpTable(
table.as_mut_ptr() as *mut _,
&mut size,
0,
2, // AF_INET
table_class,
0,
);
if ret == ERROR_INSUFFICIENT_BUFFER {
table.resize(size as usize, 0);
ret = GetExtendedUdpTable(
table.as_mut_ptr() as *mut _,
&mut size,
0,
2, // AF_INET
table_class,
0,
);
}
if ret == 0 {
let udp_table = &*(table.as_ptr() as *const MIB_UDPTABLE_OWNER_PID);
let row_ptr = &udp_table.table[0] as *const MIB_UDPROW_OWNER_PID;
for i in 0..udp_table.dwNumEntries {
let row = &*row_ptr.add(i as usize);
let local_port = u16::from_be(row.dwLocalPort as u16);
if local_port == port {
return get_process_name_from_pid(row.dwOwningPid);
}
}
}
}
None
}
#[cfg(target_os = "windows")]
fn get_process_name_from_pid(pid: u32) -> Option<String> {
use winapi::um::processthreadsapi::OpenProcess;
use winapi::um::psapi::GetModuleBaseNameW;
use winapi::um::winnt::{PROCESS_QUERY_INFORMATION, PROCESS_VM_READ};
use winapi::um::handleapi::CloseHandle;
use std::os::windows::ffi::OsStringExt;
unsafe {
let handle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, 0, pid);
if handle.is_null() {
return None;
}
let mut buffer = [0u16; 1024];
let len = GetModuleBaseNameW(handle, std::ptr::null_mut(), buffer.as_mut_ptr(), buffer.len() as u32);
CloseHandle(handle);
if len > 0 {
let name = std::ffi::OsString::from_wide(&buffer[..len as usize]);
return Some(name.to_string_lossy().into_owned());
}
}
None
}
#[cfg(target_os = "linux")]
pub fn get_process_name_from_port(port: u16) -> Option<String> {
use std::fs;
use std::io::{BufRead, BufReader};
let hex_port = format!("{:04X}", port);
let check_net_file = |path: &str| -> Option<u64> {
let file = fs::File::open(path).ok()?;
let reader = BufReader::new(file);
for line in reader.lines().skip(1).filter_map(Result::ok) {
let parts: Vec<&str> = line.split_whitespace().collect();
if parts.len() >= 10 {
let local_addr = parts[1];
if local_addr.ends_with(&format!(":{}", hex_port)) {
if let Ok(inode) = parts[9].parse::<u64>() {
return Some(inode);
}
}
}
}
None
};
let target_inode = check_net_file("/proc/net/tcp")
.or_else(|| check_net_file("/proc/net/tcp6"))
.or_else(|| check_net_file("/proc/net/udp"))
.or_else(|| check_net_file("/proc/net/udp6"));
let target_inode = target_inode?;
let socket_str = format!("socket:[{}]", target_inode);
for entry in fs::read_dir("/proc").ok()?.filter_map(Result::ok) {
let file_name = entry.file_name();
let pid_str = file_name.to_string_lossy();
if !pid_str.chars().all(char::is_numeric) {
continue;
}
let fd_dir = entry.path().join("fd");
if let Ok(fd_entries) = fs::read_dir(fd_dir) {
for fd_entry in fd_entries.filter_map(Result::ok) {
if let Ok(target) = fs::read_link(fd_entry.path()) {
if target.to_string_lossy() == socket_str {
let exe_path = entry.path().join("exe");
if let Ok(exe_link) = fs::read_link(exe_path) {
if let Some(name) = exe_link.file_name() {
return Some(name.to_string_lossy().into_owned());
}
}
if let Ok(comm) = fs::read_to_string(entry.path().join("comm")) {
return Some(comm.trim().to_string());
}
}
}
}
}
}
None
}
#[cfg(not(any(target_os = "windows", target_os = "linux")))]
pub fn get_process_name_from_port(_port: u16) -> Option<String> {
None
}
#[cfg(not(target_os = "windows"))]
pub fn get_process_name_from_port_udp(port: u16) -> Option<String> {
get_process_name_from_port(port)
}

View File

@ -1,37 +1,232 @@
use std::collections::HashMap; use std::collections::HashMap;
use crate::tunnel::exclusion::ExclusionMatcher;
use anyhow::{anyhow, Context, Result}; use anyhow::{anyhow, Context, Result};
use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::{TcpListener, TcpStream}; use tokio::net::{TcpListener, TcpStream, UdpSocket};
use std::sync::Arc;
use tokio::sync::{mpsc, watch}; use tokio::sync::{mpsc, watch};
use tokio::time::{timeout, Duration}; use tokio::time::{timeout, Duration};
use crate::config::{ExclusionConfig, LocalProxyConfig, OstpConfig}; use crate::config::{ExclusionConfig, LocalProxyConfig, OstpConfig};
use crate::tunnel::{ProxyEvent, ProxyToClientMsg}; use crate::tunnel::{ProxyEvent, ProxyToClientMsg};
#[cfg(target_os = "windows")]
use std::os::windows::io::AsRawSocket;
#[cfg(target_os = "linux")]
use std::os::fd::AsRawFd;
#[cfg(target_os = "windows")]
#[link(name = "ws2_32")]
extern "system" {
fn setsockopt(
s: usize,
level: i32,
optname: i32,
optval: *const u8,
optlen: i32,
) -> i32;
}
#[cfg(target_os = "windows")]
pub fn bind_socket_to_interface(socket: &impl AsRawSocket, is_ipv6: bool, if_index: u32) -> std::io::Result<()> {
let s = socket.as_raw_socket() as usize;
if is_ipv6 {
// IPV6_UNICAST_IF expects interface index in host byte order
let optval = if_index;
let ret = unsafe {
setsockopt(
s,
41, // IPPROTO_IPV6
31, // IPV6_UNICAST_IF
&optval as *const u32 as *const u8,
4,
)
};
if ret != 0 {
return Err(std::io::Error::last_os_error());
}
} else {
// IP_UNICAST_IF expects interface index in NETWORK byte order (big-endian)
let optval = if_index.to_be();
let ret = unsafe {
setsockopt(
s,
0, // IPPROTO_IP
31, // IP_UNICAST_IF
&optval as *const u32 as *const u8,
4,
)
};
if ret != 0 {
return Err(std::io::Error::last_os_error());
}
}
Ok(())
}
#[cfg(target_os = "linux")]
pub fn bind_socket_to_interface(socket: &impl AsRawFd, if_name: &str) -> std::io::Result<()> {
let fd = socket.as_raw_fd();
let mut if_name_bytes = if_name.as_bytes().to_vec();
if_name_bytes.push(0);
let ret = unsafe {
libc::setsockopt(
fd,
libc::SOL_SOCKET,
libc::SO_BINDTODEVICE,
if_name_bytes.as_ptr() as *const std::ffi::c_void,
if_name_bytes.len() as libc::socklen_t,
)
};
if ret != 0 {
return Err(std::io::Error::last_os_error());
}
Ok(())
}
pub fn get_windows_physical_if_index() -> Option<u32> {
#[cfg(target_os = "windows")]
{
return ostp_tun::windows::windows_route::sys::get_default_ipv4_route().map(|(_, idx)| idx);
}
#[cfg(not(target_os = "windows"))]
{
None
}
}
pub fn get_linux_physical_if_name() -> Option<String> {
#[cfg(target_os = "linux")]
{
let output = std::process::Command::new("ip")
.args(["route", "show", "default"])
.output()
.ok()?;
if output.status.success() {
let s = String::from_utf8_lossy(&output.stdout);
if let Some(dev_part) = s.split_whitespace().skip_while(|w| *w != "dev").nth(1) {
return Some(dev_part.to_string());
}
}
}
None
}
#[allow(unused_variables)]
async fn connect_bypassing_tun(
target: &str,
physical_if_index: Option<u32>,
_physical_if_name: &Option<String>,
) -> Result<TcpStream> {
let resolved = tokio::net::lookup_host(target).await
.with_context(|| format!("failed to resolve host for bypass connect: {target}"))?;
let mut last_err = None;
for addr in resolved {
let socket = if addr.is_ipv6() {
let s = tokio::net::TcpSocket::new_v6()?;
let _ = s.bind("[::]:0".parse().unwrap());
s
} else {
let s = tokio::net::TcpSocket::new_v4()?;
let _ = s.bind("0.0.0.0:0".parse().unwrap());
s
};
#[cfg(target_os = "windows")]
if let Some(if_index) = physical_if_index {
if let Err(e) = bind_socket_to_interface(&socket, addr.is_ipv6(), if_index) {
tracing::warn!("Failed to bind TCP socket to interface {}: {}", if_index, e);
}
}
#[cfg(target_os = "linux")]
if let Some(ref if_name) = _physical_if_name {
if let Err(e) = bind_socket_to_interface(&socket, if_name) {
tracing::warn!("Failed to bind TCP socket to interface {}: {}", if_name, e);
}
}
match socket.connect(addr).await {
Ok(stream) => return Ok(stream),
Err(e) => {
last_err = Some(e);
}
}
}
Err(anyhow!(
"direct connect failed: {:?}",
last_err.map(|e| e.to_string()).unwrap_or_else(|| "no addresses resolved".to_string())
))
}
#[allow(unused_variables)]
async fn create_udp_socket_bypassing_tun(
is_ipv6: bool,
physical_if_index: Option<u32>,
_physical_if_name: &Option<String>,
) -> Result<UdpSocket> {
let addr: std::net::SocketAddr = if is_ipv6 {
"[::]:0".parse().unwrap()
} else {
"0.0.0.0:0".parse().unwrap()
};
let socket = UdpSocket::bind(addr).await
.with_context(|| format!("failed to bind direct UdpSocket to wildcard {}", addr))?;
#[cfg(target_os = "windows")]
if let Some(if_index) = physical_if_index {
if let Err(e) = bind_socket_to_interface(&socket, is_ipv6, if_index) {
tracing::warn!("Failed to bind UDP socket to interface index {}: {}", if_index, e);
}
}
#[cfg(target_os = "linux")]
if let Some(ref if_name) = _physical_if_name {
if let Err(e) = bind_socket_to_interface(&socket, if_name) {
tracing::warn!("Failed to bind UDP socket to interface {}: {}", if_name, e);
}
}
Ok(socket)
}
pub async fn run_local_socks5_proxy( pub async fn run_local_socks5_proxy(
cfg: LocalProxyConfig, cfg: LocalProxyConfig,
_ostp: OstpConfig, ostp: OstpConfig,
exclusions: ExclusionConfig, mut exclusions_rx: watch::Receiver<ExclusionConfig>,
debug: bool, debug: bool,
mut shutdown: watch::Receiver<bool>, mut shutdown: watch::Receiver<bool>,
proxy_events_tx: mpsc::Sender<ProxyEvent>, proxy_events_tx: mpsc::Sender<ProxyEvent>,
mut client_msgs_rx: mpsc::Receiver<(u16, ProxyToClientMsg)>, mut client_msgs_rx: mpsc::UnboundedReceiver<(u16, ProxyToClientMsg)>,
) -> Result<()> { ) -> Result<()> {
let connect_timeout = Duration::from_millis(cfg.connect_timeout_ms.max(1)); let connect_timeout = Duration::from_millis(cfg.connect_timeout_ms.max(1));
let listener = TcpListener::bind(&cfg.bind_addr) let listener = TcpListener::bind(&cfg.bind_addr)
.await .await
.with_context(|| format!("failed to bind local HTTP/SOCKS5 proxy at {}", cfg.bind_addr))?; .with_context(|| format!("failed to bind local HTTP/SOCKS5 proxy at {}", cfg.bind_addr))?;
if debug { tracing::info!("local HTTP/SOCKS5 proxy listening at {}", cfg.bind_addr);
eprintln!("[ostp-client] local HTTP/SOCKS5 proxy listening at {}", cfg.bind_addr);
eprintln!("[ostp-client] Windows system proxy: set HTTP proxy to {}. tun2socks: SOCKS5 on same address.", cfg.bind_addr); let physical_if_index = tokio::task::spawn_blocking(get_windows_physical_if_index).await.unwrap_or(None);
let physical_if_name = tokio::task::spawn_blocking(get_linux_physical_if_name).await.unwrap_or(None);
if physical_if_index.is_some() {
tracing::info!("Local proxy physical interface index: {:?}", physical_if_index);
}
if physical_if_name.is_some() {
tracing::info!("Local proxy physical interface name: {:?}", physical_if_name);
} }
let matcher = ExclusionMatcher::new(&exclusions); let mut current_exclusions = exclusions_rx.borrow().clone();
let mut matcher = ExclusionMatcher::new(&current_exclusions, physical_if_index, physical_if_name.clone());
let (connect_tx, mut connect_rx) = mpsc::channel(128); let (connect_tx, mut connect_rx) = mpsc::channel(128);
let max_chunk = ostp.mtu.saturating_sub(150).max(512);
let mut next_stream_id: u16 = 1; let mut next_stream_id: u16 = 1;
let mut active_streams: HashMap<u16, mpsc::Sender<ProxyToClientMsg>> = HashMap::new(); let mut active_streams: HashMap<u16, mpsc::UnboundedSender<ProxyToClientMsg>> = HashMap::new();
loop { loop {
tokio::select! { tokio::select! {
@ -40,13 +235,24 @@ pub async fn run_local_socks5_proxy(
break; break;
} }
} }
Ok(_) = exclusions_rx.changed() => {
current_exclusions = exclusions_rx.borrow().clone();
matcher = ExclusionMatcher::new(&current_exclusions, physical_if_index, physical_if_name.clone());
if true {
tracing::info!("Local proxy exclusions hot-reloaded");
}
}
accepted = listener.accept() => { accepted = listener.accept() => {
let (socket, _) = accepted?; let (socket, _) = accepted?;
let stream_id = next_stream_id; let stream_id = next_stream_id;
next_stream_id = next_stream_id.wrapping_add(1); // Advance, skipping zero and any stream_id still in active_streams
if next_stream_id == 0 { next_stream_id = 1; } loop {
next_stream_id = next_stream_id.wrapping_add(1);
if next_stream_id == 0 { next_stream_id = 1; }
if !active_streams.contains_key(&next_stream_id) { break; }
}
let (tx, rx) = mpsc::channel(256); let (tx, rx) = mpsc::unbounded_channel();
active_streams.insert(stream_id, tx); active_streams.insert(stream_id, tx);
let event_tx = proxy_events_tx.clone(); let event_tx = proxy_events_tx.clone();
@ -62,23 +268,32 @@ pub async fn run_local_socks5_proxy(
connect_timeout, connect_timeout,
debug, debug,
matcher_clone, matcher_clone,
max_chunk,
).await { ).await {
let msg = err.to_string(); let msg = err.to_string();
// Suppress routine disconnects from spam logs // Suppress routine disconnects and unsupported SOCKS5 command attempts (like UDP) from spam logs
if !msg.contains("UnexpectedEof") if !msg.contains("UnexpectedEof")
&& !msg.contains("Connection reset") && !msg.contains("Connection reset")
&& !msg.contains("Broken pipe") && !msg.contains("Broken pipe")
{ && !msg.contains("unsupported SOCKS5 command")
if debug { && debug {
eprintln!("[ostp-client] proxy client error: {err}"); tracing::warn!("proxy client error: {err}");
} }
}
} }
}); });
} }
Some((stream_id, msg)) = client_msgs_rx.recv() => { Some((stream_id, msg)) = client_msgs_rx.recv() => {
if let Some(tx) = active_streams.get(&stream_id) { if stream_id == 0 {
if tx.send(msg).await.is_err() { if let ProxyToClientMsg::Close = msg {
if true {
tracing::info!("Resetting all active proxy streams on reconnect");
}
for (_, tx) in active_streams.drain() {
let _ = tx.send(ProxyToClientMsg::Close);
}
}
} else if let Some(tx) = active_streams.get(&stream_id) {
if tx.send(msg).is_err() {
active_streams.remove(&stream_id); active_streams.remove(&stream_id);
} }
} }
@ -111,16 +326,275 @@ fn extract_host_port(uri: &str, default_port: u16) -> String {
} }
} }
struct StreamGuard {
stream_id: u16,
close_tx: mpsc::Sender<u16>,
}
impl Drop for StreamGuard {
fn drop(&mut self) {
let tx = self.close_tx.clone();
let id = self.stream_id;
tokio::spawn(async move {
let _ = tx.send(id).await;
});
}
}
async fn handle_udp_associate(
mut client_tcp: TcpStream,
udp_socket: tokio::net::UdpSocket,
stream_id: u16,
event_tx: mpsc::Sender<ProxyEvent>,
mut rx: mpsc::UnboundedReceiver<ProxyToClientMsg>,
close_tx: mpsc::Sender<u16>,
debug: bool,
matcher: ExclusionMatcher,
connect_timeout: Duration,
) -> Result<()> {
let client_udp_addr = Arc::new(std::sync::Mutex::new(None));
let mut buf = vec![0u8; 65536];
let udp_socket = Arc::new(udp_socket);
let sock_rx = udp_socket.clone();
let sock_tx = udp_socket;
let mut direct_udp_v4: Option<Arc<UdpSocket>> = None;
let mut direct_udp_v6: Option<Arc<UdpSocket>> = None;
// Held only to keep the direct-UDP readers' cancellation senders alive;
// dropping this (on every return path from this function) is what tells
// spawn_direct_udp_reader's tasks to stop. See its doc comment.
let mut direct_udp_cancel_txs: Vec<tokio::sync::oneshot::Sender<()>> = Vec::new();
let mut tcp_buf = [0u8; 1];
loop {
tokio::select! {
res = client_tcp.read(&mut tcp_buf) => {
match res {
Ok(0) | Err(_) => break,
Ok(_) => {}
}
}
res = sock_rx.recv_from(&mut buf) => {
let (len, addr) = match res {
Ok(v) => v,
Err(e) => {
tracing::debug!("udp_associate recv_from error: {}", e);
continue; // transient error, don't kill the session
}
};
{
let mut guard = client_udp_addr.lock().unwrap();
if guard.is_none() {
*guard = Some(addr);
}
}
if len < 4 { continue; }
let frag = buf[2];
if frag != 0 { continue; } // Fragmented UDP not supported
let atyp = buf[3];
let (header_len, target) = match atyp {
0x01 => {
if len < 10 { continue; }
let ip = std::net::Ipv4Addr::new(buf[4], buf[5], buf[6], buf[7]);
let port = u16::from_be_bytes([buf[8], buf[9]]);
(10, format!("{}:{}", ip, port))
}
0x03 => {
if len < 5 { continue; }
let domain_len = buf[4] as usize;
if len < 5 + domain_len + 2 { continue; }
let domain = String::from_utf8_lossy(&buf[5..5+domain_len]);
let port = u16::from_be_bytes([buf[5+domain_len], buf[5+domain_len+1]]);
(5 + domain_len + 2, format!("{}:{}", domain, port))
}
0x04 => {
if len < 22 { continue; }
let mut octets = [0u8; 16];
octets.copy_from_slice(&buf[4..20]);
let ip = std::net::Ipv6Addr::from(octets);
let port = u16::from_be_bytes([buf[20], buf[21]]);
(22, format!("[{}]:{}", ip, port))
}
_ => continue,
};
let payload = bytes::Bytes::copy_from_slice(&buf[header_len..len]);
let target_host = if let Some((host, _)) = split_host_port(&target) { host } else { target.clone() };
let target_port = match split_host_port(&target) { Some((_, p)) => p, None => 0 };
// Check if target should bypass the tunnel
if matcher.should_bypass_target(&target_host, target_port, connect_timeout).await {
if true {
tracing::debug!("proxy UDP BYPASS target={}", target);
}
// Resolve target to find if it is IPv4 or IPv6
if let Ok(resolved_addrs) = tokio::net::lookup_host(&target).await {
if let Some(target_addr) = resolved_addrs.into_iter().next() {
let is_ipv6 = target_addr.is_ipv6();
let direct_socket = if is_ipv6 {
if direct_udp_v6.is_none() {
match create_udp_socket_bypassing_tun(true, matcher.physical_if_index, &matcher.physical_if_name).await {
Ok(s) => {
let s_arc = Arc::new(s);
let (cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
spawn_direct_udp_reader(s_arc.clone(), sock_tx.clone(), client_udp_addr.clone(), debug, cancel_rx);
direct_udp_cancel_txs.push(cancel_tx);
direct_udp_v6 = Some(s_arc);
}
Err(e) => {
tracing::error!("Failed to create bypass UDP v6 socket: {}", e);
}
}
}
&direct_udp_v6
} else {
if direct_udp_v4.is_none() {
match create_udp_socket_bypassing_tun(false, matcher.physical_if_index, &matcher.physical_if_name).await {
Ok(s) => {
let s_arc = Arc::new(s);
let (cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
spawn_direct_udp_reader(s_arc.clone(), sock_tx.clone(), client_udp_addr.clone(), debug, cancel_rx);
direct_udp_cancel_txs.push(cancel_tx);
direct_udp_v4 = Some(s_arc);
}
Err(e) => {
tracing::error!("Failed to create bypass UDP v4 socket: {}", e);
}
}
}
&direct_udp_v4
};
if let Some(s) = direct_socket {
if let Err(e) = s.send_to(&payload, target_addr).await {
if true {
tracing::warn!("failed to send bypass UDP packet to {}: {}", target_addr, e);
}
}
}
}
}
} else {
tracing::debug!("proxy.rs forwarding UDP DATA to server for target={} payload len={}", target, payload.len());
let _ = event_tx.send(ProxyEvent::UdpData { stream_id, target, payload }).await;
}
}
msg = rx.recv() => {
match msg {
Some(ProxyToClientMsg::UdpData(target, data)) => {
if let Some(client_addr) = {
let guard = client_udp_addr.lock().unwrap();
*guard
} {
let mut packet = vec![0x00, 0x00, 0x00];
let mut parts = target.rsplitn(2, ':');
let port_str = parts.next().unwrap_or("0");
let host_str = parts.next().unwrap_or(&target);
let host_str = host_str.trim_start_matches('[').trim_end_matches(']');
let port = port_str.parse::<u16>().unwrap_or(0);
if let Ok(ipv4) = host_str.parse::<std::net::Ipv4Addr>() {
packet.push(0x01);
packet.extend_from_slice(&ipv4.octets());
} else if let Ok(ipv6) = host_str.parse::<std::net::Ipv6Addr>() {
packet.push(0x04);
packet.extend_from_slice(&ipv6.octets());
} else {
packet.push(0x03);
let bytes = host_str.as_bytes();
packet.push(bytes.len() as u8);
packet.extend_from_slice(bytes);
}
packet.extend_from_slice(&port.to_be_bytes());
packet.extend_from_slice(&data);
tracing::debug!("proxy.rs forwarding UDP REPLY to client_addr={} from server for target={} payload len={}", client_addr, target, data.len());
let _ = sock_tx.send_to(&packet, client_addr).await;
} else {
tracing::error!("proxy.rs failed to parse target string as SocketAddr: {}", target);
}
}
Some(ProxyToClientMsg::Close) | Some(ProxyToClientMsg::Error(_)) | None => break,
_ => {}
}
}
}
}
let _ = close_tx.send(stream_id).await;
Ok(())
}
fn spawn_direct_udp_reader(
direct_socket: Arc<UdpSocket>,
sock_tx: Arc<UdpSocket>,
client_udp_addr: Arc<std::sync::Mutex<Option<std::net::SocketAddr>>>,
_debug: bool,
mut cancel_rx: tokio::sync::oneshot::Receiver<()>,
) {
tokio::spawn(async move {
let mut buf = vec![0u8; 65536];
loop {
let recv_result = tokio::select! {
// Fires as soon as the sender half (held by handle_udp_associate
// for exactly this reason) is dropped - which happens the
// instant that function returns, on every exit path, with no
// explicit signaling needed. Without this, a UDP-associate
// session that ever bypassed traffic direct (excluded IP/
// domain) leaked this socket + task for the rest of the
// process's life once the session ended: nothing else ever
// stopped this loop.
_ = &mut cancel_rx => break,
res = direct_socket.recv_from(&mut buf) => res,
};
match recv_result {
Ok((len, target_addr)) => {
let client_addr = {
let guard = client_udp_addr.lock().unwrap();
*guard
};
if let Some(client_addr) = client_addr {
let mut packet = vec![0x00, 0x00, 0x00];
if let Ok(ipv4) = target_addr.ip().to_string().parse::<std::net::Ipv4Addr>() {
packet.push(0x01);
packet.extend_from_slice(&ipv4.octets());
} else if let Ok(ipv6) = target_addr.ip().to_string().parse::<std::net::Ipv6Addr>() {
packet.push(0x04);
packet.extend_from_slice(&ipv6.octets());
} else {
continue;
}
packet.extend_from_slice(&target_addr.port().to_be_bytes());
packet.extend_from_slice(&buf[..len]);
if let Err(e) = sock_tx.send_to(&packet, client_addr).await {
if true {
tracing::warn!("failed to send direct UDP response to client: {e}");
}
}
}
}
Err(e) => {
if true {
tracing::debug!("direct UDP socket read loop exiting: {e}");
}
break;
}
}
}
});
}
async fn handle_proxy_client( async fn handle_proxy_client(
mut client: TcpStream, mut client: TcpStream,
stream_id: u16, stream_id: u16,
event_tx: mpsc::Sender<ProxyEvent>, event_tx: mpsc::Sender<ProxyEvent>,
mut rx: mpsc::Receiver<ProxyToClientMsg>, mut rx: mpsc::UnboundedReceiver<ProxyToClientMsg>,
close_tx: mpsc::Sender<u16>, close_tx: mpsc::Sender<u16>,
connect_timeout: Duration, connect_timeout: Duration,
debug: bool, debug: bool,
matcher: ExclusionMatcher, matcher: ExclusionMatcher,
max_chunk: usize,
) -> Result<()> { ) -> Result<()> {
let _guard = StreamGuard { stream_id, close_tx: close_tx.clone() };
// Peek the first byte to distinguish SOCKS5 (0x05) from HTTP (any printable ASCII) // Peek the first byte to distinguish SOCKS5 (0x05) from HTTP (any printable ASCII)
let mut first_byte = [0_u8; 1]; let mut first_byte = [0_u8; 1];
client.read_exact(&mut first_byte).await?; client.read_exact(&mut first_byte).await?;
@ -146,8 +620,10 @@ async fn handle_proxy_client(
if req[0] != 0x05 { if req[0] != 0x05 {
return Err(anyhow!("SOCKS5 request version mismatch")); return Err(anyhow!("SOCKS5 request version mismatch"));
} }
if req[1] != 0x01 {
// Not CONNECT — send COMMAND NOT SUPPORTED let is_udp = req[1] == 0x03;
if req[1] != 0x01 && !is_udp {
// Not CONNECT and Not UDP ASSOCIATE — send COMMAND NOT SUPPORTED
client.write_all(&[0x05, 0x07, 0x00, 0x01, 0, 0, 0, 0, 0, 0]).await?; client.write_all(&[0x05, 0x07, 0x00, 0x01, 0, 0, 0, 0, 0, 0]).await?;
return Err(anyhow!("unsupported SOCKS5 command {}", req[1])); return Err(anyhow!("unsupported SOCKS5 command {}", req[1]));
} }
@ -185,11 +661,41 @@ async fn handle_proxy_client(
} }
}; };
if debug { if is_udp {
eprintln!("[ostp-client] proxy CONNECT stream_id={stream_id} target={target}"); if true { tracing::debug!("proxy UDP ASSOCIATE stream_id={stream_id}"); }
let udp_socket = UdpSocket::bind("127.0.0.1:0").await?;
let port = udp_socket.local_addr()?.port();
let mut reply = vec![0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1];
reply.extend_from_slice(&port.to_be_bytes());
client.write_all(&reply).await?;
event_tx.send(ProxyEvent::UdpAssociate { stream_id }).await?;
return handle_udp_associate(
client,
udp_socket,
stream_id,
event_tx,
rx,
close_tx,
debug,
matcher,
connect_timeout,
).await;
} }
if matcher.should_bypass(&target, connect_timeout).await {
return direct_connect_socks5(client, stream_id, &target, close_tx, debug).await; tracing::debug!("proxy CONNECT stream_id={stream_id} target={target}");
let target_host = if let Some((host, _)) = split_host_port(&target) { host } else { target.clone() };
let target_port = match split_host_port(&target) { Some((_, p)) => p, None => 0 };
if matcher.should_bypass_target(&target_host, target_port, connect_timeout).await {
return direct_connect_socks5(
client,
stream_id,
&target,
matcher.physical_if_index,
&matcher.physical_if_name,
close_tx,
debug,
).await;
} }
event_tx.send(ProxyEvent::NewStream { stream_id, target: target.clone() }).await?; event_tx.send(ProxyEvent::NewStream { stream_id, target: target.clone() }).await?;
@ -219,12 +725,18 @@ async fn handle_proxy_client(
// Read the rest of the HTTP request headers byte-by-byte // Read the rest of the HTTP request headers byte-by-byte
let mut header_bytes = Vec::with_capacity(512); let mut header_bytes = Vec::with_capacity(512);
header_bytes.push(first_byte[0]); header_bytes.push(first_byte[0]);
let mut byte = [0_u8; 1]; let mut chunk = [0_u8; 512];
loop { loop {
client.read_exact(&mut byte).await?; let n = client.read(&mut chunk).await?;
header_bytes.push(byte[0]); if n == 0 {
if header_bytes.ends_with(b"\r\n\r\n") { return Err(anyhow!("connection closed during HTTP header read"));
break; }
header_bytes.extend_from_slice(&chunk[..n]);
if header_bytes.len() >= 4 {
let tail = &header_bytes[header_bytes.len().saturating_sub(4)..];
if tail.ends_with(b"\r\n\r\n") {
break;
}
} }
if header_bytes.len() > 8192 { if header_bytes.len() > 8192 {
client.write_all(b"HTTP/1.1 431 Request Header Fields Too Large\r\n\r\n").await?; client.write_all(b"HTTP/1.1 431 Request Header Fields Too Large\r\n\r\n").await?;
@ -256,16 +768,20 @@ async fn handle_proxy_client(
extract_host_port(raw_uri, default_port) extract_host_port(raw_uri, default_port)
}; };
if debug { if true {
eprintln!("[ostp-client] proxy CONNECT stream_id={stream_id} target={target}"); tracing::info!("proxy CONNECT stream_id={stream_id} target={target}");
} }
if matcher.should_bypass(&target, connect_timeout).await { let target_host = if let Some((host, _)) = split_host_port(&target) { host } else { target.clone() };
let target_port = match split_host_port(&target) { Some((_, p)) => p, None => 443 };
if matcher.should_bypass_target(&target_host, target_port, connect_timeout).await {
return direct_connect_http( return direct_connect_http(
client, client,
stream_id, stream_id,
&target, &target,
method.as_str(), method.as_str(),
header_bytes, header_bytes,
matcher.physical_if_index,
&matcher.physical_if_name,
close_tx, close_tx,
debug, debug,
).await; ).await;
@ -305,28 +821,33 @@ async fn handle_proxy_client(
} }
// ── Bidirectional raw data forwarding ───────────────────────────── // ── Bidirectional raw data forwarding ─────────────────────────────
let mut tcp_buf = vec![0_u8; 1024]; let mut tcp_buf = vec![0_u8; 65536];
loop { loop {
tokio::select! { tokio::select! {
read_res = client.read(&mut tcp_buf) => { read_res = client.read(&mut tcp_buf) => {
match read_res { match read_res {
Ok(0) => { Ok(0) => {
let _ = event_tx.send(ProxyEvent::Close { stream_id }).await; let _ = event_tx.send(ProxyEvent::Close { stream_id }).await;
if debug { if true {
eprintln!("[ostp-client] proxy CLOSE stream_id={stream_id}"); tracing::info!("proxy CLOSE stream_id={stream_id}");
} }
break; break;
} }
Ok(n) => { Ok(n) => {
let _ = event_tx.send(ProxyEvent::Data { let mut offset = 0;
stream_id, while offset < n {
payload: bytes::Bytes::copy_from_slice(&tcp_buf[..n]), let end = (offset + max_chunk).min(n);
}).await; let _ = event_tx.send(ProxyEvent::Data {
stream_id,
payload: bytes::Bytes::copy_from_slice(&tcp_buf[offset..end]),
}).await;
offset = end;
}
} }
Err(_) => { Err(_) => {
let _ = event_tx.send(ProxyEvent::Close { stream_id }).await; let _ = event_tx.send(ProxyEvent::Close { stream_id }).await;
if debug { if true {
eprintln!("[ostp-client] proxy CLOSE stream_id={stream_id}"); tracing::info!("proxy CLOSE stream_id={stream_id}");
} }
break; break;
} }
@ -343,7 +864,7 @@ async fn handle_proxy_client(
Some(ProxyToClientMsg::Close) | Some(ProxyToClientMsg::Error(_)) | None => { Some(ProxyToClientMsg::Close) | Some(ProxyToClientMsg::Error(_)) | None => {
break; break;
} }
Some(ProxyToClientMsg::ConnectOk) => {} // ignored after connect phase Some(ProxyToClientMsg::ConnectOk) | Some(ProxyToClientMsg::UdpData(_, _)) => {} // ignored after connect phase
} }
} }
} }
@ -353,121 +874,6 @@ async fn handle_proxy_client(
Ok(()) Ok(())
} }
#[derive(Clone)]
struct ExclusionMatcher {
domain_suffix: Vec<String>,
cidrs: Vec<Cidr>,
}
impl ExclusionMatcher {
fn new(exclusions: &ExclusionConfig) -> Self {
let mut cidrs = Vec::new();
for ip in &exclusions.ips {
if let Some(cidr) = parse_cidr(ip) {
cidrs.push(cidr);
}
}
Self {
domain_suffix: exclusions
.domains
.iter()
.map(|d| d.trim().trim_start_matches('.').to_lowercase())
.filter(|d| !d.is_empty())
.collect(),
cidrs,
}
}
async fn should_bypass(&self, target: &str, timeout_value: Duration) -> bool {
let (host, port) = match split_host_port(target) {
Some(v) => v,
None => return false,
};
if self.match_domain(&host) {
return true;
}
if self.cidrs.is_empty() {
return false;
}
if let Ok(ip) = host.parse::<std::net::IpAddr>() {
return self.match_ip(&ip);
}
let lookup_target = (host.clone(), port);
match timeout(timeout_value, tokio::net::lookup_host(lookup_target)).await {
Ok(Ok(addrs)) => addrs.into_iter().any(|addr| self.match_ip(&addr.ip())),
_ => false,
}
}
fn match_domain(&self, host: &str) -> bool {
if self.domain_suffix.is_empty() {
return false;
}
let host = host.trim_end_matches('.').to_lowercase();
self.domain_suffix.iter().any(|suffix| {
host == *suffix || host.ends_with(&format!(".{suffix}"))
})
}
fn match_ip(&self, ip: &std::net::IpAddr) -> bool {
self.cidrs.iter().any(|cidr| cidr.contains(ip))
}
}
#[derive(Clone)]
enum Cidr {
V4(u32, u8),
V6(u128, u8),
}
impl Cidr {
fn contains(&self, ip: &std::net::IpAddr) -> bool {
match (self, ip) {
(Cidr::V4(net, bits), std::net::IpAddr::V4(addr)) => {
let mask = if *bits == 0 { 0 } else { u32::MAX << (32 - bits) };
let ip = u32::from_be_bytes(addr.octets());
(ip & mask) == (*net & mask)
}
(Cidr::V6(net, bits), std::net::IpAddr::V6(addr)) => {
let mask = if *bits == 0 { 0 } else { u128::MAX << (128 - bits) };
let ip = u128::from_be_bytes(addr.octets());
(ip & mask) == (*net & mask)
}
_ => false,
}
}
}
fn parse_cidr(value: &str) -> Option<Cidr> {
let value = value.trim();
if value.is_empty() {
return None;
}
if let Some((addr_str, bits_str)) = value.split_once('/') {
let bits: u8 = bits_str.parse().ok()?;
if let Ok(addr) = addr_str.parse::<std::net::IpAddr>() {
return match addr {
std::net::IpAddr::V4(v4) => Some(Cidr::V4(u32::from_be_bytes(v4.octets()), bits.min(32))),
std::net::IpAddr::V6(v6) => Some(Cidr::V6(u128::from_be_bytes(v6.octets()), bits.min(128))),
};
}
}
if let Ok(addr) = value.parse::<std::net::IpAddr>() {
return match addr {
std::net::IpAddr::V4(v4) => Some(Cidr::V4(u32::from_be_bytes(v4.octets()), 32)),
std::net::IpAddr::V6(v6) => Some(Cidr::V6(u128::from_be_bytes(v6.octets()), 128)),
};
}
None
}
fn split_host_port(target: &str) -> Option<(String, u16)> { fn split_host_port(target: &str) -> Option<(String, u16)> {
if let Some((host, port)) = target.rsplit_once(':') { if let Some((host, port)) = target.rsplit_once(':') {
@ -489,14 +895,15 @@ async fn direct_connect_socks5(
mut client: TcpStream, mut client: TcpStream,
stream_id: u16, stream_id: u16,
target: &str, target: &str,
physical_if_index: Option<u32>,
physical_if_name: &Option<String>,
close_tx: mpsc::Sender<u16>, close_tx: mpsc::Sender<u16>,
debug: bool, _debug: bool,
) -> Result<()> { ) -> Result<()> {
if debug { if true {
eprintln!("[ostp-client] proxy BYPASS stream_id={stream_id} target={target}"); tracing::info!("proxy BYPASS stream_id={stream_id} target={target}");
} }
let mut remote = TcpStream::connect(target).await let mut remote = connect_bypassing_tun(target, physical_if_index, physical_if_name).await?;
.with_context(|| format!("direct connect failed: {target}"))?;
client.write_all(&[0x05, 0x00, 0x00, 0x01, 0, 0, 0, 0, 0, 0]).await?; client.write_all(&[0x05, 0x00, 0x00, 0x01, 0, 0, 0, 0, 0, 0]).await?;
let _ = tokio::io::copy_bidirectional(&mut client, &mut remote).await; let _ = tokio::io::copy_bidirectional(&mut client, &mut remote).await;
@ -510,14 +917,15 @@ async fn direct_connect_http(
target: &str, target: &str,
method: &str, method: &str,
header_bytes: Vec<u8>, header_bytes: Vec<u8>,
physical_if_index: Option<u32>,
physical_if_name: &Option<String>,
close_tx: mpsc::Sender<u16>, close_tx: mpsc::Sender<u16>,
debug: bool, _debug: bool,
) -> Result<()> { ) -> Result<()> {
if debug { if true {
eprintln!("[ostp-client] proxy BYPASS stream_id={stream_id} target={target}"); tracing::info!("proxy BYPASS stream_id={stream_id} target={target}");
} }
let mut remote = TcpStream::connect(target).await let mut remote = connect_bypassing_tun(target, physical_if_index, physical_if_name).await?;
.with_context(|| format!("direct connect failed: {target}"))?;
if method == "CONNECT" { if method == "CONNECT" {
client.write_all(b"HTTP/1.1 200 Connection Established\r\nProxy-Agent: ostp/1.0\r\n\r\n").await?; client.write_all(b"HTTP/1.1 200 Connection Established\r\nProxy-Agent: ostp/1.0\r\n\r\n").await?;

View File

@ -0,0 +1,73 @@
pub fn extract_sni(data: &[u8]) -> Option<String> {
// Basic TLS ClientHello parser
// Must be at least 43 bytes to contain anything useful
if data.len() < 43 {
return None;
}
// TLS Record layer: Handshake (22)
if data[0] != 0x16 {
return None;
}
// Record layer version: 0x0301 (TLS 1.0) or 0x0303 (TLS 1.2)
if data[1] != 0x03 {
return None;
}
// Handshake type: ClientHello (1)
if data[5] != 0x01 {
return None;
}
let mut pos = 43; // Skip fixed ClientHello header
// Skip Session ID
if pos >= data.len() { return None; }
let session_id_len = data[pos] as usize;
pos += 1 + session_id_len;
// Skip Cipher Suites
if pos + 2 > data.len() { return None; }
let cipher_suites_len = ((data[pos] as usize) << 8) | (data[pos + 1] as usize);
pos += 2 + cipher_suites_len;
// Skip Compression Methods
if pos >= data.len() { return None; }
let comp_methods_len = data[pos] as usize;
pos += 1 + comp_methods_len;
// Extensions
if pos + 2 > data.len() { return None; }
let extensions_len = ((data[pos] as usize) << 8) | (data[pos + 1] as usize);
pos += 2;
let extensions_end = pos + extensions_len;
if extensions_end > data.len() { return None; }
while pos + 4 <= extensions_end {
let ext_type = ((data[pos] as usize) << 8) | (data[pos + 1] as usize);
let ext_len = ((data[pos + 2] as usize) << 8) | (data[pos + 3] as usize);
pos += 4;
if ext_type == 0x0000 { // Server Name Indication (SNI)
if pos + 5 <= extensions_end {
let _list_len = ((data[pos] as usize) << 8) | (data[pos + 1] as usize);
let name_type = data[pos + 2];
if name_type == 0 { // Hostname
let name_len = ((data[pos + 3] as usize) << 8) | (data[pos + 4] as usize);
if pos + 5 + name_len <= extensions_end {
let sni_bytes = &data[pos + 5..pos + 5 + name_len];
if let Ok(sni) = std::str::from_utf8(sni_bytes) {
return Some(sni.to_string());
}
}
}
}
break;
}
pos += ext_len;
}
None
}

View File

@ -0,0 +1,313 @@
use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::Arc;
use tokio::sync::{mpsc, Mutex};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::{TcpStream, UdpSocket};
use futures::StreamExt;
pub async fn run_udp_nat(
udp_socket: netstack_smoltcp::UdpSocket,
proxy_addr: String,
debug: bool,
matcher: std::sync::Arc<tokio::sync::RwLock<crate::tunnel::exclusion::ExclusionMatcher>>,
phys_if_index: Option<u32>,
phys_if_name: Option<String>,
) {
let (mut rx, tx) = udp_socket.split();
let tx = Arc::new(Mutex::new(tx));
// map from internal client src to a channel that sends (payload, external_dst)
let mut sessions: HashMap<SocketAddr, mpsc::Sender<(Vec<u8>, SocketAddr)>> = HashMap::new();
let mut cleanup_tick = tokio::time::interval(std::time::Duration::from_secs(60));
loop {
tokio::select! {
packet = rx.next() => {
match packet {
Some((payload, src, dst)) => {
if payload.is_empty() { continue; }
if !sessions.contains_key(&src) {
let (session_tx, mut session_rx) = mpsc::channel::<(Vec<u8>, SocketAddr)>(1024);
sessions.insert(src, session_tx);
let proxy_addr_clone = proxy_addr.clone();
let tx_clone = tx.clone();
let mut should_bypass = false;
{
let matcher_guard = matcher.read().await;
if matcher_guard.match_ip(&dst.ip()) {
should_bypass = true;
if debug {
tracing::info!("TUN UDP BYPASS (IP match): {} → {}", src, dst);
}
}
#[cfg(target_os = "windows")]
if !should_bypass {
if let Some(proc_name) = crate::tunnel::process_lookup::get_process_name_from_port_udp(src.port()) {
if debug {
tracing::debug!("TUN UDP lookup: port {} -> process {}", src.port(), proc_name);
}
if matcher_guard.match_process(&proc_name) {
should_bypass = true;
if debug {
tracing::debug!("TUN UDP BYPASS (Process match): {} ({} → {})", proc_name, src, dst);
}
}
} else {
if debug {
tracing::debug!("TUN UDP lookup: port {} -> no process found", src.port());
}
}
}
}
let p_if_idx = phys_if_index;
let p_if_name = phys_if_name.clone();
tokio::spawn(async move {
if should_bypass {
if debug {
tracing::info!("Starting UDP BYPASS session for {}", src);
}
let res = start_udp_bypass_session(src, p_if_idx, p_if_name, &mut session_rx, tx_clone).await;
if res.is_err() {
tracing::debug!("UDP BYPASS session for {} ended: {:?}", src, res.err());
}
} else {
tracing::debug!("Starting UDP NAT session for {}", src);
let res = start_udp_session(src, proxy_addr_clone, &mut session_rx, tx_clone).await;
if res.is_err() {
tracing::debug!("UDP NAT session for {} ended: {:?}", src, res.err());
}
}
});
}
if let Some(sender) = sessions.get(&src) {
match sender.try_send((payload, dst)) {
Err(mpsc::error::TrySendError::Closed(_)) => {
sessions.remove(&src);
}
Err(mpsc::error::TrySendError::Full(_)) => {
// Drop packet to avoid blocking the TUN interface loop
}
Ok(_) => {}
}
}
}
None => break,
}
}
_ = cleanup_tick.tick() => {
sessions.retain(|_, sender| !sender.is_closed());
}
}
}
}
async fn start_udp_bypass_session(
client_src: SocketAddr,
phys_if_index: Option<u32>,
_phys_if_name: Option<String>,
session_rx: &mut mpsc::Receiver<(Vec<u8>, SocketAddr)>,
smoltcp_tx: Arc<Mutex<netstack_smoltcp::udp::WriteHalf>>,
) -> anyhow::Result<()> {
let socket = match client_src {
SocketAddr::V4(_) => UdpSocket::bind("0.0.0.0:0").await?,
SocketAddr::V6(_) => UdpSocket::bind("[::]:0").await?,
};
#[cfg(target_os = "windows")]
if let Some(idx) = phys_if_index {
if let Err(e) = crate::tunnel::proxy::bind_socket_to_interface(&socket, client_src.is_ipv6(), idx) {
tracing::error!("TUN UDP BYPASS failed to bind to physical interface {}: {}", idx, e);
} else {
// Keep debug log
}
} else {
tracing::warn!("TUN UDP BYPASS has no physical interface index!");
}
#[cfg(target_os = "linux")]
if let Some(ref name) = _phys_if_name {
let _ = crate::tunnel::proxy::bind_socket_to_interface(&socket, name);
}
// A single select! loop over both directions, rather than spawning a
// separate task for the read side, so the whole session - physical
// socket included - is torn down the moment this function returns
// (e.g. when session_rx closes). The previous spawned-task version left
// that task (and its Arc<UdpSocket> clone, keeping the OS socket fd
// alive) running forever after this function returned: nothing ever
// cancelled it, so every bypassed UDP flow (any excluded app/IP in TUN
// mode) leaked one socket + one task for the lifetime of the process.
use futures::SinkExt;
let mut buf = [0u8; 65536];
loop {
tokio::select! {
outbound = session_rx.recv() => {
match outbound {
Some((payload, dst)) => { socket.send_to(&payload, dst).await?; }
None => break,
}
}
inbound = socket.recv_from(&mut buf) => {
match inbound {
Ok((n, peer)) => {
let mut lock = smoltcp_tx.lock().await;
let _ = lock.send((buf[..n].to_vec(), peer, client_src)).await;
}
Err(_) => break,
}
}
}
}
Ok(())
}
async fn start_udp_session(
client_src: SocketAddr,
proxy_addr: String,
session_rx: &mut mpsc::Receiver<(Vec<u8>, SocketAddr)>,
smoltcp_tx: Arc<Mutex<netstack_smoltcp::udp::WriteHalf>>,
) -> anyhow::Result<()> {
// 1. TCP Connect to SOCKS5 proxy
let mut tcp = TcpStream::connect(&proxy_addr).await?;
// Auth
tcp.write_all(&[5, 1, 0]).await?;
let mut buf = [0u8; 2];
tcp.read_exact(&mut buf).await?;
if buf[0] != 5 || buf[1] != 0 {
return Err(anyhow::anyhow!("socks5 auth rejected"));
}
// UDP ASSOCIATE to 0.0.0.0:0
tcp.write_all(&[5, 3, 0, 1, 0, 0, 0, 0, 0, 0]).await?;
let mut rep_hdr = [0u8; 4];
tcp.read_exact(&mut rep_hdr).await?;
if rep_hdr[1] != 0 {
return Err(anyhow::anyhow!("socks5 udp associate rejected"));
}
let mut relay_addr = match rep_hdr[3] {
1 => {
let mut addr_buf = [0u8; 6];
tcp.read_exact(&mut addr_buf).await?;
let ip = std::net::Ipv4Addr::new(addr_buf[0], addr_buf[1], addr_buf[2], addr_buf[3]);
let port = u16::from_be_bytes([addr_buf[4], addr_buf[5]]);
SocketAddr::new(std::net::IpAddr::V4(ip), port)
}
4 => {
let mut addr_buf = [0u8; 18];
tcp.read_exact(&mut addr_buf).await?;
let mut octets = [0u8; 16];
octets.copy_from_slice(&addr_buf[0..16]);
let ip = std::net::Ipv6Addr::from(octets);
let port = u16::from_be_bytes([addr_buf[16], addr_buf[17]]);
SocketAddr::new(std::net::IpAddr::V6(ip), port)
}
_ => return Err(anyhow::anyhow!("unsupported ATYP in UDP ASSOCIATE response")),
};
// If proxy returned 0.0.0.0 or ::, use the proxy's IP
if relay_addr.ip().is_unspecified() {
if let Ok(proxy_sock) = proxy_addr.parse::<SocketAddr>() {
relay_addr.set_ip(proxy_sock.ip());
}
}
// Local SOCKS5 proxy always returns 127.0.0.1 (IPv4), so always bind IPv4
let udp = UdpSocket::bind("127.0.0.1:0").await?;
// CRITICAL for Android: protect this UDP socket so it goes out via the
// real physical interface, not back into the TUN (which would cause an
// infinite routing loop for DNS and all other UDP traffic).
#[cfg(target_os = "android")]
{
use std::os::unix::io::AsRawFd;
crate::bridge::protect_socket(udp.as_raw_fd());
}
let mut buf = vec![0u8; 65536];
let timeout = std::time::Duration::from_secs(300); // 5 min idle timeout
let mut tcp_buf = [0u8; 1];
loop {
tokio::select! {
res = tokio::time::timeout(timeout, session_rx.recv()) => {
match res {
Ok(Some((payload, dst))) => {
let mut packet = vec![0u8; 3]; // RSV, FRAG
match dst.ip() {
std::net::IpAddr::V4(v4) => { packet.push(1); packet.extend_from_slice(&v4.octets()); }
std::net::IpAddr::V6(v6) => { packet.push(4); packet.extend_from_slice(&v6.octets()); }
}
packet.extend_from_slice(&dst.port().to_be_bytes());
packet.extend_from_slice(&payload);
tracing::debug!("udp_nat SENDING UDP ASSOCIATE payload len={} to relay_addr={} (original dst: {})", payload.len(), relay_addr, dst);
let _ = udp.send_to(&packet, relay_addr).await;
}
Ok(None) => break,
Err(_) => break, // timeout
}
}
res = udp.recv_from(&mut buf) => {
match res {
Err(e) => {
tracing::debug!("udp_nat recv_from error: {}", e);
continue; // transient error, don't kill the session
}
Ok((len, _peer)) => {
if len < 4 { continue; }
let frag = buf[2];
if frag != 0 { continue; } // fragment not supported
let atyp = buf[3];
let (header_len, remote_dst) = match atyp {
1 => {
if len < 10 { continue; }
let ip = std::net::Ipv4Addr::new(buf[4], buf[5], buf[6], buf[7]);
let port = u16::from_be_bytes([buf[8], buf[9]]);
(10, SocketAddr::new(std::net::IpAddr::V4(ip), port))
}
4 => {
if len < 22 { continue; }
let mut octets = [0u8; 16];
octets.copy_from_slice(&buf[4..20]);
let ip = std::net::Ipv6Addr::from(octets);
let port = u16::from_be_bytes([buf[20], buf[21]]);
(22, SocketAddr::new(std::net::IpAddr::V6(ip), port))
}
_ => continue,
};
let payload = buf[header_len..len].to_vec();
tracing::debug!("udp_nat RECEIVED UDP ASSOCIATE REPLY from {} for {} len={}", remote_dst, client_src, payload.len());
use futures::SinkExt;
if let Err(e) = smoltcp_tx.lock().await.send((payload, remote_dst, client_src)).await {
tracing::error!("udp_nat failed to inject packet into smoltcp: {}", e);
} else {
tracing::debug!("udp_nat successfully injected packet into smoltcp from {} to {}", remote_dst, client_src);
}
}
}
}
// If TCP drops, UDP association is over
res = tcp.read(&mut tcp_buf) => {
match res {
Ok(0) | Err(_) => break,
Ok(_) => {}
}
}
}
}
Ok(())
}

View File

@ -1,49 +0,0 @@
#![allow(unused_imports)]
use anyhow::Result;
#[cfg(target_os = "windows")]
use anyhow::anyhow;
use std::path::PathBuf;
#[cfg(target_os = "windows")]
pub fn download_wintun_dll(debug: bool) -> Result<()> {
let exe = std::env::current_exe()?;
let dir = exe.parent().ok_or_else(|| anyhow!("failed to get binary directory"))?;
let dll_path = dir.join("wintun.dll");
if !dll_path.exists() {
if debug {
println!("[ostp-client] wintun.dll not found. Downloading automatically...");
}
let zip_path = dir.join("wintun.zip").to_string_lossy().replace('\\', "/");
let temp_path = dir.join("wintun_temp").to_string_lossy().replace('\\', "/");
let dll_dest = dll_path.to_string_lossy().replace('\\', "/");
let ps_script = format!(
"Invoke-WebRequest -Uri 'https://www.wintun.net/builds/wintun-0.14.1.zip' -OutFile '{}' -ErrorAction Stop; \
Expand-Archive -Path '{}' -DestinationPath '{}' -Force; \
Get-ChildItem -Path '{}' -Filter 'wintun.dll' -Recurse | Copy-Item -Destination '{}' -Force; \
Remove-Item '{}', '{}' -Recurse -Force",
zip_path, zip_path, temp_path, temp_path, dll_dest, zip_path, temp_path
);
let output = std::process::Command::new("powershell")
.args(["-Command", &ps_script])
.current_dir(dir)
.output()?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
return Err(anyhow!("Failed to download wintun.dll: {stderr}"));
}
if debug {
println!("[ostp-client] wintun.dll downloaded and installed successfully!");
}
}
Ok(())
}
#[cfg(not(target_os = "windows"))]
pub fn download_wintun_dll(_debug: bool) -> Result<()> {
Ok(())
}

View File

@ -1,91 +0,0 @@
use anyhow::{anyhow, Result};
#[cfg(target_os = "windows")]
use std::sync::Arc;
use tokio::sync::watch;
#[cfg(target_os = "windows")]
pub async fn run_wintun_tunnel(
mut shutdown: watch::Receiver<bool>,
debug: bool,
) -> Result<()> {
if debug {
println!("[ostp-client] Initializing Wintun adapter 'ostp_tun'...");
}
// 1. Load Wintun DLL
let wintun = unsafe { wintun::load_from_path("wintun.dll") }
.map_err(|e| anyhow!("Failed to load wintun.dll: {:?}", e))?;
// 2. Create or Open Adapter with static name "ostp_tun"
let adapter = match wintun::Adapter::open(&wintun, "ostp_tun") {
Ok(a) => a,
Err(_) => wintun::Adapter::create(&wintun, "ostp_tun", "OSTP TUN Adapter", None)
.map_err(|e| anyhow!("Failed to create Wintun adapter: {:?}", e))?,
};
let adapter = Arc::new(adapter);
// Set IP, Subnet and Gateway natively using netsh for bulletproof routing
if debug {
println!("[ostp-client] Configuring Wintun network settings via netsh...");
}
let output = std::process::Command::new("netsh")
.args(["interface", "ipv4", "set", "address", "name=ostp_tun", "static", "10.1.0.2", "255.255.255.0", "10.1.0.1"])
.output()?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
println!("[ostp-client] Warning: netsh returned error: {}", stderr);
} else {
if debug {
println!("[ostp-client] Network configured. ostp_tun IP: 10.1.0.2, Gateway: 10.1.0.1");
}
}
// Start Wintun session
let session = adapter.start_session(wintun::MAX_RING_CAPACITY)
.map_err(|e| anyhow!("Failed to start Wintun session: {:?}", e))?;
let session = Arc::new(session);
if debug {
println!("[ostp-client] TUN tunnel 'ostp_tun' is active and intercepting packets!");
}
// Spawn Packet Receiver Loop to read packets from Windows stack
let rx_session = session.clone();
tokio::task::spawn_blocking(move || {
loop {
match rx_session.receive_blocking() {
Ok(packet) => {
let bytes = packet.bytes();
if bytes.len() >= 20 {
let proto = bytes[9];
let src_ip = format!("{}.{}.{}.{}", bytes[12], bytes[13], bytes[14], bytes[15]);
let dest_ip = format!("{}.{}.{}.{}", bytes[16], bytes[17], bytes[18], bytes[19]);
if debug {
println!("[TUN Packet] Proto={}, Src={}, Dest={}, Len={}", proto, src_ip, dest_ip, bytes.len());
}
}
}
Err(_) => break,
}
}
});
// Wait for shutdown signal
let _ = shutdown.changed().await;
if debug {
println!("[ostp-client] Shutting down Wintun adapter...");
}
Ok(())
}
#[cfg(not(target_os = "windows"))]
pub async fn run_wintun_tunnel(
_shutdown: watch::Receiver<bool>,
_debug: bool,
) -> Result<()> {
Err(anyhow!("Wintun is only supported on Windows!"))
}

1
ostp-client/test_udp.rs Normal file
View File

@ -0,0 +1 @@
fn main() { let x: () = netstack_smoltcp::StackBuilder::default().build().unwrap(); }

View File

@ -6,12 +6,13 @@ license.workspace = true
[dependencies] [dependencies]
anyhow.workspace = true anyhow.workspace = true
async-trait.workspace = true
bytes.workspace = true bytes.workspace = true
chacha20poly1305.workspace = true chacha20poly1305.workspace = true
rand.workspace = true rand.workspace = true
snow.workspace = true snow.workspace = true
thiserror.workspace = true thiserror.workspace = true
tracing.workspace = true tracing.workspace = true
x25519-dalek.workspace = true
sha2.workspace = true sha2.workspace = true
hmac.workspace = true
x25519-dalek = { version = "2.0.1", features = ["static_secrets"] }
hkdf = "0.12.0"

660
ostp-core/src/congestion.rs Normal file
View File

@ -0,0 +1,660 @@
//! Congestion control for the OSTP protocol.
//!
//! Implements a simplified BBR-inspired algorithm that estimates bottleneck
//! bandwidth and minimum RTT to determine the optimal sending rate.
//! This replaces the fixed `retransmit_budget = 8` with an adaptive
//! congestion window that responds to network conditions.
//!
//! RTO calculation follows RFC 6298:
//! SRTT = (1 - α) * SRTT + α * RTT (α = 1/8)
//! RTTVAR = (1 - β) * RTTVAR + β * |SRTT - RTT| (β = 1/4)
//! RTO = SRTT + 4 * RTTVAR
//! clamped to [RTO_MIN, RTO_MAX]
use std::time::{Duration, Instant};
/// Congestion control state for a single OSTP session.
pub struct CongestionController {
/// Current congestion window in bytes (how much can be in-flight)
cwnd: u64,
/// Slow-start threshold in bytes
ssthresh: u64,
/// Current phase
phase: Phase,
/// Minimum RTT observed (for BBR-style bandwidth estimation)
min_rtt: Duration,
/// Smoothed RTT (RFC 6298 SRTT)
srtt: Duration,
/// RTT variance (RFC 6298 RTTVAR)
rttvar: Duration,
/// Whether we have received a first RTT sample
rtt_initialized: bool,
/// Bytes currently in flight (unacknowledged)
bytes_in_flight: u64,
/// Total bytes acknowledged (for bandwidth estimation)
total_acked: u64,
/// Last time we received an ACK
last_ack_time: Instant,
/// Number of loss events in the current window
loss_count: u32,
/// Pacing rate: bytes per second
pacing_rate: u64,
/// Token-bucket allowance for pacing, in bytes.
pacing_tokens: f64,
pacing_last_refill: Instant,
/// MTU estimate (used for cwnd → packet count conversion)
mtu: u64,
/// Min RTT expiry: re-probe after 10 seconds
min_rtt_stamp: Instant,
/// Loss events counted toward SLOW_START_LOSS_TOLERANCE within the
/// current SLOW_START_LOSS_WINDOW (see on_loss's SlowStart arm).
slow_start_losses: u32,
/// Start of the current loss-tolerance window.
slow_start_loss_window_start: Instant,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Phase {
/// Exponential growth until loss or ssthresh
SlowStart,
/// Probe bandwidth: additive increase
ProbeBandwidth,
}
/// Initial congestion window: 32 packets × MTU (IW10 is too conservative for modern links)
const INITIAL_CWND_PACKETS: u64 = 32;
/// Minimum cwnd: 2 packets
const MIN_CWND_PACKETS: u64 = 2;
/// Min RTT expiry window (after which we re-probe)
const MIN_RTT_EXPIRY: Duration = Duration::from_secs(10);
/// Minimum RTO (RFC 6298: 1s in TCP; we use 50ms since we own the protocol)
/// Absolute ceiling on the congestion window, in packets. At a ~1200-byte MTU
/// this is roughly 1.2 MB in flight — already far above the bandwidth-delay
/// product of any link this protocol realistically runs over, so anything
/// beyond it is standing queue, not throughput. The client previously allowed
/// up to 16384 packets (~20 MB), which on a mobile uplink is minutes of buffer.
const MAX_CWND_PACKETS: u64 = 1024;
/// SRTT/min_rtt ratio at which slow start stops. Doubling is what fills a deep
/// buffer fastest, so growth must end when the queue starts building rather
/// than waiting for a loss that a deep buffer may never produce.
const RTT_INFLATION_EXIT_SLOW_START: f64 = 2.0;
/// SRTT/min_rtt ratio treated as a standing queue that must be actively drained.
const RTT_INFLATION_BACKOFF: f64 = 4.0;
/// How much pacing allowance may accumulate, expressed as time-at-rate.
const PACING_BURST: Duration = Duration::from_millis(10);
const RTO_MIN: Duration = Duration::from_millis(50);
/// Maximum RTO
const RTO_MAX: Duration = Duration::from_secs(16);
/// Initial RTT estimate — 30 ms is reasonable for a well-connected VPN server.
/// Will be replaced by first real measurement within milliseconds.
const INITIAL_RTT: Duration = Duration::from_millis(30);
/// Isolated packet loss during slow start (a single dropped frame from
/// wireless noise, a brief LTE handover blip, etc.) is normal on real
/// mobile/Wi-Fi links and does NOT mean the link is congested. The previous
/// behavior exited slow start and halved cwnd on the very FIRST loss, which
/// on any link with a non-zero background loss rate permanently downgrades
/// the session from exponential growth to linear (+1 MTU/RTT) ProbeBandwidth
/// growth within the first few RTTs - turning what should be a sub-second
/// ramp-up into tens of seconds to minutes before throughput opens up
/// (observed as: a trickle of KB/s, then a sudden jump once cwnd finally
/// claws back up). Only treat loss as a real congestion signal - and pay
/// the full slow-start-exit + halving cost - once this many losses land
/// within SLOW_START_LOSS_WINDOW.
const SLOW_START_LOSS_TOLERANCE: u32 = 3;
/// Window within which SLOW_START_LOSS_TOLERANCE losses must land to count
/// as sustained (rather than isolated) loss. Roughly a few RTTs on a
/// well-connected link, generous on a slow one.
const SLOW_START_LOSS_WINDOW: Duration = Duration::from_millis(500);
impl CongestionController {
pub fn new(mtu: u64) -> Self {
let now = Instant::now();
let initial_cwnd = INITIAL_CWND_PACKETS * mtu;
// Initial pacing: deliver cwnd in ~2 RTTs to fill the pipe quickly
let initial_pacing = initial_cwnd * 1_000_000 / INITIAL_RTT.as_micros().max(1) as u64;
Self {
cwnd: initial_cwnd,
ssthresh: u64::MAX,
phase: Phase::SlowStart,
min_rtt: INITIAL_RTT,
srtt: INITIAL_RTT,
rttvar: INITIAL_RTT / 2,
rtt_initialized: false,
bytes_in_flight: 0,
total_acked: 0,
last_ack_time: now,
loss_count: 0,
pacing_rate: initial_pacing,
mtu,
min_rtt_stamp: now,
slow_start_losses: 0,
slow_start_loss_window_start: now,
pacing_tokens: (INITIAL_CWND_PACKETS * mtu) as f64,
pacing_last_refill: now,
}
}
/// Bytes of pacing allowance available right now, without consuming any.
///
/// Read-only so the send path can use it as an admission check before it
/// commits to building a datagram.
pub fn pacing_available(&self) -> f64 {
let elapsed = self.pacing_last_refill.elapsed().as_secs_f64();
(self.pacing_tokens + elapsed * self.pacing_rate as f64).min(self.pacing_burst())
}
/// Whether at least one full-size packet may be released right now.
pub fn can_pace_packet(&self) -> bool {
self.pacing_available() >= self.mtu as f64
}
/// Ceiling on accumulated allowance.
///
/// Pacing intervals here are fractions of a millisecond, so releasing
/// strictly one packet at a time would need a sub-millisecond timer per
/// packet. Instead we allow a short burst — the same trade every real
/// pacing implementation makes — sized so the loop's existing ~10ms wakeups
/// can still saturate the configured rate, with a small floor so a
/// cold/low estimate can never wedge sending entirely.
fn pacing_burst(&self) -> f64 {
let by_rate = self.pacing_rate as f64 * PACING_BURST.as_secs_f64();
by_rate.max((self.mtu * 4) as f64)
}
/// Refill from elapsed time and deduct `bytes`. Called on the real send
/// path; allowance is permitted to go negative so an oversized packet still
/// pays for itself rather than being released for free.
fn consume_pacing(&mut self, bytes: u64) {
let now = Instant::now();
let elapsed = now.duration_since(self.pacing_last_refill).as_secs_f64();
self.pacing_last_refill = now;
self.pacing_tokens =
(self.pacing_tokens + elapsed * self.pacing_rate as f64).min(self.pacing_burst())
- bytes as f64;
}
/// Returns the current congestion window in bytes.
pub fn cwnd(&self) -> u64 {
self.cwnd
}
/// Returns the current congestion window in packets.
pub fn cwnd_packets(&self) -> usize {
(self.cwnd / self.mtu).max(MIN_CWND_PACKETS) as usize
}
/// Returns the current pacing rate in bytes/sec.
pub fn pacing_rate(&self) -> u64 {
self.pacing_rate
}
/// Returns the smoothed RTT estimate (SRTT).
pub fn smoothed_rtt(&self) -> Duration {
self.srtt
}
/// Returns the adaptive RTO computed per RFC 6298:
/// RTO = SRTT + 4 * RTTVAR, clamped to [RTO_MIN, RTO_MAX].
///
/// This replaces the static `rto_ms` field in ProtocolMachine so that
/// retransmit timers automatically track changing network conditions.
pub fn rto(&self) -> Duration {
let rttvar4 = self.rttvar.saturating_mul(4);
let rto = self.srtt.saturating_add(rttvar4);
rto.clamp(RTO_MIN, RTO_MAX)
}
/// Returns how many bytes can still be sent.
pub fn available_cwnd(&self) -> u64 {
self.cwnd.saturating_sub(self.bytes_in_flight)
}
/// Returns the recommended retransmit budget per tick.
pub fn retransmit_budget(&self) -> usize {
// Allow retransmitting up to 1/4 of the cwnd in packets per tick
let budget = (self.cwnd_packets() / 4).max(2);
budget.min(64) // cap at 64 to prevent burst
}
/// Check whether we can send more data.
pub fn can_send(&self) -> bool {
self.bytes_in_flight < self.cwnd
}
/// Record that we sent `bytes` of data.
pub fn on_send(&mut self, bytes: u64) {
self.bytes_in_flight = self.bytes_in_flight.saturating_add(bytes);
// Charge the pacing bucket here rather than at the admission check, so
// every byte that actually reaches the wire is paid for exactly once —
// including retransmits, which are precisely what must not be allowed
// to bypass the rate limit and pile into an already-full queue.
self.consume_pacing(bytes);
}
/// Record that `bytes` were acknowledged but WITHOUT a usable RTT sample
/// (e.g. every acked frame was retransmitted, so Karn's algorithm forbids
/// measuring RTT from it). The window still advances; only the RTT estimator
/// is left untouched.
pub fn on_ack_no_rtt(&mut self, bytes: u64) {
let now = Instant::now();
self.bytes_in_flight = self.bytes_in_flight.saturating_sub(bytes);
self.total_acked = self.total_acked.saturating_add(bytes);
self.grow_window(bytes);
self.update_pacing_rate();
self.last_ack_time = now;
}
/// Record that `bytes` were acknowledged with the given RTT sample.
pub fn on_ack(&mut self, bytes: u64, rtt: Duration) {
let now = Instant::now();
self.bytes_in_flight = self.bytes_in_flight.saturating_sub(bytes);
self.total_acked = self.total_acked.saturating_add(bytes);
// Update RTT measurements
self.update_rtt(rtt, now);
self.grow_window(bytes);
self.update_pacing_rate();
self.last_ack_time = now;
}
/// Congestion-window growth shared by both ACK paths (slow start / probe).
fn grow_window(&mut self, bytes: u64) {
// ── Delay-based congestion signal ────────────────────────────────────
// A loss-only controller is blind on a deeply-buffered path, and mobile
// carrier buffers are very deep: they absorb a burst instead of dropping
// it, so no loss is ever signalled and cwnd keeps growing. The queue —
// not the link — is what grows, and the standing delay it adds shows up
// as RTT inflating far above the path's floor. Left unchecked this is a
// positive feedback loop: bigger queue -> larger RTT samples -> larger
// SRTT -> larger RTO -> retransmits pile on -> bigger queue, which is
// how a session ends up reporting multi-second (even multi-minute) RTT
// and stalls video until the buffer finally drains or the user
// reconnects. Treat sustained RTT inflation as congestion in its own
// right, exactly as it is.
let inflation = if self.rtt_initialized && !self.min_rtt.is_zero() {
self.srtt.as_secs_f64() / self.min_rtt.as_secs_f64()
} else {
1.0
};
if inflation >= RTT_INFLATION_BACKOFF {
// Standing queue is severe — actively drain it.
self.cwnd = (self.cwnd / 2).max(MIN_CWND_PACKETS * self.mtu);
self.ssthresh = self.cwnd;
self.phase = Phase::ProbeBandwidth;
tracing::debug!(cwnd = self.cwnd, inflation, "congestion: draining standing queue");
self.clamp_cwnd();
return;
}
match self.phase {
Phase::SlowStart => {
// Exponential doubling is what fills a deep buffer fastest, so
// leave slow start as soon as the queue starts to build rather
// than waiting for the loss that may never come.
if inflation >= RTT_INFLATION_EXIT_SLOW_START {
self.ssthresh = self.cwnd;
self.phase = Phase::ProbeBandwidth;
tracing::debug!(cwnd = self.cwnd, inflation, "congestion: RTT inflation ended slow start");
self.clamp_cwnd();
return;
}
// Exponential growth: increase cwnd by acked bytes (doubles per RTT)
self.cwnd = self.cwnd.saturating_add(bytes);
if self.cwnd >= self.ssthresh {
self.phase = Phase::ProbeBandwidth;
tracing::debug!(cwnd = self.cwnd, "congestion: exiting slow start");
}
}
Phase::ProbeBandwidth => {
// TCP Reno Additive Increase: increase cwnd by ~1 MTU per RTT
self.cwnd = self.cwnd.saturating_add(bytes * self.mtu / self.cwnd.max(1));
}
}
self.clamp_cwnd();
}
/// Hard ceiling on the congestion window.
///
/// Independent of any estimate: no real path this protocol runs over has a
/// bandwidth-delay product anywhere near this, so a window above it is
/// buffered queue rather than data in transit. Without it, slow start on a
/// buffer that never drops could grow the window into the tens of megabytes.
fn clamp_cwnd(&mut self) {
let ceiling = MAX_CWND_PACKETS.saturating_mul(self.mtu);
if self.cwnd > ceiling {
self.cwnd = ceiling;
}
}
/// Record a loss event.
pub fn on_loss(&mut self, bytes_lost: u64) {
self.bytes_in_flight = self.bytes_in_flight.saturating_sub(bytes_lost);
self.loss_count += 1;
match self.phase {
Phase::SlowStart => {
let now = Instant::now();
if now.duration_since(self.slow_start_loss_window_start) > SLOW_START_LOSS_WINDOW {
// Previous window's losses have aged out - this loss starts a fresh count.
self.slow_start_losses = 0;
self.slow_start_loss_window_start = now;
}
self.slow_start_losses += 1;
if self.slow_start_losses >= SLOW_START_LOSS_TOLERANCE {
// Sustained loss within the window: treat as real congestion.
// Exit slow start, set ssthresh to half of cwnd.
self.ssthresh = self.cwnd / 2;
self.cwnd = self.ssthresh.max(MIN_CWND_PACKETS * self.mtu);
self.phase = Phase::ProbeBandwidth;
tracing::debug!(cwnd = self.cwnd, ssthresh = self.ssthresh, "congestion: sustained loss during slow start, exiting");
} else {
// Isolated loss: likely non-congestive noise. Take a mild,
// temporary haircut but keep exponential growth going -
// don't throw away slow start over a single dropped frame.
self.cwnd = (self.cwnd * 8 / 10).max(MIN_CWND_PACKETS * self.mtu);
tracing::debug!(cwnd = self.cwnd, count = self.slow_start_losses, "congestion: isolated loss during slow start, staying in slow start");
}
}
Phase::ProbeBandwidth => {
// Multiplicative decrease: cwnd *= 0.7 (BBR-style, less aggressive than Cubic's 0.5)
self.cwnd = (self.cwnd * 7 / 10).max(MIN_CWND_PACKETS * self.mtu);
tracing::debug!(cwnd = self.cwnd, "congestion: loss, cwnd reduced");
}
}
self.update_pacing_rate();
}
// ── Private ──────────────────────────────────────────────────────────────
fn update_rtt(&mut self, rtt: Duration, now: Instant) {
// Update windowed minimum RTT (for pacing)
if rtt < self.min_rtt || now.duration_since(self.min_rtt_stamp) >= MIN_RTT_EXPIRY {
self.min_rtt = rtt;
self.min_rtt_stamp = now;
}
// Update SRTT and RTTVAR per RFC 6298
if !self.rtt_initialized {
// First measurement: initialize directly
self.srtt = rtt;
self.rttvar = rtt / 2;
self.rtt_initialized = true;
} else {
// RTTVAR = (3/4) * RTTVAR + (1/4) * |SRTT - R|
let diff = if rtt > self.srtt {
rtt - self.srtt
} else {
self.srtt - rtt
};
// Integer-safe: RTTVAR = RTTVAR - RTTVAR/4 + diff/4
self.rttvar = self.rttvar
.saturating_sub(self.rttvar / 4)
.saturating_add(diff / 4);
// SRTT = (7/8) * SRTT + (1/8) * R
self.srtt = self.srtt
.saturating_sub(self.srtt / 8)
.saturating_add(rtt / 8);
}
tracing::trace!(
srtt_ms = self.srtt.as_millis(),
rttvar_ms = self.rttvar.as_millis(),
rto_ms = self.rto().as_millis(),
"congestion: RTT updated"
);
}
fn update_pacing_rate(&mut self) {
// Pacing rate = cwnd / min_rtt (delivery rate target)
let rtt_us = self.min_rtt.as_micros().max(1) as u64;
self.pacing_rate = self.cwnd * 1_000_000 / rtt_us;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_initial_state() {
let cc = CongestionController::new(1200);
assert_eq!(cc.cwnd(), 32 * 1200); // 32 * 1200
assert!(cc.can_send());
assert_eq!(cc.cwnd_packets(), 32);
}
#[test]
fn test_slow_start_growth() {
let mut cc = CongestionController::new(1200);
let initial = cc.cwnd();
cc.on_send(1200);
cc.on_ack(1200, Duration::from_millis(50));
assert!(cc.cwnd() > initial);
}
#[test]
fn test_loss_reduces_cwnd() {
let mut cc = CongestionController::new(1200);
let initial = cc.cwnd();
cc.on_loss(1200);
assert!(cc.cwnd() < initial);
}
/// The bufferbloat case: a deep buffer absorbs everything, so NOTHING is
/// ever lost, but the standing queue inflates RTT. A loss-only controller
/// grows cwnd forever here — which is how a session ends up reporting
/// multi-second RTT and stalling video.
#[test]
fn test_rtt_inflation_halts_growth_without_any_loss() {
let mut cc = CongestionController::new(1200);
// Establish a low path floor; this becomes min_rtt.
for _ in 0..4 {
cc.on_send(1200);
cc.on_ack(1200, Duration::from_millis(20));
}
let cwnd_before = cc.cwnd();
// Queue builds: RTT climbs far above the floor, still zero loss.
for _ in 0..20 {
cc.on_send(1200);
cc.on_ack(1200, Duration::from_millis(400));
}
assert!(
cc.cwnd() <= cwnd_before,
"cwnd kept growing while the queue was inflating RTT ({} -> {})",
cwnd_before,
cc.cwnd()
);
}
/// Pacing must actually bound the release rate: draining the bucket has to
/// deny the next packet. Without this the congestion window alone decides,
/// and a whole window leaves back-to-back.
#[test]
fn test_pacing_bucket_denies_once_drained() {
let mut cc = CongestionController::new(1200);
assert!(cc.can_pace_packet(), "a fresh controller must allow sending");
// Spend well beyond one burst allowance.
let burst_bytes = cc.pacing_available();
let mut spent = 0.0;
while spent <= burst_bytes + 1200.0 {
cc.on_send(1200);
spent += 1200.0;
}
assert!(
!cc.can_pace_packet(),
"pacing allowed unbounded sending: {} bytes still available after spending {}",
cc.pacing_available(),
spent
);
}
/// The allowance must refill over time, or sending would stall permanently
/// once the first burst is spent.
#[test]
fn test_pacing_bucket_refills_over_time() {
let mut cc = CongestionController::new(1200);
while cc.can_pace_packet() {
cc.on_send(1200);
}
assert!(!cc.can_pace_packet());
std::thread::sleep(Duration::from_millis(25));
assert!(
cc.can_pace_packet(),
"pacing bucket never refilled; sending would be stuck forever"
);
}
/// cwnd must never exceed the absolute ceiling, however long slow start
/// runs unopposed — above it the window is buffered queue, not throughput.
#[test]
fn test_cwnd_never_exceeds_absolute_ceiling() {
let mut cc = CongestionController::new(1200);
// Constant RTT: no inflation signal, so only the hard cap can stop this.
for _ in 0..5000 {
cc.on_send(1200);
cc.on_ack(1200, Duration::from_millis(30));
}
assert!(
cc.cwnd() <= MAX_CWND_PACKETS * 1200,
"cwnd {} exceeded the {}-packet ceiling",
cc.cwnd(),
MAX_CWND_PACKETS
);
}
#[test]
fn test_isolated_slow_start_loss_does_not_exit_slow_start() {
// A single dropped packet (wireless noise, a brief handover blip) is
// normal on real links and must not permanently downgrade the
// session from exponential to linear growth.
let mut cc = CongestionController::new(1200);
cc.on_loss(1200);
assert_eq!(cc.phase, Phase::SlowStart, "one isolated loss must not exit slow start");
// It should still shrink the window somewhat (not ignored entirely),
// just far less punishing than the sustained-congestion case.
let after_one = cc.cwnd();
assert!(after_one < INITIAL_CWND_PACKETS * 1200);
}
#[test]
fn test_sustained_slow_start_loss_exits_slow_start() {
// Losses landing close together (within SLOW_START_LOSS_WINDOW) are
// a real congestion signal and must still trigger the harsher
// exit-slow-start + halve response.
let mut cc = CongestionController::new(1200);
for _ in 0..SLOW_START_LOSS_TOLERANCE {
cc.on_loss(1200);
}
assert_eq!(cc.phase, Phase::ProbeBandwidth, "sustained loss must exit slow start");
}
#[test]
fn test_slow_start_loss_window_resets_after_expiry() {
// Two losses far enough apart (window expired between them) must
// each be treated as isolated, not accumulated toward the sustained-
// loss threshold.
let mut cc = CongestionController::new(1200);
cc.on_loss(1200);
assert_eq!(cc.phase, Phase::SlowStart);
// Simulate the window having expired by resetting its start
// directly (std::thread::sleep in a unit test would be flaky/slow).
cc.slow_start_loss_window_start = Instant::now() - SLOW_START_LOSS_WINDOW - Duration::from_millis(1);
cc.on_loss(1200);
assert_eq!(cc.phase, Phase::SlowStart, "a loss after the window expired must restart the count, not accumulate");
assert_eq!(cc.slow_start_losses, 1);
}
#[test]
fn test_can_send_limits() {
let mut cc = CongestionController::new(1200);
// Send until cwnd is exhausted
for _ in 0..32 {
cc.on_send(1200);
}
assert!(!cc.can_send()); // cwnd exhausted
}
#[test]
fn test_retransmit_budget() {
let cc = CongestionController::new(1200);
let budget = cc.retransmit_budget();
assert!(budget >= 2);
assert!(budget <= 64);
}
#[test]
fn test_rtt_tracking_first_sample() {
let mut cc = CongestionController::new(1200);
cc.on_send(1200);
cc.on_ack(1200, Duration::from_millis(25));
// After first sample: SRTT = 25ms, RTTVAR = 12ms
assert_eq!(cc.smoothed_rtt(), Duration::from_millis(25));
}
#[test]
fn test_rto_rfc6298() {
let mut cc = CongestionController::new(1200);
// After first sample with RTT=50ms: SRTT=50ms, RTTVAR=25ms, RTO=150ms
cc.on_send(1200);
cc.on_ack(1200, Duration::from_millis(50));
let rto = cc.rto();
// RTO = 50 + 4*25 = 150ms; clamped to [50ms, 16s]
assert!(rto >= RTO_MIN);
assert!(rto <= RTO_MAX);
assert_eq!(rto, Duration::from_millis(150));
}
#[test]
fn test_on_ack_no_rtt_grows_window_without_touching_srtt() {
let mut cc = CongestionController::new(1200);
// Establish a known SRTT with a real sample.
cc.on_send(1200);
cc.on_ack(1200, Duration::from_millis(40));
let srtt_before = cc.smoothed_rtt();
let cwnd_before = cc.cwnd();
// A Karn's-algorithm ACK (all acked frames were retransmitted): window
// must advance, RTT estimate must be untouched.
cc.on_send(1200);
cc.on_ack_no_rtt(1200);
assert!(cc.cwnd() > cwnd_before, "cwnd should still grow on a no-RTT ack");
assert_eq!(cc.smoothed_rtt(), srtt_before, "SRTT must not move on a no-RTT ack");
}
#[test]
fn test_rto_clamp_min() {
let cc = CongestionController::new(1200);
// Even with no RTT samples, RTO should not go below RTO_MIN
assert!(cc.rto() >= RTO_MIN);
}
#[test]
fn test_rto_adapts_after_multiple_samples() {
let mut cc = CongestionController::new(1200);
// Feed several consistent RTT samples
for _ in 0..8 {
cc.on_send(1200);
cc.on_ack(1200, Duration::from_millis(20));
}
// After convergence, RTTVAR should be small → RTO close to SRTT + small margin
let rto = cc.rto();
// Should be well below 100ms (the old hardcoded default)
assert!(rto < Duration::from_millis(200));
assert!(rto >= RTO_MIN);
}
}

View File

@ -1,45 +0,0 @@
use rand::rngs::OsRng;
use sha2::{Digest, Sha256};
use x25519_dalek::{EphemeralSecret, PublicKey};
#[derive(Debug, Clone)]
pub struct HybridSharedSecret {
pub x25519_pubkey: [u8; 32],
pub pq_ciphertext: Vec<u8>,
pub combined_secret: [u8; 32],
}
pub trait KeyExchange {
fn client_kex() -> HybridSharedSecret;
}
pub struct HybridKex;
impl HybridKex {
pub fn client_offer() -> HybridSharedSecret {
let secret = EphemeralSecret::random_from_rng(OsRng);
let pubkey = PublicKey::from(&secret);
// Placeholder PQ ciphertext. Replace with ML-KEM encapsulation output.
let pq_ciphertext = vec![0_u8; 1088];
let mut hasher = Sha256::new();
hasher.update(pubkey.as_bytes());
hasher.update(&pq_ciphertext);
let digest = hasher.finalize();
let mut combined_secret = [0_u8; 32];
combined_secret.copy_from_slice(&digest[..32]);
HybridSharedSecret {
x25519_pubkey: *pubkey.as_bytes(),
pq_ciphertext,
combined_secret,
}
}
}
impl KeyExchange for HybridKex {
fn client_kex() -> HybridSharedSecret {
Self::client_offer()
}
}

View File

@ -1,9 +1,12 @@
pub mod aead; pub mod aead;
pub mod kex;
pub mod noise; pub mod noise;
pub mod obfuscation; pub mod obfuscation;
pub use aead::SessionCipher; pub use aead::SessionCipher;
pub use kex::{HybridSharedSecret, KeyExchange};
pub use noise::{NoiseRole, NoiseSession}; pub use noise::{NoiseRole, NoiseSession};
pub use obfuscation::{deobfuscate_packet_inplace, obfuscate_packet_inplace, derive_obfuscation_key, derive_psk}; pub use obfuscation::{
deobfuscate_header_inplace, deobfuscate_packet_inplace, obfuscate_packet_inplace,
derive_obfuscation_key, derive_psk, derive_all_secrets, DerivedSecrets,
derive_junk_marker, current_junk_window, JUNK_MARKER_WINDOW_SECS,
};

View File

@ -1,4 +1,4 @@
use snow::{Builder, HandshakeState, TransportState}; use snow::{Builder, HandshakeState};
use crate::protocol::ProtocolError; use crate::protocol::ProtocolError;
@ -10,9 +10,15 @@ pub enum NoiseRole {
Responder, Responder,
} }
pub enum NoiseSession { /// A Noise handshake in progress. OSTP does not use snow's transport mode: once
Handshake(HandshakeState), /// the handshake finishes we extract the raw Split() keys (see [`raw_split`])
Transport(TransportState), /// and drive our own out-of-order AEAD (see `crypto::aead`), because the wire
/// protocol needs explicit per-frame nonces for reordering that snow's internal
/// nonce counter can't express.
///
/// [`raw_split`]: NoiseSession::raw_split
pub struct NoiseSession {
handshake: Box<HandshakeState>,
} }
impl NoiseSession { impl NoiseSession {
@ -36,50 +42,92 @@ impl NoiseSession {
.map_err(|_| ProtocolError::Crypto("noise-responder".to_string()))?, .map_err(|_| ProtocolError::Crypto("noise-responder".to_string()))?,
}; };
Ok(Self::Handshake(handshake)) Ok(Self { handshake: Box::new(handshake) })
} }
pub fn write_handshake(&mut self, payload: &[u8], out: &mut [u8]) -> Result<usize, ProtocolError> { pub fn write_handshake(&mut self, payload: &[u8], out: &mut [u8]) -> Result<usize, ProtocolError> {
match self { self.handshake
NoiseSession::Handshake(hs) => hs .write_message(payload, out)
.write_message(payload, out) .map_err(|_| ProtocolError::Crypto("noise-write".to_string()))
.map_err(|_| ProtocolError::Crypto("noise-write".to_string())),
NoiseSession::Transport(_) => Err(ProtocolError::State("noise already in transport".to_string())),
}
} }
pub fn read_handshake(&mut self, input: &[u8], out: &mut [u8]) -> Result<usize, ProtocolError> { pub fn read_handshake(&mut self, input: &[u8], out: &mut [u8]) -> Result<usize, ProtocolError> {
match self { self.handshake
NoiseSession::Handshake(hs) => hs .read_message(input, out)
.read_message(input, out) .map_err(|e| ProtocolError::Crypto(format!("noise-read: {:?}", e)))
.map_err(|_| ProtocolError::Crypto("noise-read".to_string())),
NoiseSession::Transport(_) => Err(ProtocolError::State("noise already in transport".to_string())),
}
} }
pub fn handshake_hash(&self, out: &mut [u8]) -> Result<(), ProtocolError> { /// Derive the two directional transport keys via Noise's Split().
match self { ///
NoiseSession::Handshake(hs) => { /// SECURITY: keys are taken from the final chaining key `ck` (which absorbs
let hash = hs.get_handshake_hash(); /// the ephemeral `ee` DH result via MixKey), NOT from the handshake hash `h`
if out.len() != hash.len() { /// (which only absorbs public transcript data — ephemeral pubkeys and
return Err(ProtocolError::Crypto("handshake hash length mismatch".to_string())); /// ciphertexts — and never the DH secret). Deriving from `ck` is what gives
} /// the session forward secrecy: an adversary who later learns the PSK still
out.copy_from_slice(hash); /// cannot recompute these keys without the ephemeral private keys, which are
Ok(()) /// discarded after the handshake.
} ///
NoiseSession::Transport(_) => Err(ProtocolError::State("noise already in transport".to_string())), /// Must only be called once the handshake is finished (both messages of the
} /// NNpsk0 exchange processed); at that point `ck` is final. Returns
} /// `(send_key, recv_key)` for the given role, matching snow's TransportState
/// direction mapping: split output `.0` is initiator→responder, `.1` is
pub fn into_transport(self) -> Result<Self, ProtocolError> { /// responder→initiator.
match self { pub fn raw_split(&mut self, role: NoiseRole) -> Result<([u8; 32], [u8; 32]), ProtocolError> {
NoiseSession::Handshake(hs) => { if !self.handshake.is_handshake_finished() {
let transport = hs return Err(ProtocolError::State("handshake not finished at key split".to_string()));
.into_transport_mode()
.map_err(|_| ProtocolError::Crypto("noise-transport".to_string()))?;
Ok(NoiseSession::Transport(transport))
}
NoiseSession::Transport(_) => Ok(self),
} }
let (k0, k1) = self.handshake.dangerously_get_raw_split();
Ok(match role {
// Initiator sends on .0 (i→r), receives on .1 (r→i).
NoiseRole::Initiator => (k0, k1),
// Responder is the mirror image.
NoiseRole::Responder => (k1, k0),
})
}
}
#[cfg(test)]
mod tests {
use super::*;
/// Drive a full NNpsk0 handshake and confirm both sides derive matching
/// directional keys. This guards the .0/.1 → send/recv role mapping in
/// `raw_split`: if it were wrong, the two sides' send/recv keys wouldn't
/// cross-match and the transport channel would silently fail to decrypt.
#[test]
fn raw_split_keys_agree_across_roles() {
let psk = [7u8; 32];
let mut initiator = NoiseSession::new(NoiseRole::Initiator, &psk).unwrap();
let mut responder = NoiseSession::new(NoiseRole::Responder, &psk).unwrap();
// msg1: initiator -> responder
let mut buf1 = [0u8; 1024];
let n1 = initiator.write_handshake(&[], &mut buf1).unwrap();
let mut tmp = [0u8; 1024];
responder.read_handshake(&buf1[..n1], &mut tmp).unwrap();
// msg2: responder -> initiator
let mut buf2 = [0u8; 1024];
let n2 = responder.write_handshake(&[], &mut buf2).unwrap();
initiator.read_handshake(&buf2[..n2], &mut tmp).unwrap();
let (i_send, i_recv) = initiator.raw_split(NoiseRole::Initiator).unwrap();
let (r_send, r_recv) = responder.raw_split(NoiseRole::Responder).unwrap();
// What the initiator sends with, the responder must receive with.
assert_eq!(i_send, r_recv, "initiator send key must equal responder recv key");
assert_eq!(r_send, i_recv, "responder send key must equal initiator recv key");
// The two directions use distinct keys.
assert_ne!(i_send, i_recv, "the two directions must not share a key");
}
/// raw_split must refuse to hand out keys before the handshake is complete —
/// keys taken from a half-mixed chaining key would be wrong and insecure.
#[test]
fn raw_split_rejected_before_handshake_finishes() {
let psk = [9u8; 32];
let mut initiator = NoiseSession::new(NoiseRole::Initiator, &psk).unwrap();
// No messages exchanged yet: handshake not finished.
assert!(initiator.raw_split(NoiseRole::Initiator).is_err());
} }
} }

View File

@ -1,90 +1,286 @@
use sha2::{Digest, Sha256}; // =============================================================================
// OSTP Key Derivation — Kerckhoffs's Principle
// =============================================================================
//
// All protocol secrets (PSK, obfuscation key, padding parameters) are derived
// exclusively from the access key using HKDF-SHA256. There are NO hardcoded
// salt strings, protocol identifiers, or magic constants in this module.
//
// An adversary who reverse-engineers the binary sees only generic HMAC/SHA-256
// operations with no protocol-specific strings to search for. Building a DPI
// filter requires knowledge of the access key.
// =============================================================================
use sha2::Sha256;
use hmac::{Hmac, Mac};
type HmacSha256 = Hmac<Sha256>;
// ── HKDF-SHA256 (RFC 5869) ──────────────────────────────────────────────────
// Implemented inline to avoid adding a dependency. Uses only hmac + sha2.
/// HKDF-Extract: PRK = HMAC-SHA256(salt, IKM)
fn hkdf_extract(salt: &[u8], ikm: &[u8]) -> [u8; 32] {
let mut mac = HmacSha256::new_from_slice(salt).expect("HMAC accepts any key length");
mac.update(ikm);
let result = mac.finalize().into_bytes();
let mut prk = [0u8; 32];
prk.copy_from_slice(&result);
prk
}
/// HKDF-Expand: OKM = T(1) || T(2) || ... truncated to `len` bytes.
/// T(i) = HMAC-SHA256(PRK, T(i-1) || info || i)
fn hkdf_expand(prk: &[u8; 32], info: &[u8], len: usize) -> Vec<u8> {
let mut okm = Vec::with_capacity(len);
let mut t = Vec::new();
let mut counter = 1u8;
while okm.len() < len {
let mut mac = HmacSha256::new_from_slice(prk).expect("HMAC accepts any key length");
mac.update(&t);
mac.update(info);
mac.update(&[counter]);
let block = mac.finalize().into_bytes();
t = block.to_vec();
okm.extend_from_slice(&t[..t.len().min(len - okm.len() + t.len()).min(t.len())]);
counter = counter.wrapping_add(1);
}
okm.truncate(len);
okm
}
/// Derive all protocol secrets from a single access key.
/// Returns (obfuscation_key, psk, handshake_pad_min, handshake_pad_max).
///
/// The derivation uses the access key as both IKM and salt material,
/// split into two halves. No fixed strings are used — the access key
/// alone determines all derived values.
#[derive(Clone)]
pub struct DerivedSecrets {
pub obfuscation_key: [u8; 8],
pub psk: [u8; 32],
pub handshake_pad_min: usize,
pub handshake_pad_max: usize,
}
// NOTE: the junk marker is NOT part of DerivedSecrets — it is time-rotating and
// derived separately per window via `derive_junk_marker` (see below), so it
// carries no static per-user signature.
/// OSTP wire protocol version. Mixed into key derivation (NOT sent on the
/// wire) so peers running incompatible versions derive entirely different
/// secrets and therefore cannot deobfuscate / decrypt each other's traffic.
///
/// This is a hard, deterministic version gate that needs NO plaintext version
/// byte on the wire — a constant marker would defeat the project's stealth
/// north-star ("no recognizable header"). A pre-0.4.0 client (which derived
/// without a version) produces a different obfuscation key, so a 0.4.0 server
/// cannot recover its handshake header and rejects it as an unauthorized probe.
///
/// Bump this on any wire-breaking protocol change. 0.4.0 = version 4;
/// version 5 (0.4.x hardening) moved transport keys from the handshake hash to
/// Noise's Split() output — a wire-breaking crypto change, so old peers must not
/// interop (they would derive different session keys and fail decryption).
pub const PROTOCOL_VERSION: u8 = 5;
pub fn derive_all_secrets(access_key: &[u8]) -> DerivedSecrets {
derive_all_secrets_versioned(access_key, PROTOCOL_VERSION)
}
/// Version-parameterised derivation. `derive_all_secrets` always pins the
/// current `PROTOCOL_VERSION`; this form exists so tests can prove that a
/// different version yields incompatible secrets (the version gate).
pub(crate) fn derive_all_secrets_versioned(access_key: &[u8], version: u8) -> DerivedSecrets {
// Split the key hash into two halves for salt/info separation.
// This avoids using any hardcoded strings while still providing
// domain separation between the derived values.
use sha2::Digest;
let key_hash = sha2::Sha256::digest(access_key);
let salt = &key_hash[..16];
let info_base = &key_hash[16..];
// Mix the protocol version into the IKM so a different version produces a
// completely different PRK → different obf_key / psk / padding. This is the
// wire-version gate: it is invisible on the wire (only the derived output,
// which is already indistinguishable from random, ever leaves the host).
let mut ikm = Vec::with_capacity(access_key.len() + 1);
ikm.extend_from_slice(access_key);
ikm.push(version);
// Extract PRK from version-tagged access key using its hash as salt
let prk = hkdf_extract(salt, &ikm);
// Derive obfuscation key (8 bytes) — info = key_hash[16..] || 0x01
let mut obf_info = info_base.to_vec();
obf_info.push(0x01);
let obf_bytes = hkdf_expand(&prk, &obf_info, 8);
let mut obfuscation_key = [0u8; 8];
obfuscation_key.copy_from_slice(&obf_bytes);
// Derive PSK (32 bytes) — info = key_hash[16..] || 0x02
let mut psk_info = info_base.to_vec();
psk_info.push(0x02);
let psk_bytes = hkdf_expand(&prk, &psk_info, 32);
let mut psk = [0u8; 32];
psk.copy_from_slice(&psk_bytes);
// Derive handshake padding range (2 bytes) — info = key_hash[16..] || 0x03
// This makes different access keys produce different handshake sizes,
// preventing DPI from building a universal size-based filter.
let mut pad_info = info_base.to_vec();
pad_info.push(0x03);
let pad_bytes = hkdf_expand(&prk, &pad_info, 2);
// Map to range: min ∈ [16..80], max ∈ [min+48..min+176]
let pad_min = 16 + (pad_bytes[0] as usize % 64); // 16-79
let pad_max = pad_min + 48 + (pad_bytes[1] as usize % 128); // +48..+175
DerivedSecrets {
obfuscation_key,
psk,
handshake_pad_min: pad_min,
handshake_pad_max: pad_max,
}
}
/// Window length (seconds) for the rotating junk marker. The marker changes
/// every window, so junk carries no static per-user fingerprint on the wire;
/// the server checks the current and previous window to absorb clock skew.
pub const JUNK_MARKER_WINDOW_SECS: u64 = 60;
/// The current junk-marker time window (unix seconds / window length).
pub fn current_junk_window() -> u64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs() / JUNK_MARKER_WINDOW_SECS)
.unwrap_or(0)
}
/// Derive the 4-byte junk marker for a given time `window`.
///
/// Uses the same version-gated HKDF scheme as [`derive_all_secrets`], with the
/// window folded into the `info` (label byte `0x04`). Folding in the window
/// makes the marker rotate: to an on-path observer the junk prefix changes every
/// window (no fixed signature), and a captured marker is only valid for ~1
/// window. Only a holder of the access key can compute it, so an outsider cannot
/// forge a silently-dropped junk packet.
pub fn derive_junk_marker(access_key: &[u8], window: u64) -> [u8; 4] {
derive_junk_marker_versioned(access_key, window, PROTOCOL_VERSION)
}
pub(crate) fn derive_junk_marker_versioned(access_key: &[u8], window: u64, version: u8) -> [u8; 4] {
use sha2::Digest;
let key_hash = sha2::Sha256::digest(access_key);
let salt = &key_hash[..16];
let info_base = &key_hash[16..];
let mut ikm = Vec::with_capacity(access_key.len() + 1);
ikm.extend_from_slice(access_key);
ikm.push(version);
let prk = hkdf_extract(salt, &ikm);
// info = key_hash[16..] || 0x04 || window(LE) — same label byte as before,
// now parameterised by the time window.
let mut info = info_base.to_vec();
info.push(0x04);
info.extend_from_slice(&window.to_le_bytes());
let bytes = hkdf_expand(&prk, &info, 4);
let mut marker = [0u8; 4];
marker.copy_from_slice(&bytes);
marker
}
// ── Legacy API (delegates to derive_all_secrets) ─────────────────────────────
pub fn derive_obfuscation_key(access_key: &[u8]) -> [u8; 8] { pub fn derive_obfuscation_key(access_key: &[u8]) -> [u8; 8] {
let mut hasher = Sha256::new(); derive_all_secrets(access_key).obfuscation_key
hasher.update(access_key);
let result = hasher.finalize();
let mut key = [0u8; 8];
key.copy_from_slice(&result[0..8]);
key
} }
pub fn derive_psk(access_key: &[u8]) -> [u8; 32] { pub fn derive_psk(access_key: &[u8]) -> [u8; 32] {
let mut hasher = Sha256::new(); derive_all_secrets(access_key).psk
hasher.update(access_key);
hasher.update(b"-ostp-psk-salt");
let result = hasher.finalize();
let mut psk = [0u8; 32];
psk.copy_from_slice(&result);
psk
} }
// ── Wire Obfuscation ─────────────────────────────────────────────────────────
/// Derives a per-packet mask from the payload following the header.
/// Used by both data and handshake packets so every mask is unique.
fn derive_payload_mask(key: &[u8; 8], payload: &[u8]) -> [u8; 32] {
let mut sample = [0u8; 32];
let take_len = payload.len().min(32);
sample[..take_len].copy_from_slice(&payload[..take_len]);
let mut mac = HmacSha256::new_from_slice(key).expect("HMAC accepts any key length");
mac.update(&sample);
let result = mac.finalize().into_bytes();
let mut mask = [0u8; 32];
mask.copy_from_slice(&result);
mask
}
/// Wire layout for DATA packets:
/// [0..4] = session_id XOR mask[0..4]
/// [4..12] = nonce XOR mask[4..12]
/// [12..] = AEAD ciphertext
/// mask = HMAC-SHA256(obf_key, ciphertext_sample[0..32])
///
/// Wire layout for HANDSHAKE packets:
/// [0..6] = (session_id || noise_len) XOR mask[0..6]
/// [6..] = noise_payload || random_padding
/// mask = HMAC-SHA256(obf_key, noise_payload_sample[0..32])
///
/// In both cases, the mask is derived from the payload that follows the header.
/// Since the payload contains cryptographically random data (AEAD ciphertext
/// or Noise ephemeral key), the mask is unique per packet, making the entire
/// wire output indistinguishable from random noise.
pub fn obfuscate_packet_inplace(raw: &mut [u8], key: &[u8; 8], is_handshake: bool) { pub fn obfuscate_packet_inplace(raw: &mut [u8], key: &[u8; 8], is_handshake: bool) {
if !is_handshake && raw.len() >= 12 { if !is_handshake && raw.len() >= 12 {
// Data packet let header_len = 12;
let mut session_id_bytes = [raw[0], raw[1], raw[2], raw[3]]; if raw.len() > header_len {
let mut nonce_bytes = [ let ciphertext = &raw[header_len..];
raw[4], raw[5], raw[6], raw[7], let mask = derive_payload_mask(key, ciphertext);
raw[8], raw[9], raw[10], raw[11]
];
// 1. Obfuscate nonce with derived key for i in 0..12 {
for i in 0..8 { raw[i] ^= mask[i];
nonce_bytes[i] ^= key[i]; }
} }
} else if is_handshake && raw.len() > 6 {
let payload = &raw[6..];
let mask = derive_payload_mask(key, payload);
// 2. Obfuscate session_id with the REAL (unobfuscated) nonce for i in 0..6 {
let real_nonce = u64::from_be_bytes([ raw[i] ^= mask[i];
raw[4], raw[5], raw[6], raw[7],
raw[8], raw[9], raw[10], raw[11]
]);
let nonce_low_32 = (real_nonce & 0xFFFFFFFF) as u32;
let nonce_low_bytes = nonce_low_32.to_be_bytes();
for i in 0..4 {
session_id_bytes[i] ^= nonce_low_bytes[i];
} }
}
}
// Put them back pub fn deobfuscate_header_inplace(
raw[0..4].copy_from_slice(&session_id_bytes); header: &mut [u8; 12],
raw[4..12].copy_from_slice(&nonce_bytes); ciphertext: &[u8],
} else if raw.len() >= 4 { key: &[u8; 8],
// Handshake packet (XOR with key) is_handshake: bool,
for i in 0..4 { ) {
raw[i] ^= key[i % 8]; if !is_handshake {
let mask = derive_payload_mask(key, ciphertext);
for i in 0..12 {
header[i] ^= mask[i];
} }
} }
} }
pub fn deobfuscate_packet_inplace(raw: &mut [u8], key: &[u8; 8], is_handshake: bool) { pub fn deobfuscate_packet_inplace(raw: &mut [u8], key: &[u8; 8], is_handshake: bool) {
if !is_handshake && raw.len() >= 12 { if !is_handshake && raw.len() >= 12 {
// Data packet let (header_slice, ciphertext) = raw.split_at_mut(12);
let mut nonce_bytes = [ let mut header = [0u8; 12];
raw[4], raw[5], raw[6], raw[7], header.copy_from_slice(header_slice);
raw[8], raw[9], raw[10], raw[11] deobfuscate_header_inplace(&mut header, ciphertext, key, is_handshake);
]; header_slice.copy_from_slice(&header);
} else if is_handshake && raw.len() > 6 {
let payload = &raw[6..];
let mask = derive_payload_mask(key, payload);
// 1. Recover real nonce by XORing with key for i in 0..6 {
for i in 0..8 { raw[i] ^= mask[i];
nonce_bytes[i] ^= key[i];
}
let real_nonce = u64::from_be_bytes(nonce_bytes);
let nonce_low_32 = (real_nonce & 0xFFFFFFFF) as u32;
let nonce_low_bytes = nonce_low_32.to_be_bytes();
// 2. Recover session_id by XORing with recovered nonce
let mut session_id_bytes = [raw[0], raw[1], raw[2], raw[3]];
for i in 0..4 {
session_id_bytes[i] ^= nonce_low_bytes[i];
}
// Put them back
raw[0..4].copy_from_slice(&session_id_bytes);
raw[4..12].copy_from_slice(&nonce_bytes);
} else if raw.len() >= 4 {
// Handshake packet
for i in 0..4 {
raw[i] ^= key[i % 8];
} }
} }
} }
#[cfg(test)]
#[path = "obfuscation_tests.rs"]
mod obfuscation_tests;

View File

@ -0,0 +1,219 @@
#[cfg(test)]
mod tests {
use crate::crypto::obfuscation::*;
/// Verifies that derive_all_secrets is deterministic — same input always
/// produces the same output.
#[test]
fn test_derive_deterministic() {
let key = b"test_access_key_12345";
let s1 = derive_all_secrets(key);
let s2 = derive_all_secrets(key);
assert_eq!(s1.obfuscation_key, s2.obfuscation_key, "obf_key must be deterministic");
assert_eq!(s1.psk, s2.psk, "psk must be deterministic");
assert_eq!(s1.handshake_pad_min, s2.handshake_pad_min, "pad_min must be deterministic");
assert_eq!(s1.handshake_pad_max, s2.handshake_pad_max, "pad_max must be deterministic");
}
/// Verifies that different keys produce different secrets.
#[test]
fn test_derive_different_keys() {
let s1 = derive_all_secrets(b"key_alpha");
let s2 = derive_all_secrets(b"key_beta");
assert_ne!(s1.obfuscation_key, s2.obfuscation_key);
assert_ne!(s1.psk, s2.psk);
}
/// Verifies that the legacy API matches derive_all_secrets output.
#[test]
fn test_legacy_api_consistency() {
let key = b"consistency_check_key";
let secrets = derive_all_secrets(key);
assert_eq!(secrets.obfuscation_key, derive_obfuscation_key(key));
assert_eq!(secrets.psk, derive_psk(key));
}
/// Verifies handshake padding range is within valid bounds.
#[test]
fn test_padding_range_valid() {
for i in 0..100 {
let key = format!("test_key_{}", i);
let s = derive_all_secrets(key.as_bytes());
assert!(s.handshake_pad_min >= 16, "pad_min must be >= 16, got {}", s.handshake_pad_min);
assert!(s.handshake_pad_min < 80, "pad_min must be < 80, got {}", s.handshake_pad_min);
assert!(s.handshake_pad_max > s.handshake_pad_min, "pad_max must be > pad_min");
assert!(s.handshake_pad_max <= s.handshake_pad_min + 175,
"pad_max out of range: {} > {} + 175", s.handshake_pad_max, s.handshake_pad_min);
}
}
/// End-to-end test: obfuscate a handshake packet on the "client" side,
/// then deobfuscate on the "server" side using the same access key.
/// This simulates the exact flow that caused "Unauthorized probe" errors.
#[test]
fn test_handshake_obfuscation_roundtrip() {
let access_key = b"my_real_access_key_v2";
let secrets = derive_all_secrets(access_key);
// Simulate client building a handshake packet
let session_id: u32 = 0xDEADBEEF;
let fake_noise_payload = [0x42u8; 48]; // Typical Noise_NNpsk0 handshake size
let noise_len = fake_noise_payload.len() as u16;
let mut packet = Vec::new();
packet.extend_from_slice(&session_id.to_be_bytes()); // [0..4]
packet.extend_from_slice(&noise_len.to_be_bytes()); // [4..6]
packet.extend_from_slice(&fake_noise_payload); // [6..54]
packet.extend_from_slice(&[0xAA; 64]); // padding
// Obfuscate (client side)
obfuscate_packet_inplace(&mut packet, &secrets.obfuscation_key, true);
// At this point, bytes [0..6] are masked and should look random
let masked_sid = u32::from_be_bytes([packet[0], packet[1], packet[2], packet[3]]);
assert_ne!(masked_sid, session_id, "session_id must be masked on wire");
// Deobfuscate (server side) — using same key
deobfuscate_packet_inplace(&mut packet, &secrets.obfuscation_key, true);
// Verify session_id is recovered
let recovered_sid = u32::from_be_bytes([packet[0], packet[1], packet[2], packet[3]]);
assert_eq!(recovered_sid, session_id, "session_id must be recovered after deobfuscation");
// Verify noise_len is recovered
let recovered_noise_len = u16::from_be_bytes([packet[4], packet[5]]);
assert_eq!(recovered_noise_len, noise_len, "noise_len must be recovered");
// Verify noise payload is intact
assert_eq!(&packet[6..6 + noise_len as usize], &fake_noise_payload,
"noise payload must be intact after round-trip");
}
/// Verifies that deobfuscating with the WRONG key does NOT recover
/// the session_id — this is what prevents unauthorized probes.
#[test]
fn test_wrong_key_produces_garbage() {
let correct_key = b"correct_key";
let wrong_key = b"wrong_key";
let correct_secrets = derive_all_secrets(correct_key);
let wrong_secrets = derive_all_secrets(wrong_key);
let session_id: u32 = 0x12345678;
let fake_noise = [0x55u8; 48];
let mut packet = Vec::new();
packet.extend_from_slice(&session_id.to_be_bytes());
packet.extend_from_slice(&(48u16).to_be_bytes());
packet.extend_from_slice(&fake_noise);
packet.extend_from_slice(&[0x00; 32]);
// Obfuscate with correct key
obfuscate_packet_inplace(&mut packet, &correct_secrets.obfuscation_key, true);
// Try to deobfuscate with WRONG key
let mut wrong_trial = packet.clone();
deobfuscate_packet_inplace(&mut wrong_trial, &wrong_secrets.obfuscation_key, true);
let wrong_sid = u32::from_be_bytes([wrong_trial[0], wrong_trial[1], wrong_trial[2], wrong_trial[3]]);
// Should NOT match — this is what the dispatcher checks
assert_ne!(wrong_sid, session_id, "wrong key must NOT recover session_id");
// Deobfuscate with correct key — must work
deobfuscate_packet_inplace(&mut packet, &correct_secrets.obfuscation_key, true);
let correct_sid = u32::from_be_bytes([packet[0], packet[1], packet[2], packet[3]]);
assert_eq!(correct_sid, session_id, "correct key must recover session_id");
}
/// §C version gate: a peer on a different PROTOCOL_VERSION derives
/// different secrets, so a handshake obfuscated with the OLD version's key
/// does NOT deobfuscate to a valid session_id under the current version.
/// This is exactly what makes an old (pre-0.4.0) client fail to connect to
/// a new server — with no plaintext version marker on the wire.
#[test]
fn test_protocol_version_gates_old_clients() {
let key = b"shared_access_key_across_versions";
let new = derive_all_secrets(key); // == derive_all_secrets_versioned(key, PROTOCOL_VERSION)
let old = derive_all_secrets_versioned(key, PROTOCOL_VERSION.wrapping_sub(1));
// Different protocol version → different derived secrets.
assert_ne!(new.obfuscation_key, old.obfuscation_key, "version must change obf_key");
assert_ne!(new.psk, old.psk, "version must change psk");
// Concretely: a handshake the old client obfuscated with its key does
// not recover a valid session_id when the new server deobfuscates it.
let session_id: u32 = 0x11223344;
let noise = [0x33u8; 48];
let mut pkt = Vec::new();
pkt.extend_from_slice(&session_id.to_be_bytes());
pkt.extend_from_slice(&(noise.len() as u16).to_be_bytes());
pkt.extend_from_slice(&noise);
pkt.extend_from_slice(&[0u8; 32]);
obfuscate_packet_inplace(&mut pkt, &old.obfuscation_key, true); // old client
deobfuscate_packet_inplace(&mut pkt, &new.obfuscation_key, true); // new server
let recovered = u32::from_be_bytes([pkt[0], pkt[1], pkt[2], pkt[3]]);
assert_ne!(recovered, session_id, "old-version client must NOT be accepted by new server");
}
/// Verifies data packet obfuscation round-trip (non-handshake path).
#[test]
fn test_data_packet_obfuscation_roundtrip() {
let secrets = derive_all_secrets(b"data_test_key");
let session_id: u32 = 0xCAFEBABE;
let nonce: u64 = 42;
let ciphertext = [0x77u8; 64];
let mut packet = Vec::new();
packet.extend_from_slice(&session_id.to_be_bytes()); // [0..4]
packet.extend_from_slice(&nonce.to_be_bytes()); // [4..12]
packet.extend_from_slice(&ciphertext); // [12..]
obfuscate_packet_inplace(&mut packet, &secrets.obfuscation_key, false);
// Masked
let masked_sid = u32::from_be_bytes([packet[0], packet[1], packet[2], packet[3]]);
assert_ne!(masked_sid, session_id);
// Deobfuscate
deobfuscate_packet_inplace(&mut packet, &secrets.obfuscation_key, false);
let recovered_sid = u32::from_be_bytes([packet[0], packet[1], packet[2], packet[3]]);
let recovered_nonce = u64::from_be_bytes([
packet[4], packet[5], packet[6], packet[7],
packet[8], packet[9], packet[10], packet[11],
]);
assert_eq!(recovered_sid, session_id);
assert_eq!(recovered_nonce, nonce);
assert_eq!(&packet[12..], &ciphertext);
}
/// The junk marker must: be stable within a window (client and server agree),
/// rotate across windows (no static on-wire fingerprint), and differ per key
/// (one user's marker never silently-drops on another user's flow).
#[test]
fn test_junk_marker_rotation() {
let key_a = b"access-key-alpha";
let key_b = b"access-key-bravo";
// Stable within a window.
assert_eq!(derive_junk_marker(key_a, 1000), derive_junk_marker(key_a, 1000));
// Rotates across adjacent windows.
assert_ne!(derive_junk_marker(key_a, 1000), derive_junk_marker(key_a, 1001));
assert_ne!(derive_junk_marker(key_a, 1000), derive_junk_marker(key_a, 999));
// Distinct per key within the same window.
assert_ne!(derive_junk_marker(key_a, 1000), derive_junk_marker(key_b, 1000));
// A different protocol version yields a different marker (version gate).
assert_ne!(
derive_junk_marker_versioned(key_a, 1000, PROTOCOL_VERSION),
derive_junk_marker_versioned(key_a, 1000, PROTOCOL_VERSION.wrapping_add(1)),
);
}
}

View File

@ -35,7 +35,6 @@ impl TryFrom<u8> for FrameKind {
pub struct FrameHeader { pub struct FrameHeader {
pub version: u8, pub version: u8,
pub kind: FrameKind, pub kind: FrameKind,
pub flags: u8,
pub stream_id: u16, pub stream_id: u16,
pub payload_len: u32, pub payload_len: u32,
pub pad_len: u16, pub pad_len: u16,
@ -45,8 +44,10 @@ impl FrameHeader {
pub fn encode(&self, out: &mut BytesMut) { pub fn encode(&self, out: &mut BytesMut) {
out.put_u8(self.version); out.put_u8(self.version);
out.put_u8(self.kind as u8); out.put_u8(self.kind as u8);
out.put_u8(self.flags); // Anti-DPI: reserved bytes filled with random data instead of zeros
out.put_u8(0); // reserved // to prevent known-plaintext fingerprinting inside encrypted frames
let rnd: u16 = rand::random();
out.put_u16(rnd);
out.put_u16(self.stream_id); out.put_u16(self.stream_id);
out.put_u32(self.payload_len); out.put_u32(self.payload_len);
out.put_u16(self.pad_len); out.put_u16(self.pad_len);
@ -59,7 +60,7 @@ impl FrameHeader {
let version = buf[0]; let version = buf[0];
let kind = FrameKind::try_from(buf[1])?; let kind = FrameKind::try_from(buf[1])?;
let flags = buf[2]; // buf[2] and buf[3] are reserved
let stream_id = u16::from_be_bytes([buf[4], buf[5]]); let stream_id = u16::from_be_bytes([buf[4], buf[5]]);
let payload_len = u32::from_be_bytes([buf[6], buf[7], buf[8], buf[9]]); let payload_len = u32::from_be_bytes([buf[6], buf[7], buf[8], buf[9]]);
let pad_len = u16::from_be_bytes([buf[10], buf[11]]); let pad_len = u16::from_be_bytes([buf[10], buf[11]]);
@ -67,7 +68,6 @@ impl FrameHeader {
Ok(Self { Ok(Self {
version, version,
kind, kind,
flags,
stream_id, stream_id,
payload_len, payload_len,
pad_len, pad_len,
@ -101,7 +101,15 @@ impl FramedPacket {
let payload_len = header.payload_len as usize; let payload_len = header.payload_len as usize;
let pad_len = header.pad_len as usize; let pad_len = header.pad_len as usize;
let expected = FRAME_HEADER_LEN + payload_len + pad_len; // Use checked arithmetic: payload_len is a u32 from the (decrypted, but
// still to-be-trusted) header, and on 32-bit targets — MIPS/ARMv7
// routers are supported build targets — header+payload+pad can overflow
// usize and wrap to a small value that spuriously passes the length
// check, causing an out-of-range slice below.
let expected = FRAME_HEADER_LEN
.checked_add(payload_len)
.and_then(|v| v.checked_add(pad_len))
.ok_or_else(|| ProtocolError::Framing("frame length overflow".to_string()))?;
if buf.len() < expected { if buf.len() < expected {
return Err(ProtocolError::Framing("frame body truncated".to_string())); return Err(ProtocolError::Framing("frame body truncated".to_string()));
} }

View File

@ -10,15 +10,15 @@ pub enum TrafficProfile {
impl TrafficProfile { impl TrafficProfile {
pub fn target_size(&self, current: usize) -> usize { pub fn target_size(&self, current: usize) -> usize {
match self { match self {
TrafficProfile::JsonRpc => align_up(current.max(220), 64).min(1408), TrafficProfile::JsonRpc => align_up(current.max(220), 64).min(1280),
TrafficProfile::HttpsBurst => align_up(current.max(1200), 128).min(1472), TrafficProfile::HttpsBurst => align_up(current.max(1200), 128).min(1280),
TrafficProfile::VideoStream => align_up(current.max(900), 188).min(1472), TrafficProfile::VideoStream => align_up(current.max(900), 188).min(1280),
} }
} }
} }
fn align_up(v: usize, align: usize) -> usize { fn align_up(v: usize, align: usize) -> usize {
((v + align - 1) / align) * align v.div_ceil(align) * align
} }
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
@ -45,11 +45,11 @@ impl AdaptivePadder {
} }
pub fn padding_for_len(&self, payload_len: usize) -> usize { pub fn padding_for_len(&self, payload_len: usize) -> usize {
match self.strategy { let raw_pad = match self.strategy {
PaddingStrategy::Fixed(target) => target.saturating_sub(payload_len), PaddingStrategy::Fixed(target) => target.saturating_sub(payload_len),
PaddingStrategy::Adaptive => { PaddingStrategy::Adaptive => {
let base_bucket = 64; let base_bucket = 64;
let bucketized = ((payload_len + base_bucket - 1) / base_bucket) * base_bucket; let bucketized = payload_len.div_ceil(base_bucket) * base_bucket;
let mut target = bucketized.clamp(base_bucket, self.mtu_hint); let mut target = bucketized.clamp(base_bucket, self.mtu_hint);
if target < payload_len { if target < payload_len {
target = payload_len; target = payload_len;
@ -60,7 +60,7 @@ impl AdaptivePadder {
let jitter = if jitter_cap == 0 { let jitter = if jitter_cap == 0 {
0 0
} else { } else {
rand::thread_rng().gen_range(0..=jitter_cap.min(96)) rand::thread_rng().gen_range(0..=jitter_cap.min(256))
}; };
(base_pad + jitter).min(self.max_pad) (base_pad + jitter).min(self.max_pad)
@ -69,7 +69,12 @@ impl AdaptivePadder {
let target = prof.target_size(payload_len); let target = prof.target_size(payload_len);
target.saturating_sub(payload_len).min(self.max_pad) target.saturating_sub(payload_len).min(self.max_pad)
} }
} };
// Strict clamp to ensure total packet size (including overhead) never exceeds mtu_hint
let overhead = 38;
let max_allowed = self.mtu_hint.saturating_sub(payload_len).saturating_sub(overhead);
raw_pad.min(max_allowed)
} }
pub fn build_padding(&self, payload_len: usize) -> Vec<u8> { pub fn build_padding(&self, payload_len: usize) -> Vec<u8> {

View File

@ -1,3 +1,4 @@
pub mod congestion;
pub mod crypto; pub mod crypto;
pub mod framing; pub mod framing;
pub mod protocol; pub mod protocol;

File diff suppressed because it is too large Load Diff

View File

@ -10,6 +10,8 @@ pub enum RelayMessage {
Error(String), Error(String),
Ping(u64), Ping(u64),
Pong(u64), Pong(u64),
UdpAssociate,
UdpData(String, Vec<u8>),
} }
impl RelayMessage { impl RelayMessage {
@ -23,6 +25,17 @@ impl RelayMessage {
RelayMessage::Error(msg) => encode_with_len(6, msg.as_bytes()), RelayMessage::Error(msg) => encode_with_len(6, msg.as_bytes()),
RelayMessage::Ping(ts) => encode_with_len(7, &ts.to_be_bytes()), RelayMessage::Ping(ts) => encode_with_len(7, &ts.to_be_bytes()),
RelayMessage::Pong(ts) => encode_with_len(8, &ts.to_be_bytes()), RelayMessage::Pong(ts) => encode_with_len(8, &ts.to_be_bytes()),
RelayMessage::UdpAssociate => vec![9],
RelayMessage::UdpData(addr, data) => {
let addr_bytes = addr.as_bytes();
let mut buf = Vec::with_capacity(1 + 2 + addr_bytes.len() + 2 + data.len());
buf.push(10);
buf.extend_from_slice(&(addr_bytes.len() as u16).to_be_bytes());
buf.extend_from_slice(addr_bytes);
buf.extend_from_slice(&(data.len() as u16).to_be_bytes());
buf.extend_from_slice(data);
buf
}
} }
} }
@ -51,16 +64,34 @@ impl RelayMessage {
7 => { 7 => {
let payload = decode_with_len(&input[1..])?; let payload = decode_with_len(&input[1..])?;
if payload.len() != 8 { return Err(anyhow!("invalid ping payload len")); } if payload.len() != 8 { return Err(anyhow!("invalid ping payload len")); }
let ts = u64::from_be_bytes(payload.try_into().unwrap()); let ts = u64::from_be_bytes(payload.try_into().map_err(|_| anyhow!("invalid ping payload size"))?);
Ok(RelayMessage::Ping(ts)) Ok(RelayMessage::Ping(ts))
} }
8 => { 8 => {
let payload = decode_with_len(&input[1..])?; let payload = decode_with_len(&input[1..])?;
if payload.len() != 8 { return Err(anyhow!("invalid pong payload len")); } if payload.len() != 8 {
let ts = u64::from_be_bytes(payload.try_into().unwrap()); return Err(anyhow!("invalid pong payload"));
Ok(RelayMessage::Pong(ts)) }
let mut ts = [0u8; 8];
ts.copy_from_slice(payload);
Ok(RelayMessage::Pong(u64::from_be_bytes(ts)))
} }
t => Err(anyhow!("unknown relay message type {t}")), 9 => Ok(RelayMessage::UdpAssociate),
10 => {
if input.len() < 3 { return Err(anyhow!("invalid udp data")); }
let addr_len = u16::from_be_bytes([input[1], input[2]]) as usize;
if input.len() < 3 + addr_len + 2 { return Err(anyhow!("invalid udp data")); }
let addr = String::from_utf8(input[3..3+addr_len].to_vec())
.map_err(|_| anyhow!("invalid utf8 in udp addr"))?;
let data_offset = 3 + addr_len;
let data_len = u16::from_be_bytes([input[data_offset], input[data_offset+1]]) as usize;
if input.len() < data_offset + 2 + data_len { return Err(anyhow!("invalid udp data")); }
let data = input[data_offset+2..data_offset+2+data_len].to_vec();
Ok(RelayMessage::UdpData(addr, data))
}
_ => Err(anyhow!("unknown relay message type {}", input[0])),
} }
} }
} }
@ -84,3 +115,83 @@ fn decode_with_len(input: &[u8]) -> Result<&[u8]> {
} }
Ok(&input[2..2 + len]) Ok(&input[2..2 + len])
} }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_connect_roundtrip() {
let msg = RelayMessage::Connect("example.com:443".to_string());
let encoded = msg.encode();
let decoded = RelayMessage::decode(&encoded).unwrap();
match decoded {
RelayMessage::Connect(addr) => assert_eq!(addr, "example.com:443"),
_ => panic!("expected Connect"),
}
}
#[test]
fn test_data_roundtrip() {
let data = vec![1, 2, 3, 4, 5];
let msg = RelayMessage::Data(data.clone());
let encoded = msg.encode();
let decoded = RelayMessage::decode(&encoded).unwrap();
match decoded {
RelayMessage::Data(d) => assert_eq!(d, data),
_ => panic!("expected Data"),
}
}
#[test]
fn test_simple_tags() {
assert_eq!(RelayMessage::KeepAlive.encode(), vec![3]);
assert_eq!(RelayMessage::Close.encode(), vec![4]);
assert_eq!(RelayMessage::ConnectOk.encode(), vec![5]);
assert!(matches!(RelayMessage::decode(&[3]).unwrap(), RelayMessage::KeepAlive));
assert!(matches!(RelayMessage::decode(&[4]).unwrap(), RelayMessage::Close));
assert!(matches!(RelayMessage::decode(&[5]).unwrap(), RelayMessage::ConnectOk));
}
#[test]
fn test_error_roundtrip() {
let msg = RelayMessage::Error("connection refused".to_string());
let encoded = msg.encode();
match RelayMessage::decode(&encoded).unwrap() {
RelayMessage::Error(e) => assert_eq!(e, "connection refused"),
_ => panic!("expected Error"),
}
}
#[test]
fn test_ping_pong_roundtrip() {
let ts = 1234567890u64;
match RelayMessage::decode(&RelayMessage::Ping(ts).encode()).unwrap() {
RelayMessage::Ping(t) => assert_eq!(t, ts),
_ => panic!("expected Ping"),
}
match RelayMessage::decode(&RelayMessage::Pong(ts).encode()).unwrap() {
RelayMessage::Pong(t) => assert_eq!(t, ts),
_ => panic!("expected Pong"),
}
}
#[test]
fn test_error_cases() {
assert!(RelayMessage::decode(&[]).is_err());
assert!(RelayMessage::decode(&[255]).is_err());
// Truncated: tag=1, len=5, only 2 bytes
assert!(RelayMessage::decode(&[1, 0, 5, b'a', b'b']).is_err());
}
#[test]
fn test_empty_data_roundtrip() {
let encoded = RelayMessage::Data(vec![]).encode();
match RelayMessage::decode(&encoded).unwrap() {
RelayMessage::Data(d) => assert!(d.is_empty()),
_ => panic!("expected Data"),
}
}
}

45
ostp-flutter/.gitignore vendored Normal file
View File

@ -0,0 +1,45 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.build/
.buildlog/
.history
.svn/
.swiftpm/
migrate_working_dir/
# IntelliJ related
*.iml
*.ipr
*.iws
.idea/
# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/
# Flutter/Dart/Pub related
**/doc/api/
**/ios/Flutter/.last_build_id
.dart_tool/
.flutter-plugins-dependencies
.pub-cache/
.pub/
/build/
/coverage/
# Symbolication related
app.*.symbols
# Obfuscation related
app.*.map.json
# Android Studio will place build artifacts here
/android/app/debug
/android/app/profile
/android/app/release

45
ostp-flutter/.metadata Normal file
View File

@ -0,0 +1,45 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.
version:
revision: "db50e20168db8fee486b9abf32fc912de3bc5b6a"
channel: "stable"
project_type: app
# Tracks metadata for the flutter migrate command
migration:
platforms:
- platform: root
create_revision: db50e20168db8fee486b9abf32fc912de3bc5b6a
base_revision: db50e20168db8fee486b9abf32fc912de3bc5b6a
- platform: android
create_revision: db50e20168db8fee486b9abf32fc912de3bc5b6a
base_revision: db50e20168db8fee486b9abf32fc912de3bc5b6a
- platform: ios
create_revision: db50e20168db8fee486b9abf32fc912de3bc5b6a
base_revision: db50e20168db8fee486b9abf32fc912de3bc5b6a
- platform: linux
create_revision: db50e20168db8fee486b9abf32fc912de3bc5b6a
base_revision: db50e20168db8fee486b9abf32fc912de3bc5b6a
- platform: macos
create_revision: db50e20168db8fee486b9abf32fc912de3bc5b6a
base_revision: db50e20168db8fee486b9abf32fc912de3bc5b6a
- platform: web
create_revision: db50e20168db8fee486b9abf32fc912de3bc5b6a
base_revision: db50e20168db8fee486b9abf32fc912de3bc5b6a
- platform: windows
create_revision: db50e20168db8fee486b9abf32fc912de3bc5b6a
base_revision: db50e20168db8fee486b9abf32fc912de3bc5b6a
# User provided section
# List of Local paths (relative to this file) that should be
# ignored by the migrate tool.
#
# Files that are not part of the templates will be ignored by default.
unmanaged_files:
- 'lib/main.dart'
- 'ios/Runner.xcodeproj/project.pbxproj'

17
ostp-flutter/README.md Normal file
View File

@ -0,0 +1,17 @@
# ostp_client
A new Flutter project.
## Getting Started
This project is a starting point for a Flutter application.
A few resources to get you started if this is your first Flutter project:
- [Learn Flutter](https://docs.flutter.dev/get-started/learn-flutter)
- [Write your first Flutter app](https://docs.flutter.dev/get-started/codelab)
- [Flutter learning resources](https://docs.flutter.dev/reference/learning-resources)
For help getting started with Flutter development, view the
[online documentation](https://docs.flutter.dev/), which offers tutorials,
samples, guidance on mobile development, and a full API reference.

View File

@ -0,0 +1,28 @@
# This file configures the analyzer, which statically analyzes Dart code to
# check for errors, warnings, and lints.
#
# The issues identified by the analyzer are surfaced in the UI of Dart-enabled
# IDEs (https://dart.dev/tools#ides-and-editors). The analyzer can also be
# invoked from the command line by running `flutter analyze`.
# The following line activates a set of recommended lints for Flutter apps,
# packages, and plugins designed to encourage good coding practices.
include: package:flutter_lints/flutter.yaml
linter:
# The lint rules applied to this project can be customized in the
# section below to disable rules from the `package:flutter_lints/flutter.yaml`
# included above or to enable additional rules. A list of all available lints
# and their documentation is published at https://dart.dev/lints.
#
# Instead of disabling a lint rule for the entire project in the
# section below, it can also be suppressed for a single line of code
# or a specific dart file by using the `// ignore: name_of_lint` and
# `// ignore_for_file: name_of_lint` syntax on the line or in the file
# producing the lint.
rules:
# avoid_print: false # Uncomment to disable the `avoid_print` rule
# prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule
# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options

14
ostp-flutter/android/.gitignore vendored Normal file
View File

@ -0,0 +1,14 @@
gradle-wrapper.jar
/.gradle
/captures/
/gradlew
/gradlew.bat
/local.properties
GeneratedPluginRegistrant.java
.cxx/
# Remember to never publicly share your keystore.
# See https://flutter.dev/to/reference-keystore
key.properties
**/*.keystore
**/*.jks

View File

@ -0,0 +1,119 @@
import java.io.FileInputStream
import java.util.Properties
plugins {
id("com.android.application")
id("kotlin-android")
// The Flutter Gradle Plugin must be applied after the Android and Kotlin Gradle plugins.
id("dev.flutter.flutter-gradle-plugin")
}
// ── Release signing material ────────────────────────────────────────────────
// Supplied out-of-band and never committed: either an `android/key.properties`
// file (local release builds) or OSTP_KEYSTORE_* environment variables (CI).
//
// This exists because the release build used to be signed with the DEBUG
// keystore (the stock Flutter template TODO). Android identifies an app by
// applicationId + signing key, and refuses to update across a key change. The
// debug keystore is auto-generated per machine, and CI runners are ephemeral,
// so every published build carried a different random key — which is why
// updating on top of a previous install failed with "App not installed" /
// "unable to parse the package" and only a full uninstall+reinstall worked.
val keystoreProperties = Properties().apply {
val propsFile = rootProject.file("key.properties")
if (propsFile.exists()) {
FileInputStream(propsFile).use { load(it) }
}
}
// Blank counts as absent. GitHub Actions substitutes an EMPTY STRING (not an
// unset variable) for a secret that doesn't exist, so `getenv(...) ?: fallback`
// silently kept the empty value — the elvis operator only catches null. That is
// how an unset ANDROID_KEY_PASSWORD ended up being used as the literal key
// password instead of falling back to the store password, producing Gradle's
// "Get Key failed: Given final block not properly padded".
fun signingSetting(propKey: String, envKey: String): String? =
(keystoreProperties.getProperty(propKey) ?: System.getenv(envKey))
?.takeIf { it.isNotBlank() }
val releaseStorePath: String? = signingSetting("storeFile", "OSTP_KEYSTORE_PATH")
val hasReleaseSigning: Boolean = !releaseStorePath.isNullOrBlank()
android {
namespace = "com.ospab.ostp_client"
compileSdk = flutter.compileSdkVersion
ndkVersion = flutter.ndkVersion
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = JavaVersion.VERSION_17.toString()
}
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId = "com.ospab.ostp_client"
// You can update the following values to match your application needs.
// For more information, see: https://flutter.dev/to/review-gradle-config.
minSdk = maxOf(flutter.minSdkVersion, 24)
targetSdk = flutter.targetSdkVersion
versionCode = flutter.versionCode
versionName = flutter.versionName
ndk {
abiFilters += listOf("armeabi-v7a", "arm64-v8a", "x86_64")
}
}
signingConfigs {
create("release") {
if (hasReleaseSigning) {
val store = signingSetting("storePassword", "OSTP_KEYSTORE_PASSWORD")
storeFile = file(releaseStorePath!!)
storePassword = store
keyAlias = signingSetting("keyAlias", "OSTP_KEY_ALIAS")
// PKCS12 (the keytool default since Java 9, and what our upload
// keystore is) cannot hold a key password that differs from the
// store password — the format simply has no place to put one. So
// treat a missing key password as "same as the store password"
// instead of demanding a secret that, for this keystore, can only
// ever be a duplicate. An explicit value still wins, for the older
// JKS format where the two genuinely can differ.
keyPassword = signingSetting("keyPassword", "OSTP_KEY_PASSWORD") ?: store
}
}
}
buildTypes {
release {
// Use the real upload key when one was supplied; otherwise fall back to
// the debug keystore so a plain local `flutter build apk --release`
// still works for development. Anything PUBLISHED must take the first
// branch — a debug-signed build cannot be updated over, and its key is
// machine-local, so it also can't be reproduced later.
if (hasReleaseSigning) {
signingConfig = signingConfigs.getByName("release")
} else {
logger.warn(
"OSTP: no release keystore configured (android/key.properties or " +
"OSTP_KEYSTORE_PATH) - falling back to the DEBUG keystore. This APK " +
"is for local use only: users cannot update over it, and the key is " +
"not reproducible on another machine."
)
signingConfig = signingConfigs.getByName("debug")
}
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
}
}
}
flutter {
source = "../.."
}
dependencies {
implementation("androidx.core:core-ktx:1.13.1")
}

View File

@ -0,0 +1,3 @@
-keep class net.ostp.client.OstpClientSdk { *; }
-keep class com.ospab.ostp_client.OstpVpnService { *; }
-keep class com.ospab.ostp_client.MainActivity { *; }

View File

@ -0,0 +1,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<!-- The INTERNET permission is required for development. Specifically,
the Flutter tool needs it to communicate with the running application
to allow setting breakpoints, to provide hot reload, etc.
-->
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>

View File

@ -0,0 +1,80 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_CONNECTED_DEVICE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<application
android:label="ostp_client"
android:name="${applicationName}"
android:icon="@mipmap/launcher_icon"
android:roundIcon="@mipmap/launcher_icon_round"
android:extractNativeLibs="true">
<activity
android:name=".MainActivity"
android:exported="true"
android:launchMode="singleTop"
android:taskAffinity=""
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<!-- Specifies an Android theme to apply to this Activity as soon as
the Android process has started. This theme is visible to the user
while the Flutter UI initializes. After that, this theme continues
to determine the Window background behind the Flutter UI. -->
<meta-data
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme"
/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<action android:name="android.service.quicksettings.action.QS_TILE_PREFERENCES"/>
</intent-filter>
</activity>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />
<service
android:name=".OstpVpnService"
android:permission="android.permission.BIND_VPN_SERVICE"
android:foregroundServiceType="connectedDevice"
android:exported="false">
<intent-filter>
<action android:name="android.net.VpnService"/>
</intent-filter>
</service>
<!-- Quick Settings Tile -->
<service
android:name=".OstpTileService"
android:icon="@mipmap/launcher_icon"
android:label="OSTP VPN"
android:permission="android.permission.BIND_QUICK_SETTINGS_TILE"
android:exported="true">
<intent-filter>
<action android:name="android.service.quicksettings.action.QS_TILE"/>
</intent-filter>
</service>
</application>
<!-- Required to query activities that can process text, see:
https://developer.android.com/training/package-visibility and
https://developer.android.com/reference/android/content/Intent#ACTION_PROCESS_TEXT.
In particular, this is used by the Flutter engine in io.flutter.plugin.text.ProcessTextPlugin. -->
<queries>
<intent>
<action android:name="android.intent.action.PROCESS_TEXT"/>
<data android:mimeType="text/plain"/>
</intent>
</queries>
</manifest>

View File

@ -0,0 +1,156 @@
package com.ospab.ostp_client
import android.content.Intent
import android.net.VpnService
import androidx.annotation.NonNull
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.graphics.Canvas
import android.util.Base64
import java.io.ByteArrayOutputStream
class MainActivity : FlutterActivity() {
private val CHANNEL = "com.ospab.ostp/vpn"
private val VPN_REQUEST_CODE = 0x0F
private var pendingConfigJson: String? = null
private fun getAppIconBase64(pm: PackageManager, appInfo: ApplicationInfo): String? {
try {
val drawable = pm.getApplicationIcon(appInfo)
val width = 96
val height = 96
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
drawable.setBounds(0, 0, width, height)
drawable.draw(canvas)
val outputStream = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.PNG, 90, outputStream)
val byteArray = outputStream.toByteArray()
return Base64.encodeToString(byteArray, Base64.NO_WRAP)
} catch (e: Throwable) {
return null
}
}
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
when (call.method) {
"saveConfig" -> {
val configJson = call.argument<String>("configJson")
val prefs = getSharedPreferences("OstpPrefs", android.content.Context.MODE_PRIVATE)
prefs.edit().putString("latest_config_json", configJson).apply()
result.success(true)
}
"startTunnel" -> {
pendingConfigJson = call.argument<String>("configJson")
val intent = VpnService.prepare(this)
if (intent != null) {
startActivityForResult(intent, VPN_REQUEST_CODE)
result.success(true)
} else {
startVpnService()
result.success(true)
}
}
"stopTunnel" -> {
try {
val intent = Intent(this, OstpVpnService::class.java)
intent.action = "STOP"
startService(intent)
result.success(true)
} catch (e: Throwable) {
result.error("ERROR", e.message, null)
}
}
"getLogs" -> {
try {
val logs = net.ostp.client.OstpClientSdk.getLogs()
result.success(logs ?: "[]")
} catch (e: Throwable) {
result.error("ERROR", e.message ?: "Unknown JNI Error", null)
}
}
"clearLogs" -> {
try {
net.ostp.client.OstpClientSdk.getLogs() // Drain
result.success(true)
} catch (e: Throwable) {
result.error("ERROR", e.message, null)
}
}
"isRunning" -> {
result.success(OstpVpnService.isRunning)
}
"getMetrics" -> {
try {
val metrics = net.ostp.client.OstpClientSdk.getMetrics()
result.success(metrics ?: "{}")
} catch (e: Throwable) {
// Surfaced into the in-app log viewer (not just logcat) so a
// broken traffic counter is diagnosable from a user's bug
// report without adb access.
android.util.Log.e("MainActivity", "getMetrics failed", e)
try {
net.ostp.client.OstpClientSdk.addLog("getMetrics failed: ${e.javaClass.simpleName}: ${e.message}")
} catch (_: Throwable) {}
result.error("ERROR", e.message, null)
}
}
"getInstalledApps" -> {
// MethodChannel handlers run on the main/UI thread by default.
// Enumerating every installed package AND decoding+re-encoding
// each one's icon to PNG/base64 is expensive (100+ apps is
// common) — done inline here it blocked the main thread for
// 10-15s, during which Flutter couldn't render ANY frame, not
// even the "loading" spinner, so the screen just appeared to
// hang before jumping straight to the fully-loaded list.
// Do the work on a background thread; only the final
// `result.success(...)` needs to hop back onto the UI thread.
val pm = packageManager
Thread {
try {
val apps = pm.getInstalledApplications(PackageManager.GET_META_DATA)
val list = apps.map { app ->
val isSystem = ((app.flags and ApplicationInfo.FLAG_SYSTEM) != 0) &&
(pm.getLaunchIntentForPackage(app.packageName) == null)
val iconBase64 = getAppIconBase64(pm, app)
mapOf(
"name" to pm.getApplicationLabel(app).toString(),
"package" to app.packageName,
"isSystem" to isSystem,
"icon" to (iconBase64 ?: "")
)
}
runOnUiThread { result.success(list) }
} catch (e: Exception) {
runOnUiThread { result.error("ERROR", e.message, null) }
}
}.start()
}
else -> result.notImplemented()
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == VPN_REQUEST_CODE && resultCode == RESULT_OK) {
startVpnService()
}
super.onActivityResult(requestCode, resultCode, data)
}
private fun startVpnService() {
val intent = Intent(this, OstpVpnService::class.java)
intent.action = "START"
if (pendingConfigJson != null) {
intent.putExtra("configJson", pendingConfigJson)
}
androidx.core.content.ContextCompat.startForegroundService(this, intent)
}
}

View File

@ -0,0 +1,106 @@
package com.ospab.ostp_client
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.os.Build
import android.service.quicksettings.Tile
import android.service.quicksettings.TileService
import androidx.annotation.Keep
import androidx.annotation.RequiresApi
@Keep
@RequiresApi(Build.VERSION_CODES.N)
class OstpTileService : TileService() {
override fun onStartListening() {
super.onStartListening()
updateTile()
}
override fun onClick() {
super.onClick()
if (OstpVpnService.isRunning) {
// Отключить VPN
val stopIntent = Intent(this, OstpVpnService::class.java).apply { action = "STOP" }
startService(stopIntent)
// Обновим плитку сразу
qsTile?.state = Tile.STATE_INACTIVE
qsTile?.label = "OSTP VPN"
qsTile?.updateTile()
} else {
// Включить VPN напрямую
val prefs = getSharedPreferences("OstpPrefs", Context.MODE_PRIVATE)
val configJson = prefs.getString("latest_config_json", null)
if (configJson != null) {
// Check if VPN consent is needed
val vpnIntent = android.net.VpnService.prepare(this)
if (vpnIntent != null) {
// Consent needed, launch app
val appIntent = packageManager.getLaunchIntentForPackage(packageName)?.apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_SINGLE_TOP)
}
if (appIntent != null) {
startActivityAndCollapse(appIntent)
}
return
}
val startIntent = Intent(this, OstpVpnService::class.java).apply {
action = "START"
putExtra("configJson", configJson)
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(startIntent)
} else {
startService(startIntent)
}
qsTile?.state = Tile.STATE_ACTIVE
qsTile?.label = "OSTP VPN"
qsTile?.updateTile()
} else {
// Если конфигурация еще не сохранена, открыть приложение
val appIntent = packageManager.getLaunchIntentForPackage(packageName)?.apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_SINGLE_TOP)
putExtra("tile_connect", true)
}
if (appIntent != null) {
startActivityAndCollapse(appIntent)
}
}
}
}
private fun updateTile() {
val tile = qsTile ?: return
if (OstpVpnService.isRunning) {
tile.label = "OSTP VPN"
tile.state = Tile.STATE_ACTIVE
} else {
tile.label = "OSTP VPN"
tile.state = Tile.STATE_INACTIVE
}
tile.updateTile()
}
companion object {
/**
* Запрашивает обновление плитки быстрых настроек.
* Вызывается из OstpVpnService при изменении состояния.
*/
@Keep
fun requestListeningState(context: Context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
try {
requestListeningState(
context,
ComponentName(context, OstpTileService::class.java)
)
} catch (e: Exception) {
// Плитка может быть не добавлена в панель — это нормально
}
}
}
}
}

View File

@ -0,0 +1,307 @@
package com.ospab.ostp_client
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Intent
import android.content.pm.ServiceInfo
import android.net.VpnService
import android.os.Build
import android.os.ParcelFileDescriptor
import android.os.PowerManager
import android.util.Log
import net.ostp.client.OstpClientSdk
import java.io.IOException
import androidx.annotation.Keep
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.app.ServiceCompat
@Keep
class OstpVpnService : VpnService() {
@Keep
companion object {
@Keep
var isRunning = false
@Keep
var instance: OstpVpnService? = null
private const val NOTIF_ID = 1001
private const val CHANNEL_ID = "ostp_vpn_channel"
private const val WAKE_LOCK_TAG = "ostp:vpn_wakelock"
/**
* Called by OstpClientSdk.notifyNetworkChanged() JNI thunk.
*/
@Keep
@JvmStatic
fun onNetworkChanged() {
android.util.Log.d("OstpVpnService", "onNetworkChanged() signaled to Rust bridge")
}
}
private var vpnInterface: ParcelFileDescriptor? = null
private var wakeLock: PowerManager.WakeLock? = null
private var networkCallback: android.net.ConnectivityManager.NetworkCallback? = null
override fun onCreate() {
super.onCreate()
instance = this
createNotificationChannel()
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val action = intent?.action
if (action == "START") {
val configJson = intent.getStringExtra("configJson") ?: return START_NOT_STICKY
// Launch foreground immediately so Android doesn't kill us
ServiceCompat.startForeground(this, NOTIF_ID, buildNotification(connecting = true), ServiceInfo.FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE)
startVpn(configJson)
} else if (action == "STOP") {
stopVpn()
}
return START_STICKY
}
private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
CHANNEL_ID,
"OSTP VPN",
NotificationManager.IMPORTANCE_LOW
).apply {
description = "OSTP VPN connection status"
setShowBadge(false)
}
val nm = getSystemService(NotificationManager::class.java)
nm.createNotificationChannel(channel)
}
}
private fun buildNotification(connecting: Boolean): Notification {
val stopIntent = PendingIntent.getService(
this,
0,
Intent(this, OstpVpnService::class.java).apply { action = "STOP" },
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
val openIntent = PendingIntent.getActivity(
this,
1,
packageManager.getLaunchIntentForPackage(packageName)
?.apply { addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP) },
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
val (statusText, actionLabel) = if (connecting) {
Pair("Подключение...", "Отмена")
} else {
Pair("Подключено", "Отключить")
}
return NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("OSTP VPN")
.setContentText(statusText)
.setSmallIcon(android.R.drawable.ic_lock_lock)
.setOngoing(true)
.setShowWhen(false)
.setContentIntent(openIntent)
.addAction(android.R.drawable.ic_delete, actionLabel, stopIntent)
.setPriority(NotificationCompat.PRIORITY_LOW)
.build()
}
fun updateNotification(connected: Boolean) {
try {
val nm = NotificationManagerCompat.from(this)
nm.notify(NOTIF_ID, buildNotification(connecting = !connected))
} catch (e: Throwable) {
Log.e("OstpVpnService", "Failed to update notification", e)
}
// Refresh Quick Settings tile state
OstpTileService.requestListeningState(applicationContext)
}
private fun acquireWakeLock() {
if (wakeLock == null) {
val pm = getSystemService(POWER_SERVICE) as PowerManager
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKE_LOCK_TAG)
wakeLock?.acquire(24 * 60 * 60 * 1000L) // Max 24h
Log.d("OstpVpnService", "WakeLock acquired")
}
}
private fun releaseWakeLock() {
try {
wakeLock?.let {
if (it.isHeld) it.release()
}
wakeLock = null
Log.d("OstpVpnService", "WakeLock released")
} catch (e: Throwable) {
Log.e("OstpVpnService", "Error releasing WakeLock", e)
}
}
private fun registerNetworkCallback() {
if (networkCallback != null) return
try {
val cm = getSystemService(android.content.Context.CONNECTIVITY_SERVICE) as android.net.ConnectivityManager
networkCallback = object : android.net.ConnectivityManager.NetworkCallback() {
override fun onAvailable(network: android.net.Network) {
super.onAvailable(network)
OstpClientSdk.notifyNetworkChanged()
}
override fun onLost(network: android.net.Network) {
super.onLost(network)
OstpClientSdk.notifyNetworkChanged()
}
}
val request = android.net.NetworkRequest.Builder()
.addCapability(android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET)
.build()
cm.registerNetworkCallback(request, networkCallback!!)
} catch (e: Throwable) {
Log.e("OstpVpnService", "Failed to register NetworkCallback", e)
}
}
private fun unregisterNetworkCallback() {
try {
if (networkCallback != null) {
val cm = getSystemService(android.content.Context.CONNECTIVITY_SERVICE) as android.net.ConnectivityManager
cm.unregisterNetworkCallback(networkCallback!!)
networkCallback = null
}
} catch (e: Throwable) {
Log.e("OstpVpnService", "Failed to unregister NetworkCallback", e)
}
}
private fun startVpn(configJson: String) {
if (vpnInterface != null) return
acquireWakeLock()
try {
val json = org.json.JSONObject(configJson)
val dnsServer = json.optString("dns_server", "1.1.1.1")
val localProxy = json.optJSONObject("local_proxy")?.optString("bind_addr", "127.0.0.1:1088") ?: "127.0.0.1:1088"
val builder = Builder()
.setSession("OSTP Tunnel")
.addAddress("10.1.0.2", 24)
.addAddress("fd00:1:fd00:1:fd00:1:fd00:1", 128)
.addRoute("0.0.0.0", 0)
.addRoute("::", 0)
.addDnsServer(dnsServer)
.setMtu(Math.max(1280, json.optJSONObject("ostp")?.optInt("mtu", 1140) ?: 1140))
// Always add fallback IPv4 DNS servers
try { builder.addDnsServer("1.1.1.1") } catch (e: Throwable) {}
try { builder.addDnsServer("8.8.8.8") } catch (e: Throwable) {}
// NOTE: Do NOT add IPv6 DNS servers here — Android would send DNS
// queries over IPv6, but our smoltcp TUN stack processes them as
// IPv4 only, causing all DNS to silently fail on LTE (IPv6-only networks).
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
builder.allowBypass()
}
try {
builder.allowFamily(android.system.OsConstants.AF_INET)
builder.allowFamily(android.system.OsConstants.AF_INET6)
} catch (e: Throwable) { }
val appRules = json.optJSONObject("app_rules")
val mode = appRules?.optString("mode", "bypass") ?: "bypass"
val packages = appRules?.optJSONArray("packages")
if (mode == "proxy") {
if (packages != null) {
for (i in 0 until packages.length()) {
val pkg = packages.getString(i)
try {
builder.addAllowedApplication(pkg)
} catch (e: Throwable) {
Log.e("OstpVpnService", "Failed to add allowed application $pkg: $e")
}
}
}
} else {
try {
builder.addDisallowedApplication(applicationContext.packageName)
} catch (e: Throwable) {
Log.e("OstpVpnService", "Failed to disallow our own package: $e")
}
if (packages != null) {
for (i in 0 until packages.length()) {
val pkg = packages.getString(i)
try {
builder.addDisallowedApplication(pkg)
} catch (e: Throwable) {
Log.e("OstpVpnService", "Failed to add disallowed application $pkg: $e")
}
}
}
}
vpnInterface = builder.establish()
val fd = vpnInterface?.fd ?: throw Exception("Failed to get VPN FD")
// CRITICAL: Clear O_CLOEXEC so the child process inherits the TUN file descriptor
try {
android.system.Os.fcntlInt(vpnInterface!!.fileDescriptor, android.system.OsConstants.F_SETFD, 0)
} catch (e: Throwable) {
Log.e("OstpVpnService", "Failed to clear O_CLOEXEC", e)
}
val success = OstpClientSdk.startClient(configJson, fd, "", localProxy)
if (success) {
Log.i("OstpVpnService", "OSTP Rust Core started successfully")
isRunning = true
updateNotification(connected = true)
} else {
Log.e("OstpVpnService", "Failed to start OSTP Rust Core")
stopVpn()
}
} catch (e: Throwable) {
Log.e("OstpVpnService", "Error starting VPN", e)
android.os.Handler(android.os.Looper.getMainLooper()).post {
android.widget.Toast.makeText(applicationContext, "VPN Error: ${e.message}", android.widget.Toast.LENGTH_LONG).show()
}
stopVpn()
}
registerNetworkCallback()
}
private fun stopVpn() {
isRunning = false
releaseWakeLock()
try {
OstpClientSdk.stopClient()
vpnInterface?.close()
vpnInterface = null
} catch (e: IOException) {
Log.e("OstpVpnService", "Error closing VPN interface", e)
}
stopForeground(true)
OstpTileService.requestListeningState(applicationContext)
unregisterNetworkCallback()
stopSelf()
}
override fun onDestroy() {
super.onDestroy()
instance = null
stopVpn()
}
}

View File

@ -0,0 +1,53 @@
package net.ostp.client
import androidx.annotation.Keep
@Keep
object OstpClientSdk {
init {
System.loadLibrary("ostp_jni")
}
@Keep
@JvmStatic
fun protectSocket(fd: Int): Boolean {
var retries = 5
while (retries > 0) {
val service = com.ospab.ostp_client.OstpVpnService.instance
if (service != null) {
val res = service.protect(fd)
android.util.Log.i("OstpClientSdk", "VpnService.protect(socketFd=$fd) -> success=$res")
return res
}
android.util.Log.w("OstpClientSdk", "VpnService instance is null! Retrying... ($retries left)")
Thread.sleep(200)
retries--
}
android.util.Log.e("OstpClientSdk", "VpnService instance is null! Cannot protect socketFd=$fd")
return false
}
@Keep
@JvmStatic
external fun startClient(configJson: String, fd: Int, t2sBinPath: String, localProxy: String): Boolean
@Keep
@JvmStatic
external fun stopClient(): Boolean
@Keep
@JvmStatic
external fun getMetrics(): String
@Keep
@JvmStatic
external fun getLogs(): String
@Keep
@JvmStatic
external fun addLog(logMsg: String)
@Keep
@JvmStatic
external fun notifyNetworkChanged()
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Modify this file to customize your launch splash screen -->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="?android:colorBackground" />
<!-- You can insert your own image assets here -->
<!-- <item>
<bitmap
android:gravity="center"
android:src="@mipmap/launch_image" />
</item> -->
</layer-list>

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Modify this file to customize your launch splash screen -->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/white" />
<!-- You can insert your own image assets here -->
<!-- <item>
<bitmap
android:gravity="center"
android:src="@mipmap/launch_image" />
</item> -->
</layer-list>

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<foreground android:drawable="@mipmap/ic_launcher_foreground"/>
<background android:drawable="@color/ic_launcher_background"/>
</adaptive-icon>

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@color/ic_launcher_background"/>
<foreground android:drawable="@drawable/ic_launcher_foreground"/>
</adaptive-icon>

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.4 KiB

Some files were not shown because too many files have changed in this diff Show More