mosn

Форк
0
/
config.go 
350 строк · 10.9 Кб
1
package util
2

3
import (
4
	"fmt"
5
	"sync/atomic"
6

7
	jsoniter "github.com/json-iterator/go"
8
	v2 "mosn.io/mosn/pkg/config/v2"
9
	"mosn.io/mosn/pkg/types"
10
)
11

12
var json = jsoniter.ConfigCompatibleWithStandardLibrary
13

14
// use different mesh port to avoid "port in used" error
15
var meshIndex uint32
16

17
func CurrentMeshAddr() string {
18
	var basic uint32 = 2044
19
	atomic.AddUint32(&meshIndex, 1)
20
	return fmt.Sprintf("127.0.0.1:%d", basic+meshIndex)
21
}
22

23
// mesh as a proxy , client and servre have same protocol
24
func CreateProxyMesh(addr string, hosts []string, proto types.ProtocolName) *v2.MOSNConfig {
25
	clusterName := "proxyCluster"
26
	cmconfig := v2.ClusterManagerConfig{
27
		Clusters: []v2.Cluster{
28
			NewBasicCluster(clusterName, hosts),
29
		},
30
	}
31
	routers := []v2.Router{
32
		NewPrefixRouter(clusterName, "/"),
33
		NewHeaderRouter(clusterName, ".*"),
34
	}
35
	chains := []v2.FilterChain{
36
		NewFilterChain("proxyVirtualHost", proto, proto, routers),
37
	}
38
	listener := NewListener("proxyListener", addr, chains)
39
	return NewMOSNConfig([]v2.Listener{listener}, cmconfig)
40
}
41

42
// Mesh to Mesh
43
// clientaddr and serveraddr is mesh's addr
44
// appproto is client and server (not mesh) protocol
45
// meshproto is mesh's protocol
46
// hosts is server's addresses
47
func CreateMeshToMeshConfig(clientaddr string, serveraddr string, appproto types.ProtocolName, meshproto types.ProtocolName, hosts []string, tls bool) *v2.MOSNConfig {
48
	return CreateMeshToMeshConfigWithSub(clientaddr, serveraddr, appproto, meshproto, "", hosts, tls)
49
}
50

51
// Mesh to Mesh
52
// clientaddr and serveraddr is mesh's addr
53
// appproto is client and server (not mesh) protocol
54
// meshproto is mesh's protocol
55
// hosts is server's addresses
56
func CreateMeshToMeshConfigWithSub(clientaddr string, serveraddr string, appproto types.ProtocolName, meshproto types.ProtocolName, subproto types.ProtocolName, hosts []string, tls bool) *v2.MOSNConfig {
57
	downstreamCluster := "downstream"
58
	upstreamCluster := "upstream"
59
	downstreamRouters := []v2.Router{
60
		NewPrefixRouter(downstreamCluster, "/"),
61
		NewHeaderRouter(downstreamCluster, ".*"),
62
	}
63
	var clientChains []v2.FilterChain
64
	if subproto != "" {
65
		clientChains = []v2.FilterChain{
66
			NewFilterChainWithSub("downstreamFilter", appproto, meshproto, subproto, downstreamRouters),
67
		}
68
	} else {
69
		clientChains = []v2.FilterChain{
70
			NewFilterChain("downstreamFilter", appproto, meshproto, downstreamRouters),
71
		}
72
	}
73
	clientListener := NewListener("downstreamListener", clientaddr, clientChains)
74
	upstreamRouters := []v2.Router{
75
		NewPrefixRouter(upstreamCluster, "/"),
76
		NewHeaderRouter(upstreamCluster, ".*"),
77
	}
78
	// client mesh -> cluster need tls
79
	meshClusterConfig := NewBasicCluster(downstreamCluster, []string{serveraddr})
80
	//  server mesh listener need tls
81
	var meshServerChain v2.FilterChain
82
	if subproto != "" {
83
		meshServerChain = NewFilterChainWithSub("upstreamFilter", meshproto, appproto, subproto, upstreamRouters)
84
	} else {
85
		meshServerChain = NewFilterChain("upstreamFilter", meshproto, appproto, upstreamRouters)
86
	}
87
	if tls {
88
		tlsConf := v2.TLSConfig{
89
			Status:       true,
90
			CACert:       cacert,
91
			CertChain:    certchain,
92
			PrivateKey:   privatekey,
93
			EcdhCurves:   "P256",
94
			VerifyClient: true,
95
			//CipherSuites: "ECDHE-RSA-SM4-SM3:ECDHE-ECDSA-SM4-SM3",
96
			ServerName: "127.0.0.1",
97
		}
98
		meshClusterConfig.TLS = tlsConf
99
		meshServerChain.TLSContexts = []v2.TLSConfig{
100
			tlsConf,
101
		}
102
	}
103
	cmconfig := v2.ClusterManagerConfig{
104
		Clusters: []v2.Cluster{
105
			meshClusterConfig,
106
			NewBasicCluster(upstreamCluster, hosts),
107
		},
108
	}
109
	serverChains := []v2.FilterChain{meshServerChain}
110
	serverListener := NewListener("upstreamListener", serveraddr, serverChains)
111
	return NewMOSNConfig([]v2.Listener{
112
		clientListener, serverListener,
113
	}, cmconfig)
114
}
115

