mirror of
https://github.com/v2fly/v2ray-core.git
synced 2026-01-03 07:45:29 -05:00
massive refactoring for interoperability
This commit is contained in:
@@ -8,10 +8,7 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"v2ray.com/core/app"
|
||||
"v2ray.com/core/app/dispatcher"
|
||||
"v2ray.com/core/app/policy"
|
||||
"v2ray.com/core/app/proxyman"
|
||||
"v2ray.com/core"
|
||||
"v2ray.com/core/common"
|
||||
"v2ray.com/core/common/buf"
|
||||
"v2ray.com/core/common/errors"
|
||||
@@ -74,21 +71,16 @@ func (v *userByEmail) Get(email string) (*protocol.User, bool) {
|
||||
|
||||
// Handler is an inbound connection handler that handles messages in VMess protocol.
|
||||
type Handler struct {
|
||||
inboundHandlerManager proxyman.InboundHandlerManager
|
||||
policyManager core.PolicyManager
|
||||
inboundHandlerManager core.InboundHandlerManager
|
||||
clients protocol.UserValidator
|
||||
usersByEmail *userByEmail
|
||||
detours *DetourConfig
|
||||
sessionHistory *encoding.SessionHistory
|
||||
policyManager policy.Manager
|
||||
}
|
||||
|
||||
// New creates a new VMess inbound handler.
|
||||
func New(ctx context.Context, config *Config) (*Handler, error) {
|
||||
space := app.SpaceFromContext(ctx)
|
||||
if space == nil {
|
||||
return nil, newError("no space in context")
|
||||
}
|
||||
|
||||
allowedClients := vmess.NewTimedUserValidator(ctx, protocol.DefaultIDHash)
|
||||
for _, user := range config.User {
|
||||
if err := allowedClients.Add(user); err != nil {
|
||||
@@ -96,24 +88,19 @@ func New(ctx context.Context, config *Config) (*Handler, error) {
|
||||
}
|
||||
}
|
||||
|
||||
handler := &Handler{
|
||||
clients: allowedClients,
|
||||
detours: config.Detour,
|
||||
usersByEmail: newUserByEmail(config.User, config.GetDefaultValue()),
|
||||
sessionHistory: encoding.NewSessionHistory(ctx),
|
||||
v := core.FromContext(ctx)
|
||||
if v == nil {
|
||||
return nil, newError("V is not in context.")
|
||||
}
|
||||
|
||||
space.On(app.SpaceInitializing, func(interface{}) error {
|
||||
handler.inboundHandlerManager = proxyman.InboundHandlerManagerFromSpace(space)
|
||||
if handler.inboundHandlerManager == nil {
|
||||
return newError("InboundHandlerManager is not found is space.")
|
||||
}
|
||||
handler.policyManager = policy.FromSpace(space)
|
||||
if handler.policyManager == nil {
|
||||
return newError("Policy is not found in space.")
|
||||
}
|
||||
return nil
|
||||
})
|
||||
handler := &Handler{
|
||||
policyManager: v.PolicyManager(),
|
||||
inboundHandlerManager: v.InboundHandlerManager(),
|
||||
clients: allowedClients,
|
||||
detours: config.Detour,
|
||||
usersByEmail: newUserByEmail(config.User, config.GetDefaultValue()),
|
||||
sessionHistory: encoding.NewSessionHistory(ctx),
|
||||
}
|
||||
|
||||
return handler, nil
|
||||
}
|
||||
@@ -179,9 +166,9 @@ func transferResponse(timer signal.ActivityUpdater, session *encoding.ServerSess
|
||||
}
|
||||
|
||||
// Process implements proxy.Inbound.Process().
|
||||
func (h *Handler) Process(ctx context.Context, network net.Network, connection internet.Connection, dispatcher dispatcher.Interface) error {
|
||||
sessionPolicy := h.policyManager.GetPolicy(0)
|
||||
if err := connection.SetReadDeadline(time.Now().Add(sessionPolicy.Timeout.Handshake.Duration())); err != nil {
|
||||
func (h *Handler) Process(ctx context.Context, network net.Network, connection internet.Connection, dispatcher core.Dispatcher) error {
|
||||
sessionPolicy := h.policyManager.ForLevel(0)
|
||||
if err := connection.SetReadDeadline(time.Now().Add(sessionPolicy.Timeouts.Handshake)); err != nil {
|
||||
return newError("unable to set read deadline").Base(err).AtWarning()
|
||||
}
|
||||
|
||||
@@ -221,11 +208,11 @@ func (h *Handler) Process(ctx context.Context, network net.Network, connection i
|
||||
newError("unable to set back read deadline").Base(err).WriteToLog()
|
||||
}
|
||||
|
||||
sessionPolicy = h.policyManager.GetPolicy(request.User.Level)
|
||||
sessionPolicy = h.policyManager.ForLevel(request.User.Level)
|
||||
ctx = protocol.ContextWithUser(ctx, request.User)
|
||||
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
timer := signal.CancelAfterInactivity(ctx, cancel, sessionPolicy.Timeout.ConnectionIdle.Duration())
|
||||
timer := signal.CancelAfterInactivity(ctx, cancel, sessionPolicy.Timeouts.ConnectionIdle)
|
||||
ray, err := dispatcher.Dispatch(ctx, request.Destination())
|
||||
if err != nil {
|
||||
return newError("failed to dispatch request to ", request.Destination()).Base(err)
|
||||
@@ -235,14 +222,14 @@ func (h *Handler) Process(ctx context.Context, network net.Network, connection i
|
||||
output := ray.InboundOutput()
|
||||
|
||||
requestDone := signal.ExecuteAsync(func() error {
|
||||
defer timer.SetTimeout(sessionPolicy.Timeout.DownlinkOnly.Duration())
|
||||
defer timer.SetTimeout(sessionPolicy.Timeouts.DownlinkOnly)
|
||||
return transferRequest(timer, session, request, reader, input)
|
||||
})
|
||||
|
||||
responseDone := signal.ExecuteAsync(func() error {
|
||||
writer := buf.NewBufferedWriter(buf.NewWriter(connection))
|
||||
defer writer.Flush()
|
||||
defer timer.SetTimeout(sessionPolicy.Timeout.UplinkOnly.Duration())
|
||||
defer timer.SetTimeout(sessionPolicy.Timeouts.UplinkOnly)
|
||||
|
||||
response := &protocol.ResponseHeader{
|
||||
Command: h.generateCommand(ctx, request),
|
||||
|
||||
Reference in New Issue
Block a user