mosn

Форк
0
95 строк · 3.2 Кб
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 * contributor license agreements.  See the NOTICE file distributed with
4
 * this work for additional information regarding copyright ownership.
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 * (the "License"); you may not use this file except in compliance with
7
 * the License.  You may obtain a copy of the License at
8
 *
9
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17

18
package main
19

20
import (
21
	"context"
22
	"encoding/json"
23

24
	"mosn.io/api"
25
	"mosn.io/pkg/buffer"
26
	"mosn.io/pkg/log"
27
)
28

29
// define a function named: CreateFilterFactory, do not need init to register
30
func CreateFilterFactory(conf map[string]interface{}) (api.StreamFilterChainFactory, error) {
31
	log.DefaultLogger.Infof("create factory success")
32
	b, _ := json.Marshal(conf)
33
	m := make(map[string]string)
34
	if err := json.Unmarshal(b, &m); err != nil {
35
		return nil, err
36
	}
37
	return &DemoFactory{
38
		config: m,
39
	}, nil
40
}
41

42
// An implementation of api.StreamFilterChainFactory
43
type DemoFactory struct {
44
	config map[string]string
45
}
46

47
func (f *DemoFactory) CreateFilterChain(ctx context.Context, callbacks api.StreamFilterChainFactoryCallbacks) {
48
	filter := NewDemoFilter(ctx, f.config)
49
	// ReceiverFilter, run the filter when receive a request from downstream
50
	// The FilterPhase can be BeforeRoute or AfterRoute, we use BeforeRoute in this demo
51
	callbacks.AddStreamReceiverFilter(filter, api.BeforeRoute)
52
	// SenderFilter, run the filter when receive a response from upstream
53
	// In the demo, we are not implement this filter type
54
	// callbacks.AddStreamSenderFilter(filter, api.BeforeSend)
55
}
56

57
// What DemoFilter do:
58
// the request will be passed only if the request headers contains key&value matched in the config
59
type DemoFilter struct {
60
	config  map[string]string
61
	handler api.StreamReceiverFilterHandler
62
}
63

64
// NewDemoFilter returns a DemoFilter, the DemoFilter is an implementation of api.StreamReceiverFilter
65
// A Filter can implement both api.StreamReceiverFilter and api.StreamSenderFilter.
66
func NewDemoFilter(ctx context.Context, config map[string]string) *DemoFilter {
67
	return &DemoFilter{
68
		config: config,
69
	}
70
}
71

72
func (f *DemoFilter) OnReceive(ctx context.Context, headers api.HeaderMap, buf buffer.IoBuffer, trailers api.HeaderMap) api.StreamFilterStatus {
73
	log.DefaultContextLogger.Infof(ctx, "receive a request into demo filter")
74
	passed := true
75
CHECK:
76
	for k, v := range f.config {
77
		value, ok := headers.Get(k)
78
		if !ok || value != v {
79
			passed = false
80
			break CHECK
81
		}
82
	}
83
	if !passed {
84
		log.DefaultContextLogger.Warnf(ctx, "request does not matched the pass condition")
85
		f.handler.SendHijackReply(403, headers)
86
		return api.StreamFilterStop
87
	}
88
	return api.StreamFilterContinue
89
}
90

91
func (f *DemoFilter) SetReceiveFilterHandler(handler api.StreamReceiverFilterHandler) {
92
	f.handler = handler
93
}
94

95
func (f *DemoFilter) OnDestroy() {}
96

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

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

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

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