| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- package quic_test
- import (
- "context"
- "crypto/rand"
- "testing"
- "time"
- "v2ray.com/core/common"
- "v2ray.com/core/common/buf"
- "v2ray.com/core/common/net"
- "v2ray.com/core/common/protocol"
- "v2ray.com/core/common/protocol/tls/cert"
- "v2ray.com/core/common/serial"
- "v2ray.com/core/testing/servers/udp"
- "v2ray.com/core/transport/internet"
- "v2ray.com/core/transport/internet/headers/wireguard"
- "v2ray.com/core/transport/internet/quic"
- "v2ray.com/core/transport/internet/tls"
- . "v2ray.com/ext/assert"
- )
- func TestQuicConnection(t *testing.T) {
- assert := With(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.v2ray.com"), cert.CommonName("www.v2ray.com")))},
- },
- }, func(conn internet.Connection) {
- go func() {
- defer conn.Close()
- b := buf.New()
- defer b.Release()
- for {
- b.Clear()
- if _, err := b.ReadFrom(conn); err != nil {
- return
- }
- nBytes, err := conn.Write(b.Bytes())
- assert(err, IsNil)
- assert(int32(nBytes), Equals, b.Len())
- }
- }()
- })
- assert(err, IsNil)
- 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.v2ray.com",
- AllowInsecure: true,
- },
- })
- assert(err, IsNil)
- defer conn.Close()
- const N = 1024
- b1 := make([]byte, N)
- common.Must2(rand.Read(b1))
- b2 := buf.New()
- nBytes, err := conn.Write(b1)
- assert(nBytes, Equals, N)
- assert(err, IsNil)
- b2.Clear()
- common.Must2(b2.ReadFullFrom(conn, N))
- assert(b2.Bytes(), Equals, b1)
- nBytes, err = conn.Write(b1)
- assert(nBytes, Equals, N)
- assert(err, IsNil)
- b2.Clear()
- common.Must2(b2.ReadFullFrom(conn, N))
- assert(b2.Bytes(), Equals, b1)
- }
- func TestQuicConnectionWithoutTLS(t *testing.T) {
- assert := With(t)
- port := udp.PickPort()
- listener, err := quic.Listen(context.Background(), net.LocalHostIP, port, &internet.MemoryStreamConfig{
- ProtocolName: "quic",
- ProtocolSettings: &quic.Config{},
- }, func(conn internet.Connection) {
- go func() {
- defer conn.Close()
- b := buf.New()
- defer b.Release()
- for {
- b.Clear()
- if _, err := b.ReadFrom(conn); err != nil {
- return
- }
- nBytes, err := conn.Write(b.Bytes())
- assert(err, IsNil)
- assert(int32(nBytes), Equals, b.Len())
- }
- }()
- })
- assert(err, IsNil)
- 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{},
- })
- assert(err, IsNil)
- defer conn.Close()
- const N = 1024
- b1 := make([]byte, N)
- common.Must2(rand.Read(b1))
- b2 := buf.New()
- nBytes, err := conn.Write(b1)
- assert(nBytes, Equals, N)
- assert(err, IsNil)
- b2.Clear()
- common.Must2(b2.ReadFullFrom(conn, N))
- assert(b2.Bytes(), Equals, b1)
- nBytes, err = conn.Write(b1)
- assert(nBytes, Equals, N)
- assert(err, IsNil)
- b2.Clear()
- common.Must2(b2.ReadFullFrom(conn, N))
- assert(b2.Bytes(), Equals, b1)
- }
- func TestQuicConnectionAuthHeader(t *testing.T) {
- assert := With(t)
- port := udp.PickPort()
- listener, err := quic.Listen(context.Background(), net.LocalHostIP, port, &internet.MemoryStreamConfig{
- ProtocolName: "quic",
- ProtocolSettings: &quic.Config{
- Header: serial.ToTypedMessage(&wireguard.WireguardConfig{}),
- Key: "abcd",
- Security: &protocol.SecurityConfig{
- Type: protocol.SecurityType_AES128_GCM,
- },
- },
- }, func(conn internet.Connection) {
- go func() {
- defer conn.Close()
- b := buf.New()
- defer b.Release()
- for {
- b.Clear()
- if _, err := b.ReadFrom(conn); err != nil {
- return
- }
- nBytes, err := conn.Write(b.Bytes())
- assert(err, IsNil)
- assert(int32(nBytes), Equals, b.Len())
- }
- }()
- })
- assert(err, IsNil)
- 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{
- Header: serial.ToTypedMessage(&wireguard.WireguardConfig{}),
- Key: "abcd",
- Security: &protocol.SecurityConfig{
- Type: protocol.SecurityType_AES128_GCM,
- },
- },
- })
- assert(err, IsNil)
- defer conn.Close()
- const N = 1024
- b1 := make([]byte, N)
- common.Must2(rand.Read(b1))
- b2 := buf.New()
- nBytes, err := conn.Write(b1)
- assert(nBytes, Equals, N)
- assert(err, IsNil)
- b2.Clear()
- common.Must2(b2.ReadFullFrom(conn, N))
- assert(b2.Bytes(), Equals, b1)
- nBytes, err = conn.Write(b1)
- assert(nBytes, Equals, N)
- assert(err, IsNil)
- b2.Clear()
- common.Must2(b2.ReadFullFrom(conn, N))
- assert(b2.Bytes(), Equals, b1)
- }
|