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"
|
|
|
|
|
|
|
|
"github.com/v2ray/v2ray-core"
|
2015-09-19 17:54:36 -04:00
|
|
|
v2io "github.com/v2ray/v2ray-core/common/io"
|
|
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
2015-09-12 05:51:42 -04:00
|
|
|
"github.com/v2ray/v2ray-core/log"
|
2015-09-19 18:11:14 -04:00
|
|
|
"github.com/v2ray/v2ray-core/proxy/vmess/protocol"
|
|
|
|
"github.com/v2ray/v2ray-core/proxy/vmess/protocol/user"
|
2015-09-10 18:24:18 -04:00
|
|
|
)
|
|
|
|
|
2015-09-12 16:11:54 -04:00
|
|
|
// VNext is the next Point server in the connection chain.
|
2015-09-12 05:51:42 -04:00
|
|
|
type VNextServer struct {
|
2015-09-19 17:54:36 -04:00
|
|
|
Address v2net.Address // Address of VNext server
|
2015-09-19 18:11:14 -04:00
|
|
|
Users []user.User // User accounts for accessing VNext.
|
2015-09-12 05:51:42 -04:00
|
|
|
}
|
|
|
|
|
2015-09-10 18:24:18 -04:00
|
|
|
type VMessOutboundHandler struct {
|
2015-09-12 16:11:54 -04:00
|
|
|
vPoint *core.Point
|
|
|
|
dest v2net.Address
|
2015-09-12 05:51:42 -04:00
|
|
|
vNextList []VNextServer
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|
|
|
|
|
2015-09-12 16:11:54 -04:00
|
|
|
func NewVMessOutboundHandler(vp *core.Point, vNextList []VNextServer, dest v2net.Address) *VMessOutboundHandler {
|
2015-09-16 10:27:36 -04:00
|
|
|
return &VMessOutboundHandler{
|
|
|
|
vPoint: vp,
|
|
|
|
dest: dest,
|
|
|
|
vNextList: vNextList,
|
|
|
|
}
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|
|
|
|
|
2015-09-19 18:11:14 -04:00
|
|
|
func (handler *VMessOutboundHandler) pickVNext() (v2net.Address, user.User) {
|
2015-09-12 05:51:42 -04:00
|
|
|
vNextLen := len(handler.vNextList)
|
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
|
|
|
}
|
|
|
|
vNextIndex := mrand.Intn(vNextLen)
|
2015-09-12 05:51:42 -04:00
|
|
|
vNext := handler.vNextList[vNextIndex]
|
2015-09-10 18:24:18 -04:00
|
|
|
vNextUserLen := len(vNext.Users)
|
|
|
|
if vNextUserLen == 0 {
|
2015-09-18 07:19:12 -04:00
|
|
|
panic("VMessOut: Zero User account.")
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|
|
|
|
vNextUserIndex := mrand.Intn(vNextUserLen)
|
|
|
|
vNextUser := vNext.Users[vNextUserIndex]
|
|
|
|
return vNext.Address, vNextUser
|
|
|
|
}
|
|
|
|
|
2015-09-12 16:11:54 -04:00
|
|
|
func (handler *VMessOutboundHandler) Start(ray core.OutboundRay) error {
|
2015-09-10 18:24:18 -04:00
|
|
|
vNextAddress, vNextUser := handler.pickVNext()
|
|
|
|
|
2015-09-19 17:54:36 -04:00
|
|
|
request := &protocol.VMessRequest{
|
|
|
|
Version: protocol.Version,
|
2015-09-16 10:27:36 -04:00
|
|
|
UserId: vNextUser.Id,
|
|
|
|
Command: byte(0x01),
|
|
|
|
Address: handler.dest,
|
|
|
|
}
|
2015-09-11 11:27:36 -04:00
|
|
|
rand.Read(request.RequestIV[:])
|
|
|
|
rand.Read(request.RequestKey[:])
|
|
|
|
rand.Read(request.ResponseHeader[:])
|
2015-09-10 18:24:18 -04:00
|
|
|
|
2015-09-15 15:17:06 -04:00
|
|
|
go startCommunicate(request, vNextAddress, ray)
|
2015-09-12 14:36:21 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-19 17:54:36 -04:00
|
|
|
func startCommunicate(request *protocol.VMessRequest, dest v2net.Address, ray core.OutboundRay) error {
|
2015-09-15 18:06:22 -04:00
|
|
|
input := ray.OutboundInput()
|
2015-09-15 06:54:25 -04:00
|
|
|
output := ray.OutboundOutput()
|
2015-09-15 18:06:22 -04:00
|
|
|
|
2015-09-14 07:51:30 -04:00
|
|
|
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{dest.IP, int(dest.Port), ""})
|
2015-09-10 18:24:18 -04:00
|
|
|
if err != nil {
|
2015-09-12 14:36:21 -04:00
|
|
|
log.Error("Failed to open tcp (%s): %v", dest.String(), err)
|
2015-09-15 18:06:22 -04:00
|
|
|
close(output)
|
2015-09-10 18:24:18 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-09-18 07:19:12 -04:00
|
|
|
log.Info("VMessOut: Tunneling request for %s", request.Address.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
|
|
|
|
|
|
|
requestFinish := make(chan bool)
|
|
|
|
responseFinish := make(chan bool)
|
|
|
|
|
|
|
|
go handleRequest(conn, request, input, requestFinish)
|
|
|
|
go handleResponse(conn, request, output, responseFinish)
|
2015-09-15 15:17:06 -04:00
|
|
|
|
|
|
|
<-requestFinish
|
|
|
|
conn.CloseWrite()
|
|
|
|
<-responseFinish
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-19 17:54:36 -04:00
|
|
|
func handleRequest(conn *net.TCPConn, request *protocol.VMessRequest, input <-chan []byte, finish chan<- bool) {
|
2015-09-15 18:06:22 -04:00
|
|
|
defer close(finish)
|
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-09-19 18:11:14 -04:00
|
|
|
buffer, err := request.ToBytes(user.NewTimeHash(user.HMACHash{}), user.GenerateRandomInt64InRange)
|
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-09-18 06:31:26 -04:00
|
|
|
payload, open := <-input
|
|
|
|
if open {
|
|
|
|
encryptRequestWriter.Crypt(payload)
|
|
|
|
buffer = append(buffer, payload...)
|
2015-09-18 06:31:42 -04:00
|
|
|
|
|
|
|
_, err = conn.Write(buffer)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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-09-19 17:54:36 -04:00
|
|
|
func handleResponse(conn *net.TCPConn, request *protocol.VMessRequest, output chan<- []byte, finish chan<- bool) {
|
2015-09-15 18:06:22 -04:00
|
|
|
defer close(finish)
|
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-09-19 17:54:36 -04:00
|
|
|
response := protocol.VMessResponse{}
|
2015-09-17 17:00:17 -04:00
|
|
|
nBytes, err := decryptResponseReader.Read(response[:])
|
2015-09-10 18:24:18 -04:00
|
|
|
if err != nil {
|
2015-09-18 07:19:12 -04:00
|
|
|
log.Error("VMessOut: Failed to read VMess response (%d bytes): %v", nBytes, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !bytes.Equal(response[:], request.ResponseHeader[:]) {
|
|
|
|
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-09-15 15:17:06 -04:00
|
|
|
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-09-12 16:11:54 -04:00
|
|
|
func (factory *VMessOutboundHandlerFactory) Create(vp *core.Point, rawConfig []byte, destination v2net.Address) (core.OutboundConnectionHandler, error) {
|
2015-09-12 05:51:42 -04:00
|
|
|
config, err := loadOutboundConfig(rawConfig)
|
|
|
|
if err != nil {
|
|
|
|
panic(log.Error("Failed to load VMess outbound config: %v", err))
|
|
|
|
}
|
|
|
|
servers := make([]VNextServer, 0, len(config.VNextList))
|
|
|
|
for _, server := range config.VNextList {
|
|
|
|
servers = append(servers, server.ToVNextServer())
|
|
|
|
}
|
2015-09-12 14:36:21 -04:00
|
|
|
return NewVMessOutboundHandler(vp, servers, destination), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
core.RegisterOutboundConnectionHandlerFactory("vmess", &VMessOutboundHandlerFactory{})
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|