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

296 lines
7.4 KiB
Go
Raw Normal View History

package inbound
2015-09-10 18:24:18 -04:00
import (
2016-05-30 18:21:41 -04:00
"io"
2015-09-23 08:14:53 -04:00
"sync"
2015-09-10 18:24:18 -04:00
2016-08-20 14:55:45 -04:00
"v2ray.com/core/app"
"v2ray.com/core/app/dispatcher"
"v2ray.com/core/app/proxyman"
"v2ray.com/core/common"
2016-12-09 05:35:27 -05:00
"v2ray.com/core/common/buf"
2016-12-09 07:17:34 -05:00
"v2ray.com/core/common/bufio"
2016-12-04 03:10:47 -05:00
"v2ray.com/core/common/errors"
2016-08-20 14:55:45 -04:00
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/common/protocol"
2016-12-15 05:51:09 -05:00
"v2ray.com/core/common/serial"
2016-12-29 16:17:12 -05:00
"v2ray.com/core/common/signal"
2016-08-20 14:55:45 -04:00
"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"
2016-12-28 17:42:32 -05:00
"v2ray.com/core/transport/ray"
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
2016-09-17 18:41:21 -04:00
defaultLevel uint32
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,
2016-09-24 17:11:58 -04:00
defaultAlterIDs: uint16(config.AlterId),
2016-02-25 08:38:41 -05:00
}
}
2016-11-27 15:39:09 -05:00
func (v *userByEmail) Get(email string) (*protocol.User, bool) {
2016-05-07 14:26:29 -04:00
var user *protocol.User
2016-02-25 08:38:41 -05:00
var found bool
2016-11-27 15:39:09 -05:00
v.RLock()
user, found = v.cache[email]
v.RUnlock()
2016-02-25 08:38:41 -05:00
if !found {
2016-11-27 15:39:09 -05:00
v.Lock()
user, found = v.cache[email]
2016-02-25 08:38:41 -05:00
if !found {
2016-10-12 12:43:55 -04:00
account := &vmess.Account{
2016-09-17 18:41:21 -04:00
Id: uuid.New().String(),
2016-11-27 15:39:09 -05:00
AlterId: uint32(v.defaultAlterIDs),
2016-09-17 18:41:21 -04:00
}
user = &protocol.User{
2016-11-27 15:39:09 -05:00
Level: v.defaultLevel,
2016-09-17 18:41:21 -04:00
Email: email,
2016-12-15 05:51:09 -05:00
Account: serial.ToTypedMessage(account),
2016-05-28 07:44:11 -04:00
}
2016-11-27 15:39:09 -05:00
v.cache[email] = user
2016-02-25 08:38:41 -05:00
}
2016-11-27 15:39:09 -05:00
v.Unlock()
2016-02-25 08:38:41 -05:00
}
return user, found
}
// Inbound connection handler that handles messages in VMess format.
2015-09-10 18:24:18 -04:00
type VMessInboundHandler struct {
sync.RWMutex
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
2016-06-14 16:54:08 -04:00
listener *internet.TCPHub
2016-06-01 16:45:12 -04:00
detours *DetourConfig
2016-06-04 08:25:13 -04:00
meta *proxy.InboundHandlerMeta
2015-09-10 18:24:18 -04:00
}
2016-11-27 15:39:09 -05:00
func (v *VMessInboundHandler) Port() v2net.Port {
return v.meta.Port
2015-09-10 18:24:18 -04:00
}
2016-11-27 15:39:09 -05:00
func (v *VMessInboundHandler) Close() {
2017-01-04 06:52:24 -05:00
v.Lock()
2016-11-27 15:39:09 -05:00
v.accepting = false
if v.listener != nil {
v.listener.Close()
v.listener = nil
v.clients.Release()
v.clients = nil
}
2017-01-04 06:52:24 -05:00
v.Unlock()
}
2016-11-27 15:39:09 -05:00
func (v *VMessInboundHandler) GetUser(email string) *protocol.User {
v.RLock()
defer v.RUnlock()
2016-11-27 15:39:09 -05:00
if !v.accepting {
return nil
}
2016-11-27 15:39:09 -05:00
user, existing := v.usersByEmail.Get(email)
2016-02-25 08:38:41 -05:00
if !existing {
2016-11-27 15:39:09 -05:00
v.clients.Add(user)
2016-02-25 08:38:41 -05:00
}
return user
2016-01-08 18:10:57 -05:00
}
2016-11-27 15:39:09 -05:00
func (v *VMessInboundHandler) Start() error {
if v.accepting {
2016-06-03 18:38:22 -04:00
return nil
2016-01-19 17:41:40 -05:00
}
2016-11-27 15:39:09 -05:00
tcpListener, err := internet.ListenTCP(v.meta.Address, v.meta.Port, v.HandleConnection, v.meta.StreamSettings)
2015-09-10 18:24:18 -04:00
if err != nil {
2016-11-27 15:39:09 -05:00
log.Error("VMess|Inbound: Unable to listen tcp ", v.meta.Address, ":", v.meta.Port, ": ", err)
return err
2015-09-10 18:24:18 -04:00
}
2016-11-27 15:39:09 -05:00
v.accepting = true
v.Lock()
v.listener = tcpListener
v.Unlock()
2015-09-10 18:24:18 -04:00
return nil
}
2016-12-29 16:17:12 -05:00
func transferRequest(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(bodyReader, output); err != nil {
return err
}
return nil
}
func transferResponse(session *encoding.ServerSession, request *protocol.RequestHeader, response *protocol.ResponseHeader, input ray.InputStream, output io.Writer) error {
2016-12-29 18:51:39 -05:00
defer input.ForceClose()
2016-12-29 16:17:12 -05:00
session.EncodeResponseHeader(response, output)
bodyWriter := session.EncodeResponseBody(request, output)
// Optimize for small response packet
2017-01-06 05:59:51 -05:00
data, err := input.Read()
if err != nil {
return err
}
2016-12-29 16:17:12 -05:00
2017-01-06 05:59:51 -05:00
if err := bodyWriter.Write(data); err != nil {
return err
}
data.Release()
2016-12-29 16:17:12 -05:00
2017-01-06 05:59:51 -05:00
if bufferedWriter, ok := output.(*bufio.BufferedWriter); ok {
if err := bufferedWriter.SetBuffered(false); err != nil {
2016-12-29 16:17:12 -05:00
return err
}
}
2017-01-06 05:59:51 -05:00
if err := buf.PipeUntilEOF(input, bodyWriter); err != nil {
return err
}
2016-12-29 16:17:12 -05:00
if request.Option.Has(protocol.RequestOptionChunkStream) {
if err := bodyWriter.Write(buf.NewLocal(8)); err != nil {
return err
}
}
return nil
}
2016-11-27 15:39:09 -05:00
func (v *VMessInboundHandler) HandleConnection(connection internet.Connection) {
2015-09-10 18:24:18 -04:00
defer connection.Close()
2015-09-14 12:19:17 -04:00
2016-11-27 15:39:09 -05:00
if !v.accepting {
return
}
2016-05-30 18:21:41 -04:00
connReader := v2net.NewTimeOutReader(8, connection)
2016-12-09 07:17:34 -05:00
reader := bufio.NewReader(connReader)
2016-11-27 15:39:09 -05:00
v.RLock()
if !v.accepting {
v.RUnlock()
return
}
2016-11-27 15:39:09 -05:00
session := encoding.NewServerSession(v.clients)
2016-02-27 11:28:21 -05:00
request, err := session.DecodeRequestHeader(reader)
2016-11-27 15:39:09 -05:00
v.RUnlock()
2015-09-10 18:24:18 -04:00
if err != nil {
2016-12-04 03:10:47 -05:00
if errors.Cause(err) != io.EOF {
2016-05-30 18:21:41 -04:00
log.Access(connection.RemoteAddr(), "", log.AccessRejected, err)
2016-12-29 18:32:20 -05:00
log.Info("VMess|Inbound: Invalid request from ", connection.RemoteAddr(), ": ", err)
2016-05-30 18:21:41 -04:00
}
2016-06-05 19:20:20 -04:00
connection.SetReusable(false)
return
2015-09-10 18:24:18 -04:00
}
2016-05-24 16:41:51 -04:00
log.Access(connection.RemoteAddr(), request.Destination(), log.AccessAccepted, "")
2016-12-29 18:32:20 -05:00
log.Info("VMess|Inbound: Received request for ", request.Destination())
2015-09-10 18:24:18 -04:00
2016-06-14 16:54:08 -04:00
connection.SetReusable(request.Option.Has(protocol.RequestOptionConnectionReuse))
2016-05-30 18:21:41 -04:00
2016-11-27 15:39:09 -05:00
ray := v.packetDispatcher.DispatchToOutbound(&proxy.SessionInfo{
2016-08-14 17:20:23 -04:00
Source: v2net.DestinationFromAddr(connection.RemoteAddr()),
2016-08-14 11:08:01 -04:00
Destination: request.Destination(),
User: request.User,
2016-11-27 15:39:09 -05:00
Inbound: v.meta,
2016-08-14 11:08:01 -04:00
})
input := ray.InboundInput()
output := ray.InboundOutput()
2016-05-07 03:53:15 -04:00
defer input.Close()
2016-12-29 18:51:39 -05:00
defer output.ForceClose()
2016-05-07 03:53:15 -04:00
2016-09-17 18:41:21 -04:00
userSettings := request.User.GetSettings()
2015-10-31 04:39:45 -04:00
connReader.SetTimeOut(userSettings.PayloadReadTimeout)
2016-12-27 15:41:44 -05:00
reader.SetBuffered(false)
2016-06-02 15:34:25 -04:00
2016-12-29 16:17:12 -05:00
requestDone := signal.ExecuteAsync(func() error {
return transferRequest(session, request, reader, input)
2016-12-28 17:42:32 -05:00
})
2016-12-09 07:17:34 -05:00
writer := bufio.NewWriter(connection)
2016-05-07 14:26:29 -04:00
response := &protocol.ResponseHeader{
2016-11-27 15:39:09 -05:00
Command: v.generateCommand(request),
2016-02-27 11:28:21 -05:00
}
2015-11-03 15:26:16 -05:00
2016-06-14 16:54:08 -04:00
if connection.Reusable() {
2016-06-02 15:34:25 -04:00
response.Option.Set(protocol.ResponseOptionConnectionReuse)
}
2016-12-29 16:17:12 -05:00
responseDone := signal.ExecuteAsync(func() error {
return transferResponse(session, request, response, output, writer)
2016-12-28 17:42:32 -05:00
})
2016-05-12 20:20:07 -04:00
2016-12-29 18:32:20 -05:00
if err := signal.ErrorOrFinish2(requestDone, responseDone); err != nil {
log.Info("VMess|Inbound: Connection ending with ", err)
2016-12-28 17:42:32 -05:00
connection.SetReusable(false)
2016-12-29 16:17:12 -05:00
return
2016-06-10 19:37:33 -04:00
}
2015-09-18 06:31:42 -04:00
2016-12-29 16:17:12 -05:00
if err := writer.Flush(); err != nil {
2016-12-29 18:32:20 -05:00
log.Info("VMess|Inbound: Failed to flush remain data: ", err)
2016-12-28 17:42:32 -05:00
connection.SetReusable(false)
2016-12-29 16:17:12 -05:00
return
2016-12-28 17:42:32 -05:00
}
2015-09-10 18:24:18 -04:00
}
2016-06-14 16:54:08 -04:00
type Factory struct{}
2016-11-27 15:39:09 -05:00
func (v *Factory) StreamCapability() v2net.NetworkList {
2016-10-02 17:43:58 -04:00
return v2net.NetworkList{
Network: []v2net.Network{v2net.Network_TCP, v2net.Network_KCP, v2net.Network_WebSocket},
}
}
2016-11-27 15:39:09 -05:00
func (v *Factory) Create(space app.Space, rawConfig interface{}, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) {
2016-06-14 16:54:08 -04:00
config := rawConfig.(*Config)
2015-10-06 17:11:08 -04:00
2016-07-25 11:36:24 -04:00
allowedClients := vmess.NewTimedUserValidator(protocol.DefaultIDHash)
2016-09-24 17:11:58 -04:00
for _, user := range config.User {
2016-06-14 16:54:08 -04:00
allowedClients.Add(user)
}
2016-01-31 11:01:28 -05:00
2016-06-14 16:54:08 -04:00
handler := &VMessInboundHandler{
2017-01-06 09:32:36 -05:00
clients: allowedClients,
detours: config.Detour,
usersByEmail: NewUserByEmail(config.User, config.GetDefaultValue()),
meta: meta,
2016-06-14 16:54:08 -04:00
}
2016-01-31 11:01:28 -05:00
2017-01-06 09:32:36 -05:00
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
})
2016-06-14 16:54:08 -04:00
return handler, nil
}
func init() {
2016-12-27 18:53:29 -05:00
common.Must(proxy.RegisterInboundHandlerCreator(serial.GetMessageType(new(Config)), new(Factory)))
2015-09-10 18:24:18 -04:00
}