mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-12-10 04:00:24 +08:00
HTTP Header:实现请求方法、域名、状态码等限制,实现内容替换功能
This commit is contained in:
@@ -2,7 +2,7 @@ package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
@@ -80,22 +80,69 @@ func (this *HTTPHeaderDAO) FindHTTPHeaderName(tx *dbs.Tx, id int64) (string, err
|
||||
}
|
||||
|
||||
// CreateHeader 创建Header
|
||||
func (this *HTTPHeaderDAO) CreateHeader(tx *dbs.Tx, name string, value string) (int64, error) {
|
||||
func (this *HTTPHeaderDAO) CreateHeader(tx *dbs.Tx, userId int64, name string, value string, status []int, disableRedirect bool, shouldAppend bool, shouldReplace bool, replaceValues []*shared.HTTPHeaderReplaceValue, methods []string, domains []string) (int64, error) {
|
||||
op := NewHTTPHeaderOperator()
|
||||
op.UserId = userId
|
||||
op.State = HTTPHeaderStateEnabled
|
||||
op.IsOn = true
|
||||
op.Name = name
|
||||
op.Value = value
|
||||
|
||||
statusConfig := &shared.HTTPStatusConfig{
|
||||
Always: true,
|
||||
// status
|
||||
var statusConfig *shared.HTTPStatusConfig
|
||||
if len(status) == 0 {
|
||||
statusConfig = &shared.HTTPStatusConfig{
|
||||
Always: true,
|
||||
}
|
||||
} else {
|
||||
statusConfig = &shared.HTTPStatusConfig{
|
||||
Always: false,
|
||||
Codes: status,
|
||||
}
|
||||
}
|
||||
|
||||
statusJSON, err := json.Marshal(statusConfig)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
op.Status = statusJSON
|
||||
|
||||
op.DisableRedirect = disableRedirect
|
||||
op.ShouldAppend = shouldAppend
|
||||
op.ShouldReplace = shouldReplace
|
||||
|
||||
if len(replaceValues) == 0 {
|
||||
op.ReplaceValues = "[]"
|
||||
} else {
|
||||
replaceValuesJSON, err := json.Marshal(replaceValues)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
op.ReplaceValues = replaceValuesJSON
|
||||
}
|
||||
|
||||
// methods
|
||||
if len(methods) == 0 {
|
||||
op.Methods = "[]"
|
||||
} else {
|
||||
methodsJSON, err := json.Marshal(methods)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
op.Methods = methodsJSON
|
||||
}
|
||||
|
||||
// domains
|
||||
if len(domains) == 0 {
|
||||
op.Domains = "[]"
|
||||
} else {
|
||||
domainsJSON, err := json.Marshal(domains)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
op.Domains = domainsJSON
|
||||
}
|
||||
|
||||
err = this.Save(tx, op)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
@@ -104,7 +151,7 @@ func (this *HTTPHeaderDAO) CreateHeader(tx *dbs.Tx, name string, value string) (
|
||||
}
|
||||
|
||||
// UpdateHeader 修改Header
|
||||
func (this *HTTPHeaderDAO) UpdateHeader(tx *dbs.Tx, headerId int64, name string, value string) error {
|
||||
func (this *HTTPHeaderDAO) UpdateHeader(tx *dbs.Tx, headerId int64, name string, value string, status []int, disableRedirect bool, shouldAppend bool, shouldReplace bool, replaceValues []*shared.HTTPHeaderReplaceValue, methods []string, domains []string) error {
|
||||
if headerId <= 0 {
|
||||
return errors.New("invalid headerId")
|
||||
}
|
||||
@@ -113,7 +160,63 @@ func (this *HTTPHeaderDAO) UpdateHeader(tx *dbs.Tx, headerId int64, name string,
|
||||
op.Id = headerId
|
||||
op.Name = name
|
||||
op.Value = value
|
||||
err := this.Save(tx, op)
|
||||
|
||||
// status
|
||||
var statusConfig *shared.HTTPStatusConfig
|
||||
if len(status) == 0 {
|
||||
statusConfig = &shared.HTTPStatusConfig{
|
||||
Always: true,
|
||||
}
|
||||
} else {
|
||||
statusConfig = &shared.HTTPStatusConfig{
|
||||
Always: false,
|
||||
Codes: status,
|
||||
}
|
||||
}
|
||||
|
||||
statusJSON, err := json.Marshal(statusConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
op.Status = statusJSON
|
||||
|
||||
op.DisableRedirect = disableRedirect
|
||||
op.ShouldAppend = shouldAppend
|
||||
op.ShouldReplace = shouldReplace
|
||||
|
||||
if len(replaceValues) == 0 {
|
||||
op.ReplaceValues = "[]"
|
||||
} else {
|
||||
replaceValuesJSON, err := json.Marshal(replaceValues)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
op.ReplaceValues = replaceValuesJSON
|
||||
}
|
||||
|
||||
// methods
|
||||
if len(methods) == 0 {
|
||||
op.Methods = "[]"
|
||||
} else {
|
||||
methodsJSON, err := json.Marshal(methods)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
op.Methods = methodsJSON
|
||||
}
|
||||
|
||||
// domains
|
||||
if len(domains) == 0 {
|
||||
op.Domains = "[]"
|
||||
} else {
|
||||
domainsJSON, err := json.Marshal(domains)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
op.Domains = domainsJSON
|
||||
}
|
||||
|
||||
err = this.Save(tx, op)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -136,7 +239,21 @@ func (this *HTTPHeaderDAO) ComposeHeaderConfig(tx *dbs.Tx, headerId int64) (*sha
|
||||
config.IsOn = header.IsOn == 1
|
||||
config.Name = header.Name
|
||||
config.Value = header.Value
|
||||
config.DisableRedirect = header.DisableRedirect == 1
|
||||
config.ShouldAppend = header.ShouldAppend == 1
|
||||
|
||||
// replace
|
||||
config.ShouldReplace = header.ShouldReplace == 1
|
||||
if len(header.ReplaceValues) > 0 {
|
||||
var values = []*shared.HTTPHeaderReplaceValue{}
|
||||
err = json.Unmarshal([]byte(header.ReplaceValues), &values)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config.ReplaceValues = values
|
||||
}
|
||||
|
||||
// status
|
||||
if len(header.Status) > 0 {
|
||||
status := &shared.HTTPStatusConfig{}
|
||||
err = json.Unmarshal([]byte(header.Status), status)
|
||||
@@ -146,6 +263,26 @@ func (this *HTTPHeaderDAO) ComposeHeaderConfig(tx *dbs.Tx, headerId int64) (*sha
|
||||
config.Status = status
|
||||
}
|
||||
|
||||
// methods
|
||||
if len(header.Methods) > 0 {
|
||||
var methods = []string{}
|
||||
err = json.Unmarshal([]byte(header.Methods), &methods)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config.Methods = methods
|
||||
}
|
||||
|
||||
// domains
|
||||
if len(header.Domains) > 0 {
|
||||
var domains = []string{}
|
||||
err = json.Unmarshal([]byte(header.Domains), &domains)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config.Domains = domains
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user