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