You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/proxy/vmess/inbound/inbound.go

256 lines
6.8 KiB

package inbound
9 years ago
import (
"io"
"sync"
9 years ago
"github.com/v2ray/v2ray-core/app"
"github.com/v2ray/v2ray-core/app/dispatcher"
"github.com/v2ray/v2ray-core/app/proxyman"
"github.com/v2ray/v2ray-core/common/alloc"
v2io "github.com/v2ray/v2ray-core/common/io"
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/common/protocol"
"github.com/v2ray/v2ray-core/common/protocol/raw"
"github.com/v2ray/v2ray-core/common/uuid"
"github.com/v2ray/v2ray-core/proxy"
"github.com/v2ray/v2ray-core/proxy/internal"
vmessio "github.com/v2ray/v2ray-core/proxy/vmess/io"
"github.com/v2ray/v2ray-core/transport/internet"
9 years ago
)
type userByEmail struct {
sync.RWMutex
cache map[string]*protocol.User
defaultLevel protocol.UserLevel
defaultAlterIDs uint16
}
func NewUserByEmail(users []*protocol.User, config *DefaultConfig) *userByEmail {
cache := make(map[string]*protocol.User)
for _, user := range users {
cache[user.Email] = user
}
return &userByEmail{
cache: cache,
defaultLevel: config.Level,
defaultAlterIDs: config.AlterIDs,
}
}
func (this *userByEmail) Get(email string) (*protocol.User, bool) {
var user *protocol.User
var found bool
this.RLock()
user, found = this.cache[email]
this.RUnlock()
if !found {
this.Lock()
user, found = this.cache[email]
if !found {
id := protocol.NewID(uuid.New())
alterIDs := protocol.NewAlterIDs(id, this.defaultAlterIDs)
account := &protocol.VMessAccount{
ID: id,
AlterIDs: alterIDs,
}
user = protocol.NewUser(account, this.defaultLevel, email)
this.cache[email] = user
}
this.Unlock()
}
return user, found
}
// Inbound connection handler that handles messages in VMess format.
9 years ago
type VMessInboundHandler struct {
sync.Mutex
packetDispatcher dispatcher.PacketDispatcher
inboundHandlerManager proxyman.InboundHandlerManager
clients protocol.UserValidator
usersByEmail *userByEmail
accepting bool
listener *internet.TCPHub
detours *DetourConfig
meta *proxy.InboundHandlerMeta
9 years ago
}
func (this *VMessInboundHandler) Port() v2net.Port {
return this.meta.Port
9 years ago
}
func (this *VMessInboundHandler) Close() {
this.accepting = false
if this.listener != nil {
this.Lock()
this.listener.Close()
this.listener = nil
this.clients.Release()
this.clients = nil
this.Unlock()
}
}
func (this *VMessInboundHandler) GetUser(email string) *protocol.User {
user, existing := this.usersByEmail.Get(email)
if !existing {
this.clients.Add(user)
}
return user
}
func (this *VMessInboundHandler) Start() error {
if this.accepting {
return nil
}
tcpListener, err := internet.ListenTCP(this.meta.Address, this.meta.Port, this.HandleConnection, this.meta.StreamSettings)
9 years ago
if err != nil {
log.Error("Unable to listen tcp ", this.meta.Address, ":", this.meta.Port, ": ", err)
return err
9 years ago
}
9 years ago
this.accepting = true
this.Lock()
this.listener = tcpListener
this.Unlock()
9 years ago
return nil
}
func (this *VMessInboundHandler) HandleConnection(connection internet.Connection) {
9 years ago
defer connection.Close()
connReader := v2net.NewTimeOutReader(8, connection)
defer connReader.Release()
9 years ago
reader := v2io.NewBufferedReader(connReader)
defer reader.Release()
session := raw.NewServerSession(this.clients)
defer session.Release()
request, err := session.DecodeRequestHeader(reader)
9 years ago
if err != nil {
if err != io.EOF {
log.Access(connection.RemoteAddr(), "", log.AccessRejected, err)
log.Warning("VMessIn: Invalid request from ", connection.RemoteAddr(), ": ", err)
}
9 years ago
connection.SetReusable(false)
return
9 years ago
}
log.Access(connection.RemoteAddr(), request.Destination(), log.AccessAccepted, "")
log.Info("VMessIn: Received request for ", request.Destination())
9 years ago
connection.SetReusable(request.Option.Has(protocol.RequestOptionConnectionReuse))
ray := this.packetDispatcher.DispatchToOutbound(request.Destination())
input := ray.InboundInput()
output := ray.InboundOutput()
defer input.Close()
defer output.Release()
9 years ago
var readFinish sync.Mutex
readFinish.Lock()
9 years ago
userSettings := protocol.GetUserSettings(request.User.Level)
connReader.SetTimeOut(userSettings.PayloadReadTimeout)
reader.SetCached(false)
go func() {
bodyReader := session.DecodeRequestBody(reader)
var requestReader v2io.Reader
if request.Option.Has(protocol.RequestOptionChunkStream) {
requestReader = vmessio.NewAuthChunkReader(bodyReader)
} else {
requestReader = v2io.NewAdaptiveReader(bodyReader)
}
err := v2io.Pipe(requestReader, input)
if err != io.EOF {
connection.SetReusable(false)
}
9 years ago
requestReader.Release()
input.Close()
readFinish.Unlock()
}()
writer := v2io.NewBufferedWriter(connection)
defer writer.Release()
9 years ago
response := &protocol.ResponseHeader{
Command: this.generateCommand(request),
}
if connection.Reusable() {
response.Option.Set(protocol.ResponseOptionConnectionReuse)
}
session.EncodeResponseHeader(response, writer)
bodyWriter := session.EncodeResponseBody(writer)
var v2writer v2io.Writer = v2io.NewAdaptiveWriter(bodyWriter)
if request.Option.Has(protocol.RequestOptionChunkStream) {
v2writer = vmessio.NewAuthChunkWriter(v2writer)
}
9 years ago
// Optimize for small response packet
if data, err := output.Read(); err == nil {
9 years ago
if err := v2writer.Write(data); err != nil {
connection.SetReusable(false)
}
writer.SetCached(false)
9 years ago
err = v2io.Pipe(output, v2writer)
if err != io.EOF {
connection.SetReusable(false)
}
}
output.Release()
if request.Option.Has(protocol.RequestOptionChunkStream) {
if err := v2writer.Write(alloc.NewSmallBuffer().Clear()); err != nil {
connection.SetReusable(false)
9 years ago
}
}
v2writer.Release()
9 years ago
readFinish.Lock()
9 years ago
}
type Factory struct{}
func (this *Factory) StreamCapability() internet.StreamConnectionType {
9 years ago
return internet.StreamConnectionTypeRawTCP | internet.StreamConnectionTypeTCP | internet.StreamConnectionTypeKCP
}
func (this *Factory) Create(space app.Space, rawConfig interface{}, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) {
if !space.HasApp(dispatcher.APP_ID) {
return nil, internal.ErrorBadConfiguration
}
config := rawConfig.(*Config)
allowedClients := protocol.NewTimedUserValidator(protocol.DefaultIDHash)
for _, user := range config.AllowedUsers {
allowedClients.Add(user)
}
handler := &VMessInboundHandler{
packetDispatcher: space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher),
clients: allowedClients,
detours: config.DetourConfig,
usersByEmail: NewUserByEmail(config.AllowedUsers, config.Defaults),
meta: meta,
}
if space.HasApp(proxyman.APP_ID_INBOUND_MANAGER) {
handler.inboundHandlerManager = space.GetApp(proxyman.APP_ID_INBOUND_MANAGER).(proxyman.InboundHandlerManager)
}
return handler, nil
}
func init() {
internal.MustRegisterInboundHandlerCreator("vmess", new(Factory))
9 years ago
}