2015-12-04 11:07:32 +00:00
|
|
|
package outbound
|
2015-09-11 00:24:18 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
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-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"
|
2015-09-19 23:54:36 +02:00
|
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
2016-02-25 16:40:43 +01:00
|
|
|
proto "github.com/v2ray/v2ray-core/common/protocol"
|
2016-02-27 16:41:21 +01:00
|
|
|
raw "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"
|
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
|
2015-09-11 00:24:18 +02:00
|
|
|
}
|
|
|
|
|
2015-11-27 21:57:15 +01:00
|
|
|
func (this *VMessOutboundHandler) Dispatch(firstPacket v2net.Packet, ray ray.OutboundRay) error {
|
2015-12-05 01:16:21 +01:00
|
|
|
vNextAddress, vNextUser := this.receiverManager.PickReceiver()
|
2015-09-11 00:24:18 +02:00
|
|
|
|
2016-02-27 16:41:21 +01:00
|
|
|
command := proto.RequestCommandTCP
|
2015-10-07 00:30:44 +02:00
|
|
|
if firstPacket.Destination().IsUDP() {
|
2016-02-27 16:41:21 +01:00
|
|
|
command = proto.RequestCommandUDP
|
2015-09-20 18:22:29 +02:00
|
|
|
}
|
2016-02-27 16:41:21 +01:00
|
|
|
request := &proto.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,
|
2015-10-07 00:30:44 +02:00
|
|
|
Address: firstPacket.Destination().Address(),
|
2015-12-16 23:53:38 +01:00
|
|
|
Port: firstPacket.Destination().Port(),
|
2015-09-16 22:27:36 +08:00
|
|
|
}
|
2016-02-27 16:41:21 +01:00
|
|
|
if command == proto.RequestCommandUDP {
|
|
|
|
request.Option |= proto.RequestOptionChunkStream
|
2016-02-01 12:22:29 +01:00
|
|
|
}
|
2015-10-07 14:50:17 +02:00
|
|
|
|
2016-01-20 17:31:43 +01:00
|
|
|
return this.startCommunicate(request, vNextAddress, ray, firstPacket)
|
2015-09-12 20:36:21 +02:00
|
|
|
}
|
|
|
|
|
2016-02-27 16:41:21 +01:00
|
|
|
func (this *VMessOutboundHandler) startCommunicate(request *proto.RequestHeader, dest v2net.Destination, ray ray.OutboundRay, firstPacket v2net.Packet) error {
|
2016-02-05 22:12:46 +01:00
|
|
|
var destIP net.IP
|
2015-12-16 23:53:38 +01:00
|
|
|
if dest.Address().IsIPv4() || dest.Address().IsIPv6() {
|
2016-02-05 22:12:46 +01:00
|
|
|
destIP = dest.Address().IP()
|
2015-12-16 23:53:38 +01:00
|
|
|
} else {
|
|
|
|
ips, err := net.LookupIP(dest.Address().Domain())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-05 22:12:46 +01:00
|
|
|
destIP = ips[0]
|
2015-12-16 23:53:38 +01:00
|
|
|
}
|
|
|
|
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
|
2016-02-05 22:12:46 +01:00
|
|
|
IP: destIP,
|
2015-12-16 23:53:38 +01:00
|
|
|
Port: int(dest.Port()),
|
|
|
|
})
|
2015-09-11 00:24:18 +02:00
|
|
|
if err != nil {
|
2016-01-18 12:24:33 +01:00
|
|
|
log.Error("Failed to open ", dest, ": ", err)
|
2015-09-22 14:45:03 +02:00
|
|
|
if ray != nil {
|
2016-04-18 18:44:10 +02:00
|
|
|
ray.OutboundOutput().Close()
|
2015-09-22 14:45:03 +02:00
|
|
|
}
|
2015-09-11 00:24:18 +02:00
|
|
|
return err
|
|
|
|
}
|
2016-01-18 12:24:33 +01:00
|
|
|
log.Info("VMessOut: Tunneling request to ", request.Address, " via ", dest)
|
2015-09-17 23:00:17 +02:00
|
|
|
|
2015-09-11 00:24:18 +02:00
|
|
|
defer conn.Close()
|
2015-09-16 00:06:22 +02:00
|
|
|
|
2015-09-22 14:45:03 +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-02-27 16:41:21 +01: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-15 21:17:06 +02:00
|
|
|
|
2015-09-23 14:14:53 +02:00
|
|
|
requestFinish.Lock()
|
2015-12-16 23:53:38 +01:00
|
|
|
conn.CloseWrite()
|
2015-09-23 14:14:53 +02:00
|
|
|
responseFinish.Lock()
|
2016-04-18 18:44:10 +02:00
|
|
|
output.Close()
|
|
|
|
input.Release()
|
2015-09-15 21:17:06 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-18 18:44:10 +02:00
|
|
|
func (this *VMessOutboundHandler) handleRequest(session *raw.ClientSession, conn net.Conn, request *proto.RequestHeader, firstPacket v2net.Packet, 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)
|
2015-09-17 23:26:09 +02:00
|
|
|
|
2015-09-18 12:31:42 +02:00
|
|
|
// Send first packet of payload together with request, in favor of small requests.
|
2015-10-02 15:32:26 +02:00
|
|
|
firstChunk := firstPacket.Chunk()
|
|
|
|
moreChunks := firstPacket.MoreChunks()
|
|
|
|
|
2016-02-27 16:41:21 +01:00
|
|
|
if request.Option.IsChunkStream() {
|
2016-02-01 12:22:29 +01:00
|
|
|
vmessio.Authenticate(firstChunk)
|
|
|
|
}
|
|
|
|
|
2016-02-27 16:41:21 +01:00
|
|
|
bodyWriter := session.EncodeRequestBody(writer)
|
|
|
|
bodyWriter.Write(firstChunk.Value)
|
2016-01-05 12:08:16 +01:00
|
|
|
firstChunk.Release()
|
|
|
|
|
2016-02-27 16:41:21 +01:00
|
|
|
writer.SetCached(false)
|
2015-09-18 12:31:42 +02:00
|
|
|
|
2015-10-02 15:32:26 +02:00
|
|
|
if moreChunks {
|
2016-04-12 21:43:13 +02:00
|
|
|
var streamWriter v2io.Writer = v2io.NewAdaptiveWriter(bodyWriter)
|
2016-02-27 16:41:21 +01:00
|
|
|
if request.Option.IsChunkStream() {
|
2016-02-01 12:22:29 +01:00
|
|
|
streamWriter = vmessio.NewAuthChunkWriter(streamWriter)
|
|
|
|
}
|
2016-04-18 18:44:10 +02:00
|
|
|
v2io.Pipe(input, streamWriter)
|
2016-04-12 21:43:13 +02:00
|
|
|
streamWriter.Release()
|
2015-09-15 21:17:06 +02:00
|
|
|
}
|
2015-09-18 13:19:12 +02:00
|
|
|
return
|
2015-09-15 21:17:06 +02:00
|
|
|
}
|
2015-09-11 00:24:18 +02:00
|
|
|
|
2016-04-18 18:44:10 +02: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 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-02-27 16:41:21 +01:00
|
|
|
reader.SetCached(false)
|
|
|
|
decryptReader := session.DecodeResponseBody(conn)
|
2015-12-04 11:42:56 +00:00
|
|
|
|
2016-02-27 16:41:21 +01:00
|
|
|
var bodyReader v2io.Reader
|
|
|
|
if request.Option.IsChunkStream() {
|
|
|
|
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-04-18 18:44:10 +02:00
|
|
|
v2io.Pipe(bodyReader, output)
|
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-01-25 17:18:24 +01:00
|
|
|
func(space app.Space, rawConfig interface{}) (proxy.OutboundHandler, error) {
|
2016-01-15 12:43:06 +01:00
|
|
|
vOutConfig := rawConfig.(*Config)
|
2016-01-06 16:23:54 +01:00
|
|
|
return &VMessOutboundHandler{
|
2016-01-15 12:43:06 +01:00
|
|
|
receiverManager: NewReceiverManager(vOutConfig.Receivers),
|
2016-01-06 16:23:54 +01:00
|
|
|
}, nil
|
|
|
|
})
|
2015-09-11 00:24:18 +02:00
|
|
|
}
|