173 lines
4.6 KiB
Go
Raw Normal View History

2015-12-04 11:07:32 +00:00
package outbound
2015-09-11 00:24:18 +02:00
import (
"context"
2017-01-31 12:42:05 +01:00
"runtime"
2017-01-13 23:42:39 +01:00
"time"
2016-08-20 20:55:45 +02:00
"v2ray.com/core/app"
"v2ray.com/core/app/log"
2016-12-28 00:53:29 +01:00
"v2ray.com/core/common"
2016-12-09 11:35:27 +01:00
"v2ray.com/core/common/buf"
2016-12-09 13:17:34 +01:00
"v2ray.com/core/common/bufio"
2017-01-04 12:34:01 +01:00
"v2ray.com/core/common/errors"
2017-01-13 23:42:39 +01:00
"v2ray.com/core/common/net"
2016-08-20 20:55:45 +02:00
"v2ray.com/core/common/protocol"
"v2ray.com/core/common/retry"
2016-12-30 00:32:20 +01:00
"v2ray.com/core/common/signal"
2016-08-20 20:55:45 +02:00
"v2ray.com/core/proxy"
2016-12-07 21:43:41 +01:00
"v2ray.com/core/proxy/vmess"
2016-08-20 20:55:45 +02:00
"v2ray.com/core/proxy/vmess/encoding"
"v2ray.com/core/transport/internet"
"v2ray.com/core/transport/ray"
2015-09-11 00:24:18 +02:00
)
2016-12-21 12:53:31 +01:00
// VMessOutboundHandler is an outbound connection handler for VMess protocol.
2015-09-11 00:24:18 +02:00
type VMessOutboundHandler struct {
2016-07-25 16:48:09 +02:00
serverList *protocol.ServerList
serverPicker protocol.ServerPicker
2015-09-11 00:24:18 +02:00
}
func New(ctx context.Context, config *Config) (*VMessOutboundHandler, error) {
space := app.SpaceFromContext(ctx)
if space == nil {
return nil, errors.New("VMess|Outbound: No space in context.")
}
serverList := protocol.NewServerList()
for _, rec := range config.Receiver {
serverList.AddServer(protocol.NewServerSpecFromPB(*rec))
}
handler := &VMessOutboundHandler{
serverList: serverList,
serverPicker: protocol.NewRoundRobinServerPicker(serverList),
}
return handler, nil
}
2016-12-21 12:53:31 +01:00
// Dispatch implements OutboundHandler.Dispatch().
func (v *VMessOutboundHandler) Process(ctx context.Context, outboundRay ray.OutboundRay, dialer proxy.Dialer) error {
2016-07-25 16:48:09 +02:00
var rec *protocol.ServerSpec
2016-06-14 22:54:08 +02:00
var conn internet.Connection
2016-06-06 00:10:51 +02:00
2016-11-20 21:47:51 +01:00
err := retry.ExponentialBackoff(5, 100).On(func() error {
2016-11-27 21:39:09 +01:00
rec = v.serverPicker.PickServer()
rawConn, err := dialer.Dial(ctx, rec.Destination())
2016-06-06 00:10:51 +02:00
if err != nil {
return err
}
conn = rawConn
return nil
})
if err != nil {
log.Warning("VMess|Outbound: Failed to find an available destination:", err)
return err
2016-06-06 00:10:51 +02:00
}
defer conn.Close()
target := proxy.DestinationFromContext(ctx)
2016-08-09 23:17:02 +02:00
log.Info("VMess|Outbound: Tunneling request to ", target, " via ", rec.Destination())
2015-09-11 00:24:18 +02:00
2016-05-07 20:26:29 +02:00
command := protocol.RequestCommandTCP
2017-01-13 23:42:39 +01:00
if target.Network == net.Network_UDP {
2016-05-07 20:26:29 +02:00
command = protocol.RequestCommandUDP
2015-09-20 18:22:29 +02:00
}
2016-05-07 20:26:29 +02:00
request := &protocol.RequestHeader{
2016-07-23 13:17:51 +02:00
Version: encoding.Version,
2016-06-06 00:10:51 +02:00
User: rec.PickUser(),
2015-09-20 18:22:29 +02:00
Command: command,
2016-09-20 11:53:05 +02:00
Address: target.Address,
Port: target.Port,
2016-05-07 20:26:29 +02:00
Option: protocol.RequestOptionChunkStream,
2016-02-01 12:22:29 +01:00
}
2016-12-07 21:43:41 +01:00
rawAccount, err := request.User.GetTypedAccount()
if err != nil {
log.Warning("VMess|Outbound: Failed to get user account: ", err)
return err
2016-12-07 21:43:41 +01:00
}
account := rawAccount.(*vmess.InternalAccount)
request.Security = account.Security
2016-06-14 22:54:08 +02:00
conn.SetReusable(true)
if conn.Reusable() { // Conn reuse may be disabled on transportation layer
2016-06-02 21:34:25 +02:00
request.Option.Set(protocol.RequestOptionConnectionReuse)
2016-05-31 00:21:41 +02:00
}
2015-09-16 00:06:22 +02:00
2017-01-04 12:34:01 +01:00
input := outboundRay.OutboundInput()
output := outboundRay.OutboundOutput()
2016-01-05 12:08:16 +01:00
2016-07-23 13:17:51 +02:00
session := encoding.NewClientSession(protocol.DefaultIDHash)
2016-02-27 16:41:21 +01:00
2017-01-31 12:42:05 +01:00
ctx, cancel := context.WithCancel(ctx)
timer := signal.CancelAfterInactivity(ctx, cancel, time.Minute*2)
2016-12-30 00:32:20 +01:00
requestDone := signal.ExecuteAsync(func() error {
writer := bufio.NewWriter(conn)
session.EncodeRequestHeader(request, writer)
2015-09-16 00:06:22 +02:00
2016-12-30 00:32:20 +01:00
bodyWriter := session.EncodeRequestBody(request, writer)
2017-01-04 12:34:01 +01:00
firstPayload, err := input.ReadTimeout(time.Millisecond * 500)
if err != nil && err != ray.ErrReadTimeout {
return errors.Base(err).Message("VMess|Outbound: Failed to get first payload.")
}
if !firstPayload.IsEmpty() {
if err := bodyWriter.Write(firstPayload); err != nil {
return errors.Base(err).Message("VMess|Outbound: Failed to write first payload.")
2016-12-30 00:32:20 +01:00
}
2017-01-04 12:34:01 +01:00
firstPayload.Release()
2016-12-30 00:32:20 +01:00
}
2017-01-04 12:34:01 +01:00
2016-12-30 00:32:20 +01:00
writer.SetBuffered(false)
2016-12-07 17:32:40 +01:00
2017-01-31 12:42:05 +01:00
if err := buf.PipeUntilEOF(timer, input, bodyWriter); err != nil {
2016-12-30 00:32:20 +01:00
return err
}
2016-05-12 17:20:07 -07:00
2016-12-30 00:32:20 +01: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()
reader := bufio.NewReader(conn)
header, err := session.DecodeResponseHeader(reader)
2016-06-02 21:34:25 +02:00
if err != nil {
2016-12-30 00:32:20 +01:00
return err
2016-06-02 21:34:25 +02:00
}
2016-12-30 00:32:20 +01:00
v.handleCommand(rec.Destination(), header.Command)
2015-09-11 00:24:18 +02:00
2016-12-30 00:32:20 +01:00
conn.SetReusable(header.Option.Has(protocol.ResponseOptionConnectionReuse))
2015-10-06 09:35:02 +02:00
2016-12-30 00:32:20 +01:00
reader.SetBuffered(false)
bodyReader := session.DecodeResponseBody(request, reader)
2017-01-31 12:42:05 +01:00
if err := buf.PipeUntilEOF(timer, bodyReader, output); err != nil {
2016-12-30 00:32:20 +01:00
return err
}
2016-06-02 21:34:25 +02:00
2016-12-30 00:32:20 +01:00
return nil
})
2015-10-03 11:34:01 +02:00
if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
2016-12-30 00:32:20 +01:00
log.Info("VMess|Outbound: Connection ending with ", err)
2016-06-01 22:09:34 +02:00
conn.SetReusable(false)
return err
2016-06-01 22:09:34 +02:00
}
2017-01-31 12:42:05 +01:00
runtime.KeepAlive(timer)
2016-06-01 22:09:34 +02:00
return nil
2015-09-11 00:24:18 +02:00
}
2016-06-14 22:54:08 +02:00
func init() {
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
return New(ctx, config.(*Config))
}))
2015-09-11 00:24:18 +02:00
}