feat: 创建网站支持 ipv6 (#861)

pull/863/head
zhengkunwang223 2 years ago committed by GitHub
parent 1f2dfce7d1
commit f4716cb62f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -20,6 +20,7 @@ import (
"path" "path"
"reflect" "reflect"
"regexp" "regexp"
"strconv"
"strings" "strings"
"time" "time"
@ -452,7 +453,11 @@ func (w WebsiteService) DeleteWebsiteDomain(domainId uint) error {
domains = append(domains, webSiteDomain.Domain) domains = append(domains, webSiteDomain.Domain)
} }
if len(ports) > 0 || len(domains) > 0 { if len(ports) > 0 || len(domains) > 0 {
if err := deleteListenAndServerName(website, ports, domains); err != nil { stringBinds := make([]string, len(ports))
for i := 0; i < len(ports); i++ {
stringBinds[i] = strconv.Itoa(ports[i])
}
if err := deleteListenAndServerName(website, stringBinds, domains); err != nil {
return err return err
} }
} }
@ -599,14 +604,29 @@ func (w WebsiteService) OpWebsiteHTTPS(ctx context.Context, req request.WebsiteH
if !req.Enable { if !req.Enable {
website.Protocol = constant.ProtocolHTTP website.Protocol = constant.ProtocolHTTP
website.WebsiteSSLID = 0 website.WebsiteSSLID = 0
if err := deleteListenAndServerName(website, []int{443}, []string{}); err != nil { if err := deleteListenAndServerName(website, []string{"443", "[::]:443"}, []string{}); err != nil {
return response.WebsiteHTTPS{}, err return response.WebsiteHTTPS{}, err
} }
nginxParams := getNginxParamsFromStaticFile(dto.SSL, nil) nginxParams := getNginxParamsFromStaticFile(dto.SSL, nil)
nginxParams = append(nginxParams, dto.NginxParam{ nginxParams = append(nginxParams,
dto.NginxParam{
Name: "if", Name: "if",
Params: []string{"($scheme", "=", "http)"}, Params: []string{"($scheme", "=", "http)"},
}) },
dto.NginxParam{
Name: "ssl_certificate",
},
dto.NginxParam{
Name: "ssl_certificate_key",
},
dto.NginxParam{
Name: "ssl_protocols",
},
dto.NginxParam{
Name: "ssl_ciphers",
},
)
nginxParams = append(nginxParams)
if err := deleteNginxConfig(constant.NginxScopeServer, nginxParams, &website); err != nil { if err := deleteNginxConfig(constant.NginxScopeServer, nginxParams, &website); err != nil {
return response.WebsiteHTTPS{}, err return response.WebsiteHTTPS{}, err
} }

@ -301,7 +301,7 @@ func addListenAndServerName(website model.Website, ports []int, domains []string
return nginxCheckAndReload(nginxConfig.OldContent, nginxConfig.FilePath, nginxFull.Install.ContainerName) return nginxCheckAndReload(nginxConfig.OldContent, nginxConfig.FilePath, nginxFull.Install.ContainerName)
} }
func deleteListenAndServerName(website model.Website, ports []int, domains []string) error { func deleteListenAndServerName(website model.Website, binds []string, domains []string) error {
nginxFull, err := getNginxFull(&website) nginxFull, err := getNginxFull(&website)
if err != nil { if err != nil {
return nil return nil
@ -309,8 +309,8 @@ func deleteListenAndServerName(website model.Website, ports []int, domains []str
nginxConfig := nginxFull.SiteConfig nginxConfig := nginxFull.SiteConfig
config := nginxFull.SiteConfig.Config config := nginxFull.SiteConfig.Config
server := config.FindServers()[0] server := config.FindServers()[0]
for _, port := range ports { for _, bind := range binds {
server.DeleteListen(strconv.Itoa(port)) server.DeleteListen(bind)
} }
for _, domain := range domains { for _, domain := range domains {
server.DeleteServerName(domain) server.DeleteServerName(domain)
@ -372,16 +372,20 @@ func applySSL(website model.Website, websiteSSL model.WebsiteSSL, req request.We
config := nginxFull.SiteConfig.Config config := nginxFull.SiteConfig.Config
server := config.FindServers()[0] server := config.FindServers()[0]
server.UpdateListen("443", false, "ssl") server.UpdateListen("443", false, "ssl")
server.UpdateListen("[::]:443", false, "ssl")
switch req.HttpConfig { switch req.HttpConfig {
case constant.HTTPSOnly: case constant.HTTPSOnly:
server.RemoveListenByBind("80") server.RemoveListenByBind("80")
server.RemoveListenByBind("[::]:80")
server.RemoveDirective("if", []string{"($scheme"}) server.RemoveDirective("if", []string{"($scheme"})
case constant.HTTPToHTTPS: case constant.HTTPToHTTPS:
server.UpdateListen("80", website.DefaultServer) server.UpdateListen("80", website.DefaultServer)
server.UpdateListen("[::]:80", false)
server.AddHTTP2HTTPS() server.AddHTTP2HTTPS()
case constant.HTTPAlso: case constant.HTTPAlso:
server.UpdateListen("80", website.DefaultServer) server.UpdateListen("80", website.DefaultServer)
server.UpdateListen("[::]:80", false)
server.RemoveDirective("if", []string{"($scheme"}) server.RemoveDirective("if", []string{"($scheme"})
} }

@ -324,5 +324,4 @@ func (s *Server) AddHTTP2HTTPS() {
}) })
newDir.Block = block newDir.Block = block
s.UpdateDirectiveBySecondKey("if", "($scheme", newDir) s.UpdateDirectiveBySecondKey("if", "($scheme", newDir)
} }

@ -1,5 +1,7 @@
server { server {
listen 80; listen 80;
listen [::]:80;
server_name ko.wp-1.com; server_name ko.wp-1.com;
index index.php index.html index.htm default.php default.htm default.html; index index.php index.html index.htm default.php default.htm default.html;

Loading…
Cancel
Save