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