2015-09-10 18:24:18 -04:00
|
|
|
package vmess
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
|
|
|
"io"
|
|
|
|
"net"
|
2015-09-12 14:36:21 -04:00
|
|
|
"strconv"
|
2015-09-21 11:48:08 -04:00
|
|
|
"time"
|
2015-09-10 18:24:18 -04:00
|
|
|
|
|
|
|
"github.com/v2ray/v2ray-core"
|
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-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
|
|
|
)
|
|
|
|
|
2015-09-21 11:37:06 -04:00
|
|
|
const (
|
2015-09-21 11:48:08 -04:00
|
|
|
requestReadTimeOut = 4 * time.Second
|
2015-09-21 11:37:06 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-09-21 11:48:08 -04:00
|
|
|
zeroTime time.Time
|
2015-09-21 11:37:06 -04:00
|
|
|
)
|
|
|
|
|
2015-09-10 18:24:18 -04:00
|
|
|
type VMessInboundHandler struct {
|
2015-09-12 16:11:54 -04:00
|
|
|
vPoint *core.Point
|
2015-09-19 18:11:14 -04:00
|
|
|
clients user.UserSet
|
2015-09-10 18:24:18 -04:00
|
|
|
accepting bool
|
|
|
|
}
|
|
|
|
|
2015-09-19 18:11:14 -04:00
|
|
|
func NewVMessInboundHandler(vp *core.Point, clients user.UserSet) *VMessInboundHandler {
|
2015-09-16 10:27:36 -04:00
|
|
|
return &VMessInboundHandler{
|
|
|
|
vPoint: vp,
|
|
|
|
clients: clients,
|
|
|
|
}
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|
|
|
|
|
2015-09-12 14:36:21 -04:00
|
|
|
func (handler *VMessInboundHandler) Listen(port uint16) error {
|
|
|
|
listener, err := net.Listen("tcp", ":"+strconv.Itoa(int(port)))
|
2015-09-10 18:24:18 -04:00
|
|
|
if err != nil {
|
2015-09-12 14:36:21 -04:00
|
|
|
return log.Error("Unable to listen tcp:%d", port)
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|
|
|
|
handler.accepting = true
|
|
|
|
go handler.AcceptConnections(listener)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (handler *VMessInboundHandler) AcceptConnections(listener net.Listener) error {
|
|
|
|
for handler.accepting {
|
|
|
|
connection, err := listener.Accept()
|
|
|
|
if err != nil {
|
2015-09-12 14:36:21 -04:00
|
|
|
return log.Error("Failed to accpet connection: %s", err.Error())
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|
|
|
|
go handler.HandleConnection(connection)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (handler *VMessInboundHandler) HandleConnection(connection net.Conn) error {
|
|
|
|
defer connection.Close()
|
2015-09-14 12:19:17 -04:00
|
|
|
|
2015-09-19 17:54:36 -04:00
|
|
|
reader := protocol.NewVMessRequestReader(handler.clients)
|
2015-09-10 18:24:18 -04:00
|
|
|
|
2015-09-21 11:48:08 -04:00
|
|
|
// Timeout 4 seconds to prevent DoS attack
|
|
|
|
connection.SetReadDeadline(time.Now().Add(requestReadTimeOut))
|
2015-09-10 18:24:18 -04:00
|
|
|
request, err := reader.Read(connection)
|
|
|
|
if err != nil {
|
2015-09-18 07:19:12 -04:00
|
|
|
log.Warning("VMessIn: Invalid request from (%s): %v", connection.RemoteAddr().String(), err)
|
2015-09-10 18:24:18 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-09-18 07:19:12 -04:00
|
|
|
log.Debug("VMessIn: Received request for %s", request.Address.String())
|
2015-09-21 11:48:08 -04:00
|
|
|
// Clear read timeout
|
|
|
|
connection.SetReadDeadline(zeroTime)
|
2015-09-10 18:24:18 -04:00
|
|
|
|
2015-09-20 10:03:12 -04:00
|
|
|
ray := handler.vPoint.NewInboundConnectionAccepted(request.Destination())
|
2015-09-17 17:00:17 -04:00
|
|
|
input := ray.InboundInput()
|
|
|
|
output := ray.InboundOutput()
|
2015-09-10 18:24:18 -04:00
|
|
|
|
2015-09-17 17:00:17 -04:00
|
|
|
readFinish := make(chan bool)
|
|
|
|
writeFinish := make(chan bool)
|
2015-09-10 18:24:18 -04:00
|
|
|
|
2015-09-17 17:00:17 -04:00
|
|
|
go handleInput(request, connection, input, readFinish)
|
|
|
|
|
|
|
|
responseKey := md5.Sum(request.RequestKey[:])
|
|
|
|
responseIV := md5.Sum(request.RequestIV[:])
|
2015-09-10 18:24:18 -04:00
|
|
|
|
2015-09-19 17:54:36 -04:00
|
|
|
response := protocol.NewVMessResponse(request)
|
2015-09-10 18:24:18 -04:00
|
|
|
responseWriter, err := v2io.NewAesEncryptWriter(responseKey[:], responseIV[:], connection)
|
|
|
|
if err != nil {
|
2015-09-18 07:19:12 -04:00
|
|
|
return log.Error("VMessIn: Failed to create encrypt writer: %v", err)
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|
|
|
|
|
2015-09-17 17:00:17 -04:00
|
|
|
// Optimize for small response packet
|
|
|
|
buffer := make([]byte, 0, 1024)
|
|
|
|
buffer = append(buffer, response[:]...)
|
2015-09-18 06:31:42 -04:00
|
|
|
|
2015-09-18 06:31:26 -04:00
|
|
|
if data, open := <-output; open {
|
2015-09-17 17:00:17 -04:00
|
|
|
buffer = append(buffer, data...)
|
2015-09-18 06:31:42 -04:00
|
|
|
responseWriter.Write(buffer)
|
|
|
|
go handleOutput(request, responseWriter, output, writeFinish)
|
|
|
|
<-writeFinish
|
2015-09-17 17:00:17 -04:00
|
|
|
}
|
2015-09-18 06:31:42 -04:00
|
|
|
|
2015-09-14 12:19:17 -04:00
|
|
|
if tcpConn, ok := connection.(*net.TCPConn); ok {
|
|
|
|
tcpConn.CloseWrite()
|
|
|
|
}
|
|
|
|
<-readFinish
|
2015-09-10 18:24:18 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-19 17:54:36 -04:00
|
|
|
func handleInput(request *protocol.VMessRequest, reader io.Reader, input chan<- []byte, finish chan<- bool) {
|
2015-09-17 17:00:17 -04:00
|
|
|
defer close(input)
|
|
|
|
defer close(finish)
|
|
|
|
|
|
|
|
requestReader, err := v2io.NewAesDecryptReader(request.RequestKey[:], request.RequestIV[:], reader)
|
|
|
|
if err != nil {
|
2015-09-18 07:19:12 -04:00
|
|
|
log.Error("VMessIn: Failed to create decrypt reader: %v", err)
|
2015-09-17 17:00:17 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
v2net.ReaderToChan(input, requestReader)
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|
|
|
|
|
2015-09-19 17:54:36 -04:00
|
|
|
func handleOutput(request *protocol.VMessRequest, writer io.Writer, output <-chan []byte, finish chan<- bool) {
|
2015-09-13 14:01:50 -04:00
|
|
|
v2net.ChanToWriter(writer, output)
|
2015-09-17 17:00:17 -04:00
|
|
|
close(finish)
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type VMessInboundHandlerFactory struct {
|
2015-09-11 11:27:36 -04:00
|
|
|
}
|
|
|
|
|
2015-09-12 16:11:54 -04:00
|
|
|
func (factory *VMessInboundHandlerFactory) Create(vp *core.Point, rawConfig []byte) (core.InboundConnectionHandler, error) {
|
2015-09-12 05:51:42 -04:00
|
|
|
config, err := loadInboundConfig(rawConfig)
|
|
|
|
if err != nil {
|
2015-09-18 07:19:12 -04:00
|
|
|
panic(log.Error("VMessIn: Failed to load VMess inbound config: %v", err))
|
2015-09-11 11:27:36 -04:00
|
|
|
}
|
2015-09-19 18:11:14 -04:00
|
|
|
allowedClients := user.NewTimedUserSet()
|
2015-09-12 14:36:21 -04:00
|
|
|
for _, client := range config.AllowedClients {
|
2015-09-12 16:11:54 -04:00
|
|
|
user, err := client.ToUser()
|
2015-09-12 14:36:21 -04:00
|
|
|
if err != nil {
|
2015-09-18 07:19:12 -04:00
|
|
|
panic(log.Error("VMessIn: Failed to parse user id %s: %v", client.Id, err))
|
2015-09-12 14:36:21 -04:00
|
|
|
}
|
2015-09-12 05:51:42 -04:00
|
|
|
allowedClients.AddUser(user)
|
|
|
|
}
|
2015-09-12 14:36:21 -04:00
|
|
|
return NewVMessInboundHandler(vp, allowedClients), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
core.RegisterInboundConnectionHandlerFactory("vmess", &VMessInboundHandlerFactory{})
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|