HTTP Header:实现请求方法、域名、状态码等限制,实现内容替换功能

This commit is contained in:
刘祥超
2021-12-14 21:26:43 +08:00
parent 12985e1bd9
commit 9171242c37
6 changed files with 280 additions and 103 deletions

View File

@@ -80,6 +80,12 @@ func TestMatchDomain(t *testing.T) {
ok := MatchDomains([]string{"*"}, "example.com")
a.IsTrue(ok)
}
// port
{
ok := MatchDomains([]string{"example.com:8001"}, "example.com:8001")
a.IsTrue(ok)
}
}
func TestIsSpecialDomain(t *testing.T) {

View File

@@ -35,8 +35,15 @@ type CreateHTTPHeaderRequest struct {
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
Value string `protobuf:"bytes,2,opt,name=value,proto3" json:"value,omitempty"`
Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
Value string `protobuf:"bytes,2,opt,name=value,proto3" json:"value,omitempty"`
Status []int32 `protobuf:"varint,3,rep,packed,name=status,proto3" json:"status,omitempty"`
DisableRedirect bool `protobuf:"varint,4,opt,name=disableRedirect,proto3" json:"disableRedirect,omitempty"`
ShouldAppend bool `protobuf:"varint,5,opt,name=shouldAppend,proto3" json:"shouldAppend,omitempty"`
ShouldReplace bool `protobuf:"varint,6,opt,name=shouldReplace,proto3" json:"shouldReplace,omitempty"`
ReplaceValuesJSON []byte `protobuf:"bytes,7,opt,name=replaceValuesJSON,proto3" json:"replaceValuesJSON,omitempty"`
Methods []string `protobuf:"bytes,8,rep,name=methods,proto3" json:"methods,omitempty"`
Domains []string `protobuf:"bytes,9,rep,name=domains,proto3" json:"domains,omitempty"`
}
func (x *CreateHTTPHeaderRequest) Reset() {
@@ -85,6 +92,55 @@ func (x *CreateHTTPHeaderRequest) GetValue() string {
return ""
}
func (x *CreateHTTPHeaderRequest) GetStatus() []int32 {
if x != nil {
return x.Status
}
return nil
}
func (x *CreateHTTPHeaderRequest) GetDisableRedirect() bool {
if x != nil {
return x.DisableRedirect
}
return false
}
func (x *CreateHTTPHeaderRequest) GetShouldAppend() bool {
if x != nil {
return x.ShouldAppend
}
return false
}
func (x *CreateHTTPHeaderRequest) GetShouldReplace() bool {
if x != nil {
return x.ShouldReplace
}
return false
}
func (x *CreateHTTPHeaderRequest) GetReplaceValuesJSON() []byte {
if x != nil {
return x.ReplaceValuesJSON
}
return nil
}
func (x *CreateHTTPHeaderRequest) GetMethods() []string {
if x != nil {
return x.Methods
}
return nil
}
func (x *CreateHTTPHeaderRequest) GetDomains() []string {
if x != nil {
return x.Domains
}
return nil
}
type CreateHTTPHeaderResponse struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
@@ -138,9 +194,16 @@ type UpdateHTTPHeaderRequest struct {
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
HeaderId int64 `protobuf:"varint,1,opt,name=headerId,proto3" json:"headerId,omitempty"`
Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"`
Value string `protobuf:"bytes,3,opt,name=value,proto3" json:"value,omitempty"`
HeaderId int64 `protobuf:"varint,1,opt,name=headerId,proto3" json:"headerId,omitempty"`
Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"`
Value string `protobuf:"bytes,3,opt,name=value,proto3" json:"value,omitempty"`
Status []int32 `protobuf:"varint,4,rep,packed,name=status,proto3" json:"status,omitempty"`
DisableRedirect bool `protobuf:"varint,5,opt,name=disableRedirect,proto3" json:"disableRedirect,omitempty"`
ShouldAppend bool `protobuf:"varint,6,opt,name=shouldAppend,proto3" json:"shouldAppend,omitempty"`
ShouldReplace bool `protobuf:"varint,7,opt,name=shouldReplace,proto3" json:"shouldReplace,omitempty"`
ReplaceValuesJSON []byte `protobuf:"bytes,8,opt,name=replaceValuesJSON,proto3" json:"replaceValuesJSON,omitempty"`
Methods []string `protobuf:"bytes,9,rep,name=methods,proto3" json:"methods,omitempty"`
Domains []string `protobuf:"bytes,10,rep,name=domains,proto3" json:"domains,omitempty"`
}
func (x *UpdateHTTPHeaderRequest) Reset() {
@@ -196,6 +259,55 @@ func (x *UpdateHTTPHeaderRequest) GetValue() string {
return ""
}
func (x *UpdateHTTPHeaderRequest) GetStatus() []int32 {
if x != nil {
return x.Status
}
return nil
}
func (x *UpdateHTTPHeaderRequest) GetDisableRedirect() bool {
if x != nil {
return x.DisableRedirect
}
return false
}
func (x *UpdateHTTPHeaderRequest) GetShouldAppend() bool {
if x != nil {
return x.ShouldAppend
}
return false
}
func (x *UpdateHTTPHeaderRequest) GetShouldReplace() bool {
if x != nil {
return x.ShouldReplace
}
return false
}
func (x *UpdateHTTPHeaderRequest) GetReplaceValuesJSON() []byte {
if x != nil {
return x.ReplaceValuesJSON
}
return nil
}
func (x *UpdateHTTPHeaderRequest) GetMethods() []string {
if x != nil {
return x.Methods
}
return nil
}
func (x *UpdateHTTPHeaderRequest) GetDomains() []string {
if x != nil {
return x.Domains
}
return nil
}
// 查找配置
type FindEnabledHTTPHeaderConfigRequest struct {
state protoimpl.MessageState
@@ -297,48 +409,78 @@ var file_service_http_header_proto_rawDesc = []byte{
0x0a, 0x19, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x5f, 0x68, 0x74, 0x74, 0x70, 0x5f, 0x68,
0x65, 0x61, 0x64, 0x65, 0x72, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x02, 0x70, 0x62, 0x1a,
0x19, 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x73, 0x2f, 0x72, 0x70, 0x63, 0x5f, 0x6d, 0x65, 0x73, 0x73,
0x61, 0x67, 0x65, 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x43, 0x0a, 0x17, 0x43, 0x72,
0x65, 0x61, 0x74, 0x65, 0x48, 0x54, 0x54, 0x50, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x52, 0x65,
0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20,
0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c,
0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x22,
0x36, 0x0a, 0x18, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x48, 0x54, 0x54, 0x50, 0x48, 0x65, 0x61,
0x64, 0x65, 0x72, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x1a, 0x0a, 0x08, 0x68,
0x65, 0x61, 0x64, 0x65, 0x72, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x08, 0x68,
0x65, 0x61, 0x64, 0x65, 0x72, 0x49, 0x64, 0x22, 0x5f, 0x0a, 0x17, 0x55, 0x70, 0x64, 0x61, 0x74,
0x61, 0x67, 0x65, 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0xb1, 0x02, 0x0a, 0x17, 0x43,
0x72, 0x65, 0x61, 0x74, 0x65, 0x48, 0x54, 0x54, 0x50, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x52,
0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01,
0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61,
0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65,
0x12, 0x16, 0x0a, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x18, 0x03, 0x20, 0x03, 0x28, 0x05,
0x52, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x12, 0x28, 0x0a, 0x0f, 0x64, 0x69, 0x73, 0x61,
0x62, 0x6c, 0x65, 0x52, 0x65, 0x64, 0x69, 0x72, 0x65, 0x63, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28,
0x08, 0x52, 0x0f, 0x64, 0x69, 0x73, 0x61, 0x62, 0x6c, 0x65, 0x52, 0x65, 0x64, 0x69, 0x72, 0x65,
0x63, 0x74, 0x12, 0x22, 0x0a, 0x0c, 0x73, 0x68, 0x6f, 0x75, 0x6c, 0x64, 0x41, 0x70, 0x70, 0x65,
0x6e, 0x64, 0x18, 0x05, 0x20, 0x01, 0x28, 0x08, 0x52, 0x0c, 0x73, 0x68, 0x6f, 0x75, 0x6c, 0x64,
0x41, 0x70, 0x70, 0x65, 0x6e, 0x64, 0x12, 0x24, 0x0a, 0x0d, 0x73, 0x68, 0x6f, 0x75, 0x6c, 0x64,
0x52, 0x65, 0x70, 0x6c, 0x61, 0x63, 0x65, 0x18, 0x06, 0x20, 0x01, 0x28, 0x08, 0x52, 0x0d, 0x73,
0x68, 0x6f, 0x75, 0x6c, 0x64, 0x52, 0x65, 0x70, 0x6c, 0x61, 0x63, 0x65, 0x12, 0x2c, 0x0a, 0x11,
0x72, 0x65, 0x70, 0x6c, 0x61, 0x63, 0x65, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x73, 0x4a, 0x53, 0x4f,
0x4e, 0x18, 0x07, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x11, 0x72, 0x65, 0x70, 0x6c, 0x61, 0x63, 0x65,
0x56, 0x61, 0x6c, 0x75, 0x65, 0x73, 0x4a, 0x53, 0x4f, 0x4e, 0x12, 0x18, 0x0a, 0x07, 0x6d, 0x65,
0x74, 0x68, 0x6f, 0x64, 0x73, 0x18, 0x08, 0x20, 0x03, 0x28, 0x09, 0x52, 0x07, 0x6d, 0x65, 0x74,
0x68, 0x6f, 0x64, 0x73, 0x12, 0x18, 0x0a, 0x07, 0x64, 0x6f, 0x6d, 0x61, 0x69, 0x6e, 0x73, 0x18,
0x09, 0x20, 0x03, 0x28, 0x09, 0x52, 0x07, 0x64, 0x6f, 0x6d, 0x61, 0x69, 0x6e, 0x73, 0x22, 0x36,
0x0a, 0x18, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x48, 0x54, 0x54, 0x50, 0x48, 0x65, 0x61, 0x64,
0x65, 0x72, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x1a, 0x0a, 0x08, 0x68, 0x65,
0x61, 0x64, 0x65, 0x72, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x08, 0x68, 0x65,
0x61, 0x64, 0x65, 0x72, 0x49, 0x64, 0x22, 0xcd, 0x02, 0x0a, 0x17, 0x55, 0x70, 0x64, 0x61, 0x74,
0x65, 0x48, 0x54, 0x54, 0x50, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65,
0x73, 0x74, 0x12, 0x1a, 0x0a, 0x08, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x49, 0x64, 0x18, 0x01,
0x20, 0x01, 0x28, 0x03, 0x52, 0x08, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x49, 0x64, 0x12, 0x12,
0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61,
0x6d, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28,
0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x22, 0x40, 0x0a, 0x22, 0x46, 0x69, 0x6e, 0x64,
0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x74, 0x61, 0x74,
0x75, 0x73, 0x18, 0x04, 0x20, 0x03, 0x28, 0x05, 0x52, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73,
0x12, 0x28, 0x0a, 0x0f, 0x64, 0x69, 0x73, 0x61, 0x62, 0x6c, 0x65, 0x52, 0x65, 0x64, 0x69, 0x72,
0x65, 0x63, 0x74, 0x18, 0x05, 0x20, 0x01, 0x28, 0x08, 0x52, 0x0f, 0x64, 0x69, 0x73, 0x61, 0x62,
0x6c, 0x65, 0x52, 0x65, 0x64, 0x69, 0x72, 0x65, 0x63, 0x74, 0x12, 0x22, 0x0a, 0x0c, 0x73, 0x68,
0x6f, 0x75, 0x6c, 0x64, 0x41, 0x70, 0x70, 0x65, 0x6e, 0x64, 0x18, 0x06, 0x20, 0x01, 0x28, 0x08,
0x52, 0x0c, 0x73, 0x68, 0x6f, 0x75, 0x6c, 0x64, 0x41, 0x70, 0x70, 0x65, 0x6e, 0x64, 0x12, 0x24,
0x0a, 0x0d, 0x73, 0x68, 0x6f, 0x75, 0x6c, 0x64, 0x52, 0x65, 0x70, 0x6c, 0x61, 0x63, 0x65, 0x18,
0x07, 0x20, 0x01, 0x28, 0x08, 0x52, 0x0d, 0x73, 0x68, 0x6f, 0x75, 0x6c, 0x64, 0x52, 0x65, 0x70,
0x6c, 0x61, 0x63, 0x65, 0x12, 0x2c, 0x0a, 0x11, 0x72, 0x65, 0x70, 0x6c, 0x61, 0x63, 0x65, 0x56,
0x61, 0x6c, 0x75, 0x65, 0x73, 0x4a, 0x53, 0x4f, 0x4e, 0x18, 0x08, 0x20, 0x01, 0x28, 0x0c, 0x52,
0x11, 0x72, 0x65, 0x70, 0x6c, 0x61, 0x63, 0x65, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x73, 0x4a, 0x53,
0x4f, 0x4e, 0x12, 0x18, 0x0a, 0x07, 0x6d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x73, 0x18, 0x09, 0x20,
0x03, 0x28, 0x09, 0x52, 0x07, 0x6d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x73, 0x12, 0x18, 0x0a, 0x07,
0x64, 0x6f, 0x6d, 0x61, 0x69, 0x6e, 0x73, 0x18, 0x0a, 0x20, 0x03, 0x28, 0x09, 0x52, 0x07, 0x64,
0x6f, 0x6d, 0x61, 0x69, 0x6e, 0x73, 0x22, 0x40, 0x0a, 0x22, 0x46, 0x69, 0x6e, 0x64, 0x45, 0x6e,
0x61, 0x62, 0x6c, 0x65, 0x64, 0x48, 0x54, 0x54, 0x50, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x43,
0x6f, 0x6e, 0x66, 0x69, 0x67, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x1a, 0x0a, 0x08,
0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x08,
0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x49, 0x64, 0x22, 0x45, 0x0a, 0x23, 0x46, 0x69, 0x6e, 0x64,
0x45, 0x6e, 0x61, 0x62, 0x6c, 0x65, 0x64, 0x48, 0x54, 0x54, 0x50, 0x48, 0x65, 0x61, 0x64, 0x65,
0x72, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x1a,
0x0a, 0x08, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03,
0x52, 0x08, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x49, 0x64, 0x22, 0x45, 0x0a, 0x23, 0x46, 0x69,
0x6e, 0x64, 0x45, 0x6e, 0x61, 0x62, 0x6c, 0x65, 0x64, 0x48, 0x54, 0x54, 0x50, 0x48, 0x65, 0x61,
0x64, 0x65, 0x72, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73,
0x65, 0x12, 0x1e, 0x0a, 0x0a, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x4a, 0x53, 0x4f, 0x4e, 0x18,
0x01, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x0a, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x4a, 0x53, 0x4f,
0x4e, 0x32, 0x93, 0x02, 0x0a, 0x11, 0x48, 0x54, 0x54, 0x50, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72,
0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x4d, 0x0a, 0x10, 0x63, 0x72, 0x65, 0x61, 0x74,
0x65, 0x48, 0x54, 0x54, 0x50, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x12, 0x1b, 0x2e, 0x70, 0x62,
0x2e, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x48, 0x54, 0x54, 0x50, 0x48, 0x65, 0x61, 0x64, 0x65,
0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1c, 0x2e, 0x70, 0x62, 0x2e, 0x43, 0x72,
0x65, 0x61, 0x74, 0x65, 0x48, 0x54, 0x54, 0x50, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x52, 0x65,
0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x3f, 0x0a, 0x10, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65,
0x48, 0x54, 0x54, 0x50, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x12, 0x1b, 0x2e, 0x70, 0x62, 0x2e,
0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x48, 0x54, 0x54, 0x50, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72,
0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x0e, 0x2e, 0x70, 0x62, 0x2e, 0x52, 0x50, 0x43,
0x53, 0x75, 0x63, 0x63, 0x65, 0x73, 0x73, 0x12, 0x6e, 0x0a, 0x1b, 0x66, 0x69, 0x6e, 0x64, 0x45,
0x6e, 0x61, 0x62, 0x6c, 0x65, 0x64, 0x48, 0x54, 0x54, 0x50, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72,
0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, 0x26, 0x2e, 0x70, 0x62, 0x2e, 0x46, 0x69, 0x6e, 0x64,
0x45, 0x6e, 0x61, 0x62, 0x6c, 0x65, 0x64, 0x48, 0x54, 0x54, 0x50, 0x48, 0x65, 0x61, 0x64, 0x65,
0x72, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x27,
0x2e, 0x70, 0x62, 0x2e, 0x46, 0x69, 0x6e, 0x64, 0x45, 0x6e, 0x61, 0x62, 0x6c, 0x65, 0x64, 0x48,
0x54, 0x54, 0x50, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x52,
0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x2f, 0x70, 0x62, 0x62,
0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
0x72, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12,
0x1e, 0x0a, 0x0a, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x4a, 0x53, 0x4f, 0x4e, 0x18, 0x01, 0x20,
0x01, 0x28, 0x0c, 0x52, 0x0a, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x4a, 0x53, 0x4f, 0x4e, 0x32,
0x93, 0x02, 0x0a, 0x11, 0x48, 0x54, 0x54, 0x50, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x53, 0x65,
0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x4d, 0x0a, 0x10, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x48,
0x54, 0x54, 0x50, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x12, 0x1b, 0x2e, 0x70, 0x62, 0x2e, 0x43,
0x72, 0x65, 0x61, 0x74, 0x65, 0x48, 0x54, 0x54, 0x50, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x52,
0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1c, 0x2e, 0x70, 0x62, 0x2e, 0x43, 0x72, 0x65, 0x61,
0x74, 0x65, 0x48, 0x54, 0x54, 0x50, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x52, 0x65, 0x73, 0x70,
0x6f, 0x6e, 0x73, 0x65, 0x12, 0x3f, 0x0a, 0x10, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x48, 0x54,
0x54, 0x50, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x12, 0x1b, 0x2e, 0x70, 0x62, 0x2e, 0x55, 0x70,
0x64, 0x61, 0x74, 0x65, 0x48, 0x54, 0x54, 0x50, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x52, 0x65,
0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x0e, 0x2e, 0x70, 0x62, 0x2e, 0x52, 0x50, 0x43, 0x53, 0x75,
0x63, 0x63, 0x65, 0x73, 0x73, 0x12, 0x6e, 0x0a, 0x1b, 0x66, 0x69, 0x6e, 0x64, 0x45, 0x6e, 0x61,
0x62, 0x6c, 0x65, 0x64, 0x48, 0x54, 0x54, 0x50, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x43, 0x6f,
0x6e, 0x66, 0x69, 0x67, 0x12, 0x26, 0x2e, 0x70, 0x62, 0x2e, 0x46, 0x69, 0x6e, 0x64, 0x45, 0x6e,
0x61, 0x62, 0x6c, 0x65, 0x64, 0x48, 0x54, 0x54, 0x50, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x43,
0x6f, 0x6e, 0x66, 0x69, 0x67, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x27, 0x2e, 0x70,
0x62, 0x2e, 0x46, 0x69, 0x6e, 0x64, 0x45, 0x6e, 0x61, 0x62, 0x6c, 0x65, 0x64, 0x48, 0x54, 0x54,
0x50, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x52, 0x65, 0x73,
0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x2f, 0x70, 0x62, 0x62, 0x06, 0x70,
0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (

View File

@@ -20,6 +20,13 @@ service HTTPHeaderService {
message CreateHTTPHeaderRequest {
string name = 1;
string value = 2;
repeated int32 status = 3;
bool disableRedirect = 4;
bool shouldAppend = 5;
bool shouldReplace = 6;
bytes replaceValuesJSON = 7;
repeated string methods = 8;
repeated string domains = 9;
}
message CreateHTTPHeaderResponse {
@@ -31,6 +38,13 @@ message UpdateHTTPHeaderRequest {
int64 headerId = 1;
string name = 2;
string value = 3;
repeated int32 status = 4;
bool disableRedirect = 5;
bool shouldAppend = 6;
bool shouldReplace = 7;
bytes replaceValuesJSON = 8;
repeated string methods = 9;
repeated string domains = 10;
}
// 查找配置

View File

@@ -2,27 +2,81 @@ package shared
import (
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
"regexp"
"strings"
)
// 头部信息定义
// HTTPHeaderReplaceValue 值替换定义
type HTTPHeaderReplaceValue struct {
Pattern string `yaml:"pattern" json:"pattern"`
Replacement string `yaml:"replacement" json:"replacement"`
IsCaseInsensitive bool `yaml:"isCaseInsensitive" json:"isCaseInsensitive"` // TODO
IsRegexp bool `yaml:"isRegexp" json:"isRegexp"` // TODO
patternReg *regexp.Regexp
}
func (this *HTTPHeaderReplaceValue) Init() error {
if this.IsRegexp {
var pattern = this.Pattern
if this.IsCaseInsensitive && !strings.HasPrefix(pattern, "(?i)") {
pattern = "(?i)" + pattern
}
reg, err := regexp.Compile(pattern)
if err != nil {
return err
}
// TODO 支持匹配名(${name})和反向引用${1}。。。
this.patternReg = reg
} else {
if this.IsCaseInsensitive {
var pattern = "(?i)" + regexp.QuoteMeta(this.Pattern)
reg, err := regexp.Compile(pattern)
if err != nil {
return err
}
this.patternReg = reg
}
}
return nil
}
func (this *HTTPHeaderReplaceValue) Replace(value string) string {
if this.patternReg != nil {
return this.patternReg.ReplaceAllString(value, this.Replacement)
} else {
return strings.ReplaceAll(value, this.Pattern, this.Replacement)
}
}
// HTTPHeaderConfig 头部信息定义
type HTTPHeaderConfig struct {
Id int64 `yaml:"id" json:"id"` // ID
IsOn bool `yaml:"isOn" json:"isOn"` // 是否开启
Name string `yaml:"name" json:"name"` // Name
Value string `yaml:"value" json:"value"` // Value
Status *HTTPStatusConfig `yaml:"status" json:"status"` // 支持的状态码 TODO
Id int64 `yaml:"id" json:"id"` // ID
IsOn bool `yaml:"isOn" json:"isOn"` // 是否开启
Name string `yaml:"name" json:"name"` // Name
Value string `yaml:"value" json:"value"` // Value
Status *HTTPStatusConfig `yaml:"status" json:"status"` // 支持的状态码
DisableRedirect bool `yaml:"disableRedirect" json:"disableRedirect"` // 在跳转时不调用
ShouldAppend bool `yaml:"shouldAppend" json:"shouldAppend"` // 是否为附加
ShouldReplace bool `yaml:"shouldReplace" json:"shouldReplace"` // 是否替换值
ReplaceValues []*HTTPHeaderReplaceValue `yaml:"replaceValues" json:"replaceValues"` // 替换值
Methods []string `yaml:"methods" json:"methods"` // 请求方法
Domains []string `yaml:"domains" json:"domains"` // 专属域名
hasVariables bool
}
// 获取新Header对象
// NewHeaderConfig 获取新Header对象
func NewHeaderConfig() *HTTPHeaderConfig {
return &HTTPHeaderConfig{
IsOn: true,
}
}
// 校验
// Init 校验
func (this *HTTPHeaderConfig) Init() error {
this.hasVariables = configutils.HasVariables(this.Value)
@@ -33,23 +87,19 @@ func (this *HTTPHeaderConfig) Init() error {
}
}
if this.ShouldReplace {
for _, v := range this.ReplaceValues {
err := v.Init()
if err != nil {
return err
}
}
}
return nil
}
// 判断是否匹配状态码
func (this *HTTPHeaderConfig) Match(statusCode int) bool {
if !this.IsOn {
return false
}
if this.Status == nil {
return false
}
return this.Status.Match(statusCode)
}
// 是否有变量
// HasVariables 是否有变量
func (this *HTTPHeaderConfig) HasVariables() bool {
return this.hasVariables
}

View File

@@ -9,41 +9,18 @@ type HTTPHeaderPolicy struct {
IsOn bool `yaml:"isOn" json:"isOn"` // 是否启用 TODO
Description string `yaml:"description" json:"description"` // 描述 TODO
AddHeaderRefs []*HTTPHeaderRef `yaml:"addHeaderRefs" json:"addHeaderRefs"`
AddHeaders []*HTTPHeaderConfig `yaml:"addHeaders" json:"addHeaders"`
AddTrailerRefs []*HTTPHeaderRef `yaml:"addTrailerRefs" json:"addTrailerRefs"`
AddTrailers []*HTTPHeaderConfig `yaml:"addTrailers" json:"addTrailers"` // TODO
SetHeaderRefs []*HTTPHeaderRef `yaml:"setHeaderRefs" json:"setHeaderRefs"`
SetHeaders []*HTTPHeaderConfig `yaml:"setHeaders" json:"setHeaders"`
ReplaceHeaderRefs []*HTTPHeaderRef `yaml:"replaceHeaderRefs" json:"replaceHeaderRefs"`
ReplaceHeaders []*HTTPHeaderConfig `yaml:"replaceHeaders" json:"replaceHeaders"` // 替换Header内容 TODO
DeleteHeaders []string `yaml:"deleteHeaders" json:"deleteHeaders"` // 删除的Header
SetHeaderRefs []*HTTPHeaderRef `yaml:"setHeaderRefs" json:"setHeaderRefs"`
SetHeaders []*HTTPHeaderConfig `yaml:"setHeaders" json:"setHeaders"`
DeleteHeaders []string `yaml:"deleteHeaders" json:"deleteHeaders"` // 删除的Header
Expires *HTTPExpireHeaderConfig `yaml:"expires" json:"expires"` // TODO
addHeaderNames []string
setHeaderNames []string
deleteHeaderMap map[string]bool // header => bool
}
// Init 校验
func (this *HTTPHeaderPolicy) Init() error {
this.addHeaderNames = []string{}
for _, h := range this.AddHeaders {
err := h.Init()
if err != nil {
return err
}
this.addHeaderNames = append(this.addHeaderNames, strings.ToUpper(h.Name))
}
for _, h := range this.AddTrailers {
err := h.Init()
if err != nil {
return err
}
}
this.setHeaderNames = []string{}
for _, h := range this.SetHeaders {
err := h.Init()
@@ -53,13 +30,6 @@ func (this *HTTPHeaderPolicy) Init() error {
this.setHeaderNames = append(this.setHeaderNames, strings.ToUpper(h.Name))
}
for _, h := range this.ReplaceHeaders {
err := h.Init()
if err != nil {
return err
}
}
// delete
this.deleteHeaderMap = map[string]bool{}
for _, header := range this.DeleteHeaders {
@@ -71,18 +41,13 @@ func (this *HTTPHeaderPolicy) Init() error {
// IsEmpty 判断是否为空
func (this *HTTPHeaderPolicy) IsEmpty() bool {
return len(this.AddHeaders) == 0 && len(this.AddTrailers) == 0 && len(this.SetHeaders) == 0 && len(this.ReplaceHeaders) == 0 && this.Expires == nil && len(this.DeleteHeaders) == 0
return len(this.SetHeaders) == 0 && this.Expires == nil && len(this.DeleteHeaders) == 0
}
// ContainsHeader 判断Add和Set中是否包含某个Header
func (this *HTTPHeaderPolicy) ContainsHeader(name string) bool {
name = strings.ToUpper(name)
for _, n := range this.addHeaderNames {
if n == name {
return true
}
}
for _, n := range this.setHeaderNames {
if n == name {
return true

View File

@@ -1,6 +1,6 @@
package shared
// 状态
// HTTPStatusConfig 状态
type HTTPStatusConfig struct {
Always bool `yaml:"always" json:"always"`
Codes []int `yaml:"codes" json:"codes"`