diff --git a/pkg/rpc/pb/service_http_rewrite_rule.pb.go b/pkg/rpc/pb/service_http_rewrite_rule.pb.go index e6a4588..1db3bfc 100644 --- a/pkg/rpc/pb/service_http_rewrite_rule.pb.go +++ b/pkg/rpc/pb/service_http_rewrite_rule.pb.go @@ -42,6 +42,7 @@ type CreateHTTPRewriteRuleRequest struct { IsBreak bool `protobuf:"varint,5,opt,name=isBreak,proto3" json:"isBreak,omitempty"` ProxyHost string `protobuf:"bytes,6,opt,name=proxyHost,proto3" json:"proxyHost,omitempty"` IsOn bool `protobuf:"varint,7,opt,name=isOn,proto3" json:"isOn,omitempty"` + WithQuery bool `protobuf:"varint,8,opt,name=withQuery,proto3" json:"withQuery,omitempty"` } func (x *CreateHTTPRewriteRuleRequest) Reset() { @@ -125,6 +126,13 @@ func (x *CreateHTTPRewriteRuleRequest) GetIsOn() bool { return false } +func (x *CreateHTTPRewriteRuleRequest) GetWithQuery() bool { + if x != nil { + return x.WithQuery + } + return false +} + type CreateHTTPRewriteRuleResponse struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -186,6 +194,7 @@ type UpdateHTTPRewriteRuleRequest struct { IsBreak bool `protobuf:"varint,6,opt,name=isBreak,proto3" json:"isBreak,omitempty"` ProxyHost string `protobuf:"bytes,7,opt,name=proxyHost,proto3" json:"proxyHost,omitempty"` IsOn bool `protobuf:"varint,8,opt,name=isOn,proto3" json:"isOn,omitempty"` + WithQuery bool `protobuf:"varint,9,opt,name=withQuery,proto3" json:"withQuery,omitempty"` } func (x *UpdateHTTPRewriteRuleRequest) Reset() { @@ -276,13 +285,20 @@ func (x *UpdateHTTPRewriteRuleRequest) GetIsOn() bool { return false } +func (x *UpdateHTTPRewriteRuleRequest) GetWithQuery() bool { + if x != nil { + return x.WithQuery + } + return false +} + var File_service_http_rewrite_rule_proto protoreflect.FileDescriptor var file_service_http_rewrite_rule_proto_rawDesc = []byte{ 0x0a, 0x1f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x5f, 0x68, 0x74, 0x74, 0x70, 0x5f, 0x72, 0x65, 0x77, 0x72, 0x69, 0x74, 0x65, 0x5f, 0x72, 0x75, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x02, 0x70, 0x62, 0x1a, 0x12, 0x72, 0x70, 0x63, 0x5f, 0x6d, 0x65, 0x73, 0x73, 0x61, - 0x67, 0x65, 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0xda, 0x01, 0x0a, 0x1c, 0x43, 0x72, + 0x67, 0x65, 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0xf8, 0x01, 0x0a, 0x1c, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x48, 0x54, 0x54, 0x50, 0x52, 0x65, 0x77, 0x72, 0x69, 0x74, 0x65, 0x52, 0x75, 0x6c, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x18, 0x0a, 0x07, 0x70, 0x61, 0x74, 0x74, 0x65, 0x72, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x70, 0x61, 0x74, @@ -296,41 +312,45 @@ var file_service_http_rewrite_rule_proto_rawDesc = []byte{ 0x72, 0x65, 0x61, 0x6b, 0x12, 0x1c, 0x0a, 0x09, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x48, 0x6f, 0x73, 0x74, 0x18, 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x48, 0x6f, 0x73, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x69, 0x73, 0x4f, 0x6e, 0x18, 0x07, 0x20, 0x01, 0x28, 0x08, - 0x52, 0x04, 0x69, 0x73, 0x4f, 0x6e, 0x22, 0x45, 0x0a, 0x1d, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, - 0x48, 0x54, 0x54, 0x50, 0x52, 0x65, 0x77, 0x72, 0x69, 0x74, 0x65, 0x52, 0x75, 0x6c, 0x65, 0x52, - 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x24, 0x0a, 0x0d, 0x72, 0x65, 0x77, 0x72, 0x69, - 0x74, 0x65, 0x52, 0x75, 0x6c, 0x65, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0d, - 0x72, 0x65, 0x77, 0x72, 0x69, 0x74, 0x65, 0x52, 0x75, 0x6c, 0x65, 0x49, 0x64, 0x22, 0x80, 0x02, - 0x0a, 0x1c, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x48, 0x54, 0x54, 0x50, 0x52, 0x65, 0x77, 0x72, - 0x69, 0x74, 0x65, 0x52, 0x75, 0x6c, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x24, - 0x0a, 0x0d, 0x72, 0x65, 0x77, 0x72, 0x69, 0x74, 0x65, 0x52, 0x75, 0x6c, 0x65, 0x49, 0x64, 0x18, - 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0d, 0x72, 0x65, 0x77, 0x72, 0x69, 0x74, 0x65, 0x52, 0x75, - 0x6c, 0x65, 0x49, 0x64, 0x12, 0x18, 0x0a, 0x07, 0x70, 0x61, 0x74, 0x74, 0x65, 0x72, 0x6e, 0x18, - 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x70, 0x61, 0x74, 0x74, 0x65, 0x72, 0x6e, 0x12, 0x18, - 0x0a, 0x07, 0x72, 0x65, 0x70, 0x6c, 0x61, 0x63, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, - 0x07, 0x72, 0x65, 0x70, 0x6c, 0x61, 0x63, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x6d, 0x6f, 0x64, 0x65, - 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6d, 0x6f, 0x64, 0x65, 0x12, 0x26, 0x0a, 0x0e, - 0x72, 0x65, 0x64, 0x69, 0x72, 0x65, 0x63, 0x74, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x18, 0x05, - 0x20, 0x01, 0x28, 0x05, 0x52, 0x0e, 0x72, 0x65, 0x64, 0x69, 0x72, 0x65, 0x63, 0x74, 0x53, 0x74, - 0x61, 0x74, 0x75, 0x73, 0x12, 0x18, 0x0a, 0x07, 0x69, 0x73, 0x42, 0x72, 0x65, 0x61, 0x6b, 0x18, - 0x06, 0x20, 0x01, 0x28, 0x08, 0x52, 0x07, 0x69, 0x73, 0x42, 0x72, 0x65, 0x61, 0x6b, 0x12, 0x1c, - 0x0a, 0x09, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x48, 0x6f, 0x73, 0x74, 0x18, 0x07, 0x20, 0x01, 0x28, - 0x09, 0x52, 0x09, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x48, 0x6f, 0x73, 0x74, 0x12, 0x12, 0x0a, 0x04, - 0x69, 0x73, 0x4f, 0x6e, 0x18, 0x08, 0x20, 0x01, 0x28, 0x08, 0x52, 0x04, 0x69, 0x73, 0x4f, 0x6e, - 0x32, 0xc7, 0x01, 0x0a, 0x16, 0x48, 0x54, 0x54, 0x50, 0x52, 0x65, 0x77, 0x72, 0x69, 0x74, 0x65, - 0x52, 0x75, 0x6c, 0x65, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x5c, 0x0a, 0x15, 0x63, - 0x72, 0x65, 0x61, 0x74, 0x65, 0x48, 0x54, 0x54, 0x50, 0x52, 0x65, 0x77, 0x72, 0x69, 0x74, 0x65, - 0x52, 0x75, 0x6c, 0x65, 0x12, 0x20, 0x2e, 0x70, 0x62, 0x2e, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, - 0x48, 0x54, 0x54, 0x50, 0x52, 0x65, 0x77, 0x72, 0x69, 0x74, 0x65, 0x52, 0x75, 0x6c, 0x65, 0x52, - 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x21, 0x2e, 0x70, 0x62, 0x2e, 0x43, 0x72, 0x65, 0x61, - 0x74, 0x65, 0x48, 0x54, 0x54, 0x50, 0x52, 0x65, 0x77, 0x72, 0x69, 0x74, 0x65, 0x52, 0x75, 0x6c, - 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x4f, 0x0a, 0x15, 0x75, 0x70, 0x64, - 0x61, 0x74, 0x65, 0x48, 0x54, 0x54, 0x50, 0x52, 0x65, 0x77, 0x72, 0x69, 0x74, 0x65, 0x52, 0x75, - 0x6c, 0x65, 0x12, 0x20, 0x2e, 0x70, 0x62, 0x2e, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x48, 0x54, - 0x54, 0x50, 0x52, 0x65, 0x77, 0x72, 0x69, 0x74, 0x65, 0x52, 0x75, 0x6c, 0x65, 0x52, 0x65, 0x71, - 0x75, 0x65, 0x73, 0x74, 0x1a, 0x14, 0x2e, 0x70, 0x62, 0x2e, 0x52, 0x50, 0x43, 0x55, 0x70, 0x64, - 0x61, 0x74, 0x65, 0x53, 0x75, 0x63, 0x63, 0x65, 0x73, 0x73, 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x2f, - 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x52, 0x04, 0x69, 0x73, 0x4f, 0x6e, 0x12, 0x1c, 0x0a, 0x09, 0x77, 0x69, 0x74, 0x68, 0x51, 0x75, + 0x65, 0x72, 0x79, 0x18, 0x08, 0x20, 0x01, 0x28, 0x08, 0x52, 0x09, 0x77, 0x69, 0x74, 0x68, 0x51, + 0x75, 0x65, 0x72, 0x79, 0x22, 0x45, 0x0a, 0x1d, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x48, 0x54, + 0x54, 0x50, 0x52, 0x65, 0x77, 0x72, 0x69, 0x74, 0x65, 0x52, 0x75, 0x6c, 0x65, 0x52, 0x65, 0x73, + 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x24, 0x0a, 0x0d, 0x72, 0x65, 0x77, 0x72, 0x69, 0x74, 0x65, + 0x52, 0x75, 0x6c, 0x65, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0d, 0x72, 0x65, + 0x77, 0x72, 0x69, 0x74, 0x65, 0x52, 0x75, 0x6c, 0x65, 0x49, 0x64, 0x22, 0x9e, 0x02, 0x0a, 0x1c, + 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x48, 0x54, 0x54, 0x50, 0x52, 0x65, 0x77, 0x72, 0x69, 0x74, + 0x65, 0x52, 0x75, 0x6c, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x24, 0x0a, 0x0d, + 0x72, 0x65, 0x77, 0x72, 0x69, 0x74, 0x65, 0x52, 0x75, 0x6c, 0x65, 0x49, 0x64, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x03, 0x52, 0x0d, 0x72, 0x65, 0x77, 0x72, 0x69, 0x74, 0x65, 0x52, 0x75, 0x6c, 0x65, + 0x49, 0x64, 0x12, 0x18, 0x0a, 0x07, 0x70, 0x61, 0x74, 0x74, 0x65, 0x72, 0x6e, 0x18, 0x02, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x07, 0x70, 0x61, 0x74, 0x74, 0x65, 0x72, 0x6e, 0x12, 0x18, 0x0a, 0x07, + 0x72, 0x65, 0x70, 0x6c, 0x61, 0x63, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x72, + 0x65, 0x70, 0x6c, 0x61, 0x63, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x6d, 0x6f, 0x64, 0x65, 0x18, 0x04, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6d, 0x6f, 0x64, 0x65, 0x12, 0x26, 0x0a, 0x0e, 0x72, 0x65, + 0x64, 0x69, 0x72, 0x65, 0x63, 0x74, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x18, 0x05, 0x20, 0x01, + 0x28, 0x05, 0x52, 0x0e, 0x72, 0x65, 0x64, 0x69, 0x72, 0x65, 0x63, 0x74, 0x53, 0x74, 0x61, 0x74, + 0x75, 0x73, 0x12, 0x18, 0x0a, 0x07, 0x69, 0x73, 0x42, 0x72, 0x65, 0x61, 0x6b, 0x18, 0x06, 0x20, + 0x01, 0x28, 0x08, 0x52, 0x07, 0x69, 0x73, 0x42, 0x72, 0x65, 0x61, 0x6b, 0x12, 0x1c, 0x0a, 0x09, + 0x70, 0x72, 0x6f, 0x78, 0x79, 0x48, 0x6f, 0x73, 0x74, 0x18, 0x07, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x09, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x48, 0x6f, 0x73, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x69, 0x73, + 0x4f, 0x6e, 0x18, 0x08, 0x20, 0x01, 0x28, 0x08, 0x52, 0x04, 0x69, 0x73, 0x4f, 0x6e, 0x12, 0x1c, + 0x0a, 0x09, 0x77, 0x69, 0x74, 0x68, 0x51, 0x75, 0x65, 0x72, 0x79, 0x18, 0x09, 0x20, 0x01, 0x28, + 0x08, 0x52, 0x09, 0x77, 0x69, 0x74, 0x68, 0x51, 0x75, 0x65, 0x72, 0x79, 0x32, 0xc7, 0x01, 0x0a, + 0x16, 0x48, 0x54, 0x54, 0x50, 0x52, 0x65, 0x77, 0x72, 0x69, 0x74, 0x65, 0x52, 0x75, 0x6c, 0x65, + 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x5c, 0x0a, 0x15, 0x63, 0x72, 0x65, 0x61, 0x74, + 0x65, 0x48, 0x54, 0x54, 0x50, 0x52, 0x65, 0x77, 0x72, 0x69, 0x74, 0x65, 0x52, 0x75, 0x6c, 0x65, + 0x12, 0x20, 0x2e, 0x70, 0x62, 0x2e, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x48, 0x54, 0x54, 0x50, + 0x52, 0x65, 0x77, 0x72, 0x69, 0x74, 0x65, 0x52, 0x75, 0x6c, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, + 0x73, 0x74, 0x1a, 0x21, 0x2e, 0x70, 0x62, 0x2e, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x48, 0x54, + 0x54, 0x50, 0x52, 0x65, 0x77, 0x72, 0x69, 0x74, 0x65, 0x52, 0x75, 0x6c, 0x65, 0x52, 0x65, 0x73, + 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x4f, 0x0a, 0x15, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x48, + 0x54, 0x54, 0x50, 0x52, 0x65, 0x77, 0x72, 0x69, 0x74, 0x65, 0x52, 0x75, 0x6c, 0x65, 0x12, 0x20, + 0x2e, 0x70, 0x62, 0x2e, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x48, 0x54, 0x54, 0x50, 0x52, 0x65, + 0x77, 0x72, 0x69, 0x74, 0x65, 0x52, 0x75, 0x6c, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, + 0x1a, 0x14, 0x2e, 0x70, 0x62, 0x2e, 0x52, 0x50, 0x43, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x53, + 0x75, 0x63, 0x63, 0x65, 0x73, 0x73, 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x2f, 0x70, 0x62, 0x62, 0x06, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/pkg/rpc/protos/service_http_rewrite_rule.proto b/pkg/rpc/protos/service_http_rewrite_rule.proto index 51d9cdf..f9b31e6 100644 --- a/pkg/rpc/protos/service_http_rewrite_rule.proto +++ b/pkg/rpc/protos/service_http_rewrite_rule.proto @@ -22,6 +22,7 @@ message CreateHTTPRewriteRuleRequest { bool isBreak = 5; string proxyHost = 6; bool isOn = 7; + bool withQuery = 8; } message CreateHTTPRewriteRuleResponse { @@ -38,4 +39,5 @@ message UpdateHTTPRewriteRuleRequest { bool isBreak = 6; string proxyHost = 7; bool isOn = 8; + bool withQuery = 9; } \ No newline at end of file diff --git a/pkg/serverconfigs/http_rewrite_rule.go b/pkg/serverconfigs/http_rewrite_rule.go index 812d865..ca428cd 100644 --- a/pkg/serverconfigs/http_rewrite_rule.go +++ b/pkg/serverconfigs/http_rewrite_rule.go @@ -25,6 +25,7 @@ const ( // - http://nginx.org/en/docs/http/ngx_http_rewrite_module.html // - https://httpd.apache.org/docs/current/mod/mod_rewrite.html // - https://httpd.apache.org/docs/2.4/rewrite/flags.html +// TODO 实现对其他代理服务的引用 type HTTPRewriteRule struct { Id int64 `yaml:"id" json:"id"` // ID IsOn bool `yaml:"isOn" json:"isOn"` // 是否开启 @@ -46,20 +47,25 @@ type HTTPRewriteRule struct { RedirectStatus int `yaml:"redirectStatus" json:"redirectStatus"` // 跳转的状态码 ProxyHost string `yaml:"proxyHost" json:"proxyHost"` // 代理模式下的Host - // TODO 实现对其他代理服务的引用 - // 要替换成的URL // 支持反向引用:${0}, ${1}, ...,也支持?P语法 // - 如果以 proxy:// 开头,表示目标为代理,首先会尝试作为代理ID请求,如果找不到,会尝试作为代理Host请求 Replace string `yaml:"replace" json:"replace"` - IsBreak bool `yaml:"isBreak" json:"isBreak"` // 终止向下解析 + // 其他选项 + IsBreak bool `yaml:"isBreak" json:"isBreak"` // 终止向下解析 + WithQuery bool `yaml:"withQuery" json:"withQuery"` // 是否保留QueryString - reg *regexp.Regexp + reg *regexp.Regexp + replaceHasVariables bool + proxyHostHasVariables bool } // 校验 func (this *HTTPRewriteRule) Init() error { + this.replaceHasVariables = configutils.HasVariables(this.Replace) + this.proxyHostHasVariables = configutils.HasVariables(this.ProxyHost) + reg, err := regexp.Compile(this.Pattern) if err != nil { return err @@ -115,7 +121,9 @@ func (this *HTTPRewriteRule) Match(requestPath string, formatter func(source str return "${" + varName + "}" }) - replace = formatter(replace) + if this.replaceHasVariables { + replace = formatter(replace) + } return replace, varMapping, true } @@ -124,3 +132,8 @@ func (this *HTTPRewriteRule) Match(requestPath string, formatter func(source str func (this *HTTPRewriteRule) IsExternalURL(url string) bool { return shared.RegexpExternalURL.MatchString(url) } + +// 判断ProxyHost是否有变量 +func (this *HTTPRewriteRule) ProxyHostHasVariables() bool { + return this.proxyHostHasVariables +}