mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-11-04 17:27:23 -05:00
224 lines
4.8 KiB
Go
224 lines
4.8 KiB
Go
package quic_test
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common"
|
|
"github.com/v2fly/v2ray-core/v4/common/buf"
|
|
"github.com/v2fly/v2ray-core/v4/common/net"
|
|
"github.com/v2fly/v2ray-core/v4/common/protocol"
|
|
"github.com/v2fly/v2ray-core/v4/common/protocol/tls/cert"
|
|
"github.com/v2fly/v2ray-core/v4/common/serial"
|
|
"github.com/v2fly/v2ray-core/v4/testing/servers/udp"
|
|
"github.com/v2fly/v2ray-core/v4/transport/internet"
|
|
"github.com/v2fly/v2ray-core/v4/transport/internet/headers/wireguard"
|
|
"github.com/v2fly/v2ray-core/v4/transport/internet/quic"
|
|
"github.com/v2fly/v2ray-core/v4/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.v2fly.org"),
|
|
),
|
|
),
|
|
},
|
|
},
|
|
}, 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
|
|
}
|
|
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.v2fly.org",
|
|
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)
|
|
}
|
|
}
|
|
|
|
func TestQuicConnectionWithoutTLS(t *testing.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
|
|
}
|
|
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{},
|
|
})
|
|
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)
|
|
}
|
|
}
|
|
|
|
func TestQuicConnectionAuthHeader(t *testing.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
|
|
}
|
|
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{
|
|
Header: serial.ToTypedMessage(&wireguard.WireguardConfig{}),
|
|
Key: "abcd",
|
|
Security: &protocol.SecurityConfig{
|
|
Type: protocol.SecurityType_AES128_GCM,
|
|
},
|
|
},
|
|
})
|
|
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)
|
|
}
|
|
}
|