mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-10-22 07:30:56 +00:00
Some checks are pending
Build and Release for Windows 7 / check-assets (push) Waiting to run
Build and Release for Windows 7 / build (win7-32, 386, windows) (push) Blocked by required conditions
Build and Release for Windows 7 / build (win7-64, amd64, windows) (push) Blocked by required conditions
Build and Release / check-assets (push) Waiting to run
Build and Release / build (386, freebsd, ) (push) Blocked by required conditions
Build and Release / build (386, linux, ) (push) Blocked by required conditions
Build and Release / build (386, openbsd, ) (push) Blocked by required conditions
Build and Release / build (386, windows, ) (push) Blocked by required conditions
Build and Release / build (amd64, android, android-amd64) (push) Blocked by required conditions
Build and Release / build (amd64, darwin, ) (push) Blocked by required conditions
Build and Release / build (amd64, freebsd, ) (push) Blocked by required conditions
Build and Release / build (amd64, linux, ) (push) Blocked by required conditions
Build and Release / build (amd64, openbsd, ) (push) Blocked by required conditions
Build and Release / build (amd64, windows, ) (push) Blocked by required conditions
Build and Release / build (arm, 5, linux) (push) Blocked by required conditions
Build and Release / build (arm, 6, linux) (push) Blocked by required conditions
Build and Release / build (arm, 7, freebsd) (push) Blocked by required conditions
Build and Release / build (arm, 7, linux) (push) Blocked by required conditions
Build and Release / build (arm, 7, openbsd) (push) Blocked by required conditions
Build and Release / build (arm64, android) (push) Blocked by required conditions
Build and Release / build (arm64, darwin) (push) Blocked by required conditions
Build and Release / build (arm64, freebsd) (push) Blocked by required conditions
Build and Release / build (arm64, linux) (push) Blocked by required conditions
Build and Release / build (arm64, openbsd) (push) Blocked by required conditions
Build and Release / build (arm64, windows) (push) Blocked by required conditions
Build and Release / build (loong64, linux) (push) Blocked by required conditions
Build and Release / build (mips, linux) (push) Blocked by required conditions
Build and Release / build (mips64, linux) (push) Blocked by required conditions
Build and Release / build (mips64le, linux) (push) Blocked by required conditions
Build and Release / build (mipsle, linux) (push) Blocked by required conditions
Build and Release / build (ppc64, linux) (push) Blocked by required conditions
Build and Release / build (ppc64le, linux) (push) Blocked by required conditions
Build and Release / build (riscv64, linux) (push) Blocked by required conditions
Build and Release / build (s390x, linux) (push) Blocked by required conditions
Tests and Checkings / check-assets (push) Waiting to run
Tests and Checkings / check-proto (push) Waiting to run
Tests and Checkings / check-format (push) Waiting to run
Tests and Checkings / test (macos-latest) (push) Blocked by required conditions
Tests and Checkings / test (ubuntu-latest) (push) Blocked by required conditions
Tests and Checkings / test (windows-latest) (push) Blocked by required conditions
https://github.com/XTLS/Xray-core/pull/6796#issuecomment-5807809588
74 lines
2.2 KiB
Go
74 lines
2.2 KiB
Go
package loopback
|
|
|
|
import (
|
|
"context"
|
|
|
|
proxyman "github.com/xtls/xray-core/app/proxyman"
|
|
"github.com/xtls/xray-core/common"
|
|
"github.com/xtls/xray-core/common/errors"
|
|
"github.com/xtls/xray-core/common/session"
|
|
"github.com/xtls/xray-core/core"
|
|
"github.com/xtls/xray-core/features/routing"
|
|
"github.com/xtls/xray-core/transport"
|
|
"github.com/xtls/xray-core/transport/internet"
|
|
)
|
|
|
|
type Loopback struct {
|
|
inboundTag string
|
|
sniffingRequest session.SniffingRequest
|
|
dispatcherInstance routing.Dispatcher
|
|
}
|
|
|
|
func (l *Loopback) Process(ctx context.Context, link *transport.Link, _ internet.Dialer) error {
|
|
outbounds := session.OutboundsFromContext(ctx)
|
|
ob := outbounds[len(outbounds)-1]
|
|
if !ob.Target.IsValid() {
|
|
return errors.New("target not specified.")
|
|
}
|
|
ob.Name = "loopback"
|
|
destination := ob.Target
|
|
|
|
errors.LogInfo(ctx, "opening connection to ", destination)
|
|
content := new(session.Content)
|
|
content.SkipDNSResolve = true
|
|
content.SniffingRequest = l.sniffingRequest
|
|
|
|
ctx = session.ContextWithContent(ctx, content)
|
|
inbound := &session.Inbound{}
|
|
originInbound := session.InboundFromContext(ctx)
|
|
if originInbound != nil {
|
|
// get a shallow copy to avoid modifying the inbound tag in upstream context
|
|
*inbound = *originInbound
|
|
}
|
|
inbound.Tag = l.inboundTag
|
|
ctx = session.ContextWithInbound(ctx, inbound)
|
|
|
|
err := l.dispatcherInstance.DispatchLink(ctx, destination, link)
|
|
if err != nil {
|
|
return errors.New(ctx, "failed to process loopback connection").Base(err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (l *Loopback) init(config *Config, dispatcherInstance routing.Dispatcher) error {
|
|
l.dispatcherInstance = dispatcherInstance
|
|
l.inboundTag = config.InboundTag
|
|
if config.Sniffing.GetEnabled() {
|
|
request, err := proxyman.BuildSniffingRequest(config.Sniffing)
|
|
if err != nil {
|
|
return errors.New("failed to build loopback sniffing request").Base(err)
|
|
}
|
|
l.sniffingRequest = request
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func init() {
|
|
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
|
l := new(Loopback)
|
|
err := core.RequireFeatures(ctx, func(dispatcherInstance routing.Dispatcher) error {
|
|
return l.init(config.(*Config), dispatcherInstance)
|
|
})
|
|
return l, err
|
|
}))
|
|
}
|