-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5c366db
commit 707efd6
Showing
9 changed files
with
310 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package conf | ||
|
||
import ( | ||
"github.com/golang/protobuf/proto" | ||
|
||
"github.com/xtls/xray-core/proxy/loopback" | ||
) | ||
|
||
type LoopbackConfig struct { | ||
InboundTag string `json:"inboundTag"` | ||
} | ||
|
||
func (l LoopbackConfig) Build() (proto.Message, error) { | ||
return &loopback.Config{InboundTag: l.InboundTag}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package loopback | ||
|
||
//go:generate go run github.com/xtls/xray-core/common/errors/errorgen |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
syntax = "proto3"; | ||
|
||
package v2ray.core.proxy.loopback; | ||
option csharp_namespace = "Xray.Proxy.Loopback"; | ||
option go_package = "github.com/xtls/xray-core/proxy/loopback"; | ||
option java_package = "com.xray.proxy.loopback"; | ||
option java_multiple_files = true; | ||
|
||
message Config { | ||
string inbound_tag = 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package loopback | ||
|
||
import "github.com/xtls/xray-core/common/errors" | ||
|
||
type errPathObjHolder struct{} | ||
|
||
func newError(values ...interface{}) *errors.Error { | ||
return errors.New(values...).WithPathObj(errPathObjHolder{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package loopback | ||
|
||
import ( | ||
"context" | ||
"github.com/xtls/xray-core/common/net/cnc" | ||
|
||
"github.com/xtls/xray-core/common" | ||
"github.com/xtls/xray-core/common/buf" | ||
"github.com/xtls/xray-core/common/net" | ||
"github.com/xtls/xray-core/common/retry" | ||
"github.com/xtls/xray-core/common/session" | ||
"github.com/xtls/xray-core/common/task" | ||
"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 { | ||
config *Config | ||
dispatcherInstance routing.Dispatcher | ||
} | ||
|
||
func (l *Loopback) Process(ctx context.Context, link *transport.Link, _ internet.Dialer) error { | ||
outbound := session.OutboundFromContext(ctx) | ||
if outbound == nil || !outbound.Target.IsValid() { | ||
return newError("target not specified.") | ||
} | ||
destination := outbound.Target | ||
|
||
newError("opening connection to ", destination).WriteToLog(session.ExportIDToError(ctx)) | ||
|
||
input := link.Reader | ||
output := link.Writer | ||
|
||
var conn net.Conn | ||
err := retry.ExponentialBackoff(2, 100).On(func() error { | ||
dialDest := destination | ||
|
||
content := new(session.Content) | ||
content.SkipDNSResolve = true | ||
|
||
ctx = session.ContextWithContent(ctx, content) | ||
|
||
inbound := session.InboundFromContext(ctx) | ||
|
||
inbound.Tag = l.config.InboundTag | ||
|
||
ctx = session.ContextWithInbound(ctx, inbound) | ||
|
||
rawConn, err := l.dispatcherInstance.Dispatch(ctx, dialDest) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var readerOpt cnc.ConnectionOption | ||
if dialDest.Network == net.Network_TCP { | ||
readerOpt = cnc.ConnectionOutputMulti(rawConn.Reader) | ||
} else { | ||
readerOpt = cnc.ConnectionOutputMultiUDP(rawConn.Reader) | ||
} | ||
|
||
conn = cnc.NewConnection(cnc.ConnectionInputMulti(rawConn.Writer), readerOpt) | ||
return nil | ||
}) | ||
if err != nil { | ||
return newError("failed to open connection to ", destination).Base(err) | ||
} | ||
defer conn.Close() | ||
|
||
requestDone := func() error { | ||
var writer buf.Writer | ||
if destination.Network == net.Network_TCP { | ||
writer = buf.NewWriter(conn) | ||
} else { | ||
writer = &buf.SequentialWriter{Writer: conn} | ||
} | ||
|
||
if err := buf.Copy(input, writer); err != nil { | ||
return newError("failed to process request").Base(err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
responseDone := func() error { | ||
var reader buf.Reader | ||
if destination.Network == net.Network_TCP { | ||
reader = buf.NewReader(conn) | ||
} else { | ||
reader = buf.NewPacketReader(conn) | ||
} | ||
if err := buf.Copy(reader, output); err != nil { | ||
return newError("failed to process response").Base(err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
if err := task.Run(ctx, requestDone, task.OnSuccess(responseDone, task.Close(output))); err != nil { | ||
return newError("connection ends").Base(err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (l *Loopback) init(config *Config, dispatcherInstance routing.Dispatcher) error { | ||
l.dispatcherInstance = dispatcherInstance | ||
l.config = config | ||
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 | ||
})) | ||
} |