1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-14 00:07:09 -05:00
v2fly/proxy/vmess/vmessout.go

207 lines
5.8 KiB
Go
Raw Normal View History

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
"github.com/v2ray/v2ray-core"
v2io "github.com/v2ray/v2ray-core/common/io"
2015-09-19 18:50:21 -04:00
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
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-09-10 18:24:18 -04:00
)
const (
InfoTimeNotSync = "Please check the User ID in your vmess configuration, and make sure the time on your local and remote server are in sync."
)
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-20 12:22:29 -04:00
Destination v2net.Destination // Address of VNext server
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
packet v2net.Packet
2015-09-12 05:51:42 -04:00
vNextList []VNextServer
2015-09-10 18:24:18 -04:00
}
func NewVMessOutboundHandler(vp *core.Point, vNextList []VNextServer, firstPacket v2net.Packet) *VMessOutboundHandler {
2015-09-16 10:27:36 -04:00
return &VMessOutboundHandler{
vPoint: vp,
packet: firstPacket,
2015-09-16 10:27:36 -04:00
vNextList: vNextList,
}
2015-09-10 18:24:18 -04:00
}
2015-09-20 12:22:29 -04:00
func (handler *VMessOutboundHandler) pickVNext() (v2net.Destination, 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 := 0
if vNextLen > 1 {
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 := 0
if vNextUserLen > 1 {
vNextUserIndex = mrand.Intn(vNextUserLen)
}
2015-09-10 18:24:18 -04:00
vNextUser := vNext.Users[vNextUserIndex]
return vNext.Destination, vNextUser
2015-09-10 18:24:18 -04:00
}
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-20 12:22:29 -04:00
command := protocol.CmdTCP
if handler.packet.Destination().IsUDP() {
2015-09-20 12:22:29 -04:00
command = protocol.CmdUDP
}
request := &protocol.VMessRequest{
Version: protocol.Version,
2015-09-16 10:27:36 -04:00
UserId: vNextUser.Id,
2015-09-20 12:22:29 -04:00
Command: command,
Address: handler.packet.Destination().Address(),
2015-09-16 10:27:36 -04:00
}
rand.Read(request.RequestIV[:])
rand.Read(request.RequestKey[:])
rand.Read(request.ResponseHeader[:])
2015-09-10 18:24:18 -04:00
go startCommunicate(request, vNextAddress, ray, handler.packet)
2015-09-12 14:36:21 -04:00
return nil
}
func startCommunicate(request *protocol.VMessRequest, dest v2net.Destination, ray core.OutboundRay, firstPacket v2net.Packet) error {
conn, err := net.DialTCP(dest.Network(), nil, &net.TCPAddr{dest.Address().IP(), int(dest.Address().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)
if ray != nil {
close(ray.OutboundOutput())
}
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-10 18:24:18 -04:00
defer conn.Close()
2015-09-15 18:06:22 -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-09-23 11:13:50 -04:00
go handleResponse(conn, request, output, &responseFinish)
2015-09-23 08:14:53 -04:00
requestFinish.Lock()
conn.CloseWrite()
2015-09-23 08:14:53 -04:00
responseFinish.Lock()
return nil
}
2015-10-02 09:32:26 -04:00
func handleRequest(conn *net.TCPConn, request *protocol.VMessRequest, firstPacket v2net.Packet, input <-chan []byte, finish *sync.Mutex) {
2015-09-23 08:14:53 -04:00
defer finish.Unlock()
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-22 08:50:34 -04:00
buffer := make([]byte, 0, 2*1024)
buffer, err = request.ToBytes(user.NewTimeHash(user.HMACHash{}), user.GenerateRandomInt64InRange, buffer)
if err != nil {
log.Error("VMessOut: Failed to serialize VMess request: %v", err)
2015-09-18 07:19:12 -04:00
return
}
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 {
encryptRequestWriter.Crypt(firstChunk)
buffer = append(buffer, firstChunk...)
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
}
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-18 07:19:12 -04:00
return
}
2015-09-10 18:24:18 -04:00
2015-09-23 11:13:50 -04:00
func handleResponse(conn *net.TCPConn, request *protocol.VMessRequest, output chan<- []byte, finish *sync.Mutex) {
2015-09-23 08:14:53 -04:00
defer finish.Unlock()
defer close(output)
2015-09-15 18:06:22 -04:00
responseKey := md5.Sum(request.RequestKey[:])
responseIV := md5.Sum(request.RequestIV[:])
2015-09-10 18:24:18 -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
}
response := protocol.VMessResponse{}
2015-09-24 08:55:21 -04:00
_, err = decryptResponseReader.Read(response[:])
2015-09-10 18:24:18 -04:00
if err != nil {
2015-09-24 08:51:19 -04:00
//log.Error("VMessOut: Failed to read VMess response (%d bytes): %v", nBytes, err)
2015-09-18 07:19:12 -04:00
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
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-22 12:11:55 -04:00
servers []VNextServer
2015-09-10 18:24:18 -04:00
}
2015-09-22 12:11:55 -04:00
func (factory *VMessOutboundHandlerFactory) Initialize(rawConfig []byte) 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))
2015-09-22 12:11:55 -04:00
return err
2015-09-12 05:51:42 -04:00
}
servers := make([]VNextServer, 0, len(config.VNextList))
for _, server := range config.VNextList {
servers = append(servers, server.ToVNextServer())
}
2015-09-22 12:11:55 -04:00
factory.servers = servers
return nil
}
func (factory *VMessOutboundHandlerFactory) Create(vp *core.Point, firstPacket v2net.Packet) (core.OutboundConnectionHandler, error) {
return NewVMessOutboundHandler(vp, factory.servers, firstPacket), nil
2015-09-12 14:36:21 -04:00
}
func init() {
core.RegisterOutboundConnectionHandlerFactory("vmess", &VMessOutboundHandlerFactory{})
2015-09-10 18:24:18 -04:00
}