v2fly/proxy/vmess/outbound/outbound.go

212 lines
6.4 KiB
Go
Raw Normal View History

2019-02-01 19:08:21 +00:00
// +build !confonly
2015-12-04 11:07:32 +00:00
package outbound
2015-09-10 22:24:18 +00:00
2021-02-16 20:31:50 +00:00
//go:generate go run github.com/v2fly/v2ray-core/v4/common/errors/errorgen
2017-04-08 23:43:25 +00:00
2015-09-10 22:24:18 +00:00
import (
"context"
2017-01-13 22:42:39 +00:00
"time"
2021-02-16 20:31:50 +00:00
core "github.com/v2fly/v2ray-core/v4"
"github.com/v2fly/v2ray-core/v4/common"
"github.com/v2fly/v2ray-core/v4/common/buf"
"github.com/v2fly/v2ray-core/v4/common/net"
"github.com/v2fly/v2ray-core/v4/common/platform"
"github.com/v2fly/v2ray-core/v4/common/protocol"
"github.com/v2fly/v2ray-core/v4/common/retry"
"github.com/v2fly/v2ray-core/v4/common/session"
"github.com/v2fly/v2ray-core/v4/common/signal"
"github.com/v2fly/v2ray-core/v4/common/task"
"github.com/v2fly/v2ray-core/v4/features/policy"
"github.com/v2fly/v2ray-core/v4/proxy/vmess"
"github.com/v2fly/v2ray-core/v4/proxy/vmess/encoding"
"github.com/v2fly/v2ray-core/v4/transport"
"github.com/v2fly/v2ray-core/v4/transport/internet"
2015-09-10 22:24:18 +00:00
)
2017-02-13 12:16:37 +00:00
// Handler is an outbound connection handler for VMess protocol.
type Handler struct {
2018-10-21 08:27:13 +00:00
serverList *protocol.ServerList
serverPicker protocol.ServerPicker
policyManager policy.Manager
2015-09-10 22:24:18 +00:00
}
2018-05-25 21:20:24 +00:00
// New creates a new VMess outbound handler.
2017-02-13 12:16:37 +00:00
func New(ctx context.Context, config *Config) (*Handler, error) {
serverList := protocol.NewServerList()
for _, rec := range config.Receiver {
2020-08-26 11:35:33 +00:00
s, err := protocol.NewServerSpecFromPB(rec)
2018-08-26 22:11:32 +00:00
if err != nil {
return nil, newError("failed to parse server spec").Base(err)
}
serverList.AddServer(s)
}
2018-10-21 08:27:13 +00:00
v := core.MustFromContext(ctx)
2017-02-13 12:16:37 +00:00
handler := &Handler{
2018-10-21 08:27:13 +00:00
serverList: serverList,
serverPicker: protocol.NewRoundRobinServerPicker(serverList),
policyManager: v.GetFeature(policy.ManagerType()).(policy.Manager),
}
2017-11-27 21:09:30 +00:00
return handler, nil
}
2017-02-13 12:16:37 +00:00
// Process implements proxy.Outbound.Process().
2020-09-21 01:10:56 +00:00
func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer internet.Dialer) error {
2016-07-25 14:48:09 +00:00
var rec *protocol.ServerSpec
2016-06-14 20:54:08 +00:00
var conn internet.Connection
2016-06-05 22:10:51 +00:00
2017-02-10 10:41:50 +00:00
err := retry.ExponentialBackoff(5, 200).On(func() error {
2020-09-21 01:10:56 +00:00
rec = h.serverPicker.PickServer()
rawConn, err := dialer.Dial(ctx, rec.Destination())
2016-06-05 22:10:51 +00:00
if err != nil {
return err
}
conn = rawConn
return nil
})
if err != nil {
2017-04-08 23:43:25 +00:00
return newError("failed to find an available destination").Base(err).AtWarning()
2016-06-05 22:10:51 +00:00
}
defer conn.Close()
outbound := session.OutboundFromContext(ctx)
if outbound == nil || !outbound.Target.IsValid() {
2017-04-08 23:43:25 +00:00
return newError("target not specified").AtError()
2017-02-09 21:49:38 +00:00
}
target := outbound.Target
newError("tunneling request to ", target, " via ", rec.Destination()).WriteToLog(session.ExportIDToError(ctx))
2015-09-10 22:24:18 +00:00
2016-05-07 18:26:29 +00:00
command := protocol.RequestCommandTCP
2017-01-13 22:42:39 +00:00
if target.Network == net.Network_UDP {
2016-05-07 18:26:29 +00:00
command = protocol.RequestCommandUDP
2015-09-20 16:22:29 +00:00
}
2018-02-11 23:06:51 +00:00
if target.Address.Family().IsDomain() && target.Address.Domain() == "v1.mux.cool" {
command = protocol.RequestCommandMux
}
user := rec.PickUser()
2016-05-07 18:26:29 +00:00
request := &protocol.RequestHeader{
2016-07-23 11:17:51 +00:00
Version: encoding.Version,
User: user,
2015-09-20 16:22:29 +00:00
Command: command,
2016-09-20 09:53:05 +00:00
Address: target.Address,
Port: target.Port,
2016-05-07 18:26:29 +00:00
Option: protocol.RequestOptionChunkStream,
2016-02-01 11:22:29 +00:00
}
2018-10-18 07:25:58 +00:00
account := request.User.Account.(*vmess.MemoryAccount)
2016-12-07 20:43:41 +00:00
request.Security = account.Security
if request.Security == protocol.SecurityType_AES128_GCM || request.Security == protocol.SecurityType_NONE || request.Security == protocol.SecurityType_CHACHA20_POLY1305 {
2017-02-14 13:16:43 +00:00
request.Option.Set(protocol.RequestOptionChunkMasking)
}
if shouldEnablePadding(request.Security) && request.Option.Has(protocol.RequestOptionChunkMasking) {
request.Option.Set(protocol.RequestOptionGlobalPadding)
}
if request.Security == protocol.SecurityType_ZERO {
request.Security = protocol.SecurityType_NONE
request.Option.Clear(protocol.RequestOptionChunkStream)
request.Option.Clear(protocol.RequestOptionChunkMasking)
}
2018-04-16 22:31:10 +00:00
input := link.Reader
output := link.Writer
2016-01-05 11:08:16 +00:00
2020-09-21 01:10:56 +00:00
isAEAD := false
if !aeadDisabled && len(account.AlterIDs) == 0 {
2020-09-21 01:10:56 +00:00
isAEAD = true
}
session := encoding.NewClientSession(ctx, isAEAD, protocol.DefaultIDHash)
2020-09-21 01:10:56 +00:00
sessionPolicy := h.policyManager.ForLevel(request.User.Level)
2016-02-27 15:41:21 +00:00
2017-11-14 23:36:14 +00:00
ctx, cancel := context.WithCancel(ctx)
timer := signal.CancelAfterInactivity(ctx, cancel, sessionPolicy.Timeouts.ConnectionIdle)
2017-01-31 11:42:05 +00:00
2018-04-11 14:45:09 +00:00
requestDone := func() error {
2018-02-19 16:50:21 +00:00
defer timer.SetTimeout(sessionPolicy.Timeouts.DownlinkOnly)
2017-11-09 21:33:15 +00:00
writer := buf.NewBufferedWriter(buf.NewWriter(conn))
if err := session.EncodeRequestHeader(request, writer); err != nil {
return newError("failed to encode request").Base(err).AtWarning()
}
2015-09-15 22:06:22 +00:00
2016-12-29 23:32:20 +00:00
bodyWriter := session.EncodeRequestBody(request, writer)
2018-08-21 21:09:45 +00:00
if err := buf.CopyOnceTimeout(input, bodyWriter, time.Millisecond*100); err != nil && err != buf.ErrNotTimeoutReader && err != buf.ErrReadTimeout {
return newError("failed to write first payload").Base(err)
2016-12-29 23:32:20 +00:00
}
2017-01-04 11:34:01 +00:00
2017-03-27 09:26:44 +00:00
if err := writer.SetBuffered(false); err != nil {
return err
}
2017-04-27 20:30:48 +00:00
if err := buf.Copy(input, bodyWriter, buf.UpdateActivity(timer)); err != nil {
2016-12-29 23:32:20 +00:00
return err
}
2016-05-13 00:20:07 +00:00
2016-12-29 23:32:20 +00:00
if request.Option.Has(protocol.RequestOptionChunkStream) {
2017-11-09 21:33:15 +00:00
if err := bodyWriter.WriteMultiBuffer(buf.MultiBuffer{}); err != nil {
2016-12-29 23:32:20 +00:00
return err
}
}
2018-02-19 16:50:21 +00:00
2016-12-29 23:32:20 +00:00
return nil
2018-04-11 14:45:09 +00:00
}
2016-12-29 23:32:20 +00:00
2018-04-11 14:45:09 +00:00
responseDone := func() error {
defer timer.SetTimeout(sessionPolicy.Timeouts.UplinkOnly)
2016-12-29 23:32:20 +00:00
reader := &buf.BufferedReader{Reader: buf.NewReader(conn)}
2016-12-29 23:32:20 +00:00
header, err := session.DecodeResponseHeader(reader)
2016-06-02 19:34:25 +00:00
if err != nil {
2018-02-19 16:50:21 +00:00
return newError("failed to read header").Base(err)
2016-06-02 19:34:25 +00:00
}
2020-09-21 01:10:56 +00:00
h.handleCommand(rec.Destination(), header.Command)
2015-09-10 22:24:18 +00:00
2016-12-29 23:32:20 +00:00
bodyReader := session.DecodeResponseBody(request, reader)
2018-02-19 16:50:21 +00:00
2017-11-23 22:46:46 +00:00
return buf.Copy(bodyReader, output, buf.UpdateActivity(timer))
2018-04-11 14:45:09 +00:00
}
2015-10-03 09:34:01 +00:00
2021-05-19 21:28:52 +00:00
responseDonePost := task.OnSuccess(responseDone, task.Close(output))
2018-12-06 10:35:02 +00:00
if err := task.Run(ctx, requestDone, responseDonePost); err != nil {
2017-04-08 23:43:25 +00:00
return newError("connection ends").Base(err)
2016-06-01 20:09:34 +00:00
}
return nil
2015-09-10 22:24:18 +00:00
}
2016-06-14 20:54:08 +00:00
2018-07-18 08:40:28 +00:00
var (
enablePadding = false
aeadDisabled = false
2018-07-18 08:40:28 +00:00
)
func shouldEnablePadding(s protocol.SecurityType) bool {
return enablePadding || s == protocol.SecurityType_AES128_GCM || s == protocol.SecurityType_CHACHA20_POLY1305 || s == protocol.SecurityType_AUTO
}
2016-06-14 20:54:08 +00:00
func init() {
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
return New(ctx, config.(*Config))
}))
2018-07-18 08:40:28 +00:00
const defaultFlagValue = "NOT_DEFINED_AT_ALL"
2020-09-21 03:20:04 +00:00
2018-07-18 08:40:28 +00:00
paddingValue := platform.NewEnvFlag("v2ray.vmess.padding").GetValue(func() string { return defaultFlagValue })
if paddingValue != defaultFlagValue {
enablePadding = true
}
2020-09-21 03:20:04 +00:00
isAeadDisabled := platform.NewEnvFlag("v2ray.vmess.aead.disabled").GetValue(func() string { return defaultFlagValue })
if isAeadDisabled == "true" {
aeadDisabled = true
2020-09-21 03:20:04 +00:00
}
2015-09-10 22:24:18 +00:00
}