2020-11-25 11:01:53 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
|
2020-12-04 01:36:16 +00:00
|
|
|
"github.com/xtls/xray-core/common"
|
|
|
|
"github.com/xtls/xray-core/common/net"
|
2020-12-27 19:20:39 +00:00
|
|
|
"github.com/xtls/xray-core/common/net/cnc"
|
2020-12-04 01:36:16 +00:00
|
|
|
"github.com/xtls/xray-core/features/routing"
|
|
|
|
"github.com/xtls/xray-core/transport/internet/udp"
|
2020-11-25 11:01:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// CreateObject creates a new object based on the given Xray instance and config. The Xray instance may be nil.
|
|
|
|
func CreateObject(v *Instance, config interface{}) (interface{}, error) {
|
|
|
|
ctx := v.ctx
|
|
|
|
if v != nil {
|
2022-02-20 03:45:41 +00:00
|
|
|
ctx = toContext(v.ctx, v)
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
return common.CreateObject(ctx, config)
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartInstance starts a new Xray instance with given serialized config.
|
|
|
|
// By default Xray only support config in protobuf format, i.e., configFormat = "protobuf". Caller need to load other packages to add JSON support.
|
|
|
|
//
|
|
|
|
// xray:api:stable
|
|
|
|
func StartInstance(configFormat string, configBytes []byte) (*Instance, error) {
|
2021-02-12 14:12:58 +00:00
|
|
|
config, err := LoadConfig(configFormat, bytes.NewReader(configBytes))
|
2020-11-25 11:01:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
instance, err := New(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := instance.Start(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return instance, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dial provides an easy way for upstream caller to create net.Conn through Xray.
|
|
|
|
// It dispatches the request to the given destination by the given Xray instance.
|
|
|
|
// Since it is under a proxy context, the LocalAddr() and RemoteAddr() in returned net.Conn
|
|
|
|
// will not show real addresses being used for communication.
|
|
|
|
//
|
|
|
|
// xray:api:stable
|
|
|
|
func Dial(ctx context.Context, v *Instance, dest net.Destination) (net.Conn, error) {
|
2022-02-20 03:45:41 +00:00
|
|
|
ctx = toContext(ctx, v)
|
|
|
|
|
2020-11-25 11:01:53 +00:00
|
|
|
dispatcher := v.GetFeature(routing.DispatcherType())
|
|
|
|
if dispatcher == nil {
|
|
|
|
return nil, newError("routing.Dispatcher is not registered in Xray core")
|
|
|
|
}
|
2022-02-20 03:45:41 +00:00
|
|
|
|
2020-11-25 11:01:53 +00:00
|
|
|
r, err := dispatcher.(routing.Dispatcher).Dispatch(ctx, dest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-27 19:20:39 +00:00
|
|
|
var readerOpt cnc.ConnectionOption
|
2020-11-25 11:01:53 +00:00
|
|
|
if dest.Network == net.Network_TCP {
|
2020-12-27 19:20:39 +00:00
|
|
|
readerOpt = cnc.ConnectionOutputMulti(r.Reader)
|
2020-11-25 11:01:53 +00:00
|
|
|
} else {
|
2020-12-27 19:20:39 +00:00
|
|
|
readerOpt = cnc.ConnectionOutputMultiUDP(r.Reader)
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
2020-12-27 19:20:39 +00:00
|
|
|
return cnc.NewConnection(cnc.ConnectionInputMulti(r.Writer), readerOpt), nil
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DialUDP provides a way to exchange UDP packets through Xray instance to remote servers.
|
|
|
|
// Since it is under a proxy context, the LocalAddr() in returned PacketConn will not show the real address.
|
|
|
|
//
|
|
|
|
// TODO: SetDeadline() / SetReadDeadline() / SetWriteDeadline() are not implemented.
|
|
|
|
//
|
|
|
|
// xray:api:beta
|
|
|
|
func DialUDP(ctx context.Context, v *Instance) (net.PacketConn, error) {
|
2022-02-20 03:45:41 +00:00
|
|
|
ctx = toContext(ctx, v)
|
|
|
|
|
2020-11-25 11:01:53 +00:00
|
|
|
dispatcher := v.GetFeature(routing.DispatcherType())
|
|
|
|
if dispatcher == nil {
|
|
|
|
return nil, newError("routing.Dispatcher is not registered in Xray core")
|
|
|
|
}
|
|
|
|
return udp.DialDispatcher(ctx, dispatcher.(routing.Dispatcher))
|
|
|
|
}
|