首页 >后端开发 >Golang >为 KrakenD 实现插件时无效的节点类型恐慌

为 KrakenD 实现插件时无效的节点类型恐慌

WBOY
WBOY转载
2024-02-09 10:21:091082浏览

为 KrakenD 实现插件时无效的节点类型恐慌

php小编百草今天为大家介绍的是关于KrakenD插件开发中的一个常见问题:"为 KrakenD 实现插件时无效的节点类型恐慌"。KrakenD是一个快速、高性能的API网关,它提供了丰富的功能和灵活的插件系统。然而,在开发KrakenD插件时,有时会遇到节点类型无效的问题,这可能导致插件无法正常运行。在本文中,我们将探讨这个问题的原因以及解决方案,帮助开发者克服这个恐慌。

问题内容

我正在开发一个无重定向的插件。我正在使用 krakend-ce 2.2.1(使用 golang 1.19),我感到恐慌:

gw_krakend_1  | [krakend] 2023/03/15 - 21:09:06.675 ? debug no_redirect_plugin: request received https://127.0.0.1:8443/abc
gw_krakend_1  | [krakend] 2023/03/15 - 21:09:06.689 ? debug no_redirect_plugin: redirect detected https://127.0.0.1:8443/abc
gw_krakend_1  | [krakend] 2023/03/15 - 21:09:06.689 ? debug status code 302
gw_krakend_1  | 2023/03/15 21:09:06 http: panic serving [::1]:54778: invalid node type
gw_krakend_1  | goroutine 84 [running]:
gw_krakend_1  | net/http.(*conn).serve.func1()
gw_krakend_1  |         /usr/local/go/src/net/http/server.go:1854 +0xbf
gw_krakend_1  | panic({0x28cbb60, 0x34b5810})
gw_krakend_1  |         /usr/local/go/src/runtime/panic.go:890 +0x263
gw_krakend_1  | github.com/gin-gonic/gin.(*node).findcaseinsensitivepathrec(0x0?, {0xc0016ac2ec?, 0x0?}, {0xc0010fe800?, 0xc0016ac2ed?, 0xc000ced928?}, {0x0, 0x0, 0x0, 0x0}, ...)
gw_krakend_1  |         /go/pkg/mod/github.com/gin-gonic/[email protected]/tree.go:862 +0xa9d
gw_krakend_1  | github.com/gin-gonic/gin.(*node).findcaseinsensitivepath(0xc0016ac2ec?, {0xc0016ac2ec, 0x5}, 0x30?)
gw_krakend_1  |         /go/pkg/mod/github.com/gin-gonic/[email protected]/tree.go:669 +0x9c
gw_krakend_1  | github.com/gin-gonic/gin.redirectfixedpath(0xc000664300, 0xc0016ac2ec?, 0x60?)
gw_krakend_1  |         /go/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:684 +0x5b
gw_krakend_1  | github.com/gin-gonic/gin.(*engine).handlehttprequest(0xc000602b60, 0xc000664300)

看起来与它类似https://github.com/gin-gonic/gin/issues/2959,但是gin的版本已经在以前版本的krakend上升级了。如果它们真的是大写字母那就很奇怪了,没有插件它可以完美地工作。我还修剪了最后一个 / (由于某种原因它在某个点添加了)

顺便说一句,我正在使用相同版本的 krakend 编译插件。

package main

import (
    "context"
    "errors"
    "fmt"
    "io"
    "net/http"
    "net/url"
    "strings"
)

func main() {}

var clientregisterer = registerer("no_redirect_plugin")

type registerer string

type logger interface {
    debug(v ...interface{})
    info(v ...interface{})
    warning(v ...interface{})
    error(v ...interface{})
    critical(v ...interface{})
    fatal(v ...interface{})
}

var logger logger = nil

func (registerer) registerlogger(v interface{}) {
    l, ok := v.(logger)
    if !ok {
        return
    }
    logger = l
    logger.info(fmt.sprintf("[plugin: %s] logger loaded", clientregisterer))
}

func (r registerer) registerclients(f func(
    name string,
    handler func(context.context, map[string]interface{}) (http.handler, error),
)) {
    f(string(r), r.registerclients)
}

