调试上游填写IP,且协议为HTTPS,重写Host证书验证失败的问题

This commit is contained in:
Liujian
2023-11-30 17:29:02 +08:00
parent 218f4b06ce
commit 1338f0d490
5 changed files with 25 additions and 16 deletions

View File

@@ -116,18 +116,22 @@ func (h *complete) Complete(org eocontext.EoContext) error {
}
request.URI()
host := ""
passHost, targetHost := ctx.GetUpstreamHostHandler().PassHost()
switch passHost {
case eocontext.PassHost:
request.URI().SetHost(strings.Join(ctx.Proxy().Headers().Get(":authority"), ","))
host = strings.Join(ctx.Proxy().Headers().Get(":authority"), ",")
request.URI().SetHost(host)
case eocontext.NodeHost:
request.URI().SetHost(node.Addr())
host = node.Addr()
case eocontext.ReWriteHost:
request.URI().SetHost(targetHost)
host = targetHost
}
response := fasthttp.AcquireResponse()
lastErr = fasthttp_client.ProxyTimeout(scheme, node, request, response, timeOut)
lastErr = fasthttp_client.ProxyTimeout(scheme, host, node, request, response, timeOut)
if lastErr == nil {
return newGRPCResponse(ctx, response, methodDesc)
}

View File

@@ -13,9 +13,9 @@ import (
"github.com/valyala/fasthttp"
)
func ProxyTimeout(scheme string, node eocontext.INode, req *fasthttp.Request, resp *fasthttp.Response, timeout time.Duration) error {
func ProxyTimeout(scheme string, host string, node eocontext.INode, req *fasthttp.Request, resp *fasthttp.Response, timeout time.Duration) error {
addr := fmt.Sprintf("%s://%s", scheme, node.Addr())
err := defaultClient.ProxyTimeout(addr, req, resp, timeout)
err := defaultClient.ProxyTimeout(addr, host, req, resp, timeout)
if err != nil {
node.Down()
}
@@ -50,13 +50,14 @@ func readAddress(addr string) (scheme, host string) {
return "http", addr
}
func (c *Client) getHostClient(addr string) (*fasthttp.HostClient, string, error) {
scheme, host := readAddress(addr)
func (c *Client) getHostClient(addr string, rewriteHost string) (*fasthttp.HostClient, string, error) {
scheme, nodeAddr := readAddress(addr)
host := nodeAddr
isTLS := false
if strings.EqualFold(scheme, "https") {
isTLS = true
host = rewriteHost + nodeAddr
} else if !strings.EqualFold(scheme, "http") {
return nil, "", fmt.Errorf("unsupported protocol %q. http and https are supported", scheme)
}
@@ -79,7 +80,7 @@ func (c *Client) getHostClient(addr string) (*fasthttp.HostClient, string, error
hc := m[host]
if hc == nil {
hc = &fasthttp.HostClient{
Addr: addMissingPort(host, isTLS),
Addr: addMissingPort(nodeAddr, isTLS),
IsTLS: isTLS,
Dial: Dial,
MaxConns: DefaultMaxConns,
@@ -129,7 +130,7 @@ func (c *Client) getHostClient(addr string) (*fasthttp.HostClient, string, error
// continue in the background and the response will be discarded.
// If requests take too long and the connection pool gets filled up please
// try setting a ReadTimeout.
func (c *Client) ProxyTimeout(addr string, req *fasthttp.Request, resp *fasthttp.Response, timeout time.Duration) error {
func (c *Client) ProxyTimeout(addr string, host string, req *fasthttp.Request, resp *fasthttp.Response, timeout time.Duration) error {
request := req
request.Header.ResetConnectionClose()
request.Header.Set("Connection", "keep-alive")
@@ -144,7 +145,7 @@ func (c *Client) ProxyTimeout(addr string, req *fasthttp.Request, resp *fasthttp
var requestURI string
redirectCount := 0
for {
client, scheme, err := c.getHostClient(addr)
client, scheme, err := c.getHostClient(addr, "")
if err != nil {
return err
}

View File

@@ -18,7 +18,7 @@ func TestMyselfProxyTimeout(t *testing.T) {
req.Header.SetContentType("application/json")
t.Log(string(req.URI().RequestURI()), req.URI().String(), string(req.URI().Host()), string(req.URI().Scheme()))
req.SetBody([]byte(`{"cpCode":"YTO","province":"广东省","city":"广州市"}`))
err := defaultClient.ProxyTimeout(addr, req, resp, 0)
err := defaultClient.ProxyTimeout(addr, "", req, resp, 0)
if err != nil {
t.Error(err)
}

View File

@@ -130,17 +130,19 @@ func (ctx *cloneContext) SendTo(scheme string, node eoscContext.INode, timeout t
host := node.Addr()
request := ctx.proxyRequest.Request()
rewriteHost := string(request.Host())
passHost, targetHost := ctx.GetUpstreamHostHandler().PassHost()
switch passHost {
case eoscContext.PassHost:
case eoscContext.NodeHost:
request.URI().SetHost(node.Addr())
rewriteHost = host
request.URI().SetHost(host)
case eoscContext.ReWriteHost:
rewriteHost = targetHost
request.URI().SetHost(targetHost)
}
beginTime := time.Now()
ctx.responseError = fasthttp_client.ProxyTimeout(scheme, node, request, ctx.response.Response, timeout)
ctx.responseError = fasthttp_client.ProxyTimeout(scheme, rewriteHost, node, request, ctx.response.Response, timeout)
agent := newRequestAgent(&ctx.proxyRequest, host, scheme, beginTime, time.Now())
if ctx.responseError != nil {
agent.setStatusCode(504)

View File

@@ -136,17 +136,19 @@ func (ctx *HttpContext) SendTo(scheme string, node eoscContext.INode, timeout ti
host := node.Addr()
request := ctx.proxyRequest.Request()
rewriteHost := string(request.Host())
passHost, targetHost := ctx.GetUpstreamHostHandler().PassHost()
switch passHost {
case eoscContext.PassHost:
case eoscContext.NodeHost:
rewriteHost = host
request.URI().SetHost(host)
case eoscContext.ReWriteHost:
rewriteHost = targetHost
request.URI().SetHost(targetHost)
}
beginTime := time.Now()
ctx.response.responseError = fasthttp_client.ProxyTimeout(scheme, node, request, &ctx.fastHttpRequestCtx.Response, timeout)
ctx.response.responseError = fasthttp_client.ProxyTimeout(scheme, rewriteHost, node, request, &ctx.fastHttpRequestCtx.Response, timeout)
agent := newRequestAgent(&ctx.proxyRequest, host, scheme, beginTime, time.Now())
if ctx.response.responseError != nil {
agent.setStatusCode(504)