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

321 lines
7.5 KiB

package inbound
9 years ago
import (
"io"
"sync"
9 years ago
"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/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/common/protocol"
"v2ray.com/core/common/serial"
8 years ago
"v2ray.com/core/common/task"
"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
)
8 years ago
type requestProcessor struct {
session *encoding.ServerSession
request *protocol.RequestHeader
input io.Reader
output ray.OutputStream
}
func (r *requestProcessor) Execute() error {
defer r.output.Close()
bodyReader := r.session.DecodeRequestBody(r.request, r.input)
defer bodyReader.Release()
if err := buf.PipeUntilEOF(bodyReader, r.output); err != nil {
log.Debug("VMess|Inbound: Error when sending data to outbound: ", err)
return err
}
return nil
}
type responseProcessor struct {
session *encoding.ServerSession
request *protocol.RequestHeader
response *protocol.ResponseHeader
input ray.InputStream
output io.Writer
}
func (r *responseProcessor) Execute() error {
defer r.input.Release()
r.session.EncodeResponseHeader(r.response, r.output)
bodyWriter := r.session.EncodeResponseBody(r.request, r.output)
// Optimize for small response packet
if data, err := r.input.Read(); err == nil {
if err := bodyWriter.Write(data); err != nil {
return err
}
if bufferedWriter, ok := r.output.(*bufio.BufferedWriter); ok {
bufferedWriter.SetBuffered(false)
}
if err := buf.PipeUntilEOF(r.input, bodyWriter); err != nil {
log.Debug("VMess|Inbound: Error when sending data to downstream: ", err)
return err
}
}
if r.request.Option.Has(protocol.RequestOptionChunkStream) {
if err := bodyWriter.Write(buf.NewLocal(8)); err != nil {
return err
}
}
return nil
}
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.PacketDispatcher
inboundHandlerManager proxyman.InboundHandlerManager
clients protocol.UserValidator
usersByEmail *userByEmail
accepting bool
listener *internet.TCPHub
detours *DetourConfig
meta *proxy.InboundHandlerMeta
9 years ago
}
8 years ago
func (v *VMessInboundHandler) Port() v2net.Port {
return v.meta.Port
9 years ago
}
8 years ago
func (v *VMessInboundHandler) Close() {
v.accepting = false
if v.listener != nil {
v.Lock()
v.listener.Close()
v.listener = nil
v.clients.Release()
v.clients = nil
v.Unlock()
}
}
8 years ago
func (v *VMessInboundHandler) GetUser(email string) *protocol.User {
v.RLock()
defer v.RUnlock()
8 years ago
if !v.accepting {
return nil
}
8 years ago
user, existing := v.usersByEmail.Get(email)
if !existing {
8 years ago
v.clients.Add(user)
}
return user
}
8 years ago
func (v *VMessInboundHandler) Start() error {
if v.accepting {
return nil
}
8 years ago
tcpListener, err := internet.ListenTCP(v.meta.Address, v.meta.Port, v.HandleConnection, v.meta.StreamSettings)
9 years ago
if err != nil {
8 years ago
log.Error("VMess|Inbound: Unable to listen tcp ", v.meta.Address, ":", v.meta.Port, ": ", err)
return err
9 years ago
}
8 years ago
v.accepting = true
v.Lock()
v.listener = tcpListener
v.Unlock()
9 years ago
return nil
}
8 years ago
func (v *VMessInboundHandler) HandleConnection(connection internet.Connection) {
9 years ago
defer connection.Close()
8 years ago
if !v.accepting {
return
}
connReader := v2net.NewTimeOutReader(8, connection)
defer connReader.Release()
9 years ago
reader := bufio.NewReader(connReader)
defer reader.Release()
8 years ago
v.RLock()
if !v.accepting {
v.RUnlock()
return
}
8 years ago
session := encoding.NewServerSession(v.clients)
defer session.Release()
request, err := session.DecodeRequestHeader(reader)
8 years ago
v.RUnlock()
9 years ago
if err != nil {
if errors.Cause(err) != io.EOF {
log.Access(connection.RemoteAddr(), "", log.AccessRejected, err)
8 years ago
log.Info("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))
8 years ago
ray := v.packetDispatcher.DispatchToOutbound(&proxy.SessionInfo{
Source: v2net.DestinationFromAddr(connection.RemoteAddr()),
Destination: request.Destination(),
User: request.User,
8 years ago
Inbound: v.meta,
})
input := ray.InboundInput()
output := ray.InboundOutput()
defer input.Close()
defer output.Release()
userSettings := request.User.GetSettings()
connReader.SetTimeOut(userSettings.PayloadReadTimeout)
reader.SetBuffered(false)
8 years ago
var executor task.ParallelExecutor
executor.Execute(&requestProcessor{
session: session,
request: request,
input: reader,
output: input,
})
writer := bufio.NewWriter(connection)
defer writer.Release()
9 years ago
response := &protocol.ResponseHeader{
8 years ago
Command: v.generateCommand(request),
}
if connection.Reusable() {
response.Option.Set(protocol.ResponseOptionConnectionReuse)
}
8 years ago
executor.Execute(&responseProcessor{
session: session,
request: request,
response: response,
input: output,
output: writer,
})
9 years ago
8 years ago
executor.Wait()
8 years ago
if err := writer.Flush(); err != nil {
connection.SetReusable(false)
}
9 years ago
8 years ago
errors := executor.Errors()
if len(errors) > 0 {
connection.SetReusable(false)
}
9 years ago
}
type Factory struct{}
8 years ago
func (v *Factory) StreamCapability() v2net.NetworkList {
return v2net.NetworkList{
Network: []v2net.Network{v2net.Network_TCP, v2net.Network_KCP, v2net.Network_WebSocket},
}
}
8 years ago
func (v *Factory) Create(space app.Space, rawConfig interface{}, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) {
if !space.HasApp(dispatcher.APP_ID) {
return nil, common.ErrBadConfiguration
}
config := rawConfig.(*Config)
allowedClients := vmess.NewTimedUserValidator(protocol.DefaultIDHash)
for _, user := range config.User {
allowedClients.Add(user)
}
handler := &VMessInboundHandler{
packetDispatcher: space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher),
clients: allowedClients,
detours: config.Detour,
usersByEmail: NewUserByEmail(config.User, config.GetDefaultValue()),
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() {
8 years ago
common.Must(proxy.RegisterInboundHandlerCreator(serial.GetMessageType(new(Config)), new(Factory)))
9 years ago
}