diff --git a/config/version b/config/version index f90aee59..f819c546 100644 --- a/config/version +++ b/config/version @@ -1 +1 @@ -v1.8.0.8 +v1.8.0.9 diff --git a/database/shared_state.go b/database/shared_state.go index dac5483f..4bf703e7 100644 --- a/database/shared_state.go +++ b/database/shared_state.go @@ -9,6 +9,8 @@ import ( const SharedAccountsVersionKey = "shared_accounts_version" +const SharedGeoVersionKey = "shared_geo_version" + func txOrDB(tx *gorm.DB) *gorm.DB { if tx != nil { return tx @@ -47,6 +49,40 @@ func BumpSharedAccountsVersion(tx *gorm.DB) error { }).Error } +func seedSharedGeoVersion(tx *gorm.DB) error { + state := &model.SharedState{ + Key: SharedGeoVersionKey, + } + return txOrDB(tx). + Attrs(&model.SharedState{ + Version: 0, + UpdatedAt: time.Now().Unix(), + }). + FirstOrCreate(state).Error +} + +func GetSharedGeoVersion(tx *gorm.DB) (int64, error) { + state := &model.SharedState{ + Key: SharedGeoVersionKey, + } + if err := txOrDB(tx).First(state).Error; err != nil { + return 0, err + } + return state.Version, nil +} + +func BumpSharedGeoVersion(tx *gorm.DB) error { + if err := seedSharedGeoVersion(tx); err != nil { + return err + } + return txOrDB(tx).Model(&model.SharedState{}). + Where(&model.SharedState{Key: SharedGeoVersionKey}). + Updates(map[string]any{ + "version": gorm.Expr("version + 1"), + "updated_at": time.Now().Unix(), + }).Error +} + func UpsertNodeState(tx *gorm.DB, state *model.NodeState) error { state.UpdatedAt = time.Now().Unix() return txOrDB(tx).Save(state).Error diff --git a/docs/Tasktracking/2026-04-27-add-geofile-sync-to-workers.md b/docs/Tasktracking/2026-04-27-add-geofile-sync-to-workers.md new file mode 100644 index 00000000..4d1784ed --- /dev/null +++ b/docs/Tasktracking/2026-04-27-add-geofile-sync-to-workers.md @@ -0,0 +1,31 @@ +# Task Record + +Date: 2026-04-27 +Related Module: database/shared_state, web/controller/server, web/service/node_sync, web/html/index +Change Type: Add + +## Background +The user needed the ability to trigger "Update Geofiles" on all worker nodes from the master node's UI. Previously, Geofile updates only operated locally on the node where the button was clicked. Workers only synced inbound configurations via `SharedAccountsVersion`; there was no mechanism for the master to broadcast arbitrary actions to workers. + +## Changes +- `database/shared_state.go`: Added `SharedGeoVersionKey` constant and `GetSharedGeoVersion`/`BumpSharedGeoVersion`/`seedSharedGeoVersion` functions, following the same pattern as `SharedAccountsVersion`. +- `web/controller/server.go`: Added `POST /syncUpdateGeofile` route and `syncUpdateGeofile` handler that updates local Geofiles, then bumps `SharedGeoVersion` in the shared database if shared mode is enabled. +- `web/service/node_sync.go`: Extended `NodeSyncService` with `lastGeoVersion`, `loadGeoVersion`, `updateGeofiles` fields and a `syncGeoIfNeeded()` method. Workers check for geo version changes on each sync tick and trigger `UpdateGeofile("")` when a new version is detected. +- `web/html/index.html`: Added "Sync Update" button next to "Update all" in the Geofiles panel, plus the `syncUpdateGeofile()` JavaScript method. +- Translation files: Added i18n keys `geofileSyncUpdate`, `geofileSyncUpdateDialog`, `geofileSyncUpdateDialogDesc` in both zh_CN and en_US. + +## Impact +- New API endpoint: `POST /panel/api/server/syncUpdateGeofile` (triggers local + broadcast Geofile update) +- Worker sync loop now includes geo version checking on every tick +- UI: New "同步更新" button in the Geofiles section of the Xray version modal +- Database: New `shared_geo_version` key in the `shared_state` table (on first use) +- No breaking changes to existing API or database schema + +## Verification +- `gofmt -l -w .` passed with no changes +- `go vet ./...` passed with no errors + +## Risks And Follow-Up +- Geofile sync uses the same polling interval as inbound sync (`SyncIntervalSeconds`); there may be a delay before workers pick up the new version +- If the shared database is unreachable when bumping the geo version, workers will not be notified but the local update will have already succeeded (handled via warning log) +- Future: could add a notification to the master's UI showing which workers have/haven't applied the geo update diff --git a/web/controller/server.go b/web/controller/server.go index 352226f4..84415ad1 100644 --- a/web/controller/server.go +++ b/web/controller/server.go @@ -7,6 +7,8 @@ import ( "strconv" "time" + "github.com/mhsanaei/3x-ui/v2/database" + "github.com/mhsanaei/3x-ui/v2/logger" "github.com/mhsanaei/3x-ui/v2/web/global" "github.com/mhsanaei/3x-ui/v2/web/service" "github.com/mhsanaei/3x-ui/v2/web/websocket" @@ -56,6 +58,7 @@ func (a *ServerController) initRouter(g *gin.RouterGroup) { g.POST("/installXray/:version", a.installXray) g.POST("/updateGeofile", a.updateGeofile) g.POST("/updateGeofile/:fileName", a.updateGeofile) + g.POST("/syncUpdateGeofile", a.syncUpdateGeofile) g.POST("/logs/:count", a.getLogs) g.POST("/xraylogs/:count", a.getXrayLogs) g.POST("/importDB", a.importDB) @@ -157,6 +160,23 @@ func (a *ServerController) updateGeofile(c *gin.Context) { jsonMsg(c, I18nWeb(c, "pages.index.geofileUpdatePopover"), err) } +// syncUpdateGeofile updates local Geofiles and notifies worker nodes to do the same. +func (a *ServerController) syncUpdateGeofile(c *gin.Context) { + err := a.serverService.UpdateGeofile("") + if err != nil { + jsonMsg(c, I18nWeb(c, "pages.index.geofileUpdatePopover"), err) + return + } + + if service.IsSharedModeEnabled() { + if bumpErr := database.BumpSharedGeoVersion(database.GetDB()); bumpErr != nil { + logger.Warning("syncUpdateGeofile: local update succeeded but failed to notify workers:", bumpErr) + } + } + + jsonMsg(c, I18nWeb(c, "pages.index.geofileUpdatePopover"), nil) +} + // stopXrayService stops the Xray service. func (a *ServerController) stopXrayService(c *gin.Context) { err := a.serverService.StopXrayService() diff --git a/web/html/index.html b/web/html/index.html index 53c97acb..3eebb40a 100644 --- a/web/html/index.html +++ b/web/html/index.html @@ -335,8 +335,10 @@ -
{{ i18n - "pages.index.geofilesUpdateAll" }}
+
+ {{ i18n "pages.index.geofileSyncUpdate" }} + {{ i18n "pages.index.geofilesUpdateAll" }} +
@@ -1009,6 +1011,21 @@ }, }); }, + syncUpdateGeofile() { + this.$confirm({ + title: '{{ i18n "pages.index.geofileSyncUpdateDialog" }}', + content: '{{ i18n "pages.index.geofileSyncUpdateDialogDesc" }}', + okText: '{{ i18n "confirm"}}', + class: themeSwitcher.currentTheme, + cancelText: '{{ i18n "cancel"}}', + onOk: async () => { + versionModal.hide(); + this.loading(true, '{{ i18n "pages.index.dontRefresh"}}'); + await HttpUtil.post('/panel/api/server/syncUpdateGeofile'); + this.loading(false); + }, + }); + }, async stopXrayService() { this.loading(true); const msg = await HttpUtil.post('/panel/api/server/stopXrayService'); diff --git a/web/service/node_sync.go b/web/service/node_sync.go index 30faaf13..42fe8d21 100644 --- a/web/service/node_sync.go +++ b/web/service/node_sync.go @@ -5,6 +5,7 @@ import ( "errors" "log" "os" + "sync" "time" "github.com/mhsanaei/3x-ui/v2/config" @@ -19,6 +20,11 @@ type NodeSyncService struct { loadVersion func() (int64, error) loadSnapshot func() (*SharedAccountsSnapshot, error) applySnapshot func(*SharedAccountsSnapshot) error + + geoMu sync.Mutex + lastGeoVersion int64 + loadGeoVersion func() (int64, error) + updateGeofiles func() error } func NewNodeSyncService() *NodeSyncService { @@ -36,6 +42,13 @@ func NewNodeSyncService() *NodeSyncService { return &SharedAccountsSnapshot{Inbounds: inbounds}, nil } svc.applySnapshot = svc.xrayService.ApplySharedSnapshot + svc.loadGeoVersion = func() (int64, error) { + return database.GetSharedGeoVersion(database.GetDB()) + } + svc.updateGeofiles = func() error { + s := &ServerService{} + return s.UpdateGeofile("") + } return svc } @@ -149,6 +162,30 @@ func (s *NodeSyncService) SyncOnce() (bool, error) { return true, nil } +func (s *NodeSyncService) syncGeoIfNeeded() { + current, err := s.loadGeoVersion() + if err != nil { + return + } + s.geoMu.Lock() + if current <= s.lastGeoVersion { + s.geoMu.Unlock() + return + } + s.geoMu.Unlock() + + log.Printf("[NodeSync] geo version changed from %d to %d, updating geofiles...", s.lastGeoVersion, current) + + if err := s.updateGeofiles(); err != nil { + log.Printf("[NodeSync] failed to update geofiles: %v", err) + return + } + + s.geoMu.Lock() + s.lastGeoVersion = current + s.geoMu.Unlock() +} + func (s *NodeSyncService) Run(ctx context.Context, interval time.Duration) { _ = s.BootstrapFromCache() _, _ = s.SyncOnce() @@ -162,6 +199,7 @@ func (s *NodeSyncService) Run(ctx context.Context, interval time.Duration) { return case <-ticker.C: _, _ = s.SyncOnce() + s.syncGeoIfNeeded() } } } diff --git a/web/translation/translate.en_US.toml b/web/translation/translate.en_US.toml index 083d085d..953135ec 100644 --- a/web/translation/translate.en_US.toml +++ b/web/translation/translate.en_US.toml @@ -162,6 +162,9 @@ "geofileUpdateDialogDesc" = "This will update the #filename# file." "geofilesUpdateDialogDesc" = "This will update all geofiles." "geofilesUpdateAll" = "Update all" +"geofileSyncUpdate" = "Sync Update" +"geofileSyncUpdateDialog" = "Sync update Geofiles on all nodes?" +"geofileSyncUpdateDialogDesc" = "This will update Geofiles on the master and all worker nodes. Xray will restart on each node to apply the new rule files." "geofileUpdatePopover" = "Geofile updated successfully" "dontRefresh" = "Installation is in progress, please do not refresh this page" "logs" = "Logs" diff --git a/web/translation/translate.zh_CN.toml b/web/translation/translate.zh_CN.toml index aedb7af2..0d05f718 100644 --- a/web/translation/translate.zh_CN.toml +++ b/web/translation/translate.zh_CN.toml @@ -162,6 +162,9 @@ "geofileUpdateDialogDesc" = "这将更新 #filename# 文件。" "geofilesUpdateDialogDesc" = "这将更新所有文件。" "geofilesUpdateAll" = "全部更新" +"geofileSyncUpdate" = "同步更新" +"geofileSyncUpdateDialog" = "是否在所有节点同步更新 Geofiles?" +"geofileSyncUpdateDialogDesc" = "这将更新主节点和所有子节点的 Geofiles,更新完成后各节点将重启 Xray 以应用新的规则文件。" "geofileUpdatePopover" = "地理文件更新成功" "dontRefresh" = "安装中,请勿刷新此页面" "logs" = "日志"