1
0

xray_test.go 2.3 KB

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