v2fly/proxy/vmess/outbound/outbound.go

202 lines
5.9 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 (
"crypto/md5"
"crypto/rand"
"io"
2015-09-10 22:24:18 +00:00
"net"
2015-09-23 12:14:53 +00:00
"sync"
2016-01-12 10:38:43 +00:00
"time"
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"
2015-11-03 20:26:16 +00:00
v2crypto "github.com/v2ray/v2ray-core/common/crypto"
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-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"
2015-09-19 22:11:14 +00:00
"github.com/v2ray/v2ray-core/proxy/vmess/protocol"
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
2015-09-20 16:22:29 +00:00
command := protocol.CmdTCP
2015-10-06 22:30:44 +00:00
if firstPacket.Destination().IsUDP() {
2015-09-20 16:22:29 +00:00
command = protocol.CmdUDP
}
request := &protocol.VMessRequest{
Version: protocol.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
}
2015-10-21 20:38:53 +00:00
buffer := alloc.NewSmallBuffer()
defer buffer.Release() // Buffer is released after communication finishes.
io.ReadFull(rand.Reader, buffer.Value[:33]) // 16 + 16 + 1
2015-10-21 20:38:53 +00:00
request.RequestIV = buffer.Value[:16]
request.RequestKey = buffer.Value[16:32]
request.ResponseHeader = buffer.Value[32]
2015-09-10 22:24:18 +00:00
return this.startCommunicate(request, vNextAddress, ray, firstPacket)
2015-09-12 18:36:21 +00:00
}
func (this *VMessOutboundHandler) startCommunicate(request *protocol.VMessRequest, dest v2net.Destination, ray ray.OutboundRay, firstPacket v2net.Packet) error {
2015-12-16 22:53:38 +00: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 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
go this.handleRequest(conn, request, firstPacket, input, &requestFinish)
2016-01-21 16:22:56 +00:00
go this.handleResponse(conn, request, dest, output, &responseFinish, (request.Command == protocol.CmdUDP))
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
}
func (this *VMessOutboundHandler) handleRequest(conn net.Conn, request *protocol.VMessRequest, firstPacket v2net.Packet, input <-chan *alloc.Buffer, finish *sync.Mutex) {
2015-09-23 12:14:53 +00:00
defer finish.Unlock()
2015-11-03 20:26:16 +00:00
aesStream, err := v2crypto.NewAesEncryptionStream(request.RequestKey[:], request.RequestIV[:])
2015-09-12 18:36:21 +00:00
if err != nil {
2016-01-18 11:24:33 +00:00
log.Error("VMessOut: Failed to create AES encryption stream: ", err)
2015-09-18 11:19:12 +00:00
return
2015-09-12 18:36:21 +00:00
}
2015-11-03 20:26:16 +00:00
encryptRequestWriter := v2crypto.NewCryptionWriter(aesStream, conn)
2015-09-15 22:06:22 +00:00
2015-10-10 14:54:15 +00:00
buffer := alloc.NewBuffer().Clear()
2016-01-04 22:00:14 +00:00
defer buffer.Release()
2016-01-12 10:52:40 +00:00
buffer, err = request.ToBytes(protocol.NewRandomTimestampGenerator(protocol.Timestamp(time.Now().Unix()), 30), buffer)
if err != nil {
2016-01-18 11:24:33 +00:00
log.Error("VMessOut: Failed to serialize VMess request: ", err)
2015-09-18 11:19:12 +00:00
return
}
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-01-05 11:08:16 +00:00
aesStream.XORKeyStream(firstChunk.Value, firstChunk.Value)
buffer.Append(firstChunk.Value)
firstChunk.Release()
_, err = conn.Write(buffer.Value)
if err != nil {
2016-01-18 11:24:33 +00:00
log.Error("VMessOut: Failed to write VMess request: ", err)
2016-01-05 11:08:16 +00:00
return
2015-10-02 13:32:26 +00:00
}
2015-09-18 10:31:42 +00:00
2015-10-02 13:32:26 +00:00
if moreChunks {
2016-01-29 13:39:55 +00:00
v2io.ChanToWriter(encryptRequestWriter, input)
}
2015-09-18 11:19:12 +00:00
return
}
2015-09-10 22:24:18 +00:00
func headerMatch(request *protocol.VMessRequest, responseHeader byte) bool {
return request.ResponseHeader == responseHeader
2015-11-28 09:11:56 +00:00
}
2016-01-21 16:22:56 +00:00
func (this *VMessOutboundHandler) handleResponse(conn net.Conn, request *protocol.VMessRequest, dest v2net.Destination, output chan<- *alloc.Buffer, finish *sync.Mutex, isUDP bool) {
2015-09-23 12:14:53 +00:00
defer finish.Unlock()
defer close(output)
2015-09-15 22:06:22 +00:00
responseKey := md5.Sum(request.RequestKey[:])
responseIV := md5.Sum(request.RequestIV[:])
2015-09-10 22:24:18 +00:00
2015-11-03 20:26:16 +00:00
aesStream, err := v2crypto.NewAesDecryptionStream(responseKey[:], responseIV[:])
2015-09-12 18:36:21 +00:00
if err != nil {
2016-01-18 11:24:33 +00:00
log.Error("VMessOut: Failed to create AES encryption stream: ", err)
2015-09-18 11:19:12 +00:00
return
2015-09-12 18:36:21 +00:00
}
2015-11-03 20:26:16 +00:00
decryptResponseReader := v2crypto.NewCryptionReader(aesStream, conn)
2015-10-06 07:35:02 +00:00
2016-01-29 13:39:55 +00:00
buffer, err := v2io.ReadFrom(decryptResponseReader, nil)
2015-09-10 22:24:18 +00:00
if err != nil {
2016-01-18 11:24:33 +00:00
log.Error("VMessOut: Failed to read VMess response (", buffer.Len(), " bytes): ", err)
2016-01-04 21:02:22 +00:00
buffer.Release()
2015-09-18 11:19:12 +00:00
return
}
if buffer.Len() < 4 || !headerMatch(request, buffer.Value[0]) {
2015-09-18 11:19:12 +00:00
log.Warning("VMessOut: unexepcted response header. The connection is probably hijacked.")
return
2015-09-10 22:24:18 +00:00
}
2016-01-18 11:24:33 +00:00
log.Info("VMessOut received ", buffer.Len()-4, " bytes from ", conn.RemoteAddr())
2015-09-15 22:06:22 +00:00
responseBegin := 4
if buffer.Value[2] != 0 {
dataLen := int(buffer.Value[3])
if buffer.Len() < dataLen+4 { // Rare case
diffBuffer := make([]byte, dataLen+4-buffer.Len())
io.ReadFull(decryptResponseReader, diffBuffer)
buffer.Append(diffBuffer)
}
command := buffer.Value[2]
data := buffer.Value[4 : 4+dataLen]
2016-01-21 16:22:56 +00:00
go this.handleCommand(dest, command, data)
responseBegin = 4 + dataLen
}
buffer.SliceFrom(responseBegin)
output <- buffer
2015-10-03 09:34:01 +00:00
if !isUDP {
2016-01-29 13:39:55 +00:00
v2io.RawReaderToChan(output, decryptResponseReader)
2015-10-03 09:34:01 +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
}