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