diff --git a/agent/app/model/setting.go b/agent/app/model/setting.go index 462a3bd279c0..721ce42aa02c 100644 --- a/agent/app/model/setting.go +++ b/agent/app/model/setting.go @@ -10,6 +10,7 @@ type Setting struct { type NodeInfo struct { Scope string `json:"scope"` BaseDir string `json:"baseDir"` + NodePort uint `json:"nodePort"` Version string `json:"version"` ServerCrt string `json:"serverCrt"` ServerKey string `json:"serverKey"` diff --git a/agent/app/service/setting.go b/agent/app/service/setting.go index e1c7dd1840fa..1d85afe37a87 100644 --- a/agent/app/service/setting.go +++ b/agent/app/service/setting.go @@ -2,6 +2,7 @@ package service import ( "encoding/json" + "fmt" "time" "github.com/1Panel-dev/1Panel/agent/app/dto" @@ -103,9 +104,14 @@ func (u *SettingService) ReloadConn() error { global.LOG.Errorf("update base dir failed, err: %v", err) return nil } + if err := settingRepo.Update("NodePort", fmt.Sprintf("%v", nodeInfo.NodePort)); err != nil { + global.LOG.Errorf("update node port failed, err: %v", err) + return nil + } global.CONF.System.BaseDir = nodeInfo.BaseDir global.CONF.System.Version = nodeInfo.Version + global.CONF.System.Port = fmt.Sprintf("%v", nodeInfo.NodePort) global.IsMaster = nodeInfo.Scope == "master" return nil } diff --git a/agent/configs/system.go b/agent/configs/system.go index ce5820a01924..be6f6897b084 100644 --- a/agent/configs/system.go +++ b/agent/configs/system.go @@ -3,6 +3,7 @@ package configs type System struct { Mode string `mapstructure:"mode"` + Port string `mapstructure:"version"` Version string `mapstructure:"version"` BaseDir string `mapstructure:"base_dir"` EncryptKey string `mapstructure:"encrypt_key"` diff --git a/agent/init/migration/migrate.go b/agent/init/migration/migrate.go index d60f56dca83e..acca28ec6f3f 100644 --- a/agent/init/migration/migrate.go +++ b/agent/init/migration/migrate.go @@ -21,6 +21,7 @@ func InitAgentDB() { migrations.InitImageRepo, migrations.InitDefaultCA, migrations.InitPHPExtensions, + migrations.InitNodePort, }) if err := m.Migrate(); err != nil { global.LOG.Error(err) diff --git a/agent/init/migration/migrations/init.go b/agent/init/migration/migrations/init.go index 025817e8fc2c..986d894c3990 100644 --- a/agent/init/migration/migrations/init.go +++ b/agent/init/migration/migrations/init.go @@ -1,6 +1,8 @@ package migrations import ( + "fmt" + "github.com/1Panel-dev/1Panel/agent/app/dto/request" "github.com/1Panel-dev/1Panel/agent/app/model" "github.com/1Panel-dev/1Panel/agent/app/service" @@ -92,6 +94,9 @@ var InitSetting = &gormigrate.Migration{ if err := tx.Create(&model.Setting{Key: "NodeScope", Value: nodeInfo.Scope}).Error; err != nil { return err } + if err := tx.Create(&model.Setting{Key: "NodePort", Value: fmt.Sprintf("%v", nodeInfo.NodePort)}).Error; err != nil { + return err + } if err := tx.Create(&model.Setting{Key: "SystemVersion", Value: nodeInfo.Version}).Error; err != nil { return err } @@ -226,3 +231,17 @@ var AddTaskTable = &gormigrate.Migration{ ) }, } + +var InitNodePort = &gormigrate.Migration{ + ID: "20241226-init-node-port", + Migrate: func(tx *gorm.DB) error { + var itemPort model.Setting + _ = tx.Where("key = ?", "NodePort").First(&itemPort).Error + if itemPort.ID == 0 { + if err := tx.Create(&model.Setting{Key: "NodePort", Value: "9999"}).Error; err != nil { + return err + } + } + return nil + }, +} diff --git a/agent/server/server.go b/agent/server/server.go index 3598038484d8..23d19dbc7cd5 100644 --- a/agent/server/server.go +++ b/agent/server/server.go @@ -3,11 +3,12 @@ package server import ( "crypto/tls" "fmt" - "github.com/1Panel-dev/1Panel/agent/constant" "net" "net/http" "os" + "github.com/1Panel-dev/1Panel/agent/constant" + "github.com/1Panel-dev/1Panel/agent/app/repo" "github.com/1Panel-dev/1Panel/agent/cron" "github.com/1Panel-dev/1Panel/agent/global" @@ -64,7 +65,7 @@ func Start() { _ = server.Serve(listener) return } else { - server.Addr = "0.0.0.0:9999" + server.Addr = fmt.Sprintf("0.0.0.0:%s", global.CONF.System.Port) settingRepo := repo.NewISettingRepo() certItem, err := settingRepo.Get(settingRepo.WithByKey("ServerCrt")) if err != nil { @@ -86,7 +87,7 @@ func Start() { ClientAuth: tls.RequireAnyClientCert, } business.Init() - global.LOG.Info("listen at https://0.0.0.0:9999") + global.LOG.Infof("listen at https://0.0.0.0:%s", global.CONF.System.Port) if err := server.ListenAndServeTLS("", ""); err != nil { panic(err) }