v2fly/proxy/vmess/outbound/outbound.go

175 lines
4.7 KiB
Go
Raw Normal View History

2015-12-04 11:07:32 +00:00
package outbound
2015-09-10 22:24:18 +00:00
import (
2016-06-02 00:20:53 +00:00
"io"
2015-09-23 12:14:53 +00:00
"sync"
2015-09-10 22:24:18 +00:00
2015-12-06 10:00:10 +00:00
"github.com/v2ray/v2ray-core/app"
2016-04-25 22:13:26 +00:00
"github.com/v2ray/v2ray-core/common/alloc"
2016-01-29 13:39:55 +00:00
v2io "github.com/v2ray/v2ray-core/common/io"
2015-09-19 22:50:21 +00:00
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
2016-05-07 18:26:29 +00:00
"github.com/v2ray/v2ray-core/common/protocol"
"github.com/v2ray/v2ray-core/common/protocol/raw"
2016-06-05 22:10:51 +00:00
"github.com/v2ray/v2ray-core/common/retry"
2016-01-02 22:32:18 +00:00
"github.com/v2ray/v2ray-core/proxy"
2016-01-02 22:08:36 +00:00
"github.com/v2ray/v2ray-core/proxy/internal"
2016-02-01 11:22:29 +00:00
vmessio "github.com/v2ray/v2ray-core/proxy/vmess/io"
2016-06-02 19:34:25 +00:00
"github.com/v2ray/v2ray-core/transport"
2016-05-30 22:21:41 +00:00
"github.com/v2ray/v2ray-core/transport/hub"
2015-10-14 12:51:19 +00:00
"github.com/v2ray/v2ray-core/transport/ray"
2015-09-10 22:24:18 +00:00
)
type VMessOutboundHandler struct {
2015-12-05 00:16:21 +00:00
receiverManager *ReceiverManager
2016-06-04 12:25:13 +00:00
meta *proxy.OutboundHandlerMeta
2015-09-10 22:24:18 +00:00
}
2016-04-25 22:13:26 +00:00
func (this *VMessOutboundHandler) Dispatch(target v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) error {
2016-05-06 22:19:06 +00:00
defer ray.OutboundInput().Release()
defer ray.OutboundOutput().Close()
2016-06-05 22:10:51 +00:00
var rec *Receiver
var conn *hub.Connection
err := retry.Timed(5, 100).On(func() error {
rec = this.receiverManager.PickReceiver()
rawConn, err := hub.Dial3(this.meta.Address, rec.Destination, this.meta)
2016-06-05 22:10:51 +00:00
if err != nil {
return err
}
conn = rawConn
return nil
})
if err != nil {
log.Error("Failed to find an available destination:", err)
return err
}
log.Info("VMessOut: 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-04-25 22:13:26 +00:00
if target.IsUDP() {
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-02-27 21:37:22 +00:00
Version: raw.Version,
2016-06-05 22:10:51 +00:00
User: rec.PickUser(),
2015-09-20 16:22:29 +00:00
Command: command,
2016-04-25 22:13:26 +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-09-10 22:24:18 +00:00
defer conn.Close()
2016-06-02 19:34:25 +00:00
if transport.IsConnectionReusable() {
request.Option.Set(protocol.RequestOptionConnectionReuse)
2016-05-30 22:21:41 +00:00
conn.SetReusable(true)
}
2015-09-15 22:06:22 +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-05-07 18:26:29 +00:00
session := raw.NewClientSession(protocol.DefaultIDHash)
2016-02-27 15:41:21 +00:00
2016-04-25 22:13:26 +00:00
go this.handleRequest(session, conn, request, payload, input, &requestFinish)
2016-06-05 22:10:51 +00:00
go this.handleResponse(session, conn, request, rec.Destination, output, &responseFinish)
2015-09-23 12:14:53 +00:00
requestFinish.Lock()
responseFinish.Lock()
return nil
}
2016-06-01 20:09:34 +00:00
func (this *VMessOutboundHandler) handleRequest(session *raw.ClientSession, conn *hub.Connection, request *protocol.RequestHeader, payload *alloc.Buffer, input v2io.Reader, finish *sync.Mutex) {
2015-09-23 12:14:53 +00:00
defer finish.Unlock()
2015-09-15 22:06:22 +00:00
2016-02-27 15:41:21 +00:00
writer := v2io.NewBufferedWriter(conn)
2016-04-12 19:56:36 +00:00
defer writer.Release()
2016-02-27 15:41:21 +00:00
session.EncodeRequestHeader(request, writer)
2016-02-27 15:41:21 +00:00
bodyWriter := session.EncodeRequestBody(writer)
2016-04-25 22:13:26 +00:00
var streamWriter v2io.Writer = v2io.NewAdaptiveWriter(bodyWriter)
2016-06-02 19:34:25 +00:00
if request.Option.Has(protocol.RequestOptionChunkStream) {
2016-04-25 22:13:26 +00:00
streamWriter = vmessio.NewAuthChunkWriter(streamWriter)
}
2016-06-05 22:10:51 +00:00
if err := streamWriter.Write(payload); err != nil {
conn.SetReusable(false)
}
2016-05-13 00:20:07 +00:00
writer.SetCached(false)
2016-06-01 20:09:34 +00:00
err := v2io.Pipe(input, streamWriter)
2016-06-02 00:20:53 +00:00
if err != io.EOF {
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) {
err := streamWriter.Write(alloc.NewSmallBuffer().Clear())
if err != nil {
conn.SetReusable(false)
}
2016-04-28 19:14:00 +00:00
}
2016-04-25 22:13:26 +00:00
streamWriter.Release()
2015-09-18 11:19:12 +00:00
return
}
2015-09-10 22:24:18 +00:00
2016-06-01 20:09:34 +00:00
func (this *VMessOutboundHandler) handleResponse(session *raw.ClientSession, conn *hub.Connection, request *protocol.RequestHeader, dest v2net.Destination, output v2io.Writer, finish *sync.Mutex) {
2015-09-23 12:14:53 +00:00
defer finish.Unlock()
2015-10-06 07:35:02 +00:00
2016-02-27 15:41:21 +00:00
reader := v2io.NewBufferedReader(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-02-27 15:41:21 +00:00
log.Warning("VMessOut: Failed to read response: ", err)
2015-09-18 11:19:12 +00:00
return
2015-09-10 22:24:18 +00:00
}
2016-02-27 15:41:21 +00:00
go this.handleCommand(dest, header.Command)
2015-09-15 22:06:22 +00:00
2016-06-02 19:34:25 +00:00
if !header.Option.Has(protocol.ResponseOptionConnectionReuse) {
conn.SetReusable(false)
}
2016-02-27 15:41:21 +00:00
reader.SetCached(false)
2016-05-13 00:20:07 +00:00
decryptReader := session.DecodeResponseBody(reader)
2016-02-27 15:41:21 +00:00
var bodyReader v2io.Reader
2016-06-02 19:34:25 +00:00
if request.Option.Has(protocol.RequestOptionChunkStream) {
2016-02-27 15:41:21 +00:00
bodyReader = vmessio.NewAuthChunkReader(decryptReader)
2016-02-01 11:22:29 +00:00
} else {
2016-02-27 15:41:21 +00:00
bodyReader = v2io.NewAdaptiveReader(decryptReader)
2015-10-03 09:34:01 +00:00
}
2016-06-01 20:09:34 +00:00
err = v2io.Pipe(bodyReader, output)
2016-06-02 00:20:53 +00:00
if err != io.EOF {
2016-06-01 20:09:34 +00:00
conn.SetReusable(false)
}
2016-04-12 19:43:13 +00:00
bodyReader.Release()
2016-02-01 11:22:29 +00:00
2015-09-18 11:19:12 +00:00
return
2015-09-10 22:24:18 +00:00
}
func (this *VMessOutboundHandler) setProxyCap() {
this.meta.KcpSupported = true
}
2015-09-12 18:36:21 +00:00
func init() {
2016-01-25 16:29:26 +00:00
internal.MustRegisterOutboundHandlerCreator("vmess",
2016-06-04 12:25:13 +00:00
func(space app.Space, rawConfig interface{}, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) {
vOutConfig := rawConfig.(*Config)
handler := &VMessOutboundHandler{
receiverManager: NewReceiverManager(vOutConfig.Receivers),
2016-06-04 12:25:13 +00:00
meta: meta,
}
handler.setProxyCap()
return handler, nil
})
2015-09-10 22:24:18 +00:00
}