wireguard_test.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package main
  2. import (
  3. "net/netip"
  4. "os"
  5. "testing"
  6. "time"
  7. C "github.com/sagernet/sing-box/constant"
  8. "github.com/sagernet/sing-box/option"
  9. )
  10. func TestWireGuard(t *testing.T) {
  11. startDockerContainer(t, DockerOptions{
  12. Image: ImageBoringTun,
  13. Cap: []string{"MKNOD", "NET_ADMIN", "NET_RAW"},
  14. Ports: []uint16{serverPort, testPort},
  15. Bind: map[string]string{
  16. "wireguard.conf": "/etc/wireguard/wg0.conf",
  17. },
  18. Cmd: []string{"wg0"},
  19. })
  20. time.Sleep(5 * time.Second)
  21. startInstance(t, option.Options{
  22. Inbounds: []option.Inbound{
  23. {
  24. Type: C.TypeMixed,
  25. MixedOptions: option.HTTPMixedInboundOptions{
  26. ListenOptions: option.ListenOptions{
  27. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  28. ListenPort: clientPort,
  29. },
  30. },
  31. },
  32. },
  33. Outbounds: []option.Outbound{
  34. {
  35. Type: C.TypeWireGuard,
  36. WireGuardOptions: option.WireGuardOutboundOptions{
  37. ServerOptions: option.ServerOptions{
  38. Server: "127.0.0.1",
  39. ServerPort: serverPort,
  40. },
  41. LocalAddress: []option.ListenPrefix{option.ListenPrefix(netip.MustParsePrefix("10.0.0.2/32"))},
  42. PrivateKey: "qGnwlkZljMxeECW8fbwAWdvgntnbK7B8UmMFl3zM0mk=",
  43. PeerPublicKey: "QsdcBm+oJw2oNv0cIFXLIq1E850lgTBonup4qnKEQBg=",
  44. },
  45. },
  46. },
  47. })
  48. testSuitWg(t, clientPort, testPort)
  49. }
  50. func TestWireGuardSystem(t *testing.T) {
  51. if os.Getuid() != 0 {
  52. t.Skip("requires root")
  53. }
  54. startDockerContainer(t, DockerOptions{
  55. Image: ImageBoringTun,
  56. Cap: []string{"MKNOD", "NET_ADMIN", "NET_RAW"},
  57. Ports: []uint16{serverPort, testPort},
  58. Bind: map[string]string{
  59. "wireguard.conf": "/etc/wireguard/wg0.conf",
  60. },
  61. Cmd: []string{"wg0"},
  62. })
  63. time.Sleep(5 * time.Second)
  64. startInstance(t, option.Options{
  65. Inbounds: []option.Inbound{
  66. {
  67. Type: C.TypeMixed,
  68. MixedOptions: option.HTTPMixedInboundOptions{
  69. ListenOptions: option.ListenOptions{
  70. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  71. ListenPort: clientPort,
  72. },
  73. },
  74. },
  75. },
  76. Outbounds: []option.Outbound{
  77. {
  78. Type: C.TypeWireGuard,
  79. WireGuardOptions: option.WireGuardOutboundOptions{
  80. InterfaceName: "wg",
  81. ServerOptions: option.ServerOptions{
  82. Server: "127.0.0.1",
  83. ServerPort: serverPort,
  84. },
  85. LocalAddress: []option.ListenPrefix{option.ListenPrefix(netip.MustParsePrefix("10.0.0.2/32"))},
  86. PrivateKey: "qGnwlkZljMxeECW8fbwAWdvgntnbK7B8UmMFl3zM0mk=",
  87. PeerPublicKey: "QsdcBm+oJw2oNv0cIFXLIq1E850lgTBonup4qnKEQBg=",
  88. },
  89. },
  90. },
  91. })
  92. time.Sleep(10 * time.Second)
  93. testSuitWg(t, clientPort, testPort)
  94. }