-
Notifications
You must be signed in to change notification settings - Fork 128
/
factory.go
265 lines (230 loc) · 8.8 KB
/
factory.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
Copyright NetFoundry Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package xgress_edge_tunnel
import (
"fmt"
"github.com/michaelquigley/pfxlog"
"github.com/openziti/channel/v2"
"github.com/openziti/foundation/v2/stringz"
"github.com/openziti/identity"
"github.com/openziti/metrics"
"github.com/openziti/ziti/common/pb/edge_ctrl_pb"
"github.com/openziti/ziti/router"
"github.com/openziti/ziti/router/env"
"github.com/openziti/ziti/router/handler_edge_ctrl"
"github.com/openziti/ziti/router/state"
"github.com/openziti/ziti/router/xgress"
"github.com/pkg/errors"
"strings"
"time"
)
const (
DefaultMode = "tproxy"
DefaultServicePollRate = 15 * time.Second
DefaultDnsResolver = "udp://127.0.0.1:53"
DefaultDnsServiceIpRange = "100.64.0.1/10"
)
type Factory struct {
id *identity.TokenId
ctrls env.NetworkControllers
routerConfig *router.Config
stateManager state.Manager
serviceListHandler *handler_edge_ctrl.ServiceListHandler
tunneler *tunneler
metricsRegistry metrics.UsageRegistry
env env.RouterEnv
}
func (self *Factory) NotifyOfReconnect(channel.Channel) {
pfxlog.Logger().Info("control channel reconnected, re-establishing hosted services")
self.tunneler.HandleReconnect()
}
func (self *Factory) GetTraceDecoders() []channel.TraceMessageDecoder {
return nil
}
func (self *Factory) Enabled() bool {
return true
}
func (self *Factory) BindChannel(binding channel.Binding) error {
self.serviceListHandler = handler_edge_ctrl.NewServiceListHandler(self.tunneler.servicePoller.handleServiceListUpdate)
binding.AddTypedReceiveHandler(self.serviceListHandler)
binding.AddReceiveHandlerF(int32(edge_ctrl_pb.ContentType_CreateTunnelTerminatorResponseType), self.tunneler.fabricProvider.HandleTunnelResponse)
return nil
}
func (self *Factory) Run(env env.RouterEnv) error {
self.ctrls = env.GetNetworkControllers()
if self.tunneler.listenOptions != nil {
return self.tunneler.Start(env.GetCloseNotify())
}
return nil
}
func (self *Factory) LoadConfig(map[interface{}]interface{}) error {
return nil
}
func (self *Factory) DefaultRequestTimeout() time.Duration {
return self.routerConfig.Ctrl.DefaultRequestTimeout
}
// NewFactory constructs a new Edge Xgress Tunnel Factory instance
func NewFactory(env env.RouterEnv, routerConfig *router.Config, stateManager state.Manager) *Factory {
factory := &Factory{
id: env.GetRouterId(),
routerConfig: routerConfig,
stateManager: stateManager,
metricsRegistry: env.GetMetricsRegistry(),
env: env,
}
factory.tunneler = newTunneler(factory, stateManager)
return factory
}
// CreateListener creates a new Edge Tunnel Xgress listener
func (self *Factory) CreateListener(optionsData xgress.OptionsData) (xgress.Listener, error) {
options := &Options{}
if err := options.load(optionsData); err != nil {
return nil, err
}
self.tunneler.listenOptions = options
pfxlog.Logger().Debugf("xgress edge tunnel options: %v", options.ToLoggableString())
return self.tunneler, nil
}
// CreateDialer creates a new Edge Xgress dialer
func (self *Factory) CreateDialer(optionsData xgress.OptionsData) (xgress.Dialer, error) {
options := &Options{}
if err := options.load(optionsData); err != nil {
return nil, err
}
self.tunneler.dialOptions = options
return self.tunneler, nil
}
type Options struct {
*xgress.Options
mode string
svcPollRate time.Duration
resolver string
dnsSvcIpRange string
lanIf string
services []string
udpIdleTimeout time.Duration
udpCheckInterval time.Duration
}
func (options *Options) load(data xgress.OptionsData) error {
options.mode = DefaultMode
options.svcPollRate = DefaultServicePollRate
options.resolver = DefaultDnsResolver
options.dnsSvcIpRange = DefaultDnsServiceIpRange
var err error
options.Options, err = xgress.LoadOptions(data)
if err != nil {
return err
}
if value, found := data["options"]; found {
data = value.(map[interface{}]interface{})
if value, found := data["svcPollRate"]; found {
if strVal, ok := value.(string); ok {
dur, err := time.ParseDuration(strVal)
if err != nil {
return errors.Wrapf(err, "invalid value '%v' for svcPollRate, must be string duration (ex: 1m or 30s)", value)
}
options.svcPollRate = dur
} else {
return errors.Errorf("invalid value '%v' for svcPollRate, must be string duration (ex: 1m or 30s)", value)
}
}
if value, found := data["resolver"]; found {
if strVal, ok := value.(string); ok {
options.resolver = strVal
} else {
return errors.Errorf("invalid value '%v' for resolver, must be string value", value)
}
}
if value, found := data["dnsSvcIpRange"]; found {
if strVal, ok := value.(string); ok {
options.dnsSvcIpRange = strVal
} else {
return errors.Errorf("invalid value '%v' for dnsSvcIpRange, must be string value", value)
}
}
if value, found := data["mode"]; found {
if strVal, ok := value.(string); ok && stringz.Contains([]string{"tproxy", "host", "proxy"}, strVal) ||
strings.HasPrefix(strVal, "tproxy:") {
options.mode = strVal
} else {
return errors.Errorf(`invalid value '%v' for mode, must be one of ["tproxy", "host", "proxy"']`, value)
}
}
if value, found := data["services"]; found {
if slice, ok := value.([]interface{}); ok {
for _, value := range slice {
if strVal, ok := value.(string); ok {
options.services = append(options.services, strVal)
} else {
return errors.Errorf(`invalid value '%v' for services, must be list of strings`, value)
}
}
} else {
return errors.New(`invalid value for services, must be list of strings']`)
}
}
if value, found := data["lanIf"]; found {
if strVal, ok := value.(string); ok {
options.lanIf = strVal
} else {
return errors.Errorf(`invalid value '%v' for lanIf, must be a string value`, value)
}
}
if value, found := data["udpIdleTimeout"]; found {
if strVal, ok := value.(string); ok {
dur, err := time.ParseDuration(strVal)
if err != nil {
return errors.Wrapf(err, "invalid value '%v' for udpIdleTimeout, must be string duration (ex: 1m or 30s)", value)
}
options.udpIdleTimeout = dur
} else {
return errors.Errorf("invalid value '%v' for udpIdleTimeout, must be string duration (ex: 1m or 30s)", value)
}
}
if value, found := data["udpCheckInterval"]; found {
if strVal, ok := value.(string); ok {
dur, err := time.ParseDuration(strVal)
if err != nil {
return errors.Wrapf(err, "invalid value '%v' for udpCheckInterval, must be string duration (ex: 1m or 30s)", value)
}
options.udpCheckInterval = dur
} else {
return errors.Errorf("invalid value '%v' for udpCheckInterval, must be string duration (ex: 1m or 30s)", value)
}
}
}
return nil
}
func (options *Options) ToLoggableString() string {
buf := strings.Builder{}
buf.WriteString(fmt.Sprintf("mtu=%v\n", options.Mtu))
buf.WriteString(fmt.Sprintf("randomDrops=%v\n", options.RandomDrops))
buf.WriteString(fmt.Sprintf("drop1InN=%v\n", options.Drop1InN))
buf.WriteString(fmt.Sprintf("txQueueSize=%v\n", options.TxQueueSize))
buf.WriteString(fmt.Sprintf("txPortalStartSize=%v\n", options.TxPortalStartSize))
buf.WriteString(fmt.Sprintf("txPortalMaxSize=%v\n", options.TxPortalMaxSize))
buf.WriteString(fmt.Sprintf("txPortalMinSize=%v\n", options.TxPortalMinSize))
buf.WriteString(fmt.Sprintf("txPortalIncreaseThresh=%v\n", options.TxPortalIncreaseThresh))
buf.WriteString(fmt.Sprintf("txPortalIncreaseScale=%v\n", options.TxPortalIncreaseScale))
buf.WriteString(fmt.Sprintf("txPortalRetxThresh=%v\n", options.TxPortalRetxThresh))
buf.WriteString(fmt.Sprintf("txPortalRetxScale=%v\n", options.TxPortalRetxScale))
buf.WriteString(fmt.Sprintf("txPortalDupAckThresh=%v\n", options.TxPortalDupAckThresh))
buf.WriteString(fmt.Sprintf("txPortalDupAckScale=%v\n", options.TxPortalDupAckScale))
buf.WriteString(fmt.Sprintf("rxBufferSize=%v\n", options.RxBufferSize))
buf.WriteString(fmt.Sprintf("retxStartMs=%v\n", options.RetxStartMs))
buf.WriteString(fmt.Sprintf("retxScale=%v\n", options.RetxScale))
buf.WriteString(fmt.Sprintf("retxAddMs=%v\n", options.RetxAddMs))
buf.WriteString(fmt.Sprintf("maxCloseWait=%v\n", options.MaxCloseWait))
buf.WriteString(fmt.Sprintf("getCircuitTimeout=%v\n", options.GetCircuitTimeout))
return buf.String()
}