v2ray-core/proxy/vmess/inbound.go

159 lines
4.7 KiB
Go
Raw Normal View History

2015-09-10 22:24:18 +00:00
package vmess
import (
"crypto/md5"
"io"
"net"
2015-09-23 12:14:53 +00:00
"sync"
2015-09-10 22:24:18 +00:00
2015-10-14 12:51:19 +00:00
"github.com/v2ray/v2ray-core/app"
"github.com/v2ray/v2ray-core/common/alloc"
2015-11-03 20:26:16 +00:00
v2crypto "github.com/v2ray/v2ray-core/common/crypto"
2015-09-19 22:50:21 +00:00
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
2015-10-15 11:42:43 +00:00
"github.com/v2ray/v2ray-core/common/retry"
"github.com/v2ray/v2ray-core/proxy/common/connhandler"
2015-10-16 10:03:22 +00:00
"github.com/v2ray/v2ray-core/proxy/vmess/config"
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
)
// Inbound connection handler that handles messages in VMess format.
2015-09-10 22:24:18 +00:00
type VMessInboundHandler struct {
2015-10-14 12:51:19 +00:00
dispatcher app.PacketDispatcher
2015-10-03 09:34:01 +00:00
clients user.UserSet
accepting bool
2015-09-10 22:24:18 +00:00
}
2015-11-04 20:52:48 +00:00
func NewVMessInboundHandler(dispatcher app.PacketDispatcher, clients user.UserSet) *VMessInboundHandler {
2015-09-16 14:27:36 +00:00
return &VMessInboundHandler{
2015-10-14 12:51:19 +00:00
dispatcher: dispatcher,
2015-10-03 09:34:01 +00:00
clients: clients,
2015-09-16 14:27:36 +00:00
}
2015-09-10 22:24:18 +00:00
}
2015-11-27 20:57:15 +00:00
func (this *VMessInboundHandler) Listen(port uint16) error {
2015-10-07 23:39:50 +00:00
listener, err := net.ListenTCP("tcp", &net.TCPAddr{
IP: []byte{0, 0, 0, 0},
Port: int(port),
Zone: "",
})
2015-09-10 22:24:18 +00:00
if err != nil {
log.Error("Unable to listen tcp port %d: %v", port, err)
return err
2015-09-10 22:24:18 +00:00
}
2015-11-27 20:57:15 +00:00
this.accepting = true
go this.AcceptConnections(listener)
2015-09-10 22:24:18 +00:00
return nil
}
2015-11-27 20:57:15 +00:00
func (this *VMessInboundHandler) AcceptConnections(listener *net.TCPListener) error {
for this.accepting {
2015-10-15 11:42:43 +00:00
retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
connection, err := listener.AcceptTCP()
if err != nil {
log.Error("Failed to accpet connection: %s", err.Error())
return err
}
2015-11-27 20:57:15 +00:00
go this.HandleConnection(connection)
2015-10-15 11:42:43 +00:00
return nil
})
2015-09-10 22:24:18 +00:00
}
return nil
}
2015-11-27 20:57:15 +00:00
func (this *VMessInboundHandler) HandleConnection(connection *net.TCPConn) error {
2015-09-10 22:24:18 +00:00
defer connection.Close()
2015-09-14 16:19:17 +00:00
connReader := v2net.NewTimeOutReader(16, connection)
2015-11-27 20:57:15 +00:00
requestReader := protocol.NewVMessRequestReader(this.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-10-09 15:49:59 +00:00
log.Access(connection.RemoteAddr().String(), "", log.AccessRejected, err.Error())
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-10-09 15:43:27 +00:00
log.Access(connection.RemoteAddr().String(), request.Address.String(), log.AccessAccepted, "")
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-11-27 20:57:15 +00:00
ray := this.dispatcher.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-10-31 13:08:13 +00:00
userSettings := config.GetUserSettings(request.User.Level())
2015-10-31 08:39:45 +00:00
connReader.SetTimeOut(userSettings.PayloadReadTimeout)
2015-09-24 12:51:19 +00:00
go handleInput(request, connReader, input, &readFinish)
2015-10-07 12:58:31 +00:00
responseKey := md5.Sum(request.RequestKey)
responseIV := md5.Sum(request.RequestIV)
2015-09-10 22:24:18 +00:00
2015-11-03 20:26:16 +00:00
aesStream, err := v2crypto.NewAesEncryptionStream(responseKey[:], responseIV[:])
2015-09-10 22:24:18 +00:00
if err != nil {
2015-11-03 20:26:16 +00:00
log.Error("VMessIn: Failed to create AES decryption stream: %v", err)
return err
2015-09-10 22:24:18 +00:00
}
2015-11-03 20:26:16 +00:00
responseWriter := v2crypto.NewCryptionWriter(aesStream, connection)
// Optimize for small response packet
2015-10-10 14:50:19 +00:00
buffer := alloc.NewLargeBuffer().Clear()
buffer.AppendBytes(request.ResponseHeader[0] ^ request.ResponseHeader[1])
buffer.AppendBytes(request.ResponseHeader[2] ^ request.ResponseHeader[3])
2015-11-28 09:11:56 +00:00
buffer.AppendBytes(byte(0), byte(0))
2015-09-18 10:31:42 +00:00
if data, open := <-output; open {
2015-10-08 15:41:38 +00:00
buffer.Append(data.Value)
data.Release()
responseWriter.Write(buffer.Value)
buffer.Release()
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-10-07 23:39:50 +00:00
connection.CloseWrite()
2015-09-23 12:14:53 +00:00
readFinish.Lock()
2015-09-10 22:24:18 +00:00
return nil
}
func handleInput(request *protocol.VMessRequest, reader io.Reader, input chan<- *alloc.Buffer, finish *sync.Mutex) {
defer close(input)
2015-09-23 12:14:53 +00:00
defer finish.Unlock()
2015-11-03 20:26:16 +00:00
aesStream, err := v2crypto.NewAesDecryptionStream(request.RequestKey, request.RequestIV)
if err != nil {
2015-11-03 20:26:16 +00:00
log.Error("VMessIn: Failed to create AES decryption stream: %v", err)
return
}
2015-11-03 20:26:16 +00:00
requestReader := v2crypto.NewCryptionReader(aesStream, reader)
v2net.ReaderToChan(input, requestReader)
2015-09-10 22:24:18 +00:00
}
func handleOutput(request *protocol.VMessRequest, writer io.Writer, output <-chan *alloc.Buffer, 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-11-27 20:57:15 +00:00
func (this *VMessInboundHandlerFactory) Create(dispatcher app.PacketDispatcher, rawConfig interface{}) (connhandler.InboundConnectionHandler, error) {
2015-10-16 10:03:22 +00:00
config := rawConfig.(config.Inbound)
2015-10-06 21:11:08 +00:00
2015-09-19 22:11:14 +00:00
allowedClients := user.NewTimedUserSet()
2015-10-16 10:03:22 +00:00
for _, user := range config.AllowedUsers() {
2015-09-12 09:51:42 +00:00
allowedClients.AddUser(user)
}
2015-10-03 09:34:01 +00:00
2015-11-04 20:52:48 +00:00
return NewVMessInboundHandler(dispatcher, allowedClients), nil
2015-09-12 18:36:21 +00:00
}
func init() {
connhandler.RegisterInboundConnectionHandlerFactory("vmess", &VMessInboundHandlerFactory{})
2015-09-10 22:24:18 +00:00
}