2017-01-14 18:48:37 -05:00
|
|
|
package outbound
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-06-19 09:36:54 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v4/common/serial"
|
2017-01-14 18:48:37 -05:00
|
|
|
|
2021-02-16 15:31:50 -05:00
|
|
|
core "github.com/v2fly/v2ray-core/v4"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/app/proxyman"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/mux"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/net"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/session"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/features/outbound"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/features/policy"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/features/stats"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/proxy"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/transport"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/transport/internet"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/transport/internet/tls"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/transport/pipe"
|
2017-01-14 18:48:37 -05:00
|
|
|
)
|
|
|
|
|
2020-06-22 23:55:00 -04:00
|
|
|
func getStatCounter(v *core.Instance, tag string) (stats.Counter, stats.Counter) {
|
|
|
|
var uplinkCounter stats.Counter
|
|
|
|
var downlinkCounter stats.Counter
|
|
|
|
|
|
|
|
policy := v.GetFeature(policy.ManagerType()).(policy.Manager)
|
|
|
|
if len(tag) > 0 && policy.ForSystem().Stats.OutboundUplink {
|
|
|
|
statsManager := v.GetFeature(stats.ManagerType()).(stats.Manager)
|
|
|
|
name := "outbound>>>" + tag + ">>>traffic>>>uplink"
|
|
|
|
c, _ := stats.GetOrRegisterCounter(statsManager, name)
|
|
|
|
if c != nil {
|
|
|
|
uplinkCounter = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(tag) > 0 && policy.ForSystem().Stats.OutboundDownlink {
|
|
|
|
statsManager := v.GetFeature(stats.ManagerType()).(stats.Manager)
|
|
|
|
name := "outbound>>>" + tag + ">>>traffic>>>downlink"
|
|
|
|
c, _ := stats.GetOrRegisterCounter(statsManager, name)
|
|
|
|
if c != nil {
|
|
|
|
downlinkCounter = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return uplinkCounter, downlinkCounter
|
|
|
|
}
|
|
|
|
|
2018-10-22 02:26:03 -04:00
|
|
|
// Handler is an implements of outbound.Handler.
|
2017-01-14 18:48:37 -05:00
|
|
|
type Handler struct {
|
2018-10-22 02:26:03 -04:00
|
|
|
tag string
|
2017-01-26 14:46:44 -05:00
|
|
|
senderSettings *proxyman.SenderConfig
|
2018-09-07 08:50:25 -04:00
|
|
|
streamSettings *internet.MemoryStreamConfig
|
2017-01-26 14:57:18 -05:00
|
|
|
proxy proxy.Outbound
|
2018-10-12 17:57:56 -04:00
|
|
|
outboundManager outbound.Manager
|
2017-04-02 15:30:21 -04:00
|
|
|
mux *mux.ClientManager
|
2020-06-22 23:55:00 -04:00
|
|
|
uplinkCounter stats.Counter
|
|
|
|
downlinkCounter stats.Counter
|
2017-01-14 18:48:37 -05:00
|
|
|
}
|
|
|
|
|
2018-10-22 02:26:03 -04:00
|
|
|
// NewHandler create a new Handler based on the given configuration.
|
2018-10-11 14:43:37 -04:00
|
|
|
func NewHandler(ctx context.Context, config *core.OutboundHandlerConfig) (outbound.Handler, error) {
|
2018-02-21 11:05:29 -05:00
|
|
|
v := core.MustFromContext(ctx)
|
2020-06-22 23:55:00 -04:00
|
|
|
uplinkCounter, downlinkCounter := getStatCounter(v, config.Tag)
|
2018-01-10 06:22:37 -05:00
|
|
|
h := &Handler{
|
2018-10-22 02:26:03 -04:00
|
|
|
tag: config.Tag,
|
2018-10-21 04:27:13 -04:00
|
|
|
outboundManager: v.GetFeature(outbound.ManagerType()).(outbound.Manager),
|
2020-06-22 23:55:00 -04:00
|
|
|
uplinkCounter: uplinkCounter,
|
|
|
|
downlinkCounter: downlinkCounter,
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if config.SenderSettings != nil {
|
2021-06-19 09:36:54 -04:00
|
|
|
senderSettings, err := serial.GetInstanceOf(config.SenderSettings)
|
2017-01-14 18:48:37 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-01-26 14:46:44 -05:00
|
|
|
switch s := senderSettings.(type) {
|
|
|
|
case *proxyman.SenderConfig:
|
|
|
|
h.senderSettings = s
|
2018-09-07 08:50:25 -04: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 18:48:37 -05:00
|
|
|
default:
|
2017-04-08 19:43:25 -04:00
|
|
|
return nil, newError("settings is not SenderConfig")
|
2017-01-14 18:48:37 -05:00
|
|
|
}
|
|
|
|
}
|
2017-01-26 14:46:44 -05:00
|
|
|
|
2021-06-19 09:36:54 -04:00
|
|
|
proxyConfig, err := serial.GetInstanceOf(config.ProxySettings)
|
2018-01-10 06:22:37 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-02-09 17:07:38 -05:00
|
|
|
rawProxyHandler, err := common.CreateObject(ctx, proxyConfig)
|
2017-01-14 18:48:37 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-02-09 17:07:38 -05:00
|
|
|
proxyHandler, ok := rawProxyHandler.(proxy.Outbound)
|
|
|
|
if !ok {
|
|
|
|
return nil, newError("not an outbound handler")
|
|
|
|
}
|
|
|
|
|
2019-06-29 11:43:30 -04:00
|
|
|
if h.senderSettings != nil && h.senderSettings.MultiplexSettings != nil {
|
2017-04-12 04:40:21 -04:00
|
|
|
config := h.senderSettings.MultiplexSettings
|
|
|
|
if config.Concurrency < 1 || config.Concurrency > 1024 {
|
2017-12-27 16:25:12 -05:00
|
|
|
return nil, newError("invalid mux concurrency: ", config.Concurrency).AtWarning()
|
2017-04-12 04:40:21 -04:00
|
|
|
}
|
2018-10-24 16:34:48 -04:00
|
|
|
h.mux = &mux.ClientManager{
|
2019-06-29 11:43:30 -04:00
|
|
|
Enabled: h.senderSettings.MultiplexSettings.Enabled,
|
2018-10-24 16:34:48 -04:00
|
|
|
Picker: &mux.IncrementalWorkerPicker{
|
2021-03-06 09:31:46 -05:00
|
|
|
Factory: mux.NewDialingWorkerFactory(
|
|
|
|
ctx,
|
|
|
|
proxyHandler,
|
|
|
|
h,
|
|
|
|
mux.ClientStrategy{
|
2018-10-24 16:34:48 -04:00
|
|
|
MaxConcurrency: config.Concurrency,
|
|
|
|
MaxConnection: 128,
|
2018-10-25 03:32:03 -04:00
|
|
|
},
|
2021-03-06 09:31:46 -05:00
|
|
|
),
|
2018-10-24 16:34:48 -04:00
|
|
|
},
|
|
|
|
}
|
2017-04-02 19:07:18 -04:00
|
|
|
}
|
|
|
|
|
2017-01-14 18:48:37 -05:00
|
|
|
h.proxy = proxyHandler
|
|
|
|
return h, nil
|
|
|
|
}
|
|
|
|
|
2018-10-11 14:43:37 -04:00
|
|
|
// Tag implements outbound.Handler.
|
2018-01-10 06:22:37 -05:00
|
|
|
func (h *Handler) Tag() string {
|
2018-10-22 02:26:03 -04:00
|
|
|
return h.tag
|
2018-01-10 06:22:37 -05:00
|
|
|
}
|
|
|
|
|
2017-04-12 04:40:21 -04:00
|
|
|
// Dispatch implements proxy.Outbound.Dispatch.
|
2018-11-03 07:36:29 -04:00
|
|
|
func (h *Handler) Dispatch(ctx context.Context, link *transport.Link) {
|
2019-06-29 11:43:30 -04:00
|
|
|
if h.mux != nil && (h.mux.Enabled || session.MuxPreferedFromContext(ctx)) {
|
2018-04-16 18:31:10 -04:00
|
|
|
if err := h.mux.Dispatch(ctx, link); err != nil {
|
2021-03-07 07:14:58 -05:00
|
|
|
err := newError("failed to process mux outbound traffic").Base(err)
|
|
|
|
session.SubmitOutboundErrorToOriginator(ctx, err)
|
|
|
|
err.WriteToLog(session.ExportIDToError(ctx))
|
2018-12-31 15:25:10 -05:00
|
|
|
common.Interrupt(link.Writer)
|
2017-02-21 17:14:07 -05:00
|
|
|
}
|
2017-02-10 06:01:52 -05:00
|
|
|
} else {
|
2018-04-16 18:31:10 -04:00
|
|
|
if err := h.proxy.Process(ctx, link, h); err != nil {
|
2018-04-11 14:17:19 -04:00
|
|
|
// Ensure outbound ray is properly closed.
|
2021-03-07 07:14:58 -05:00
|
|
|
err := newError("failed to process outbound traffic").Base(err)
|
|
|
|
session.SubmitOutboundErrorToOriginator(ctx, err)
|
|
|
|
err.WriteToLog(session.ExportIDToError(ctx))
|
2018-12-31 15:25:10 -05:00
|
|
|
common.Interrupt(link.Writer)
|
2017-04-02 15:30:21 -04:00
|
|
|
} else {
|
2018-04-23 10:42:37 -04:00
|
|
|
common.Must(common.Close(link.Writer))
|
2017-04-02 15:30:21 -04:00
|
|
|
}
|
2018-12-31 15:25:10 -05:00
|
|
|
common.Interrupt(link.Reader)
|
2017-01-28 15:24:46 -05:00
|
|
|
}
|
2017-01-14 18:48:37 -05:00
|
|
|
}
|
|
|
|
|
2018-11-19 15:36:46 -05:00
|
|
|
// Address implements internet.Dialer.
|
|
|
|
func (h *Handler) Address() net.Address {
|
|
|
|
if h.senderSettings == nil || h.senderSettings.Via == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return h.senderSettings.Via.AsAddress()
|
|
|
|
}
|
|
|
|
|
2018-10-22 16:12:50 -04:00
|
|
|
// Dial implements internet.Dialer.
|
2017-08-29 06:56:57 -04:00
|
|
|
func (h *Handler) Dial(ctx context.Context, dest net.Destination) (internet.Connection, error) {
|
2017-01-26 14:46:44 -05:00
|
|
|
if h.senderSettings != nil {
|
2021-02-28 13:47:31 -05:00
|
|
|
if h.senderSettings.ProxySettings.HasTag() && !h.senderSettings.ProxySettings.TransportLayerProxy {
|
2017-01-26 14:46:44 -05:00
|
|
|
tag := h.senderSettings.ProxySettings.Tag
|
|
|
|
handler := h.outboundManager.GetHandler(tag)
|
|
|
|
if handler != nil {
|
2018-06-24 19:09:02 -04:00
|
|
|
newError("proxying to ", tag, " for dest ", dest).AtDebug().WriteToLog(session.ExportIDToError(ctx))
|
2018-09-18 17:09:54 -04:00
|
|
|
ctx = session.ContextWithOutbound(ctx, &session.Outbound{
|
|
|
|
Target: dest,
|
|
|
|
})
|
2018-04-16 18:31:10 -04:00
|
|
|
|
2018-05-25 06:08:28 -04:00
|
|
|
opts := pipe.OptionsFromContext(ctx)
|
|
|
|
uplinkReader, uplinkWriter := pipe.New(opts...)
|
|
|
|
downlinkReader, downlinkWriter := pipe.New(opts...)
|
2018-04-16 18:31:10 -04:00
|
|
|
|
2018-11-03 07:36:29 -04:00
|
|
|
go handler.Dispatch(ctx, &transport.Link{Reader: uplinkReader, Writer: downlinkWriter})
|
2019-10-25 06:00:01 -04:00
|
|
|
conn := net.NewConnection(net.ConnectionInputMulti(uplinkWriter), net.ConnectionOutputMulti(downlinkReader))
|
|
|
|
|
|
|
|
if config := tls.ConfigFromStreamSettings(h.streamSettings); config != nil {
|
2020-05-31 05:35:21 -04:00
|
|
|
tlsConfig := config.GetTLSConfig(tls.WithDestination(dest))
|
2019-10-25 06:00:01 -04:00
|
|
|
conn = tls.Client(conn, tlsConfig)
|
|
|
|
}
|
|
|
|
|
2020-06-22 23:55:00 -04:00
|
|
|
return h.getStatCouterConnection(conn), nil
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
|
|
|
|
2018-06-24 19:09:02 -04:00
|
|
|
newError("failed to get outbound handler with tag: ", tag).AtWarning().WriteToLog(session.ExportIDToError(ctx))
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if h.senderSettings.Via != nil {
|
2018-09-18 17:09:54 -04:00
|
|
|
outbound := session.OutboundFromContext(ctx)
|
|
|
|
if outbound == nil {
|
|
|
|
outbound = new(session.Outbound)
|
|
|
|
ctx = session.ContextWithOutbound(ctx, outbound)
|
|
|
|
}
|
|
|
|
outbound.Gateway = h.senderSettings.Via.AsAddress()
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
2017-01-14 18:48:37 -05:00
|
|
|
}
|
2017-01-26 14:46:44 -05:00
|
|
|
|
2021-02-28 15:42:32 -05:00
|
|
|
if h.senderSettings != nil && h.senderSettings.ProxySettings != nil && h.senderSettings.ProxySettings.HasTag() && h.senderSettings.ProxySettings.TransportLayerProxy {
|
2021-02-28 13:47:31 -05:00
|
|
|
tag := h.senderSettings.ProxySettings.Tag
|
|
|
|
newError("transport layer proxying to ", tag, " for dest ", dest).AtDebug().WriteToLog(session.ExportIDToError(ctx))
|
2021-03-06 09:31:46 -05:00
|
|
|
ctx = session.SetTransportLayerProxyTagToContext(ctx, tag)
|
2021-02-28 13:47:31 -05:00
|
|
|
}
|
|
|
|
|
2020-06-22 23:55:00 -04:00
|
|
|
conn, err := internet.Dial(ctx, dest, h.streamSettings)
|
|
|
|
return h.getStatCouterConnection(conn), err
|
|
|
|
}
|
|
|
|
|
2020-08-30 09:17:22 -04:00
|
|
|
func (h *Handler) getStatCouterConnection(conn internet.Connection) internet.Connection {
|
2020-06-22 23:55:00 -04:00
|
|
|
if h.uplinkCounter != nil || h.downlinkCounter != nil {
|
|
|
|
return &internet.StatCouterConnection{
|
|
|
|
Connection: conn,
|
|
|
|
ReadCounter: h.downlinkCounter,
|
|
|
|
WriteCounter: h.uplinkCounter,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return conn
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
2018-02-05 17:38:24 -05:00
|
|
|
|
2018-02-08 11:46:50 -05:00
|
|
|
// GetOutbound implements proxy.GetOutbound.
|
2018-02-05 17:38:24 -05:00
|
|
|
func (h *Handler) GetOutbound() proxy.Outbound {
|
|
|
|
return h.proxy
|
|
|
|
}
|
2018-02-08 09:45:50 -05:00
|
|
|
|
2018-02-08 11:46:50 -05:00
|
|
|
// Start implements common.Runnable.
|
2018-02-08 09:45:50 -05:00
|
|
|
func (h *Handler) Start() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-03 05:11:54 -04:00
|
|
|
// Close implements common.Closable.
|
2018-02-08 09:45:50 -05:00
|
|
|
func (h *Handler) Close() error {
|
2018-02-09 17:07:38 -05:00
|
|
|
common.Close(h.mux)
|
2018-02-08 09:45:50 -05:00
|
|
|
return nil
|
|
|
|
}
|