Skip to content

Support URL requests through active core outbounds - #205

Open
eliotcougar wants to merge 5 commits into
2dust:mainfrom
eliotcougar:codex/xray-tun-connection-info
Open

Support URL requests through active core outbounds#205
eliotcougar wants to merge 5 commits into
2dust:mainfrom
eliotcougar:codex/xray-tun-connection-info

Conversation

@eliotcougar

@eliotcougar eliotcougar commented Aug 18, 2026

Copy link
Copy Markdown

Summary

  • add CoreController.GetUrlContent for bounded text requests through the running Xray instance
  • require an explicit outbound tag and force every dial through that outbound
  • expose the first viable principle target of a running balancer
  • add CoreController.DownloadUrlToFile for header-aware, binary-safe streaming to disk
  • make file downloads transactional so incomplete responses cannot replace an existing destination
  • keep the existing delay and lifecycle paths unchanged

Why

Android callers sometimes need to reuse the already-running Xray connection when no local proxy listener exists. The initial use is v2rayNG's current-connection test in native Xray TUN mode, where the configured connection-info URL supplies the displayed country code and IP address.

Calling core.Dial without selecting an outbound is insufficient: normal routing rules may send an app-initiated request through direct or another detour. The result would then describe the wrong connection. Large subscriptions and binary resource files also should not be returned as strings or passed through Binder.

Downstream use

v2rayNG #6109 uses these APIs to:

  • fetch connection information through the active outbound
  • download subscriptions through the running core when native Xray TUN is active and the local proxy is disabled
  • stream geodata and other resource files through the same connection
  • resolve balancer-main to a concrete member for primary policy groups

Ordinary profiles pass the proxy outbound. Policy groups use the first non-empty principle target exposed by Xray's existing routing feature. Strategies with several principle candidates remain best-effort; querying the target does not advance random or round-robin state.

The file-download API is intentionally general and can be reused by other Android callers that need subscriptions or resources to follow an existing Xray-core connection.

Implementation

The shared request helper snapshots the running core instance, applies session.SetForcedOutboundTagToContext inside DialContext, and opens connections with core.Dial. Redirects therefore remain on the requested outbound.

DownloadUrlToFile accepts request headers and a caller-defined timeout. It streams the response to a mode-0600 sibling temporary file, verifies a declared Content-Length, closes the file, and then replaces the destination with os.Rename. Read, write, close, or rename failures remove only the temporary file, preserving any existing destination.

Unsolicited HTTP 206 responses are rejected because the helper never sends a Range request. Other successful 2xx responses remain supported.

The implementation is confined to libv2ray_utils.go; libv2ray_main.go is unchanged.

Validation

  • go test ./...
  • four-ABI gomobile bind AAR build: armeabi-v7a, arm64-v8a, x86, and x86_64
  • generated AAR API and ABI inspection
  • downstream v2rayNG Play Store debug Kotlin compilation and focused HttpUtilTest

@eliotcougar
eliotcougar marked this pull request as ready for review August 18, 2026 14:32
@eliotcougar
eliotcougar marked this pull request as draft August 18, 2026 14:50
@eliotcougar eliotcougar changed the title Expose URL fetching through the active core Fetch URL content through an explicit core outbound Aug 18, 2026
@eliotcougar

Copy link
Copy Markdown
Author

Follow-up correction in 16b52e1: core.Dial uses Xray's normal dispatcher, so the original one-argument method still allowed routing rules to select direct or another detour. GetUrlContent now requires an outbound tag and applies Xray's forcedOutboundTag session metadata before every dial. The downstream v2rayNG caller passes its proxy tag, ensuring the country/IP result represents the selected proxy even when the local proxy listener is disabled.

This explicit contract also makes the API suitable for future subscription and resource-file downloads through the running core: those callers can select the intended outbound instead of inheriting user routing accidentally.

@eliotcougar
eliotcougar marked this pull request as ready for review August 18, 2026 14:58
@eliotcougar
eliotcougar marked this pull request as draft August 18, 2026 15:25
@eliotcougar eliotcougar changed the title Fetch URL content through an explicit core outbound Support URL fetching through active core outbounds Aug 18, 2026
@eliotcougar
eliotcougar marked this pull request as ready for review August 18, 2026 15:51
Add CoreController.GetUrlContent to perform bounded HTTP GET requests with core.Dial. This lets Android callers retrieve response bodies through the running Xray instance without depending on a local proxy inbound.

The API is intended for xray-tun connection metadata and can also support resource or subscription downloads that should follow the active core route.
GetUrlContent previously passed requests to core.Dial without an outbound override, allowing normal routing rules to send platform-initiated downloads through direct or another detour.

Accept an explicit outbound tag and attach it to every dial with Xray session metadata. This preserves dispatcher-managed connections while ensuring redirects and retries continue through the caller-selected outbound.
Add CoreController.GetBalancerPrincipleTarget using Xray's existing routing feature and return the first viable strategy target. This gives Android callers a concrete outbound for policy-group requests without requiring a custom Xray-core API.
@eliotcougar
eliotcougar force-pushed the codex/xray-tun-connection-info branch from 12f008b to 4e640ec Compare August 20, 2026 08:19
Add a binary-safe URL download API that uses the active core and an explicitly selected outbound. Accept request headers and a caller-defined timeout, stream successful responses directly to disk, and remove partial files on failure.
@eliotcougar eliotcougar changed the title Support URL fetching through active core outbounds Support URL requests through active core outbounds Aug 20, 2026
Write responses to a sibling temporary file and replace the destination only after a complete transfer. Reject unsolicited partial-content responses and verify declared response lengths so truncation or write failures leave existing files intact.
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.

1 participant