pull/787/head
Darien Raymond 2017-11-25 15:20:59 +01:00
parent 6de4ef014a
commit 25dd739285
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
2 changed files with 17 additions and 17 deletions

View File

@ -1,12 +1,12 @@
package inbound package inbound
// GetDefaultValue returns default settings of DefaultConfig. // GetDefaultValue returns default settings of DefaultConfig.
func (v *Config) GetDefaultValue() *DefaultConfig { func (c *Config) GetDefaultValue() *DefaultConfig {
if v.GetDefault() == nil { if c.GetDefault() == nil {
return &DefaultConfig{ return &DefaultConfig{
AlterId: 32, AlterId: 32,
Level: 0, Level: 0,
} }
} }
return v.Default return c.Default
} }

View File

@ -33,7 +33,7 @@ type userByEmail struct {
defaultAlterIDs uint16 defaultAlterIDs uint16
} }
func NewUserByEmail(users []*protocol.User, config *DefaultConfig) *userByEmail { func newUserByEmail(users []*protocol.User, config *DefaultConfig) *userByEmail {
cache := make(map[string]*protocol.User) cache := make(map[string]*protocol.User)
for _, user := range users { for _, user := range users {
cache[user.Email] = user cache[user.Email] = user
@ -80,6 +80,7 @@ type Handler struct {
sessionHistory *encoding.SessionHistory sessionHistory *encoding.SessionHistory
} }
// New creates a new VMess inbound handler.
func New(ctx context.Context, config *Config) (*Handler, error) { func New(ctx context.Context, config *Config) (*Handler, error) {
space := app.SpaceFromContext(ctx) space := app.SpaceFromContext(ctx)
if space == nil { if space == nil {
@ -96,7 +97,7 @@ func New(ctx context.Context, config *Config) (*Handler, error) {
handler := &Handler{ handler := &Handler{
clients: allowedClients, clients: allowedClients,
detours: config.Detour, detours: config.Detour,
usersByEmail: NewUserByEmail(config.User, config.GetDefaultValue()), usersByEmail: newUserByEmail(config.User, config.GetDefaultValue()),
sessionHistory: encoding.NewSessionHistory(ctx), sessionHistory: encoding.NewSessionHistory(ctx),
} }
@ -118,10 +119,10 @@ func (*Handler) Network() net.NetworkList {
} }
} }
func (v *Handler) GetUser(email string) *protocol.User { func (h *Handler) GetUser(email string) *protocol.User {
user, existing := v.usersByEmail.Get(email) user, existing := h.usersByEmail.Get(email)
if !existing { if !existing {
v.clients.Add(user) h.clients.Add(user)
} }
return user return user
} }
@ -172,14 +173,14 @@ func transferResponse(timer signal.ActivityUpdater, session *encoding.ServerSess
} }
// Process implements proxy.Inbound.Process(). // Process implements proxy.Inbound.Process().
func (v *Handler) Process(ctx context.Context, network net.Network, connection internet.Connection, dispatcher dispatcher.Interface) error { func (h *Handler) Process(ctx context.Context, network net.Network, connection internet.Connection, dispatcher dispatcher.Interface) error {
if err := connection.SetReadDeadline(time.Now().Add(time.Second * 8)); err != nil { if err := connection.SetReadDeadline(time.Now().Add(time.Second * 8)); err != nil {
return newError("unable to set read deadline").Base(err).AtWarning() return newError("unable to set read deadline").Base(err).AtWarning()
} }
reader := buf.NewBufferedReader(buf.NewReader(connection)) reader := buf.NewBufferedReader(buf.NewReader(connection))
session := encoding.NewServerSession(v.clients, v.sessionHistory) session := encoding.NewServerSession(h.clients, h.sessionHistory)
request, err := session.DecodeRequestHeader(reader) request, err := session.DecodeRequestHeader(reader)
if err != nil { if err != nil {
@ -203,7 +204,6 @@ func (v *Handler) Process(ctx context.Context, network net.Network, connection i
} }
userSettings := request.User.GetSettings() userSettings := request.User.GetSettings()
ctx = protocol.ContextWithUser(ctx, request.User) ctx = protocol.ContextWithUser(ctx, request.User)
ctx, cancel := context.WithCancel(ctx) ctx, cancel := context.WithCancel(ctx)
@ -225,7 +225,7 @@ func (v *Handler) Process(ctx context.Context, network net.Network, connection i
defer writer.Flush() defer writer.Flush()
response := &protocol.ResponseHeader{ response := &protocol.ResponseHeader{
Command: v.generateCommand(ctx, request), Command: h.generateCommand(ctx, request),
} }
return transferResponse(timer, session, request, response, output, writer) return transferResponse(timer, session, request, response, output, writer)
}) })
@ -239,11 +239,11 @@ func (v *Handler) Process(ctx context.Context, network net.Network, connection i
return nil return nil
} }
func (v *Handler) generateCommand(ctx context.Context, request *protocol.RequestHeader) protocol.ResponseCommand { func (h *Handler) generateCommand(ctx context.Context, request *protocol.RequestHeader) protocol.ResponseCommand {
if v.detours != nil { if h.detours != nil {
tag := v.detours.To tag := h.detours.To
if v.inboundHandlerManager != nil { if h.inboundHandlerManager != nil {
handler, err := v.inboundHandlerManager.GetHandler(ctx, tag) handler, err := h.inboundHandlerManager.GetHandler(ctx, tag)
if err != nil { if err != nil {
log.Trace(newError("failed to get detour handler: ", tag, err).AtWarning()) log.Trace(newError("failed to get detour handler: ", tag, err).AtWarning())
return nil return nil