From 18b823b4a611fe102fdef320f0481f4b342a565a Mon Sep 17 00:00:00 2001 From: RPRX <63339210+RPRX@users.noreply.github.com> Date: Sun, 17 Mar 2024 20:43:19 +0000 Subject: [PATCH] HTTPUpgrade 0-RTT (#3152) * Add ed to enable HTTPUpgrade 0-RTT https://github.com/XTLS/Xray-core/issues/3128#issuecomment-2002563369 * WebSocket hub.go MaxHeaderBytes: 4096 -> 8192 --- infra/conf/transport_internet.go | 30 +++++++++++++-------- transport/internet/httpupgrade/config.pb.go | 15 ++++++++--- transport/internet/httpupgrade/config.proto | 4 ++- transport/internet/httpupgrade/dialer.go | 15 ++++++++++- transport/internet/websocket/hub.go | 2 +- 5 files changed, 49 insertions(+), 17 deletions(-) diff --git a/infra/conf/transport_internet.go b/infra/conf/transport_internet.go index 9ab34cf5..96b458ed 100644 --- a/infra/conf/transport_internet.go +++ b/infra/conf/transport_internet.go @@ -172,12 +172,10 @@ func (c *WebSocketConfig) Build() (proto.Message, error) { } } config := &websocket.Config{ - Path: path, - Header: header, - Ed: ed, - } - if c.AcceptProxyProtocol { - config.AcceptProxyProtocol = c.AcceptProxyProtocol + Path: path, + Header: header, + AcceptProxyProtocol: c.AcceptProxyProtocol, + Ed: ed, } return config, nil } @@ -190,12 +188,22 @@ type HttpUpgradeConfig struct { // Build implements Buildable. func (c *HttpUpgradeConfig) Build() (proto.Message, error) { - config := &httpupgrade.Config{ - Path: c.Path, - Host: c.Host, + path := c.Path + var ed uint32 + if u, err := url.Parse(path); err == nil { + if q := u.Query(); q.Get("ed") != "" { + Ed, _ := strconv.Atoi(q.Get("ed")) + ed = uint32(Ed) + q.Del("ed") + u.RawQuery = q.Encode() + path = u.String() + } } - if c.AcceptProxyProtocol { - config.AcceptProxyProtocol = c.AcceptProxyProtocol + config := &httpupgrade.Config{ + Path: path, + Host: c.Host, + AcceptProxyProtocol: c.AcceptProxyProtocol, + Ed: ed, } return config, nil } diff --git a/transport/internet/httpupgrade/config.pb.go b/transport/internet/httpupgrade/config.pb.go index f097f63f..5b9fb7bc 100644 --- a/transport/internet/httpupgrade/config.pb.go +++ b/transport/internet/httpupgrade/config.pb.go @@ -1,7 +1,7 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: // protoc-gen-go v1.32.0 -// protoc v4.25.1 +// protoc v4.23.1 // source: transport/internet/httpupgrade/config.proto package httpupgrade @@ -28,6 +28,7 @@ type Config struct { Path string `protobuf:"bytes,2,opt,name=path,proto3" json:"path,omitempty"` Host string `protobuf:"bytes,3,opt,name=host,proto3" json:"host,omitempty"` AcceptProxyProtocol bool `protobuf:"varint,4,opt,name=accept_proxy_protocol,json=acceptProxyProtocol,proto3" json:"accept_proxy_protocol,omitempty"` + Ed uint32 `protobuf:"varint,5,opt,name=ed,proto3" json:"ed,omitempty"` } func (x *Config) Reset() { @@ -83,6 +84,13 @@ func (x *Config) GetAcceptProxyProtocol() bool { return false } +func (x *Config) GetEd() uint32 { + if x != nil { + return x.Ed + } + return 0 +} + var File_transport_internet_httpupgrade_config_proto protoreflect.FileDescriptor var file_transport_internet_httpupgrade_config_proto_rawDesc = []byte{ @@ -91,13 +99,14 @@ var file_transport_internet_httpupgrade_config_proto_rawDesc = []byte{ 0x2f, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x23, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x70, 0x6f, 0x72, 0x74, 0x2e, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x2e, 0x68, 0x74, 0x74, 0x70, 0x75, 0x70, 0x67, 0x72, 0x61, - 0x64, 0x65, 0x22, 0x6a, 0x0a, 0x06, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, 0x12, 0x0a, 0x04, + 0x64, 0x65, 0x22, 0x7a, 0x0a, 0x06, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, 0x12, 0x0a, 0x04, 0x70, 0x61, 0x74, 0x68, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x70, 0x61, 0x74, 0x68, 0x12, 0x12, 0x0a, 0x04, 0x68, 0x6f, 0x73, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x68, 0x6f, 0x73, 0x74, 0x12, 0x32, 0x0a, 0x15, 0x61, 0x63, 0x63, 0x65, 0x70, 0x74, 0x5f, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x5f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x6c, 0x18, 0x04, 0x20, 0x01, 0x28, 0x08, 0x52, 0x13, 0x61, 0x63, 0x63, 0x65, 0x70, 0x74, 0x50, 0x72, 0x6f, 0x78, 0x79, - 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x6c, 0x4a, 0x04, 0x08, 0x01, 0x10, 0x02, 0x42, 0x8b, + 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x6c, 0x12, 0x0e, 0x0a, 0x02, 0x65, 0x64, 0x18, 0x05, + 0x20, 0x01, 0x28, 0x0d, 0x52, 0x02, 0x65, 0x64, 0x4a, 0x04, 0x08, 0x01, 0x10, 0x02, 0x42, 0x8b, 0x01, 0x0a, 0x27, 0x63, 0x6f, 0x6d, 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x70, 0x6f, 0x72, 0x74, 0x2e, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x2e, 0x68, 0x74, 0x74, 0x70, 0x75, 0x70, 0x67, 0x72, 0x61, 0x64, 0x65, 0x50, 0x01, 0x5a, 0x38, 0x67, 0x69, diff --git a/transport/internet/httpupgrade/config.proto b/transport/internet/httpupgrade/config.proto index 2be7cc22..c1874c9e 100644 --- a/transport/internet/httpupgrade/config.proto +++ b/transport/internet/httpupgrade/config.proto @@ -13,4 +13,6 @@ message Config { string host = 3; bool accept_proxy_protocol = 4; -} \ No newline at end of file + + uint32 ed = 5; +} diff --git a/transport/internet/httpupgrade/dialer.go b/transport/internet/httpupgrade/dialer.go index 159dcb98..691f85a8 100644 --- a/transport/internet/httpupgrade/dialer.go +++ b/transport/internet/httpupgrade/dialer.go @@ -81,7 +81,20 @@ func dialhttpUpgrade(ctx context.Context, dest net.Destination, streamSettings * return nil, err } - return &ConnRF{Conn: conn, Req: req, First: true}, nil + connRF := &ConnRF{ + Conn: conn, + Req: req, + First: true, + } + + if transportConfiguration.Ed == 0 { + _, err = connRF.Read([]byte{}) + if err != nil { + return nil, err + } + } + + return connRF, nil } func dial(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (stat.Connection, error) { diff --git a/transport/internet/websocket/hub.go b/transport/internet/websocket/hub.go index c907e224..b3fe9ff4 100644 --- a/transport/internet/websocket/hub.go +++ b/transport/internet/websocket/hub.go @@ -129,7 +129,7 @@ func ListenWS(ctx context.Context, address net.Address, port net.Port, streamSet ln: l, }, ReadHeaderTimeout: time.Second * 4, - MaxHeaderBytes: 4096, + MaxHeaderBytes: 8192, } go func() {