From 47e114d43ee25a2ab6e357702ee0415ed451aeff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=97=E5=AD=90?= Date: Wed, 13 Nov 2024 12:01:47 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BF=AE=E6=94=B9=E9=9D=A2=E6=9D=BF?= =?UTF-8?q?=E7=AB=AF=E5=8F=A3=E5=89=8D=E6=A3=80=E6=9F=A5=E5=8D=A0=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/data/setting.go | 9 ++++++++- internal/http/request/setting.go | 2 +- internal/service/cli.go | 9 ++++++++- pkg/types/config.go | 2 +- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/internal/data/setting.go b/internal/data/setting.go index f7145b92..5ba7e424 100644 --- a/internal/data/setting.go +++ b/internal/data/setting.go @@ -18,6 +18,7 @@ import ( "github.com/TheTNB/panel/pkg/cert" "github.com/TheTNB/panel/pkg/firewall" "github.com/TheTNB/panel/pkg/io" + "github.com/TheTNB/panel/pkg/os" "github.com/TheTNB/panel/pkg/shell" "github.com/TheTNB/panel/pkg/tools" "github.com/TheTNB/panel/pkg/types" @@ -127,7 +128,7 @@ func (r *settingRepo) GetPanelSetting(ctx context.Context) (*request.PanelSettin BackupPath: backupPath, Username: user.Username, Email: user.Email, - Port: app.Conf.Int("http.port"), + Port: uint(app.Conf.Int("http.port")), HTTPS: app.Conf.Bool("http.tls"), Cert: crt, Key: key, @@ -202,6 +203,12 @@ func (r *settingRepo) UpdatePanelSetting(ctx context.Context, setting *request.P return false, err } + if setting.Port != config.HTTP.Port { + if os.TCPPortInUse(setting.Port) { + return false, errors.New("端口已被占用") + } + } + config.App.Locale = setting.Locale config.HTTP.Port = setting.Port config.HTTP.Entrance = setting.Entrance diff --git a/internal/http/request/setting.go b/internal/http/request/setting.go index c6825fff..5e667b18 100644 --- a/internal/http/request/setting.go +++ b/internal/http/request/setting.go @@ -10,7 +10,7 @@ type PanelSetting struct { Username string `json:"username" validate:"required"` Password string `json:"password" validate:"password"` Email string `json:"email" validate:"required"` - Port int `json:"port" validate:"required,number,gte=1,lte=65535"` + Port uint `json:"port" validate:"required,number,gte=1,lte=65535"` HTTPS bool `json:"https"` Cert string `json:"cert" validate:"required"` Key string `json:"key" validate:"required"` diff --git a/internal/service/cli.go b/internal/service/cli.go index bbf052bf..809bcf3c 100644 --- a/internal/service/cli.go +++ b/internal/service/cli.go @@ -24,6 +24,7 @@ import ( "github.com/TheTNB/panel/pkg/firewall" "github.com/TheTNB/panel/pkg/io" "github.com/TheTNB/panel/pkg/ntp" + "github.com/TheTNB/panel/pkg/os" "github.com/TheTNB/panel/pkg/systemctl" "github.com/TheTNB/panel/pkg/tools" "github.com/TheTNB/panel/pkg/types" @@ -370,7 +371,7 @@ func (s *CliService) EntranceOff(ctx context.Context, cmd *cli.Command) error { } func (s *CliService) Port(ctx context.Context, cmd *cli.Command) error { - port := cast.ToInt(cmd.Args().First()) + port := cast.ToUint(cmd.Args().First()) if port < 1 || port > 65535 { return fmt.Errorf("端口范围错误") } @@ -384,6 +385,12 @@ func (s *CliService) Port(ctx context.Context, cmd *cli.Command) error { return err } + if port != config.HTTP.Port { + if os.TCPPortInUse(port) { + return errors.New("端口已被占用") + } + } + config.HTTP.Port = port encoded, err := yaml.Marshal(config) diff --git a/pkg/types/config.go b/pkg/types/config.go index 77ecd515..511c827b 100644 --- a/pkg/types/config.go +++ b/pkg/types/config.go @@ -17,7 +17,7 @@ type PanelAppConfig struct { type PanelHTTPConfig struct { Debug bool `yaml:"debug"` - Port int `yaml:"port"` + Port uint `yaml:"port"` Entrance string `yaml:"entrance"` TLS bool `yaml:"tls"` }