Reapply SingboxForPanel integration on upstream stable
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"net"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"github.com/sagernet/sing-box/adapter"
|
||||
"github.com/sagernet/sing-box/adapter/inbound"
|
||||
@@ -31,7 +32,10 @@ func RegisterInbound(registry *inbound.Registry) {
|
||||
inbound.Register[option.VLESSInboundOptions](registry, C.TypeVLESS, NewInbound)
|
||||
}
|
||||
|
||||
var _ adapter.TCPInjectableInbound = (*Inbound)(nil)
|
||||
var (
|
||||
_ adapter.TCPInjectableInbound = (*Inbound)(nil)
|
||||
_ adapter.ManagedSSMServer = (*Inbound)(nil)
|
||||
)
|
||||
|
||||
type Inbound struct {
|
||||
inbound.Adapter
|
||||
@@ -43,6 +47,40 @@ type Inbound struct {
|
||||
service *vless.Service[int]
|
||||
tlsConfig tls.ServerConfig
|
||||
transport adapter.V2RayServerTransport
|
||||
tracker adapter.SSMTracker
|
||||
ssmMutex sync.RWMutex
|
||||
}
|
||||
|
||||
func (h *Inbound) SetTracker(tracker adapter.SSMTracker) {
|
||||
h.ssmMutex.Lock()
|
||||
defer h.ssmMutex.Unlock()
|
||||
h.tracker = tracker
|
||||
}
|
||||
|
||||
func (h *Inbound) UpdateUsers(users []string, uuids []string, flows []string) error {
|
||||
h.ssmMutex.Lock()
|
||||
defer h.ssmMutex.Unlock()
|
||||
newUsers := make([]option.VLESSUser, len(users))
|
||||
for i := range users {
|
||||
flow := ""
|
||||
if i < len(flows) {
|
||||
flow = flows[i]
|
||||
}
|
||||
newUsers[i] = option.VLESSUser{
|
||||
Name: users[i],
|
||||
UUID: uuids[i],
|
||||
Flow: flow,
|
||||
}
|
||||
}
|
||||
h.users = newUsers
|
||||
h.service.UpdateUsers(common.MapIndexed(h.users, func(index int, _ option.VLESSUser) int {
|
||||
return index
|
||||
}), common.Map(h.users, func(it option.VLESSUser) string {
|
||||
return it.UUID
|
||||
}), common.Map(h.users, func(it option.VLESSUser) string {
|
||||
return it.Flow
|
||||
}))
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewInbound(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.VLESSInboundOptions) (adapter.Inbound, error) {
|
||||
@@ -172,13 +210,25 @@ func (h *Inbound) newConnectionEx(ctx context.Context, conn net.Conn, metadata a
|
||||
N.CloseOnHandshakeFailure(conn, onClose, os.ErrInvalid)
|
||||
return
|
||||
}
|
||||
user := h.users[userIndex].Name
|
||||
h.ssmMutex.RLock()
|
||||
if userIndex < 0 || userIndex >= len(h.users) {
|
||||
h.ssmMutex.RUnlock()
|
||||
N.CloseOnHandshakeFailure(conn, onClose, os.ErrInvalid)
|
||||
return
|
||||
}
|
||||
userEntry := h.users[userIndex]
|
||||
tracker := h.tracker
|
||||
h.ssmMutex.RUnlock()
|
||||
user := userEntry.Name
|
||||
if user == "" {
|
||||
user = F.ToString(userIndex)
|
||||
} else {
|
||||
metadata.User = user
|
||||
}
|
||||
h.logger.InfoContext(ctx, "[", user, "] inbound connection to ", metadata.Destination)
|
||||
if tracker != nil {
|
||||
conn = tracker.TrackConnection(conn, metadata)
|
||||
}
|
||||
h.router.RouteConnectionEx(ctx, conn, metadata, onClose)
|
||||
}
|
||||
|
||||
@@ -190,7 +240,16 @@ func (h *Inbound) newPacketConnectionEx(ctx context.Context, conn N.PacketConn,
|
||||
N.CloseOnHandshakeFailure(conn, onClose, os.ErrInvalid)
|
||||
return
|
||||
}
|
||||
user := h.users[userIndex].Name
|
||||
h.ssmMutex.RLock()
|
||||
if userIndex < 0 || userIndex >= len(h.users) {
|
||||
h.ssmMutex.RUnlock()
|
||||
N.CloseOnHandshakeFailure(conn, onClose, os.ErrInvalid)
|
||||
return
|
||||
}
|
||||
userEntry := h.users[userIndex]
|
||||
tracker := h.tracker
|
||||
h.ssmMutex.RUnlock()
|
||||
user := userEntry.Name
|
||||
if user == "" {
|
||||
user = F.ToString(userIndex)
|
||||
} else {
|
||||
@@ -203,6 +262,9 @@ func (h *Inbound) newPacketConnectionEx(ctx context.Context, conn N.PacketConn,
|
||||
} else {
|
||||
h.logger.InfoContext(ctx, "[", user, "] inbound packet connection to ", metadata.Destination)
|
||||
}
|
||||
if tracker != nil {
|
||||
conn = tracker.TrackPacketConnection(conn, metadata)
|
||||
}
|
||||
h.router.RoutePacketConnectionEx(ctx, conn, metadata, onClose)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user