1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-01 19:45:24 +00:00
v2fly/proxy/vmess/vmessin.go

152 lines
4.0 KiB
Go
Raw Normal View History

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