functions.go 3.0 KB

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