/
host.go
78 lines (70 loc) · 1.81 KB
/
host.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package proxy
import (
"strings"
"github.com/rs/zerolog/log"
"github.com/dxvgef/tsing-gateway/global"
"github.com/dxvgef/tsing-gateway/middleware"
)
// 写入主机,如果存在则覆盖,不存在则创建
func SetHost(hostname string, host global.HostType) error {
hostname = strings.ToLower(hostname)
global.Hosts.Store(hostname, host)
// 更新中间件
mwLen := len(host.Middleware)
if mwLen == 0 {
global.HostMiddleware.Delete(hostname)
return nil
}
mw := make([]global.MiddlewareType, mwLen)
for k := range host.Middleware {
m, err := middleware.Build(host.Middleware[k].Name, host.Middleware[k].Config, false)
if err != nil {
log.Err(err).Caller().Send()
return err
}
mw = append(mw, m)
}
global.HostMiddleware.Store(hostname, mw)
return nil
}
// 删除主机
func DelHost(hostname string) error {
hostname = strings.ToLower(hostname)
global.Hosts.Delete(hostname)
global.HostMiddleware.Delete(hostname)
return nil
}
// 匹配主机名,返回对应的路由组ID
func matchHost(reqHost string) (string, string, bool) {
pos := strings.LastIndex(reqHost, ":")
if pos > -1 {
reqHost = reqHost[:pos]
}
if v, exist := global.Hosts.Load(reqHost); exist {
host, ok := v.(global.HostType)
if !ok {
log.Error().Caller().Msg("类型断言失败")
return "", "", false
}
return reqHost, host.RouteGroupID, ok
}
reqHost = "*"
if v, exist := global.Hosts.Load(reqHost); exist {
host, ok := v.(global.HostType)
if !ok {
log.Error().Caller().Msg("类型断言失败")
return "", "", false
}
return reqHost, host.RouteGroupID, ok
}
v, exist := global.Hosts.Load(reqHost)
if !exist {
return "", "", false
}
host, ok := v.(global.HostType)
if !ok {
log.Error().Caller().Msg("类型断言失败")
return "", "", false
}
return reqHost, host.RouteGroupID, ok
}