fix(reverseproxy): bridge websocket extended connect upstreams

This commit is contained in:
wjqserver 2026-04-02 18:19:41 +08:00
parent 919236665b
commit a9c1662333
5 changed files with 508 additions and 99 deletions

View file

@ -68,6 +68,7 @@ type ReverseProxyConfig struct {
Transport http.RoundTripper
FlushInterval time.Duration
BufferPool BufferPool
AllowH2CUpstream bool
ModifyRequest func(*http.Request)
ModifyResponse func(*http.Response) error
@ -191,6 +192,24 @@ r.ANY("/api/*path", touka.ReverseProxy(touka.ReverseProxyConfig{
}))
```
### `AllowH2CUpstream`
允许代理使用未加密 HTTP/2h2c`http://` upstream 通信。
- 默认关闭
- 这是一个显式配置项
- 启用后Touka 会为该 upstream 使用 h2c prior-knowledge 方式连接上游
- 这意味着上游本身也必须显式支持 h2c它不是“先试 h2c失败再自动回退到 h1”的协商模式
```go
r.GET("/api/*path", touka.ReverseProxy(touka.ReverseProxyConfig{
Target: target,
AllowH2CUpstream: true,
}))
```
对于下游 HTTP/2 extended `CONNECT` websocket 场景Touka 会只在该特殊桥接路径上强制与上游使用 HTTP/1.1 websocket upgrade以匹配 Caddy 风格的桥接语义;普通 HTTP 请求不会因为这个特性而被强制降级为 HTTP/1.1。
### `Transport`
可选。用于自定义底层转发所使用的 `http.RoundTripper`