functions.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package core
  2. import (
  3. "bytes"
  4. "context"
  5. "github.com/xtls/xray-core/common"
  6. "github.com/xtls/xray-core/common/errors"
  7. "github.com/xtls/xray-core/common/net"
  8. "github.com/xtls/xray-core/common/net/cnc"
  9. "github.com/xtls/xray-core/features/routing"
  10. "github.com/xtls/xray-core/transport/internet/udp"
  11. )
  12. // CreateObject creates a new object based on the given Xray instance and config. The Xray instance may be nil.
  13. func CreateObject(v *Instance, config interface{}) (interface{}, error) {
  14. ctx := v.ctx
  15. if v != nil {
  16. ctx = toContext(v.ctx, v)
  17. }
  18. return common.CreateObject(ctx, config)
  19. }
  20. // StartInstance starts a new Xray instance with given serialized config.
  21. // By default Xray only support config in protobuf format, i.e., configFormat = "protobuf". Caller need to load other packages to add JSON support.
  22. //
  23. // xray:api:stable
  24. func StartInstance(configFormat string, configBytes []byte) (*Instance, error) {
  25. config, err := LoadConfig(configFormat, bytes.NewReader(configBytes))
  26. if err != nil {
  27. return nil, err
  28. }
  29. instance, err := New(config)
  30. if err != nil {
  31. return nil, err
  32. }
  33. if err := instance.Start(); err != nil {
  34. return nil, err
  35. }
  36. return instance, nil
  37. }
  38. // Dial provides an easy way for upstream caller to create net.Conn through Xray.
  39. // It dispatches the request to the given destination by the given Xray instance.
  40. // Since it is under a proxy context, the LocalAddr() and RemoteAddr() in returned net.Conn
  41. // will not show real addresses being used for communication.
  42. //
  43. // xray:api:stable
  44. func Dial(ctx context.Context, v *Instance, dest net.Destination) (net.Conn, error) {
  45. ctx = toContext(ctx, v)
  46. dispatcher := v.GetFeature(routing.DispatcherType())
  47. if dispatcher == nil {
  48. return nil, errors.New("routing.Dispatcher is not registered in Xray core")
  49. }
  50. r, err := dispatcher.(routing.Dispatcher).Dispatch(ctx, dest)
  51. if err != nil {
  52. return nil, err
  53. }
  54. var readerOpt cnc.ConnectionOption
  55. if dest.Network == net.Network_TCP {
  56. readerOpt = cnc.ConnectionOutputMulti(r.Reader)
  57. } else {
  58. readerOpt = cnc.ConnectionOutputMultiUDP(r.Reader)
  59. }
  60. return cnc.NewConnection(cnc.ConnectionInputMulti(r.Writer), readerOpt), nil
  61. }
  62. // DialUDP provides a way to exchange UDP packets through Xray instance to remote servers.
  63. // Since it is under a proxy context, the LocalAddr() in returned PacketConn will not show the real address.
  64. //
  65. // TODO: SetDeadline() / SetReadDeadline() / SetWriteDeadline() are not implemented.
  66. //
  67. // xray:api:beta
  68. func DialUDP(ctx context.Context, v *Instance) (net.PacketConn, error) {
  69. ctx = toContext(ctx, v)
  70. dispatcher := v.GetFeature(routing.DispatcherType())
  71. if dispatcher == nil {
  72. return nil, errors.New("routing.Dispatcher is not registered in Xray core")
  73. }
  74. return udp.DialDispatcher(ctx, dispatcher.(routing.Dispatcher))
  75. }