1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 01:57:12 -05:00

fix a race condition issue in VMess inbound

This commit is contained in:
v2ray 2016-06-18 17:01:48 +02:00
parent bac9304e05
commit 20f046af0b
2 changed files with 22 additions and 1 deletions

View File

@ -18,6 +18,9 @@ func (this *VMessInboundHandler) generateCommand(request *protocol.RequestHeader
log.Info("VMessIn: Pick detour handler for port ", inboundHandler.Port(), " for ", availableMin, " minutes.") log.Info("VMessIn: Pick detour handler for port ", inboundHandler.Port(), " for ", availableMin, " minutes.")
user := inboundHandler.GetUser(request.User.Email) user := inboundHandler.GetUser(request.User.Email)
if user == nil {
return nil
}
return &protocol.CommandSwitchAccount{ return &protocol.CommandSwitchAccount{
Port: inboundHandler.Port(), Port: inboundHandler.Port(),
ID: user.Account.(*protocol.VMessAccount).ID.UUID(), ID: user.Account.(*protocol.VMessAccount).ID.UUID(),

View File

@ -65,7 +65,7 @@ func (this *userByEmail) Get(email string) (*protocol.User, bool) {
// Inbound connection handler that handles messages in VMess format. // Inbound connection handler that handles messages in VMess format.
type VMessInboundHandler struct { type VMessInboundHandler struct {
sync.Mutex sync.RWMutex
packetDispatcher dispatcher.PacketDispatcher packetDispatcher dispatcher.PacketDispatcher
inboundHandlerManager proxyman.InboundHandlerManager inboundHandlerManager proxyman.InboundHandlerManager
clients protocol.UserValidator clients protocol.UserValidator
@ -93,6 +93,13 @@ func (this *VMessInboundHandler) Close() {
} }
func (this *VMessInboundHandler) GetUser(email string) *protocol.User { func (this *VMessInboundHandler) GetUser(email string) *protocol.User {
this.RLock()
defer this.RUnlock()
if !this.accepting {
return nil
}
user, existing := this.usersByEmail.Get(email) user, existing := this.usersByEmail.Get(email)
if !existing { if !existing {
this.clients.Add(user) this.clients.Add(user)
@ -120,16 +127,27 @@ func (this *VMessInboundHandler) Start() error {
func (this *VMessInboundHandler) HandleConnection(connection internet.Connection) { func (this *VMessInboundHandler) HandleConnection(connection internet.Connection) {
defer connection.Close() defer connection.Close()
if !this.accepting {
return
}
connReader := v2net.NewTimeOutReader(8, connection) connReader := v2net.NewTimeOutReader(8, connection)
defer connReader.Release() defer connReader.Release()
reader := v2io.NewBufferedReader(connReader) reader := v2io.NewBufferedReader(connReader)
defer reader.Release() defer reader.Release()
this.RLock()
if !this.accepting {
this.RUnlock()
return
}
session := raw.NewServerSession(this.clients) session := raw.NewServerSession(this.clients)
defer session.Release() defer session.Release()
request, err := session.DecodeRequestHeader(reader) request, err := session.DecodeRequestHeader(reader)
this.RUnlock()
if err != nil { if err != nil {
if err != io.EOF { if err != io.EOF {
log.Access(connection.RemoteAddr(), "", log.AccessRejected, err) log.Access(connection.RemoteAddr(), "", log.AccessRejected, err)