mirror of
https://github.com/eolinker/apinto
synced 2025-10-22 08:19:34 +08:00
调试上游填写IP,且协议为HTTPS,重写Host证书验证失败的问题
This commit is contained in:
@@ -116,18 +116,22 @@ func (h *complete) Complete(org eocontext.EoContext) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
request.URI()
|
request.URI()
|
||||||
|
host := ""
|
||||||
passHost, targetHost := ctx.GetUpstreamHostHandler().PassHost()
|
passHost, targetHost := ctx.GetUpstreamHostHandler().PassHost()
|
||||||
switch passHost {
|
switch passHost {
|
||||||
case eocontext.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:
|
case eocontext.NodeHost:
|
||||||
request.URI().SetHost(node.Addr())
|
request.URI().SetHost(node.Addr())
|
||||||
|
host = node.Addr()
|
||||||
case eocontext.ReWriteHost:
|
case eocontext.ReWriteHost:
|
||||||
request.URI().SetHost(targetHost)
|
request.URI().SetHost(targetHost)
|
||||||
|
host = targetHost
|
||||||
}
|
}
|
||||||
response := fasthttp.AcquireResponse()
|
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 {
|
if lastErr == nil {
|
||||||
return newGRPCResponse(ctx, response, methodDesc)
|
return newGRPCResponse(ctx, response, methodDesc)
|
||||||
}
|
}
|
||||||
|
@@ -13,9 +13,9 @@ import (
|
|||||||
"github.com/valyala/fasthttp"
|
"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())
|
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 {
|
if err != nil {
|
||||||
node.Down()
|
node.Down()
|
||||||
}
|
}
|
||||||
@@ -50,13 +50,14 @@ func readAddress(addr string) (scheme, host string) {
|
|||||||
return "http", addr
|
return "http", addr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) getHostClient(addr string) (*fasthttp.HostClient, string, error) {
|
func (c *Client) getHostClient(addr string, rewriteHost string) (*fasthttp.HostClient, string, error) {
|
||||||
|
|
||||||
scheme, host := readAddress(addr)
|
|
||||||
|
|
||||||
|
scheme, nodeAddr := readAddress(addr)
|
||||||
|
host := nodeAddr
|
||||||
isTLS := false
|
isTLS := false
|
||||||
if strings.EqualFold(scheme, "https") {
|
if strings.EqualFold(scheme, "https") {
|
||||||
isTLS = true
|
isTLS = true
|
||||||
|
host = rewriteHost + nodeAddr
|
||||||
} else if !strings.EqualFold(scheme, "http") {
|
} else if !strings.EqualFold(scheme, "http") {
|
||||||
return nil, "", fmt.Errorf("unsupported protocol %q. http and https are supported", scheme)
|
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]
|
hc := m[host]
|
||||||
if hc == nil {
|
if hc == nil {
|
||||||
hc = &fasthttp.HostClient{
|
hc = &fasthttp.HostClient{
|
||||||
Addr: addMissingPort(host, isTLS),
|
Addr: addMissingPort(nodeAddr, isTLS),
|
||||||
IsTLS: isTLS,
|
IsTLS: isTLS,
|
||||||
Dial: Dial,
|
Dial: Dial,
|
||||||
MaxConns: DefaultMaxConns,
|
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.
|
// continue in the background and the response will be discarded.
|
||||||
// If requests take too long and the connection pool gets filled up please
|
// If requests take too long and the connection pool gets filled up please
|
||||||
// try setting a ReadTimeout.
|
// 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 := req
|
||||||
request.Header.ResetConnectionClose()
|
request.Header.ResetConnectionClose()
|
||||||
request.Header.Set("Connection", "keep-alive")
|
request.Header.Set("Connection", "keep-alive")
|
||||||
@@ -144,7 +145,7 @@ func (c *Client) ProxyTimeout(addr string, req *fasthttp.Request, resp *fasthttp
|
|||||||
var requestURI string
|
var requestURI string
|
||||||
redirectCount := 0
|
redirectCount := 0
|
||||||
for {
|
for {
|
||||||
client, scheme, err := c.getHostClient(addr)
|
client, scheme, err := c.getHostClient(addr, "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@@ -18,7 +18,7 @@ func TestMyselfProxyTimeout(t *testing.T) {
|
|||||||
req.Header.SetContentType("application/json")
|
req.Header.SetContentType("application/json")
|
||||||
t.Log(string(req.URI().RequestURI()), req.URI().String(), string(req.URI().Host()), string(req.URI().Scheme()))
|
t.Log(string(req.URI().RequestURI()), req.URI().String(), string(req.URI().Host()), string(req.URI().Scheme()))
|
||||||
req.SetBody([]byte(`{"cpCode":"YTO","province":"广东省","city":"广州市"}`))
|
req.SetBody([]byte(`{"cpCode":"YTO","province":"广东省","city":"广州市"}`))
|
||||||
err := defaultClient.ProxyTimeout(addr, req, resp, 0)
|
err := defaultClient.ProxyTimeout(addr, "", req, resp, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
@@ -130,17 +130,19 @@ func (ctx *cloneContext) SendTo(scheme string, node eoscContext.INode, timeout t
|
|||||||
|
|
||||||
host := node.Addr()
|
host := node.Addr()
|
||||||
request := ctx.proxyRequest.Request()
|
request := ctx.proxyRequest.Request()
|
||||||
|
rewriteHost := string(request.Host())
|
||||||
passHost, targetHost := ctx.GetUpstreamHostHandler().PassHost()
|
passHost, targetHost := ctx.GetUpstreamHostHandler().PassHost()
|
||||||
switch passHost {
|
switch passHost {
|
||||||
case eoscContext.PassHost:
|
case eoscContext.PassHost:
|
||||||
case eoscContext.NodeHost:
|
case eoscContext.NodeHost:
|
||||||
request.URI().SetHost(node.Addr())
|
rewriteHost = host
|
||||||
|
request.URI().SetHost(host)
|
||||||
case eoscContext.ReWriteHost:
|
case eoscContext.ReWriteHost:
|
||||||
|
rewriteHost = targetHost
|
||||||
request.URI().SetHost(targetHost)
|
request.URI().SetHost(targetHost)
|
||||||
}
|
}
|
||||||
beginTime := time.Now()
|
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())
|
agent := newRequestAgent(&ctx.proxyRequest, host, scheme, beginTime, time.Now())
|
||||||
if ctx.responseError != nil {
|
if ctx.responseError != nil {
|
||||||
agent.setStatusCode(504)
|
agent.setStatusCode(504)
|
||||||
|
@@ -136,17 +136,19 @@ func (ctx *HttpContext) SendTo(scheme string, node eoscContext.INode, timeout ti
|
|||||||
|
|
||||||
host := node.Addr()
|
host := node.Addr()
|
||||||
request := ctx.proxyRequest.Request()
|
request := ctx.proxyRequest.Request()
|
||||||
|
rewriteHost := string(request.Host())
|
||||||
passHost, targetHost := ctx.GetUpstreamHostHandler().PassHost()
|
passHost, targetHost := ctx.GetUpstreamHostHandler().PassHost()
|
||||||
switch passHost {
|
switch passHost {
|
||||||
case eoscContext.PassHost:
|
case eoscContext.PassHost:
|
||||||
case eoscContext.NodeHost:
|
case eoscContext.NodeHost:
|
||||||
|
rewriteHost = host
|
||||||
request.URI().SetHost(host)
|
request.URI().SetHost(host)
|
||||||
case eoscContext.ReWriteHost:
|
case eoscContext.ReWriteHost:
|
||||||
|
rewriteHost = targetHost
|
||||||
request.URI().SetHost(targetHost)
|
request.URI().SetHost(targetHost)
|
||||||
}
|
}
|
||||||
beginTime := time.Now()
|
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())
|
agent := newRequestAgent(&ctx.proxyRequest, host, scheme, beginTime, time.Now())
|
||||||
if ctx.response.responseError != nil {
|
if ctx.response.responseError != nil {
|
||||||
agent.setStatusCode(504)
|
agent.setStatusCode(504)
|
||||||
|
Reference in New Issue
Block a user