2015-09-10 18:24:18 -04:00
|
|
|
package vmess
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
|
|
|
"crypto/rand"
|
|
|
|
"io"
|
|
|
|
mrand "math/rand"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/v2ray/v2ray-core"
|
|
|
|
v2io "github.com/v2ray/v2ray-core/io"
|
|
|
|
vmessio "github.com/v2ray/v2ray-core/io/vmess"
|
2015-09-12 05:51:42 -04:00
|
|
|
"github.com/v2ray/v2ray-core/log"
|
2015-09-10 18:24:18 -04:00
|
|
|
v2net "github.com/v2ray/v2ray-core/net"
|
|
|
|
)
|
|
|
|
|
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-12 16:11:54 -04:00
|
|
|
Address v2net.Address // Address of VNext server
|
|
|
|
Users []core.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-10 18:24:18 -04:00
|
|
|
handler := new(VMessOutboundHandler)
|
|
|
|
handler.vPoint = vp
|
|
|
|
handler.dest = dest
|
2015-09-12 05:51:42 -04:00
|
|
|
handler.vNextList = vNextList
|
2015-09-10 18:24:18 -04:00
|
|
|
return handler
|
|
|
|
}
|
|
|
|
|
2015-09-12 16:11:54 -04:00
|
|
|
func (handler *VMessOutboundHandler) pickVNext() (v2net.Address, core.User) {
|
2015-09-12 05:51:42 -04:00
|
|
|
vNextLen := len(handler.vNextList)
|
2015-09-10 18:24:18 -04:00
|
|
|
if vNextLen == 0 {
|
|
|
|
panic("Zero vNext is configured.")
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
panic("Zero User account.")
|
|
|
|
}
|
|
|
|
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()
|
|
|
|
|
|
|
|
request := new(vmessio.VMessRequest)
|
2015-09-11 11:27:36 -04:00
|
|
|
request.Version = vmessio.Version
|
|
|
|
request.UserId = vNextUser.Id
|
|
|
|
rand.Read(request.RequestIV[:])
|
|
|
|
rand.Read(request.RequestKey[:])
|
|
|
|
rand.Read(request.ResponseHeader[:])
|
|
|
|
request.Command = byte(0x01)
|
|
|
|
request.Address = handler.dest
|
2015-09-10 18:24:18 -04:00
|
|
|
|
2015-09-12 14:36:21 -04:00
|
|
|
go handler.startCommunicate(request, vNextAddress, ray)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-12 16:11:54 -04:00
|
|
|
func (handler *VMessOutboundHandler) startCommunicate(request *vmessio.VMessRequest, dest v2net.Address, ray core.OutboundRay) error {
|
2015-09-12 14:36:21 -04:00
|
|
|
conn, err := net.Dial("tcp", dest.String())
|
|
|
|
log.Debug("VMessOutbound dialing tcp: %s", dest.String())
|
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-10 18:24:18 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
2015-09-11 11:27:36 -04:00
|
|
|
requestWriter := vmessio.NewVMessRequestWriter()
|
2015-09-12 14:36:21 -04:00
|
|
|
err = requestWriter.Write(conn, request)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Failed to write VMess request: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2015-09-10 18:24:18 -04:00
|
|
|
|
2015-09-11 11:27:36 -04:00
|
|
|
requestKey := request.RequestKey[:]
|
|
|
|
requestIV := request.RequestIV[:]
|
2015-09-10 18:24:18 -04:00
|
|
|
responseKey := md5.Sum(requestKey)
|
|
|
|
responseIV := md5.Sum(requestIV)
|
|
|
|
|
2015-09-12 14:36:21 -04:00
|
|
|
response := vmessio.VMessResponse{}
|
|
|
|
nBytes, err := conn.Read(response[:])
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Failed to read VMess response (%d bytes): %v", nBytes, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Debug("Got response %v", response)
|
|
|
|
// TODO: check response
|
|
|
|
|
2015-09-10 18:24:18 -04:00
|
|
|
encryptRequestWriter, err := v2io.NewAesEncryptWriter(requestKey, requestIV, conn)
|
|
|
|
if err != nil {
|
2015-09-12 14:36:21 -04:00
|
|
|
log.Error("Failed to create encrypt writer: %v", err)
|
2015-09-10 18:24:18 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-09-12 14:36:21 -04:00
|
|
|
decryptResponseReader, err := v2io.NewAesDecryptReader(responseKey[:], responseIV[:], conn)
|
2015-09-10 18:24:18 -04:00
|
|
|
if err != nil {
|
2015-09-12 14:36:21 -04:00
|
|
|
log.Error("Failed to create decrypt reader: %v", err)
|
2015-09-10 18:24:18 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
input := ray.OutboundInput()
|
|
|
|
output := ray.OutboundOutput()
|
|
|
|
finish := make(chan bool, 2)
|
|
|
|
|
|
|
|
go handler.dumpInput(encryptRequestWriter, input, finish)
|
2015-09-12 14:36:21 -04:00
|
|
|
go handler.dumpOutput(decryptResponseReader, output, finish)
|
2015-09-10 18:24:18 -04:00
|
|
|
handler.waitForFinish(finish)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (handler *VMessOutboundHandler) dumpOutput(reader io.Reader, output chan<- []byte, finish chan<- bool) {
|
|
|
|
for {
|
|
|
|
buffer := make([]byte, BufferSize)
|
|
|
|
nBytes, err := reader.Read(buffer)
|
2015-09-12 14:36:21 -04:00
|
|
|
log.Debug("VMessOutbound: Reading %d bytes, with error %v", nBytes, err)
|
2015-09-10 18:24:18 -04:00
|
|
|
if err == io.EOF {
|
2015-09-12 05:51:42 -04:00
|
|
|
close(output)
|
2015-09-10 18:24:18 -04:00
|
|
|
finish <- true
|
2015-09-12 14:36:21 -04:00
|
|
|
log.Debug("VMessOutbound finishing output.")
|
2015-09-10 18:24:18 -04:00
|
|
|
break
|
|
|
|
}
|
|
|
|
output <- buffer[:nBytes]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (handler *VMessOutboundHandler) dumpInput(writer io.Writer, input <-chan []byte, finish chan<- bool) {
|
|
|
|
for {
|
|
|
|
buffer, open := <-input
|
|
|
|
if !open {
|
|
|
|
finish <- true
|
2015-09-12 14:36:21 -04:00
|
|
|
log.Debug("VMessOutbound finishing input.")
|
2015-09-10 18:24:18 -04:00
|
|
|
break
|
|
|
|
}
|
2015-09-12 14:36:21 -04:00
|
|
|
nBytes, err := writer.Write(buffer)
|
|
|
|
log.Debug("VMessOutbound: Wrote %d bytes with error %v", nBytes, err)
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (handler *VMessOutboundHandler) waitForFinish(finish <-chan bool) {
|
2015-09-12 18:25:50 -04:00
|
|
|
<-finish
|
|
|
|
<-finish
|
2015-09-12 14:36:21 -04:00
|
|
|
log.Debug("Finishing waiting for VMessOutbound ending.")
|
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
|
|
|
}
|