1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-29 18:45:23 +00:00
v2fly/proxy/vmess/inbound/inbound.go

165 lines
4.6 KiB
Go
Raw Normal View History

package inbound
2015-09-10 22:24:18 +00:00
import (
"crypto/md5"
"io"
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"
2016-01-18 11:31:27 +00:00
"github.com/v2ray/v2ray-core/common/serial"
2016-01-02 22:32:18 +00:00
"github.com/v2ray/v2ray-core/proxy"
2016-01-02 22:08:36 +00:00
"github.com/v2ray/v2ray-core/proxy/internal"
2015-12-07 19:32:38 +00:00
"github.com/v2ray/v2ray-core/proxy/vmess"
2015-09-19 22:11:14 +00:00
"github.com/v2ray/v2ray-core/proxy/vmess/protocol"
2016-01-28 16:08:32 +00:00
"github.com/v2ray/v2ray-core/transport/hub"
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 {
sync.Mutex
2016-01-19 22:41:40 +00:00
space app.Space
clients protocol.UserSet
user *vmess.User
accepting bool
2016-01-28 19:47:00 +00:00
listener *hub.TCPHub
2016-01-19 22:41:40 +00:00
features *FeaturesConfig
listeningPort v2net.Port
2015-09-10 22:24:18 +00:00
}
2016-01-19 22:41:40 +00:00
func (this *VMessInboundHandler) Port() v2net.Port {
return this.listeningPort
2015-09-10 22:24:18 +00:00
}
func (this *VMessInboundHandler) Close() {
this.accepting = false
if this.listener != nil {
2016-01-22 16:56:19 +00:00
this.Lock()
this.listener.Close()
2016-01-03 23:33:25 +00:00
this.listener = nil
this.Unlock()
}
}
2016-01-19 22:41:40 +00:00
func (this *VMessInboundHandler) GetUser() *vmess.User {
return this.user
2016-01-08 23:10:57 +00:00
}
2015-12-02 20:44:01 +00:00
func (this *VMessInboundHandler) Listen(port v2net.Port) error {
2016-01-19 22:41:40 +00:00
if this.accepting {
if this.listeningPort == port {
return nil
} else {
return proxy.ErrorAlreadyListening
}
}
this.listeningPort = port
2016-01-28 16:08:32 +00:00
tcpListener, err := hub.ListenTCP(port, this.HandleConnection)
2015-09-10 22:24:18 +00:00
if err != nil {
2016-01-18 11:24:33 +00:00
log.Error("Unable to listen tcp port ", port, ": ", err)
return err
2015-09-10 22:24:18 +00:00
}
2015-11-27 20:57:15 +00:00
this.accepting = true
2016-01-04 07:40:24 +00:00
this.Lock()
this.listener = tcpListener
2016-01-04 07:40:24 +00:00
this.Unlock()
2015-09-10 22:24:18 +00:00
return nil
}
2016-01-28 16:08:32 +00:00
func (this *VMessInboundHandler) HandleConnection(connection *hub.TCPConn) {
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 {
2016-01-18 11:31:27 +00:00
log.Access(connection.RemoteAddr(), serial.StringLiteral(""), log.AccessRejected, serial.StringLiteral(err.Error()))
2016-01-18 11:24:33 +00:00
log.Warning("VMessIn: Invalid request from ", connection.RemoteAddr(), ": ", err)
return
2015-09-10 22:24:18 +00:00
}
2016-01-18 11:31:27 +00:00
log.Access(connection.RemoteAddr(), request.Address, log.AccessAccepted, serial.StringLiteral(""))
2016-01-18 11:24:33 +00:00
log.Debug("VMessIn: Received request for ", request.Address)
2015-09-10 22:24:18 +00:00
2015-12-05 21:55:45 +00:00
ray := this.space.PacketDispatcher().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
userSettings := vmess.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 {
2016-01-18 11:24:33 +00:00
log.Error("VMessIn: Failed to create AES decryption stream: ", err)
close(input)
return
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()
2016-01-04 12:01:32 +00:00
defer buffer.Release()
buffer.AppendBytes(request.ResponseHeader, byte(0))
2016-01-21 16:22:56 +00:00
this.generateCommand(buffer)
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)
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
}
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 {
2016-01-18 11:24:33 +00:00
log.Error("VMessIn: Failed to create AES decryption stream: ", 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
}
func init() {
2016-01-25 16:29:26 +00:00
internal.MustRegisterInboundHandlerCreator("vmess",
func(space app.Space, rawConfig interface{}) (proxy.InboundHandler, error) {
config := rawConfig.(*Config)
2016-01-12 10:52:40 +00:00
allowedClients := protocol.NewTimedUserSet()
for _, user := range config.AllowedUsers {
allowedClients.AddUser(user)
}
2015-10-06 21:11:08 +00:00
2016-01-19 22:41:40 +00:00
return &VMessInboundHandler{
space: space,
clients: allowedClients,
features: config.Features,
user: config.AllowedUsers[0],
}, nil
})
2015-09-10 22:24:18 +00:00
}