2015-12-04 11:07:32 +00:00
|
|
|
package outbound
|
2015-09-10 22:24:18 +00:00
|
|
|
|
2017-04-08 23:43:25 +00:00
|
|
|
//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg outbound -path Proxy,VMess,Outbound
|
|
|
|
|
2015-09-10 22:24:18 +00:00
|
|
|
import (
|
2017-01-12 23:56:21 +00:00
|
|
|
"context"
|
2017-01-31 11:42:05 +00:00
|
|
|
"runtime"
|
2017-01-13 22:42:39 +00:00
|
|
|
"time"
|
2017-01-12 23:56:21 +00:00
|
|
|
|
2016-08-20 18:55:45 +00:00
|
|
|
"v2ray.com/core/app"
|
2017-02-03 21:35:09 +00:00
|
|
|
"v2ray.com/core/app/log"
|
2016-12-27 23:53:29 +00:00
|
|
|
"v2ray.com/core/common"
|
2016-12-09 10:35:27 +00:00
|
|
|
"v2ray.com/core/common/buf"
|
2017-01-13 22:42:39 +00:00
|
|
|
"v2ray.com/core/common/net"
|
2016-08-20 18:55:45 +00:00
|
|
|
"v2ray.com/core/common/protocol"
|
|
|
|
"v2ray.com/core/common/retry"
|
2016-12-29 23:32:20 +00:00
|
|
|
"v2ray.com/core/common/signal"
|
2016-08-20 18:55:45 +00:00
|
|
|
"v2ray.com/core/proxy"
|
2016-12-07 20:43:41 +00:00
|
|
|
"v2ray.com/core/proxy/vmess"
|
2016-08-20 18:55:45 +00:00
|
|
|
"v2ray.com/core/proxy/vmess/encoding"
|
|
|
|
"v2ray.com/core/transport/internet"
|
|
|
|
"v2ray.com/core/transport/ray"
|
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 {
|
2016-07-25 14:48:09 +00:00
|
|
|
serverList *protocol.ServerList
|
|
|
|
serverPicker protocol.ServerPicker
|
2015-09-10 22:24:18 +00:00
|
|
|
}
|
|
|
|
|
2017-02-13 12:16:37 +00:00
|
|
|
func New(ctx context.Context, config *Config) (*Handler, error) {
|
2017-01-12 23:56:21 +00:00
|
|
|
space := app.SpaceFromContext(ctx)
|
|
|
|
if space == nil {
|
2017-04-08 23:43:25 +00:00
|
|
|
return nil, newError("no space in context.")
|
2017-01-12 23:56:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
serverList := protocol.NewServerList()
|
|
|
|
for _, rec := range config.Receiver {
|
|
|
|
serverList.AddServer(protocol.NewServerSpecFromPB(*rec))
|
|
|
|
}
|
2017-02-13 12:16:37 +00:00
|
|
|
handler := &Handler{
|
2017-01-12 23:56:21 +00:00
|
|
|
serverList: serverList,
|
|
|
|
serverPicker: protocol.NewRoundRobinServerPicker(serverList),
|
|
|
|
}
|
|
|
|
|
|
|
|
return handler, nil
|
|
|
|
}
|
|
|
|
|
2017-02-13 12:16:37 +00:00
|
|
|
// Process implements proxy.Outbound.Process().
|
|
|
|
func (v *Handler) Process(ctx context.Context, outboundRay ray.OutboundRay, dialer proxy.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 {
|
2016-11-27 20:39:09 +00:00
|
|
|
rec = v.serverPicker.PickServer()
|
2017-01-26 19:46:44 +00:00
|
|
|
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
|
|
|
}
|
2017-01-26 19:46:44 +00:00
|
|
|
defer conn.Close()
|
|
|
|
|
2017-02-09 21:49:38 +00:00
|
|
|
target, ok := proxy.TargetFromContext(ctx)
|
|
|
|
if !ok {
|
2017-04-08 23:43:25 +00:00
|
|
|
return newError("target not specified").AtError()
|
2017-02-09 21:49:38 +00:00
|
|
|
}
|
2017-04-08 23:43:25 +00:00
|
|
|
log.Trace(newError("tunneling request to ", target, " via ", rec.Destination()))
|
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
|
|
|
}
|
2016-05-07 18:26:29 +00:00
|
|
|
request := &protocol.RequestHeader{
|
2016-07-23 11:17:51 +00:00
|
|
|
Version: encoding.Version,
|
2016-06-05 22:10:51 +00:00
|
|
|
User: rec.PickUser(),
|
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
|
|
|
}
|
2015-10-07 12:50:17 +00:00
|
|
|
|
2016-12-07 20:43:41 +00:00
|
|
|
rawAccount, err := request.User.GetTypedAccount()
|
|
|
|
if err != nil {
|
2017-04-08 23:43:25 +00:00
|
|
|
return newError("failed to get user account").Base(err).AtWarning()
|
2016-12-07 20:43:41 +00:00
|
|
|
}
|
|
|
|
account := rawAccount.(*vmess.InternalAccount)
|
|
|
|
request.Security = account.Security
|
|
|
|
|
2017-02-14 13:16:43 +00: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 11:34:01 +00:00
|
|
|
input := outboundRay.OutboundInput()
|
|
|
|
output := outboundRay.OutboundOutput()
|
2016-01-05 11:08:16 +00:00
|
|
|
|
2016-07-23 11:17:51 +00:00
|
|
|
session := encoding.NewClientSession(protocol.DefaultIDHash)
|
2016-02-27 15:41:21 +00:00
|
|
|
|
2017-03-31 19:45:43 +00:00
|
|
|
ctx, timer := signal.CancelAfterInactivity(ctx, time.Minute*2)
|
2017-01-31 11:42:05 +00:00
|
|
|
|
2016-12-29 23:32:20 +00:00
|
|
|
requestDone := signal.ExecuteAsync(func() error {
|
2017-02-15 21:51:01 +00:00
|
|
|
writer := buf.NewBufferedWriter(conn)
|
2016-12-29 23:32:20 +00:00
|
|
|
session.EncodeRequestHeader(request, writer)
|
2015-09-15 22:06:22 +00:00
|
|
|
|
2016-12-29 23:32:20 +00:00
|
|
|
bodyWriter := session.EncodeRequestBody(request, writer)
|
2017-01-04 11:34:01 +00:00
|
|
|
firstPayload, err := input.ReadTimeout(time.Millisecond * 500)
|
2017-03-27 09:14:55 +00:00
|
|
|
if err != nil && err != buf.ErrReadTimeout {
|
2017-04-08 23:43:25 +00:00
|
|
|
return newError("failed to get first payload").Base(err)
|
2017-01-04 11:34:01 +00:00
|
|
|
}
|
|
|
|
if !firstPayload.IsEmpty() {
|
|
|
|
if err := bodyWriter.Write(firstPayload); err != nil {
|
2017-04-08 23:43:25 +00:00
|
|
|
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
|
|
|
firstPayload.Release()
|
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-08-15 10:23:35 +00:00
|
|
|
}
|
2016-05-13 00:20:07 +00:00
|
|
|
|
2016-12-29 23:32:20 +00:00
|
|
|
if request.Option.Has(protocol.RequestOptionChunkStream) {
|
2017-04-15 19:07:23 +00:00
|
|
|
if err := bodyWriter.Write(buf.NewMultiBuffer()); err != nil {
|
2016-12-29 23:32:20 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
responseDone := signal.ExecuteAsync(func() error {
|
|
|
|
defer output.Close()
|
|
|
|
|
2017-02-15 21:51:01 +00:00
|
|
|
reader := buf.NewBufferedReader(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 {
|
2016-12-29 23:32:20 +00:00
|
|
|
return err
|
2016-06-02 19:34:25 +00:00
|
|
|
}
|
2016-12-29 23:32:20 +00:00
|
|
|
v.handleCommand(rec.Destination(), header.Command)
|
2015-09-10 22:24:18 +00:00
|
|
|
|
2016-12-29 23:32:20 +00:00
|
|
|
reader.SetBuffered(false)
|
|
|
|
bodyReader := session.DecodeResponseBody(request, reader)
|
2017-04-27 20:30:48 +00:00
|
|
|
if err := buf.Copy(bodyReader, output, buf.UpdateActivity(timer)); err != nil {
|
2016-12-29 23:32:20 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-06-02 19:34:25 +00:00
|
|
|
|
2016-12-29 23:32:20 +00:00
|
|
|
return nil
|
|
|
|
})
|
2015-10-03 09:34:01 +00:00
|
|
|
|
2017-01-28 20:24:46 +00:00
|
|
|
if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
|
2017-04-08 23:43:25 +00:00
|
|
|
return newError("connection ends").Base(err)
|
2016-06-01 20:09:34 +00:00
|
|
|
}
|
2017-01-31 11:42:05 +00:00
|
|
|
runtime.KeepAlive(timer)
|
2016-06-01 20:09:34 +00:00
|
|
|
|
2017-01-26 19:46:44 +00:00
|
|
|
return nil
|
2015-09-10 22:24:18 +00:00
|
|
|
}
|
2016-06-14 20:54:08 +00:00
|
|
|
|
|
|
|
func init() {
|
2017-01-12 23:56:21 +00:00
|
|
|
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
|
|
|
return New(ctx, config.(*Config))
|
|
|
|
}))
|
2015-09-10 22:24:18 +00:00
|
|
|
}
|