2020-09-16 20:29:13 +08:00
|
|
|
package headers
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
2023-06-30 18:08:30 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
|
2020-09-16 20:29:13 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
|
|
|
|
"github.com/iwind/TeaGo/actions"
|
2022-09-23 19:00:51 +08:00
|
|
|
"strings"
|
2020-09-16 20:29:13 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type CreateSetPopupAction struct {
|
|
|
|
|
actionutils.ParentAction
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *CreateSetPopupAction) Init() {
|
|
|
|
|
this.Nav("", "", "")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *CreateSetPopupAction) RunGet(params struct {
|
|
|
|
|
HeaderPolicyId int64
|
2021-12-14 21:26:32 +08:00
|
|
|
Type string
|
2020-09-16 20:29:13 +08:00
|
|
|
}) {
|
|
|
|
|
this.Data["headerPolicyId"] = params.HeaderPolicyId
|
2021-12-14 21:26:32 +08:00
|
|
|
this.Data["type"] = params.Type
|
2020-09-16 20:29:13 +08:00
|
|
|
|
|
|
|
|
this.Show()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *CreateSetPopupAction) RunPost(params struct {
|
2023-07-03 15:21:46 +08:00
|
|
|
Type string
|
|
|
|
|
|
2020-09-16 20:29:13 +08:00
|
|
|
HeaderPolicyId int64
|
|
|
|
|
Name string
|
|
|
|
|
Value string
|
|
|
|
|
|
2021-12-14 21:26:32 +08:00
|
|
|
StatusListJSON []byte
|
|
|
|
|
MethodsJSON []byte
|
|
|
|
|
DomainsJSON []byte
|
|
|
|
|
ShouldAppend bool
|
|
|
|
|
DisableRedirect bool
|
|
|
|
|
ShouldReplace bool
|
|
|
|
|
ReplaceValuesJSON []byte
|
|
|
|
|
|
2020-09-16 20:29:13 +08:00
|
|
|
Must *actions.Must
|
|
|
|
|
}) {
|
2020-11-20 15:32:42 +08:00
|
|
|
// 日志
|
2023-07-03 15:21:46 +08:00
|
|
|
if params.Type == "request" {
|
|
|
|
|
defer this.CreateLogInfo(codes.ServerHTTPHeader_LogCreateSettingRequestHeader, params.HeaderPolicyId, params.Name, params.Value)
|
|
|
|
|
} else {
|
|
|
|
|
defer this.CreateLogInfo(codes.ServerHTTPHeader_LogCreateSettingResponseHeader, params.HeaderPolicyId, params.Name, params.Value)
|
|
|
|
|
}
|
2020-11-20 15:32:42 +08:00
|
|
|
|
2022-09-23 19:00:51 +08:00
|
|
|
params.Name = strings.TrimSuffix(params.Name, ":")
|
|
|
|
|
|
2020-09-16 20:29:13 +08:00
|
|
|
params.Must.
|
|
|
|
|
Field("name", params.Name).
|
|
|
|
|
Require("请输入Header名称")
|
|
|
|
|
|
2022-12-29 17:16:07 +08:00
|
|
|
configResp, err := this.RPC().HTTPHeaderPolicyRPC().FindEnabledHTTPHeaderPolicyConfig(this.AdminContext(), &pb.FindEnabledHTTPHeaderPolicyConfigRequest{HttpHeaderPolicyId: params.HeaderPolicyId})
|
2020-09-16 20:29:13 +08:00
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
policyConfig := &shared.HTTPHeaderPolicy{}
|
2022-12-29 17:16:07 +08:00
|
|
|
err = json.Unmarshal(configResp.HttpHeaderPolicyJSON, policyConfig)
|
2020-09-16 20:29:13 +08:00
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-14 21:26:32 +08:00
|
|
|
// status list
|
|
|
|
|
var statusList = []int32{}
|
|
|
|
|
if len(params.StatusListJSON) > 0 {
|
|
|
|
|
err = json.Unmarshal(params.StatusListJSON, &statusList)
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// methods
|
|
|
|
|
var methods = []string{}
|
|
|
|
|
if len(params.MethodsJSON) > 0 {
|
|
|
|
|
err = json.Unmarshal(params.MethodsJSON, &methods)
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// domains
|
|
|
|
|
var domains = []string{}
|
|
|
|
|
if len(params.DomainsJSON) > 0 {
|
|
|
|
|
err = json.Unmarshal(params.DomainsJSON, &domains)
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// replace values
|
|
|
|
|
var replaceValues = []*shared.HTTPHeaderReplaceValue{}
|
|
|
|
|
if len(params.ReplaceValuesJSON) > 0 {
|
|
|
|
|
err = json.Unmarshal(params.ReplaceValuesJSON, &replaceValues)
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-16 20:29:13 +08:00
|
|
|
// 创建Header
|
|
|
|
|
createHeaderResp, err := this.RPC().HTTPHeaderRPC().CreateHTTPHeader(this.AdminContext(), &pb.CreateHTTPHeaderRequest{
|
2021-12-14 21:26:32 +08:00
|
|
|
Name: params.Name,
|
|
|
|
|
Value: params.Value,
|
|
|
|
|
Status: statusList,
|
|
|
|
|
Methods: methods,
|
|
|
|
|
Domains: domains,
|
|
|
|
|
ShouldAppend: params.ShouldAppend,
|
|
|
|
|
DisableRedirect: params.DisableRedirect,
|
|
|
|
|
ShouldReplace: params.ShouldReplace,
|
|
|
|
|
ReplaceValuesJSON: params.ReplaceValuesJSON,
|
2020-09-16 20:29:13 +08:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
headerId := createHeaderResp.HeaderId
|
|
|
|
|
|
|
|
|
|
// 保存
|
2020-09-26 08:07:18 +08:00
|
|
|
refs := policyConfig.SetHeaderRefs
|
|
|
|
|
refs = append(refs, &shared.HTTPHeaderRef{
|
|
|
|
|
IsOn: true,
|
|
|
|
|
HeaderId: headerId,
|
2020-09-16 20:29:13 +08:00
|
|
|
})
|
2020-09-26 08:07:18 +08:00
|
|
|
refsJSON, err := json.Marshal(refs)
|
2020-09-16 20:29:13 +08:00
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err = this.RPC().HTTPHeaderPolicyRPC().UpdateHTTPHeaderPolicySettingHeaders(this.AdminContext(), &pb.UpdateHTTPHeaderPolicySettingHeadersRequest{
|
2022-12-29 17:16:07 +08:00
|
|
|
HttpHeaderPolicyId: params.HeaderPolicyId,
|
|
|
|
|
HeadersJSON: refsJSON,
|
2020-09-16 20:29:13 +08:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.Success()
|
|
|
|
|
}
|