144 lines
4.0 KiB
Go
Raw Normal View History

2017-01-15 00:48:37 +01:00
package outbound
import (
"context"
"v2ray.com/core"
2017-01-15 00:48:37 +01:00
"v2ray.com/core/app/proxyman"
2017-04-02 21:30:21 +02:00
"v2ray.com/core/app/proxyman/mux"
2018-02-09 23:07:38 +01:00
"v2ray.com/core/common"
"v2ray.com/core/common/net"
"v2ray.com/core/common/session"
2017-01-15 00:48:37 +01:00
"v2ray.com/core/proxy"
"v2ray.com/core/transport/internet"
2018-04-17 00:31:10 +02:00
"v2ray.com/core/transport/pipe"
2017-01-15 00:48:37 +01:00
)
type Handler struct {
config *core.OutboundHandlerConfig
senderSettings *proxyman.SenderConfig
2017-01-26 20:57:18 +01:00
proxy proxy.Outbound
outboundManager core.OutboundHandlerManager
2017-04-02 21:30:21 +02:00
mux *mux.ClientManager
2017-01-15 00:48:37 +01:00
}
2018-04-04 18:17:42 +08:00
func NewHandler(ctx context.Context, config *core.OutboundHandlerConfig) (core.OutboundHandler, error) {
2018-02-21 17:05:29 +01:00
v := core.MustFromContext(ctx)
h := &Handler{
config: config,
outboundManager: v.OutboundHandlerManager(),
}
if config.SenderSettings != nil {
senderSettings, err := config.SenderSettings.GetInstance()
2017-01-15 00:48:37 +01:00
if err != nil {
return nil, err
}
switch s := senderSettings.(type) {
case *proxyman.SenderConfig:
h.senderSettings = s
2017-01-15 00:48:37 +01:00
default:
2017-04-09 01:43:25 +02:00
return nil, newError("settings is not SenderConfig")
2017-01-15 00:48:37 +01:00
}
}
proxyConfig, err := config.ProxySettings.GetInstance()
if err != nil {
return nil, err
}
2018-02-09 23:07:38 +01:00
rawProxyHandler, err := common.CreateObject(ctx, proxyConfig)
2017-01-15 00:48:37 +01:00
if err != nil {
return nil, err
}
2018-02-09 23:07:38 +01:00
proxyHandler, ok := rawProxyHandler.(proxy.Outbound)
if !ok {
return nil, newError("not an outbound handler")
}
2017-04-03 01:07:18 +02:00
if h.senderSettings != nil && h.senderSettings.MultiplexSettings != nil && h.senderSettings.MultiplexSettings.Enabled {
2017-04-12 10:40:21 +02:00
config := h.senderSettings.MultiplexSettings
if config.Concurrency < 1 || config.Concurrency > 1024 {
2017-12-27 22:25:12 +01:00
return nil, newError("invalid mux concurrency: ", config.Concurrency).AtWarning()
2017-04-12 10:40:21 +02:00
}
h.mux = mux.NewClientManager(proxyHandler, h, config)
2017-04-03 01:07:18 +02:00
}
2017-01-15 00:48:37 +01:00
h.proxy = proxyHandler
return h, nil
}
2018-02-07 12:38:12 +01:00
// Tag implements core.OutboundHandler.
func (h *Handler) Tag() string {
return h.config.Tag
}
2017-04-12 10:40:21 +02:00
// Dispatch implements proxy.Outbound.Dispatch.
2018-04-17 00:31:10 +02:00
func (h *Handler) Dispatch(ctx context.Context, link *core.Link) {
2017-04-02 21:30:21 +02:00
if h.mux != nil {
2018-04-17 00:31:10 +02:00
if err := h.mux.Dispatch(ctx, link); err != nil {
newError("failed to process mux outbound traffic").Base(err).WriteToLog(session.ExportIDToError(ctx))
2018-04-17 00:31:10 +02:00
pipe.CloseError(link.Writer)
2017-02-21 23:14:07 +01:00
}
2017-02-10 12:01:52 +01:00
} else {
2018-04-17 00:31:10 +02:00
if err := h.proxy.Process(ctx, link, h); err != nil {
2018-04-11 20:17:19 +02:00
// Ensure outbound ray is properly closed.
newError("failed to process outbound traffic").Base(err).WriteToLog(session.ExportIDToError(ctx))
2018-04-17 00:31:10 +02:00
pipe.CloseError(link.Writer)
2017-04-02 21:30:21 +02:00
} else {
2018-04-23 16:42:37 +02:00
common.Must(common.Close(link.Writer))
2017-04-02 21:30:21 +02:00
}
2018-04-17 00:31:10 +02:00
pipe.CloseError(link.Reader)
}
2017-01-15 00:48:37 +01:00
}
2017-02-15 22:51:01 +01:00
// Dial implements proxy.Dialer.Dial().
func (h *Handler) Dial(ctx context.Context, dest net.Destination) (internet.Connection, error) {
if h.senderSettings != nil {
if h.senderSettings.ProxySettings.HasTag() {
tag := h.senderSettings.ProxySettings.Tag
handler := h.outboundManager.GetHandler(tag)
if handler != nil {
newError("proxying to ", tag, " for dest ", dest).AtDebug().WriteToLog(session.ExportIDToError(ctx))
2017-02-09 22:49:38 +01:00
ctx = proxy.ContextWithTarget(ctx, dest)
2018-04-17 00:31:10 +02:00
2018-05-25 12:08:28 +02:00
opts := pipe.OptionsFromContext(ctx)
uplinkReader, uplinkWriter := pipe.New(opts...)
downlinkReader, downlinkWriter := pipe.New(opts...)
2018-04-17 00:31:10 +02:00
go handler.Dispatch(ctx, &core.Link{Reader: uplinkReader, Writer: downlinkWriter})
return net.NewConnection(net.ConnectionInputMulti(uplinkWriter), net.ConnectionOutputMulti(downlinkReader)), nil
}
newError("failed to get outbound handler with tag: ", tag).AtWarning().WriteToLog(session.ExportIDToError(ctx))
}
if h.senderSettings.Via != nil {
ctx = internet.ContextWithDialerSource(ctx, h.senderSettings.Via.AsAddress())
}
2017-02-04 00:27:16 +01:00
if h.senderSettings.StreamSettings != nil {
ctx = internet.ContextWithStreamSettings(ctx, h.senderSettings.StreamSettings)
}
2017-01-15 00:48:37 +01:00
}
return internet.Dial(ctx, dest)
}
2018-02-05 23:38:24 +01:00
2018-02-08 17:46:50 +01:00
// GetOutbound implements proxy.GetOutbound.
2018-02-05 23:38:24 +01:00
func (h *Handler) GetOutbound() proxy.Outbound {
return h.proxy
}
2018-02-08 15:45:50 +01:00
2018-02-08 17:46:50 +01:00
// Start implements common.Runnable.
2018-02-08 15:45:50 +01:00
func (h *Handler) Start() error {
return nil
}
2018-04-03 17:11:54 +08:00
// Close implements common.Closable.
2018-02-08 15:45:50 +01:00
func (h *Handler) Close() error {
2018-02-09 23:07:38 +01:00
common.Close(h.mux)
2018-02-08 15:45:50 +01:00
return nil
}