2015-12-04 11:42:56 +00:00
|
|
|
package inbound
|
2015-09-10 22:24:18 +00:00
|
|
|
|
2017-12-03 00:04:57 +00:00
|
|
|
//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg inbound -path Proxy,VMess,Inbound
|
2017-04-08 23:43:25 +00:00
|
|
|
|
2015-09-10 22:24:18 +00:00
|
|
|
import (
|
2017-01-13 22:42:39 +00:00
|
|
|
"context"
|
2016-05-30 22:21:41 +00:00
|
|
|
"io"
|
2017-02-13 12:10:43 +00:00
|
|
|
"sync"
|
2017-01-31 11:42:05 +00:00
|
|
|
"time"
|
|
|
|
|
2016-08-20 18:55:45 +00:00
|
|
|
"v2ray.com/core/app"
|
|
|
|
"v2ray.com/core/app/dispatcher"
|
2017-02-03 21:35:09 +00:00
|
|
|
"v2ray.com/core/app/log"
|
2017-11-27 21:09:30 +00:00
|
|
|
"v2ray.com/core/app/policy"
|
2016-08-20 18:55:45 +00:00
|
|
|
"v2ray.com/core/app/proxyman"
|
|
|
|
"v2ray.com/core/common"
|
2016-12-09 10:35:27 +00:00
|
|
|
"v2ray.com/core/common/buf"
|
2016-12-04 08:10:47 +00:00
|
|
|
"v2ray.com/core/common/errors"
|
2017-01-13 22:42:39 +00:00
|
|
|
"v2ray.com/core/common/net"
|
2016-08-20 18:55:45 +00:00
|
|
|
"v2ray.com/core/common/protocol"
|
2016-12-15 10:51:09 +00:00
|
|
|
"v2ray.com/core/common/serial"
|
2016-12-29 21:17:12 +00:00
|
|
|
"v2ray.com/core/common/signal"
|
2016-08-20 18:55:45 +00: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 22:42:32 +00:00
|
|
|
"v2ray.com/core/transport/ray"
|
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
|
|
|
|
}
|
|
|
|
|
2017-11-25 14:20:59 +00:00
|
|
|
func newUserByEmail(users []*protocol.User, config *DefaultConfig) *userByEmail {
|
2016-05-07 18:26:29 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-02-13 12:16:37 +00:00
|
|
|
// Handler is an inbound connection handler that handles messages in VMess protocol.
|
|
|
|
type Handler struct {
|
2016-01-31 16:01:28 +00:00
|
|
|
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-06-01 20:45:12 +00:00
|
|
|
detours *DetourConfig
|
2017-02-12 15:53:23 +00:00
|
|
|
sessionHistory *encoding.SessionHistory
|
2017-11-27 21:18:39 +00:00
|
|
|
policyManager policy.Manager
|
2015-09-10 22:24:18 +00:00
|
|
|
}
|
|
|
|
|
2017-11-25 14:20:59 +00:00
|
|
|
// New creates a new VMess inbound handler.
|
2017-02-13 12:16:37 +00:00
|
|
|
func New(ctx context.Context, config *Config) (*Handler, error) {
|
2017-01-12 23:56:21 +00:00
|
|
|
space := app.SpaceFromContext(ctx)
|
|
|
|
if space == nil {
|
2017-04-08 23:43:25 +00:00
|
|
|
return nil, newError("no space in context")
|
2017-01-12 23:56:21 +00:00
|
|
|
}
|
|
|
|
|
2017-01-29 11:58:52 +00:00
|
|
|
allowedClients := vmess.NewTimedUserValidator(ctx, protocol.DefaultIDHash)
|
2017-01-12 23:56:21 +00:00
|
|
|
for _, user := range config.User {
|
2017-05-23 23:35:05 +00:00
|
|
|
if err := allowedClients.Add(user); err != nil {
|
|
|
|
return nil, newError("failed to initiate user").Base(err)
|
|
|
|
}
|
2017-01-12 23:56:21 +00:00
|
|
|
}
|
|
|
|
|
2017-02-13 12:16:37 +00:00
|
|
|
handler := &Handler{
|
2017-02-12 15:53:23 +00:00
|
|
|
clients: allowedClients,
|
|
|
|
detours: config.Detour,
|
2017-11-25 14:20:59 +00:00
|
|
|
usersByEmail: newUserByEmail(config.User, config.GetDefaultValue()),
|
2017-02-12 15:53:23 +00:00
|
|
|
sessionHistory: encoding.NewSessionHistory(ctx),
|
2017-01-12 23:56:21 +00:00
|
|
|
}
|
|
|
|
|
2017-11-28 22:41:20 +00:00
|
|
|
space.On(app.SpaceInitializing, func(interface{}) error {
|
2017-01-12 23:56:21 +00:00
|
|
|
handler.inboundHandlerManager = proxyman.InboundHandlerManagerFromSpace(space)
|
|
|
|
if handler.inboundHandlerManager == nil {
|
2017-11-27 21:09:30 +00:00
|
|
|
return newError("InboundHandlerManager is not found is space.")
|
|
|
|
}
|
2017-11-27 21:18:39 +00:00
|
|
|
handler.policyManager = policy.FromSpace(space)
|
2017-11-27 21:09:30 +00:00
|
|
|
if handler.policyManager == nil {
|
|
|
|
return newError("Policy is not found in space.")
|
2017-01-12 23:56:21 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return handler, nil
|
|
|
|
}
|
|
|
|
|
2017-02-13 12:16:37 +00:00
|
|
|
// Network implements proxy.Inbound.Network().
|
|
|
|
func (*Handler) Network() net.NetworkList {
|
2017-01-14 23:57:06 +00:00
|
|
|
return net.NetworkList{
|
|
|
|
Network: []net.Network{net.Network_TCP},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-25 14:20:59 +00:00
|
|
|
func (h *Handler) GetUser(email string) *protocol.User {
|
|
|
|
user, existing := h.usersByEmail.Get(email)
|
2016-02-25 13:38:41 +00:00
|
|
|
if !existing {
|
2017-11-25 14:20:59 +00:00
|
|
|
h.clients.Add(user)
|
2016-02-25 13:38:41 +00:00
|
|
|
}
|
|
|
|
return user
|
2016-01-08 23:10:57 +00:00
|
|
|
}
|
|
|
|
|
2017-09-27 13:29:00 +00:00
|
|
|
func transferRequest(timer signal.ActivityUpdater, session *encoding.ServerSession, request *protocol.RequestHeader, input io.Reader, output ray.OutputStream) error {
|
2016-12-29 21:17:12 +00:00
|
|
|
defer output.Close()
|
|
|
|
|
|
|
|
bodyReader := session.DecodeRequestBody(request, input)
|
2017-04-27 20:30:48 +00:00
|
|
|
if err := buf.Copy(bodyReader, output, buf.UpdateActivity(timer)); err != nil {
|
2017-09-19 21:27:49 +00:00
|
|
|
return newError("failed to transfer request").Base(err)
|
2016-12-29 21:17:12 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-27 13:29:00 +00:00
|
|
|
func transferResponse(timer signal.ActivityUpdater, session *encoding.ServerSession, request *protocol.RequestHeader, response *protocol.ResponseHeader, input buf.Reader, output io.Writer) error {
|
2016-12-29 21:17:12 +00:00
|
|
|
session.EncodeResponseHeader(response, output)
|
|
|
|
|
|
|
|
bodyWriter := session.EncodeResponseBody(request, output)
|
|
|
|
|
|
|
|
// Optimize for small response packet
|
2017-11-09 21:33:15 +00:00
|
|
|
data, err := input.ReadMultiBuffer()
|
2017-01-06 10:59:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-29 21:17:12 +00:00
|
|
|
|
2017-11-09 21:33:15 +00:00
|
|
|
if err := bodyWriter.WriteMultiBuffer(data); err != nil {
|
2017-01-06 10:59:51 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
data.Release()
|
2016-12-29 21:17:12 +00:00
|
|
|
|
2017-02-15 21:51:01 +00:00
|
|
|
if bufferedWriter, ok := output.(*buf.BufferedWriter); ok {
|
2017-01-06 10:59:51 +00:00
|
|
|
if err := bufferedWriter.SetBuffered(false); err != nil {
|
2016-12-29 21:17:12 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-27 20:30:48 +00:00
|
|
|
if err := buf.Copy(input, bodyWriter, buf.UpdateActivity(timer)); err != nil {
|
2017-01-06 10:59:51 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-29 21:17:12 +00:00
|
|
|
if request.Option.Has(protocol.RequestOptionChunkStream) {
|
2017-11-09 21:33:15 +00:00
|
|
|
if err := bodyWriter.WriteMultiBuffer(buf.MultiBuffer{}); err != nil {
|
2016-12-29 21:17:12 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-13 12:16:37 +00:00
|
|
|
// Process implements proxy.Inbound.Process().
|
2017-11-25 14:20:59 +00:00
|
|
|
func (h *Handler) Process(ctx context.Context, network net.Network, connection internet.Connection, dispatcher dispatcher.Interface) error {
|
2017-11-27 21:09:30 +00:00
|
|
|
sessionPolicy := h.policyManager.GetPolicy(0)
|
|
|
|
if err := connection.SetReadDeadline(time.Now().Add(sessionPolicy.Timeout.Handshake.Duration())); err != nil {
|
2017-11-15 00:14:02 +00:00
|
|
|
return newError("unable to set read deadline").Base(err).AtWarning()
|
2017-05-30 15:33:41 +00:00
|
|
|
}
|
2017-05-23 23:35:05 +00:00
|
|
|
|
2017-11-09 21:33:15 +00:00
|
|
|
reader := buf.NewBufferedReader(buf.NewReader(connection))
|
2017-01-26 19:46:44 +00:00
|
|
|
|
2017-11-25 14:20:59 +00:00
|
|
|
session := encoding.NewServerSession(h.clients, h.sessionHistory)
|
2016-02-27 16:28:21 +00:00
|
|
|
request, err := session.DecodeRequestHeader(reader)
|
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)
|
2017-05-23 22:54:30 +00:00
|
|
|
log.Trace(newError("invalid request from ", connection.RemoteAddr(), ": ", err).AtInfo())
|
2016-05-30 22:21:41 +00:00
|
|
|
}
|
2017-01-26 19:46:44 +00:00
|
|
|
return err
|
2015-09-10 22:24:18 +00:00
|
|
|
}
|
2017-05-17 19:13:01 +00:00
|
|
|
|
|
|
|
if request.Command == protocol.RequestCommandMux {
|
|
|
|
request.Address = net.DomainAddress("v1.mux.com")
|
|
|
|
request.Port = net.Port(0)
|
|
|
|
}
|
|
|
|
|
2016-05-24 20:41:51 +00:00
|
|
|
log.Access(connection.RemoteAddr(), request.Destination(), log.AccessAccepted, "")
|
2017-04-08 23:43:25 +00:00
|
|
|
log.Trace(newError("received request for ", request.Destination()))
|
2015-09-10 22:24:18 +00:00
|
|
|
|
2017-11-15 00:14:02 +00:00
|
|
|
if err := connection.SetReadDeadline(time.Time{}); err != nil {
|
|
|
|
log.Trace(newError("unable to set back read deadline").Base(err))
|
|
|
|
}
|
2017-01-31 15:49:59 +00:00
|
|
|
|
2017-11-27 21:09:30 +00:00
|
|
|
sessionPolicy = h.policyManager.GetPolicy(request.User.Level)
|
2017-01-26 19:46:44 +00:00
|
|
|
ctx = protocol.ContextWithUser(ctx, request.User)
|
2017-03-31 19:10:33 +00:00
|
|
|
|
2017-11-14 23:36:14 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2017-11-27 21:09:30 +00:00
|
|
|
timer := signal.CancelAfterInactivity(ctx, cancel, sessionPolicy.Timeout.ConnectionIdle.Duration())
|
2017-02-03 21:35:09 +00:00
|
|
|
ray, err := dispatcher.Dispatch(ctx, request.Destination())
|
|
|
|
if err != nil {
|
2017-04-09 07:49:19 +00:00
|
|
|
return newError("failed to dispatch request to ", request.Destination()).Base(err)
|
2017-02-03 21:35:09 +00:00
|
|
|
}
|
2017-01-26 19:46:44 +00:00
|
|
|
|
2015-09-17 21:00:17 +00:00
|
|
|
input := ray.InboundInput()
|
|
|
|
output := ray.InboundOutput()
|
2016-05-07 07:53:15 +00:00
|
|
|
|
2016-12-29 21:17:12 +00:00
|
|
|
requestDone := signal.ExecuteAsync(func() error {
|
2017-11-27 21:09:30 +00:00
|
|
|
defer timer.SetTimeout(sessionPolicy.Timeout.DownlinkOnly.Duration())
|
2017-01-31 11:42:05 +00:00
|
|
|
return transferRequest(timer, session, request, reader, input)
|
2016-12-28 22:42:32 +00:00
|
|
|
})
|
2015-09-17 21:00:17 +00:00
|
|
|
|
2016-12-29 21:17:12 +00:00
|
|
|
responseDone := signal.ExecuteAsync(func() error {
|
2017-11-09 21:33:15 +00:00
|
|
|
writer := buf.NewBufferedWriter(buf.NewWriter(connection))
|
2017-05-23 23:35:05 +00:00
|
|
|
defer writer.Flush()
|
2017-11-27 21:09:30 +00:00
|
|
|
defer timer.SetTimeout(sessionPolicy.Timeout.UplinkOnly.Duration())
|
2017-05-23 23:35:05 +00:00
|
|
|
|
|
|
|
response := &protocol.ResponseHeader{
|
2017-11-25 14:20:59 +00:00
|
|
|
Command: h.generateCommand(ctx, request),
|
2017-05-23 23:35:05 +00:00
|
|
|
}
|
2017-01-31 11:42:05 +00:00
|
|
|
return transferResponse(timer, session, request, response, output, writer)
|
2016-12-28 22:42:32 +00:00
|
|
|
})
|
2016-05-13 00:20:07 +00:00
|
|
|
|
2017-01-28 20:24:46 +00:00
|
|
|
if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
|
2017-01-10 13:22:42 +00:00
|
|
|
input.CloseError()
|
|
|
|
output.CloseError()
|
2017-04-09 07:49:19 +00:00
|
|
|
return newError("connection ends").Base(err)
|
2016-06-10 23:37:33 +00:00
|
|
|
}
|
2015-09-18 10:31:42 +00:00
|
|
|
|
2017-01-26 19:46:44 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-25 14:20:59 +00:00
|
|
|
func (h *Handler) generateCommand(ctx context.Context, request *protocol.RequestHeader) protocol.ResponseCommand {
|
|
|
|
if h.detours != nil {
|
|
|
|
tag := h.detours.To
|
|
|
|
if h.inboundHandlerManager != nil {
|
|
|
|
handler, err := h.inboundHandlerManager.GetHandler(ctx, tag)
|
2017-01-26 19:46:44 +00:00
|
|
|
if err != nil {
|
2017-04-08 23:43:25 +00:00
|
|
|
log.Trace(newError("failed to get detour handler: ", tag, err).AtWarning())
|
2017-01-26 19:46:44 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
proxyHandler, port, availableMin := handler.GetRandomInboundProxy()
|
2017-02-13 12:16:37 +00:00
|
|
|
inboundHandler, ok := proxyHandler.(*Handler)
|
2017-02-13 12:08:28 +00:00
|
|
|
if ok && inboundHandler != nil {
|
2017-01-26 19:46:44 +00:00
|
|
|
if availableMin > 255 {
|
|
|
|
availableMin = 255
|
|
|
|
}
|
|
|
|
|
2017-04-08 23:43:25 +00:00
|
|
|
log.Trace(newError("pick detour handler for port ", port, " for ", availableMin, " minutes.").AtDebug())
|
2017-01-26 19:46:44 +00: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 22:24:18 +00:00
|
|
|
}
|
2016-06-14 20:54:08 +00:00
|
|
|
|
|
|
|
func init() {
|
2017-01-12 23:56:21 +00:00
|
|
|
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
|
|
|
return New(ctx, config.(*Config))
|
|
|
|
}))
|
2015-09-10 22:24:18 +00:00
|
|
|
}
|