functions.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // +build !confonly
  2. package core
  3. import (
  4. "bytes"
  5. "context"
  6. "github.com/v2fly/v2ray-core/v4/common"
  7. "github.com/v2fly/v2ray-core/v4/common/net"
  8. "github.com/v2fly/v2ray-core/v4/features/routing"
  9. "github.com/v2fly/v2ray-core/v4/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. var ctx context.Context
  14. if v != nil {
  15. ctx = context.WithValue(v.ctx, v2rayKey, v)
  16. }
  17. return common.CreateObject(ctx, config)
  18. }
  19. // StartInstance starts a new V2Ray instance with given serialized config.
  20. // By default V2Ray only support config in protobuf format, i.e., configFormat = "protobuf". Caller need to load other packages to add JSON support.
  21. //
  22. // v2ray: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 V2Ray.
  38. // It dispatches the request to the given destination by the given V2Ray 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. // v2ray:api:stable
  43. func Dial(ctx context.Context, v *Instance, dest net.Destination) (net.Conn, error) {
  44. if FromContext(ctx) == nil {
  45. ctx = context.WithValue(ctx, v2rayKey, v)
  46. }
  47. dispatcher := v.GetFeature(routing.DispatcherType())
  48. if dispatcher == nil {
  49. return nil, newError("routing.Dispatcher is not registered in V2Ray core")
  50. }
  51. r, err := dispatcher.(routing.Dispatcher).Dispatch(ctx, dest)
  52. if err != nil {
  53. return nil, err
  54. }
  55. var readerOpt net.ConnectionOption
  56. if dest.Network == net.Network_TCP {
  57. readerOpt = net.ConnectionOutputMulti(r.Reader)
  58. } else {
  59. readerOpt = net.ConnectionOutputMultiUDP(r.Reader)
  60. }
  61. return net.NewConnection(net.ConnectionInputMulti(r.Writer), readerOpt), nil
  62. }
  63. // DialUDP provides a way to exchange UDP packets through V2Ray instance to remote servers.
  64. // Since it is under a proxy context, the LocalAddr() in returned PacketConn will not show the real address.
  65. //
  66. // TODO: SetDeadline() / SetReadDeadline() / SetWriteDeadline() are not implemented.
  67. //
  68. // v2ray:api:beta
  69. func DialUDP(ctx context.Context, v *Instance) (net.PacketConn, error) {
  70. if FromContext(ctx) == nil {
  71. ctx = context.WithValue(ctx, v2rayKey, v)
  72. }
  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. }