1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-18 18:36:11 -05:00
v2fly/net/vmess/vmessin.go

139 lines
3.7 KiB
Go
Raw Normal View History

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-10 18:24:18 -04:00
"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-13 14:01:50 -04:00
v2net "github.com/v2ray/v2ray-core/net"
2015-09-10 18:24:18 -04:00
)
type VMessInboundHandler struct {
2015-09-12 16:11:54 -04:00
vPoint *core.Point
2015-09-14 13:03:31 -04:00
clients core.UserSet
2015-09-10 18:24:18 -04:00
accepting bool
}
2015-09-14 13:03:31 -04:00
func NewVMessInboundHandler(vp *core.Point, clients core.UserSet) *VMessInboundHandler {
2015-09-10 18:24:18 -04:00
handler := new(VMessInboundHandler)
handler.vPoint = vp
handler.clients = clients
2015-09-10 18:24:18 -04:00
return handler
}
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
reader := vmessio.NewVMessRequestReader(handler.clients)
2015-09-10 18:24:18 -04:00
request, err := reader.Read(connection)
if err != nil {
2015-09-14 15:59:44 -04:00
log.Debug("Failed to parse VMess request: %v", err)
2015-09-10 18:24:18 -04:00
return err
}
2015-09-12 14:36:21 -04:00
log.Debug("Received request for %s", request.Address.String())
2015-09-10 18:24:18 -04:00
response := vmessio.NewVMessResponse(request)
2015-09-12 14:36:21 -04:00
nBytes, err := connection.Write(response[:])
if err != nil {
return log.Error("Failed to write VMess response (%d bytes): %v", nBytes, err)
}
2015-09-10 18:24:18 -04:00
requestKey := request.RequestKey[:]
requestIV := request.RequestIV[:]
2015-09-10 18:24:18 -04:00
responseKey := md5.Sum(requestKey)
responseIV := md5.Sum(requestIV)
requestReader, err := v2io.NewAesDecryptReader(requestKey, requestIV, connection)
if err != nil {
2015-09-12 14:36:21 -04:00
return log.Error("Failed to create decrypt reader: %v", err)
2015-09-10 18:24:18 -04:00
}
responseWriter, err := v2io.NewAesEncryptWriter(responseKey[:], responseIV[:], connection)
if err != nil {
2015-09-12 14:36:21 -04:00
return log.Error("Failed to create encrypt writer: %v", err)
2015-09-10 18:24:18 -04:00
}
ray := handler.vPoint.NewInboundConnectionAccepted(request.Address)
2015-09-10 18:24:18 -04:00
input := ray.InboundInput()
output := ray.InboundOutput()
2015-09-14 12:19:17 -04:00
readFinish := make(chan bool)
2015-09-14 12:19:17 -04:00
writeFinish := make(chan bool)
go handler.dumpInput(requestReader, input, readFinish)
go handler.dumpOutput(responseWriter, output, writeFinish)
2015-09-14 12:19:17 -04:00
<-writeFinish
if tcpConn, ok := connection.(*net.TCPConn); ok {
log.Debug("VMessIn closing write")
tcpConn.CloseWrite()
}
<-readFinish
2015-09-10 18:24:18 -04:00
return nil
}
func (handler *VMessInboundHandler) dumpInput(reader io.Reader, input chan<- []byte, finish chan<- bool) {
2015-09-13 14:01:50 -04:00
v2net.ReaderToChan(input, reader)
close(input)
2015-09-14 12:19:17 -04:00
log.Debug("VMessIn closing input")
2015-09-13 14:01:50 -04:00
finish <- true
2015-09-10 18:24:18 -04:00
}
func (handler *VMessInboundHandler) dumpOutput(writer io.Writer, output <-chan []byte, finish chan<- bool) {
2015-09-13 14:01:50 -04:00
v2net.ChanToWriter(writer, output)
2015-09-14 12:19:17 -04:00
log.Debug("VMessOut closing output")
2015-09-13 14:01:50 -04:00
finish <- true
2015-09-10 18:24:18 -04:00
}
type VMessInboundHandlerFactory struct {
}
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 {
panic(log.Error("Failed to load VMess inbound config: %v", err))
}
2015-09-14 13:03:31 -04:00
allowedClients := core.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 {
panic(log.Error("Failed to parse user id %s: %v", client.Id, err))
}
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
}