1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-27 22:36:12 -04:00
v2fly/proxy/vmess/inbound/inbound.go

233 lines
6.2 KiB
Go
Raw Normal View History

package inbound
2015-09-10 18:24:18 -04:00
import (
2015-09-23 08:14:53 -04:00
"sync"
2015-09-10 18:24:18 -04:00
2015-10-14 08:51:19 -04:00
"github.com/v2ray/v2ray-core/app"
2016-01-31 11:01:28 -05:00
"github.com/v2ray/v2ray-core/app/dispatcher"
"github.com/v2ray/v2ray-core/app/proxyman"
2016-04-28 15:14:00 -04:00
"github.com/v2ray/v2ray-core/common/alloc"
2016-01-29 08:39:55 -05:00
v2io "github.com/v2ray/v2ray-core/common/io"
2015-09-19 18:50:21 -04:00
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
2016-05-07 14:26:29 -04:00
"github.com/v2ray/v2ray-core/common/protocol"
"github.com/v2ray/v2ray-core/common/protocol/raw"
2016-01-18 06:31:27 -05:00
"github.com/v2ray/v2ray-core/common/serial"
2016-02-25 08:38:41 -05:00
"github.com/v2ray/v2ray-core/common/uuid"
2016-01-02 17:32:18 -05:00
"github.com/v2ray/v2ray-core/proxy"
2016-01-02 17:08:36 -05:00
"github.com/v2ray/v2ray-core/proxy/internal"
2016-02-01 06:22:29 -05:00
vmessio "github.com/v2ray/v2ray-core/proxy/vmess/io"
2016-01-28 11:08:32 -05:00
"github.com/v2ray/v2ray-core/transport/hub"
2015-09-10 18:24:18 -04:00
)
2016-02-25 08:38:41 -05:00
type userByEmail struct {
sync.RWMutex
2016-05-07 14:26:29 -04:00
cache map[string]*protocol.User
defaultLevel protocol.UserLevel
2016-02-25 08:38:41 -05:00
defaultAlterIDs uint16
}
2016-05-07 14:26:29 -04:00
func NewUserByEmail(users []*protocol.User, config *DefaultConfig) *userByEmail {
cache := make(map[string]*protocol.User)
2016-02-25 08:38:41 -05:00
for _, user := range users {
cache[user.Email] = user
}
return &userByEmail{
cache: cache,
defaultLevel: config.Level,
defaultAlterIDs: config.AlterIDs,
}
}
2016-05-07 14:26:29 -04:00
func (this *userByEmail) Get(email string) (*protocol.User, bool) {
var user *protocol.User
2016-02-25 08:38:41 -05:00
var found bool
this.RLock()
user, found = this.cache[email]
this.RUnlock()
if !found {
this.Lock()
user, found = this.cache[email]
if !found {
2016-05-07 14:26:29 -04:00
id := protocol.NewID(uuid.New())
2016-05-07 15:07:46 -04:00
alterIDs := protocol.NewAlterIDs(id, this.defaultAlterIDs)
user = protocol.NewUser(id, alterIDs, this.defaultLevel, email)
2016-02-25 08:38:41 -05:00
this.cache[email] = user
}
this.Unlock()
}
return user, found
}
// Inbound connection handler that handles messages in VMess format.
2015-09-10 18:24:18 -04:00
type VMessInboundHandler struct {
sync.Mutex
2016-01-31 11:01:28 -05:00
packetDispatcher dispatcher.PacketDispatcher
inboundHandlerManager proxyman.InboundHandlerManager
2016-05-07 14:26:29 -04:00
clients protocol.UserValidator
2016-02-25 08:38:41 -05:00
usersByEmail *userByEmail
2016-01-31 11:01:28 -05:00
accepting bool
listener *hub.TCPHub
features *FeaturesConfig
listeningPort v2net.Port
2015-09-10 18:24:18 -04:00
}
2016-01-19 17:41:40 -05:00
func (this *VMessInboundHandler) Port() v2net.Port {
return this.listeningPort
2015-09-10 18:24:18 -04:00
}
func (this *VMessInboundHandler) Close() {
this.accepting = false
if this.listener != nil {
2016-01-22 11:56:19 -05:00
this.Lock()
this.listener.Close()
2016-01-03 18:33:25 -05:00
this.listener = nil
this.Unlock()
}
}
2016-05-07 14:26:29 -04:00
func (this *VMessInboundHandler) GetUser(email string) *protocol.User {
2016-02-25 08:38:41 -05:00
user, existing := this.usersByEmail.Get(email)
if !existing {
2016-02-25 10:40:43 -05:00
this.clients.Add(user)
2016-02-25 08:38:41 -05:00
}
return user
2016-01-08 18:10:57 -05:00
}
2015-12-02 15:44:01 -05:00
func (this *VMessInboundHandler) Listen(port v2net.Port) error {
2016-01-19 17:41:40 -05:00
if this.accepting {
if this.listeningPort == port {
return nil
} else {
return proxy.ErrorAlreadyListening
}
}
this.listeningPort = port
tcpListener, err := hub.ListenTCP(port, this.HandleConnection, nil)
2015-09-10 18:24:18 -04:00
if err != nil {
2016-01-18 06:24:33 -05:00
log.Error("Unable to listen tcp port ", port, ": ", err)
return err
2015-09-10 18:24:18 -04:00
}
2015-11-27 15:57:15 -05:00
this.accepting = true
2016-01-04 02:40:24 -05:00
this.Lock()
this.listener = tcpListener
2016-01-04 02:40:24 -05:00
this.Unlock()
2015-09-10 18:24:18 -04:00
return nil
}
2016-05-02 17:53:16 -04:00
func (this *VMessInboundHandler) HandleConnection(connection *hub.Connection) {
2015-09-10 18:24:18 -04:00
defer connection.Close()
2015-09-14 12:19:17 -04:00
connReader := v2net.NewTimeOutReader(16, connection)
2016-03-11 17:51:58 -05:00
defer connReader.Release()
2015-09-10 18:24:18 -04:00
2016-02-27 11:28:21 -05:00
reader := v2io.NewBufferedReader(connReader)
2016-03-11 17:51:58 -05:00
defer reader.Release()
2016-02-27 11:28:21 -05:00
session := raw.NewServerSession(this.clients)
2016-04-25 12:39:30 -04:00
defer session.Release()
2016-02-27 11:28:21 -05:00
request, err := session.DecodeRequestHeader(reader)
2015-09-10 18:24:18 -04:00
if err != nil {
2016-01-18 06:31:27 -05:00
log.Access(connection.RemoteAddr(), serial.StringLiteral(""), log.AccessRejected, serial.StringLiteral(err.Error()))
2016-01-18 06:24:33 -05:00
log.Warning("VMessIn: Invalid request from ", connection.RemoteAddr(), ": ", err)
return
2015-09-10 18:24:18 -04:00
}
2016-02-22 11:10:11 -05:00
log.Access(connection.RemoteAddr(), request.Destination(), log.AccessAccepted, serial.StringLiteral(""))
log.Debug("VMessIn: Received request for ", request.Destination())
2015-09-10 18:24:18 -04:00
2016-04-25 18:13:26 -04:00
ray := this.packetDispatcher.DispatchToOutbound(request.Destination())
input := ray.InboundInput()
output := ray.InboundOutput()
2016-05-07 03:53:15 -04:00
defer input.Close()
defer output.Release()
2015-09-23 08:14:53 -04:00
var readFinish, writeFinish sync.Mutex
readFinish.Lock()
writeFinish.Lock()
2015-09-10 18:24:18 -04:00
2016-05-07 14:26:29 -04:00
userSettings := protocol.GetUserSettings(request.User.Level)
2015-10-31 04:39:45 -04:00
connReader.SetTimeOut(userSettings.PayloadReadTimeout)
2016-02-27 11:28:21 -05:00
reader.SetCached(false)
go func() {
bodyReader := session.DecodeRequestBody(reader)
2016-04-12 15:43:13 -04:00
var requestReader v2io.Reader
2016-02-27 11:28:21 -05:00
if request.Option.IsChunkStream() {
requestReader = vmessio.NewAuthChunkReader(bodyReader)
} else {
requestReader = v2io.NewAdaptiveReader(bodyReader)
}
2016-04-18 12:44:10 -04:00
v2io.Pipe(requestReader, input)
2016-03-13 06:34:09 -04:00
requestReader.Release()
2016-05-09 11:23:31 -04:00
input.Close()
readFinish.Unlock()
2016-02-27 11:28:21 -05:00
}()
2016-02-27 11:28:21 -05:00
writer := v2io.NewBufferedWriter(connection)
2016-03-11 17:51:58 -05:00
defer writer.Release()
2015-09-10 18:24:18 -04:00
2016-05-07 14:26:29 -04:00
response := &protocol.ResponseHeader{
2016-02-27 11:28:21 -05:00
Command: this.generateCommand(request),
}
2015-11-03 15:26:16 -05:00
2016-02-27 11:28:21 -05:00
session.EncodeResponseHeader(response, writer)
bodyWriter := session.EncodeResponseBody(writer)
2015-09-18 06:31:42 -04:00
2016-02-27 11:28:21 -05:00
// Optimize for small response packet
2016-04-18 12:44:10 -04:00
if data, err := output.Read(); err == nil {
2016-02-27 11:28:21 -05:00
if request.Option.IsChunkStream() {
2016-02-01 06:22:29 -05:00
vmessio.Authenticate(data)
}
2016-02-27 11:28:21 -05:00
bodyWriter.Write(data.Value)
2015-10-08 11:41:38 -04:00
data.Release()
2016-02-27 11:28:21 -05:00
writer.SetCached(false)
2016-02-01 06:22:29 -05:00
go func(finish *sync.Mutex) {
2016-04-12 15:43:13 -04:00
var writer v2io.Writer = v2io.NewAdaptiveWriter(bodyWriter)
2016-02-27 11:28:21 -05:00
if request.Option.IsChunkStream() {
2016-02-01 06:22:29 -05:00
writer = vmessio.NewAuthChunkWriter(writer)
}
2016-04-18 12:44:10 -04:00
v2io.Pipe(output, writer)
2016-05-09 11:23:31 -04:00
output.Release()
2016-04-28 15:14:00 -04:00
if request.Option.IsChunkStream() {
writer.Write(alloc.NewSmallBuffer().Clear())
}
2016-04-12 15:43:13 -04:00
writer.Release()
2016-02-01 06:22:29 -05:00
finish.Unlock()
}(&writeFinish)
2015-09-23 08:14:53 -04:00
writeFinish.Lock()
}
2015-09-18 06:31:42 -04:00
2015-09-23 08:14:53 -04:00
readFinish.Lock()
2015-09-10 18:24:18 -04:00
}
func init() {
2016-01-25 11:29:26 -05:00
internal.MustRegisterInboundHandlerCreator("vmess",
func(space app.Space, rawConfig interface{}) (proxy.InboundHandler, error) {
2016-01-31 11:01:28 -05:00
if !space.HasApp(dispatcher.APP_ID) {
return nil, internal.ErrorBadConfiguration
}
config := rawConfig.(*Config)
2016-05-07 14:26:29 -04:00
allowedClients := protocol.NewTimedUserValidator(protocol.DefaultIDHash)
for _, user := range config.AllowedUsers {
2016-02-25 10:40:43 -05:00
allowedClients.Add(user)
}
2015-10-06 17:11:08 -04:00
2016-01-31 11:01:28 -05:00
handler := &VMessInboundHandler{
packetDispatcher: space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher),
clients: allowedClients,
features: config.Features,
2016-02-25 08:38:41 -05:00
usersByEmail: NewUserByEmail(config.AllowedUsers, config.Defaults),
2016-01-31 11:01:28 -05:00
}
if space.HasApp(proxyman.APP_ID_INBOUND_MANAGER) {
handler.inboundHandlerManager = space.GetApp(proxyman.APP_ID_INBOUND_MANAGER).(proxyman.InboundHandlerManager)
}
return handler, nil
})
2015-09-10 18:24:18 -04:00
}