Skip to content

Commit

Permalink
REALITY: Unblock SplitHTTP transport (XTLS#3816)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmmray authored Oct 30, 2024
1 parent 9f8bb47 commit e733148
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
4 changes: 2 additions & 2 deletions infra/conf/transport_internet.go
Original file line number Diff line number Diff line change
Expand Up @@ -838,8 +838,8 @@ func (c *StreamConfig) Build() (*internet.StreamConfig, error) {
config.SecuritySettings = append(config.SecuritySettings, tm)
config.SecurityType = tm.Type
case "reality":
if config.ProtocolName != "tcp" && config.ProtocolName != "http" && config.ProtocolName != "grpc" {
return nil, errors.New("REALITY only supports TCP, H2 and gRPC for now.")
if config.ProtocolName != "tcp" && config.ProtocolName != "http" && config.ProtocolName != "grpc" && config.ProtocolName != "splithttp" {
return nil, errors.New("REALITY only supports TCP, H2, gRPC and SplitHTTP for now.")
}
if c.REALITYSettings == nil {
return nil, errors.New(`REALITY: Empty "realitySettings".`)
Expand Down
26 changes: 22 additions & 4 deletions transport/internet/splithttp/dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/xtls/xray-core/common/uuid"
"github.com/xtls/xray-core/transport/internet"
"github.com/xtls/xray-core/transport/internet/browser_dialer"
"github.com/xtls/xray-core/transport/internet/reality"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/xtls/xray-core/transport/internet/tls"
"github.com/xtls/xray-core/transport/pipe"
Expand Down Expand Up @@ -46,7 +47,9 @@ var (
)

func getHTTPClient(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (DialerClient, *muxResource) {
if browser_dialer.HasBrowserDialer() {
realityConfig := reality.ConfigFromStreamSettings(streamSettings)

if browser_dialer.HasBrowserDialer() && realityConfig != nil {
return &BrowserDialerClient{}, nil
}

Expand Down Expand Up @@ -80,8 +83,18 @@ func getHTTPClient(ctx context.Context, dest net.Destination, streamSettings *in

func createHTTPClient(dest net.Destination, streamSettings *internet.MemoryStreamConfig) DialerClient {
tlsConfig := tls.ConfigFromStreamSettings(streamSettings)
isH2 := tlsConfig != nil && !(len(tlsConfig.NextProtocol) == 1 && tlsConfig.NextProtocol[0] == "http/1.1")
isH3 := tlsConfig != nil && (len(tlsConfig.NextProtocol) == 1 && tlsConfig.NextProtocol[0] == "h3")
realityConfig := reality.ConfigFromStreamSettings(streamSettings)

isH2 := false
isH3 := false

if tlsConfig != nil {
isH2 = !(len(tlsConfig.NextProtocol) == 1 && tlsConfig.NextProtocol[0] == "http/1.1")
isH3 = len(tlsConfig.NextProtocol) == 1 && tlsConfig.NextProtocol[0] == "h3"
} else if realityConfig != nil {
isH2 = true
isH3 = false
}

if isH3 {
dest.Network = net.Network_UDP
Expand All @@ -101,6 +114,10 @@ func createHTTPClient(dest net.Destination, streamSettings *internet.MemoryStrea
return nil, err
}

if realityConfig != nil {
return reality.UClient(conn, realityConfig, ctxInner, dest)
}

if gotlsConfig != nil {
if fingerprint := tls.GetFingerprint(tlsConfig.Fingerprint); fingerprint != nil {
conn = tls.UClient(conn, gotlsConfig, fingerprint)
Expand Down Expand Up @@ -215,12 +232,13 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me

transportConfiguration := streamSettings.ProtocolSettings.(*Config)
tlsConfig := tls.ConfigFromStreamSettings(streamSettings)
realityConfig := reality.ConfigFromStreamSettings(streamSettings)

scMaxConcurrentPosts := transportConfiguration.GetNormalizedScMaxConcurrentPosts()
scMaxEachPostBytes := transportConfiguration.GetNormalizedScMaxEachPostBytes()
scMinPostsIntervalMs := transportConfiguration.GetNormalizedScMinPostsIntervalMs()

if tlsConfig != nil {
if tlsConfig != nil || realityConfig != nil {
requestURL.Scheme = "https"
} else {
requestURL.Scheme = "http"
Expand Down
6 changes: 6 additions & 0 deletions transport/internet/splithttp/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ import (

"github.com/quic-go/quic-go"
"github.com/quic-go/quic-go/http3"
goreality "github.com/xtls/reality"
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/common/net"
http_proto "github.com/xtls/xray-core/common/protocol/http"
"github.com/xtls/xray-core/common/signal/done"
"github.com/xtls/xray-core/transport/internet"
"github.com/xtls/xray-core/transport/internet/reality"
"github.com/xtls/xray-core/transport/internet/stat"
v2tls "github.com/xtls/xray-core/transport/internet/tls"
"golang.org/x/net/http2"
Expand Down Expand Up @@ -344,6 +346,10 @@ func ListenSH(ctx context.Context, address net.Address, port net.Port, streamSet
}
}

if config := reality.ConfigFromStreamSettings(streamSettings); config != nil {
listener = goreality.NewListener(listener, config.GetREALITYConfig())
}

// h2cHandler can handle both plaintext HTTP/1.1 and h2c
h2cHandler := h2c.NewHandler(handler, &http2.Server{})
l.listener = listener
Expand Down

0 comments on commit e733148

Please sign in to comment.