Files
v2rayA/service/pre_update.go
T
Zakk bb1f79bb87 fix(api): stop reporting results as failures
Three endpoints answered FAIL for something the caller asked for and got:

- an up-to-date GFWList came back as "could not update GFWList: latest version
  is …. GFWList is up to date", because the check returned an error;
- deleting the GFWList when it was already gone returned the raw
  "remove …: no such file or directory";
- the update check said "failed to get latest version from Package file: 1".

The up-to-date case is now a sentinel error the controller turns into a
success carrying alreadyUpToDate, deleting what is already gone succeeds, and
the two package-index messages say what was missing. "which can not be nil"
also became a sentence about the request.
2026-09-18 06:38:52 +10:00

128 lines
3.7 KiB
Go

package main
import (
"errors"
"sync"
"time"
"github.com/v2rayA/v2rayA/conf"
"github.com/v2rayA/v2rayA/db/configure"
"github.com/v2rayA/v2rayA/kernel/v2ray/asset/dat"
"github.com/v2rayA/v2rayA/pkg/util/log"
"github.com/v2rayA/v2rayA/server/service"
)
func updateSubscriptions() {
subs := configure.GetSubscriptions()
lenSubs := len(subs)
control := make(chan struct{}, 2) // concurrency limit: update 2 subscriptions at a time
// Disconnect from subscriptions before auto-selecting servers from them
// to limit the number of connected servers and avoid hitting the limit
shouldDisconnect := true
err := service.AutoSelectServersFromSubscriptions(shouldDisconnect)
if err != nil {
log.Error("[AutoSelect] Failed to disconnect servers from subscriptions -- err: %v", err)
}
wg := new(sync.WaitGroup)
for i := 0; i < lenSubs; i++ {
wg.Add(1)
go func(i int) {
control <- struct{}{}
err := service.UpdateSubscription(i, false)
if err != nil {
log.Info("[AutoUpdate] Subscriptions: Failed to update subscription -- ID: %d, err: %v", i, err)
} else {
log.Info("[AutoUpdate] Subscriptions: Complete updating subscription -- ID: %d, Address: %s", i, subs[i].Address)
}
wg.Done()
<-control
}(i)
}
wg.Wait()
shouldDisconnect = false
err2 := service.AutoSelectServersFromSubscriptions(shouldDisconnect)
if err2 != nil {
log.Error("[AutoSelect] Failed to auto-select servers from subscriptions -- err: %v", err2)
}
}
func initUpdatingTicker() {
conf.TickerUpdateGFWList = time.NewTicker(24 * time.Hour * 365 * 100)
conf.TickerUpdateSubscription = time.NewTicker(24 * time.Hour * 365 * 100)
go func() {
for range conf.TickerUpdateGFWList.C {
version, err := dat.CheckAndUpdateGFWList("")
switch {
case errors.Is(err, dat.ErrGFWListUpToDate):
log.Info("[AutoUpdate] GFWList is already at %v", version)
case err != nil:
log.Info("[AutoUpdate] GFWList: %v", err)
}
}
}()
go func() {
for range conf.TickerUpdateSubscription.C {
updateSubscriptions()
}
}()
}
func checkUpdate() {
setting := service.GetSetting()
// initialize ticker
initUpdatingTicker()
// check for PAC file updates
if setting.GFWListAutoUpdateMode == configure.AutoUpdate ||
setting.GFWListAutoUpdateMode == configure.AutoUpdateAtIntervals ||
setting.Transparent == configure.TransparentGfwlist {
if setting.GFWListAutoUpdateMode == configure.AutoUpdateAtIntervals {
conf.TickerUpdateGFWList.Reset(configure.IntervalHours(setting.GFWListAutoUpdateIntervalHour))
}
switch setting.RulePortMode {
case configure.GfwlistMode:
go func() {
/* Update LoyalsoldierSite.dat */
localGFWListVersion, err := dat.CheckAndUpdateGFWList("")
if errors.Is(err, dat.ErrGFWListUpToDate) {
log.Info("PAC file is already at %v", localGFWListVersion)
return
}
if err != nil {
log.Warn("Failed to update PAC file: %v", err.Error())
return
}
log.Info("Complete updating PAC file. Localtime: %v", localGFWListVersion)
}()
case configure.CustomMode:
// obsolete
}
}
// check for subscription updates
if setting.SubscriptionAutoUpdateMode == configure.AutoUpdate ||
setting.SubscriptionAutoUpdateMode == configure.AutoUpdateAtIntervals {
if setting.SubscriptionAutoUpdateMode == configure.AutoUpdateAtIntervals {
conf.TickerUpdateSubscription.Reset(configure.IntervalHours(setting.SubscriptionAutoUpdateIntervalHour))
}
go updateSubscriptions()
}
// check for server updates
go func() {
f := func() {
if foundNew, remote, err := service.CheckUpdate(); err == nil {
conf.FoundNew = foundNew
conf.RemoteVersion = remote
}
}
f()
c := time.Tick(7 * 24 * time.Hour)
for range c {
f()
}
}()
}