1

我正在尝试为 go kong 插件https://github.com/Kong/go-pdk配置公司代理

这是我的代码:

package main

import (
    "github.com/Kong/go-pdk"
)

type Config struct {
    Apikey string
}

func New() interface{} {
    return &Config{}
}

func (conf Config) Access(kong *pdk.PDK) {

    err := kong.Nginx.SetCtx("balancer_address.host", "127.0.0.1")
    if err != nil {
        kong.Log.Err(err.Error())
    }
    errC := kong.Nginx.SetCtx("balancer_address.port","8080")
    if errC != nil {
        kong.Log.Err(errC)
    }

    key, err := kong.Request.GetQueryArg("key")
    apiKey := conf.Apikey

    if err != nil {
        kong.Log.Err(err.Error())
    }

    x := make(map[string][]string)
    x["Content-Type"] = append(x["Content-Type"], "application/json")

    if apiKey != key {
        kong.Response.Exit(403, "Youu have no correct key", x)
    }
}

当然我在我的主机上创建了测试代理服务器:127.0.0.1:8080

但它不工作

通过插件文档:kong.Nginx.SetCtx() 在 ngx.ctx 请求上下文表中设置一个值

我通过示例https://github.com/tfabien/kong-forward-proxy/blob/master/src/access.lua制作 了我的 go 插件

ngx.ctx.balancer_address.host = plugin_conf.proxy_host
ngx.ctx.balancer_address.port = plugin_conf.proxy_port

正在配置代理

所以,无论如何它不起作用

我的 dockerfile 是

FROM kong/go-plugin-tool:2.0.4-alpine-latest AS builder

RUN mkdir -p /tmp/key-checker/

COPY . /tmp/key-checker/

RUN cd /tmp/key-checker/ && \
    go get github.com/Kong/go-pdk && \
    go mod init kong-go-plugin && \
    go get -d -v github.com/Kong/go-pluginserver && \
    go build github.com/Kong/go-pluginserver && \
    go build -buildmode plugin key-checker.go

FROM kong:2.3-alpine

RUN mkdir /tmp/go-plugins
COPY --from=builder  /tmp/key-checker/go-pluginserver /usr/local/bin/go-pluginserver
COPY --from=builder  /tmp/key-checker/key-checker.so /tmp/go-plugins

ENV KONG_PLUGINSERVER_NAMES="go"
ENV KONG_PLUGINSERVER_GO_SOCKET="/tmp/go-plugins/go_pluginserver.sock"
ENV KONG_PLUGINSERVER_GO_START_CMD="/usr/local/bin/go-pluginserver -kong-prefix /tmp/go-plugins/ -plugins-directory /tmp/go-plugins"
ENV KONG_PLUGINSERVER_GO_QUERY_CMD="/usr/local/bin/go-pluginserver -dump-all-plugins -plugins-directory /tmp/go-plugins"

COPY config.yml /tmp/config.yml

USER root
RUN chmod -R 777 /tmp
RUN /usr/local/bin/go-pluginserver -version && \
    cd /tmp/go-plugins && \
    /usr/local/bin/go-pluginserver -dump-plugin-info key-checker
USER kong

通过以下方式启动 docker:

docker run -ti --rm --name kong-go-plugins \
  -e "KONG_DATABASE=off" \
  -e "KONG_GO_PLUGINS_DIR=/tmp/go-plugins" \
  -e "KONG_DECLARATIVE_CONFIG=/tmp/config.yml" \
  -e "KONG_GO_PLUGINSERVER_EXE=/usr/local/bin/go-pluginserver" \
  -e "KONG_PLUGINS=key-checker" \
  -e "KONG_PROXY_LISTEN=0.0.0.0:8000" \
  -p 8000:8000 \
  kong-demo

我该如何解决?

4

0 回答 0