xray_test.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. Address: net.NewIPOrDomain(net.LocalHostIP),
  62. Port: uint32(0),
  63. User: &protocol.User{
  64. Account: serial.ToTypedMessage(&vmess.Account{
  65. Id: userID.String(),
  66. }),
  67. },
  68. },
  69. }),
  70. },
  71. },
  72. }
  73. cfgBytes, err := proto.Marshal(config)
  74. common.Must(err)
  75. server, err := StartInstance("protobuf", cfgBytes)
  76. common.Must(err)
  77. server.Close()
  78. }