1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 18:17:52 -05:00
v2fly/proxy/vmess/outbound/outbound.go

139 lines
3.8 KiB
Go
Raw Normal View History

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