2015-12-04 11:07:32 +00:00
|
|
|
package outbound
|
2015-09-10 22:24:18 +00:00
|
|
|
|
|
|
|
import (
|
2015-09-23 12:14:53 +00:00
|
|
|
"sync"
|
2015-09-10 22:24:18 +00:00
|
|
|
|
2016-08-20 18:55:45 +00:00
|
|
|
"v2ray.com/core/app"
|
2016-12-09 10:35:27 +00:00
|
|
|
"v2ray.com/core/common/buf"
|
2016-12-09 12:17:34 +00:00
|
|
|
"v2ray.com/core/common/bufio"
|
2016-08-20 18:55:45 +00: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 10:51:09 +00:00
|
|
|
"v2ray.com/core/common/serial"
|
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
|
|
|
)
|
|
|
|
|
2016-12-21 11:53:31 +00:00
|
|
|
// VMessOutboundHandler is an outbound connection handler for VMess protocol.
|
2015-09-10 22:24:18 +00:00
|
|
|
type VMessOutboundHandler struct {
|
2016-07-25 14:48:09 +00:00
|
|
|
serverList *protocol.ServerList
|
|
|
|
serverPicker protocol.ServerPicker
|
|
|
|
meta *proxy.OutboundHandlerMeta
|
2015-09-10 22:24:18 +00:00
|
|
|
}
|
|
|
|
|
2016-12-21 11:53:31 +00:00
|
|
|
// Dispatch implements OutboundHandler.Dispatch().
|
2016-12-09 10:35:27 +00:00
|
|
|
func (v *VMessOutboundHandler) Dispatch(target v2net.Destination, payload *buf.Buffer, ray ray.OutboundRay) {
|
2016-05-06 22:19:06 +00:00
|
|
|
defer ray.OutboundInput().Release()
|
|
|
|
defer ray.OutboundOutput().Close()
|
|
|
|
|
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
|
|
|
|
2016-11-20 20:47:51 +00:00
|
|
|
err := retry.ExponentialBackoff(5, 100).On(func() error {
|
2016-11-27 20:39:09 +00:00
|
|
|
rec = v.serverPicker.PickServer()
|
|
|
|
rawConn, err := internet.Dial(v.meta.Address, rec.Destination(), v.meta.GetDialerOptions())
|
2016-06-05 22:10:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
conn = rawConn
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2016-11-27 22:57:19 +00:00
|
|
|
log.Warning("VMess|Outbound: Failed to find an available destination:", err)
|
|
|
|
return
|
2016-06-05 22:10:51 +00:00
|
|
|
}
|
2016-08-09 21:17:02 +00:00
|
|
|
log.Info("VMess|Outbound: 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
|
2016-09-20 09:53:05 +00:00
|
|
|
if target.Network == v2net.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 {
|
|
|
|
log.Warning("VMess|Outbound: Failed to get user account: ", err)
|
|
|
|
}
|
|
|
|
account := rawAccount.(*vmess.InternalAccount)
|
|
|
|
request.Security = account.Security
|
|
|
|
|
2015-09-10 22:24:18 +00:00
|
|
|
defer conn.Close()
|
2016-06-02 19:34:25 +00:00
|
|
|
|
2016-06-14 20:54:08 +00:00
|
|
|
conn.SetReusable(true)
|
|
|
|
if conn.Reusable() { // Conn reuse may be disabled on transportation layer
|
2016-06-02 19:34:25 +00:00
|
|
|
request.Option.Set(protocol.RequestOptionConnectionReuse)
|
2016-05-30 22:21:41 +00:00
|
|
|
}
|
2015-09-15 22:06:22 +00:00
|
|
|
|
2015-09-22 12:45:03 +00:00
|
|
|
input := ray.OutboundInput()
|
|
|
|
output := ray.OutboundOutput()
|
2016-01-05 11:08:16 +00:00
|
|
|
|
2015-09-23 12:14:53 +00:00
|
|
|
var requestFinish, responseFinish sync.Mutex
|
|
|
|
requestFinish.Lock()
|
|
|
|
responseFinish.Lock()
|
2015-09-15 22:06:22 +00:00
|
|
|
|
2016-07-23 11:17:51 +00:00
|
|
|
session := encoding.NewClientSession(protocol.DefaultIDHash)
|
2016-02-27 15:41:21 +00:00
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
go v.handleRequest(session, conn, request, payload, input, &requestFinish)
|
|
|
|
go v.handleResponse(session, conn, request, rec.Destination(), output, &responseFinish)
|
2015-09-15 19:17:06 +00:00
|
|
|
|
2015-09-23 12:14:53 +00:00
|
|
|
requestFinish.Lock()
|
|
|
|
responseFinish.Lock()
|
2016-11-27 22:57:19 +00:00
|
|
|
return
|
2015-09-15 19:17:06 +00:00
|
|
|
}
|
|
|
|
|
2016-12-09 12:17:34 +00:00
|
|
|
func (v *VMessOutboundHandler) handleRequest(session *encoding.ClientSession, conn internet.Connection, request *protocol.RequestHeader, payload *buf.Buffer, input buf.Reader, finish *sync.Mutex) {
|
2015-09-23 12:14:53 +00:00
|
|
|
defer finish.Unlock()
|
2015-09-15 22:06:22 +00:00
|
|
|
|
2016-12-09 12:17:34 +00:00
|
|
|
writer := bufio.NewWriter(conn)
|
2016-04-12 19:56:36 +00:00
|
|
|
defer writer.Release()
|
2016-02-27 15:41:21 +00:00
|
|
|
session.EncodeRequestHeader(request, writer)
|
2015-09-17 21:26:09 +00:00
|
|
|
|
2016-12-07 16:32:40 +00:00
|
|
|
bodyWriter := session.EncodeRequestBody(request, writer)
|
|
|
|
defer bodyWriter.Release()
|
|
|
|
|
2016-08-15 10:23:35 +00:00
|
|
|
if !payload.IsEmpty() {
|
2016-12-07 16:32:40 +00:00
|
|
|
if err := bodyWriter.Write(payload); err != nil {
|
2016-11-19 21:39:18 +00:00
|
|
|
log.Info("VMess|Outbound: Failed to write payload. Disabling connection reuse.", err)
|
2016-08-15 10:23:35 +00:00
|
|
|
conn.SetReusable(false)
|
|
|
|
}
|
2016-11-19 21:39:18 +00:00
|
|
|
payload.Release()
|
2016-06-05 22:10:51 +00:00
|
|
|
}
|
2016-05-13 00:20:07 +00:00
|
|
|
writer.SetCached(false)
|
|
|
|
|
2016-12-09 12:17:34 +00:00
|
|
|
if err := buf.PipeUntilEOF(input, bodyWriter); err != nil {
|
2016-06-01 20:09:34 +00:00
|
|
|
conn.SetReusable(false)
|
|
|
|
}
|
|
|
|
|
2016-06-02 19:34:25 +00:00
|
|
|
if request.Option.Has(protocol.RequestOptionChunkStream) {
|
2016-12-09 11:08:25 +00:00
|
|
|
err := bodyWriter.Write(buf.NewLocal(8))
|
2016-06-02 19:34:25 +00:00
|
|
|
if err != nil {
|
|
|
|
conn.SetReusable(false)
|
|
|
|
}
|
2016-04-28 19:14:00 +00:00
|
|
|
}
|
2015-09-18 11:19:12 +00:00
|
|
|
return
|
2015-09-15 19:17:06 +00:00
|
|
|
}
|
2015-09-10 22:24:18 +00:00
|
|
|
|
2016-12-09 12:17:34 +00:00
|
|
|
func (v *VMessOutboundHandler) handleResponse(session *encoding.ClientSession, conn internet.Connection, request *protocol.RequestHeader, dest v2net.Destination, output buf.Writer, finish *sync.Mutex) {
|
2015-09-23 12:14:53 +00:00
|
|
|
defer finish.Unlock()
|
2015-10-06 07:35:02 +00:00
|
|
|
|
2016-12-09 12:17:34 +00:00
|
|
|
reader := bufio.NewReader(conn)
|
2016-04-12 19:56:36 +00:00
|
|
|
defer reader.Release()
|
2016-02-01 11:22:29 +00:00
|
|
|
|
2016-02-27 15:41:21 +00:00
|
|
|
header, err := session.DecodeResponseHeader(reader)
|
2015-09-10 22:24:18 +00:00
|
|
|
if err != nil {
|
2016-06-05 22:10:51 +00:00
|
|
|
conn.SetReusable(false)
|
2016-07-24 09:29:59 +00:00
|
|
|
log.Warning("VMess|Outbound: Failed to read response from ", request.Destination(), ": ", err)
|
2015-09-18 11:19:12 +00:00
|
|
|
return
|
2015-09-10 22:24:18 +00:00
|
|
|
}
|
2016-12-14 20:50:14 +00:00
|
|
|
v.handleCommand(dest, header.Command)
|
2015-09-15 22:06:22 +00:00
|
|
|
|
2016-12-12 12:08:31 +00:00
|
|
|
conn.SetReusable(header.Option.Has(protocol.ResponseOptionConnectionReuse))
|
2016-06-02 19:34:25 +00:00
|
|
|
|
2016-02-27 15:41:21 +00:00
|
|
|
reader.SetCached(false)
|
2016-12-07 16:32:40 +00:00
|
|
|
bodyReader := session.DecodeResponseBody(request, reader)
|
|
|
|
defer bodyReader.Release()
|
2015-10-03 09:34:01 +00:00
|
|
|
|
2016-12-09 12:17:34 +00:00
|
|
|
if err := buf.PipeUntilEOF(bodyReader, output); err != nil {
|
2016-06-01 20:09:34 +00:00
|
|
|
conn.SetReusable(false)
|
|
|
|
}
|
|
|
|
|
2015-09-18 11:19:12 +00:00
|
|
|
return
|
2015-09-10 22:24:18 +00:00
|
|
|
}
|
2016-06-14 20:54:08 +00:00
|
|
|
|
2016-12-21 11:53:31 +00:00
|
|
|
// Factory is a proxy factory for VMess outbound.
|
2016-06-14 20:54:08 +00:00
|
|
|
type Factory struct{}
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
func (v *Factory) StreamCapability() v2net.NetworkList {
|
2016-10-02 21:43:58 +00:00
|
|
|
return v2net.NetworkList{
|
|
|
|
Network: []v2net.Network{v2net.Network_TCP, v2net.Network_KCP, v2net.Network_WebSocket},
|
|
|
|
}
|
2016-06-12 05:52:29 +00:00
|
|
|
}
|
2016-06-12 05:57:08 +00:00
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
func (v *Factory) Create(space app.Space, rawConfig interface{}, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) {
|
2016-06-14 20:54:08 +00:00
|
|
|
vOutConfig := rawConfig.(*Config)
|
2016-06-12 05:57:08 +00:00
|
|
|
|
2016-07-25 14:48:09 +00:00
|
|
|
serverList := protocol.NewServerList()
|
2016-09-24 21:11:58 +00:00
|
|
|
for _, rec := range vOutConfig.Receiver {
|
2016-11-02 15:33:04 +00:00
|
|
|
serverList.AddServer(protocol.NewServerSpecFromPB(*rec))
|
2016-07-25 14:48:09 +00:00
|
|
|
}
|
2016-06-14 20:54:08 +00:00
|
|
|
handler := &VMessOutboundHandler{
|
2016-07-25 14:48:09 +00:00
|
|
|
serverList: serverList,
|
|
|
|
serverPicker: protocol.NewRoundRobinServerPicker(serverList),
|
|
|
|
meta: meta,
|
2016-06-14 20:54:08 +00:00
|
|
|
}
|
2016-06-12 05:57:08 +00:00
|
|
|
|
2016-06-14 20:54:08 +00:00
|
|
|
return handler, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2016-12-15 14:46:20 +00:00
|
|
|
proxy.MustRegisterOutboundHandlerCreator(serial.GetMessageType(new(Config)), new(Factory))
|
2015-09-10 22:24:18 +00:00
|
|
|
}
|