1
0

xray_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package core_test
  2. import (
  3. "testing"
  4. "github.com/xtls/xray-core/app/dispatcher"
  5. "github.com/xtls/xray-core/app/proxyman"
  6. "github.com/xtls/xray-core/common"
  7. "github.com/xtls/xray-core/common/net"
  8. "github.com/xtls/xray-core/common/protocol"
  9. "github.com/xtls/xray-core/common/serial"
  10. "github.com/xtls/xray-core/common/uuid"
  11. . "github.com/xtls/xray-core/core"
  12. "github.com/xtls/xray-core/features/dns"
  13. "github.com/xtls/xray-core/features/dns/localdns"
  14. _ "github.com/xtls/xray-core/main/distro/all"
  15. "github.com/xtls/xray-core/proxy/dokodemo"
  16. "github.com/xtls/xray-core/proxy/vmess"
  17. "github.com/xtls/xray-core/proxy/vmess/outbound"
  18. "github.com/xtls/xray-core/testing/servers/tcp"
  19. "google.golang.org/protobuf/proto"
  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. }, false)
  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. Networks: []net.Network{net.Network_TCP},
  54. }),
  55. },
  56. },
  57. Outbound: []*OutboundHandlerConfig{
  58. {
  59. ProxySettings: serial.ToTypedMessage(&outbound.Config{
  60. Receiver: []*protocol.ServerEndpoint{
  61. {
  62. Address: net.NewIPOrDomain(net.LocalHostIP),
  63. Port: uint32(0),
  64. User: []*protocol.User{
  65. {
  66. Account: serial.ToTypedMessage(&vmess.Account{
  67. Id: userID.String(),
  68. }),
  69. },
  70. },
  71. },
  72. },
  73. }),
  74. },
  75. },
  76. }
  77. cfgBytes, err := proto.Marshal(config)
  78. common.Must(err)
  79. server, err := StartInstance("protobuf", cfgBytes)
  80. common.Must(err)
  81. server.Close()
  82. }