1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-02-20 23:47:21 -05:00
v2fly/proxy/vmess/outbound/outbound.go

176 lines
4.8 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 (
2017-01-04 12:34:01 +01:00
"time"
2016-08-20 20:55:45 +02:00
"v2ray.com/core/app"
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"
2016-08-20 20:55:45 +02:00
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/common/protocol"
"v2ray.com/core/common/retry"
2016-12-15 11:51:09 +01:00
"v2ray.com/core/common/serial"
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
meta *proxy.OutboundHandlerMeta
2015-09-11 00:24:18 +02:00
}
2016-12-21 12:53:31 +01:00
// Dispatch implements OutboundHandler.Dispatch().
2017-01-04 12:34:01 +01:00
func (v *VMessOutboundHandler) Dispatch(target v2net.Destination, outboundRay ray.OutboundRay) {
defer outboundRay.OutboundInput().ForceClose()
defer outboundRay.OutboundOutput().Close()
2016-05-07 00:19:06 +02:00
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 := internet.Dial(v.meta.Address, rec.Destination(), v.meta.GetDialerOptions())
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
2016-06-06 00:10:51 +02:00
}
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
2016-09-20 11:53:05 +02:00
if target.Network == v2net.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)
}
account := rawAccount.(*vmess.InternalAccount)
request.Security = account.Security
2015-09-11 00:24:18 +02:00
defer conn.Close()
2016-06-02 21:34:25 +02:00
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
2016-12-30 00:32:20 +01:00
requestDone := signal.ExecuteAsync(func() error {
2016-12-30 00:51:39 +01:00
defer input.ForceClose()
2016-12-30 00:32:20 +01:00
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
2016-12-30 00:32:20 +01:00
if err := buf.PipeUntilEOF(input, bodyWriter); err != nil {
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)
if err := buf.PipeUntilEOF(bodyReader, output); err != nil {
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
2016-12-30 00:32:20 +01:00
if err := signal.ErrorOrFinish2(requestDone, responseDone); err != nil {
log.Info("VMess|Outbound: Connection ending with ", err)
2016-06-01 22:09:34 +02:00
conn.SetReusable(false)
}
2015-09-18 13:19:12 +02:00
return
2015-09-11 00:24:18 +02:00
}
2016-06-14 22:54:08 +02:00
2016-12-21 12:53:31 +01:00
// Factory is a proxy factory for VMess outbound.
2016-06-14 22:54:08 +02:00
type Factory struct{}
2016-11-27 21:39:09 +01:00
func (v *Factory) StreamCapability() v2net.NetworkList {
2016-10-02 23:43:58 +02:00
return v2net.NetworkList{
Network: []v2net.Network{v2net.Network_TCP, v2net.Network_KCP, v2net.Network_WebSocket},
}
}
2016-11-27 21:39:09 +01:00
func (v *Factory) Create(space app.Space, rawConfig interface{}, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) {
2016-06-14 22:54:08 +02:00
vOutConfig := rawConfig.(*Config)
2016-07-25 16:48:09 +02:00
serverList := protocol.NewServerList()
2016-09-24 23:11:58 +02:00
for _, rec := range vOutConfig.Receiver {
2016-11-02 16:33:04 +01:00
serverList.AddServer(protocol.NewServerSpecFromPB(*rec))
2016-07-25 16:48:09 +02:00
}
2016-06-14 22:54:08 +02:00
handler := &VMessOutboundHandler{
2016-07-25 16:48:09 +02:00
serverList: serverList,
serverPicker: protocol.NewRoundRobinServerPicker(serverList),
meta: meta,
2016-06-14 22:54:08 +02:00
}
2016-06-14 22:54:08 +02:00
return handler, nil
}
func init() {
2016-12-28 00:53:29 +01:00
common.Must(proxy.RegisterOutboundHandlerCreator(serial.GetMessageType(new(Config)), new(Factory)))
2015-09-11 00:24:18 +02:00
}