fix not loading user in hysteria

This commit is contained in:
Nova 2023-07-10 06:43:32 +03:30
parent e929dde13e
commit 38c8a8d117

View File

@ -4,6 +4,7 @@ package inbound
import ( import (
"context" "context"
"github.com/sagernet/sing/common/auth"
"sync" "sync"
"github.com/sagernet/quic-go" "github.com/sagernet/quic-go"
@ -15,7 +16,6 @@ import (
"github.com/sagernet/sing-box/option" "github.com/sagernet/sing-box/option"
"github.com/sagernet/sing-box/transport/hysteria" "github.com/sagernet/sing-box/transport/hysteria"
"github.com/sagernet/sing/common" "github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/auth"
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
F "github.com/sagernet/sing/common/format" F "github.com/sagernet/sing/common/format"
M "github.com/sagernet/sing/common/metadata" M "github.com/sagernet/sing/common/metadata"
@ -198,9 +198,10 @@ func (h *Hysteria) accept(ctx context.Context, conn quic.Connection) error {
user := h.authUser[userIndex] user := h.authUser[userIndex]
if user == "" { if user == "" {
user = F.ToString(userIndex) user = F.ToString(userIndex)
} else {
ctx = auth.ContextWithUser(ctx, user)
} }
ctx = auth.ContextWithUser(ctx, userIndex)
h.logger.InfoContext(ctx, "[", user, "] inbound connection from ", conn.RemoteAddr()) h.logger.InfoContext(ctx, "[", user, "] inbound connection from ", conn.RemoteAddr())
} else { } else {
h.logger.InfoContext(ctx, "inbound connection from ", conn.RemoteAddr()) h.logger.InfoContext(ctx, "inbound connection from ", conn.RemoteAddr())
@ -284,6 +285,10 @@ func (h *Hysteria) acceptStream(ctx context.Context, conn quic.Connection, strea
metadata.OriginDestination = M.SocksaddrFromNet(conn.LocalAddr()).Unwrap() metadata.OriginDestination = M.SocksaddrFromNet(conn.LocalAddr()).Unwrap()
metadata.Destination = M.ParseSocksaddrHostPort(request.Host, request.Port).Unwrap() metadata.Destination = M.ParseSocksaddrHostPort(request.Host, request.Port).Unwrap()
if userIndex, loaded := auth.UserFromContext[int](ctx); loaded {
metadata.User = h.authUser[userIndex]
}
if !request.UDP { if !request.UDP {
err = hysteria.WriteServerResponse(stream, hysteria.ServerResponse{ err = hysteria.WriteServerResponse(stream, hysteria.ServerResponse{
OK: true, OK: true,