Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,7 @@ Any comments, questions, issues reports and PRs are welcome.
Please open issues/PRs or reach me [@mathetake](https://twitter.com/mathetake).

## TODOs
- docs
- support get/set shared queue
- support get/set shared data
- support get/set property
- support metrics
- support gRPC
- enhance error handling


## references

Expand Down
23 changes: 20 additions & 3 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@

Theses are the proxy-wasm-go reimplementation of examples in https://github.com/proxy-wasm/proxy-wasm-rust-sdk/tree/master/examples.

Before you try an example, you must install tinygo(0.12.0 or above) for compiling your programs to wasm binary.
Please follow the official instruction [here](https://tinygo.org/getting-started/macos/).
## requirements

Currently they are tested against `docker.io/istio/proxyv2:1.5.0` image
- TinyGo(0.14.0+): https://tinygo.org/
- GetEnvoy: https://www.getenvoy.io/install/

To download compatible envoyproxy, run
```bash
getenvoy fetch wasm:1.15
```

## build

```bash
tinygo build -o ./${example}/wasm.wasm -wasm-abi=generic -target wasm ./${example}/main.go
```

## run

```bash
getenvoy run wasm:1.15 -- -c ./${example}/envoy.yaml
```
8 changes: 0 additions & 8 deletions examples/helloworld/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@
this example handles http request/response headers events and log all headers.


### build && run
```
tinygo build -o wasm.wasm -wasm-abi=generic -target wasm ./main.go && docker-compose up | grep OnTick
```

now you can see something like:


```bash
proxy_1 | [2020-03-25 09:04:56.149][1][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:1077] wasm log my_root_id: OnTick on 1, it's 1585127096149656000
proxy_1 | [2020-03-25 09:04:56.154][1][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:1077] wasm log my_root_id: OnTick on 1, it's 1585127096154901000
Expand Down
12 changes: 0 additions & 12 deletions examples/helloworld/docker-compose.yml

This file was deleted.

6 changes: 3 additions & 3 deletions examples/helloworld/envoy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ static_resources:
- name: main
address:
socket_address:
address: 0.0.0.0
port_value: 80
address: 127.0.0.1
port_value: 1234
filter_chains:
- filters:
- name: envoy.http_connection_manager
Expand Down Expand Up @@ -33,7 +33,7 @@ static_resources:
runtime: "envoy.wasm.runtime.v8"
code:
local:
filename: "/etc/envoy_filter_http_wasm_example.wasm"
filename: "./helloworld/wasm.wasm"
allow_precompiled: true
- name: envoy.router
config: {}
Expand Down
Binary file removed examples/helloworld/wasm.wasm
Binary file not shown.
10 changes: 1 addition & 9 deletions examples/http_auth_random/README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
## http_auth_random

this example authorize requests depending
on the hash values of a response from http://httpbin.org/uuid.

### build && run
```
tinygo build -o wasm.wasm -wasm-abi=generic -target wasm ./main.go && docker-compose up
```

now you can make requests authorized randomly:
this example authorize requests depending on the hash values of a response from http://httpbin.org/uuid.

```bash
$ curl localhost:18000/uuid -v
Expand Down
12 changes: 0 additions & 12 deletions examples/http_auth_random/docker-compose.yml

This file was deleted.

6 changes: 3 additions & 3 deletions examples/http_auth_random/envoy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ static_resources:
- name: main
address:
socket_address:
address: 0.0.0.0
port_value: 80
address: 127.0.0.1
port_value: 18000
filter_chains:
- filters:
- name: envoy.http_connection_manager
Expand Down Expand Up @@ -33,7 +33,7 @@ static_resources:
runtime: "envoy.wasm.runtime.v8"
code:
local:
filename: "/etc/envoy_filter_http_wasm_example.wasm"
filename: "./http_auth_random/wasm.wasm"
allow_precompiled: true
- name: envoy.router
config: {}
Expand Down
2 changes: 1 addition & 1 deletion examples/http_auth_random/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func newContext(contextID uint32) runtime.HttpContext {
}

// override default
func (ctx *httpHeaders) OnHttpRequestHeaders(_ int) runtime.Action {
func (ctx *httpHeaders) OnHttpRequestHeaders(_ int, _ bool) runtime.Action {
hs, st := ctx.GetHttpRequestHeaders()
if st != runtime.StatusOk {
runtime.LogCritical("failed to get request headers")
Expand Down
Binary file removed examples/http_auth_random/wasm.wasm
Binary file not shown.
8 changes: 0 additions & 8 deletions examples/http_headers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@
this example handles http request/response headers events and log all headers.


### build && run
```
tinygo build -o wasm.wasm -wasm-abi=generic -target wasm ./main.go && docker-compose up | grep header:
```

now you can send requests and see headers logged:


```bash
proxy_1 | [2020-03-25 09:09:24.937][16][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:1077] wasm log my_plugin my_root_id: request header: :authority: localhost:18000
proxy_1 | [2020-03-25 09:09:24.937][16][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:1077] wasm log my_plugin my_root_id: request header: :path: /
Expand Down
12 changes: 0 additions & 12 deletions examples/http_headers/docker-compose.yml

This file was deleted.

6 changes: 3 additions & 3 deletions examples/http_headers/envoy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ static_resources:
- name: main
address:
socket_address:
address: 0.0.0.0
port_value: 80
address: 127.0.0.1
port_value: 18000
filter_chains:
- filters:
- name: envoy.http_connection_manager
Expand Down Expand Up @@ -33,7 +33,7 @@ static_resources:
runtime: "envoy.wasm.runtime.v8"
code:
local:
filename: "/etc/envoy_filter_http_wasm_example.wasm"
filename: "./http_headers/wasm.wasm"
allow_precompiled: true
- name: envoy.router
config: {}
Expand Down
4 changes: 2 additions & 2 deletions examples/http_headers/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func newContext(contextID uint32) runtime.HttpContext {
}

// override
func (ctx *httpHeaders) OnHttpRequestHeaders(_ int) runtime.Action {
func (ctx *httpHeaders) OnHttpRequestHeaders(_ int, _ bool) runtime.Action {
hs, st := ctx.GetHttpRequestHeaders()
if st != runtime.StatusOk {
runtime.LogCritical("failed to get request headers")
Expand All @@ -34,7 +34,7 @@ func (ctx *httpHeaders) OnHttpRequestHeaders(_ int) runtime.Action {
}

// override
func (ctx *httpHeaders) OnHttpResponseHeaders(_ int) runtime.Action {
func (ctx *httpHeaders) OnHttpResponseHeaders(_ int, _ bool) runtime.Action {
hs, st := ctx.GetHttpResponseHeaders()
if st != runtime.StatusOk {
runtime.LogCritical("failed to get request headers")
Expand Down
Binary file removed examples/http_headers/wasm.wasm
Binary file not shown.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/mathetake/proxy-wasm-go

go 1.14
2 changes: 1 addition & 1 deletion runtime/abi.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@

package runtime

//go:export proxy_abi_version_0_1_0
//export proxy_abi_version_0_1_0
func proxyABIVersion010() {}
2 changes: 1 addition & 1 deletion runtime/abi_alloc.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

package runtime

//go:export malloc
//export malloc
func malloc(size uint) *byte {
buf := make([]byte, size)
return &buf[0]
Expand Down
4 changes: 2 additions & 2 deletions runtime/abi_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

package runtime

//go:export proxy_on_vm_start
//export proxy_on_vm_start
func proxyOnVMStart(rootContextID uint32, vmConfigurationSize int) bool {
ctx, ok := currentState.rootContexts[rootContextID]
if !ok {
Expand All @@ -24,7 +24,7 @@ func proxyOnVMStart(rootContextID uint32, vmConfigurationSize int) bool {
return ctx.OnVMStart(vmConfigurationSize)
}

//go:export proxy_on_configure
//export proxy_on_configure
func proxyOnConfigure(rootContextID uint32, pluginConfigurationSize int) bool {
ctx, ok := currentState.rootContexts[rootContextID]
if !ok {
Expand Down
62 changes: 28 additions & 34 deletions runtime/abi_hostcalls.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,83 +14,77 @@

package runtime

//go:export proxy_log
//export proxy_log
func proxyLog(logLevel LogLevel, messageData *byte, messageSize int) Status

//go:export proxy_get_configuration
func proxyGetConfiguration(returnBufferData **byte, returnBufferSize *int) Status

//go:export proxy_set_property
//export proxy_set_property
func proxySetProperty(pathData *byte, pathSize int, valueData *byte, valueSize int)

//go:export proxy_get_property
//export proxy_get_property
func proxyGetProperty(pathData *byte, pathSize int, returnValueData **byte, returnValueSize *int)

//go:export proxy_continue_request
func proxyContinueRequest() Status

//go:export proxy_continue_response
func proxyContinueResponse() Status

//go:export proxy_send_local_response
//export proxy_send_local_response
func proxySendLocalResponse(statusCode uint32, statusCodeDetailData *byte, statusCodeDetailsSize int,
bodyData *byte, bodySize int, headersData *byte, headersSize int, grpcStatus int32) Status

//go:export proxy_clear_route_cache
func proxyClearRouteCache() Status

//go:export proxy_get_shared_data
//export proxy_get_shared_data
func proxyGetSharedData(keyData *byte, keySize int, returnValueData **byte, returnValueSize *byte, returnCas *uint32) Status

//go:export proxy_set_shared_data
//export proxy_set_shared_data
func proxySetSharedData(keyData *byte, keySize int, valueData *byte, valueSize int, cas uint32) Status

//go:export proxy_register_shared_queue
//export proxy_register_shared_queue
func proxyRegisterSharedQueue(nameData *byte, nameSize uint, returnID *uint32) Status

//go:export proxy_resolve_shared_queue
//export proxy_resolve_shared_queue
func proxyResolveSharedQueue(vmIDData *byte, vmIDSize uint, nameData *byte, nameSize uint, returnID *uint32) Status

//go:export proxy_dequeue_shared_queue
//export proxy_dequeue_shared_queue
func proxyDequeueSharedQueue(queueID uint32, returnValueData **byte, returnValueSize *byte) Status

//go:export proxy_enqueue_shared_queue
//export proxy_enqueue_shared_queue
func proxyEnqueueSharedQueue(queueID uint32, valueData *byte, valueSize uint) Status

//go:export proxy_get_header_map_value
//export proxy_get_header_map_value
func proxyGetHeaderMapValue(mapType MapType, keyData *byte, keySize int, returnValueData **byte, returnValueSize *int) Status

//go:export proxy_add_header_map_value
//export proxy_add_header_map_value
func proxyAddHeaderMapValue(mapType MapType, keyData *byte, keySize int, valueData *byte, valueSize int) Status

//go:export proxy_replace_header_map_value
//export proxy_replace_header_map_value
func proxyReplaceHeaderMapValue(mapType MapType, keyData *byte, keySize int, valueData *byte, valueSize int) Status

//go:export proxy_remove_header_map_value
//export proxy_continue_stream
func proxyContinueStream(streamType StreamType) Status

//export proxy_close_stream
func proxyCloseStream(streamType StreamType) Status

//export proxy_remove_header_map_value
func proxyRemoveHeaderMapValue(mapType MapType, keyData *byte, keySize int) Status

//go:export proxy_get_header_map_pairs
//export proxy_get_header_map_pairs
func proxyGetHeaderMapPairs(mapType MapType, returnValueData **byte, returnValueSize *int) Status

//go:export proxy_set_header_map_pairs
//export proxy_set_header_map_pairs
func proxySetHeaderMapPairs(mapType MapType, mapData *byte, mapSize int) Status

//go:export proxy_get_buffer_bytes
//export proxy_get_buffer_bytes
func proxyGetBufferBytes(bt BufferType, start int, maxSize int, returnBufferData **byte, returnBufferSize *int) Status

//go:export proxy_http_call
//export proxy_http_call
func proxyHttpCall(upstreamData *byte, upstreamSize int, headerData *byte, headerSize int,
bodyData *byte, bodySize int, trailersData *byte, trailersSize int, timeout uint32, calloutIDPtr *uint32,
) Status

//go:export proxy_set_tick_period_milliseconds
//export proxy_set_tick_period_milliseconds
func proxySetTickPeriodMilliseconds(period uint32) Status

//go:export proxy_get_current_time_nanoseconds
//export proxy_get_current_time_nanoseconds
func proxyGetCurrentTimeNanoseconds(returnTime *int64) Status

//go:export proxy_set_effective_context
//export proxy_set_effective_context
func proxySetEffectiveContext(contextID uint32) Status

//go:export proxy_done
//export proxy_done
func proxyDone() Status
Loading