1
0

box_test.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package main
  2. import (
  3. "context"
  4. "crypto/tls"
  5. "io"
  6. "net"
  7. "net/http"
  8. "testing"
  9. "time"
  10. "github.com/sagernet/quic-go"
  11. "github.com/sagernet/quic-go/http3"
  12. "github.com/sagernet/sing-box"
  13. C "github.com/sagernet/sing-box/constant"
  14. "github.com/sagernet/sing-box/option"
  15. "github.com/sagernet/sing/common/bufio"
  16. "github.com/sagernet/sing/common/debug"
  17. M "github.com/sagernet/sing/common/metadata"
  18. N "github.com/sagernet/sing/common/network"
  19. "github.com/sagernet/sing/protocol/socks"
  20. "github.com/stretchr/testify/require"
  21. "go.uber.org/goleak"
  22. )
  23. func TestMain(m *testing.M) {
  24. goleak.VerifyTestMain(m)
  25. }
  26. func startInstance(t *testing.T, options option.Options) *box.Box {
  27. if debug.Enabled {
  28. options.Log = &option.LogOptions{
  29. Level: "trace",
  30. }
  31. } else {
  32. options.Log = &option.LogOptions{
  33. Level: "warning",
  34. }
  35. }
  36. // ctx := context.Background()
  37. ctx, cancel := context.WithCancel(context.Background())
  38. var instance *box.Box
  39. var err error
  40. for retry := 0; retry < 3; retry++ {
  41. instance, err = box.New(box.Options{
  42. Context: ctx,
  43. Options: options,
  44. })
  45. require.NoError(t, err)
  46. err = instance.Start()
  47. if err != nil {
  48. time.Sleep(time.Second)
  49. continue
  50. }
  51. break
  52. }
  53. require.NoError(t, err)
  54. t.Cleanup(func() {
  55. instance.Close()
  56. cancel()
  57. })
  58. return instance
  59. }
  60. func testSuit(t *testing.T, clientPort uint16, testPort uint16) {
  61. dialer := socks.NewClient(N.SystemDialer, M.ParseSocksaddrHostPort("127.0.0.1", clientPort), socks.Version5, "", "")
  62. dialTCP := func() (net.Conn, error) {
  63. return dialer.DialContext(context.Background(), "tcp", M.ParseSocksaddrHostPort("127.0.0.1", testPort))
  64. }
  65. dialUDP := func() (net.PacketConn, error) {
  66. return dialer.ListenPacket(context.Background(), M.ParseSocksaddrHostPort("127.0.0.1", testPort))
  67. }
  68. require.NoError(t, testPingPongWithConn(t, testPort, dialTCP))
  69. require.NoError(t, testPingPongWithPacketConn(t, testPort, dialUDP))
  70. require.NoError(t, testLargeDataWithConn(t, testPort, dialTCP))
  71. require.NoError(t, testLargeDataWithPacketConn(t, testPort, dialUDP))
  72. // require.NoError(t, testPacketConnTimeout(t, dialUDP))
  73. }
  74. func testQUIC(t *testing.T, clientPort uint16) {
  75. dialer := socks.NewClient(N.SystemDialer, M.ParseSocksaddrHostPort("127.0.0.1", clientPort), socks.Version5, "", "")
  76. client := &http.Client{
  77. Transport: &http3.RoundTripper{
  78. Dial: func(ctx context.Context, addr string, tlsCfg *tls.Config, cfg *quic.Config) (quic.EarlyConnection, error) {
  79. destination := M.ParseSocksaddr(addr)
  80. udpConn, err := dialer.DialContext(ctx, N.NetworkUDP, destination)
  81. if err != nil {
  82. return nil, err
  83. }
  84. return quic.DialEarly(ctx, udpConn.(net.PacketConn), destination, tlsCfg, cfg)
  85. },
  86. },
  87. }
  88. response, err := client.Get("https://cloudflare.com/cdn-cgi/trace")
  89. require.NoError(t, err)
  90. require.Equal(t, http.StatusOK, response.StatusCode)
  91. content, err := io.ReadAll(response.Body)
  92. require.NoError(t, err)
  93. println(string(content))
  94. }
  95. func testSuitLargeUDP(t *testing.T, clientPort uint16, testPort uint16) {
  96. dialer := socks.NewClient(N.SystemDialer, M.ParseSocksaddrHostPort("127.0.0.1", clientPort), socks.Version5, "", "")
  97. dialTCP := func() (net.Conn, error) {
  98. return dialer.DialContext(context.Background(), "tcp", M.ParseSocksaddrHostPort("127.0.0.1", testPort))
  99. }
  100. dialUDP := func() (net.PacketConn, error) {
  101. return dialer.ListenPacket(context.Background(), M.ParseSocksaddrHostPort("127.0.0.1", testPort))
  102. }
  103. require.NoError(t, testPingPongWithConn(t, testPort, dialTCP))
  104. require.NoError(t, testPingPongWithPacketConn(t, testPort, dialUDP))
  105. require.NoError(t, testLargeDataWithConn(t, testPort, dialTCP))
  106. require.NoError(t, testLargeDataWithPacketConn(t, testPort, dialUDP))
  107. require.NoError(t, testLargeDataWithPacketConnSize(t, testPort, 5000, dialUDP))
  108. }
  109. func testTCP(t *testing.T, clientPort uint16, testPort uint16) {
  110. dialer := socks.NewClient(N.SystemDialer, M.ParseSocksaddrHostPort("127.0.0.1", clientPort), socks.Version5, "", "")
  111. dialTCP := func() (net.Conn, error) {
  112. return dialer.DialContext(context.Background(), "tcp", M.ParseSocksaddrHostPort("127.0.0.1", testPort))
  113. }
  114. require.NoError(t, testPingPongWithConn(t, testPort, dialTCP))
  115. require.NoError(t, testLargeDataWithConn(t, testPort, dialTCP))
  116. }
  117. func testSuitSimple(t *testing.T, clientPort uint16, testPort uint16) {
  118. dialer := socks.NewClient(N.SystemDialer, M.ParseSocksaddrHostPort("127.0.0.1", clientPort), socks.Version5, "", "")
  119. dialTCP := func() (net.Conn, error) {
  120. return dialer.DialContext(context.Background(), "tcp", M.ParseSocksaddrHostPort("127.0.0.1", testPort))
  121. }
  122. dialUDP := func() (net.PacketConn, error) {
  123. return dialer.ListenPacket(context.Background(), M.ParseSocksaddrHostPort("127.0.0.1", testPort))
  124. }
  125. require.NoError(t, testPingPongWithConn(t, testPort, dialTCP))
  126. require.NoError(t, testPingPongWithPacketConn(t, testPort, dialUDP))
  127. require.NoError(t, testPingPongWithConn(t, testPort, dialTCP))
  128. require.NoError(t, testPingPongWithPacketConn(t, testPort, dialUDP))
  129. }
  130. func testSuitSimple1(t *testing.T, clientPort uint16, testPort uint16) {
  131. dialer := socks.NewClient(N.SystemDialer, M.ParseSocksaddrHostPort("127.0.0.1", clientPort), socks.Version5, "", "")
  132. dialTCP := func() (net.Conn, error) {
  133. return dialer.DialContext(context.Background(), "tcp", M.ParseSocksaddrHostPort("127.0.0.1", testPort))
  134. }
  135. dialUDP := func() (net.PacketConn, error) {
  136. return dialer.ListenPacket(context.Background(), M.ParseSocksaddrHostPort("127.0.0.1", testPort))
  137. }
  138. require.NoError(t, testPingPongWithConn(t, testPort, dialTCP))
  139. if !C.IsDarwin {
  140. require.NoError(t, testPingPongWithPacketConn(t, testPort, dialUDP))
  141. }
  142. require.NoError(t, testPingPongWithConn(t, testPort, dialTCP))
  143. if !C.IsDarwin {
  144. require.NoError(t, testLargeDataWithPacketConn(t, testPort, dialUDP))
  145. }
  146. }
  147. func testSuitWg(t *testing.T, clientPort uint16, testPort uint16) {
  148. dialer := socks.NewClient(N.SystemDialer, M.ParseSocksaddrHostPort("127.0.0.1", clientPort), socks.Version5, "", "")
  149. dialTCP := func() (net.Conn, error) {
  150. return dialer.DialContext(context.Background(), "tcp", M.ParseSocksaddrHostPort("10.0.0.1", testPort))
  151. }
  152. dialUDP := func() (net.PacketConn, error) {
  153. conn, err := dialer.DialContext(context.Background(), "udp", M.ParseSocksaddrHostPort("10.0.0.1", testPort))
  154. if err != nil {
  155. return nil, err
  156. }
  157. return bufio.NewUnbindPacketConn(conn), nil
  158. }
  159. require.NoError(t, testPingPongWithConn(t, testPort, dialTCP))
  160. require.NoError(t, testPingPongWithPacketConn(t, testPort, dialUDP))
  161. require.NoError(t, testLargeDataWithConn(t, testPort, dialTCP))
  162. require.NoError(t, testLargeDataWithPacketConn(t, testPort, dialUDP))
  163. }