mirror of
https://github.com/TeaOSLab/EdgeCommon.git
synced 2026-03-31 09:55:20 +08:00
修改反向代理实现
This commit is contained in:
31
pkg/serverconfigs/http_location_config.go
Normal file
31
pkg/serverconfigs/http_location_config.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package serverconfigs
|
||||
|
||||
type HTTPLocationConfig struct {
|
||||
Id int64 `yaml:"id" json:"id"` // ID
|
||||
IsOn bool `yaml:"isOn" json:"isOn"` // 是否启用
|
||||
Pattern string `yaml:"pattern" json:"pattern"` // 匹配规则 TODO 未来支持更多样的匹配规则
|
||||
Name string `yaml:"name" json:"name"` // 名称
|
||||
Web *HTTPWebConfig `yaml:"web" json:"web"` // Web配置
|
||||
URLPrefix string `yaml:"urlPrefix" json:"urlPrefix"` // 实际的URL前缀,TODO 未来支持变量
|
||||
Description string `yaml:"description" json:"description"` // 描述
|
||||
ReverseProxyRef *ReverseProxyRef `yaml:"reverseProxyRef" json:"reverseProxyRef"` // 反向代理引用
|
||||
ReverseProxy *ReverseProxyConfig `yaml:"reverseProxy" json:"reverseProxy"` // 反向代理设置
|
||||
}
|
||||
|
||||
func (this *HTTPLocationConfig) Init() error {
|
||||
if this.Web != nil {
|
||||
err := this.Web.Init()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if this.ReverseProxy != nil {
|
||||
err := this.Web.Init()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -5,7 +5,7 @@ import "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
||||
type HTTPWebConfig struct {
|
||||
Id int64 `yaml:"id" json:"id"` // ID
|
||||
IsOn bool `yaml:"isOn" json:"isOn"` // 是否启用
|
||||
Locations []*LocationConfig `yaml:"locations" json:"locations"` // 路径规则 TODO
|
||||
Locations []*HTTPLocationConfig `yaml:"locations" json:"locations"` // 路径规则 TODO
|
||||
GzipRef *HTTPGzipRef `yaml:"gzipRef" json:"gzipRef"`
|
||||
Gzip *HTTPGzipConfig `yaml:"gzip" json:"gzip"` // Gzip配置
|
||||
Charset string `yaml:"charset" json:"charset"` // 字符编码
|
||||
@@ -21,4 +21,10 @@ type HTTPWebConfig struct {
|
||||
StatRef *HTTPStatRef `yaml:"statRef" json:"statRef"` // 统计配置
|
||||
CacheRef *HTTPCacheRef `yaml:"cacheRef" json:"cacheRef"` // 缓存配置
|
||||
FirewallRef *HTTPFirewallRef `yaml:"firewallRef" json:"firewallRef"` // 防火墙设置
|
||||
WebsocketRef *HTTPWebsocketRef `yaml:"websocketRef" json:"websocketRef"` // Websocket应用配置
|
||||
Websocket *HTTPWebsocketConfig `yaml:"websocket" json:"websocket"` // Websocket配置
|
||||
}
|
||||
|
||||
func (this *HTTPWebConfig) Init() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
65
pkg/serverconfigs/http_websocket_config.go
Normal file
65
pkg/serverconfigs/http_websocket_config.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package serverconfigs
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/configutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
"time"
|
||||
)
|
||||
|
||||
// websocket设置
|
||||
type HTTPWebsocketConfig struct {
|
||||
IsOn bool `yaml:"isOn" json:"isOn"` // 是否开启
|
||||
|
||||
// 握手超时时间
|
||||
HandshakeTimeout *shared.TimeDuration `yaml:"handshakeTimeout" json:"handshakeTimeout"`
|
||||
|
||||
// 允许的域名,支持 www.example.com, example.com, .example.com, *.example.com
|
||||
AllowAllOrigins bool `yaml:"allowAllOrigins" json:"allowAllOrigins"`
|
||||
Origins []string `yaml:"origins" json:"origins"`
|
||||
|
||||
// 转发方式
|
||||
ForwardMode HTTPWebsocketForwardMode `yaml:"forwardMode" json:"forwardMode"`
|
||||
|
||||
handshakeTimeoutDuration time.Duration
|
||||
}
|
||||
|
||||
// 获取新对象
|
||||
func NewHTTPWebsocketConfig() *HTTPWebsocketConfig {
|
||||
return &HTTPWebsocketConfig{
|
||||
IsOn: true,
|
||||
}
|
||||
}
|
||||
|
||||
// 校验
|
||||
func (this *HTTPWebsocketConfig) Init() error {
|
||||
// duration
|
||||
if this.HandshakeTimeout != nil {
|
||||
this.handshakeTimeoutDuration = this.HandshakeTimeout.Duration()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// 获取握手超时时间
|
||||
func (this *HTTPWebsocketConfig) HandshakeTimeoutDuration() time.Duration {
|
||||
return this.handshakeTimeoutDuration
|
||||
}
|
||||
|
||||
// 转发模式名称
|
||||
func (this *HTTPWebsocketConfig) ForwardModeSummary() maps.Map {
|
||||
for _, mode := range AllWebsocketForwardModes() {
|
||||
if mode["mode"] == this.ForwardMode {
|
||||
return mode
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 匹配域名
|
||||
func (this *HTTPWebsocketConfig) MatchOrigin(origin string) bool {
|
||||
if this.AllowAllOrigins {
|
||||
return true
|
||||
}
|
||||
return configutils.MatchDomains(this.Origins, origin)
|
||||
}
|
||||
27
pkg/serverconfigs/http_websocket_forward_modes.go
Normal file
27
pkg/serverconfigs/http_websocket_forward_modes.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package serverconfigs
|
||||
|
||||
import "github.com/iwind/TeaGo/maps"
|
||||
|
||||
// Websocket转发类型
|
||||
type HTTPWebsocketForwardMode = string
|
||||
|
||||
const (
|
||||
HTTPWebsocketForwardModeWebsocket = "websocket"
|
||||
HTTPWebsocketForwardModeHttp = "http"
|
||||
)
|
||||
|
||||
// 所有的转发方式
|
||||
func AllWebsocketForwardModes() []maps.Map {
|
||||
return []maps.Map{
|
||||
{
|
||||
"name": "Websocket连接",
|
||||
"mode": HTTPWebsocketForwardModeWebsocket,
|
||||
"description": "通过Websocket连接后端服务器并发送数据",
|
||||
},
|
||||
{
|
||||
"name": "HTTP连接",
|
||||
"mode": HTTPWebsocketForwardModeHttp,
|
||||
"description": "通过HTTP PUT转发服务器到后端服务器",
|
||||
},
|
||||
}
|
||||
}
|
||||
6
pkg/serverconfigs/http_websocket_ref.go
Normal file
6
pkg/serverconfigs/http_websocket_ref.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package serverconfigs
|
||||
|
||||
type HTTPWebsocketRef struct {
|
||||
IsOn bool `yaml:"isOn" json:"isOn"`
|
||||
WebsocketId int64 `yaml:"websocketId" json:"websocketIs"`
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
package serverconfigs
|
||||
|
||||
type LocationConfig struct {
|
||||
}
|
||||
53
pkg/serverconfigs/location_pattern_types.go
Normal file
53
pkg/serverconfigs/location_pattern_types.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package serverconfigs
|
||||
|
||||
import "github.com/iwind/TeaGo/maps"
|
||||
|
||||
// 匹配类型
|
||||
type HTTPLocationPatternType = int
|
||||
|
||||
// 内置的匹配类型定义
|
||||
const (
|
||||
HTTPLocationPatternTypePrefix HTTPLocationPatternType = 1
|
||||
HTTPLocationPatternTypeExact HTTPLocationPatternType = 2
|
||||
HTTPLocationPatternTypeRegexp HTTPLocationPatternType = 3
|
||||
)
|
||||
|
||||
// 取得所有的匹配类型信息
|
||||
func AllLocationPatternTypes() []maps.Map {
|
||||
return []maps.Map{
|
||||
{
|
||||
"name": "匹配前缀",
|
||||
"type": HTTPLocationPatternTypePrefix,
|
||||
"description": "带有此前缀的路径才会被匹配",
|
||||
},
|
||||
{
|
||||
"name": "精准匹配",
|
||||
"type": HTTPLocationPatternTypeExact,
|
||||
"description": "带此路径完全一样的路径才会被匹配",
|
||||
},
|
||||
{
|
||||
"name": "正则表达式匹配",
|
||||
"type": HTTPLocationPatternTypeRegexp,
|
||||
"description": "通过正则表达式来匹配路径,<a href=\"http://teaos.cn/doc/regexp/Regexp.md\" target=\"_blank\">正则表达式语法 »</a>",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// 查找单个匹配类型信息
|
||||
func FindLocationPatternType(patternType int) maps.Map {
|
||||
for _, t := range AllLocationPatternTypes() {
|
||||
if t["type"] == patternType {
|
||||
return t
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 查找单个匹配类型名称
|
||||
func FindLocationPatternTypeName(patternType int) string {
|
||||
t := FindLocationPatternType(patternType)
|
||||
if t == nil {
|
||||
return ""
|
||||
}
|
||||
return t["name"].(string)
|
||||
}
|
||||
@@ -8,7 +8,8 @@ import (
|
||||
|
||||
// 反向代理设置
|
||||
type ReverseProxyConfig struct {
|
||||
IsOn bool `yaml:"isOn" json:"isOn"` // 是否启用 TODO
|
||||
Id int64 `yaml:"id" json:"id"` // ID
|
||||
IsOn bool `yaml:"isOn" json:"isOn"` // 是否启用
|
||||
PrimaryOrigins []*OriginServerConfig `yaml:"primaryOrigins" json:"primaryOrigins"` // 主要源站列表
|
||||
BackupOrigins []*OriginServerConfig `yaml:"backupOrigins" json:"backupOrigins"` // 备用源站列表
|
||||
Scheduling *SchedulingConfig `yaml:"scheduling" json:"scheduling"` // 调度算法选项
|
||||
|
||||
7
pkg/serverconfigs/reverse_proxy_ref.go
Normal file
7
pkg/serverconfigs/reverse_proxy_ref.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package serverconfigs
|
||||
|
||||
// 反向代理引用
|
||||
type ReverseProxyRef struct {
|
||||
IsOn bool `yaml:"isOn" json:"isOn"` // 是否启用
|
||||
ReverseProxyId int64 `yaml:"reverseProxyId" json:"reverseProxyId"` // 反向代理ID
|
||||
}
|
||||
@@ -28,7 +28,8 @@ type ServerConfig struct {
|
||||
Web *HTTPWebConfig `yaml:"web" json:"web"`
|
||||
|
||||
// 反向代理配置
|
||||
ReverseProxy *ReverseProxyConfig `yaml:"reverseProxy" json:"reverseProxy"`
|
||||
ReverseProxyRef *ReverseProxyRef `yaml:"reverseProxyRef" json:"reverseProxyRef"`
|
||||
ReverseProxy *ReverseProxyConfig `yaml:"reverseProxy" json:"reverseProxy"`
|
||||
}
|
||||
|
||||
// 从JSON中解析Server配置
|
||||
|
||||
Reference in New Issue
Block a user