quic_test.go 5.0 KB

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