2026-06-23 01:47:29 -07:00
|
|
|
package sub
|
|
|
|
|
|
|
|
|
|
import (
|
style: adopt golangci-lint v2 and resolve all findings
Add .golangci.yml (v2): the standard linters plus bodyclose, errorlint, noctx, misspell, rowserrcheck, sqlclosecheck, unconvert, usestdlibvars, with gofumpt + goimports formatters. Enable the std-error-handling exclusion preset for idiomatic Close/Remove/Setenv ignores; scope-exclude SA1019 (parser.ParseDir in tools/openapigen) and ST1005 (intentional capitalized user-facing error copy that tests assert verbatim). No inline nolint directives were introduced.
Resolve all 217 findings behavior-preserving: gofumpt/goimports formatting, explicit blank assignment on intentionally ignored errors, errors.Is/errors.As and %w wrapping, context-aware stdlib calls (CommandContext/QueryContext/NewRequestWithContext/Dialer), staticcheck simplifications, removed redundant conversions, http.StatusOK and http.MethodGet, inlined the go:fix intPtr helper, and deferred sql rows Close. Add a golangci CI job mirroring the existing Go jobs.
2026-06-27 06:42:22 -07:00
|
|
|
"errors"
|
2026-06-23 01:47:29 -07:00
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
2026-07-29 11:58:00 -07:00
|
|
|
"strconv"
|
2026-06-23 01:47:29 -07:00
|
|
|
"strings"
|
2026-07-29 11:58:00 -07:00
|
|
|
"sync"
|
|
|
|
|
"sync/atomic"
|
2026-06-23 01:47:29 -07:00
|
|
|
"testing"
|
2026-07-29 11:58:00 -07:00
|
|
|
"time"
|
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>
2026-08-18 04:55:04 -07:00
|
|
|
|
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database"
|
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
2026-06-23 01:47:29 -07:00
|
|
|
)
|
|
|
|
|
|
2026-07-29 11:58:00 -07:00
|
|
|
func resetSubscriptionCache(t *testing.T) {
|
|
|
|
|
t.Helper()
|
|
|
|
|
subscriptionCache.Lock()
|
|
|
|
|
previousEntries := subscriptionCache.m
|
|
|
|
|
previousInflight := subscriptionCache.inflight
|
|
|
|
|
subscriptionCache.m = make(map[string]subscriptionCacheEntry)
|
|
|
|
|
subscriptionCache.inflight = make(map[string]*subscriptionFetch)
|
|
|
|
|
subscriptionCache.Unlock()
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
|
subscriptionCache.Lock()
|
|
|
|
|
subscriptionCache.m = previousEntries
|
|
|
|
|
subscriptionCache.inflight = previousInflight
|
|
|
|
|
subscriptionCache.Unlock()
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestFetchSubscriptionLinksSharesConcurrentRefresh(t *testing.T) {
|
|
|
|
|
resetSubscriptionCache(t)
|
|
|
|
|
var requests atomic.Int32
|
|
|
|
|
release := make(chan struct{})
|
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
requests.Add(1)
|
|
|
|
|
<-release
|
|
|
|
|
_, _ = w.Write([]byte("vless://uuid@example.com:443"))
|
|
|
|
|
}))
|
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
|
|
const callers = 16
|
|
|
|
|
results := make(chan []string, callers)
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
for range callers {
|
chore: refresh dependencies and modernize Go test idioms
Frontend deps: @hookform/resolvers 5.4.3 -> 5.5.7, Storybook 10.5.4 -> 10.5.5
across the four packages we declare, globals 17.7.0 -> 17.8.0, and jsdom
29.1.1 -> 30.0.1. The jsdom major replaces its CSS and selector stack --
@asamuzakjp/css-color 5 -> 6, @asamuzakjp/dom-selector 7 -> 8, undici 7 -> 8,
nwsapi and generational-cache folded into their parents, whatwg-url 17 nested
underneath. Nothing in the Vitest suites reaches those directly and the whole
frontend gate (typecheck, lint, tests, build, Storybook compile) is green.
Panel frontend version to 0.6.0.
Backend deps: mattn/go-sqlite3 1.14.48 -> 1.14.49 and valyala/fasthttp
1.72.0 -> 1.73.0, plus the golang.org/x/exp and genproto/googleapis/rpc
indirect bumps that came with them.
Go tests: modernize -fix output, covering range-over-int, sync.WaitGroup.Go
in place of manual Add/Done pairs, maps.Copy, and Go 1.26 new(expr) for
pointer-to-value in the forwarded-trust table. The storedAs helper is deleted
instead of being left behind a //go:fix inline directive -- keeping it that way
fails govet on the one call site the rewrite did not reach, and every caller now
takes new(...) directly. Behaviour is unchanged.
DnsTab: the hosts-sync effect tested dns while declaring dnsEnabled in its
dependency array. Both carry the same truth value, so this is exhaustive-deps
hygiene rather than a behaviour change.
2026-07-29 18:14:22 -07:00
|
|
|
wg.Go(func() {
|
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>
2026-08-18 04:55:04 -07:00
|
|
|
results <- fetchSubscriptionLinks(srv.URL).links
|
chore: refresh dependencies and modernize Go test idioms
Frontend deps: @hookform/resolvers 5.4.3 -> 5.5.7, Storybook 10.5.4 -> 10.5.5
across the four packages we declare, globals 17.7.0 -> 17.8.0, and jsdom
29.1.1 -> 30.0.1. The jsdom major replaces its CSS and selector stack --
@asamuzakjp/css-color 5 -> 6, @asamuzakjp/dom-selector 7 -> 8, undici 7 -> 8,
nwsapi and generational-cache folded into their parents, whatwg-url 17 nested
underneath. Nothing in the Vitest suites reaches those directly and the whole
frontend gate (typecheck, lint, tests, build, Storybook compile) is green.
Panel frontend version to 0.6.0.
Backend deps: mattn/go-sqlite3 1.14.48 -> 1.14.49 and valyala/fasthttp
1.72.0 -> 1.73.0, plus the golang.org/x/exp and genproto/googleapis/rpc
indirect bumps that came with them.
Go tests: modernize -fix output, covering range-over-int, sync.WaitGroup.Go
in place of manual Add/Done pairs, maps.Copy, and Go 1.26 new(expr) for
pointer-to-value in the forwarded-trust table. The storedAs helper is deleted
instead of being left behind a //go:fix inline directive -- keeping it that way
fails govet on the one call site the rewrite did not reach, and every caller now
takes new(...) directly. Behaviour is unchanged.
DnsTab: the hosts-sync effect tested dns while declaring dnsEnabled in its
dependency array. Both carry the same truth value, so this is exhaustive-deps
hygiene rather than a behaviour change.
2026-07-29 18:14:22 -07:00
|
|
|
})
|
2026-07-29 11:58:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
close(release)
|
|
|
|
|
wg.Wait()
|
|
|
|
|
close(results)
|
|
|
|
|
|
|
|
|
|
for links := range results {
|
|
|
|
|
if len(links) != 1 || links[0] != "vless://uuid@example.com:443" {
|
|
|
|
|
t.Fatalf("links = %#v", links)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if got := requests.Load(); got != 1 {
|
|
|
|
|
t.Fatalf("requests = %d, want 1", got)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestFetchSubscriptionLinksBoundsCacheSize(t *testing.T) {
|
|
|
|
|
resetSubscriptionCache(t)
|
|
|
|
|
var requests atomic.Int32
|
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
requests.Add(1)
|
|
|
|
|
_, _ = w.Write([]byte("vless://uuid@example.com:443"))
|
|
|
|
|
}))
|
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
|
|
for i := range subscriptionCacheCapacity + 1 {
|
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>
2026-08-18 04:55:04 -07:00
|
|
|
links := fetchSubscriptionLinks(srv.URL + "?id=" + strconv.Itoa(i)).links
|
2026-07-29 11:58:00 -07:00
|
|
|
if len(links) != 1 {
|
|
|
|
|
t.Fatalf("links at %d = %#v", i, links)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
subscriptionCache.Lock()
|
|
|
|
|
entries := len(subscriptionCache.m)
|
|
|
|
|
subscriptionCache.Unlock()
|
|
|
|
|
if entries != subscriptionCacheCapacity {
|
|
|
|
|
t.Fatalf("cache entries = %d, want %d", entries, subscriptionCacheCapacity)
|
|
|
|
|
}
|
|
|
|
|
if got := requests.Load(); got != subscriptionCacheCapacity+1 {
|
|
|
|
|
t.Fatalf("requests = %d, want %d", got, subscriptionCacheCapacity+1)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestFetchSubscriptionLinksSharesStaleResultAfterRefreshFailure(t *testing.T) {
|
|
|
|
|
resetSubscriptionCache(t)
|
|
|
|
|
stale := []string{"vless://stale@example.com:443"}
|
|
|
|
|
release := make(chan struct{})
|
|
|
|
|
var staleRequests atomic.Int32
|
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if r.URL.Path == "/stale" {
|
|
|
|
|
staleRequests.Add(1)
|
|
|
|
|
<-release
|
|
|
|
|
w.WriteHeader(http.StatusBadGateway)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
_, _ = w.Write([]byte("vless://fresh@example.com:443"))
|
|
|
|
|
}))
|
|
|
|
|
defer srv.Close()
|
|
|
|
|
staleURL := srv.URL + "/stale"
|
|
|
|
|
|
|
|
|
|
subscriptionCache.Lock()
|
|
|
|
|
subscriptionCache.m[staleURL] = subscriptionCacheEntry{
|
|
|
|
|
links: stale,
|
|
|
|
|
fetchedAt: time.Now().Add(-subscriptionCacheTTL),
|
|
|
|
|
}
|
|
|
|
|
for i := range subscriptionCacheCapacity - 1 {
|
|
|
|
|
subscriptionCache.m["cached-"+strconv.Itoa(i)] = subscriptionCacheEntry{fetchedAt: time.Now()}
|
|
|
|
|
}
|
|
|
|
|
subscriptionCache.Unlock()
|
|
|
|
|
|
|
|
|
|
const callers = 16
|
|
|
|
|
results := make(chan []string, callers)
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
for range callers {
|
chore: refresh dependencies and modernize Go test idioms
Frontend deps: @hookform/resolvers 5.4.3 -> 5.5.7, Storybook 10.5.4 -> 10.5.5
across the four packages we declare, globals 17.7.0 -> 17.8.0, and jsdom
29.1.1 -> 30.0.1. The jsdom major replaces its CSS and selector stack --
@asamuzakjp/css-color 5 -> 6, @asamuzakjp/dom-selector 7 -> 8, undici 7 -> 8,
nwsapi and generational-cache folded into their parents, whatwg-url 17 nested
underneath. Nothing in the Vitest suites reaches those directly and the whole
frontend gate (typecheck, lint, tests, build, Storybook compile) is green.
Panel frontend version to 0.6.0.
Backend deps: mattn/go-sqlite3 1.14.48 -> 1.14.49 and valyala/fasthttp
1.72.0 -> 1.73.0, plus the golang.org/x/exp and genproto/googleapis/rpc
indirect bumps that came with them.
Go tests: modernize -fix output, covering range-over-int, sync.WaitGroup.Go
in place of manual Add/Done pairs, maps.Copy, and Go 1.26 new(expr) for
pointer-to-value in the forwarded-trust table. The storedAs helper is deleted
instead of being left behind a //go:fix inline directive -- keeping it that way
fails govet on the one call site the rewrite did not reach, and every caller now
takes new(...) directly. Behaviour is unchanged.
DnsTab: the hosts-sync effect tested dns while declaring dnsEnabled in its
dependency array. Both carry the same truth value, so this is exhaustive-deps
hygiene rather than a behaviour change.
2026-07-29 18:14:22 -07:00
|
|
|
wg.Go(func() {
|
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>
2026-08-18 04:55:04 -07:00
|
|
|
results <- fetchSubscriptionLinks(staleURL).links
|
chore: refresh dependencies and modernize Go test idioms
Frontend deps: @hookform/resolvers 5.4.3 -> 5.5.7, Storybook 10.5.4 -> 10.5.5
across the four packages we declare, globals 17.7.0 -> 17.8.0, and jsdom
29.1.1 -> 30.0.1. The jsdom major replaces its CSS and selector stack --
@asamuzakjp/css-color 5 -> 6, @asamuzakjp/dom-selector 7 -> 8, undici 7 -> 8,
nwsapi and generational-cache folded into their parents, whatwg-url 17 nested
underneath. Nothing in the Vitest suites reaches those directly and the whole
frontend gate (typecheck, lint, tests, build, Storybook compile) is green.
Panel frontend version to 0.6.0.
Backend deps: mattn/go-sqlite3 1.14.48 -> 1.14.49 and valyala/fasthttp
1.72.0 -> 1.73.0, plus the golang.org/x/exp and genproto/googleapis/rpc
indirect bumps that came with them.
Go tests: modernize -fix output, covering range-over-int, sync.WaitGroup.Go
in place of manual Add/Done pairs, maps.Copy, and Go 1.26 new(expr) for
pointer-to-value in the forwarded-trust table. The storedAs helper is deleted
instead of being left behind a //go:fix inline directive -- keeping it that way
fails govet on the one call site the rewrite did not reach, and every caller now
takes new(...) directly. Behaviour is unchanged.
DnsTab: the hosts-sync effect tested dns while declaring dnsEnabled in its
dependency array. Both carry the same truth value, so this is exhaustive-deps
hygiene rather than a behaviour change.
2026-07-29 18:14:22 -07:00
|
|
|
})
|
2026-07-29 11:58:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
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>
2026-08-18 04:55:04 -07:00
|
|
|
if links := fetchSubscriptionLinks(srv.URL + "/fresh").links; len(links) != 1 || links[0] != "vless://fresh@example.com:443" {
|
2026-07-29 11:58:00 -07:00
|
|
|
t.Fatalf("fresh links = %#v", links)
|
|
|
|
|
}
|
|
|
|
|
close(release)
|
|
|
|
|
wg.Wait()
|
|
|
|
|
close(results)
|
|
|
|
|
|
|
|
|
|
for links := range results {
|
|
|
|
|
if len(links) != 1 || links[0] != stale[0] {
|
|
|
|
|
t.Fatalf("links = %#v, want %#v", links, stale)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if got := staleRequests.Load(); got != 1 {
|
|
|
|
|
t.Fatalf("requests = %d, want 1", got)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-23 01:47:29 -07:00
|
|
|
func TestDoFetchSubscriptionLinks_RejectsOversizedBody(t *testing.T) {
|
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
_, _ = w.Write([]byte(strings.Repeat("a", subscriptionMaxBytes+1)))
|
|
|
|
|
}))
|
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
|
|
links, err := doFetchSubscriptionLinks(srv.URL)
|
style: adopt golangci-lint v2 and resolve all findings
Add .golangci.yml (v2): the standard linters plus bodyclose, errorlint, noctx, misspell, rowserrcheck, sqlclosecheck, unconvert, usestdlibvars, with gofumpt + goimports formatters. Enable the std-error-handling exclusion preset for idiomatic Close/Remove/Setenv ignores; scope-exclude SA1019 (parser.ParseDir in tools/openapigen) and ST1005 (intentional capitalized user-facing error copy that tests assert verbatim). No inline nolint directives were introduced.
Resolve all 217 findings behavior-preserving: gofumpt/goimports formatting, explicit blank assignment on intentionally ignored errors, errors.Is/errors.As and %w wrapping, context-aware stdlib calls (CommandContext/QueryContext/NewRequestWithContext/Dialer), staticcheck simplifications, removed redundant conversions, http.StatusOK and http.MethodGet, inlined the go:fix intPtr helper, and deferred sql rows Close. Add a golangci CI job mirroring the existing Go jobs.
2026-06-27 06:42:22 -07:00
|
|
|
if !errors.Is(err, errSubscriptionBodyTooLarge) {
|
2026-06-23 01:47:29 -07:00
|
|
|
t.Fatalf("err = %v, want errSubscriptionBodyTooLarge", err)
|
|
|
|
|
}
|
|
|
|
|
if links != nil {
|
|
|
|
|
t.Fatalf("links = %v, want nil", links)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestDoFetchSubscriptionLinks_AcceptsBodyAtLimit(t *testing.T) {
|
|
|
|
|
link := "vless://example"
|
|
|
|
|
body := link + "\n" + strings.Repeat("#", subscriptionMaxBytes-len(link)-1)
|
|
|
|
|
if len(body) != subscriptionMaxBytes {
|
|
|
|
|
t.Fatalf("fixture size = %d, want %d", len(body), subscriptionMaxBytes)
|
|
|
|
|
}
|
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
_, _ = w.Write([]byte(body))
|
|
|
|
|
}))
|
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
|
|
links, err := doFetchSubscriptionLinks(srv.URL)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("unexpected err: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if len(links) != 1 || links[0] != link {
|
|
|
|
|
t.Fatalf("links = %v, want [%q]", links, link)
|
|
|
|
|
}
|
|
|
|
|
}
|
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>
2026-08-18 04:55:04 -07:00
|
|
|
|
|
|
|
|
func TestRecordExternalSubscriptionFetchStampsEveryRowForTheURL(t *testing.T) {
|
|
|
|
|
initMutDB(t)
|
|
|
|
|
resetSubscriptionCache(t)
|
|
|
|
|
db := database.GetDB()
|
|
|
|
|
|
|
|
|
|
var failing atomic.Bool
|
|
|
|
|
failing.Store(true)
|
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if failing.Load() {
|
|
|
|
|
w.WriteHeader(http.StatusBadGateway)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
_, _ = w.Write([]byte("vless://uuid@example.com:443#Node"))
|
|
|
|
|
}))
|
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
|
|
owners := []model.ClientRecord{
|
|
|
|
|
{Email: "one@example.com", SubID: "sub-fetch", UUID: "uuid-1", Enable: true},
|
|
|
|
|
{Email: "two@example.com", SubID: "sub-fetch", UUID: "uuid-2", Enable: true},
|
|
|
|
|
}
|
|
|
|
|
for i := range owners {
|
|
|
|
|
if err := db.Create(&owners[i]).Error; err != nil {
|
|
|
|
|
t.Fatalf("seed client %d: %v", i, err)
|
|
|
|
|
}
|
|
|
|
|
row := model.ClientExternalLink{
|
|
|
|
|
ClientId: owners[i].Id,
|
|
|
|
|
Kind: model.ExternalLinkKindSubscription,
|
|
|
|
|
Value: srv.URL,
|
|
|
|
|
}
|
|
|
|
|
if err := db.Create(&row).Error; err != nil {
|
|
|
|
|
t.Fatalf("seed external link %d: %v", i, err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
svc := NewSubService("")
|
|
|
|
|
entries, err := svc.getClientExternalLinksBySubId("sub-fetch")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("getClientExternalLinksBySubId: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if len(entries) != 2 {
|
|
|
|
|
t.Fatalf("entries = %d, want 2", len(entries))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, e := range entries {
|
|
|
|
|
expandEntry(e)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var rows []model.ClientExternalLink
|
|
|
|
|
if err := db.Where("value = ?", srv.URL).Find(&rows).Error; err != nil {
|
|
|
|
|
t.Fatalf("read rows: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if len(rows) != 2 {
|
|
|
|
|
t.Fatalf("rows = %d, want 2", len(rows))
|
|
|
|
|
}
|
|
|
|
|
for _, row := range rows {
|
|
|
|
|
if row.LastFetchAt <= 0 {
|
|
|
|
|
t.Fatalf("row %d lastFetchAt = %d, want a stamped timestamp", row.Id, row.LastFetchAt)
|
|
|
|
|
}
|
|
|
|
|
if row.LastFetchError != errBadStatus.Error() {
|
|
|
|
|
t.Fatalf("row %d lastFetchError = %q, want %q", row.Id, row.LastFetchError, errBadStatus)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
failing.Store(false)
|
|
|
|
|
resetSubscriptionCache(t)
|
|
|
|
|
for _, e := range entries {
|
|
|
|
|
expandEntry(e)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := db.Where("value = ?", srv.URL).Find(&rows).Error; err != nil {
|
|
|
|
|
t.Fatalf("re-read rows: %v", err)
|
|
|
|
|
}
|
|
|
|
|
for _, row := range rows {
|
|
|
|
|
if row.LastFetchError != "" {
|
|
|
|
|
t.Fatalf("row %d lastFetchError = %q, want cleared after a good fetch", row.Id, row.LastFetchError)
|
|
|
|
|
}
|
|
|
|
|
if row.LastFetchAt <= 0 {
|
|
|
|
|
t.Fatalf("row %d lastFetchAt = %d, want a stamped timestamp", row.Id, row.LastFetchAt)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestExpandEntryCacheHitWritesNothing(t *testing.T) {
|
|
|
|
|
initMutDB(t)
|
|
|
|
|
resetSubscriptionCache(t)
|
|
|
|
|
db := database.GetDB()
|
|
|
|
|
|
|
|
|
|
const subURL = "https://provider.example/cached"
|
|
|
|
|
rec := model.ClientRecord{Email: "cached@example.com", SubID: "sub-cached", UUID: "uuid", Enable: true}
|
|
|
|
|
if err := db.Create(&rec).Error; err != nil {
|
|
|
|
|
t.Fatalf("seed client: %v", err)
|
|
|
|
|
}
|
|
|
|
|
row := model.ClientExternalLink{ClientId: rec.Id, Kind: model.ExternalLinkKindSubscription, Value: subURL}
|
|
|
|
|
if err := db.Create(&row).Error; err != nil {
|
|
|
|
|
t.Fatalf("seed external link: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
subscriptionCache.Lock()
|
|
|
|
|
subscriptionCache.m[subURL] = subscriptionCacheEntry{
|
|
|
|
|
links: []string{"vless://uuid@example.com:443#Node"},
|
|
|
|
|
fetchedAt: time.Now(),
|
|
|
|
|
}
|
|
|
|
|
subscriptionCache.Unlock()
|
|
|
|
|
|
|
|
|
|
if got := expandEntry(externalLinkEntry{Kind: model.ExternalLinkKindSubscription, Value: subURL}); len(got) != 1 {
|
|
|
|
|
t.Fatalf("expandEntry = %#v, want the cached link", got)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var after model.ClientExternalLink
|
|
|
|
|
if err := db.First(&after, row.Id).Error; err != nil {
|
|
|
|
|
t.Fatalf("read row: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if after.LastFetchAt != 0 || after.LastFetchError != "" {
|
|
|
|
|
t.Fatalf("cache hit wrote fetch status: %#v", after)
|
|
|
|
|
}
|
|
|
|
|
}
|