Add per-client external link controls (#5650)

* Add enable toggle for external client links

* Document external link enable API fields

* Extend external client link metadata

* Fix external subscription cache status updates

* fix(sub): address the review on per-client external link controls

Blocking: the expiry filter dropped legacy rows. expiry_time was added
without a default, so AutoMigrate makes it nullable and backfills NULL,
and `expiry_time = 0 OR expiry_time > ?` is false for NULL under
three-valued logic — every external link written before the upgrade
vanished from all subscriptions. Add `default:0` on expiry_time and
last_fetch_at, make the predicate NULL-tolerant, and backfill the NULLs
a pre-fix build could already have written.

Rework fetch-status recording. It ran inside the singleflight in-flight
window, so every goroutine parked on the shared fetch waited for a DB
write to commit on the public, unauthenticated subscription path — and
because it was keyed on the row id, waiters and cache hits recorded
nothing, leaving rows that lost the race stuck on "Not fetched yet"
forever. fetchSubscriptionLinks now reports whether it did the network
fetch and expandEntry records afterwards, off the serving path, keyed on
kind+value so every row sharing the URL is stamped by the one fetch.
Keying on value also closes the recycled-rowid hazard: saves delete and
re-insert rows, and SQLite reuses rowids, so an in-flight write could
land on an unrelated client's row. The write no longer discards its
error either.

Drop the inert id round-trip. The panel never sent it, and the byId
branch was guarded by the exact kind+value equality that byKindValue
already keys on, so it could not change an outcome. Matching on
kind+value alone is what actually preserves fetch status across saves.

Reject a negative expiryTime instead of storing a row that is silently
invisible in every subscription — elsewhere a negative expiryTime means
"a duration from first use", so an API caller reusing that convention
got no error and no links.

Drop the ~50 lines of .client-form-* / .client-inbounds-field CSS that
no component renders; it is leftover from the WireGuard PR this one was
split from.

i18n: reuse the already-translated pages.inbounds.leaveBlankToNeverExpire
instead of shipping an English duplicate under pages.clients, and
translate namePrefix, lastFetchAt, lastFetchError and neverFetched into
all 12 non-English locales.

Cover the persistence path that had no test: the fetch-status writer over
a real DB against a failing then a succeeding server, a cache hit writing
nothing, and the negative-expiry rejection.

---------

Co-authored-by: MHSanaei <ho3ein.sanaei@gmail.com>
This commit is contained in:
jason zhang
2026-08-18 19:55:04 +08:00
committed by GitHub
parent 708a69acde
commit abd320994a
29 changed files with 728 additions and 84 deletions
+40
View File
@@ -137,6 +137,12 @@ func initModels() error {
if err := normalizeInboundSubSortIndex(); err != nil {
return err
}
if err := normalizeClientExternalLinkEnable(); err != nil {
return err
}
if err := normalizeClientExternalLinkTimestamps(); err != nil {
return err
}
if err := repairOverflowedTrafficCounters(); err != nil {
return err
}
@@ -965,6 +971,40 @@ func normalizeInboundSubSortIndex() error {
return nil
}
// normalizeClientExternalLinkEnable keeps external-link rows written before the
// enable column existed enabled; disabled rows from newer builds stay false.
func normalizeClientExternalLinkEnable() error {
res := db.Exec("UPDATE client_external_links SET enable = ? WHERE enable IS NULL", true)
if res.Error != nil {
log.Printf("Error normalizing client external link enable: %v", res.Error)
return res.Error
}
if res.RowsAffected > 0 {
log.Printf("Normalized enable on %d client external link(s)", res.RowsAffected)
}
return nil
}
// normalizeClientExternalLinkTimestamps zeroes the NULLs an older build could
// leave behind, so the sub-side expiry predicate never drops a legacy row.
func normalizeClientExternalLinkTimestamps() error {
res := db.Exec("UPDATE client_external_links SET expiry_time = 0 WHERE expiry_time IS NULL")
if res.Error != nil {
log.Printf("Error normalizing client external link expiry_time: %v", res.Error)
return res.Error
}
expiryRows := res.RowsAffected
res = db.Exec("UPDATE client_external_links SET last_fetch_at = 0 WHERE last_fetch_at IS NULL")
if res.Error != nil {
log.Printf("Error normalizing client external link last_fetch_at: %v", res.Error)
return res.Error
}
if expiryRows+res.RowsAffected > 0 {
log.Printf("Normalized timestamps on %d client external link(s)", expiryRows+res.RowsAffected)
}
return nil
}
// repairOverflowedTrafficCounters heals traffic counters that historic
// compounding bugs pushed past int64: on SQLite an overflowing INTEGER is
// silently promoted to REAL, after which the column no longer scans into the