func (r registerer) registerclients(_ context.context, extra map[string]interface{}) (http.handler, error) {
    name, ok := extra["name"].(string)
    if !ok {
        return nil, errors.new("wrong config")
    }

    if name != string(r) {
        return nil, fmt.errorf("unknown register %s", name)
    }

    httpclient := &http.client{
        checkredirect: func(req *http.request, via []*http.request) error {

            // trim the last "/" character from the url if it exists
            urlstr := strings.trimright(req.url.string(), "/")
            req.url, _ = url.parse(urlstr)

            logger.debug("no_redirect_plugin: redirect detected", req.url.string())
            return http.erruselastresponse
        },
    }

    return http.handlerfunc(func(w http.responsewriter, req *http.request) {

        // trim the last "/" character from the url if it exists
        urlstr := strings.trimright(req.url.string(), "/")
        req.url, _ = url.parse(urlstr)

        logger.debug("no_redirect_plugin: request received", req.url.string())
        resp, err := httpclient.do(req)
        if err != nil {
            logger.debug("error while proxying request", err.error())
            http.error(w, err.error(), http.statusinternalservererror)
            return
        }

        defer resp.body.close()

        for k, hs := range resp.header {
            for _, h := range hs {
                w.header().add(k, h)
            }
        }

        w.writeheader(resp.statuscode)
        logger.debug("status code", resp.statuscode)
        if resp.body == nil {
            return
        }

        _, err = io.copy(w, resp.body)
        if err != nil {
            logger.debug("error while proxying request 2", err.error())
            http.error(w, err.error(), http.statusinternalservererror)
            return
        }

    }), nil
}

我的端点定义如下:

{
     "endpoint": "/",
     "input_headers":[
       "*"
     ],
     "input_query_strings":[
       "*"
     ],
     "method": "GET",
     "output_encoding": "no-op",
     "extra_config": {},
     "backend": [{
       "url_pattern": "",
       "encoding": "no-op",
       "sd": "static",
       "method": "GET",
       "extra_config": {
         "plugin/http-client": {
           "name": "no_redirect_plugin"
         }
       },
       "host": [
         "{{ env "HOST" }}"
       ],
       "disable_host_sanitize": false
   }]
},{
     "endpoint": "/{level1}",
     "input_headers":[
       "*"
     ],
     "input_query_strings":[
       "*"
     ],
     "method": "GET",
     "output_encoding": "no-op",
     "extra_config": {
     },
     "backend": [{
       "url_pattern": "/{level1}",
       "encoding": "no-op",
       "sd": "static",
       "method": "GET",
       "extra_config": {
         "plugin/http-client": {
           "name": "no_redirect_plugin"
         }
       },
       "host": [
         "{{ env "HOST" }}"
       ],
       "disable_host_sanitize": false
   }]
},{
     "endpoint": "/{level1}/{level2}",
     "input_headers":[
       "*"
     ],
     "input_query_strings":[
       "*"
     ],
     "method": "GET",
     "output_encoding": "no-op",
     "extra_config": {
     },
     "backend": [{
       "url_pattern": "/{level1}/{level2}",
       "encoding": "no-op",
       "sd": "static",
       "method": "GET",
       "extra_config": {
         "plugin/http-client": {
           "name": "no_redirect_plugin"
         }
       },
       "host": [
         "{{ env "HOST" }}"
       ],
       "disable_host_sanitize": false
   }]
}

编辑:我的浏览器仍然显示 /abc/ 而不是 /abc,这可能会在路线之间产生可能的碰撞(如下所示:https://github.com/krakendio/krakend-ce/issues/386)无论如何我不知道知道在哪里添加斜杠(我以为我把它永久修剪了......似乎我没有)

edit2:我发现了这个 https://www.krakend.io/docs/service-settings/router-options/ 并使用“disable_redirect_fixed_pa​​th”:true 和“disable_redirect_trailing_slash”:true,它不再恐慌......现在我有另一个问题:无限重定向(开玩笑只是10)当我的巨石试图重定向到/a/或任何结尾带有斜杠的路线时,这在插件,因为 krakend 过去常常通过自己的方式处理重定向...

我猜这里的主要问题是路由冲突,当 /{level1} 和 /{level1}/{level2} 同时与 /abc/ 匹配时

想法?

解决方法

很好,为了解决这个问题,我尝试使用这个 创建一个通配符+no_redirect 插件https://www.php.cn/link/ba530cdf0a884348613f2aaa3a5ba5e8 配置,但即使使用 copilot&gpt4 我也失败了,我敢说你可以实现这个。所以我用另一种方法解决了这个问题:我将网关放在我的巨石前面,每次请求失败时我都会添加路由......因为我们没有记录的路由(*哭泣)。对于需要无重定向插件的文件,它工作得很好。一个很棒的解决方案,但它到目前为止一直有效,而且没有我想象的那么长

以上是为 KrakenD 实现插件时无效的节点类型恐慌的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:stackoverflow.com。如有侵权,请联系admin@php.cn删除