Some checks failed
Go / build (.exe, 386, windows, windows-386) (push) Has been cancelled
Go / build (.exe, amd64, windows, windows-amd64) (push) Has been cancelled
Go / build (.exe, arm64, windows, windows-arm64) (push) Has been cancelled
Go / build (386, freebsd, freebsd-386) (push) Has been cancelled
Go / build (386, linux, linux-386) (push) Has been cancelled
Go / build (386, netbsd, netbsd-386) (push) Has been cancelled
Go / build (386, openbsd, openbsd-386) (push) Has been cancelled
Go / build (386, plan9, plan9-386) (push) Has been cancelled
Go / build (amd64, darwin, darwin-amd64) (push) Has been cancelled
Go / build (amd64, dragonfly, dragonfly-amd64) (push) Has been cancelled
Go / build (amd64, freebsd, freebsd-amd64) (push) Has been cancelled
Go / build (amd64, illumos, illumos-amd64) (push) Has been cancelled
Go / build (amd64, linux, linux-amd64) (push) Has been cancelled
Go / build (amd64, netbsd, netbsd-amd64) (push) Has been cancelled
Go / build (amd64, openbsd, openbsd-amd64) (push) Has been cancelled
Go / build (amd64, plan9, plan9-amd64) (push) Has been cancelled
Go / build (amd64, solaris, solaris-amd64) (push) Has been cancelled
Go / build (arm, 6, linux, linux-armv6) (push) Has been cancelled
Go / build (arm, 7, linux, linux-armv7) (push) Has been cancelled
Go / build (arm, freebsd, freebsd-arm) (push) Has been cancelled
Go / build (arm, netbsd, netbsd-arm) (push) Has been cancelled
Go / build (arm, openbsd, openbsd-arm) (push) Has been cancelled
Go / build (arm, plan9, plan9-arm) (push) Has been cancelled
Go / build (arm64, darwin, darwin-arm64) (push) Has been cancelled
Go / build (arm64, freebsd, freebsd-arm64) (push) Has been cancelled
Go / build (arm64, linux, linux-arm64) (push) Has been cancelled
Go / build (arm64, netbsd, netbsd-arm64) (push) Has been cancelled
Go / build (arm64, openbsd, openbsd-arm64) (push) Has been cancelled
Go / build (loong64, linux, linux-loong64) (push) Has been cancelled
Go / build (mips, linux, linux-mips) (push) Has been cancelled
Go / build (mips64, linux, linux-mips64) (push) Has been cancelled
Go / build (mips64le, linux, linux-mips64le) (push) Has been cancelled
Go / build (mipsle, linux, linux-mipsle) (push) Has been cancelled
Go / build (ppc64, aix, aix-ppc64) (push) Has been cancelled
Go / build (ppc64, linux, linux-ppc64) (push) Has been cancelled
Go / build (ppc64, openbsd, openbsd-ppc64) (push) Has been cancelled
Go / build (ppc64le, linux, linux-ppc64le) (push) Has been cancelled
Go / build (riscv64, freebsd, freebsd-riscv64) (push) Has been cancelled
Go / build (riscv64, linux, linux-riscv64) (push) Has been cancelled
Go / build (riscv64, openbsd, openbsd-riscv64) (push) Has been cancelled
Go / build (s390x, linux, linux-s390x) (push) Has been cancelled
Go / merge-artifacts (push) Has been cancelled
Docker Image / docker (push) Has been cancelled
100 lines
2.4 KiB
Go
100 lines
2.4 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net"
|
|
"runtime"
|
|
|
|
"github.com/tursom/mc-gateway/plugin/api"
|
|
)
|
|
|
|
type PluginImpl struct {
|
|
api.AbstractPlugin
|
|
config Config
|
|
}
|
|
|
|
type Config struct {
|
|
MatchHost string `json:"match_host"`
|
|
Upstream string `json:"upstream"`
|
|
}
|
|
|
|
func Plugin() api.Plugin {
|
|
return &PluginImpl{}
|
|
}
|
|
|
|
func MCGatewayPluginMetadata() string {
|
|
return manifestJSON
|
|
}
|
|
|
|
func (p *PluginImpl) NewConfigObj() any {
|
|
return &Config{}
|
|
}
|
|
|
|
func (p *PluginImpl) ReloadConfig(config any) error {
|
|
if cfg, ok := config.(*Config); ok {
|
|
p.config = *cfg
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (p *PluginImpl) Init(gateway api.Gateway) error {
|
|
return api.RegisterHookHandler(
|
|
gateway,
|
|
api.HookUpstreamConnect,
|
|
func(req api.UpstreamConnectRequest) bool {
|
|
return p.config.MatchHost == "" || req.Host == p.config.MatchHost || req.Upstream == p.config.MatchHost
|
|
},
|
|
func(req api.UpstreamConnectRequest) (net.Conn, error) {
|
|
if p.config.Upstream == "" {
|
|
return nil, api.ErrPass
|
|
}
|
|
if p.config.MatchHost != "" && req.Host != p.config.MatchHost && req.Upstream != p.config.MatchHost {
|
|
return nil, api.ErrPass
|
|
}
|
|
return net.Dial("tcp", p.config.Upstream)
|
|
},
|
|
)
|
|
}
|
|
|
|
var manifestJSON = compactJSON(map[string]any{
|
|
"schema_version": "mc-gateway.plugin/v1",
|
|
"id": "upstream-rewrite",
|
|
"name": "Upstream Rewrite",
|
|
"version": "0.1.0",
|
|
"description": "Rewrite selected upstream targets before dialing.",
|
|
"artifact_type": "binary",
|
|
"runtime": map[string]any{
|
|
"type": "go-plugin",
|
|
"entry": "plugin.so",
|
|
"entry_symbol": "Plugin",
|
|
"metadata_symbol": "MCGatewayPluginMetadata",
|
|
},
|
|
"api_version": "plugin-api/v1",
|
|
"sdk_module": "github.com/tursom/mc-gateway/plugin/api",
|
|
"go_version": runtime.Version(),
|
|
"go_os": runtime.GOOS,
|
|
"go_arch": runtime.GOARCH,
|
|
"extension_points": []map[string]any{
|
|
{"type": "hook", "key": "upstream.connect/v1"},
|
|
},
|
|
"capabilities": map[string]any{
|
|
"extension_points": []string{"upstream.connect/v1"},
|
|
"network": map[string]any{"outbound": []string{"tcp:*:*"}},
|
|
},
|
|
"runtime_limits": map[string]any{
|
|
"handler_timeout_ms": 3000,
|
|
},
|
|
"config_schema": map[string]any{
|
|
"type": "object",
|
|
"properties": map[string]any{
|
|
"match_host": map[string]any{"type": "string"},
|
|
"upstream": map[string]any{"type": "string"},
|
|
},
|
|
},
|
|
})
|
|
|
|
func compactJSON(value any) string {
|
|
data, _ := json.Marshal(value)
|
|
return string(data)
|
|
}
|