feat: 优化网站 IPV6 设置 (#1732)

Refs https://github.com/1Panel-dev/1Panel/issues/1404
This commit is contained in:
zhengkunwang 2023-07-22 22:04:17 +08:00 committed by GitHub
parent c14e192bee
commit 34e8d88a53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 1 deletions

View File

@ -342,6 +342,11 @@ func (w WebsiteService) UpdateWebsite(req request.WebsiteUpdate) error {
if err != nil {
return err
}
if website.IPV6 != req.IPV6 {
if err := changeIPV6(website, req.IPV6); err != nil {
return err
}
}
website.PrimaryDomain = req.PrimaryDomain
website.WebsiteGroupID = req.WebsiteGroupID
website.Remark = req.Remark

View File

@ -576,6 +576,44 @@ func opWebsite(website *model.Website, operate string) error {
return nginxCheckAndReload(nginxInstall.SiteConfig.OldContent, config.FilePath, nginxInstall.Install.ContainerName)
}
func changeIPV6(website model.Website, enable bool) error {
nginxFull, err := getNginxFull(&website)
if err != nil {
return nil
}
config := nginxFull.SiteConfig.Config
server := config.FindServers()[0]
listens := server.Listens
if enable {
for _, listen := range listens {
if strings.HasPrefix(listen.Bind, "[::]:") {
continue
}
exist := false
ipv6Bind := fmt.Sprintf("[::]:%s", listen.Bind)
for _, li := range listens {
if li.Bind == ipv6Bind {
exist = true
break
}
}
if !exist {
server.UpdateListen(ipv6Bind, false, listen.GetParameters()[1:]...)
}
}
} else {
for _, listen := range listens {
if strings.HasPrefix(listen.Bind, "[::]:") {
server.RemoveListenByBind(listen.Bind)
}
}
}
if err := nginx.WriteConfig(config, nginx.IndentedStyle); err != nil {
return err
}
return nginxCheckAndReload(nginxFull.SiteConfig.OldContent, config.FilePath, nginxFull.Install.ContainerName)
}
func checkIsLinkApp(website model.Website) bool {
if website.Type == constant.Deployment {
return true

View File

@ -315,7 +315,7 @@ func (s *Server) UpdateDirectiveBySecondKey(name string, key string, directive D
func (s *Server) RemoveListenByBind(bind string) {
var listens []*ServerListen
for _, listen := range s.Listens {
if listen.Bind != bind || len(listen.Parameters) > 0 {
if listen.Bind != bind {
listens = append(listens, listen)
}
}