2015-12-04 06:07:32 -05:00
|
|
|
package outbound
|
2015-09-10 18:24:18 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
|
|
|
"crypto/rand"
|
2016-01-28 17:58:23 -05:00
|
|
|
"io"
|
2015-09-10 18:24:18 -04:00
|
|
|
"net"
|
2015-09-23 08:14:53 -04:00
|
|
|
"sync"
|
2016-01-12 05:38:43 -05:00
|
|
|
"time"
|
2015-09-10 18:24:18 -04:00
|
|
|
|
2015-12-06 05:00:10 -05:00
|
|
|
"github.com/v2ray/v2ray-core/app"
|
2015-10-08 08:46:18 -04:00
|
|
|
"github.com/v2ray/v2ray-core/common/alloc"
|
2015-11-03 15:26:16 -05:00
|
|
|
v2crypto "github.com/v2ray/v2ray-core/common/crypto"
|
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"
|
2015-09-19 17:54:36 -04:00
|
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
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"
|
2015-09-19 18:11:14 -04:00
|
|
|
"github.com/v2ray/v2ray-core/proxy/vmess/protocol"
|
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
|
|
|
}
|
|
|
|
|
2015-11-27 15:57:15 -05:00
|
|
|
func (this *VMessOutboundHandler) Dispatch(firstPacket v2net.Packet, ray ray.OutboundRay) error {
|
2015-12-04 19:16:21 -05:00
|
|
|
vNextAddress, vNextUser := this.receiverManager.PickReceiver()
|
2015-09-10 18:24:18 -04:00
|
|
|
|
2015-09-20 12:22:29 -04:00
|
|
|
command := protocol.CmdTCP
|
2015-10-06 18:30:44 -04:00
|
|
|
if firstPacket.Destination().IsUDP() {
|
2015-09-20 12:22:29 -04:00
|
|
|
command = protocol.CmdUDP
|
|
|
|
}
|
2015-09-19 17:54:36 -04:00
|
|
|
request := &protocol.VMessRequest{
|
|
|
|
Version: protocol.Version,
|
2015-10-31 09:08:13 -04:00
|
|
|
User: vNextUser,
|
2015-09-20 12:22:29 -04:00
|
|
|
Command: command,
|
2015-10-06 18:30:44 -04:00
|
|
|
Address: firstPacket.Destination().Address(),
|
2015-12-16 17:53:38 -05:00
|
|
|
Port: firstPacket.Destination().Port(),
|
2015-09-16 10:27:36 -04:00
|
|
|
}
|
2016-02-01 06:22:29 -05:00
|
|
|
if command == protocol.CmdUDP {
|
|
|
|
request.Option |= protocol.OptionChunk
|
|
|
|
}
|
2015-10-07 08:50:17 -04:00
|
|
|
|
2015-10-21 16:38:53 -04:00
|
|
|
buffer := alloc.NewSmallBuffer()
|
2016-01-28 17:58:23 -05:00
|
|
|
defer buffer.Release() // Buffer is released after communication finishes.
|
|
|
|
io.ReadFull(rand.Reader, buffer.Value[:33]) // 16 + 16 + 1
|
2015-10-21 16:38:53 -04:00
|
|
|
request.RequestIV = buffer.Value[:16]
|
|
|
|
request.RequestKey = buffer.Value[16:32]
|
2016-01-21 04:08:00 -05:00
|
|
|
request.ResponseHeader = buffer.Value[32]
|
2015-09-10 18:24:18 -04:00
|
|
|
|
2016-01-20 11:31:43 -05:00
|
|
|
return this.startCommunicate(request, vNextAddress, ray, firstPacket)
|
2015-09-12 14:36:21 -04:00
|
|
|
}
|
|
|
|
|
2016-01-20 11:31:43 -05:00
|
|
|
func (this *VMessOutboundHandler) startCommunicate(request *protocol.VMessRequest, dest v2net.Destination, ray ray.OutboundRay, firstPacket v2net.Packet) error {
|
2015-12-16 17:53:38 -05:00
|
|
|
var destIp net.IP
|
|
|
|
if dest.Address().IsIPv4() || dest.Address().IsIPv6() {
|
|
|
|
destIp = dest.Address().IP()
|
|
|
|
} else {
|
|
|
|
ips, err := net.LookupIP(dest.Address().Domain())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
destIp = ips[0]
|
|
|
|
}
|
|
|
|
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
|
|
|
|
IP: destIp,
|
|
|
|
Port: int(dest.Port()),
|
|
|
|
})
|
2015-09-10 18:24:18 -04:00
|
|
|
if err != nil {
|
2016-01-18 06:24:33 -05:00
|
|
|
log.Error("Failed to open ", dest, ": ", err)
|
2015-09-22 08:45:03 -04:00
|
|
|
if ray != nil {
|
|
|
|
close(ray.OutboundOutput())
|
|
|
|
}
|
2015-09-10 18:24:18 -04:00
|
|
|
return err
|
|
|
|
}
|
2016-01-18 06:24:33 -05:00
|
|
|
log.Info("VMessOut: Tunneling request to ", request.Address, " via ", dest)
|
2015-09-17 17:00:17 -04:00
|
|
|
|
2015-09-10 18:24:18 -04:00
|
|
|
defer conn.Close()
|
2015-09-15 18:06:22 -04:00
|
|
|
|
2015-09-22 08:45:03 -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-01-20 11:31:43 -05:00
|
|
|
go this.handleRequest(conn, request, firstPacket, input, &requestFinish)
|
2016-02-01 06:22:29 -05:00
|
|
|
go this.handleResponse(conn, request, dest, output, &responseFinish)
|
2015-09-15 15:17:06 -04:00
|
|
|
|
2015-09-23 08:14:53 -04:00
|
|
|
requestFinish.Lock()
|
2015-12-16 17:53:38 -05:00
|
|
|
conn.CloseWrite()
|
2015-09-23 08:14:53 -04:00
|
|
|
responseFinish.Lock()
|
2015-09-15 15:17:06 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-20 11:31:43 -05:00
|
|
|
func (this *VMessOutboundHandler) handleRequest(conn net.Conn, request *protocol.VMessRequest, firstPacket v2net.Packet, input <-chan *alloc.Buffer, finish *sync.Mutex) {
|
2015-09-23 08:14:53 -04:00
|
|
|
defer finish.Unlock()
|
2015-11-03 15:26:16 -05:00
|
|
|
aesStream, err := v2crypto.NewAesEncryptionStream(request.RequestKey[:], request.RequestIV[:])
|
2015-09-12 14:36:21 -04:00
|
|
|
if err != nil {
|
2016-01-18 06:24:33 -05:00
|
|
|
log.Error("VMessOut: Failed to create AES encryption stream: ", err)
|
2015-09-18 07:19:12 -04:00
|
|
|
return
|
2015-09-12 14:36:21 -04:00
|
|
|
}
|
2015-11-03 15:26:16 -05:00
|
|
|
encryptRequestWriter := v2crypto.NewCryptionWriter(aesStream, conn)
|
2015-09-15 18:06:22 -04:00
|
|
|
|
2015-10-10 10:54:15 -04:00
|
|
|
buffer := alloc.NewBuffer().Clear()
|
2016-01-04 17:00:14 -05:00
|
|
|
defer buffer.Release()
|
2016-01-12 05:52:40 -05:00
|
|
|
buffer, err = request.ToBytes(protocol.NewRandomTimestampGenerator(protocol.Timestamp(time.Now().Unix()), 30), buffer)
|
2015-09-15 15:17:06 -04:00
|
|
|
if err != nil {
|
2016-01-18 06:24:33 -05:00
|
|
|
log.Error("VMessOut: Failed to serialize VMess request: ", err)
|
2015-09-18 07:19:12 -04:00
|
|
|
return
|
2015-09-17 17:26:09 -04:00
|
|
|
}
|
|
|
|
|
2015-09-18 06:31:42 -04:00
|
|
|
// Send first packet of payload together with request, in favor of small requests.
|
2015-10-02 09:32:26 -04:00
|
|
|
firstChunk := firstPacket.Chunk()
|
|
|
|
moreChunks := firstPacket.MoreChunks()
|
|
|
|
|
2016-01-05 06:08:16 -05:00
|
|
|
for firstChunk == nil && moreChunks {
|
2015-10-02 09:32:26 -04:00
|
|
|
firstChunk, moreChunks = <-input
|
|
|
|
}
|
|
|
|
|
2016-01-05 06:08:16 -05:00
|
|
|
if firstChunk == nil && !moreChunks {
|
|
|
|
log.Warning("VMessOut: Nothing to send. Existing...")
|
|
|
|
return
|
|
|
|
}
|
2015-10-08 08:46:18 -04:00
|
|
|
|
2016-02-01 06:22:29 -05:00
|
|
|
if request.IsChunkStream() {
|
|
|
|
vmessio.Authenticate(firstChunk)
|
|
|
|
}
|
|
|
|
|
2016-01-05 06:08:16 -05:00
|
|
|
aesStream.XORKeyStream(firstChunk.Value, firstChunk.Value)
|
|
|
|
buffer.Append(firstChunk.Value)
|
|
|
|
firstChunk.Release()
|
|
|
|
|
|
|
|
_, err = conn.Write(buffer.Value)
|
|
|
|
if err != nil {
|
2016-01-18 06:24:33 -05:00
|
|
|
log.Error("VMessOut: Failed to write VMess request: ", err)
|
2016-01-05 06:08:16 -05:00
|
|
|
return
|
2015-10-02 09:32:26 -04:00
|
|
|
}
|
2015-09-18 06:31:42 -04:00
|
|
|
|
2015-10-02 09:32:26 -04:00
|
|
|
if moreChunks {
|
2016-02-03 15:36:52 -05:00
|
|
|
var streamWriter v2io.Writer = v2io.NewAdaptiveWriter(encryptRequestWriter)
|
2016-02-01 06:22:29 -05:00
|
|
|
if request.IsChunkStream() {
|
|
|
|
streamWriter = vmessio.NewAuthChunkWriter(streamWriter)
|
|
|
|
}
|
|
|
|
v2io.ChanToWriter(streamWriter, input)
|
2015-09-15 15:17:06 -04:00
|
|
|
}
|
2015-09-18 07:19:12 -04:00
|
|
|
return
|
2015-09-15 15:17:06 -04:00
|
|
|
}
|
2015-09-10 18:24:18 -04:00
|
|
|
|
2016-01-21 04:08:00 -05:00
|
|
|
func headerMatch(request *protocol.VMessRequest, responseHeader byte) bool {
|
|
|
|
return request.ResponseHeader == responseHeader
|
2015-11-28 04:11:56 -05:00
|
|
|
}
|
|
|
|
|
2016-02-01 06:22:29 -05:00
|
|
|
func (this *VMessOutboundHandler) handleResponse(conn net.Conn, request *protocol.VMessRequest, dest v2net.Destination, output chan<- *alloc.Buffer, finish *sync.Mutex) {
|
2015-09-23 08:14:53 -04:00
|
|
|
defer finish.Unlock()
|
2015-09-17 17:00:17 -04:00
|
|
|
defer close(output)
|
2015-09-15 18:06:22 -04:00
|
|
|
responseKey := md5.Sum(request.RequestKey[:])
|
2015-09-15 15:17:06 -04:00
|
|
|
responseIV := md5.Sum(request.RequestIV[:])
|
2015-09-10 18:24:18 -04:00
|
|
|
|
2015-11-03 15:26:16 -05:00
|
|
|
aesStream, err := v2crypto.NewAesDecryptionStream(responseKey[:], responseIV[:])
|
2015-09-12 14:36:21 -04:00
|
|
|
if err != nil {
|
2016-01-18 06:24:33 -05:00
|
|
|
log.Error("VMessOut: Failed to create AES encryption stream: ", err)
|
2015-09-18 07:19:12 -04:00
|
|
|
return
|
2015-09-12 14:36:21 -04:00
|
|
|
}
|
2015-11-03 15:26:16 -05:00
|
|
|
decryptResponseReader := v2crypto.NewCryptionReader(aesStream, conn)
|
2015-10-06 03:35:02 -04:00
|
|
|
|
2016-02-01 06:22:29 -05:00
|
|
|
buffer := alloc.NewSmallBuffer()
|
|
|
|
defer buffer.Release()
|
|
|
|
_, err = io.ReadFull(decryptResponseReader, buffer.Value[:4])
|
|
|
|
|
2015-09-10 18:24:18 -04:00
|
|
|
if err != nil {
|
2016-01-18 06:24:33 -05:00
|
|
|
log.Error("VMessOut: Failed to read VMess response (", buffer.Len(), " bytes): ", err)
|
2015-09-18 07:19:12 -04:00
|
|
|
return
|
|
|
|
}
|
2016-02-01 06:22:29 -05:00
|
|
|
if !headerMatch(request, buffer.Value[0]) {
|
2015-09-18 07:19:12 -04:00
|
|
|
log.Warning("VMessOut: unexepcted response header. The connection is probably hijacked.")
|
|
|
|
return
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|
2015-09-15 18:06:22 -04:00
|
|
|
|
2015-12-04 06:42:56 -05:00
|
|
|
if buffer.Value[2] != 0 {
|
2016-02-01 06:22:29 -05:00
|
|
|
command := buffer.Value[2]
|
2015-12-04 06:42:56 -05:00
|
|
|
dataLen := int(buffer.Value[3])
|
2016-02-01 06:22:29 -05:00
|
|
|
_, err := io.ReadFull(decryptResponseReader, buffer.Value[:dataLen])
|
|
|
|
if err != nil {
|
|
|
|
log.Error("VMessOut: Failed to read response command: ", err)
|
|
|
|
return
|
2015-12-04 06:42:56 -05:00
|
|
|
}
|
2016-02-01 06:22:29 -05:00
|
|
|
data := buffer.Value[:dataLen]
|
2016-01-21 11:22:56 -05:00
|
|
|
go this.handleCommand(dest, command, data)
|
2015-12-04 06:42:56 -05:00
|
|
|
}
|
|
|
|
|
2016-02-01 06:22:29 -05:00
|
|
|
var reader v2io.Reader
|
|
|
|
if request.IsChunkStream() {
|
|
|
|
reader = vmessio.NewAuthChunkReader(decryptResponseReader)
|
|
|
|
} else {
|
|
|
|
reader = v2io.NewAdaptiveReader(decryptResponseReader)
|
2015-10-03 05:34:01 -04:00
|
|
|
}
|
|
|
|
|
2016-02-01 06:22:29 -05:00
|
|
|
v2io.ReaderToChan(output, reader)
|
|
|
|
|
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",
|
2016-01-25 11:18:24 -05:00
|
|
|
func(space app.Space, rawConfig interface{}) (proxy.OutboundHandler, error) {
|
2016-01-15 06:43:06 -05:00
|
|
|
vOutConfig := rawConfig.(*Config)
|
2016-01-06 10:23:54 -05:00
|
|
|
return &VMessOutboundHandler{
|
2016-01-15 06:43:06 -05:00
|
|
|
receiverManager: NewReceiverManager(vOutConfig.Receivers),
|
2016-01-06 10:23:54 -05:00
|
|
|
}, nil
|
|
|
|
})
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|