116
// XProtocol mesh as a proxy , client and servre have same protocol
117
func CreateXProtocolProxyMesh(addr string, hosts []string, proto types.ProtocolName) *v2.MOSNConfig {
118
	clusterName := "proxyCluster"
119
	cmconfig := v2.ClusterManagerConfig{
120
		Clusters: []v2.Cluster{
121
			NewBasicCluster(clusterName, hosts),
122
		},
123
	}
124
	routers := []v2.Router{
125
		NewPrefixRouter(clusterName, "/"),
126
		NewHeaderRouter(clusterName, ".*"),
127
	}
128
	chains := []v2.FilterChain{
129
		NewXProtocolFilterChain("proxyVirtualHost", proto, routers),
130
	}
131
	listener := NewListener("proxyListener", addr, chains)
132
	return NewMOSNConfig([]v2.Listener{listener}, cmconfig)
133
}
134

135
// XProtocol mesh to mesh
136
// currently, support Path/Prefix is "/" only
137
func CreateXProtocolMesh(clientaddr string, serveraddr string, subProtocol types.ProtocolName, hosts []string, tls bool) *v2.MOSNConfig {
138
	downstreamCluster := "downstream"
139
	upstreamCluster := "upstream"
140
	downstreamRouters := []v2.Router{
141
		NewPrefixRouter(downstreamCluster, "/"),
142
		NewHeaderRouter(downstreamCluster, ".*"),
143
	}
144
	clientChains := []v2.FilterChain{
145
		NewXProtocolFilterChain("xprotocol_test_router_config_name", subProtocol, downstreamRouters),
146
	}
147
	clientListener := NewListener("downstreamListener", clientaddr, clientChains)
148
	upstreamRouters := []v2.Router{
149
		NewPrefixRouter(upstreamCluster, "/"),
150
		NewHeaderRouter(upstreamCluster, ".*"),
151
	}
152
	meshClusterConfig := NewBasicCluster(downstreamCluster, []string{serveraddr})
153
	meshServerChain := NewXProtocolFilterChain("upstreamFilter", subProtocol, upstreamRouters)
154
	if tls {
155
		tlsConf := v2.TLSConfig{
156
			Status:       true,
157
			CACert:       cacert,
158
			CertChain:    certchain,
159
			PrivateKey:   privatekey,
160
			EcdhCurves:   "P256",
161
			VerifyClient: true,
162
			//CipherSuites: "ECDHE-RSA-SM4-SM3:ECDHE-ECDSA-SM4-SM3",
163
			ServerName: "127.0.0.1",
164
		}
165
		meshClusterConfig.TLS = tlsConf
166
		meshServerChain.TLSContexts = []v2.TLSConfig{
167
			tlsConf,
168
		}
169
	}
170
	cmconfig := v2.ClusterManagerConfig{
171
		Clusters: []v2.Cluster{
172
			meshClusterConfig,
173
			NewBasicCluster(upstreamCluster, hosts),
174
		},
175
	}
176
	serverChains := []v2.FilterChain{meshServerChain}
177
	serverListener := NewListener("upstreamListener", serveraddr, serverChains)
178
	return NewMOSNConfig([]v2.Listener{
179
		clientListener, serverListener,
180
	}, cmconfig)
181
}
182

