/
metadata.go
85 lines (76 loc) · 2.48 KB
/
metadata.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
79
80
81
82
83
84
85
package tunnel
import (
"github.com/jxo-me/netx/x/app"
"strings"
"time"
"github.com/jxo-me/netx/core/ingress"
"github.com/jxo-me/netx/core/logger"
mdata "github.com/jxo-me/netx/core/metadata"
mdutil "github.com/jxo-me/netx/core/metadata/util"
"github.com/jxo-me/netx/core/sd"
"github.com/jxo-me/netx/relay"
xingress "github.com/jxo-me/netx/x/ingress"
"github.com/jxo-me/netx/x/internal/util/mux"
)
const (
defaultTTL = 15 * time.Second
)
type metadata struct {
readTimeout time.Duration
entryPoint string
entryPointID relay.TunnelID
entryPointProxyProtocol int
directTunnel bool
tunnelTTL time.Duration
ingress ingress.IIngress
sd sd.ISD
muxCfg *mux.Config
}
func (h *tunnelHandler) parseMetadata(md mdata.IMetaData) (err error) {
h.md.readTimeout = mdutil.GetDuration(md, "readTimeout")
h.md.tunnelTTL = mdutil.GetDuration(md, "tunnel.ttl")
if h.md.tunnelTTL <= 0 {
h.md.tunnelTTL = defaultTTL
}
h.md.directTunnel = mdutil.GetBool(md, "tunnel.direct")
h.md.entryPoint = mdutil.GetString(md, "entrypoint")
h.md.entryPointID = parseTunnelID(mdutil.GetString(md, "entrypoint.id"))
h.md.entryPointProxyProtocol = mdutil.GetInt(md, "entrypoint.ProxyProtocol")
h.md.ingress = app.Runtime.IngressRegistry().Get(mdutil.GetString(md, "ingress"))
if h.md.ingress == nil {
var rules []*ingress.Rule
for _, s := range strings.Split(mdutil.GetString(md, "tunnel"), ",") {
ss := strings.SplitN(s, ":", 2)
if len(ss) != 2 {
continue
}
rules = append(rules, &ingress.Rule{
Hostname: ss[0],
Endpoint: ss[1],
})
}
if len(rules) > 0 {
h.md.ingress = xingress.NewIngress(
xingress.RulesOption(rules),
xingress.LoggerOption(logger.Default().WithFields(map[string]any{
"kind": "ingress",
"ingress": "@internal",
})),
)
}
}
h.md.sd = app.Runtime.SDRegistry().Get(mdutil.GetString(md, "sd"))
h.md.muxCfg = &mux.Config{
Version: mdutil.GetInt(md, "mux.version"),
KeepAliveInterval: mdutil.GetDuration(md, "mux.keepaliveInterval"),
KeepAliveDisabled: mdutil.GetBool(md, "mux.keepaliveDisabled"),
KeepAliveTimeout: mdutil.GetDuration(md, "mux.keepaliveTimeout"),
MaxFrameSize: mdutil.GetInt(md, "mux.maxFrameSize"),
MaxReceiveBuffer: mdutil.GetInt(md, "mux.maxReceiveBuffer"),
MaxStreamBuffer: mdutil.GetInt(md, "mux.maxStreamBuffer"),
}
if h.md.muxCfg.Version == 0 {
h.md.muxCfg.Version = 2
}
return
}