1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-03-13 17:24:44 +08:00

feat: Support modifying the node port (#7576)

This commit is contained in:
ssongliu 2024-12-28 17:58:43 +08:00 committed by GitHub
parent 4d548adbf7
commit 72c86c3525
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 32 additions and 3 deletions

View File

@ -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"`

View File

@ -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
}

View File

@ -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"`

View File

@ -21,6 +21,7 @@ func InitAgentDB() {
migrations.InitImageRepo,
migrations.InitDefaultCA,
migrations.InitPHPExtensions,
migrations.InitNodePort,
})
if err := m.Migrate(); err != nil {
global.LOG.Error(err)

View File

@ -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
},
}

View File

@ -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)
}