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

141 lines
3.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"
2017-04-02 19:30:21 +00:00
"v2ray.com/core/app/proxyman/mux"
2018-02-09 22:07:38 +00:00
"v2ray.com/core/common"
"v2ray.com/core/common/net"
2017-01-14 23:48:37 +00:00
"v2ray.com/core/proxy"
"v2ray.com/core/transport/internet"
"v2ray.com/core/transport/ray"
2017-01-14 23:48:37 +00:00
)
type Handler struct {
config *core.OutboundHandlerConfig
senderSettings *proxyman.SenderConfig
2017-01-26 19:57:18 +00:00
proxy proxy.Outbound
outboundManager core.OutboundHandlerManager
2017-04-02 19:30:21 +00:00
mux *mux.ClientManager
2017-01-14 23:48:37 +00:00
}
func NewHandler(ctx context.Context, config *core.OutboundHandlerConfig) (*Handler, error) {
2018-02-21 16:05:29 +00:00
v := core.MustFromContext(ctx)
h := &Handler{
config: config,
outboundManager: v.OutboundHandlerManager(),
}
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
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.NewClientManager(proxyHandler, h, config)
2017-04-02 23:07:18 +00:00
}
2017-01-14 23:48:37 +00:00
h.proxy = proxyHandler
return h, nil
}
2018-02-07 11:38:12 +00:00
// Tag implements core.OutboundHandler.
func (h *Handler) Tag() string {
return h.config.Tag
}
2017-04-12 08:40:21 +00:00
// Dispatch implements proxy.Outbound.Dispatch.
func (h *Handler) Dispatch(ctx context.Context, outboundRay ray.OutboundRay) {
2017-04-02 19:30:21 +00:00
if h.mux != nil {
err := h.mux.Dispatch(ctx, outboundRay)
if err != nil {
2018-02-22 14:26:00 +00:00
newError("failed to process outbound traffic").Base(err).WithContext(ctx).WriteToLog()
2017-10-24 15:33:28 +00:00
outboundRay.OutboundOutput().CloseError()
2017-02-21 22:14:07 +00:00
}
2017-02-10 11:01:52 +00:00
} else {
2017-04-02 19:30:21 +00:00
err := h.proxy.Process(ctx, outboundRay, h)
// Ensure outbound ray is properly closed.
2018-02-19 16:51:10 +00:00
if err != nil {
2018-02-22 14:26:00 +00:00
newError("failed to process outbound traffic").Base(err).WithContext(ctx).WriteToLog()
2017-04-02 19:30:21 +00:00
outboundRay.OutboundOutput().CloseError()
} else {
outboundRay.OutboundOutput().Close()
}
outboundRay.OutboundInput().CloseError()
}
2017-01-14 23:48:37 +00:00
}
2017-02-15 21:51:01 +00: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 {
2018-02-22 14:26:00 +00:00
newError("proxying to ", tag, " for dest ", dest).AtDebug().WithContext(ctx).WriteToLog()
2017-02-09 21:49:38 +00:00
ctx = proxy.ContextWithTarget(ctx, dest)
2018-03-30 21:17:28 +00:00
stream := ray.New(ctx)
go handler.Dispatch(ctx, stream)
2018-02-05 22:38:24 +00:00
return ray.NewConnection(stream.InboundOutput(), stream.InboundInput()), nil
}
2018-02-22 14:26:00 +00:00
newError("failed to get outbound handler with tag: ", tag).AtWarning().WithContext(ctx).WriteToLog()
}
if h.senderSettings.Via != nil {
ctx = internet.ContextWithDialerSource(ctx, h.senderSettings.Via.AsAddress())
}
2017-02-03 23:27:16 +00:00
if h.senderSettings.StreamSettings != nil {
ctx = internet.ContextWithStreamSettings(ctx, h.senderSettings.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-02-08 16:46:50 +00:00
// Close implements common.Runnable.
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
}