XHTTP server: Switch to Go 1.24 native h2c support (#4451)

This commit is contained in:
A1lo 2025-03-01 11:20:54 +08:00 committed by RPRX
parent 06b4a7ce4d
commit 4b616f5cd0
2 changed files with 10 additions and 17 deletions

View File

@ -24,8 +24,6 @@ import (
"github.com/xtls/xray-core/transport/internet/reality" "github.com/xtls/xray-core/transport/internet/reality"
"github.com/xtls/xray-core/transport/internet/stat" "github.com/xtls/xray-core/transport/internet/stat"
"github.com/xtls/xray-core/transport/internet/tls" "github.com/xtls/xray-core/transport/internet/tls"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
) )
type requestHandler struct { type requestHandler struct {
@ -426,11 +424,15 @@ func ListenXH(ctx context.Context, address net.Address, port net.Port, streamSet
handler.localAddr = l.listener.Addr() handler.localAddr = l.listener.Addr()
// h2cHandler can handle both plaintext HTTP/1.1 and h2c // server can handle both plaintext HTTP/1.1 and h2c
protocols := new(http.Protocols)
protocols.SetHTTP1(true)
protocols.SetUnencryptedHTTP2(true)
l.server = http.Server{ l.server = http.Server{
Handler: h2c.NewHandler(handler, &http2.Server{}), Handler: handler,
ReadHeaderTimeout: time.Second * 4, ReadHeaderTimeout: time.Second * 4,
MaxHeaderBytes: 8192, MaxHeaderBytes: 8192,
Protocols: protocols,
} }
go func() { go func() {
if err := l.server.Serve(l.listener); err != nil { if err := l.server.Serve(l.listener); err != nil {

View File

@ -3,10 +3,8 @@ package splithttp_test
import ( import (
"context" "context"
"crypto/rand" "crypto/rand"
gotls "crypto/tls"
"fmt" "fmt"
"io" "io"
gonet "net"
"net/http" "net/http"
"runtime" "runtime"
"testing" "testing"
@ -23,7 +21,6 @@ import (
. "github.com/xtls/xray-core/transport/internet/splithttp" . "github.com/xtls/xray-core/transport/internet/splithttp"
"github.com/xtls/xray-core/transport/internet/stat" "github.com/xtls/xray-core/transport/internet/stat"
"github.com/xtls/xray-core/transport/internet/tls" "github.com/xtls/xray-core/transport/internet/tls"
"golang.org/x/net/http2"
) )
func Test_ListenXHAndDial(t *testing.T) { func Test_ListenXHAndDial(t *testing.T) {
@ -201,17 +198,11 @@ func Test_ListenXHAndDial_H2C(t *testing.T) {
common.Must(err) common.Must(err)
defer listen.Close() defer listen.Close()
protocols := new(http.Protocols)
protocols.SetUnencryptedHTTP2(true)
client := http.Client{ client := http.Client{
Transport: &http2.Transport{ Transport: &http.Transport{
// So http2.Transport doesn't complain the URL scheme isn't 'https' Protocols: protocols,
AllowHTTP: true,
// even with AllowHTTP, http2.Transport will attempt to establish
// the connection using DialTLSContext. Disable TLS with custom
// dial context.
DialTLSContext: func(ctx context.Context, network, addr string, cfg *gotls.Config) (gonet.Conn, error) {
var d gonet.Dialer
return d.DialContext(ctx, network, addr)
},
}, },
} }