|
| 1 | +// Copyright 2020-2024 Tetrate |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package main |
| 16 | + |
| 17 | +import ( |
| 18 | + "github.com/proxy-wasm/proxy-wasm-go-sdk/proxywasm" |
| 19 | + "github.com/proxy-wasm/proxy-wasm-go-sdk/proxywasm/types" |
| 20 | +) |
| 21 | + |
| 22 | +func main() {} |
| 23 | +func init() { |
| 24 | + proxywasm.SetVMContext(&vmContext{}) |
| 25 | +} |
| 26 | + |
| 27 | +// vmContext implements types.VMContext. |
| 28 | +type vmContext struct { |
| 29 | + // Embed the default VM context here, |
| 30 | + // so that we don't need to reimplement all the methods. |
| 31 | + types.DefaultVMContext |
| 32 | +} |
| 33 | + |
| 34 | +// NewPluginContext implements types.VMContext. |
| 35 | +func (*vmContext) NewPluginContext(contextID uint32) types.PluginContext { |
| 36 | + return &pluginContext{counter: proxywasm.DefineCounterMetric("proxy_wasm_go.connection_counter")} |
| 37 | +} |
| 38 | + |
| 39 | +// pluginContext implements types.PluginContext. |
| 40 | +type pluginContext struct { |
| 41 | + // Embed the default plugin context here, |
| 42 | + // so that we don't need to reimplement all the methods. |
| 43 | + types.DefaultPluginContext |
| 44 | + counter proxywasm.MetricCounter |
| 45 | +} |
| 46 | + |
| 47 | +// NewTcpContext implements types.PluginContext. |
| 48 | +func (ctx *pluginContext) NewTcpContext(contextID uint32) types.TcpContext { |
| 49 | + return &networkContext{counter: ctx.counter} |
| 50 | +} |
| 51 | + |
| 52 | +// networkContext implements types.TcpContext. |
| 53 | +type networkContext struct { |
| 54 | + // Embed the default tcp context here, |
| 55 | + // so that we don't need to reimplement all the methods. |
| 56 | + types.DefaultTcpContext |
| 57 | + counter proxywasm.MetricCounter |
| 58 | +} |
| 59 | + |
| 60 | +// OnNewConnection implements types.TcpContext. |
| 61 | +func (ctx *networkContext) OnNewConnection() types.Action { |
| 62 | + proxywasm.LogInfo("new connection!") |
| 63 | + return types.ActionContinue |
| 64 | +} |
| 65 | + |
| 66 | +// OnDownstreamData implements types.TcpContext. |
| 67 | +func (ctx *networkContext) OnDownstreamData(dataSize int, endOfStream bool) types.Action { |
| 68 | + if dataSize == 0 { |
| 69 | + return types.ActionContinue |
| 70 | + } |
| 71 | + |
| 72 | + data, err := proxywasm.GetDownstreamData(0, dataSize) |
| 73 | + if err != nil && err != types.ErrorStatusNotFound { |
| 74 | + proxywasm.LogCriticalf("failed to get downstream data: %v", err) |
| 75 | + return types.ActionContinue |
| 76 | + } |
| 77 | + |
| 78 | + proxywasm.LogInfof(">>>>>> downstream data received >>>>>>\n%s", string(data)) |
| 79 | + return types.ActionContinue |
| 80 | +} |
| 81 | + |
| 82 | +// OnDownstreamClose implements types.TcpContext. |
| 83 | +func (ctx *networkContext) OnDownstreamClose(types.PeerType) { |
| 84 | + proxywasm.LogInfo("downstream connection close!") |
| 85 | +} |
| 86 | + |
| 87 | +// OnUpstreamData implements types.TcpContext. |
| 88 | +func (ctx *networkContext) OnUpstreamData(dataSize int, endOfStream bool) types.Action { |
| 89 | + if dataSize == 0 { |
| 90 | + return types.ActionContinue |
| 91 | + } |
| 92 | + |
| 93 | + // Get the remote ip address of the upstream cluster. |
| 94 | + address, err := proxywasm.GetProperty([]string{"upstream", "address"}) |
| 95 | + if err != nil { |
| 96 | + proxywasm.LogWarnf("failed to get upstream remote address: %v", err) |
| 97 | + } |
| 98 | + |
| 99 | + proxywasm.LogInfof("remote address: %s", string(address)) |
| 100 | + |
| 101 | + // Get the upstream cluster's metadata in the cluster configuration. |
| 102 | + metadataKeyValues, err := proxywasm.GetPropertyMap([]string{"cluster_metadata", "filter_metadata", "location"}) |
| 103 | + if err != nil { |
| 104 | + proxywasm.LogWarnf("failed to get upstream location metadata: %v", err) |
| 105 | + } |
| 106 | + |
| 107 | + for _, metadata := range metadataKeyValues { |
| 108 | + key, value := metadata[0], metadata[1] |
| 109 | + proxywasm.LogInfof("upstream cluster metadata location[%s]=%s", string(key), string(value)) |
| 110 | + } |
| 111 | + |
| 112 | + data, err := proxywasm.GetUpstreamData(0, dataSize) |
| 113 | + if err != nil && err != types.ErrorStatusNotFound { |
| 114 | + proxywasm.LogCritical(err.Error()) |
| 115 | + } |
| 116 | + |
| 117 | + proxywasm.LogInfof("<<<<<< upstream data received <<<<<<\n%s", string(data)) |
| 118 | + return types.ActionContinue |
| 119 | +} |
| 120 | + |
| 121 | +// OnStreamDone implements types.TcpContext. |
| 122 | +func (ctx *networkContext) OnStreamDone() { |
| 123 | + ctx.counter.Increment(1) |
| 124 | + proxywasm.LogInfo("connection complete!") |
| 125 | +} |
0 commit comments