quic_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package quic_test
  2. import (
  3. "context"
  4. "crypto/rand"
  5. "testing"
  6. "time"
  7. "github.com/google/go-cmp/cmp"
  8. "github.com/v2fly/v2ray-core/v4/common"
  9. "github.com/v2fly/v2ray-core/v4/common/buf"
  10. "github.com/v2fly/v2ray-core/v4/common/net"
  11. "github.com/v2fly/v2ray-core/v4/common/protocol"
  12. "github.com/v2fly/v2ray-core/v4/common/protocol/tls/cert"
  13. "github.com/v2fly/v2ray-core/v4/common/serial"
  14. "github.com/v2fly/v2ray-core/v4/testing/servers/udp"
  15. "github.com/v2fly/v2ray-core/v4/transport/internet"
  16. "github.com/v2fly/v2ray-core/v4/transport/internet/headers/wireguard"
  17. "github.com/v2fly/v2ray-core/v4/transport/internet/quic"
  18. "github.com/v2fly/v2ray-core/v4/transport/internet/tls"
  19. )
  20. func TestQuicConnection(t *testing.T) {
  21. port := udp.PickPort()
  22. listener, err := quic.Listen(context.Background(), net.LocalHostIP, port, &internet.MemoryStreamConfig{
  23. ProtocolName: "quic",
  24. ProtocolSettings: &quic.Config{},
  25. SecurityType: "tls",
  26. SecuritySettings: &tls.Config{
  27. Certificate: []*tls.Certificate{
  28. tls.ParseCertificate(
  29. cert.MustGenerate(nil,
  30. cert.DNSNames("www.v2fly.org"),
  31. ),
  32. ),
  33. },
  34. },
  35. }, func(conn internet.Connection) {
  36. go func() {
  37. defer conn.Close()
  38. b := buf.New()
  39. defer b.Release()
  40. for {
  41. b.Clear()
  42. if _, err := b.ReadFrom(conn); err != nil {
  43. return
  44. }
  45. common.Must2(conn.Write(b.Bytes()))
  46. }
  47. }()
  48. })
  49. common.Must(err)
  50. defer listener.Close()
  51. time.Sleep(time.Second)
  52. dctx := context.Background()
  53. conn, err := quic.Dial(dctx, net.TCPDestination(net.LocalHostIP, port), &internet.MemoryStreamConfig{
  54. ProtocolName: "quic",
  55. ProtocolSettings: &quic.Config{},
  56. SecurityType: "tls",
  57. SecuritySettings: &tls.Config{
  58. ServerName: "www.v2fly.org",
  59. AllowInsecure: true,
  60. },
  61. })
  62. common.Must(err)
  63. defer conn.Close()
  64. const N = 1024
  65. b1 := make([]byte, N)
  66. common.Must2(rand.Read(b1))
  67. b2 := buf.New()
  68. common.Must2(conn.Write(b1))
  69. b2.Clear()
  70. common.Must2(b2.ReadFullFrom(conn, N))
  71. if r := cmp.Diff(b2.Bytes(), b1); r != "" {
  72. t.Error(r)
  73. }
  74. common.Must2(conn.Write(b1))
  75. b2.Clear()
  76. common.Must2(b2.ReadFullFrom(conn, N))
  77. if r := cmp.Diff(b2.Bytes(), b1); r != "" {
  78. t.Error(r)
  79. }
  80. }
  81. func TestQuicConnectionWithoutTLS(t *testing.T) {
  82. port := udp.PickPort()
  83. listener, err := quic.Listen(context.Background(), net.LocalHostIP, port, &internet.MemoryStreamConfig{
  84. ProtocolName: "quic",
  85. ProtocolSettings: &quic.Config{},
  86. }, func(conn internet.Connection) {
  87. go func() {
  88. defer conn.Close()
  89. b := buf.New()
  90. defer b.Release()
  91. for {
  92. b.Clear()
  93. if _, err := b.ReadFrom(conn); err != nil {
  94. return
  95. }
  96. common.Must2(conn.Write(b.Bytes()))
  97. }
  98. }()
  99. })
  100. common.Must(err)
  101. defer listener.Close()
  102. time.Sleep(time.Second)
  103. dctx := context.Background()
  104. conn, err := quic.Dial(dctx, net.TCPDestination(net.LocalHostIP, port), &internet.MemoryStreamConfig{
  105. ProtocolName: "quic",
  106. ProtocolSettings: &quic.Config{},
  107. })
  108. common.Must(err)
  109. defer conn.Close()
  110. const N = 1024
  111. b1 := make([]byte, N)
  112. common.Must2(rand.Read(b1))
  113. b2 := buf.New()
  114. common.Must2(conn.Write(b1))
  115. b2.Clear()
  116. common.Must2(b2.ReadFullFrom(conn, N))
  117. if r := cmp.Diff(b2.Bytes(), b1); r != "" {
  118. t.Error(r)
  119. }
  120. common.Must2(conn.Write(b1))
  121. b2.Clear()
  122. common.Must2(b2.ReadFullFrom(conn, N))
  123. if r := cmp.Diff(b2.Bytes(), b1); r != "" {
  124. t.Error(r)
  125. }
  126. }
  127. func TestQuicConnectionAuthHeader(t *testing.T) {
  128. port := udp.PickPort()
  129. listener, err := quic.Listen(context.Background(), net.LocalHostIP, port, &internet.MemoryStreamConfig{
  130. ProtocolName: "quic",
  131. ProtocolSettings: &quic.Config{
  132. Header: serial.ToTypedMessage(&wireguard.WireguardConfig{}),
  133. Key: "abcd",
  134. Security: &protocol.SecurityConfig{
  135. Type: protocol.SecurityType_AES128_GCM,
  136. },
  137. },
  138. }, func(conn internet.Connection) {
  139. go func() {
  140. defer conn.Close()
  141. b := buf.New()
  142. defer b.Release()
  143. for {
  144. b.Clear()
  145. if _, err := b.ReadFrom(conn); err != nil {
  146. return
  147. }
  148. common.Must2(conn.Write(b.Bytes()))
  149. }
  150. }()
  151. })
  152. common.Must(err)
  153. defer listener.Close()
  154. time.Sleep(time.Second)
  155. dctx := context.Background()
  156. conn, err := quic.Dial(dctx, net.TCPDestination(net.LocalHostIP, port), &internet.MemoryStreamConfig{
  157. ProtocolName: "quic",
  158. ProtocolSettings: &quic.Config{
  159. Header: serial.ToTypedMessage(&wireguard.WireguardConfig{}),
  160. Key: "abcd",
  161. Security: &protocol.SecurityConfig{
  162. Type: protocol.SecurityType_AES128_GCM,
  163. },
  164. },
  165. })
  166. common.Must(err)
  167. defer conn.Close()
  168. const N = 1024
  169. b1 := make([]byte, N)
  170. common.Must2(rand.Read(b1))
  171. b2 := buf.New()
  172. common.Must2(conn.Write(b1))
  173. b2.Clear()
  174. common.Must2(b2.ReadFullFrom(conn, N))
  175. if r := cmp.Diff(b2.Bytes(), b1); r != "" {
  176. t.Error(r)
  177. }
  178. common.Must2(conn.Write(b1))
  179. b2.Clear()
  180. common.Must2(b2.ReadFullFrom(conn, N))
  181. if r := cmp.Diff(b2.Bytes(), b1); r != "" {
  182. t.Error(r)
  183. }
  184. }