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"
|
|
|
|
"v2ray.com/core/common/alloc"
|
|
|
|
v2io "v2ray.com/core/common/io"
|
|
|
|
"v2ray.com/core/common/log"
|
|
|
|
v2net "v2ray.com/core/common/net"
|
|
|
|
"v2ray.com/core/common/protocol"
|
|
|
|
"v2ray.com/core/common/uuid"
|
|
|
|
"v2ray.com/core/proxy"
|
|
|
|
"v2ray.com/core/proxy/registry"
|
|
|
|
"v2ray.com/core/proxy/vmess"
|
|
|
|
"v2ray.com/core/proxy/vmess/encoding"
|
|
|
|
vmessio "v2ray.com/core/proxy/vmess/io"
|
|
|
|
"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
|
|
|
|
defaultLevel protocol.UserLevel
|
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,
|
|
|
|
defaultAlterIDs: config.AlterIDs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-07 18:26:29 +00:00
|
|
|
func (this *userByEmail) Get(email string) (*protocol.User, bool) {
|
|
|
|
var user *protocol.User
|
2016-02-25 13:38:41 +00:00
|
|
|
var found bool
|
|
|
|
this.RLock()
|
|
|
|
user, found = this.cache[email]
|
|
|
|
this.RUnlock()
|
|
|
|
if !found {
|
|
|
|
this.Lock()
|
|
|
|
user, found = this.cache[email]
|
|
|
|
if !found {
|
2016-05-07 18:26:29 +00:00
|
|
|
id := protocol.NewID(uuid.New())
|
2016-05-07 19:07:46 +00:00
|
|
|
alterIDs := protocol.NewAlterIDs(id, this.defaultAlterIDs)
|
2016-07-25 15:36:24 +00:00
|
|
|
account := &vmess.Account{
|
2016-05-28 11:44:11 +00:00
|
|
|
ID: id,
|
|
|
|
AlterIDs: alterIDs,
|
|
|
|
}
|
2016-07-25 15:36:24 +00:00
|
|
|
user = protocol.NewUser(this.defaultLevel, email)
|
|
|
|
user.Account = account
|
2016-02-25 13:38:41 +00:00
|
|
|
this.cache[email] = user
|
|
|
|
}
|
|
|
|
this.Unlock()
|
|
|
|
}
|
|
|
|
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-01-19 22:41:40 +00:00
|
|
|
func (this *VMessInboundHandler) Port() v2net.Port {
|
2016-06-04 12:25:13 +00:00
|
|
|
return this.meta.Port
|
2015-09-10 22:24:18 +00:00
|
|
|
}
|
|
|
|
|
2016-01-03 22:30:37 +00:00
|
|
|
func (this *VMessInboundHandler) Close() {
|
|
|
|
this.accepting = false
|
|
|
|
if this.listener != nil {
|
2016-01-22 16:56:19 +00:00
|
|
|
this.Lock()
|
2016-01-27 21:11:31 +00:00
|
|
|
this.listener.Close()
|
2016-01-03 23:33:25 +00:00
|
|
|
this.listener = nil
|
2016-06-03 22:38:22 +00:00
|
|
|
this.clients.Release()
|
|
|
|
this.clients = nil
|
2016-01-03 22:30:37 +00:00
|
|
|
this.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-07 18:26:29 +00:00
|
|
|
func (this *VMessInboundHandler) GetUser(email string) *protocol.User {
|
2016-06-18 15:01:48 +00:00
|
|
|
this.RLock()
|
|
|
|
defer this.RUnlock()
|
|
|
|
|
|
|
|
if !this.accepting {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-25 13:38:41 +00:00
|
|
|
user, existing := this.usersByEmail.Get(email)
|
|
|
|
if !existing {
|
2016-02-25 15:40:43 +00:00
|
|
|
this.clients.Add(user)
|
2016-02-25 13:38:41 +00:00
|
|
|
}
|
|
|
|
return user
|
2016-01-08 23:10:57 +00:00
|
|
|
}
|
|
|
|
|
2016-06-03 22:38:22 +00:00
|
|
|
func (this *VMessInboundHandler) Start() error {
|
2016-01-19 22:41:40 +00:00
|
|
|
if this.accepting {
|
2016-06-03 22:38:22 +00:00
|
|
|
return nil
|
2016-01-19 22:41:40 +00:00
|
|
|
}
|
|
|
|
|
2016-06-14 20:54:08 +00:00
|
|
|
tcpListener, err := internet.ListenTCP(this.meta.Address, this.meta.Port, this.HandleConnection, this.meta.StreamSettings)
|
2015-09-10 22:24:18 +00:00
|
|
|
if err != nil {
|
2016-06-19 00:02:38 +00:00
|
|
|
log.Error("VMess|Inbound: Unable to listen tcp ", this.meta.Address, ":", this.meta.Port, ": ", err)
|
2015-10-13 16:27:29 +00:00
|
|
|
return err
|
2015-09-10 22:24:18 +00:00
|
|
|
}
|
2015-11-27 20:57:15 +00:00
|
|
|
this.accepting = true
|
2016-01-04 07:40:24 +00:00
|
|
|
this.Lock()
|
2016-01-27 21:11:31 +00:00
|
|
|
this.listener = tcpListener
|
2016-01-04 07:40:24 +00:00
|
|
|
this.Unlock()
|
2015-09-10 22:24:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-06-14 20:54:08 +00:00
|
|
|
func (this *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-06-18 15:01:48 +00:00
|
|
|
if !this.accepting {
|
|
|
|
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-02-27 16:28:21 +00:00
|
|
|
reader := v2io.NewBufferedReader(connReader)
|
2016-03-11 22:51:58 +00:00
|
|
|
defer reader.Release()
|
|
|
|
|
2016-06-18 15:01:48 +00:00
|
|
|
this.RLock()
|
|
|
|
if !this.accepting {
|
|
|
|
this.RUnlock()
|
|
|
|
return
|
|
|
|
}
|
2016-07-23 11:17:51 +00:00
|
|
|
session := encoding.NewServerSession(this.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-06-18 15:01:48 +00:00
|
|
|
this.RUnlock()
|
|
|
|
|
2015-09-10 22:24:18 +00:00
|
|
|
if err != nil {
|
2016-05-30 22:21:41 +00:00
|
|
|
if err != io.EOF {
|
|
|
|
log.Access(connection.RemoteAddr(), "", log.AccessRejected, err)
|
|
|
|
log.Warning("VMessIn: Invalid request from ", connection.RemoteAddr(), ": ", err)
|
|
|
|
}
|
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-08-14 15:08:01 +00:00
|
|
|
ray := this.packetDispatcher.DispatchToOutbound(this.meta, &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(),
|
|
|
|
})
|
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-05-07 18:26:29 +00:00
|
|
|
userSettings := protocol.GetUserSettings(request.User.Level)
|
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() {
|
|
|
|
bodyReader := session.DecodeRequestBody(reader)
|
2016-04-12 19:43:13 +00:00
|
|
|
var requestReader v2io.Reader
|
2016-06-02 19:34:25 +00:00
|
|
|
if request.Option.Has(protocol.RequestOptionChunkStream) {
|
2016-02-27 16:28:21 +00:00
|
|
|
requestReader = vmessio.NewAuthChunkReader(bodyReader)
|
|
|
|
} else {
|
|
|
|
requestReader = v2io.NewAdaptiveReader(bodyReader)
|
|
|
|
}
|
2016-06-01 20:09:34 +00:00
|
|
|
err := v2io.Pipe(requestReader, input)
|
2016-06-02 00:20:53 +00:00
|
|
|
if err != io.EOF {
|
2016-06-01 20:09:34 +00:00
|
|
|
connection.SetReusable(false)
|
|
|
|
}
|
|
|
|
|
2016-03-13 10:34:09 +00:00
|
|
|
requestReader.Release()
|
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-02-27 16:28:21 +00:00
|
|
|
writer := v2io.NewBufferedWriter(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-02-27 16:28:21 +00:00
|
|
|
Command: this.generateCommand(request),
|
|
|
|
}
|
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)
|
|
|
|
|
|
|
|
bodyWriter := session.EncodeResponseBody(writer)
|
2016-06-10 23:37:33 +00:00
|
|
|
var v2writer v2io.Writer = v2io.NewAdaptiveWriter(bodyWriter)
|
|
|
|
if request.Option.Has(protocol.RequestOptionChunkStream) {
|
|
|
|
v2writer = vmessio.NewAuthChunkWriter(v2writer)
|
|
|
|
}
|
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-06-05 23:20:20 +00:00
|
|
|
if err := v2writer.Write(data); err != nil {
|
|
|
|
connection.SetReusable(false)
|
|
|
|
}
|
2016-02-27 16:28:21 +00:00
|
|
|
|
|
|
|
writer.SetCached(false)
|
2016-05-13 00:20:07 +00:00
|
|
|
|
2016-06-01 20:09:34 +00:00
|
|
|
err = v2io.Pipe(output, v2writer)
|
2016-06-02 00:20:53 +00:00
|
|
|
if err != io.EOF {
|
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-07-24 07:59:52 +00:00
|
|
|
if err := v2writer.Write(alloc.NewLocalBuffer(32).Clear()); 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-06-10 23:37:33 +00:00
|
|
|
v2writer.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{}
|
|
|
|
|
|
|
|
func (this *Factory) StreamCapability() internet.StreamConnectionType {
|
2016-08-13 13:33:34 +00:00
|
|
|
return internet.StreamConnectionTypeRawTCP | internet.StreamConnectionTypeTCP | internet.StreamConnectionTypeKCP | internet.StreamConnectionTypeWebSocket
|
2016-06-12 05:52:29 +00:00
|
|
|
}
|
2015-09-11 15:27:36 +00:00
|
|
|
|
2016-06-14 20:54:08 +00:00
|
|
|
func (this *Factory) Create(space app.Space, rawConfig interface{}, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) {
|
|
|
|
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-06-14 20:54:08 +00:00
|
|
|
for _, user := range config.AllowedUsers {
|
|
|
|
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,
|
|
|
|
detours: config.DetourConfig,
|
|
|
|
usersByEmail: NewUserByEmail(config.AllowedUsers, config.Defaults),
|
|
|
|
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-08-17 21:30:15 +00:00
|
|
|
registry.MustRegisterInboundHandlerCreator("vmess", new(Factory))
|
2015-09-10 22:24:18 +00:00
|
|
|
}
|