From 79d020f4d7253b6d1587dc6f9f5d8f3ca93529b6 Mon Sep 17 00:00:00 2001 From: yuhan6665 <1588741+yuhan6665@users.noreply.github.com> Date: Sun, 12 Jan 2025 12:57:47 -0500 Subject: [PATCH 1/2] Add config proto --- transport/internet/quic/config.pb.go | 137 +++++++++++++++++++++++++++ transport/internet/quic/config.proto | 14 +++ 2 files changed, 151 insertions(+) create mode 100644 transport/internet/quic/config.pb.go create mode 100644 transport/internet/quic/config.proto diff --git a/transport/internet/quic/config.pb.go b/transport/internet/quic/config.pb.go new file mode 100644 index 000000000000..5220cd79dcc6 --- /dev/null +++ b/transport/internet/quic/config.pb.go @@ -0,0 +1,137 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.35.1 +// protoc v5.28.2 +// source: transport/internet/quic/config.proto + +package quic + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type Config struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // string key = 1; + // xray.common.protocol.SecurityConfig security = 2; + // xray.common.serial.TypedMessage header = 3; + Fec bool `protobuf:"varint,4,opt,name=fec,proto3" json:"fec,omitempty"` +} + +func (x *Config) Reset() { + *x = Config{} + mi := &file_transport_internet_quic_config_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *Config) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Config) ProtoMessage() {} + +func (x *Config) ProtoReflect() protoreflect.Message { + mi := &file_transport_internet_quic_config_proto_msgTypes[0] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Config.ProtoReflect.Descriptor instead. +func (*Config) Descriptor() ([]byte, []int) { + return file_transport_internet_quic_config_proto_rawDescGZIP(), []int{0} +} + +func (x *Config) GetFec() bool { + if x != nil { + return x.Fec + } + return false +} + +var File_transport_internet_quic_config_proto protoreflect.FileDescriptor + +var file_transport_internet_quic_config_proto_rawDesc = []byte{ + 0x0a, 0x24, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x70, 0x6f, 0x72, 0x74, 0x2f, 0x69, 0x6e, 0x74, 0x65, + 0x72, 0x6e, 0x65, 0x74, 0x2f, 0x71, 0x75, 0x69, 0x63, 0x2f, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, + 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x1c, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x74, 0x72, 0x61, + 0x6e, 0x73, 0x70, 0x6f, 0x72, 0x74, 0x2e, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x2e, + 0x71, 0x75, 0x69, 0x63, 0x22, 0x1a, 0x0a, 0x06, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, 0x10, + 0x0a, 0x03, 0x66, 0x65, 0x63, 0x18, 0x04, 0x20, 0x01, 0x28, 0x08, 0x52, 0x03, 0x66, 0x65, 0x63, + 0x42, 0x76, 0x0a, 0x20, 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, + 0x71, 0x75, 0x69, 0x63, 0x50, 0x01, 0x5a, 0x31, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, + 0x6f, 0x6d, 0x2f, 0x78, 0x74, 0x6c, 0x73, 0x2f, 0x78, 0x72, 0x61, 0x79, 0x2d, 0x63, 0x6f, 0x72, + 0x65, 0x2f, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x70, 0x6f, 0x72, 0x74, 0x2f, 0x69, 0x6e, 0x74, 0x65, + 0x72, 0x6e, 0x65, 0x74, 0x2f, 0x71, 0x75, 0x69, 0x63, 0xaa, 0x02, 0x1c, 0x58, 0x72, 0x61, 0x79, + 0x2e, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x70, 0x6f, 0x72, 0x74, 0x2e, 0x49, 0x6e, 0x74, 0x65, 0x72, + 0x6e, 0x65, 0x74, 0x2e, 0x51, 0x75, 0x69, 0x63, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_transport_internet_quic_config_proto_rawDescOnce sync.Once + file_transport_internet_quic_config_proto_rawDescData = file_transport_internet_quic_config_proto_rawDesc +) + +func file_transport_internet_quic_config_proto_rawDescGZIP() []byte { + file_transport_internet_quic_config_proto_rawDescOnce.Do(func() { + file_transport_internet_quic_config_proto_rawDescData = protoimpl.X.CompressGZIP(file_transport_internet_quic_config_proto_rawDescData) + }) + return file_transport_internet_quic_config_proto_rawDescData +} + +var file_transport_internet_quic_config_proto_msgTypes = make([]protoimpl.MessageInfo, 1) +var file_transport_internet_quic_config_proto_goTypes = []any{ + (*Config)(nil), // 0: xray.transport.internet.quic.Config +} +var file_transport_internet_quic_config_proto_depIdxs = []int32{ + 0, // [0:0] is the sub-list for method output_type + 0, // [0:0] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_transport_internet_quic_config_proto_init() } +func file_transport_internet_quic_config_proto_init() { + if File_transport_internet_quic_config_proto != nil { + return + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_transport_internet_quic_config_proto_rawDesc, + NumEnums: 0, + NumMessages: 1, + NumExtensions: 0, + NumServices: 0, + }, + GoTypes: file_transport_internet_quic_config_proto_goTypes, + DependencyIndexes: file_transport_internet_quic_config_proto_depIdxs, + MessageInfos: file_transport_internet_quic_config_proto_msgTypes, + }.Build() + File_transport_internet_quic_config_proto = out.File + file_transport_internet_quic_config_proto_rawDesc = nil + file_transport_internet_quic_config_proto_goTypes = nil + file_transport_internet_quic_config_proto_depIdxs = nil +} diff --git a/transport/internet/quic/config.proto b/transport/internet/quic/config.proto new file mode 100644 index 000000000000..2393d6b57ac6 --- /dev/null +++ b/transport/internet/quic/config.proto @@ -0,0 +1,14 @@ +syntax = "proto3"; + +package xray.transport.internet.quic; +option csharp_namespace = "Xray.Transport.Internet.Quic"; +option go_package = "github.com/xtls/xray-core/transport/internet/quic"; +option java_package = "com.xray.transport.internet.quic"; +option java_multiple_files = true; + +message Config { +// string key = 1; +// xray.common.protocol.SecurityConfig security = 2; +// xray.common.serial.TypedMessage header = 3; + bool fec = 4; +} \ No newline at end of file From 8882937bce8f2d658a3759acaa33161235102479 Mon Sep 17 00:00:00 2001 From: yuhan6665 <1588741+yuhan6665@users.noreply.github.com> Date: Sun, 12 Jan 2025 14:49:34 -0500 Subject: [PATCH 2/2] Add Datagram transport --- infra/conf/transport_internet.go | 32 +++++- main/distro/all/all.go | 1 + transport/internet/quic/conn.go | 61 +++++++++++ transport/internet/quic/dialer.go | 154 +++++++++++++++++++++++++++ transport/internet/quic/hub.go | 113 ++++++++++++++++++++ transport/internet/quic/quic.go | 17 +++ transport/internet/quic/quic_test.go | 92 ++++++++++++++++ 7 files changed, 468 insertions(+), 2 deletions(-) create mode 100644 transport/internet/quic/conn.go create mode 100644 transport/internet/quic/dialer.go create mode 100644 transport/internet/quic/hub.go create mode 100644 transport/internet/quic/quic.go create mode 100644 transport/internet/quic/quic_test.go diff --git a/infra/conf/transport_internet.go b/infra/conf/transport_internet.go index cb5465373ab3..b95fbfc8ea01 100644 --- a/infra/conf/transport_internet.go +++ b/infra/conf/transport_internet.go @@ -18,6 +18,7 @@ import ( "github.com/xtls/xray-core/transport/internet" "github.com/xtls/xray-core/transport/internet/httpupgrade" "github.com/xtls/xray-core/transport/internet/kcp" + "github.com/xtls/xray-core/transport/internet/quic" "github.com/xtls/xray-core/transport/internet/reality" "github.com/xtls/xray-core/transport/internet/splithttp" "github.com/xtls/xray-core/transport/internet/tcp" @@ -326,6 +327,22 @@ func (c *SplitHTTPConfig) Build() (proto.Message, error) { return config, nil } +type QUICConfig struct { + // Header json.RawMessage `json:"header"` + // Security string `json:"security"` + // Key string `json:"key"` + + Fec bool `json:"fec"` +} + +// Build implements Buildable. +func (c *QUICConfig) Build() (proto.Message, error) { + config := &quic.Config{ + Fec: c.Fec, + } + return config, nil +} + func readFileOrString(f string, s []string) ([]byte, error) { if len(f) > 0 { return filesystem.ReadFile(f) @@ -659,8 +676,8 @@ func (p TransportProtocol) Build() (string, error) { return "httpupgrade", nil case "h2", "h3", "http": return "", errors.PrintRemovedFeatureError("HTTP transport (without header padding, etc.)", "XHTTP stream-one H2 & H3") - case "quic": - return "", errors.PrintRemovedFeatureError("QUIC transport (without web service, etc.)", "XHTTP stream-one H3") + case "quic", "datagram": + return "quic", nil default: return "", errors.New("Config: unknown transport protocol: ", p) } @@ -793,6 +810,7 @@ type StreamConfig struct { XHTTPSettings *SplitHTTPConfig `json:"xhttpSettings"` SplitHTTPSettings *SplitHTTPConfig `json:"splithttpSettings"` KCPSettings *KCPConfig `json:"kcpSettings"` + QUICSettings *QUICConfig `json:"quicSettings"` GRPCSettings *GRPCConfig `json:"grpcSettings"` WSSettings *WebSocketConfig `json:"wsSettings"` HTTPUPGRADESettings *HttpUpgradeConfig `json:"httpupgradeSettings"` @@ -884,6 +902,16 @@ func (c *StreamConfig) Build() (*internet.StreamConfig, error) { Settings: serial.ToTypedMessage(ts), }) } + if c.QUICSettings != nil { + qs, err := c.QUICSettings.Build() + if err != nil { + return nil, errors.New("Failed to build QUIC config").Base(err) + } + config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{ + ProtocolName: "quic", + Settings: serial.ToTypedMessage(qs), + }) + } if c.GRPCSettings != nil { gs, err := c.GRPCSettings.Build() if err != nil { diff --git a/main/distro/all/all.go b/main/distro/all/all.go index 198abb3fd2c2..e3b982d74f8b 100644 --- a/main/distro/all/all.go +++ b/main/distro/all/all.go @@ -53,6 +53,7 @@ import ( _ "github.com/xtls/xray-core/transport/internet/grpc" _ "github.com/xtls/xray-core/transport/internet/httpupgrade" _ "github.com/xtls/xray-core/transport/internet/kcp" + _ "github.com/xtls/xray-core/transport/internet/quic" _ "github.com/xtls/xray-core/transport/internet/reality" _ "github.com/xtls/xray-core/transport/internet/splithttp" _ "github.com/xtls/xray-core/transport/internet/tcp" diff --git a/transport/internet/quic/conn.go b/transport/internet/quic/conn.go new file mode 100644 index 000000000000..b82dcff68694 --- /dev/null +++ b/transport/internet/quic/conn.go @@ -0,0 +1,61 @@ +package quic + +import ( + "context" + "time" + + "github.com/xtls/quic-go" + "github.com/xtls/xray-core/common/buf" + "github.com/xtls/xray-core/common/net" +) + +type interConn struct { + ctx context.Context + quicConn quic.Connection + local net.Addr + remote net.Addr +} + +func (c *interConn) Read(b []byte) (int, error) { + received, e := c.quicConn.ReceiveDatagram(c.ctx) + if e != nil { + return 0, e + } + nBytes := copy(b, received[:]) + return nBytes, nil +} + +func (c *interConn) WriteMultiBuffer(mb buf.MultiBuffer) error { + mb = buf.Compact(mb) + mb, err := buf.WriteMultiBuffer(c, mb) + buf.ReleaseMulti(mb) + return err +} + +func (c *interConn) Write(b []byte) (int, error) { + return len(b), c.quicConn.SendDatagram(b) +} + +func (c *interConn) Close() error { + return nil +} + +func (c *interConn) LocalAddr() net.Addr { + return c.local +} + +func (c *interConn) RemoteAddr() net.Addr { + return c.remote +} + +func (c *interConn) SetDeadline(t time.Time) error { + return nil +} + +func (c *interConn) SetReadDeadline(t time.Time) error { + return nil +} + +func (c *interConn) SetWriteDeadline(t time.Time) error { + return nil +} diff --git a/transport/internet/quic/dialer.go b/transport/internet/quic/dialer.go new file mode 100644 index 000000000000..316fbbcd2328 --- /dev/null +++ b/transport/internet/quic/dialer.go @@ -0,0 +1,154 @@ +package quic + +import ( + "context" + "sync" + "time" + + "github.com/xtls/quic-go" + "github.com/xtls/xray-core/common" + "github.com/xtls/xray-core/common/errors" + "github.com/xtls/xray-core/common/net" + "github.com/xtls/xray-core/transport/internet" + "github.com/xtls/xray-core/transport/internet/stat" + "github.com/xtls/xray-core/transport/internet/tls" +) + +type connectionContext struct { + rawConn *net.UDPConn + conn quic.Connection +} + +type clientConnections struct { + access sync.Mutex + conns map[net.Destination][]*connectionContext + // cleanup *task.Periodic +} + +func isActive(s quic.Connection) bool { + select { + case <-s.Context().Done(): + return false + default: + return true + } +} + +func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (stat.Connection, error) { + tlsConfig := tls.ConfigFromStreamSettings(streamSettings) + if tlsConfig == nil { + tlsConfig = &tls.Config{ + ServerName: internalDomain, + AllowInsecure: true, + } + } + + var destAddr *net.UDPAddr + if dest.Address.Family().IsIP() { + destAddr = &net.UDPAddr{ + IP: dest.Address.IP(), + Port: int(dest.Port), + } + } else { + dialerIp := internet.DestIpAddress() + if dialerIp != nil { + destAddr = &net.UDPAddr{ + IP: dialerIp, + Port: int(dest.Port), + } + errors.LogInfo(ctx, "quic Dial use dialer dest addr: ", destAddr) + } else { + addr, err := net.ResolveUDPAddr("udp", dest.NetAddr()) + if err != nil { + return nil, err + } + destAddr = addr + } + } + + config := streamSettings.ProtocolSettings.(*Config) + + return client.openConnection(ctx, destAddr, config, tlsConfig, streamSettings.SocketSettings) +} + +func (s *clientConnections) openConnection(ctx context.Context, destAddr net.Addr, config *Config, tlsConfig *tls.Config, sockopt *internet.SocketConfig) (stat.Connection, error) { + s.access.Lock() + defer s.access.Unlock() + + if s.conns == nil { + s.conns = make(map[net.Destination][]*connectionContext) + } + + dest := net.DestinationFromAddr(destAddr) + + var conns []*connectionContext + if s, found := s.conns[dest]; found { + conns = s + } + + if len(conns) > 0 { + s := conns[len(conns)-1] + if isActive(s.conn) { + return &interConn{ + ctx: ctx, + quicConn: s.conn, + local: s.conn.LocalAddr(), + remote: destAddr, + }, nil + } else { + errors.LogInfo(ctx, "current quic connection is not active!") + } + } + + errors.LogInfo(ctx, "dialing quic to ", dest) + rawConn, err := internet.DialSystem(ctx, dest, sockopt) + if err != nil { + return nil, errors.New("failed to dial to dest: ", err).AtWarning().Base(err) + } + + quicConfig := &quic.Config{ + KeepAlivePeriod: 0, + HandshakeIdleTimeout: time.Second * 8, + MaxIdleTimeout: time.Second * 300, + EnableDatagrams: true, + } + + var udpConn *net.UDPConn + switch conn := rawConn.(type) { + case *net.UDPConn: + udpConn = conn + case *internet.PacketConnWrapper: + udpConn = conn.Conn.(*net.UDPConn) + default: + rawConn.Close() + return nil, errors.New("QUIC with sockopt is unsupported").AtWarning() + } + + tr := quic.Transport{ + ConnectionIDLength: 12, + Conn: udpConn, + } + conn, err := tr.Dial(context.Background(), destAddr, tlsConfig.GetTLSConfig(tls.WithDestination(dest)), quicConfig) + if err != nil { + udpConn.Close() + return nil, err + } + + context := &connectionContext{ + conn: conn, + rawConn: udpConn, + } + s.conns[dest] = append(conns, context) + return &interConn{ + ctx: ctx, + quicConn: context.conn, + local: context.conn.LocalAddr(), + remote: destAddr, + }, nil +} + +var client clientConnections + +func init() { + common.Must(internet.RegisterTransportDialer(protocolName, Dial)) +} diff --git a/transport/internet/quic/hub.go b/transport/internet/quic/hub.go new file mode 100644 index 000000000000..6d5cec935475 --- /dev/null +++ b/transport/internet/quic/hub.go @@ -0,0 +1,113 @@ +package quic + +import ( + "context" + "time" + + "github.com/xtls/quic-go" + "github.com/xtls/xray-core/common" + "github.com/xtls/xray-core/common/errors" + "github.com/xtls/xray-core/common/net" + "github.com/xtls/xray-core/common/protocol/tls/cert" + "github.com/xtls/xray-core/common/signal/done" + "github.com/xtls/xray-core/transport/internet" + "github.com/xtls/xray-core/transport/internet/tls" +) + +// Listener is an internet.Listener that listens for TCP connections. +type Listener struct { + rawConn *net.UDPConn + listener *quic.Listener + done *done.Instance + addConn internet.ConnHandler +} + +func (l *Listener) keepAccepting(ctx context.Context) { + for { + conn, err := l.listener.Accept(context.Background()) + if err != nil { + errors.LogInfoInner(context.Background(), err, "failed to accept QUIC connection") + if l.done.Done() { + break + } + time.Sleep(time.Second) + continue + } + l.addConn(&interConn{ + ctx: ctx, + quicConn: conn, + local: conn.LocalAddr(), + remote: conn.RemoteAddr(), + }) + } +} + +// Addr implements internet.Listener.Addr. +func (l *Listener) Addr() net.Addr { + return l.listener.Addr() +} + +// Close implements internet.Listener.Close. +func (l *Listener) Close() error { + l.done.Close() + l.listener.Close() + l.rawConn.Close() + return nil +} + +// Listen creates a new Listener based on configurations. +func Listen(ctx context.Context, address net.Address, port net.Port, streamSettings *internet.MemoryStreamConfig, handler internet.ConnHandler) (internet.Listener, error) { + if address.Family().IsDomain() { + return nil, errors.New("domain address is not allows for listening quic") + } + + tlsConfig := tls.ConfigFromStreamSettings(streamSettings) + if tlsConfig == nil { + tlsConfig = &tls.Config{ + Certificate: []*tls.Certificate{tls.ParseCertificate(cert.MustGenerate(nil, cert.DNSNames(internalDomain), cert.CommonName(internalDomain)))}, + } + } + + //config := streamSettings.ProtocolSettings.(*Config) + rawConn, err := internet.ListenSystemPacket(context.Background(), &net.UDPAddr{ + IP: address.IP(), + Port: int(port), + }, streamSettings.SocketSettings) + if err != nil { + return nil, err + } + + quicConfig := &quic.Config{ + KeepAlivePeriod: 0, + HandshakeIdleTimeout: time.Second * 8, + MaxIdleTimeout: time.Second * 300, + MaxIncomingStreams: 32, + MaxIncomingUniStreams: -1, + EnableDatagrams: true, + } + + tr := quic.Transport{ + ConnectionIDLength: 12, + Conn: rawConn.(*net.UDPConn), + } + qListener, err := tr.Listen(tlsConfig.GetTLSConfig(), quicConfig) + if err != nil { + rawConn.Close() + return nil, err + } + + listener := &Listener{ + done: done.New(), + rawConn: rawConn.(*net.UDPConn), + listener: qListener, + addConn: handler, + } + + go listener.keepAccepting(ctx) + + return listener, nil +} + +func init() { + common.Must(internet.RegisterTransportListener(protocolName, Listen)) +} diff --git a/transport/internet/quic/quic.go b/transport/internet/quic/quic.go new file mode 100644 index 000000000000..db523ab94e87 --- /dev/null +++ b/transport/internet/quic/quic.go @@ -0,0 +1,17 @@ +package quic + +import ( + "github.com/xtls/xray-core/common" + "github.com/xtls/xray-core/transport/internet" +) + +const ( + protocolName = "quic" + internalDomain = "quic.internal.example.com" +) + +func init() { + common.Must(internet.RegisterProtocolConfigCreator(protocolName, func() interface{} { + return new(Config) + })) +} diff --git a/transport/internet/quic/quic_test.go b/transport/internet/quic/quic_test.go new file mode 100644 index 000000000000..d7ddd592664f --- /dev/null +++ b/transport/internet/quic/quic_test.go @@ -0,0 +1,92 @@ +package quic_test + +import ( + "context" + "crypto/rand" + "testing" + "time" + + "github.com/google/go-cmp/cmp" + "github.com/xtls/xray-core/common" + "github.com/xtls/xray-core/common/buf" + "github.com/xtls/xray-core/common/net" + "github.com/xtls/xray-core/common/protocol/tls/cert" + "github.com/xtls/xray-core/testing/servers/udp" + "github.com/xtls/xray-core/transport/internet" + "github.com/xtls/xray-core/transport/internet/quic" + "github.com/xtls/xray-core/transport/internet/stat" + "github.com/xtls/xray-core/transport/internet/tls" +) + +func TestQuicConnection(t *testing.T) { + port := udp.PickPort() + + listener, err := quic.Listen(context.Background(), net.LocalHostIP, port, &internet.MemoryStreamConfig{ + ProtocolName: "quic", + ProtocolSettings: &quic.Config{}, + SecurityType: "tls", + SecuritySettings: &tls.Config{ + Certificate: []*tls.Certificate{ + tls.ParseCertificate( + cert.MustGenerate(nil, + cert.DNSNames("www.example.com"), + ), + ), + }, + }, + }, func(conn stat.Connection) { + go func() { + defer conn.Close() + + b := buf.New() + defer b.Release() + + for { + b.Clear() + if _, err := b.ReadFrom(conn); err != nil { + return + } + common.Must2(conn.Write(b.Bytes())) + } + }() + }) + common.Must(err) + + defer listener.Close() + + time.Sleep(time.Second) + + dctx := context.Background() + conn, err := quic.Dial(dctx, net.TCPDestination(net.LocalHostIP, port), &internet.MemoryStreamConfig{ + ProtocolName: "quic", + ProtocolSettings: &quic.Config{}, + SecurityType: "tls", + SecuritySettings: &tls.Config{ + ServerName: "www.example.com", + AllowInsecure: true, + }, + }) + common.Must(err) + defer conn.Close() + + const N = 1024 + b1 := make([]byte, N) + common.Must2(rand.Read(b1)) + b2 := buf.New() + + common.Must2(conn.Write(b1)) + + b2.Clear() + common.Must2(b2.ReadFullFrom(conn, N)) + if r := cmp.Diff(b2.Bytes(), b1); r != "" { + t.Error(r) + } + + common.Must2(conn.Write(b1)) + + b2.Clear() + common.Must2(b2.ReadFullFrom(conn, N)) + if r := cmp.Diff(b2.Bytes(), b1); r != "" { + t.Error(r) + } +}