1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-29 18:45:23 +00:00
v2fly/proxy/vmess/outbound/outbound.go

167 lines
4.6 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 (
"net"
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"
"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-02-25 15:40:43 +00:00
proto "github.com/v2ray/v2ray-core/common/protocol"
2016-02-27 15:41:21 +00:00
raw "github.com/v2ray/v2ray-core/common/protocol/raw"
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"
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
2015-09-10 22:24:18 +00:00
}
2015-11-27 20:57:15 +00:00
func (this *VMessOutboundHandler) Dispatch(firstPacket v2net.Packet, ray ray.OutboundRay) error {
2015-12-05 00:16:21 +00:00
vNextAddress, vNextUser := this.receiverManager.PickReceiver()
2015-09-10 22:24:18 +00:00
2016-02-27 15:41:21 +00:00
command := proto.RequestCommandTCP
2015-10-06 22:30:44 +00:00
if firstPacket.Destination().IsUDP() {
2016-02-27 15:41:21 +00:00
command = proto.RequestCommandUDP
2015-09-20 16:22:29 +00:00
}
2016-02-27 15:41:21 +00:00
request := &proto.RequestHeader{
2016-02-27 21:37:22 +00:00
Version: raw.Version,
2015-10-31 13:08:13 +00:00
User: vNextUser,
2015-09-20 16:22:29 +00:00
Command: command,
2015-10-06 22:30:44 +00:00
Address: firstPacket.Destination().Address(),
2015-12-16 22:53:38 +00:00
Port: firstPacket.Destination().Port(),
2015-09-16 14:27:36 +00:00
}
2016-02-27 15:41:21 +00:00
if command == proto.RequestCommandUDP {
request.Option |= proto.RequestOptionChunkStream
2016-02-01 11:22:29 +00:00
}
return this.startCommunicate(request, vNextAddress, ray, firstPacket)
2015-09-12 18:36:21 +00:00
}
2016-02-27 15:41:21 +00:00
func (this *VMessOutboundHandler) startCommunicate(request *proto.RequestHeader, dest v2net.Destination, ray ray.OutboundRay, firstPacket v2net.Packet) error {
2016-02-05 21:12:46 +00:00
var destIP net.IP
2015-12-16 22:53:38 +00:00
if dest.Address().IsIPv4() || dest.Address().IsIPv6() {
2016-02-05 21:12:46 +00:00
destIP = dest.Address().IP()
2015-12-16 22:53:38 +00:00
} else {
ips, err := net.LookupIP(dest.Address().Domain())
if err != nil {
return err
}
2016-02-05 21:12:46 +00:00
destIP = ips[0]
2015-12-16 22:53:38 +00:00
}
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
2016-02-05 21:12:46 +00:00
IP: destIP,
2015-12-16 22:53:38 +00:00
Port: int(dest.Port()),
})
2015-09-10 22:24:18 +00:00
if err != nil {
2016-01-18 11:24:33 +00:00
log.Error("Failed to open ", dest, ": ", err)
if ray != nil {
close(ray.OutboundOutput())
}
2015-09-10 22:24:18 +00:00
return err
}
2016-01-18 11:24:33 +00:00
log.Info("VMessOut: Tunneling request to ", request.Address, " via ", dest)
2015-09-10 22:24:18 +00:00
defer conn.Close()
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-02-27 15:41:21 +00:00
session := raw.NewClientSession(proto.DefaultIDHash)
go this.handleRequest(session, conn, request, firstPacket, input, &requestFinish)
go this.handleResponse(session, conn, request, dest, output, &responseFinish)
2015-09-23 12:14:53 +00:00
requestFinish.Lock()
2015-12-16 22:53:38 +00:00
conn.CloseWrite()
2015-09-23 12:14:53 +00:00
responseFinish.Lock()
return nil
}
2016-02-27 15:41:21 +00:00
func (this *VMessOutboundHandler) handleRequest(session *raw.ClientSession, conn net.Conn, request *proto.RequestHeader, firstPacket v2net.Packet, input <-chan *alloc.Buffer, 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)
session.EncodeRequestHeader(request, writer)
2015-09-18 10:31:42 +00:00
// Send first packet of payload together with request, in favor of small requests.
2015-10-02 13:32:26 +00:00
firstChunk := firstPacket.Chunk()
moreChunks := firstPacket.MoreChunks()
2016-01-05 11:08:16 +00:00
for firstChunk == nil && moreChunks {
2015-10-02 13:32:26 +00:00
firstChunk, moreChunks = <-input
}
2016-01-05 11:08:16 +00:00
if firstChunk == nil && !moreChunks {
log.Warning("VMessOut: Nothing to send. Existing...")
return
}
2016-02-27 15:41:21 +00:00
if request.Option.IsChunkStream() {
2016-02-01 11:22:29 +00:00
vmessio.Authenticate(firstChunk)
}
2016-02-27 15:41:21 +00:00
bodyWriter := session.EncodeRequestBody(writer)
bodyWriter.Write(firstChunk.Value)
2016-01-05 11:08:16 +00:00
firstChunk.Release()
2016-02-27 15:41:21 +00:00
writer.SetCached(false)
2015-09-18 10:31:42 +00:00
2015-10-02 13:32:26 +00:00
if moreChunks {
2016-04-12 19:43:13 +00:00
var streamWriter v2io.Writer = v2io.NewAdaptiveWriter(bodyWriter)
2016-02-27 15:41:21 +00:00
if request.Option.IsChunkStream() {
2016-02-01 11:22:29 +00:00
streamWriter = vmessio.NewAuthChunkWriter(streamWriter)
}
v2io.ChanToWriter(streamWriter, input)
2016-04-12 19:43:13 +00:00
streamWriter.Release()
}
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
func (this *VMessOutboundHandler) handleResponse(session *raw.ClientSession, conn net.Conn, request *proto.RequestHeader, dest v2net.Destination, output chan<- *alloc.Buffer, finish *sync.Mutex) {
2015-09-23 12:14:53 +00:00
defer finish.Unlock()
defer close(output)
2015-10-06 07:35:02 +00:00
2016-02-27 15:41:21 +00:00
reader := v2io.NewBufferedReader(conn)
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-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-02-27 15:41:21 +00:00
reader.SetCached(false)
decryptReader := session.DecodeResponseBody(conn)
2016-02-27 15:41:21 +00:00
var bodyReader v2io.Reader
if request.Option.IsChunkStream() {
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-02-27 15:41:21 +00:00
v2io.ReaderToChan(output, bodyReader)
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
}
2015-09-12 18:36:21 +00:00
func init() {
2016-01-25 16:29:26 +00: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 22:24:18 +00:00
}