Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/omengye/Xray-core into minimal
Browse files Browse the repository at this point in the history
# Conflicts:
#	common/reflect/marshal_test.go
  • Loading branch information
omengye committed Dec 16, 2024
2 parents f6210dd + 68137a8 commit c0a4625
Show file tree
Hide file tree
Showing 34 changed files with 787 additions and 647 deletions.
2 changes: 1 addition & 1 deletion app/dispatcher/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func init() {
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
d := new(DefaultDispatcher)
if err := core.RequireFeatures(ctx, func(om outbound.Manager, router routing.Router, pm policy.Manager, sm stats.Manager, dc dns.Client) error {
core.RequireFeatures(ctx, func(fdns dns.FakeDNSEngine) {
core.RequireFeatures(ctx, func(fdns dns.FakeDNSEngine) { // FakeDNSEngine is optional
d.fdns = fdns
})
return d.Init(config.(*Config), om, router, pm, sm, dc)
Expand Down
10 changes: 7 additions & 3 deletions app/dns/nameserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type Client struct {
var errExpectedIPNonMatch = errors.New("expectIPs not match")

// NewServer creates a name server object according to the network destination url.
func NewServer(dest net.Destination, dispatcher routing.Dispatcher, queryStrategy QueryStrategy) (Server, error) {
func NewServer(ctx context.Context, dest net.Destination, dispatcher routing.Dispatcher, queryStrategy QueryStrategy) (Server, error) {
if address := dest.Address; address.Family().IsDomain() {
u, err := url.Parse(address.Domain())
if err != nil {
Expand All @@ -55,7 +55,11 @@ func NewServer(dest net.Destination, dispatcher routing.Dispatcher, queryStrateg
case strings.EqualFold(u.Scheme, "tcp+local"): // DNS-over-TCP Local mode
return NewTCPLocalNameServer(u, queryStrategy)
case strings.EqualFold(u.String(), "fakedns"):
return NewFakeDNSServer(), nil
var fd dns.FakeDNSEngine
core.RequireFeatures(ctx, func(fdns dns.FakeDNSEngine) { // FakeDNSEngine is optional
fd = fdns
})
return NewFakeDNSServer(fd), nil
}
}
if dest.Network == net.Network_Unknown {
Expand All @@ -80,7 +84,7 @@ func NewClient(

err := core.RequireFeatures(ctx, func(dispatcher routing.Dispatcher) error {
// Create a new server for each client for now
server, err := NewServer(ns.Address.AsDestination(), dispatcher, ns.GetQueryStrategy())
server, err := NewServer(ctx, ns.Address.AsDestination(), dispatcher, ns.GetQueryStrategy())
if err != nil {
return errors.New("failed to create nameserver").Base(err).AtWarning()
}
Expand Down
12 changes: 4 additions & 8 deletions app/dns/nameserver_fakedns.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ import (

"github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/core"
"github.com/xtls/xray-core/features/dns"
)

type FakeDNSServer struct {
fakeDNSEngine dns.FakeDNSEngine
}

func NewFakeDNSServer() *FakeDNSServer {
return &FakeDNSServer{}
func NewFakeDNSServer(fd dns.FakeDNSEngine) *FakeDNSServer {
return &FakeDNSServer{fakeDNSEngine: fd}
}

func (FakeDNSServer) Name() string {
Expand All @@ -23,12 +22,9 @@ func (FakeDNSServer) Name() string {

func (f *FakeDNSServer) QueryIP(ctx context.Context, domain string, _ net.IP, opt dns.IPOption, _ bool) ([]net.IP, error) {
if f.fakeDNSEngine == nil {
if err := core.RequireFeatures(ctx, func(fd dns.FakeDNSEngine) {
f.fakeDNSEngine = fd
}); err != nil {
return nil, errors.New("Unable to locate a fake DNS Engine").Base(err).AtError()
}
return nil, errors.New("Unable to locate a fake DNS Engine").AtError()
}

var ips []net.Address
if fkr0, ok := f.fakeDNSEngine.(dns.FakeDNSEngineRev0); ok {
ips = fkr0.GetFakeIPForDomain3(domain, opt.IPv4Enable, opt.IPv6Enable)
Expand Down
7 changes: 5 additions & 2 deletions app/observatory/burst/burstobserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/xtls/xray-core/core"
"github.com/xtls/xray-core/features/extension"
"github.com/xtls/xray-core/features/outbound"
"github.com/xtls/xray-core/features/routing"
"google.golang.org/protobuf/proto"
)

Expand Down Expand Up @@ -88,13 +89,15 @@ func (o *Observer) Close() error {

func New(ctx context.Context, config *Config) (*Observer, error) {
var outboundManager outbound.Manager
err := core.RequireFeatures(ctx, func(om outbound.Manager) {
var dispatcher routing.Dispatcher
err := core.RequireFeatures(ctx, func(om outbound.Manager, rd routing.Dispatcher) {
outboundManager = om
dispatcher = rd
})
if err != nil {
return nil, errors.New("Cannot get depended features").Base(err)
}
hp := NewHealthPing(ctx, config.PingConfig)
hp := NewHealthPing(ctx, dispatcher, config.PingConfig)
return &Observer{
config: config,
ctx: ctx,
Expand Down
6 changes: 5 additions & 1 deletion app/observatory/burst/healthping.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/xtls/xray-core/common/dice"
"github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/features/routing"
)

// HealthPingSettings holds settings for health Checker
Expand All @@ -23,6 +24,7 @@ type HealthPingSettings struct {
// HealthPing is the health checker for balancers
type HealthPing struct {
ctx context.Context
dispatcher routing.Dispatcher
access sync.Mutex
ticker *time.Ticker
tickerClose chan struct{}
Expand All @@ -32,7 +34,7 @@ type HealthPing struct {
}

// NewHealthPing creates a new HealthPing with settings
func NewHealthPing(ctx context.Context, config *HealthPingConfig) *HealthPing {
func NewHealthPing(ctx context.Context, dispatcher routing.Dispatcher, config *HealthPingConfig) *HealthPing {
settings := &HealthPingSettings{}
if config != nil {
settings = &HealthPingSettings{
Expand Down Expand Up @@ -65,6 +67,7 @@ func NewHealthPing(ctx context.Context, config *HealthPingConfig) *HealthPing {
}
return &HealthPing{
ctx: ctx,
dispatcher: dispatcher,
Settings: settings,
Results: nil,
}
Expand Down Expand Up @@ -149,6 +152,7 @@ func (h *HealthPing) doCheck(tags []string, duration time.Duration, rounds int)
handler := tag
client := newPingClient(
h.ctx,
h.dispatcher,
h.Settings.Destination,
h.Settings.Timeout,
handler,
Expand Down
9 changes: 5 additions & 4 deletions app/observatory/burst/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/features/routing"
"github.com/xtls/xray-core/transport/internet/tagged"
)

Expand All @@ -14,10 +15,10 @@ type pingClient struct {
httpClient *http.Client
}

func newPingClient(ctx context.Context, destination string, timeout time.Duration, handler string) *pingClient {
func newPingClient(ctx context.Context, dispatcher routing.Dispatcher, destination string, timeout time.Duration, handler string) *pingClient {
return &pingClient{
destination: destination,
httpClient: newHTTPClient(ctx, handler, timeout),
httpClient: newHTTPClient(ctx, dispatcher, handler, timeout),
}
}

Expand All @@ -28,15 +29,15 @@ func newDirectPingClient(destination string, timeout time.Duration) *pingClient
}
}

func newHTTPClient(ctxv context.Context, handler string, timeout time.Duration) *http.Client {
func newHTTPClient(ctxv context.Context, dispatcher routing.Dispatcher, handler string, timeout time.Duration) *http.Client {
tr := &http.Transport{
DisableKeepAlives: true,
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
dest, err := net.ParseDestination(network + ":" + addr)
if err != nil {
return nil, err
}
return tagged.Dialer(ctxv, dest, handler)
return tagged.Dialer(ctxv, dispatcher, dest, handler)
},
}
return &http.Client{
Expand Down
9 changes: 7 additions & 2 deletions app/observatory/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/xtls/xray-core/core"
"github.com/xtls/xray-core/features/extension"
"github.com/xtls/xray-core/features/outbound"
"github.com/xtls/xray-core/features/routing"
"github.com/xtls/xray-core/transport/internet/tagged"
"google.golang.org/protobuf/proto"
)
Expand All @@ -32,6 +33,7 @@ type Observer struct {
finished *done.Instance

ohm outbound.Manager
dispatcher routing.Dispatcher
}

func (o *Observer) GetObservation(ctx context.Context) (proto.Message, error) {
Expand Down Expand Up @@ -131,7 +133,7 @@ func (o *Observer) probe(outbound string) ProbeResult {
return errors.New("cannot understand address").Base(err)
}
trackedCtx := session.TrackedConnectionError(o.ctx, errorCollectorForRequest)
conn, err := tagged.Dialer(trackedCtx, dest, outbound)
conn, err := tagged.Dialer(trackedCtx, o.dispatcher, dest, outbound)
if err != nil {
return errors.New("cannot dial remote address ", dest).Base(err)
}
Expand Down Expand Up @@ -215,8 +217,10 @@ func (o *Observer) findStatusLocationLockHolderOnly(outbound string) int {

func New(ctx context.Context, config *Config) (*Observer, error) {
var outboundManager outbound.Manager
err := core.RequireFeatures(ctx, func(om outbound.Manager) {
var dispatcher routing.Dispatcher
err := core.RequireFeatures(ctx, func(om outbound.Manager, rd routing.Dispatcher) {
outboundManager = om
dispatcher = rd
})
if err != nil {
return nil, errors.New("Cannot get depended features").Base(err)
Expand All @@ -225,6 +229,7 @@ func New(ctx context.Context, config *Config) (*Observer, error) {
config: config,
ctx: ctx,
ohm: outboundManager,
dispatcher: dispatcher,
}, nil
}

Expand Down
12 changes: 5 additions & 7 deletions app/router/balancing.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
sync "sync"

"github.com/xtls/xray-core/app/observatory"
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/core"
"github.com/xtls/xray-core/features/extension"
Expand All @@ -31,19 +30,18 @@ type RoundRobinStrategy struct {

func (s *RoundRobinStrategy) InjectContext(ctx context.Context) {
s.ctx = ctx
if len(s.FallbackTag) > 0 {
core.RequireFeaturesAsync(s.ctx, func(observatory extension.Observatory) {
s.observatory = observatory
})
}
}

func (s *RoundRobinStrategy) GetPrincipleTarget(strings []string) []string {
return strings
}

func (s *RoundRobinStrategy) PickOutbound(tags []string) string {
if len(s.FallbackTag) > 0 && s.observatory == nil {
common.Must(core.RequireFeatures(s.ctx, func(observatory extension.Observatory) error {
s.observatory = observatory
return nil
}))
}
if s.observatory != nil {
observeReport, err := s.observatory.GetObservation(s.ctx)
if err == nil {
Expand Down
14 changes: 7 additions & 7 deletions app/router/strategy_leastload.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"time"

"github.com/xtls/xray-core/app/observatory"
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/dice"
"github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/core"
Expand Down Expand Up @@ -58,8 +57,11 @@ type node struct {
RTTDeviationCost time.Duration
}

func (l *LeastLoadStrategy) InjectContext(ctx context.Context) {
l.ctx = ctx
func (s *LeastLoadStrategy) InjectContext(ctx context.Context) {
s.ctx = ctx
core.RequireFeaturesAsync(s.ctx, func(observatory extension.Observatory) {
s.observer = observatory
})
}

func (s *LeastLoadStrategy) PickOutbound(candidates []string) string {
Expand Down Expand Up @@ -136,10 +138,8 @@ func (s *LeastLoadStrategy) selectLeastLoad(nodes []*node) []*node {

func (s *LeastLoadStrategy) getNodes(candidates []string, maxRTT time.Duration) []*node {
if s.observer == nil {
common.Must(core.RequireFeatures(s.ctx, func(observatory extension.Observatory) error {
s.observer = observatory
return nil
}))
errors.LogError(s.ctx, "observer is nil")
return make([]*node, 0)
}
observeResult, err := s.observer.GetObservation(s.ctx)
if err != nil {
Expand Down
13 changes: 6 additions & 7 deletions app/router/strategy_leastping.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"

"github.com/xtls/xray-core/app/observatory"
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/core"
"github.com/xtls/xray-core/features/extension"
Expand All @@ -21,19 +20,19 @@ func (l *LeastPingStrategy) GetPrincipleTarget(strings []string) []string {

func (l *LeastPingStrategy) InjectContext(ctx context.Context) {
l.ctx = ctx
core.RequireFeaturesAsync(l.ctx, func(observatory extension.Observatory) {
l.observatory = observatory
})
}

func (l *LeastPingStrategy) PickOutbound(strings []string) string {
if l.observatory == nil {
common.Must(core.RequireFeatures(l.ctx, func(observatory extension.Observatory) error {
l.observatory = observatory
return nil
}))
errors.LogError(l.ctx, "observer is nil")
return ""
}

observeReport, err := l.observatory.GetObservation(l.ctx)
if err != nil {
errors.LogInfoInner(l.ctx, err, "cannot get observe report")
errors.LogInfoInner(l.ctx, err, "cannot get observer report")
return ""
}
outboundsList := outboundList(strings)
Expand Down
12 changes: 5 additions & 7 deletions app/router/strategy_random.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"

"github.com/xtls/xray-core/app/observatory"
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/dice"
"github.com/xtls/xray-core/core"
"github.com/xtls/xray-core/features/extension"
Expand All @@ -20,19 +19,18 @@ type RandomStrategy struct {

func (s *RandomStrategy) InjectContext(ctx context.Context) {
s.ctx = ctx
if len(s.FallbackTag) > 0 {
core.RequireFeaturesAsync(s.ctx, func(observatory extension.Observatory) {
s.observatory = observatory
})
}
}

func (s *RandomStrategy) GetPrincipleTarget(strings []string) []string {
return strings
}

func (s *RandomStrategy) PickOutbound(candidates []string) string {
if len(s.FallbackTag) > 0 && s.observatory == nil {
common.Must(core.RequireFeatures(s.ctx, func(observatory extension.Observatory) error {
s.observatory = observatory
return nil
}))
}
if s.observatory != nil {
observeReport, err := s.observatory.GetObservation(s.ctx)
if err == nil {
Expand Down
4 changes: 2 additions & 2 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (

var (
Version_x byte = 24
Version_y byte = 11
Version_z byte = 30
Version_y byte = 12
Version_z byte = 15
)

var (
Expand Down
Loading

0 comments on commit c0a4625

Please sign in to comment.