functions.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //go:build !confonly
  2. // +build !confonly
  3. package core
  4. import (
  5. "bytes"
  6. "context"
  7. "github.com/v2fly/v2ray-core/v4/common"
  8. "github.com/v2fly/v2ray-core/v4/common/net"
  9. "github.com/v2fly/v2ray-core/v4/features/routing"
  10. "github.com/v2fly/v2ray-core/v4/transport/internet/udp"
  11. )
  12. // CreateObject creates a new object based on the given V2Ray instance and config. The V2Ray instance may be nil.
  13. func CreateObject(v *Instance, config interface{}) (interface{}, error) {
  14. var ctx context.Context
  15. if v != nil {
  16. ctx = toContext(v.ctx, v)
  17. }
  18. return common.CreateObject(ctx, config)
  19. }
  20. // StartInstance starts a new V2Ray instance with given serialized config.
  21. // By default V2Ray only support config in protobuf format, i.e., configFormat = "protobuf". Caller need to load other packages to add JSON support.
  22. //
  23. // v2ray: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 V2Ray.
  39. // It dispatches the request to the given destination by the given V2Ray 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. // v2ray: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, newError("routing.Dispatcher is not registered in V2Ray core")
  49. }
  50. r, err := dispatcher.(routing.Dispatcher).Dispatch(ctx, dest)
  51. if err != nil {
  52. return nil, err
  53. }
  54. var readerOpt net.ConnectionOption
  55. if dest.Network == net.Network_TCP {
  56. readerOpt = net.ConnectionOutputMulti(r.Reader)
  57. } else {
  58. readerOpt = net.ConnectionOutputMultiUDP(r.Reader)
  59. }
  60. return net.NewConnection(net.ConnectionInputMulti(r.Writer), readerOpt), nil
  61. }
  62. // DialUDP provides a way to exchange UDP packets through V2Ray 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. // v2ray: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, newError("routing.Dispatcher is not registered in V2Ray core")
  73. }
  74. return udp.DialDispatcher(ctx, dispatcher.(routing.Dispatcher))
  75. }