Skip to content

apple: let a provider subclass answer its own app messages - #349

Merged
aimalygin merged 1 commit into
aimalygin:mainfrom
huzgnm:feat/provider-custom-app-messages
Nov 24, 2026
Merged

aimalygin merged 1 commit into
aimalygin:mainfrom
huzgnm:feat/provider-custom-app-messages

Conversation

@huzgnm

@huzgnm huzgnm commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Summary

XrayPacketTunnelProvider keeps its runtime private and answers only the stats and close-connections requests, so a host app cannot ask its tunnel for the connection, accounting, selection or health snapshot, pin a selector group or replace the routing policy without forking the provider.

This adds an open handleCustomAppMessage(_:core:) hook. handleAppMessage calls it with the active core for every message after the close-connections request and before the stats request; a non-nil result is sent back, nil falls through to the built-in handling. The default returns nil, so existing hosts see no change.

I use it in an iOS client to show open connections with per-route traffic, show and pin the server a leastPing balancer picked, and apply routing changes with replaceRoutingPolicy without reconnecting. The README gets a short example; the CHANGELOG gets an Unreleased entry.

Verification

  • Added or updated regression tests — not added: exercising the hook needs a running provider lifecycle; the default implementation returns nil and the built-in requests are handled before / after it unchanged
  • Added or updated benchmarks for performance claims — not applicable
  • Ran formatting, strict Clippy, and relevant tests — Swift only; the change builds in an iOS app (Xcode 27.2, iOS 27.2 SDK) that subclasses the provider and answers its own messages, tested on device
  • Updated compatibility or user documentation — platform/apple/README.md, CHANGELOG.md
  • Used only synthetic endpoints and credentials

Compatibility and security

Additive Swift API in XrayAppleTunnel (one new open method with a default). No change to the C ABI, the Kotlin adapter, provider configuration keys or the built-in messages. A subclass decides what it exposes to its own app; nothing new leaves the extension by default.

XrayPacketTunnelProvider keeps its runtime private and answers only the
stats and close-connections requests, so a host app could not ask its
tunnel for the connection, accounting, selection or health snapshot, pin
a selector group or replace the routing policy without forking the
provider.

Add an open handleCustomAppMessage(_:core:) hook. handleAppMessage calls
it with the active core for every message after the close-connections
request and before the stats request; a non-nil result is sent back,
nil falls through to the built-in handling. The default returns nil, so
existing hosts see no change.
aimalygin added a commit that referenced this pull request Nov 24, 2026
Contributor wave 14: VMess link TLS fields, provider app-message hook, traffic-triggered re-probes (#346, #347, #349, #308)
@aimalygin
aimalygin merged commit ed22ec7 into aimalygin:main Nov 24, 2026
huzgnm pushed a commit to huzgnm/xray-rust that referenced this pull request Nov 24, 2026
handleAppMessage dropped a message that is not valid UTF-8 before the
hook aimalygin#349 added could see it, while the hook's documentation promises
every message the provider does not handle itself. A host whose app and
extension speak a binary format, a property list or protobuf, would
have had its messages answered with nil.

The provider now decodes the text only to recognise its own requests,
which are text: a message that is not names none of them, goes to the
hook, and without an answer from it gets nil as before. Hosts that do
not override the hook see no change.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants