2015-09-10 18:24:18 -04:00
|
|
|
package vmess
|
|
|
|
|
|
|
|
import (
|
2015-09-18 07:19:12 -04:00
|
|
|
"bytes"
|
2015-09-10 18:24:18 -04:00
|
|
|
"crypto/md5"
|
|
|
|
"crypto/rand"
|
|
|
|
mrand "math/rand"
|
|
|
|
"net"
|
2015-09-23 08:14:53 -04:00
|
|
|
"sync"
|
2015-09-10 18:24:18 -04:00
|
|
|
|
2015-10-08 08:46:18 -04:00
|
|
|
"github.com/v2ray/v2ray-core/common/alloc"
|
2015-09-19 17:54:36 -04: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"
|
2015-10-29 19:11:29 -04:00
|
|
|
"github.com/v2ray/v2ray-core/proxy/common/connhandler"
|
2015-10-16 06:03:22 -04:00
|
|
|
"github.com/v2ray/v2ray-core/proxy/vmess/config"
|
2015-09-19 18:11:14 -04:00
|
|
|
"github.com/v2ray/v2ray-core/proxy/vmess/protocol"
|
2015-09-19 18:50:21 -04:00
|
|
|
"github.com/v2ray/v2ray-core/proxy/vmess/protocol/user"
|
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-10-16 06:03:22 -04:00
|
|
|
vNextList []*config.OutboundTarget
|
|
|
|
vNextListUDP []*config.OutboundTarget
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|
|
|
|
|
2015-10-16 06:03:22 -04:00
|
|
|
func NewVMessOutboundHandler(vNextList, vNextListUDP []*config.OutboundTarget) *VMessOutboundHandler {
|
2015-09-16 10:27:36 -04:00
|
|
|
return &VMessOutboundHandler{
|
2015-10-03 05:34:01 -04:00
|
|
|
vNextList: vNextList,
|
|
|
|
vNextListUDP: vNextListUDP,
|
2015-09-16 10:27:36 -04:00
|
|
|
}
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|
|
|
|
|
2015-10-16 06:03:22 -04:00
|
|
|
func pickVNext(serverList []*config.OutboundTarget) (v2net.Destination, config.User) {
|
2015-10-02 15:55:37 -04:00
|
|
|
vNextLen := len(serverList)
|
2015-09-10 18:24:18 -04:00
|
|
|
if vNextLen == 0 {
|
2015-09-18 07:19:12 -04:00
|
|
|
panic("VMessOut: Zero vNext is configured.")
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|
2015-09-21 18:28:52 -04:00
|
|
|
vNextIndex := 0
|
|
|
|
if vNextLen > 1 {
|
|
|
|
vNextIndex = mrand.Intn(vNextLen)
|
|
|
|
}
|
|
|
|
|
2015-10-02 15:55:37 -04:00
|
|
|
vNext := serverList[vNextIndex]
|
2015-10-16 06:03:22 -04:00
|
|
|
vNextUserLen := len(vNext.Accounts)
|
2015-09-10 18:24:18 -04:00
|
|
|
if vNextUserLen == 0 {
|
2015-09-18 07:19:12 -04:00
|
|
|
panic("VMessOut: Zero User account.")
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|
2015-09-21 18:28:52 -04:00
|
|
|
vNextUserIndex := 0
|
|
|
|
if vNextUserLen > 1 {
|
|
|
|
vNextUserIndex = mrand.Intn(vNextUserLen)
|
|
|
|
}
|
2015-10-16 06:03:22 -04:00
|
|
|
vNextUser := vNext.Accounts[vNextUserIndex]
|
2015-09-20 10:03:12 -04:00
|
|
|
return vNext.Destination, vNextUser
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|
|
|
|
|
2015-10-14 08:51:19 -04:00
|
|
|
func (handler *VMessOutboundHandler) Dispatch(firstPacket v2net.Packet, ray ray.OutboundRay) error {
|
2015-10-03 05:34:01 -04:00
|
|
|
vNextList := handler.vNextList
|
2015-10-06 18:30:44 -04:00
|
|
|
if firstPacket.Destination().IsUDP() {
|
2015-10-03 05:34:01 -04:00
|
|
|
vNextList = handler.vNextListUDP
|
|
|
|
}
|
|
|
|
vNextAddress, vNextUser := pickVNext(vNextList)
|
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-09-16 10:27:36 -04:00
|
|
|
}
|
2015-10-07 08:50:17 -04:00
|
|
|
|
2015-10-21 16:38:53 -04:00
|
|
|
buffer := alloc.NewSmallBuffer()
|
|
|
|
defer buffer.Release()
|
|
|
|
v2net.ReadAllBytes(rand.Reader, buffer.Value[:36]) // 16 + 16 + 4
|
|
|
|
request.RequestIV = buffer.Value[:16]
|
|
|
|
request.RequestKey = buffer.Value[16:32]
|
|
|
|
request.ResponseHeader = buffer.Value[32:36]
|
2015-09-10 18:24:18 -04:00
|
|
|
|
2015-10-09 19:25:12 -04:00
|
|
|
return startCommunicate(request, vNextAddress, ray, firstPacket)
|
2015-09-12 14:36:21 -04:00
|
|
|
}
|
|
|
|
|
2015-10-14 08:51:19 -04:00
|
|
|
func startCommunicate(request *protocol.VMessRequest, dest v2net.Destination, ray ray.OutboundRay, firstPacket v2net.Packet) error {
|
2015-10-03 05:34:01 -04:00
|
|
|
conn, err := net.Dial(dest.Network(), dest.Address().String())
|
2015-09-10 18:24:18 -04:00
|
|
|
if err != nil {
|
2015-10-03 05:34:01 -04:00
|
|
|
log.Error("Failed to open %s: %v", dest.String(), 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
|
|
|
|
}
|
2015-10-06 10:43:50 -04:00
|
|
|
log.Info("VMessOut: Tunneling request to %s via %s", request.Address.String(), dest.String())
|
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()
|
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
|
|
|
|
2015-10-02 09:32:26 -04:00
|
|
|
go handleRequest(conn, request, firstPacket, input, &requestFinish)
|
2015-10-03 05:34:01 -04:00
|
|
|
go handleResponse(conn, request, output, &responseFinish, dest.IsUDP())
|
2015-09-15 15:17:06 -04:00
|
|
|
|
2015-09-23 08:14:53 -04:00
|
|
|
requestFinish.Lock()
|
2015-10-03 05:34:01 -04:00
|
|
|
if tcpConn, ok := conn.(*net.TCPConn); ok {
|
|
|
|
tcpConn.CloseWrite()
|
|
|
|
}
|
2015-09-23 08:14:53 -04:00
|
|
|
responseFinish.Lock()
|
2015-09-15 15:17:06 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-08 08:46:18 -04:00
|
|
|
func 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-09-17 17:26:09 -04:00
|
|
|
encryptRequestWriter, err := v2io.NewAesEncryptWriter(request.RequestKey[:], request.RequestIV[:], conn)
|
2015-09-12 14:36:21 -04:00
|
|
|
if err != nil {
|
2015-09-18 07:19:12 -04:00
|
|
|
log.Error("VMessOut: Failed to create encrypt writer: %v", err)
|
|
|
|
return
|
2015-09-12 14:36:21 -04:00
|
|
|
}
|
2015-09-15 18:06:22 -04:00
|
|
|
|
2015-10-10 10:54:15 -04:00
|
|
|
buffer := alloc.NewBuffer().Clear()
|
2015-10-21 16:28:26 -04:00
|
|
|
buffer, err = request.ToBytes(user.NewTimeHash(user.HMACHash{}), user.GenerateRandomInt64InRange, buffer)
|
2015-09-15 15:17:06 -04:00
|
|
|
if err != nil {
|
2015-09-17 17:26:09 -04:00
|
|
|
log.Error("VMessOut: Failed to serialize VMess request: %v", 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()
|
|
|
|
|
|
|
|
if firstChunk == nil && moreChunks {
|
|
|
|
firstChunk, moreChunks = <-input
|
|
|
|
}
|
|
|
|
|
|
|
|
if firstChunk != nil {
|
2015-10-08 08:46:18 -04:00
|
|
|
encryptRequestWriter.Crypt(firstChunk.Value)
|
2015-10-21 16:28:26 -04:00
|
|
|
buffer.Append(firstChunk.Value)
|
2015-10-08 08:46:18 -04:00
|
|
|
firstChunk.Release()
|
|
|
|
|
2015-10-21 16:28:26 -04:00
|
|
|
_, err = conn.Write(buffer.Value)
|
2015-10-08 08:46:18 -04:00
|
|
|
buffer.Release()
|
2015-09-18 06:31:42 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Error("VMessOut: Failed to write VMess request: %v", err)
|
2015-09-18 07:19:12 -04:00
|
|
|
return
|
2015-09-18 06:31:42 -04:00
|
|
|
}
|
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 {
|
2015-09-18 06:31:42 -04:00
|
|
|
v2net.ChanToWriter(encryptRequestWriter, 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
|
|
|
|
2015-10-08 08:46:18 -04:00
|
|
|
func handleResponse(conn net.Conn, request *protocol.VMessRequest, output chan<- *alloc.Buffer, finish *sync.Mutex, isUDP bool) {
|
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-09-17 17:00:17 -04:00
|
|
|
decryptResponseReader, err := v2io.NewAesDecryptReader(responseKey[:], responseIV[:], conn)
|
2015-09-12 14:36:21 -04:00
|
|
|
if err != nil {
|
2015-09-18 07:19:12 -04:00
|
|
|
log.Error("VMessOut: Failed to create decrypt reader: %v", err)
|
|
|
|
return
|
2015-09-12 14:36:21 -04:00
|
|
|
}
|
2015-10-06 03:35:02 -04:00
|
|
|
|
2015-10-08 08:46:18 -04:00
|
|
|
buffer, err := v2net.ReadFrom(decryptResponseReader, nil)
|
2015-09-10 18:24:18 -04:00
|
|
|
if err != nil {
|
2015-10-08 08:46:18 -04:00
|
|
|
log.Error("VMessOut: Failed to read VMess response (%d bytes): %v", buffer.Len(), err)
|
2015-09-18 07:19:12 -04:00
|
|
|
return
|
|
|
|
}
|
2015-10-08 08:46:18 -04:00
|
|
|
if buffer.Len() < 4 || !bytes.Equal(buffer.Value[:4], request.ResponseHeader[:]) {
|
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-10-08 08:46:18 -04:00
|
|
|
log.Info("VMessOut received %d bytes from %s", buffer.Len()-4, conn.RemoteAddr().String())
|
2015-09-15 18:06:22 -04:00
|
|
|
|
2015-10-08 08:46:18 -04:00
|
|
|
buffer.SliceFrom(4)
|
|
|
|
output <- buffer
|
2015-10-03 05:34:01 -04:00
|
|
|
|
|
|
|
if !isUDP {
|
|
|
|
v2net.ReaderToChan(output, decryptResponseReader)
|
|
|
|
}
|
|
|
|
|
2015-09-18 07:19:12 -04:00
|
|
|
return
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type VMessOutboundHandlerFactory struct {
|
|
|
|
}
|
|
|
|
|
2015-10-29 19:11:29 -04:00
|
|
|
func (factory *VMessOutboundHandlerFactory) Create(rawConfig interface{}) (connhandler.OutboundConnectionHandler, error) {
|
2015-10-16 06:03:22 -04:00
|
|
|
vOutConfig := rawConfig.(config.Outbound)
|
|
|
|
servers := make([]*config.OutboundTarget, 0, 16)
|
|
|
|
udpServers := make([]*config.OutboundTarget, 0, 16)
|
|
|
|
for _, target := range vOutConfig.Targets() {
|
|
|
|
if target.Destination.IsTCP() {
|
|
|
|
servers = append(servers, target)
|
2015-10-02 15:55:37 -04:00
|
|
|
}
|
2015-10-16 06:03:22 -04:00
|
|
|
if target.Destination.IsUDP() {
|
|
|
|
udpServers = append(udpServers, target)
|
2015-10-02 15:55:37 -04:00
|
|
|
}
|
2015-09-12 05:51:42 -04:00
|
|
|
}
|
2015-10-14 08:51:19 -04:00
|
|
|
return NewVMessOutboundHandler(servers, udpServers), nil
|
2015-09-12 14:36:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2015-10-29 19:11:29 -04:00
|
|
|
connhandler.RegisterOutboundConnectionHandlerFactory("vmess", &VMessOutboundHandlerFactory{})
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|