box_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package main
  2. import (
  3. "context"
  4. "net"
  5. "testing"
  6. "time"
  7. "github.com/sagernet/sing-box"
  8. "github.com/sagernet/sing-box/option"
  9. "github.com/sagernet/sing/common/bufio"
  10. M "github.com/sagernet/sing/common/metadata"
  11. N "github.com/sagernet/sing/common/network"
  12. "github.com/sagernet/sing/protocol/socks"
  13. "github.com/stretchr/testify/require"
  14. )
  15. func startInstance(t *testing.T, options option.Options) {
  16. var instance *box.Box
  17. var err error
  18. for retry := 0; retry < 3; retry++ {
  19. instance, err = box.New(context.Background(), options)
  20. require.NoError(t, err)
  21. err = instance.Start()
  22. if err != nil {
  23. time.Sleep(time.Second)
  24. continue
  25. }
  26. break
  27. }
  28. require.NoError(t, err)
  29. t.Cleanup(func() {
  30. instance.Close()
  31. })
  32. }
  33. func testSuit(t *testing.T, clientPort uint16, testPort uint16) {
  34. dialer := socks.NewClient(N.SystemDialer, M.ParseSocksaddrHostPort("127.0.0.1", clientPort), socks.Version5, "", "")
  35. dialTCP := func() (net.Conn, error) {
  36. return dialer.DialContext(context.Background(), "tcp", M.ParseSocksaddrHostPort("127.0.0.1", testPort))
  37. }
  38. dialUDP := func() (net.PacketConn, error) {
  39. return dialer.ListenPacket(context.Background(), M.ParseSocksaddrHostPort("127.0.0.1", testPort))
  40. }
  41. // require.NoError(t, testPingPongWithConn(t, testPort, dialTCP))
  42. // require.NoError(t, testPingPongWithPacketConn(t, testPort, dialUDP))
  43. require.NoError(t, testLargeDataWithConn(t, testPort, dialTCP))
  44. require.NoError(t, testLargeDataWithPacketConn(t, testPort, dialUDP))
  45. // require.NoError(t, testPacketConnTimeout(t, dialUDP))
  46. }
  47. func testTCP(t *testing.T, clientPort uint16, testPort uint16) {
  48. dialer := socks.NewClient(N.SystemDialer, M.ParseSocksaddrHostPort("127.0.0.1", clientPort), socks.Version5, "", "")
  49. dialTCP := func() (net.Conn, error) {
  50. return dialer.DialContext(context.Background(), "tcp", M.ParseSocksaddrHostPort("127.0.0.1", testPort))
  51. }
  52. require.NoError(t, testLargeDataWithConn(t, testPort, dialTCP))
  53. }
  54. func testSuitHy(t *testing.T, clientPort uint16, testPort uint16) {
  55. dialer := socks.NewClient(N.SystemDialer, M.ParseSocksaddrHostPort("127.0.0.1", clientPort), socks.Version5, "", "")
  56. dialTCP := func() (net.Conn, error) {
  57. return dialer.DialContext(context.Background(), "tcp", M.ParseSocksaddrHostPort("127.0.0.1", testPort))
  58. }
  59. dialUDP := func() (net.PacketConn, error) {
  60. return dialer.ListenPacket(context.Background(), M.ParseSocksaddrHostPort("127.0.0.1", testPort))
  61. }
  62. require.NoError(t, testPingPongWithConn(t, testPort, dialTCP))
  63. require.NoError(t, testPingPongWithPacketConn(t, testPort, dialUDP))
  64. }
  65. func testSuitWg(t *testing.T, clientPort uint16, testPort uint16) {
  66. dialer := socks.NewClient(N.SystemDialer, M.ParseSocksaddrHostPort("127.0.0.1", clientPort), socks.Version5, "", "")
  67. dialTCP := func() (net.Conn, error) {
  68. return dialer.DialContext(context.Background(), "tcp", M.ParseSocksaddrHostPort("10.0.0.1", testPort))
  69. }
  70. dialUDP := func() (net.PacketConn, error) {
  71. conn, err := dialer.DialContext(context.Background(), "udp", M.ParseSocksaddrHostPort("10.0.0.1", testPort))
  72. if err != nil {
  73. return nil, err
  74. }
  75. return bufio.NewUnbindPacketConn(conn), nil
  76. }
  77. require.NoError(t, testLargeDataWithConn(t, testPort, dialTCP))
  78. require.NoError(t, testLargeDataWithPacketConn(t, testPort, dialUDP))
  79. // require.NoError(t, testPingPongWithConn(t, testPort, dialTCP))
  80. // require.NoError(t, testPingPongWithPacketConn(t, testPort, dialUDP))
  81. }