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