v2ray_test.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package core_test
  2. import (
  3. "testing"
  4. "google.golang.org/protobuf/proto"
  5. "google.golang.org/protobuf/types/known/anypb"
  6. . "github.com/v2fly/v2ray-core/v5"
  7. "github.com/v2fly/v2ray-core/v5/app/dispatcher"
  8. "github.com/v2fly/v2ray-core/v5/app/proxyman"
  9. "github.com/v2fly/v2ray-core/v5/common"
  10. "github.com/v2fly/v2ray-core/v5/common/net"
  11. "github.com/v2fly/v2ray-core/v5/common/protocol"
  12. "github.com/v2fly/v2ray-core/v5/common/serial"
  13. "github.com/v2fly/v2ray-core/v5/common/uuid"
  14. "github.com/v2fly/v2ray-core/v5/features/dns"
  15. "github.com/v2fly/v2ray-core/v5/features/dns/localdns"
  16. _ "github.com/v2fly/v2ray-core/v5/main/distro/all"
  17. "github.com/v2fly/v2ray-core/v5/proxy/dokodemo"
  18. "github.com/v2fly/v2ray-core/v5/proxy/vmess"
  19. "github.com/v2fly/v2ray-core/v5/proxy/vmess/outbound"
  20. "github.com/v2fly/v2ray-core/v5/testing/servers/tcp"
  21. )
  22. func TestV2RayDependency(t *testing.T) {
  23. instance := new(Instance)
  24. wait := make(chan bool, 1)
  25. instance.RequireFeatures(func(d dns.Client) {
  26. if d == nil {
  27. t.Error("expected dns client fulfilled, but actually nil")
  28. }
  29. wait <- true
  30. })
  31. instance.AddFeature(localdns.New())
  32. <-wait
  33. }
  34. func TestV2RayClose(t *testing.T) {
  35. port := tcp.PickPort()
  36. userID := uuid.New()
  37. config := &Config{
  38. App: []*anypb.Any{
  39. serial.ToTypedMessage(&dispatcher.Config{}),
  40. serial.ToTypedMessage(&proxyman.InboundConfig{}),
  41. serial.ToTypedMessage(&proxyman.OutboundConfig{}),
  42. },
  43. Inbound: []*InboundHandlerConfig{
  44. {
  45. ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  46. PortRange: net.SinglePortRange(port),
  47. Listen: net.NewIPOrDomain(net.LocalHostIP),
  48. }),
  49. ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
  50. Address: net.NewIPOrDomain(net.LocalHostIP),
  51. Port: uint32(0),
  52. NetworkList: &net.NetworkList{
  53. Network: []net.Network{net.Network_TCP, net.Network_UDP},
  54. },
  55. }),
  56. },
  57. },
  58. Outbound: []*OutboundHandlerConfig{
  59. {
  60. ProxySettings: serial.ToTypedMessage(&outbound.Config{
  61. Receiver: []*protocol.ServerEndpoint{
  62. {
  63. Address: net.NewIPOrDomain(net.LocalHostIP),
  64. Port: uint32(0),
  65. User: []*protocol.User{
  66. {
  67. Account: serial.ToTypedMessage(&vmess.Account{
  68. Id: userID.String(),
  69. }),
  70. },
  71. },
  72. },
  73. },
  74. }),
  75. },
  76. },
  77. }
  78. cfgBytes, err := proto.Marshal(config)
  79. common.Must(err)
  80. server, err := StartInstance(FormatProtobuf, cfgBytes)
  81. common.Must(err)
  82. server.Close()
  83. }