7
jsoniter "github.com/json-iterator/go"
8
v2 "mosn.io/mosn/pkg/config/v2"
9
"mosn.io/mosn/pkg/types"
12
var json = jsoniter.ConfigCompatibleWithStandardLibrary
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)
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),
31
routers := []v2.Router{
32
NewPrefixRouter(clusterName, "/"),
33
NewHeaderRouter(clusterName, ".*"),
35
chains := []v2.FilterChain{
36
NewFilterChain("proxyVirtualHost", proto, proto, routers),
38
listener := NewListener("proxyListener", addr, chains)
39
return NewMOSNConfig([]v2.Listener{listener}, cmconfig)
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)
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, ".*"),
63
var clientChains []v2.FilterChain
65
clientChains = []v2.FilterChain{
66
NewFilterChainWithSub("downstreamFilter", appproto, meshproto, subproto, downstreamRouters),
69
clientChains = []v2.FilterChain{
70
NewFilterChain("downstreamFilter", appproto, meshproto, downstreamRouters),
73
clientListener := NewListener("downstreamListener", clientaddr, clientChains)
74
upstreamRouters := []v2.Router{
75
NewPrefixRouter(upstreamCluster, "/"),
76
NewHeaderRouter(upstreamCluster, ".*"),
79
meshClusterConfig := NewBasicCluster(downstreamCluster, []string{serveraddr})
81
var meshServerChain v2.FilterChain
83
meshServerChain = NewFilterChainWithSub("upstreamFilter", meshproto, appproto, subproto, upstreamRouters)
85
meshServerChain = NewFilterChain("upstreamFilter", meshproto, appproto, upstreamRouters)
88
tlsConf := v2.TLSConfig{
92
PrivateKey: privatekey,
96
ServerName: "127.0.0.1",
98
meshClusterConfig.TLS = tlsConf
99
meshServerChain.TLSContexts = []v2.TLSConfig{
103
cmconfig := v2.ClusterManagerConfig{
104
Clusters: []v2.Cluster{
106
NewBasicCluster(upstreamCluster, hosts),
109
serverChains := []v2.FilterChain{meshServerChain}
110
serverListener := NewListener("upstreamListener", serveraddr, serverChains)
111
return NewMOSNConfig([]v2.Listener{
112
clientListener, serverListener,
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),
124
routers := []v2.Router{
125
NewPrefixRouter(clusterName, "/"),
126
NewHeaderRouter(clusterName, ".*"),
128
chains := []v2.FilterChain{
129
NewXProtocolFilterChain("proxyVirtualHost", proto, routers),
131
listener := NewListener("proxyListener", addr, chains)
132
return NewMOSNConfig([]v2.Listener{listener}, cmconfig)
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, ".*"),
144
clientChains := []v2.FilterChain{
145
NewXProtocolFilterChain("xprotocol_test_router_config_name", subProtocol, downstreamRouters),
147
clientListener := NewListener("downstreamListener", clientaddr, clientChains)
148
upstreamRouters := []v2.Router{
149
NewPrefixRouter(upstreamCluster, "/"),
150
NewHeaderRouter(upstreamCluster, ".*"),
152
meshClusterConfig := NewBasicCluster(downstreamCluster, []string{serveraddr})
153
meshServerChain := NewXProtocolFilterChain("upstreamFilter", subProtocol, upstreamRouters)
155
tlsConf := v2.TLSConfig{
158
CertChain: certchain,
159
PrivateKey: privatekey,
163
ServerName: "127.0.0.1",
165
meshClusterConfig.TLS = tlsConf
166
meshServerChain.TLSContexts = []v2.TLSConfig{
170
cmconfig := v2.ClusterManagerConfig{
171
Clusters: []v2.Cluster{
173
NewBasicCluster(upstreamCluster, hosts),
176
serverChains := []v2.FilterChain{meshServerChain}
177
serverListener := NewListener("upstreamListener", serveraddr, serverChains)
178
return NewMOSNConfig([]v2.Listener{
179
clientListener, serverListener,
184
type ExtendVerifyConfig struct {
186
VerifyConfig map[string]interface{}
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, ".*"),
196
clientChains := []v2.FilterChain{
197
NewFilterChain("downstreamFilter", appproto, meshproto, downstreamRouters),
199
clientListener := NewListener("downstreamListener", clientaddr, clientChains)
200
upstreamRouters := []v2.Router{
201
NewPrefixRouter(upstreamCluster, "/"),
202
NewHeaderRouter(upstreamCluster, ".*"),
204
tlsConf := v2.TLSConfig{
206
Type: ext.ExtendType,
208
ExtendVerify: ext.VerifyConfig,
210
meshClusterConfig := NewBasicCluster(downstreamCluster, []string{serveraddr})
211
meshClusterConfig.TLS = tlsConf
212
meshServerChain := NewFilterChain("upstreamFilter", meshproto, appproto, upstreamRouters)
213
meshServerChain.TLSContexts = []v2.TLSConfig{
216
cmconfig := v2.ClusterManagerConfig{
217
Clusters: []v2.Cluster{
219
NewBasicCluster(upstreamCluster, hosts),
222
serverChains := []v2.FilterChain{meshServerChain}
223
serverListener := NewListener("upstreamListener", serveraddr, serverChains)
224
return NewMOSNConfig([]v2.Listener{
225
clientListener, serverListener,
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, ".*"),
237
clientChains := []v2.FilterChain{
238
NewXProtocolFilterChain("downstreamFilter", subProtocol, downstreamRouters),
240
clientListener := NewListener("downstreamListener", clientaddr, clientChains)
241
upstreamRouters := []v2.Router{
242
NewPrefixRouter(upstreamCluster, "/"),
243
NewHeaderRouter(upstreamCluster, ".*"),
245
tlsConf := v2.TLSConfig{
247
Type: ext.ExtendType,
249
ExtendVerify: ext.VerifyConfig,
251
meshClusterConfig := NewBasicCluster(downstreamCluster, []string{serveraddr})
252
meshClusterConfig.TLS = tlsConf
253
meshServerChain := NewXProtocolFilterChain("upstreamFilter", subProtocol, upstreamRouters)
254
meshServerChain.TLSContexts = []v2.TLSConfig{
257
cmconfig := v2.ClusterManagerConfig{
258
Clusters: []v2.Cluster{
260
NewBasicCluster(upstreamCluster, hosts),
263
serverChains := []v2.FilterChain{meshServerChain}
264
serverListener := NewListener("upstreamListener", serveraddr, serverChains)
265
return NewMOSNConfig([]v2.Listener{
266
clientListener, serverListener,
272
func CreateTCPProxyConfig(meshaddr string, hosts []string, isRouteEntryMode bool) *v2.MOSNConfig {
273
clusterName := "cluster"
274
cluster := clusterName
275
if isRouteEntryMode {
278
tcpConfig := v2.StreamProxy{
280
Routes: []*v2.StreamRoute{
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",
290
chains := make(map[string]interface{})
291
b, _ := json.Marshal(tcpConfig)
292
json.Unmarshal(b, &chains)
293
filterChains := []v2.FilterChain{
295
FilterChainConfig: v2.FilterChainConfig{
296
Filters: []v2.Filter{
297
{Type: "tcp_proxy", Config: chains},
302
cmconfig := v2.ClusterManagerConfig{
303
Clusters: []v2.Cluster{
304
NewBasicCluster(clusterName, hosts),
307
listener := NewListener("listener", meshaddr, filterChains)
308
return NewMOSNConfig([]v2.Listener{
313
type WeightCluster struct {
318
type WeightHost struct {
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{
338
cmconfig := v2.ClusterManagerConfig{
339
Clusters: clusterConfigs,
341
routers := []v2.Router{
342
NewHeaderWeightedRouter(weightClusters, ".*"),
344
chains := []v2.FilterChain{
345
NewXProtocolFilterChain("proxyVirtualHost", proto, routers),
347
listener := NewListener("proxyListener", addr, chains)
349
return NewMOSNConfig([]v2.Listener{listener}, cmconfig)