2020-08-21 21:09:42 +08:00
|
|
|
|
package https
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2023-08-08 14:17:16 +08:00
|
|
|
|
"context"
|
2020-09-16 09:09:10 +08:00
|
|
|
|
"encoding/json"
|
2020-10-01 16:01:04 +08:00
|
|
|
|
"errors"
|
2020-08-21 21:09:42 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
2020-09-16 09:09:10 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/serverutils"
|
2024-05-04 17:33:44 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
2023-06-30 18:08:30 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
|
2020-09-16 09:09:10 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
2020-10-01 16:01:04 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/sslconfigs"
|
2020-09-16 09:09:10 +08:00
|
|
|
|
"github.com/iwind/TeaGo/actions"
|
2023-03-17 11:11:54 +08:00
|
|
|
|
"github.com/iwind/TeaGo/lists"
|
2020-09-16 09:09:10 +08:00
|
|
|
|
"github.com/iwind/TeaGo/maps"
|
2020-10-01 16:01:04 +08:00
|
|
|
|
"github.com/iwind/TeaGo/types"
|
2021-05-23 20:03:14 +08:00
|
|
|
|
"regexp"
|
2020-08-21 21:09:42 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type IndexAction struct {
|
|
|
|
|
|
actionutils.ParentAction
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (this *IndexAction) Init() {
|
|
|
|
|
|
this.Nav("", "setting", "index")
|
|
|
|
|
|
this.SecondMenu("https")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (this *IndexAction) RunGet(params struct {
|
|
|
|
|
|
ServerId int64
|
|
|
|
|
|
}) {
|
2024-04-14 16:45:17 +08:00
|
|
|
|
// 只有HTTP服务才支持
|
|
|
|
|
|
if this.FilterHTTPFamily() {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-21 19:51:50 +08:00
|
|
|
|
server, _, isOk := serverutils.FindServer(this.Parent(), params.ServerId)
|
2020-09-16 09:09:10 +08:00
|
|
|
|
if !isOk {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2022-12-31 17:12:49 +08:00
|
|
|
|
var httpsConfig = &serverconfigs.HTTPSProtocolConfig{}
|
2020-09-16 09:09:10 +08:00
|
|
|
|
if len(server.HttpsJSON) > 0 {
|
|
|
|
|
|
err := json.Unmarshal(server.HttpsJSON, httpsConfig)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
httpsConfig.IsOn = true
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-08 14:17:16 +08:00
|
|
|
|
_ = httpsConfig.Init(context.TODO())
|
2023-03-17 11:11:54 +08:00
|
|
|
|
var httpsPorts = httpsConfig.AllPorts()
|
|
|
|
|
|
|
|
|
|
|
|
// 检查http和https端口冲突
|
|
|
|
|
|
var conflictingPorts = []int{}
|
|
|
|
|
|
if len(server.HttpJSON) > 0 {
|
|
|
|
|
|
var httpConfig = &serverconfigs.HTTPProtocolConfig{}
|
|
|
|
|
|
err := json.Unmarshal(server.HttpJSON, httpConfig)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
_ = httpConfig.Init()
|
|
|
|
|
|
for _, port := range httpConfig.AllPorts() {
|
|
|
|
|
|
if lists.ContainsInt(httpsPorts, port) {
|
|
|
|
|
|
conflictingPorts = append(conflictingPorts, port)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
this.Data["conflictingPorts"] = conflictingPorts
|
|
|
|
|
|
|
2020-10-01 16:01:04 +08:00
|
|
|
|
var sslPolicy *sslconfigs.SSLPolicy
|
2024-05-04 17:33:44 +08:00
|
|
|
|
var allDNSNames []string
|
2020-10-01 16:01:04 +08:00
|
|
|
|
if httpsConfig.SSLPolicyRef != nil && httpsConfig.SSLPolicyRef.SSLPolicyId > 0 {
|
2022-12-31 17:12:49 +08:00
|
|
|
|
sslPolicyConfigResp, err := this.RPC().SSLPolicyRPC().FindEnabledSSLPolicyConfig(this.AdminContext(), &pb.FindEnabledSSLPolicyConfigRequest{
|
|
|
|
|
|
SslPolicyId: httpsConfig.SSLPolicyRef.SSLPolicyId,
|
|
|
|
|
|
IgnoreData: true,
|
|
|
|
|
|
})
|
2020-10-01 16:01:04 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2022-12-31 17:12:49 +08:00
|
|
|
|
var sslPolicyConfigJSON = sslPolicyConfigResp.SslPolicyJSON
|
2020-10-01 16:01:04 +08:00
|
|
|
|
if len(sslPolicyConfigJSON) > 0 {
|
|
|
|
|
|
sslPolicy = &sslconfigs.SSLPolicy{}
|
|
|
|
|
|
err = json.Unmarshal(sslPolicyConfigJSON, sslPolicy)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2024-05-04 17:33:44 +08:00
|
|
|
|
|
|
|
|
|
|
for _, cert := range sslPolicy.Certs {
|
|
|
|
|
|
for _, dnsName := range cert.DNSNames {
|
|
|
|
|
|
if !lists.ContainsString(allDNSNames, dnsName) {
|
|
|
|
|
|
allDNSNames = append(allDNSNames, dnsName)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-10-01 16:01:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-06-01 17:44:39 +08:00
|
|
|
|
// 当前集群是否支持HTTP/3
|
|
|
|
|
|
if server.NodeCluster == nil {
|
|
|
|
|
|
this.ErrorPage(errors.New("no node cluster for the server"))
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2023-06-01 18:07:46 +08:00
|
|
|
|
supportsHTTP3, err := this.checkSupportsHTTP3(server.NodeCluster.Id)
|
2023-06-01 17:44:39 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-16 09:09:10 +08:00
|
|
|
|
this.Data["serverType"] = server.Type
|
|
|
|
|
|
this.Data["httpsConfig"] = maps.Map{
|
2023-06-01 17:44:39 +08:00
|
|
|
|
"isOn": httpsConfig.IsOn,
|
|
|
|
|
|
"addresses": httpsConfig.Listen,
|
|
|
|
|
|
"sslPolicy": sslPolicy,
|
|
|
|
|
|
"supportsHTTP3": supportsHTTP3,
|
2020-09-16 09:09:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-04 17:33:44 +08:00
|
|
|
|
// 检查域名是否都已经上传了证书
|
|
|
|
|
|
serverNamesResp, err := this.RPC().ServerRPC().FindServerNames(this.AdminContext(), &pb.FindServerNamesRequest{
|
|
|
|
|
|
ServerId: server.Id,
|
|
|
|
|
|
})
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
var allServerNames []string
|
|
|
|
|
|
if len(serverNamesResp.ServerNamesJSON) > 0 {
|
|
|
|
|
|
var serverNamesConfigs = []*serverconfigs.ServerNameConfig{}
|
|
|
|
|
|
err = json.Unmarshal(serverNamesResp.ServerNamesJSON, &serverNamesConfigs)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
allServerNames = serverconfigs.PlainServerNames(serverNamesConfigs)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
this.Data["missingCertServerNames"] = []string{}
|
|
|
|
|
|
if len(allServerNames) > 0 {
|
|
|
|
|
|
var missingServerNames []string
|
|
|
|
|
|
for _, serverName := range allServerNames {
|
|
|
|
|
|
if !configutils.MatchDomains(allDNSNames, serverName) {
|
|
|
|
|
|
missingServerNames = append(missingServerNames, serverName)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
this.Data["missingCertServerNames"] = missingServerNames
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-08-21 21:09:42 +08:00
|
|
|
|
this.Show()
|
|
|
|
|
|
}
|
2020-09-16 09:09:10 +08:00
|
|
|
|
|
|
|
|
|
|
func (this *IndexAction) RunPost(params struct {
|
|
|
|
|
|
ServerId int64
|
2020-09-28 16:25:26 +08:00
|
|
|
|
IsOn bool
|
2020-09-16 09:09:10 +08:00
|
|
|
|
Addresses string
|
|
|
|
|
|
|
2020-10-01 16:01:04 +08:00
|
|
|
|
SslPolicyJSON []byte
|
|
|
|
|
|
|
2020-09-16 09:09:10 +08:00
|
|
|
|
Must *actions.Must
|
|
|
|
|
|
}) {
|
2020-11-17 15:41:43 +08:00
|
|
|
|
// 记录日志
|
2023-06-30 18:08:30 +08:00
|
|
|
|
defer this.CreateLogInfo(codes.ServerHTTPS_LogUpdateHTTPSSettings, params.ServerId)
|
2020-11-17 15:41:43 +08:00
|
|
|
|
|
2022-06-08 20:11:38 +08:00
|
|
|
|
var addresses = []*serverconfigs.NetworkAddressConfig{}
|
2020-09-16 09:09:10 +08:00
|
|
|
|
err := json.Unmarshal([]byte(params.Addresses), &addresses)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
this.Fail("端口地址解析失败:" + err.Error())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-06-08 20:11:38 +08:00
|
|
|
|
// 如果启用HTTPS时没有填写端口,则默认为443
|
|
|
|
|
|
if params.IsOn && len(addresses) == 0 {
|
|
|
|
|
|
addresses = []*serverconfigs.NetworkAddressConfig{
|
|
|
|
|
|
{
|
|
|
|
|
|
Protocol: serverconfigs.ProtocolHTTPS,
|
|
|
|
|
|
PortRange: "443",
|
|
|
|
|
|
},
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-23 20:03:14 +08:00
|
|
|
|
// 检查端口地址是否正确
|
|
|
|
|
|
for _, addr := range addresses {
|
|
|
|
|
|
err = addr.Init()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
this.Fail("绑定端口校验失败:" + err.Error())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if regexp.MustCompile(`^\d+$`).MatchString(addr.PortRange) {
|
|
|
|
|
|
port := types.Int(addr.PortRange)
|
|
|
|
|
|
if port > 65535 {
|
|
|
|
|
|
this.Fail("绑定的端口地址不能大于65535")
|
|
|
|
|
|
}
|
|
|
|
|
|
if port == 80 {
|
|
|
|
|
|
this.Fail("端口80通常是HTTP的端口,不能用在HTTPS上")
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-10-01 16:01:04 +08:00
|
|
|
|
|
|
|
|
|
|
// 校验SSL
|
|
|
|
|
|
var sslPolicyId = int64(0)
|
|
|
|
|
|
if params.SslPolicyJSON != nil {
|
|
|
|
|
|
sslPolicy := &sslconfigs.SSLPolicy{}
|
|
|
|
|
|
err = json.Unmarshal(params.SslPolicyJSON, sslPolicy)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
this.ErrorPage(errors.New("解析SSL配置时发生了错误:" + err.Error()))
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sslPolicyId = sslPolicy.Id
|
|
|
|
|
|
|
|
|
|
|
|
certsJSON, err := json.Marshal(sslPolicy.CertRefs)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
hstsJSON, err := json.Marshal(sslPolicy.HSTS)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
clientCACertsJSON, err := json.Marshal(sslPolicy.ClientCARefs)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if sslPolicyId > 0 {
|
|
|
|
|
|
_, err := this.RPC().SSLPolicyRPC().UpdateSSLPolicy(this.AdminContext(), &pb.UpdateSSLPolicyRequest{
|
|
|
|
|
|
SslPolicyId: sslPolicyId,
|
|
|
|
|
|
Http2Enabled: sslPolicy.HTTP2Enabled,
|
2023-06-01 17:44:39 +08:00
|
|
|
|
Http3Enabled: sslPolicy.HTTP3Enabled,
|
2020-10-01 16:01:04 +08:00
|
|
|
|
MinVersion: sslPolicy.MinVersion,
|
2020-12-18 21:18:35 +08:00
|
|
|
|
SslCertsJSON: certsJSON,
|
2020-10-01 16:01:04 +08:00
|
|
|
|
HstsJSON: hstsJSON,
|
2022-03-10 11:55:09 +08:00
|
|
|
|
OcspIsOn: sslPolicy.OCSPIsOn,
|
2020-10-01 16:01:04 +08:00
|
|
|
|
ClientAuthType: types.Int32(sslPolicy.ClientAuthType),
|
|
|
|
|
|
ClientCACertsJSON: clientCACertsJSON,
|
|
|
|
|
|
CipherSuitesIsOn: sslPolicy.CipherSuitesIsOn,
|
|
|
|
|
|
CipherSuites: sslPolicy.CipherSuites,
|
|
|
|
|
|
})
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
resp, err := this.RPC().SSLPolicyRPC().CreateSSLPolicy(this.AdminContext(), &pb.CreateSSLPolicyRequest{
|
|
|
|
|
|
Http2Enabled: sslPolicy.HTTP2Enabled,
|
2023-06-01 17:44:39 +08:00
|
|
|
|
Http3Enabled: sslPolicy.HTTP3Enabled,
|
2020-10-01 16:01:04 +08:00
|
|
|
|
MinVersion: sslPolicy.MinVersion,
|
2020-12-18 21:18:35 +08:00
|
|
|
|
SslCertsJSON: certsJSON,
|
2020-10-01 16:01:04 +08:00
|
|
|
|
HstsJSON: hstsJSON,
|
2022-03-10 11:55:09 +08:00
|
|
|
|
OcspIsOn: sslPolicy.OCSPIsOn,
|
2020-10-01 16:01:04 +08:00
|
|
|
|
ClientAuthType: types.Int32(sslPolicy.ClientAuthType),
|
|
|
|
|
|
ClientCACertsJSON: clientCACertsJSON,
|
|
|
|
|
|
CipherSuitesIsOn: sslPolicy.CipherSuitesIsOn,
|
|
|
|
|
|
CipherSuites: sslPolicy.CipherSuites,
|
|
|
|
|
|
})
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
sslPolicyId = resp.SslPolicyId
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-21 19:51:50 +08:00
|
|
|
|
server, _, isOk := serverutils.FindServer(this.Parent(), params.ServerId)
|
2020-09-16 09:09:10 +08:00
|
|
|
|
if !isOk {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2022-06-08 20:11:38 +08:00
|
|
|
|
var httpsConfig = &serverconfigs.HTTPSProtocolConfig{}
|
2020-09-16 09:09:10 +08:00
|
|
|
|
if len(server.HttpsJSON) > 0 {
|
|
|
|
|
|
err = json.Unmarshal(server.HttpsJSON, httpsConfig)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-27 16:31:52 +08:00
|
|
|
|
httpsConfig.SSLPolicy = nil
|
2020-10-01 16:01:04 +08:00
|
|
|
|
httpsConfig.SSLPolicyRef = &sslconfigs.SSLPolicyRef{
|
|
|
|
|
|
IsOn: true,
|
|
|
|
|
|
SSLPolicyId: sslPolicyId,
|
|
|
|
|
|
}
|
2020-09-28 16:25:26 +08:00
|
|
|
|
httpsConfig.IsOn = params.IsOn
|
2020-09-16 09:09:10 +08:00
|
|
|
|
httpsConfig.Listen = addresses
|
|
|
|
|
|
configData, err := json.Marshal(httpsConfig)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_, err = this.RPC().ServerRPC().UpdateServerHTTPS(this.AdminContext(), &pb.UpdateServerHTTPSRequest{
|
2020-12-23 09:52:31 +08:00
|
|
|
|
ServerId: params.ServerId,
|
|
|
|
|
|
HttpsJSON: configData,
|
2020-09-16 09:09:10 +08:00
|
|
|
|
})
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
this.Success()
|
|
|
|
|
|
}
|