183
// TLS Extension
184
type ExtendVerifyConfig struct {
185
	ExtendType   string
186
	VerifyConfig map[string]interface{}
187
}
188

189
func CreateTLSExtensionConfig(clientaddr string, serveraddr string, appproto types.ProtocolName, meshproto types.ProtocolName, hosts []string, ext *ExtendVerifyConfig) *v2.MOSNConfig {
190
	downstreamCluster := "downstream"
191
	upstreamCluster := "upstream"
192
	downstreamRouters := []v2.Router{
193
		NewPrefixRouter(downstreamCluster, "/"),
194
		NewHeaderRouter(downstreamCluster, ".*"),
195
	}
196
	clientChains := []v2.FilterChain{
197
		NewFilterChain("downstreamFilter", appproto, meshproto, downstreamRouters),
198
	}
199
	clientListener := NewListener("downstreamListener", clientaddr, clientChains)
200
	upstreamRouters := []v2.Router{
201
		NewPrefixRouter(upstreamCluster, "/"),
202
		NewHeaderRouter(upstreamCluster, ".*"),
203
	}
204
	tlsConf := v2.TLSConfig{
205
		Status:       true,
206
		Type:         ext.ExtendType,
207
		VerifyClient: true,
208
		ExtendVerify: ext.VerifyConfig,
209
	}
210
	meshClusterConfig := NewBasicCluster(downstreamCluster, []string{serveraddr})
211
	meshClusterConfig.TLS = tlsConf
212
	meshServerChain := NewFilterChain("upstreamFilter", meshproto, appproto, upstreamRouters)
213
	meshServerChain.TLSContexts = []v2.TLSConfig{
214
		tlsConf,
215
	}
216
	cmconfig := v2.ClusterManagerConfig{
217
		Clusters: []v2.Cluster{
218
			meshClusterConfig,
219
			NewBasicCluster(upstreamCluster, hosts),
220
		},
221
	}
222
	serverChains := []v2.FilterChain{meshServerChain}
223
	serverListener := NewListener("upstreamListener", serveraddr, serverChains)
224
	return NewMOSNConfig([]v2.Listener{
225
		clientListener, serverListener,
226
	}, cmconfig)
227

228
}
229

230
func CreateXprotocolTLSExtensionConfig(clientaddr string, serveraddr string, subProtocol types.ProtocolName, hosts []string, ext *ExtendVerifyConfig) *v2.MOSNConfig {
231
	downstreamCluster := "downstream"
232
	upstreamCluster := "upstream"
233
	downstreamRouters := []v2.Router{
234
		NewPrefixRouter(downstreamCluster, "/"),
235
		NewHeaderRouter(downstreamCluster, ".*"),
236
	}
237
	clientChains := []v2.FilterChain{
238
		NewXProtocolFilterChain("downstreamFilter", subProtocol, downstreamRouters),
239
	}
240
	clientListener := NewListener("downstreamListener", clientaddr, clientChains)
241
	upstreamRouters := []v2.Router{
242
		NewPrefixRouter(upstreamCluster, "/"),
243
		NewHeaderRouter(upstreamCluster, ".*"),
244
	}
245
	tlsConf := v2.TLSConfig{
246
		Status:       true,
247
		Type:         ext.ExtendType,
248
		VerifyClient: true,
249
		ExtendVerify: ext.VerifyConfig,
250
	}
251
	meshClusterConfig := NewBasicCluster(downstreamCluster, []string{serveraddr})
252
	meshClusterConfig.TLS = tlsConf
253
	meshServerChain := NewXProtocolFilterChain("upstreamFilter", subProtocol, upstreamRouters)
254
	meshServerChain.TLSContexts = []v2.TLSConfig{
255
		tlsConf,
256
	}
257
	cmconfig := v2.ClusterManagerConfig{
258
		Clusters: []v2.Cluster{
259
			meshClusterConfig,
260
			NewBasicCluster(upstreamCluster, hosts),
261
		},
262
	}
263
	serverChains := []v2.FilterChain{meshServerChain}
264
	serverListener := NewListener("upstreamListener", serveraddr, serverChains)
265
	return NewMOSNConfig([]v2.Listener{
266
		clientListener, serverListener,
267
	}, cmconfig)
268

269
}
270

