mirror of
https://github.com/TeaOSLab/EdgeCommon.git
synced 2025-11-09 16:50:25 +08:00
可以配置是否在反向代理中添加X-Real-IP和X-Forwarded-*
This commit is contained in:
@@ -498,18 +498,19 @@ func (x *UpdateReverseProxyBackupOriginsRequest) GetOriginsJSON() []byte {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 修改是否启用
|
// 修改反向代理设置
|
||||||
type UpdateReverseProxyRequest struct {
|
type UpdateReverseProxyRequest struct {
|
||||||
state protoimpl.MessageState
|
state protoimpl.MessageState
|
||||||
sizeCache protoimpl.SizeCache
|
sizeCache protoimpl.SizeCache
|
||||||
unknownFields protoimpl.UnknownFields
|
unknownFields protoimpl.UnknownFields
|
||||||
|
|
||||||
ReverseProxyId int64 `protobuf:"varint,1,opt,name=reverseProxyId,proto3" json:"reverseProxyId,omitempty"`
|
ReverseProxyId int64 `protobuf:"varint,1,opt,name=reverseProxyId,proto3" json:"reverseProxyId,omitempty"`
|
||||||
RequestHostType int32 `protobuf:"varint,6,opt,name=requestHostType,proto3" json:"requestHostType,omitempty"`
|
RequestHostType int32 `protobuf:"varint,6,opt,name=requestHostType,proto3" json:"requestHostType,omitempty"`
|
||||||
RequestHost string `protobuf:"bytes,2,opt,name=requestHost,proto3" json:"requestHost,omitempty"`
|
RequestHost string `protobuf:"bytes,2,opt,name=requestHost,proto3" json:"requestHost,omitempty"`
|
||||||
RequestURI string `protobuf:"bytes,3,opt,name=requestURI,proto3" json:"requestURI,omitempty"`
|
RequestURI string `protobuf:"bytes,3,opt,name=requestURI,proto3" json:"requestURI,omitempty"`
|
||||||
StripPrefix string `protobuf:"bytes,4,opt,name=stripPrefix,proto3" json:"stripPrefix,omitempty"`
|
StripPrefix string `protobuf:"bytes,4,opt,name=stripPrefix,proto3" json:"stripPrefix,omitempty"`
|
||||||
AutoFlush bool `protobuf:"varint,5,opt,name=autoFlush,proto3" json:"autoFlush,omitempty"`
|
AutoFlush bool `protobuf:"varint,5,opt,name=autoFlush,proto3" json:"autoFlush,omitempty"`
|
||||||
|
AddHeaders []string `protobuf:"bytes,7,rep,name=addHeaders,proto3" json:"addHeaders,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (x *UpdateReverseProxyRequest) Reset() {
|
func (x *UpdateReverseProxyRequest) Reset() {
|
||||||
@@ -586,6 +587,13 @@ func (x *UpdateReverseProxyRequest) GetAutoFlush() bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (x *UpdateReverseProxyRequest) GetAddHeaders() []string {
|
||||||
|
if x != nil {
|
||||||
|
return x.AddHeaders
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
var File_service_reverse_proxy_proto protoreflect.FileDescriptor
|
var File_service_reverse_proxy_proto protoreflect.FileDescriptor
|
||||||
|
|
||||||
var file_service_reverse_proxy_proto_rawDesc = []byte{
|
var file_service_reverse_proxy_proto_rawDesc = []byte{
|
||||||
@@ -652,7 +660,7 @@ var file_service_reverse_proxy_proto_rawDesc = []byte{
|
|||||||
0x72, 0x65, 0x76, 0x65, 0x72, 0x73, 0x65, 0x50, 0x72, 0x6f, 0x78, 0x79, 0x49, 0x64, 0x12, 0x20,
|
0x72, 0x65, 0x76, 0x65, 0x72, 0x73, 0x65, 0x50, 0x72, 0x6f, 0x78, 0x79, 0x49, 0x64, 0x12, 0x20,
|
||||||
0x0a, 0x0b, 0x6f, 0x72, 0x69, 0x67, 0x69, 0x6e, 0x73, 0x4a, 0x53, 0x4f, 0x4e, 0x18, 0x02, 0x20,
|
0x0a, 0x0b, 0x6f, 0x72, 0x69, 0x67, 0x69, 0x6e, 0x73, 0x4a, 0x53, 0x4f, 0x4e, 0x18, 0x02, 0x20,
|
||||||
0x01, 0x28, 0x0c, 0x52, 0x0b, 0x6f, 0x72, 0x69, 0x67, 0x69, 0x6e, 0x73, 0x4a, 0x53, 0x4f, 0x4e,
|
0x01, 0x28, 0x0c, 0x52, 0x0b, 0x6f, 0x72, 0x69, 0x67, 0x69, 0x6e, 0x73, 0x4a, 0x53, 0x4f, 0x4e,
|
||||||
0x22, 0xef, 0x01, 0x0a, 0x19, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x52, 0x65, 0x76, 0x65, 0x72,
|
0x22, 0x8f, 0x02, 0x0a, 0x19, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x52, 0x65, 0x76, 0x65, 0x72,
|
||||||
0x73, 0x65, 0x50, 0x72, 0x6f, 0x78, 0x79, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x26,
|
0x73, 0x65, 0x50, 0x72, 0x6f, 0x78, 0x79, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x26,
|
||||||
0x0a, 0x0e, 0x72, 0x65, 0x76, 0x65, 0x72, 0x73, 0x65, 0x50, 0x72, 0x6f, 0x78, 0x79, 0x49, 0x64,
|
0x0a, 0x0e, 0x72, 0x65, 0x76, 0x65, 0x72, 0x73, 0x65, 0x50, 0x72, 0x6f, 0x78, 0x79, 0x49, 0x64,
|
||||||
0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0e, 0x72, 0x65, 0x76, 0x65, 0x72, 0x73, 0x65, 0x50,
|
0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0e, 0x72, 0x65, 0x76, 0x65, 0x72, 0x73, 0x65, 0x50,
|
||||||
@@ -667,7 +675,9 @@ var file_service_reverse_proxy_proto_rawDesc = []byte{
|
|||||||
0x78, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x73, 0x74, 0x72, 0x69, 0x70, 0x50, 0x72,
|
0x78, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x73, 0x74, 0x72, 0x69, 0x70, 0x50, 0x72,
|
||||||
0x65, 0x66, 0x69, 0x78, 0x12, 0x1c, 0x0a, 0x09, 0x61, 0x75, 0x74, 0x6f, 0x46, 0x6c, 0x75, 0x73,
|
0x65, 0x66, 0x69, 0x78, 0x12, 0x1c, 0x0a, 0x09, 0x61, 0x75, 0x74, 0x6f, 0x46, 0x6c, 0x75, 0x73,
|
||||||
0x68, 0x18, 0x05, 0x20, 0x01, 0x28, 0x08, 0x52, 0x09, 0x61, 0x75, 0x74, 0x6f, 0x46, 0x6c, 0x75,
|
0x68, 0x18, 0x05, 0x20, 0x01, 0x28, 0x08, 0x52, 0x09, 0x61, 0x75, 0x74, 0x6f, 0x46, 0x6c, 0x75,
|
||||||
0x73, 0x68, 0x32, 0xa2, 0x05, 0x0a, 0x13, 0x52, 0x65, 0x76, 0x65, 0x72, 0x73, 0x65, 0x50, 0x72,
|
0x73, 0x68, 0x12, 0x1e, 0x0a, 0x0a, 0x61, 0x64, 0x64, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x73,
|
||||||
|
0x18, 0x07, 0x20, 0x03, 0x28, 0x09, 0x52, 0x0a, 0x61, 0x64, 0x64, 0x48, 0x65, 0x61, 0x64, 0x65,
|
||||||
|
0x72, 0x73, 0x32, 0xa2, 0x05, 0x0a, 0x13, 0x52, 0x65, 0x76, 0x65, 0x72, 0x73, 0x65, 0x50, 0x72,
|
||||||
0x6f, 0x78, 0x79, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x53, 0x0a, 0x12, 0x63, 0x72,
|
0x6f, 0x78, 0x79, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x53, 0x0a, 0x12, 0x63, 0x72,
|
||||||
0x65, 0x61, 0x74, 0x65, 0x52, 0x65, 0x76, 0x65, 0x72, 0x73, 0x65, 0x50, 0x72, 0x6f, 0x78, 0x79,
|
0x65, 0x61, 0x74, 0x65, 0x52, 0x65, 0x76, 0x65, 0x72, 0x73, 0x65, 0x50, 0x72, 0x6f, 0x78, 0x79,
|
||||||
0x12, 0x1d, 0x2e, 0x70, 0x62, 0x2e, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x52, 0x65, 0x76, 0x65,
|
0x12, 0x1d, 0x2e, 0x70, 0x62, 0x2e, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x52, 0x65, 0x76, 0x65,
|
||||||
@@ -936,7 +946,7 @@ type ReverseProxyServiceClient interface {
|
|||||||
UpdateReverseProxyPrimaryOrigins(ctx context.Context, in *UpdateReverseProxyPrimaryOriginsRequest, opts ...grpc.CallOption) (*RPCSuccess, error)
|
UpdateReverseProxyPrimaryOrigins(ctx context.Context, in *UpdateReverseProxyPrimaryOriginsRequest, opts ...grpc.CallOption) (*RPCSuccess, error)
|
||||||
// 修改备用源站信息
|
// 修改备用源站信息
|
||||||
UpdateReverseProxyBackupOrigins(ctx context.Context, in *UpdateReverseProxyBackupOriginsRequest, opts ...grpc.CallOption) (*RPCSuccess, error)
|
UpdateReverseProxyBackupOrigins(ctx context.Context, in *UpdateReverseProxyBackupOriginsRequest, opts ...grpc.CallOption) (*RPCSuccess, error)
|
||||||
// 修改是否启用
|
// 修改反向代理设置
|
||||||
UpdateReverseProxy(ctx context.Context, in *UpdateReverseProxyRequest, opts ...grpc.CallOption) (*RPCSuccess, error)
|
UpdateReverseProxy(ctx context.Context, in *UpdateReverseProxyRequest, opts ...grpc.CallOption) (*RPCSuccess, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1025,7 +1035,7 @@ type ReverseProxyServiceServer interface {
|
|||||||
UpdateReverseProxyPrimaryOrigins(context.Context, *UpdateReverseProxyPrimaryOriginsRequest) (*RPCSuccess, error)
|
UpdateReverseProxyPrimaryOrigins(context.Context, *UpdateReverseProxyPrimaryOriginsRequest) (*RPCSuccess, error)
|
||||||
// 修改备用源站信息
|
// 修改备用源站信息
|
||||||
UpdateReverseProxyBackupOrigins(context.Context, *UpdateReverseProxyBackupOriginsRequest) (*RPCSuccess, error)
|
UpdateReverseProxyBackupOrigins(context.Context, *UpdateReverseProxyBackupOriginsRequest) (*RPCSuccess, error)
|
||||||
// 修改是否启用
|
// 修改反向代理设置
|
||||||
UpdateReverseProxy(context.Context, *UpdateReverseProxyRequest) (*RPCSuccess, error)
|
UpdateReverseProxy(context.Context, *UpdateReverseProxyRequest) (*RPCSuccess, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ service ReverseProxyService {
|
|||||||
// 修改备用源站信息
|
// 修改备用源站信息
|
||||||
rpc updateReverseProxyBackupOrigins (UpdateReverseProxyBackupOriginsRequest) returns (RPCSuccess);
|
rpc updateReverseProxyBackupOrigins (UpdateReverseProxyBackupOriginsRequest) returns (RPCSuccess);
|
||||||
|
|
||||||
// 修改是否启用
|
// 修改反向代理设置
|
||||||
rpc updateReverseProxy (UpdateReverseProxyRequest) returns (RPCSuccess);
|
rpc updateReverseProxy (UpdateReverseProxyRequest) returns (RPCSuccess);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,7 +76,7 @@ message UpdateReverseProxyBackupOriginsRequest {
|
|||||||
bytes originsJSON = 2;
|
bytes originsJSON = 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 修改是否启用
|
// 修改反向代理设置
|
||||||
message UpdateReverseProxyRequest {
|
message UpdateReverseProxyRequest {
|
||||||
int64 reverseProxyId = 1;
|
int64 reverseProxyId = 1;
|
||||||
int32 requestHostType = 6;
|
int32 requestHostType = 6;
|
||||||
@@ -84,4 +84,5 @@ message UpdateReverseProxyRequest {
|
|||||||
string requestURI = 3;
|
string requestURI = 3;
|
||||||
string stripPrefix = 4;
|
string stripPrefix = 4;
|
||||||
bool autoFlush = 5;
|
bool autoFlush = 5;
|
||||||
|
repeated string addHeaders = 7;
|
||||||
}
|
}
|
||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/schedulingconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/schedulingconfigs"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
||||||
|
"github.com/iwind/TeaGo/lists"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -30,6 +31,8 @@ type ReverseProxyConfig struct {
|
|||||||
RequestHost string `yaml:"requestHost" json:"requestHost"` // 请求Host,支持变量
|
RequestHost string `yaml:"requestHost" json:"requestHost"` // 请求Host,支持变量
|
||||||
RequestURI string `yaml:"requestURI" json:"requestURI"` // 请求URI,支持变量,如果同时定义了StripPrefix,则先执行StripPrefix
|
RequestURI string `yaml:"requestURI" json:"requestURI"` // 请求URI,支持变量,如果同时定义了StripPrefix,则先执行StripPrefix
|
||||||
|
|
||||||
|
AddHeaders []string `yaml:"addHeaders" json:"addHeaders"` // 自动添加的Header
|
||||||
|
|
||||||
AutoFlush bool `yaml:"autoFlush" json:"autoFlush"` // 是否自动刷新缓冲区,在比如SSE(server-sent events)场景下很有用
|
AutoFlush bool `yaml:"autoFlush" json:"autoFlush"` // 是否自动刷新缓冲区,在比如SSE(server-sent events)场景下很有用
|
||||||
|
|
||||||
requestHostHasVariables bool
|
requestHostHasVariables bool
|
||||||
@@ -40,6 +43,13 @@ type ReverseProxyConfig struct {
|
|||||||
schedulingIsBackup bool
|
schedulingIsBackup bool
|
||||||
schedulingObject schedulingconfigs.SchedulingInterface
|
schedulingObject schedulingconfigs.SchedulingInterface
|
||||||
schedulingLocker sync.Mutex
|
schedulingLocker sync.Mutex
|
||||||
|
|
||||||
|
addXRealIPHeader bool
|
||||||
|
addXForwardedForHeader bool
|
||||||
|
addForwardedHeader bool
|
||||||
|
addXForwardedByHeader bool
|
||||||
|
addXForwardedHostHeader bool
|
||||||
|
addXForwardedProtoHeader bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// 初始化
|
// 初始化
|
||||||
@@ -67,6 +77,13 @@ func (this *ReverseProxyConfig) Init() error {
|
|||||||
// scheduling
|
// scheduling
|
||||||
this.SetupScheduling(false)
|
this.SetupScheduling(false)
|
||||||
|
|
||||||
|
// Header
|
||||||
|
this.addXRealIPHeader = lists.ContainsString(this.AddHeaders, "X-Real-IP")
|
||||||
|
this.addXForwardedForHeader = lists.ContainsString(this.AddHeaders, "X-Forwarded-For")
|
||||||
|
this.addXForwardedByHeader = lists.ContainsString(this.AddHeaders, "X-Forwarded-By")
|
||||||
|
this.addXForwardedHostHeader = lists.ContainsString(this.AddHeaders, "X-Forwarded-Host")
|
||||||
|
this.addXForwardedProtoHeader = lists.ContainsString(this.AddHeaders, "X-Forwarded-Proto")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -170,3 +187,28 @@ func (this *ReverseProxyConfig) RequestHostHasVariables() bool {
|
|||||||
func (this *ReverseProxyConfig) RequestURIHasVariables() bool {
|
func (this *ReverseProxyConfig) RequestURIHasVariables() bool {
|
||||||
return this.requestURIHasVariables
|
return this.requestURIHasVariables
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 是否添加X-Real-IP
|
||||||
|
func (this *ReverseProxyConfig) ShouldAddXRealIPHeader() bool {
|
||||||
|
return this.addXRealIPHeader
|
||||||
|
}
|
||||||
|
|
||||||
|
// 是否添加X-Forwarded-For
|
||||||
|
func (this *ReverseProxyConfig) ShouldAddXForwardedForHeader() bool {
|
||||||
|
return this.addXForwardedForHeader
|
||||||
|
}
|
||||||
|
|
||||||
|
// 是否添加X-Forwarded-By
|
||||||
|
func (this *ReverseProxyConfig) ShouldAddXForwardedByHeader() bool {
|
||||||
|
return this.addXForwardedByHeader
|
||||||
|
}
|
||||||
|
|
||||||
|
// 是否添加X-Forwarded-Host
|
||||||
|
func (this *ReverseProxyConfig) ShouldAddXForwardedHostHeader() bool {
|
||||||
|
return this.addXForwardedHostHeader
|
||||||
|
}
|
||||||
|
|
||||||
|
// 是否添加X-Forwarded-Proto
|
||||||
|
func (this *ReverseProxyConfig) ShouldAddXForwardedProtoHeader() bool {
|
||||||
|
return this.addXForwardedProtoHeader
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user