box_test.go 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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, testPingPongWithConn(t, testPort, dialTCP))
  53. require.NoError(t, testLargeDataWithConn(t, testPort, dialTCP))
  54. }
  55. func testSuitQUIC(t *testing.T, clientPort uint16, testPort uint16) {
  56. dialer := socks.NewClient(N.SystemDialer, M.ParseSocksaddrHostPort("127.0.0.1", clientPort), socks.Version5, "", "")
  57. dialTCP := func() (net.Conn, error) {
  58. return dialer.DialContext(context.Background(), "tcp", M.ParseSocksaddrHostPort("127.0.0.1", testPort))
  59. }
  60. dialUDP := func() (net.PacketConn, error) {
  61. return dialer.ListenPacket(context.Background(), M.ParseSocksaddrHostPort("127.0.0.1", testPort))
  62. }
  63. require.NoError(t, testPingPongWithConn(t, testPort, dialTCP))
  64. require.NoError(t, testPingPongWithPacketConn(t, testPort, dialUDP))
  65. }
  66. func testSuitWg(t *testing.T, clientPort uint16, testPort uint16) {
  67. dialer := socks.NewClient(N.SystemDialer, M.ParseSocksaddrHostPort("127.0.0.1", clientPort), socks.Version5, "", "")
  68. dialTCP := func() (net.Conn, error) {
  69. return dialer.DialContext(context.Background(), "tcp", M.ParseSocksaddrHostPort("10.0.0.1", testPort))
  70. }
  71. dialUDP := func() (net.PacketConn, error) {
  72. conn, err := dialer.DialContext(context.Background(), "udp", M.ParseSocksaddrHostPort("10.0.0.1", testPort))
  73. if err != nil {
  74. return nil, err
  75. }
  76. return bufio.NewUnbindPacketConn(conn), nil
  77. }
  78. require.NoError(t, testLargeDataWithConn(t, testPort, dialTCP))
  79. require.NoError(t, testLargeDataWithPacketConn(t, testPort, dialUDP))
  80. // require.NoError(t, testPingPongWithConn(t, testPort, dialTCP))
  81. // require.NoError(t, testPingPongWithPacketConn(t, testPort, dialUDP))
  82. }