|
@@ -36,6 +36,7 @@ func NewWebForwardingService(
|
|
|
globalLimitRep repository.GlobalLimitRepository,
|
|
|
cdn CdnService,
|
|
|
proxy ProxyService,
|
|
|
+ sslCert SslCertService,
|
|
|
) WebForwardingService {
|
|
|
return &webForwardingService{
|
|
|
Service: service,
|
|
@@ -51,6 +52,7 @@ func NewWebForwardingService(
|
|
|
cdn: cdn,
|
|
|
globalLimitRep: globalLimitRep,
|
|
|
proxy: proxy,
|
|
|
+ sslCert: sslCert,
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -75,6 +77,7 @@ type webForwardingService struct {
|
|
|
cdn CdnService
|
|
|
globalLimitRep repository.GlobalLimitRepository
|
|
|
proxy ProxyService
|
|
|
+ sslCert SslCertService
|
|
|
}
|
|
|
|
|
|
func (s *webForwardingService) require(ctx context.Context, req v1.GlobalRequire) (v1.GlobalRequire, error) {
|
|
@@ -160,22 +163,23 @@ func (s *webForwardingService) GetWebForwarding(ctx context.Context, req v1.GetF
|
|
|
// ruleId 是从 WAF 系统获取的 ID
|
|
|
func (s *webForwardingService) buildWebForwardingModel(req *v1.WebForwardingDataRequest, ruleId int, require RequireResponse) *model.WebForwarding {
|
|
|
return &model.WebForwarding{
|
|
|
- HostId: require.HostId,
|
|
|
- CdnWebId: ruleId,
|
|
|
- Port: req.Port,
|
|
|
- Domain: req.Domain,
|
|
|
- IsHttps: req.IsHttps,
|
|
|
- Comment: req.Comment,
|
|
|
- HttpsCert: req.HttpsCert,
|
|
|
- HttpsKey: req.HttpsKey,
|
|
|
- SslCertId: int(require.SslPolicyId),
|
|
|
- Cc: req.CcConfig.IsOn,
|
|
|
+ HostId: require.HostId,
|
|
|
+ CdnWebId: ruleId,
|
|
|
+ Port: req.Port,
|
|
|
+ Domain: req.Domain,
|
|
|
+ IsHttps: req.IsHttps,
|
|
|
+ Comment: req.Comment,
|
|
|
+ HttpsCert: req.HttpsCert,
|
|
|
+ HttpsKey: req.HttpsKey,
|
|
|
+ SslCertId: int(req.SslCertId),
|
|
|
+ SslPolicyId: int(req.SslPolicyId),
|
|
|
+ Cc: req.CcConfig.IsOn,
|
|
|
ThresholdMethod: req.CcConfig.ThresholdMethod,
|
|
|
Level: req.CcConfig.Level,
|
|
|
Limit5s: req.CcConfig.Limit5s,
|
|
|
Limit60s: req.CcConfig.Limit60s,
|
|
|
Limit300s: req.CcConfig.Limit300s,
|
|
|
- Proxy: req.Proxy,
|
|
|
+ Proxy: req.Proxy,
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -208,11 +212,10 @@ func (s *webForwardingService) prepareWafData(ctx context.Context, req *v1.WebFo
|
|
|
}
|
|
|
|
|
|
// 2. 调用辅助函数,构建核心的代理配置 (将复杂逻辑封装起来)
|
|
|
- byteData, sslPolicyId, err := s.buildProxyJSONConfig(ctx, req, require)
|
|
|
+ byteData, err := s.buildProxyConfig(ctx, req, require)
|
|
|
if err != nil {
|
|
|
return RequireResponse{}, v1.Website{}, err // 错误信息在辅助函数中已经包装好了
|
|
|
}
|
|
|
- require.SslPolicyId = sslPolicyId
|
|
|
type serverNames struct {
|
|
|
ServerNames string `json:"name" form:"name"`
|
|
|
Type string `json:"type" form:"type"`
|
|
@@ -242,15 +245,14 @@ func (s *webForwardingService) prepareWafData(ctx context.Context, req *v1.WebFo
|
|
|
NodeClusterId: defaultNodeClusterId,
|
|
|
}
|
|
|
|
|
|
- var noSslByteData, _ = json.Marshal(v1.TypeJSON{IsOn: false})
|
|
|
|
|
|
// 4. 根据协议类型,填充 HttpJSON 和 HttpsJSON 字段
|
|
|
if req.WebForwardingData.IsHttps == isHttps {
|
|
|
- formData.HttpJSON = noSslByteData
|
|
|
+ formData.HttpJSON = v1.TypeJSON{IsOn: false}
|
|
|
formData.HttpsJSON = byteData
|
|
|
} else {
|
|
|
formData.HttpJSON = byteData
|
|
|
- formData.HttpsJSON = noSslByteData
|
|
|
+ formData.HttpsJSON = v1.TypeJSON{IsOn: false}
|
|
|
}
|
|
|
|
|
|
return require, formData, nil
|
|
@@ -260,46 +262,25 @@ func (s *webForwardingService) prepareWafData(ctx context.Context, req *v1.WebFo
|
|
|
// 辅助函数:buildProxyJSONConfig
|
|
|
// 职责:专门负责处理 HTTP/HTTPS 的差异,并生成对应的 JSON 配置。
|
|
|
// =================================================================
|
|
|
-func (s *webForwardingService) buildProxyJSONConfig(ctx context.Context, req *v1.WebForwardingRequest, require RequireResponse) ([]byte, int64, error) {
|
|
|
+func (s *webForwardingService) buildProxyConfig(ctx context.Context, req *v1.WebForwardingRequest, require RequireResponse) (v1.TypeJSON, error) {
|
|
|
var (
|
|
|
jsonData v1.TypeJSON
|
|
|
apiType string
|
|
|
- err error
|
|
|
+
|
|
|
)
|
|
|
|
|
|
- var sslPolicyId int64
|
|
|
+
|
|
|
jsonData.IsOn = true
|
|
|
apiType = protocolHttps
|
|
|
// 判断协议类型,并处理 HTTPS 的特殊逻辑(证书)
|
|
|
if req.WebForwardingData.IsHttps == isHttps {
|
|
|
// 处理证书信息
|
|
|
- if req.WebForwardingData.SslCertId == 0 {
|
|
|
- sslPolicyId, _, err = s.wafformatter.AddSSLPolicy(ctx, v1.SSL{
|
|
|
- CdnUserId: require.CdnUid,
|
|
|
- Domain: req.WebForwardingData.Domain,
|
|
|
- Name: req.WebForwardingData.Domain,
|
|
|
- Description: req.WebForwardingData.Comment,
|
|
|
- CertData: req.WebForwardingData.HttpsCert,
|
|
|
- KeyData: req.WebForwardingData.HttpsKey,
|
|
|
- })
|
|
|
+ if req.WebForwardingData.SslPolicyId == 0 {
|
|
|
+ sslPolicyId, err := s.sslCert.AddSslPolicy(ctx, nil)
|
|
|
if err != nil {
|
|
|
- return nil, 0, fmt.Errorf("处理证书失败: %w", err)
|
|
|
+ return v1.TypeJSON{}, err
|
|
|
}
|
|
|
jsonData.SslPolicyRef.SslPolicyId = sslPolicyId
|
|
|
- } else {
|
|
|
- err = s.wafformatter.EditSSL(ctx, v1.SSL{
|
|
|
- WebId: int64(req.WebForwardingData.Id),
|
|
|
- SSLPolicyId: int(req.WebForwardingData.SslCertId),
|
|
|
- CdnUserId: require.CdnUid,
|
|
|
- Name: req.WebForwardingData.Domain,
|
|
|
- Description: req.WebForwardingData.Comment,
|
|
|
- CertData: req.WebForwardingData.HttpsCert,
|
|
|
- KeyData: req.WebForwardingData.HttpsKey,
|
|
|
- })
|
|
|
- if err != nil {
|
|
|
- return nil, 0, fmt.Errorf("处理证书失败: %w", err)
|
|
|
- }
|
|
|
- jsonData.SslPolicyRef.SslPolicyId = req.WebForwardingData.SslCertId
|
|
|
}
|
|
|
jsonData.SslPolicyRef.IsOn = true
|
|
|
} else {
|
|
@@ -319,13 +300,8 @@ func (s *webForwardingService) buildProxyJSONConfig(ctx context.Context, req *v1
|
|
|
})
|
|
|
}
|
|
|
|
|
|
- // 序列化为 JSON
|
|
|
- byteData, err := json.Marshal(jsonData)
|
|
|
- if err != nil {
|
|
|
- return nil, 0, fmt.Errorf("序列化WAF配置失败: %w", err)
|
|
|
- }
|
|
|
|
|
|
- return byteData, sslPolicyId, nil
|
|
|
+ return jsonData, nil
|
|
|
}
|
|
|
|
|
|
// 查找两个列表的差异
|
|
@@ -373,6 +349,29 @@ func (s *webForwardingService) AddWebForwarding(ctx context.Context, req *v1.Web
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
+ // 添加证书
|
|
|
+ if req.WebForwardingData.IsHttps == isHttps {
|
|
|
+ sslCertId, err := s.sslCert.AddSSLCert(ctx, v1.SSL{
|
|
|
+ Name: req.WebForwardingData.Domain,
|
|
|
+ Domain: req.WebForwardingData.Domain,
|
|
|
+ CertData: req.WebForwardingData.HttpsCert,
|
|
|
+ KeyData: req.WebForwardingData.HttpsKey,
|
|
|
+ CdnUserId: require.CdnUid,
|
|
|
+ Description: req.WebForwardingData.Comment,
|
|
|
+ })
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ req.WebForwardingData.SslCertId = sslCertId
|
|
|
+ req.WebForwardingData.SslPolicyId = formData.HttpsJSON.SslPolicyRef.SslPolicyId
|
|
|
+ err = s.sslCert.EditSslPolicy(ctx, formData.HttpsJSON.SslPolicyRef.SslPolicyId, []int64{sslCertId}, "add")
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // 添加网站
|
|
|
webId, err := s.cdn.CreateWebsite(ctx, formData)
|
|
|
if err != nil {
|
|
|
return err
|
|
@@ -520,21 +519,51 @@ func (s *webForwardingService) EditWebForwarding(ctx context.Context, req *v1.We
|
|
|
|
|
|
//修改网站端口
|
|
|
if oldData.Port != req.WebForwardingData.Port || oldData.IsHttps != req.WebForwardingData.IsHttps || oldData.HttpsCert != req.WebForwardingData.HttpsCert || oldData.HttpsKey != req.WebForwardingData.HttpsKey {
|
|
|
- var typeJson []byte
|
|
|
- var closeJson []byte
|
|
|
var apiType string
|
|
|
var closeType string
|
|
|
+
|
|
|
+ // 修改证书
|
|
|
+ if oldData.HttpsCert != req.WebForwardingData.HttpsCert || oldData.HttpsKey != req.WebForwardingData.HttpsKey {
|
|
|
+ err = s.sslCert.EditSSLCert(ctx, v1.SSL{
|
|
|
+ Name: req.WebForwardingData.Domain,
|
|
|
+ CertId: oldData.SslCertId,
|
|
|
+ CertData: req.WebForwardingData.HttpsCert,
|
|
|
+ KeyData: req.WebForwardingData.HttpsKey,
|
|
|
+ CdnUserId: require.CdnUid,
|
|
|
+ Domain: req.WebForwardingData.Domain,
|
|
|
+ Description: req.WebForwardingData.Comment,
|
|
|
+ })
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 切换协议
|
|
|
+ var typeConfig v1.TypeJSON
|
|
|
+ var closeConfig v1.TypeJSON
|
|
|
if req.WebForwardingData.IsHttps == isHttps {
|
|
|
- typeJson = formData.HttpsJSON
|
|
|
- closeJson = formData.HttpJSON
|
|
|
+ typeConfig = formData.HttpsJSON
|
|
|
+ closeConfig = formData.HttpJSON
|
|
|
apiType = protocolHttps
|
|
|
closeType = protocolHttp
|
|
|
} else {
|
|
|
- typeJson = formData.HttpJSON
|
|
|
- closeJson = formData.HttpsJSON
|
|
|
+ typeConfig = formData.HttpJSON
|
|
|
+ closeConfig = formData.HttpsJSON
|
|
|
apiType = protocolHttp
|
|
|
closeType = protocolHttps
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ typeJson,err := json.Marshal(typeConfig)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ closeJson,err := json.Marshal(closeConfig)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ // 切换协议
|
|
|
err = s.cdn.EditServerType(ctx, v1.EditWebsite{
|
|
|
Id: int64(oldData.CdnWebId),
|
|
|
TypeJSON: typeJson,
|
|
@@ -796,25 +825,17 @@ func (s *webForwardingService) DeleteWebForwarding(ctx context.Context, Ids []in
|
|
|
}
|
|
|
|
|
|
// 删除ssl
|
|
|
- data, err := s.webForwardingRepository.GetWebForwarding(ctx, int64(Id))
|
|
|
+ err = s.cdn.DelSSLCert(ctx, int64(oldData.SslCertId))
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
- if data.SslCertId != 0 {
|
|
|
- sslPolicyData, err := s.webForwardingRepository.GetSslCertId(ctx, data.SslCertId)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- if sslPolicyData != nil {
|
|
|
- for _, v := range sslPolicyData {
|
|
|
- err := s.cdn.DelSSLCert(ctx, v.CertId)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ err = s.sslCert.EditSslPolicy(ctx, int64(oldData.SslPolicyId), []int64{int64(oldData.SslCertId)}, "del")
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
if err = s.webForwardingRepository.DeleteWebForwarding(ctx, int64(Id)); err != nil {
|
|
|
return err
|
|
|
}
|
|
@@ -1048,4 +1069,4 @@ func (s *webForwardingService) EditCcConfig(ctx context.Context,webId int64, req
|
|
|
return err
|
|
|
}
|
|
|
return nil
|
|
|
-}
|
|
|
+}
|