1
0

functions.go 2.7 KB

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