Dual LAN/WAN Addressing, an IPv6 Trap, and Keeping Offline Copies Alive
Networking and offline recovery in Vela Player 1.2.0 and 1.3.0 (commits b87d562, ed3280b, 24caa23): SourceAddress routing between LAN and DDNS without leaking tokens or breaking posters, SourceReach's IPv6 bare-hostname bug, and preserving downloaded local copies across offline states and library rescans.
Across 1.2.0 and 1.3.0 (commits b87d562, ed3280b, and 24caa23), Vela Player added dual LAN/WAN (DDNS) source addressing and tightened offline playback recovery.
At first glance, “add an external DDNS address to a NAS source” sounds like adding one externalHost string. In practice, it touched media identity, poster and subtitle URL relocation, token-leak boundaries, IPv6 reachability, and library rescan pruning.
One resolver, and never mutate the saved host
entry/src/main/ets/common/SourceAddress.ets is the single decision point for every source endpoint. Each MediaSource keeps three fields: host (primary/LAN), externalHost (WAN/DDNS), and addressMode (Auto, Internal, or External).
Rule one: SourceAddress.resolve(source) returns a shallow copy via withMode() and never overwrites source.host. Library paths, home-screen service cards, and credential bindings depend on the original saved host; mutating it in-place when leaving home would erase the LAN address when returning to home Wi-Fi.
The routing rules in resolve() are concise:
Internalmode: returnssourceunchanged.Externalmode (for hotel or office Wi-Fi where LAN subnets collide or fail): throws ifexternalHostis empty, otherwise substitutesexternalHost.Automode: checksNetProbe.status().kind— prefershoston Wi-Fi/Ethernet and switches toexternalHoston cellular.
Relocating posters, subtitles, and Plex auth without leaking tokens
Dual addressing immediately exposed a second problem: scanning an Emby, Jellyfin, or Plex server on home Wi-Fi saves poster, backdrop, and subtitle URLs pointing at 192.168.x.x. Step outside on cellular and every artwork request fails.
A subtler bug caught in the 1.3.1 review (DR-28) hit Plex specifically: LibraryService.listDirRaw() called SourceAddress.resolve() before passing the source to PlexClient, stripping the LAN origin so PlexClient could no longer map stored URLs back across endpoints.
SourceAddress.resourceUrl(source, url) handles relocation safely:
- Callers must pass the un-resolved
MediaSourcewith both endpoints intact. primaryandexternalendpoints are parsed and sorted longest-path-prefix first (so reverse proxies sharing an origin with different subpaths never double-prepend prefixes).parseResource()rejects backslashes, whitespace, userinfo (@/%), and./..traversal segments even after three rounds of URL decoding.- Only URLs matching the origin and path prefix of one of the configured endpoints are rewritten to the currently selected endpoint; anything else throws (
PlexClient.resolveMetaImage()leaves external public artwork hosts untouched and unauthenticated). That guarantees auth tokens never travel to arbitrary third-party domains.
When a user edits a source’s address, relocateStoredUrl() migrates persisted vela-image:// and direct URLs in SQLite cleanly.
SourceReach, and the IPv6 bare-hostname trap
With dual addresses in place, entry/src/main/ets/services/SourceReach.ets prevents unreachable LAN-only sources from cluttering the library when away from home, using two rules:
- Private host + off-LAN = instant unreachable, without burning 4 seconds on a TCP timeout. Sources explicitly locked to
InternalorExternalbypass this shortcut so cellular VPN setups are still probed directly. - Everything else is probed concurrently (
SourceProbe.reachable()). If the device is completely offline (NetKind.NONE),refresh()clears the unreachable set instead of graying out the entire library — the user already sees “No Network” in the system bar, and downloaded copies remain playable.
While testing isLanHost(), a real device uncovered an IPv6 bug. My original heuristic treated any hostname without a dot . as a bare LAN hostname (like nas or openwrt). Home-broadband IPv6 NAS URLs such as http://[2408:8256::1]:8096 contain colons and zero dots, so isLanHost() marked public IPv6 servers as “LAN-only” and grayed them out on 5G even when directly reachable. Moving colon-based IPv6 parsing above the dotless check fixed it: only fe80::/10 (link-local) and fc00::/7 (ULA) count as LAN, IPv4-mapped ::ffff: delegates recursively, and ::1 / 127.* are treated as local loopback.
Network listeners (netAvailable, netLost, netCapabilitiesChange) debounce for 600ms, skip redundant probes when buildSignature(kind, sources) (id|resolvedHost) is unchanged, and react immediately to offlineChanged() and hideUnreachable toggles.
Keeping offline copies intact through disconnects and rescans
The guiding rule in ed3280b and 24caa23 is that a downloaded local copy is first-class and survives remote outages and rescans:
- Local-first playback:
PlayRequestFactory.fromItem()checksDownloadStore.of()and verifiesfs.accessSync(copy.localPath)before touching any network client. WhenautoQualityselects a lower-bitrate version inforNetwork(), versions are filtered throughSourceReach.itemAvailable(v.id, v.sourceId)(!down.has(sourceId) || offlineItems.has(itemId)), and if the chosen version also has a local copy, it plays locally. - Rescans demote instead of deleting: in
LibraryService.pruneMissing()anddropSuperseded(), any missing remote path or superseded whole-ISO row is checked againstDownloadStore.of(context, it.id). If an offline copy exists,LibraryStore.demoteFromLibrary()setslibraryMember = 0instead of callingLibraryStore.remove(), keeping the item ID, watch progress, downloaded subtitles, and file mapping intact. Even if a copy becomes orphaned from the library view,PlayRequestFactory.fromDownloadTask()launches it directly fromDownloadsPage. - Safe filtering and explicit cleanup: 1.2.0 added an “Offline available” (
离线可看) quick filter inLibraryFilterSheet, andDownloadsPage.etsconfirmation dialogs spell out that deleting or restarting an offline copy leaves the remote source file, library metadata, and watch progress untouched.
Comments