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

291 lines
7.8 KiB

package inbound
9 years ago
import (
8 years ago
"context"
"io"
"sync"
9 years ago
"runtime"
"time"
"v2ray.com/core/app"
"v2ray.com/core/app/dispatcher"
"v2ray.com/core/app/proxyman"
"v2ray.com/core/common"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/bufio"
"v2ray.com/core/common/errors"
"v2ray.com/core/app/log"
8 years ago
"v2ray.com/core/common/net"
"v2ray.com/core/common/protocol"
"v2ray.com/core/common/serial"
"v2ray.com/core/common/signal"
"v2ray.com/core/common/uuid"
"v2ray.com/core/proxy"
"v2ray.com/core/proxy/vmess"
"v2ray.com/core/proxy/vmess/encoding"
"v2ray.com/core/transport/internet"
8 years ago
"v2ray.com/core/transport/ray"
9 years ago
)
type userByEmail struct {
sync.RWMutex
cache map[string]*protocol.User
defaultLevel uint32
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: uint16(config.AlterId),
}
}
8 years ago
func (v *userByEmail) Get(email string) (*protocol.User, bool) {
var user *protocol.User
var found bool
8 years ago
v.RLock()
user, found = v.cache[email]
v.RUnlock()
if !found {
8 years ago
v.Lock()
user, found = v.cache[email]
if !found {
8 years ago
account := &vmess.Account{
Id: uuid.New().String(),
8 years ago
AlterId: uint32(v.defaultAlterIDs),
}
user = &protocol.User{
8 years ago
Level: v.defaultLevel,
Email: email,
Account: serial.ToTypedMessage(account),
}
8 years ago
v.cache[email] = user
}
8 years ago
v.Unlock()
}
return user, found
}
// Inbound connection handler that handles messages in VMess format.
9 years ago
type VMessInboundHandler struct {
sync.RWMutex
packetDispatcher dispatcher.Interface
inboundHandlerManager proxyman.InboundHandlerManager
clients protocol.UserValidator
usersByEmail *userByEmail
detours *DetourConfig
9 years ago
}
func New(ctx context.Context, config *Config) (*VMessInboundHandler, error) {
space := app.SpaceFromContext(ctx)
if space == nil {
return nil, errors.New("VMess|Inbound: No space in context.")
}
allowedClients := vmess.NewTimedUserValidator(ctx, protocol.DefaultIDHash)
for _, user := range config.User {
allowedClients.Add(user)
}
handler := &VMessInboundHandler{
clients: allowedClients,
detours: config.Detour,
usersByEmail: NewUserByEmail(config.User, config.GetDefaultValue()),
}
space.OnInitialize(func() error {
handler.packetDispatcher = dispatcher.FromSpace(space)
if handler.packetDispatcher == nil {
return errors.New("VMess|Inbound: Dispatcher is not found in space.")
}
handler.inboundHandlerManager = proxyman.InboundHandlerManagerFromSpace(space)
if handler.inboundHandlerManager == nil {
return errors.New("VMess|Inbound: InboundHandlerManager is not found is space.")
}
return nil
})
return handler, nil
}
func (*VMessInboundHandler) Network() net.NetworkList {
return net.NetworkList{
Network: []net.Network{net.Network_TCP},
}
}
8 years ago
func (v *VMessInboundHandler) GetUser(email string) *protocol.User {
v.RLock()
defer v.RUnlock()
8 years ago
user, existing := v.usersByEmail.Get(email)
if !existing {
8 years ago
v.clients.Add(user)
}
return user
}
func transferRequest(timer *signal.ActivityTimer, session *encoding.ServerSession, request *protocol.RequestHeader, input io.Reader, output ray.OutputStream) error {
defer output.Close()
bodyReader := session.DecodeRequestBody(request, input)
if err := buf.PipeUntilEOF(timer, bodyReader, output); err != nil {
return err
}
return nil
}
func transferResponse(timer *signal.ActivityTimer, session *encoding.ServerSession, request *protocol.RequestHeader, response *protocol.ResponseHeader, input ray.InputStream, output io.Writer) error {
session.EncodeResponseHeader(response, output)
bodyWriter := session.EncodeResponseBody(request, output)
// Optimize for small response packet
data, err := input.Read()
if err != nil {
return err
}
if err := bodyWriter.Write(data); err != nil {
return err
}
data.Release()
if bufferedWriter, ok := output.(*bufio.BufferedWriter); ok {
if err := bufferedWriter.SetBuffered(false); err != nil {
return err
}
}
if err := buf.PipeUntilEOF(timer, input, bodyWriter); err != nil {
return err
}
if request.Option.Has(protocol.RequestOptionChunkStream) {
if err := bodyWriter.Write(buf.NewLocal(8)); err != nil {
return err
}
}
return nil
}
func (v *VMessInboundHandler) Process(ctx context.Context, network net.Network, connection internet.Connection) error {
connection.SetReadDeadline(time.Now().Add(time.Second * 8))
reader := bufio.NewReader(connection)
8 years ago
session := encoding.NewServerSession(v.clients)
request, err := session.DecodeRequestHeader(reader)
9 years ago
if err != nil {
if errors.Cause(err) != io.EOF {
log.Access(connection.RemoteAddr(), "", log.AccessRejected, err)
log.Info("VMess|Inbound: Invalid request from ", connection.RemoteAddr(), ": ", err)
}
9 years ago
connection.SetReusable(false)
return err
9 years ago
}
log.Access(connection.RemoteAddr(), request.Destination(), log.AccessAccepted, "")
log.Info("VMess|Inbound: Received request for ", request.Destination())
9 years ago
connection.SetReadDeadline(time.Time{})
connection.SetReusable(request.Option.Has(protocol.RequestOptionConnectionReuse))
userSettings := request.User.GetSettings()
ctx = proxy.ContextWithDestination(ctx, request.Destination())
ctx = protocol.ContextWithUser(ctx, request.User)
ctx, cancel := context.WithCancel(ctx)
timer := signal.CancelAfterInactivity(ctx, cancel, userSettings.PayloadTimeout)
ray := v.packetDispatcher.DispatchToOutbound(ctx)
input := ray.InboundInput()
output := ray.InboundOutput()
reader.SetBuffered(false)
requestDone := signal.ExecuteAsync(func() error {
return transferRequest(timer, session, request, reader, input)
8 years ago
})
writer := bufio.NewWriter(connection)
response := &protocol.ResponseHeader{
Command: v.generateCommand(ctx, request),
}
if connection.Reusable() {
response.Option.Set(protocol.ResponseOptionConnectionReuse)
}
responseDone := signal.ExecuteAsync(func() error {
return transferResponse(timer, session, request, response, output, writer)
8 years ago
})
9 years ago
if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
log.Info("VMess|Inbound: Connection ending with ", err)
8 years ago
connection.SetReusable(false)
input.CloseError()
output.CloseError()
return err
}
9 years ago
if err := writer.Flush(); err != nil {
log.Info("VMess|Inbound: Failed to flush remain data: ", err)
8 years ago
connection.SetReusable(false)
return err
8 years ago
}
runtime.KeepAlive(timer)
return nil
}
func (v *VMessInboundHandler) generateCommand(ctx context.Context, request *protocol.RequestHeader) protocol.ResponseCommand {
if v.detours != nil {
tag := v.detours.To
if v.inboundHandlerManager != nil {
handler, err := v.inboundHandlerManager.GetHandler(ctx, tag)
if err != nil {
log.Warning("VMess|Inbound: Failed to get detour handler: ", tag, err)
return nil
}
proxyHandler, port, availableMin := handler.GetRandomInboundProxy()
inboundHandler, ok := proxyHandler.(*VMessInboundHandler)
if ok {
if availableMin > 255 {
availableMin = 255
}
log.Info("VMessIn: Pick detour handler for port ", port, " for ", availableMin, " minutes.")
user := inboundHandler.GetUser(request.User.Email)
if user == nil {
return nil
}
account, _ := user.GetTypedAccount()
return &protocol.CommandSwitchAccount{
Port: port,
ID: account.(*vmess.InternalAccount).ID.UUID(),
AlterIds: uint16(len(account.(*vmess.InternalAccount).AlterIDs)),
Level: user.Level,
ValidMin: byte(availableMin),
}
}
}
}
return nil
9 years ago
}
func init() {
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
return New(ctx, config.(*Config))
}))
9 years ago
}