271
// TCP Proxy
272
func CreateTCPProxyConfig(meshaddr string, hosts []string, isRouteEntryMode bool) *v2.MOSNConfig {
273
	clusterName := "cluster"
274
	cluster := clusterName
275
	if isRouteEntryMode {
276
		cluster = ""
277
	}
278
	tcpConfig := v2.StreamProxy{
279
		Cluster: cluster,
280
		Routes: []*v2.StreamRoute{
281
			&v2.StreamRoute{
282
				Cluster:          "cluster",
283
				SourceAddrs:      []v2.CidrRange{v2.CidrRange{Address: "127.0.0.1", Length: 24}},
284
				DestinationAddrs: []v2.CidrRange{v2.CidrRange{Address: "127.0.0.1", Length: 24}},
285
				SourcePort:       "1-65535",
286
				DestinationPort:  "1-65535",
287
			},
288
		},
289
	}
290
	chains := make(map[string]interface{})
291
	b, _ := json.Marshal(tcpConfig)
292
	json.Unmarshal(b, &chains)
293
	filterChains := []v2.FilterChain{
294
		{
295
			FilterChainConfig: v2.FilterChainConfig{
296
				Filters: []v2.Filter{
297
					{Type: "tcp_proxy", Config: chains},
298
				},
299
			},
300
		},
301
	}
302
	cmconfig := v2.ClusterManagerConfig{
303
		Clusters: []v2.Cluster{
304
			NewBasicCluster(clusterName, hosts),
305
		},
306
	}
307
	listener := NewListener("listener", meshaddr, filterChains)
308
	return NewMOSNConfig([]v2.Listener{
309
		listener,
310
	}, cmconfig)
311
}
312

313
type WeightCluster struct {
314
	Name   string
315
	Hosts  []*WeightHost
316
	Weight uint32
317
}
318
type WeightHost struct {
319
	Addr   string
320
	Weight uint32
321
}
322

323
// mesh as a proxy , client and servre have same protocol
324
func CreateXWeightProxyMesh(addr string, proto types.ProtocolName, clusters []*WeightCluster) *v2.MOSNConfig {
325
	var clusterConfigs []v2.Cluster
326
	var weightClusters []v2.WeightedCluster
327
	for _, c := range clusters {
328
		clusterConfigs = append(clusterConfigs, NewWeightedCluster(c.Name, c.Hosts))
329
		weightClusters = append(weightClusters, v2.WeightedCluster{
330
			Cluster: v2.ClusterWeight{
331
				ClusterWeightConfig: v2.ClusterWeightConfig{
332
					Name:   c.Name,
333
					Weight: c.Weight,
334
				},
335
			},
336
		})
337
	}
338
	cmconfig := v2.ClusterManagerConfig{
339
		Clusters: clusterConfigs,
340
	}
341
	routers := []v2.Router{
342
		NewHeaderWeightedRouter(weightClusters, ".*"),
343
	}
344
	chains := []v2.FilterChain{
345
		NewXProtocolFilterChain("proxyVirtualHost", proto, routers),
346
	}
347
	listener := NewListener("proxyListener", addr, chains)
348

349
	return NewMOSNConfig([]v2.Listener{listener}, cmconfig)
350
}
351

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.