2015-12-04 06:42:56 -05:00
|
|
|
package inbound
|
2015-09-10 18:24:18 -04:00
|
|
|
|
|
|
|
import (
|
2017-01-13 17:42:39 -05:00
|
|
|
"context"
|
2016-05-30 18:21:41 -04:00
|
|
|
"io"
|
2017-01-31 06:42:05 -05:00
|
|
|
"runtime"
|
2017-02-13 07:10:43 -05:00
|
|
|
"sync"
|
2017-01-31 06:42:05 -05:00
|
|
|
"time"
|
|
|
|
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/app"
|
|
|
|
"v2ray.com/core/app/dispatcher"
|
2017-02-03 16:35:09 -05:00
|
|
|
"v2ray.com/core/app/log"
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/app/proxyman"
|
|
|
|
"v2ray.com/core/common"
|
2016-12-09 05:35:27 -05:00
|
|
|
"v2ray.com/core/common/buf"
|
2016-12-04 03:10:47 -05:00
|
|
|
"v2ray.com/core/common/errors"
|
2017-01-13 17:42:39 -05:00
|
|
|
"v2ray.com/core/common/net"
|
2016-08-20 14:55:45 -04:00
|
|
|
"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/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
|
|
|
|
}
|
|
|
|
|
2017-02-13 07:16:37 -05:00
|
|
|
// Handler is an inbound connection handler that handles messages in VMess protocol.
|
|
|
|
type Handler struct {
|
2016-01-31 11:01:28 -05:00
|
|
|
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-06-01 16:45:12 -04:00
|
|
|
detours *DetourConfig
|
2017-02-12 10:53:23 -05:00
|
|
|
sessionHistory *encoding.SessionHistory
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|
|
|
|
|
2017-02-13 07:16:37 -05:00
|
|
|
func New(ctx context.Context, config *Config) (*Handler, error) {
|
2017-01-12 18:56:21 -05:00
|
|
|
space := app.SpaceFromContext(ctx)
|
|
|
|
if space == nil {
|
2017-04-07 16:56:27 -04:00
|
|
|
return nil, errors.New("no space in context").Path("Proxy", "VMess", "Inbound")
|
2017-01-12 18:56:21 -05:00
|
|
|
}
|
|
|
|
|
2017-01-29 06:58:52 -05:00
|
|
|
allowedClients := vmess.NewTimedUserValidator(ctx, protocol.DefaultIDHash)
|
2017-01-12 18:56:21 -05:00
|
|
|
for _, user := range config.User {
|
|
|
|
allowedClients.Add(user)
|
|
|
|
}
|
|
|
|
|
2017-02-13 07:16:37 -05:00
|
|
|
handler := &Handler{
|
2017-02-12 10:53:23 -05:00
|
|
|
clients: allowedClients,
|
|
|
|
detours: config.Detour,
|
|
|
|
usersByEmail: NewUserByEmail(config.User, config.GetDefaultValue()),
|
|
|
|
sessionHistory: encoding.NewSessionHistory(ctx),
|
2017-01-12 18:56:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
space.OnInitialize(func() error {
|
|
|
|
handler.inboundHandlerManager = proxyman.InboundHandlerManagerFromSpace(space)
|
|
|
|
if handler.inboundHandlerManager == nil {
|
2017-04-07 16:56:27 -04:00
|
|
|
return errors.New("InboundHandlerManager is not found is space").Path("Proxy", "VMess", "Inbound")
|
2017-01-12 18:56:21 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return handler, nil
|
|
|
|
}
|
|
|
|
|
2017-02-13 07:16:37 -05:00
|
|
|
// Network implements proxy.Inbound.Network().
|
|
|
|
func (*Handler) Network() net.NetworkList {
|
2017-01-14 18:57:06 -05:00
|
|
|
return net.NetworkList{
|
|
|
|
Network: []net.Network{net.Network_TCP},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-13 07:16:37 -05:00
|
|
|
func (v *Handler) GetUser(email string) *protocol.User {
|
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
|
|
|
}
|
|
|
|
|
2017-04-04 04:24:38 -04:00
|
|
|
func transferRequest(timer signal.ActivityTimer, session *encoding.ServerSession, request *protocol.RequestHeader, input io.Reader, output ray.OutputStream) error {
|
2016-12-29 16:17:12 -05:00
|
|
|
defer output.Close()
|
|
|
|
|
|
|
|
bodyReader := session.DecodeRequestBody(request, input)
|
2017-01-31 06:42:05 -05:00
|
|
|
if err := buf.PipeUntilEOF(timer, bodyReader, output); err != nil {
|
2016-12-29 16:17:12 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-04 04:24:38 -04:00
|
|
|
func transferResponse(timer signal.ActivityTimer, session *encoding.ServerSession, request *protocol.RequestHeader, response *protocol.ResponseHeader, input ray.InputStream, output io.Writer) error {
|
2016-12-29 16:17:12 -05:00
|
|
|
session.EncodeResponseHeader(response, output)
|
|
|
|
|
|
|
|
bodyWriter := session.EncodeResponseBody(request, output)
|
|
|
|
|
2017-04-07 15:54:40 -04:00
|
|
|
mergeReader := buf.NewMergingReader(input)
|
2016-12-29 16:17:12 -05:00
|
|
|
// Optimize for small response packet
|
2017-04-07 15:54:40 -04:00
|
|
|
data, err := mergeReader.Read()
|
2017-01-06 05:59:51 -05:00
|
|
|
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-02-15 16:51:01 -05:00
|
|
|
if bufferedWriter, ok := output.(*buf.BufferedWriter); ok {
|
2017-01-06 05:59:51 -05:00
|
|
|
if err := bufferedWriter.SetBuffered(false); err != nil {
|
2016-12-29 16:17:12 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-07 15:54:40 -04:00
|
|
|
if err := buf.PipeUntilEOF(timer, mergeReader, bodyWriter); err != nil {
|
2017-01-06 05:59:51 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-02-13 07:16:37 -05:00
|
|
|
// Process implements proxy.Inbound.Process().
|
|
|
|
func (v *Handler) Process(ctx context.Context, network net.Network, connection internet.Connection, dispatcher dispatcher.Interface) error {
|
2017-01-31 10:49:59 -05:00
|
|
|
connection.SetReadDeadline(time.Now().Add(time.Second * 8))
|
2017-02-15 16:51:01 -05:00
|
|
|
reader := buf.NewBufferedReader(connection)
|
2017-01-26 14:46:44 -05:00
|
|
|
|
2017-02-12 10:53:23 -05:00
|
|
|
session := encoding.NewServerSession(v.clients, v.sessionHistory)
|
2016-02-27 11:28:21 -05:00
|
|
|
request, err := session.DecodeRequestHeader(reader)
|
2016-06-18 11:01:48 -04:00
|
|
|
|
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)
|
2017-04-07 16:56:27 -04:00
|
|
|
log.Trace(errors.New("invalid request from ", connection.RemoteAddr(), ": ", err).Path("Proxy", "VMess", "Inbound"))
|
2016-05-30 18:21:41 -04:00
|
|
|
}
|
2017-01-26 14:46:44 -05:00
|
|
|
return err
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|
2016-05-24 16:41:51 -04:00
|
|
|
log.Access(connection.RemoteAddr(), request.Destination(), log.AccessAccepted, "")
|
2017-04-07 16:56:27 -04:00
|
|
|
log.Trace(errors.New("received request for ", request.Destination()).Path("Proxy", "VMess", "Inbound"))
|
2015-09-10 18:24:18 -04:00
|
|
|
|
2017-01-31 10:49:59 -05:00
|
|
|
connection.SetReadDeadline(time.Time{})
|
|
|
|
|
|
|
|
userSettings := request.User.GetSettings()
|
2016-05-30 18:21:41 -04:00
|
|
|
|
2017-01-26 14:46:44 -05:00
|
|
|
ctx = protocol.ContextWithUser(ctx, request.User)
|
2017-03-31 15:10:33 -04:00
|
|
|
|
2017-03-31 15:45:43 -04:00
|
|
|
ctx, timer := signal.CancelAfterInactivity(ctx, userSettings.PayloadTimeout)
|
2017-02-03 16:35:09 -05:00
|
|
|
ray, err := dispatcher.Dispatch(ctx, request.Destination())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-26 14:46:44 -05:00
|
|
|
|
2015-09-17 17:00:17 -04:00
|
|
|
input := ray.InboundInput()
|
|
|
|
output := ray.InboundOutput()
|
2016-05-07 03:53:15 -04:00
|
|
|
|
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 {
|
2017-01-31 06:42:05 -05:00
|
|
|
return transferRequest(timer, session, request, reader, input)
|
2016-12-28 17:42:32 -05:00
|
|
|
})
|
2015-09-17 17:00:17 -04:00
|
|
|
|
2017-02-15 16:51:01 -05:00
|
|
|
writer := buf.NewBufferedWriter(connection)
|
2016-05-07 14:26:29 -04:00
|
|
|
response := &protocol.ResponseHeader{
|
2017-01-26 14:46:44 -05:00
|
|
|
Command: v.generateCommand(ctx, request),
|
2016-02-27 11:28:21 -05:00
|
|
|
}
|
2015-11-03 15:26:16 -05:00
|
|
|
|
2016-12-29 16:17:12 -05:00
|
|
|
responseDone := signal.ExecuteAsync(func() error {
|
2017-01-31 06:42:05 -05:00
|
|
|
return transferResponse(timer, session, request, response, output, writer)
|
2016-12-28 17:42:32 -05:00
|
|
|
})
|
2016-05-12 20:20:07 -04:00
|
|
|
|
2017-01-28 15:24:46 -05:00
|
|
|
if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
|
2017-01-10 08:22:42 -05:00
|
|
|
input.CloseError()
|
|
|
|
output.CloseError()
|
2017-04-07 15:54:40 -04:00
|
|
|
return errors.New("error during processing").Base(err).Path("Proxy", "VMess", "Inbound")
|
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 {
|
2017-04-07 15:54:40 -04:00
|
|
|
return errors.New("error during flushing remaining data").Base(err).Path("Proxy", "VMess", "Inbound")
|
2016-12-28 17:42:32 -05:00
|
|
|
}
|
2017-01-26 14:46:44 -05:00
|
|
|
|
2017-01-31 06:42:05 -05:00
|
|
|
runtime.KeepAlive(timer)
|
|
|
|
|
2017-01-26 14:46:44 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-13 07:16:37 -05:00
|
|
|
func (v *Handler) generateCommand(ctx context.Context, request *protocol.RequestHeader) protocol.ResponseCommand {
|
2017-01-26 14:46:44 -05:00
|
|
|
if v.detours != nil {
|
|
|
|
tag := v.detours.To
|
|
|
|
if v.inboundHandlerManager != nil {
|
|
|
|
handler, err := v.inboundHandlerManager.GetHandler(ctx, tag)
|
|
|
|
if err != nil {
|
2017-04-07 16:56:27 -04:00
|
|
|
log.Trace(errors.New("failed to get detour handler: ", tag, err).AtWarning().Path("Proxy", "VMess", "Inbound"))
|
2017-01-26 14:46:44 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
proxyHandler, port, availableMin := handler.GetRandomInboundProxy()
|
2017-02-13 07:16:37 -05:00
|
|
|
inboundHandler, ok := proxyHandler.(*Handler)
|
2017-02-13 07:08:28 -05:00
|
|
|
if ok && inboundHandler != nil {
|
2017-01-26 14:46:44 -05:00
|
|
|
if availableMin > 255 {
|
|
|
|
availableMin = 255
|
|
|
|
}
|
|
|
|
|
2017-04-07 16:56:27 -04:00
|
|
|
log.Trace(errors.New("pick detour handler for port ", port, " for ", availableMin, " minutes.").Path("Proxy", "VMess", "Inbound").AtDebug())
|
2017-01-26 14:46:44 -05:00
|
|
|
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
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|
2016-06-14 16:54:08 -04:00
|
|
|
|
|
|
|
func init() {
|
2017-01-12 18:56:21 -05:00
|
|
|
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
|
|
|
return New(ctx, config.(*Config))
|
|
|
|
}))
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|