quic_test.go 4.6 KB

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