99 "net"
1010 "os"
1111 "path"
12+ "regexp"
1213 "strconv"
1314 "strings"
1415 "time"
@@ -21,6 +22,8 @@ import (
2122 "github.com/1Panel-dev/1Panel/core/utils/cmd"
2223 "github.com/1Panel-dev/1Panel/core/utils/common"
2324 "github.com/1Panel-dev/1Panel/core/utils/encrypt"
25+ "github.com/1Panel-dev/1Panel/core/utils/firewall"
26+ "github.com/1Panel-dev/1Panel/core/utils/xpack"
2427 "github.com/gin-gonic/gin"
2528)
2629
@@ -75,15 +78,20 @@ func (u *SettingService) GetSettingInfo() (*dto.SettingInfo, error) {
7578}
7679
7780func (u * SettingService ) Update (key , value string ) error {
81+ oldVal , err := settingRepo .Get (repo .WithByKey (key ))
82+ if err != nil {
83+ return err
84+ }
85+ if oldVal .Value == value {
86+ return nil
87+ }
7888 switch key {
7989 case "AppStoreLastModified" :
8090 exist , _ := settingRepo .Get (repo .WithByKey ("AppStoreLastModified" ))
8191 if exist .ID == 0 {
8292 _ = settingRepo .Create ("AppStoreLastModified" , value )
8393 return nil
8494 }
85- case "MasterAddr" :
86- global .CONF .System .MasterAddr = value
8795 }
8896
8997 if err := settingRepo .Update (key , value ); err != nil {
@@ -105,7 +113,12 @@ func (u *SettingService) Update(key, value string) error {
105113 }
106114 case "UserName" , "Password" :
107115 _ = global .SESSION .Clean ()
108-
116+ case "MasterAddr" :
117+ go func () {
118+ if err := xpack .UpdateMasterAddr (value ); err != nil {
119+ global .LOG .Errorf ("update master addr failed, err: %v" , err )
120+ }
121+ }()
109122 }
110123
111124 return nil
@@ -174,16 +187,40 @@ func (u *SettingService) UpdatePort(port uint) error {
174187 if common .ScanPort (int (port )) {
175188 return buserr .WithDetail (constant .ErrPortInUsed , port , nil )
176189 }
177- // TODO 修改防火墙端口
190+ oldPort , err := settingRepo .Get (repo .WithByKey ("Port" ))
191+ if err != nil {
192+ return err
193+ }
194+ if oldPort .Value == fmt .Sprintf ("%v" , port ) {
195+ return nil
196+ }
197+ if err := firewall .UpdatePort (oldPort .Value , fmt .Sprintf ("%v" , port )); err != nil {
198+ return err
199+ }
178200
179201 if err := settingRepo .Update ("ServerPort" , strconv .Itoa (int (port ))); err != nil {
180202 return err
181203 }
182204 go func () {
183205 time .Sleep (1 * time .Second )
184- _ , err := cmd .Exec ("systemctl restart 1panel.service" )
206+ defer func () {
207+ if _ , err := cmd .Exec ("systemctl restart 1panel.service" ); err != nil {
208+ global .LOG .Errorf ("restart system port failed, err: %v" , err )
209+ }
210+ }()
211+ masterAddr , err := settingRepo .Get (repo .WithByKey ("MasterAddr" ))
185212 if err != nil {
186- global .LOG .Errorf ("restart system port failed, err: %v" , err )
213+ global .LOG .Errorf ("load master addr from db failed, err: %v" , err )
214+ return
215+ }
216+ if len (masterAddr .Value ) != 0 {
217+ oldMasterPort := loadPort (masterAddr .Value )
218+ if len (oldMasterPort ) != 0 {
219+ if err := xpack .UpdateMasterAddr (strings .ReplaceAll (masterAddr .Value , oldMasterPort , fmt .Sprintf ("%v" , port ))); err != nil {
220+ global .LOG .Errorf ("update master addr from db failed, err: %v" , err )
221+ return
222+ }
223+ }
187224 }
188225 }()
189226 return nil
@@ -250,7 +287,31 @@ func (u *SettingService) UpdateSSL(c *gin.Context, req dto.SSLUpdate) error {
250287 if err := settingRepo .Update ("SSL" , req .SSL ); err != nil {
251288 return err
252289 }
253- return u .UpdateSystemSSL ()
290+
291+ if err := u .UpdateSystemSSL (); err != nil {
292+ return err
293+ }
294+
295+ go func () {
296+ oldSSL , _ := settingRepo .Get (repo .WithByKey ("SSL" ))
297+ if oldSSL .Value != req .SSL {
298+ masterAddr , err := settingRepo .Get (repo .WithByKey ("MasterAddr" ))
299+ if err != nil {
300+ global .LOG .Errorf ("load master addr from db failed, err: %v" , err )
301+ return
302+ }
303+ addrItem := masterAddr .Value
304+ if req .SSL == constant .StatusDisable {
305+ addrItem = strings .ReplaceAll (addrItem , "https://" , "http://" )
306+ } else {
307+ addrItem = strings .ReplaceAll (addrItem , "http://" , "https://" )
308+ }
309+ if err := xpack .UpdateMasterAddr (addrItem ); err != nil {
310+ global .LOG .Errorf ("update master addr from db failed, err: %v" , err )
311+ }
312+ }
313+ }()
314+ return nil
254315}
255316
256317func (u * SettingService ) LoadFromCert () (* dto.SSLInfo , error ) {
@@ -452,3 +513,17 @@ func checkCertValid() error {
452513
453514 return nil
454515}
516+
517+ func loadPort (address string ) string {
518+ re := regexp .MustCompile (`(?:(?:\[([0-9a-fA-F:]+)\])|([^:/\s]+))(?::(\d+))?$` )
519+ matches := re .FindStringSubmatch (address )
520+ if len (matches ) <= 0 {
521+ return ""
522+ }
523+ var port string
524+ port = matches [3 ]
525+ if len (port ) != 0 {
526+ return port
527+ }
528+ return ""
529+ }
0 commit comments