1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-27 22:36:12 -04:00
v2fly/proxy/vmess/outbound/outbound.go

174 lines
4.9 KiB
Go
Raw Normal View History

2015-12-04 06:07:32 -05:00
package outbound
2015-09-10 18:24:18 -04:00
import (
"context"
2017-01-31 06:42:05 -05:00
"runtime"
2017-01-13 17:42:39 -05:00
"time"
2016-08-20 14:55:45 -04:00
"v2ray.com/core/app"
"v2ray.com/core/app/log"
2016-12-27 18:53:29 -05:00
"v2ray.com/core/common"
2016-12-09 05:35:27 -05:00
"v2ray.com/core/common/buf"
2017-01-04 06:34:01 -05:00
"v2ray.com/core/common/errors"
2017-01-13 17:42:39 -05:00
"v2ray.com/core/common/net"
2016-08-20 14:55:45 -04:00
"v2ray.com/core/common/protocol"
"v2ray.com/core/common/retry"
2016-12-29 18:32:20 -05:00
"v2ray.com/core/common/signal"
2016-08-20 14:55:45 -04:00
"v2ray.com/core/proxy"
2016-12-07 15:43:41 -05:00
"v2ray.com/core/proxy/vmess"
2016-08-20 14:55:45 -04:00
"v2ray.com/core/proxy/vmess/encoding"
"v2ray.com/core/transport/internet"
"v2ray.com/core/transport/ray"
2015-09-10 18:24:18 -04:00
)
2017-02-13 07:16:37 -05:00
// Handler is an outbound connection handler for VMess protocol.
type Handler struct {
2016-07-25 10:48:09 -04:00
serverList *protocol.ServerList
serverPicker protocol.ServerPicker
2015-09-10 18:24:18 -04:00
}
2017-02-13 07:16:37 -05:00
func New(ctx context.Context, config *Config) (*Handler, error) {
space := app.SpaceFromContext(ctx)
if space == nil {
2017-04-07 16:56:27 -04:00
return nil, errors.New("no space in context.").Path("Proxy", "VMess", "Outbound")
}
serverList := protocol.NewServerList()
for _, rec := range config.Receiver {
serverList.AddServer(protocol.NewServerSpecFromPB(*rec))
}
2017-02-13 07:16:37 -05:00
handler := &Handler{
serverList: serverList,
serverPicker: protocol.NewRoundRobinServerPicker(serverList),
}
return handler, nil
}
2017-02-13 07:16:37 -05:00
// Process implements proxy.Outbound.Process().
func (v *Handler) Process(ctx context.Context, outboundRay ray.OutboundRay, dialer proxy.Dialer) error {
2016-07-25 10:48:09 -04:00
var rec *protocol.ServerSpec
2016-06-14 16:54:08 -04:00
var conn internet.Connection
2016-06-05 18:10:51 -04:00
2017-02-10 05:41:50 -05:00
err := retry.ExponentialBackoff(5, 200).On(func() error {
2016-11-27 15:39:09 -05:00
rec = v.serverPicker.PickServer()
rawConn, err := dialer.Dial(ctx, rec.Destination())
2016-06-05 18:10:51 -04:00
if err != nil {
return err
}
conn = rawConn
return nil
})
if err != nil {
2017-04-07 16:56:27 -04:00
return errors.New("failed to find an available destination").Base(err).AtWarning().Path("Proxy", "VMess", "Outbound")
2016-06-05 18:10:51 -04:00
}
defer conn.Close()
2017-02-09 16:49:38 -05:00
target, ok := proxy.TargetFromContext(ctx)
if !ok {
2017-04-07 16:56:27 -04:00
return errors.New("target not specified").Path("Proxy", "VMess", "Outbound").AtError()
2017-02-09 16:49:38 -05:00
}
2017-04-07 16:56:27 -04:00
log.Trace(errors.New("tunneling request to ", target, " via ", rec.Destination()).Path("Proxy", "VMess", "Outbound"))
2015-09-10 18:24:18 -04:00
2016-05-07 14:26:29 -04:00
command := protocol.RequestCommandTCP
2017-01-13 17:42:39 -05:00
if target.Network == net.Network_UDP {
2016-05-07 14:26:29 -04:00
command = protocol.RequestCommandUDP
2015-09-20 12:22:29 -04:00
}
2016-05-07 14:26:29 -04:00
request := &protocol.RequestHeader{
2016-07-23 07:17:51 -04:00
Version: encoding.Version,
2016-06-05 18:10:51 -04:00
User: rec.PickUser(),
2015-09-20 12:22:29 -04:00
Command: command,
2016-09-20 05:53:05 -04:00
Address: target.Address,
Port: target.Port,
2016-05-07 14:26:29 -04:00
Option: protocol.RequestOptionChunkStream,
2016-02-01 06:22:29 -05:00
}
2016-12-07 15:43:41 -05:00
rawAccount, err := request.User.GetTypedAccount()
if err != nil {
2017-04-07 16:56:27 -04:00
return errors.New("failed to get user account").Base(err).AtWarning().Path("Proxy", "VMess", "Outbound")
2016-12-07 15:43:41 -05:00
}
account := rawAccount.(*vmess.InternalAccount)
request.Security = account.Security
2017-02-14 08:16:43 -05:00
if request.Security.Is(protocol.SecurityType_AES128_GCM) || request.Security.Is(protocol.SecurityType_NONE) || request.Security.Is(protocol.SecurityType_CHACHA20_POLY1305) {
request.Option.Set(protocol.RequestOptionChunkMasking)
}
2017-01-04 06:34:01 -05:00
input := outboundRay.OutboundInput()
output := outboundRay.OutboundOutput()
2016-01-05 06:08:16 -05:00
2016-07-23 07:17:51 -04:00
session := encoding.NewClientSession(protocol.DefaultIDHash)
2016-02-27 10:41:21 -05:00
2017-03-31 15:45:43 -04:00
ctx, timer := signal.CancelAfterInactivity(ctx, time.Minute*2)
2017-01-31 06:42:05 -05:00
2016-12-29 18:32:20 -05:00
requestDone := signal.ExecuteAsync(func() error {
2017-02-15 16:51:01 -05:00
writer := buf.NewBufferedWriter(conn)
2016-12-29 18:32:20 -05:00
session.EncodeRequestHeader(request, writer)
2015-09-15 18:06:22 -04:00
2016-12-29 18:32:20 -05:00
bodyWriter := session.EncodeRequestBody(request, writer)
2017-01-04 06:34:01 -05:00
firstPayload, err := input.ReadTimeout(time.Millisecond * 500)
2017-03-27 05:14:55 -04:00
if err != nil && err != buf.ErrReadTimeout {
2017-04-07 16:56:27 -04:00
return errors.New("failed to get first payload").Base(err).Path("Proxy", "VMess", "Outbound")
2017-01-04 06:34:01 -05:00
}
if !firstPayload.IsEmpty() {
if err := bodyWriter.Write(firstPayload); err != nil {
2017-04-07 16:56:27 -04:00
return errors.New("failed to write first payload").Base(err).Path("Proxy", "VMess", "Outbound")
2016-12-29 18:32:20 -05:00
}
2017-01-04 06:34:01 -05:00
firstPayload.Release()
2016-12-29 18:32:20 -05:00
}
2017-01-04 06:34:01 -05:00
2017-03-27 05:26:44 -04:00
if err := writer.SetBuffered(false); err != nil {
return err
}
var inputReader buf.Reader = input
if request.Command == protocol.RequestCommandTCP {
inputReader = buf.NewMergingReader(input)
}
2016-12-07 11:32:40 -05:00
2017-03-27 05:26:44 -04:00
if err := buf.PipeUntilEOF(timer, inputReader, bodyWriter); err != nil {
2016-12-29 18:32:20 -05:00
return err
}
2016-05-12 20:20:07 -04:00
2016-12-29 18:32:20 -05:00
if request.Option.Has(protocol.RequestOptionChunkStream) {
if err := bodyWriter.Write(buf.NewLocal(8)); err != nil {
return err
}
}
return nil
})
responseDone := signal.ExecuteAsync(func() error {
defer output.Close()
2017-02-15 16:51:01 -05:00
reader := buf.NewBufferedReader(conn)
2016-12-29 18:32:20 -05:00
header, err := session.DecodeResponseHeader(reader)
2016-06-02 15:34:25 -04:00
if err != nil {
2016-12-29 18:32:20 -05:00
return err
2016-06-02 15:34:25 -04:00
}
2016-12-29 18:32:20 -05:00
v.handleCommand(rec.Destination(), header.Command)
2015-09-10 18:24:18 -04:00
2016-12-29 18:32:20 -05:00
reader.SetBuffered(false)
bodyReader := session.DecodeResponseBody(request, reader)
2017-01-31 06:42:05 -05:00
if err := buf.PipeUntilEOF(timer, bodyReader, output); err != nil {
2016-12-29 18:32:20 -05:00
return err
}
2016-06-02 15:34:25 -04:00
2016-12-29 18:32:20 -05:00
return nil
})
2015-10-03 05:34:01 -04:00
if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
2017-04-07 16:56:27 -04:00
return errors.New("connection ends").Base(err).Path("Proxy", "VMess", "Outbound")
2016-06-01 16:09:34 -04:00
}
2017-01-31 06:42:05 -05:00
runtime.KeepAlive(timer)
2016-06-01 16:09:34 -04:00
return nil
2015-09-10 18:24:18 -04:00
}
2016-06-14 16:54:08 -04:00
func init() {
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
return New(ctx, config.(*Config))
}))
2015-09-10 18:24:18 -04:00
}