1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-03 06:30:42 +00:00
v2fly/app/proxyman/outbound/handler.go

170 lines
4.8 KiB
Go
Raw Normal View History

2017-01-14 23:48:37 +00:00
package outbound
import (
"context"
"v2ray.com/core"
2017-01-14 23:48:37 +00:00
"v2ray.com/core/app/proxyman"
2018-02-09 22:07:38 +00:00
"v2ray.com/core/common"
2018-10-23 10:21:12 +00:00
"v2ray.com/core/common/mux"
"v2ray.com/core/common/net"
"v2ray.com/core/common/session"
"v2ray.com/core/common/vio"
"v2ray.com/core/features/outbound"
2017-01-14 23:48:37 +00:00
"v2ray.com/core/proxy"
"v2ray.com/core/transport/internet"
2018-04-16 22:31:10 +00:00
"v2ray.com/core/transport/pipe"
2017-01-14 23:48:37 +00:00
)
2018-10-22 06:26:03 +00:00
// Handler is an implements of outbound.Handler.
2017-01-14 23:48:37 +00:00
type Handler struct {
2018-10-22 06:26:03 +00:00
tag string
senderSettings *proxyman.SenderConfig
2018-09-07 12:50:25 +00:00
streamSettings *internet.MemoryStreamConfig
2017-01-26 19:57:18 +00:00
proxy proxy.Outbound
2018-10-12 21:57:56 +00:00
outboundManager outbound.Manager
2017-04-02 19:30:21 +00:00
mux *mux.ClientManager
2017-01-14 23:48:37 +00:00
}
2018-10-22 06:26:03 +00:00
// NewHandler create a new Handler based on the given configuration.
func NewHandler(ctx context.Context, config *core.OutboundHandlerConfig) (outbound.Handler, error) {
2018-02-21 16:05:29 +00:00
v := core.MustFromContext(ctx)
h := &Handler{
2018-10-22 06:26:03 +00:00
tag: config.Tag,
2018-10-21 08:27:13 +00:00
outboundManager: v.GetFeature(outbound.ManagerType()).(outbound.Manager),
}
if config.SenderSettings != nil {
senderSettings, err := config.SenderSettings.GetInstance()
2017-01-14 23:48:37 +00:00
if err != nil {
return nil, err
}
switch s := senderSettings.(type) {
case *proxyman.SenderConfig:
h.senderSettings = s
2018-09-07 12:50:25 +00:00
mss, err := internet.ToMemoryStreamConfig(s.StreamSettings)
if err != nil {
return nil, newError("failed to parse stream settings").Base(err).AtWarning()
}
h.streamSettings = mss
2017-01-14 23:48:37 +00:00
default:
2017-04-08 23:43:25 +00:00
return nil, newError("settings is not SenderConfig")
2017-01-14 23:48:37 +00:00
}
}
proxyConfig, err := config.ProxySettings.GetInstance()
if err != nil {
return nil, err
}
2018-02-09 22:07:38 +00:00
rawProxyHandler, err := common.CreateObject(ctx, proxyConfig)
2017-01-14 23:48:37 +00:00
if err != nil {
return nil, err
}
2018-02-09 22:07:38 +00:00
proxyHandler, ok := rawProxyHandler.(proxy.Outbound)
if !ok {
return nil, newError("not an outbound handler")
}
2017-04-02 23:07:18 +00:00
if h.senderSettings != nil && h.senderSettings.MultiplexSettings != nil && h.senderSettings.MultiplexSettings.Enabled {
2017-04-12 08:40:21 +00:00
config := h.senderSettings.MultiplexSettings
if config.Concurrency < 1 || config.Concurrency > 1024 {
2017-12-27 21:25:12 +00:00
return nil, newError("invalid mux concurrency: ", config.Concurrency).AtWarning()
2017-04-12 08:40:21 +00:00
}
h.mux = &mux.ClientManager{
Picker: &mux.IncrementalWorkerPicker{
2018-10-25 07:32:03 +00:00
Factory: &mux.DialingWorkerFactory{
Proxy: proxyHandler,
Dialer: h,
Strategy: mux.ClientStrategy{
MaxConcurrency: config.Concurrency,
MaxConnection: 128,
2018-10-25 07:32:03 +00:00
},
},
},
}
2017-04-02 23:07:18 +00:00
}
2017-01-14 23:48:37 +00:00
h.proxy = proxyHandler
return h, nil
}
// Tag implements outbound.Handler.
func (h *Handler) Tag() string {
2018-10-22 06:26:03 +00:00
return h.tag
}
2017-04-12 08:40:21 +00:00
// Dispatch implements proxy.Outbound.Dispatch.
func (h *Handler) Dispatch(ctx context.Context, link *vio.Link) {
2017-04-02 19:30:21 +00:00
if h.mux != nil {
2018-04-16 22:31:10 +00: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-16 22:31:10 +00:00
pipe.CloseError(link.Writer)
2017-02-21 22:14:07 +00:00
}
2017-02-10 11:01:52 +00:00
} else {
2018-04-16 22:31:10 +00:00
if err := h.proxy.Process(ctx, link, h); err != nil {
2018-04-11 18:17:19 +00:00
// Ensure outbound ray is properly closed.
newError("failed to process outbound traffic").Base(err).WriteToLog(session.ExportIDToError(ctx))
2018-04-16 22:31:10 +00:00
pipe.CloseError(link.Writer)
2017-04-02 19:30:21 +00:00
} else {
2018-04-23 14:42:37 +00:00
common.Must(common.Close(link.Writer))
2017-04-02 19:30:21 +00:00
}
2018-04-16 22:31:10 +00:00
pipe.CloseError(link.Reader)
}
2017-01-14 23:48:37 +00:00
}
2018-10-22 20:12:50 +00:00
// Dial implements internet.Dialer.
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))
ctx = session.ContextWithOutbound(ctx, &session.Outbound{
Target: dest,
})
2018-04-16 22:31:10 +00:00
2018-05-25 10:08:28 +00:00
opts := pipe.OptionsFromContext(ctx)
uplinkReader, uplinkWriter := pipe.New(opts...)
downlinkReader, downlinkWriter := pipe.New(opts...)
2018-04-16 22:31:10 +00:00
go handler.Dispatch(ctx, &vio.Link{Reader: uplinkReader, Writer: downlinkWriter})
2018-04-16 22:31:10 +00:00
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 {
outbound := session.OutboundFromContext(ctx)
if outbound == nil {
outbound = new(session.Outbound)
ctx = session.ContextWithOutbound(ctx, outbound)
}
outbound.Gateway = h.senderSettings.Via.AsAddress()
}
2017-02-03 23:27:16 +00:00
2018-09-07 12:50:25 +00:00
ctx = internet.ContextWithStreamSettings(ctx, h.streamSettings)
2017-01-14 23:48:37 +00:00
}
return internet.Dial(ctx, dest)
}
2018-02-05 22:38:24 +00:00
2018-02-08 16:46:50 +00:00
// GetOutbound implements proxy.GetOutbound.
2018-02-05 22:38:24 +00:00
func (h *Handler) GetOutbound() proxy.Outbound {
return h.proxy
}
2018-02-08 14:45:50 +00:00
2018-02-08 16:46:50 +00:00
// Start implements common.Runnable.
2018-02-08 14:45:50 +00:00
func (h *Handler) Start() error {
return nil
}
2018-04-03 09:11:54 +00:00
// Close implements common.Closable.
2018-02-08 14:45:50 +00:00
func (h *Handler) Close() error {
2018-02-09 22:07:38 +00:00
common.Close(h.mux)
2018-02-08 14:45:50 +00:00
return nil
}