Move session id codec to separate package.

This commit is contained in:
Joachim Bauch 2026-01-08 14:19:34 +01:00
commit daaf16bbf8
No known key found for this signature in database
GPG key ID: 77C1D22D53E15F02
13 changed files with 79 additions and 68 deletions

View file

@ -100,6 +100,10 @@ component_management:
name: security
paths:
- security/**
- component_id: module_session
name: session
paths:
- session/**
- component_id: module_sfu
name: sfu
paths:

View file

@ -41,6 +41,7 @@ import (
"github.com/strukturag/nextcloud-spreed-signaling/internal"
"github.com/strukturag/nextcloud-spreed-signaling/log"
"github.com/strukturag/nextcloud-spreed-signaling/nats"
"github.com/strukturag/nextcloud-spreed-signaling/session"
"github.com/strukturag/nextcloud-spreed-signaling/sfu"
"github.com/strukturag/nextcloud-spreed-signaling/talk"
)
@ -63,7 +64,7 @@ type ClientSession struct {
events events.AsyncEvents
privateId api.PrivateSessionId
publicId api.PublicSessionId
data *SessionIdData
data *session.SessionIdData
ctx context.Context
closeFunc context.CancelFunc
@ -125,7 +126,7 @@ type ClientSession struct {
responseHandlers map[string]ResponseHandlerFunc
}
func NewClientSession(hub *Hub, privateId api.PrivateSessionId, publicId api.PublicSessionId, data *SessionIdData, backend *talk.Backend, hello *api.HelloClientMessage, auth *talk.BackendClientAuthResponse) (*ClientSession, error) {
func NewClientSession(hub *Hub, privateId api.PrivateSessionId, publicId api.PublicSessionId, data *session.SessionIdData, backend *talk.Backend, hello *api.HelloClientMessage, auth *talk.BackendClientAuthResponse) (*ClientSession, error) {
ctx := log.NewLoggerContext(context.Background(), hub.logger)
ctx, closeFunc := context.WithCancel(ctx)
s := &ClientSession{
@ -183,7 +184,7 @@ func (s *ClientSession) RoomSessionId() api.RoomSessionId {
return s.roomSessionId
}
func (s *ClientSession) Data() *SessionIdData {
func (s *ClientSession) Data() *session.SessionIdData {
return s.data
}

View file

@ -48,7 +48,6 @@ import (
"github.com/gorilla/websocket"
"github.com/prometheus/client_golang/prometheus/promhttp"
signaling "github.com/strukturag/nextcloud-spreed-signaling"
"github.com/strukturag/nextcloud-spreed-signaling/api"
"github.com/strukturag/nextcloud-spreed-signaling/async"
"github.com/strukturag/nextcloud-spreed-signaling/client"
@ -57,6 +56,7 @@ import (
"github.com/strukturag/nextcloud-spreed-signaling/geoip"
"github.com/strukturag/nextcloud-spreed-signaling/log"
"github.com/strukturag/nextcloud-spreed-signaling/proxy"
"github.com/strukturag/nextcloud-spreed-signaling/session"
"github.com/strukturag/nextcloud-spreed-signaling/sfu"
"github.com/strukturag/nextcloud-spreed-signaling/sfu/janus"
janusapi "github.com/strukturag/nextcloud-spreed-signaling/sfu/janus/janus"
@ -147,7 +147,7 @@ type ProxyServer struct {
trustedProxies atomic.Pointer[container.IPList]
sid atomic.Uint64
cookie *signaling.SessionIdCodec
cookie *session.SessionIdCodec
sessionsLock sync.RWMutex
// +checklocks:sessionsLock
sessions map[uint64]*ProxySession
@ -242,7 +242,7 @@ func NewProxyServer(ctx context.Context, r *mux.Router, version string, config *
return nil, fmt.Errorf("could not generate random block key: %s", err)
}
sessionIds, err := signaling.NewSessionIdCodec(hashKey, blockKey)
sessionIds, err := session.NewSessionIdCodec(hashKey, blockKey)
if err != nil {
return nil, fmt.Errorf("error creating session id codec: %w", err)
}
@ -1507,7 +1507,7 @@ func (s *ProxyServer) NewSession(hello *proxy.HelloClientMessage) (*ProxySession
sid = s.sid.Add(1)
}
sessionIdData := &signaling.SessionIdData{
sessionIdData := &session.SessionIdData{
Sid: sid,
Created: time.Now().UnixMicro(),
}

27
hub.go
View file

@ -62,6 +62,7 @@ import (
"github.com/strukturag/nextcloud-spreed-signaling/grpc"
"github.com/strukturag/nextcloud-spreed-signaling/internal"
"github.com/strukturag/nextcloud-spreed-signaling/log"
"github.com/strukturag/nextcloud-spreed-signaling/session"
"github.com/strukturag/nextcloud-spreed-signaling/sfu"
"github.com/strukturag/nextcloud-spreed-signaling/sfu/janus"
"github.com/strukturag/nextcloud-spreed-signaling/talk"
@ -159,7 +160,7 @@ type Hub struct {
logger log.Logger
events events.AsyncEvents
upgrader websocket.Upgrader
sessionIds *SessionIdCodec
sessionIds *session.SessionIdCodec
info *api.WelcomeServerMessage
infoInternal *api.WelcomeServerMessage
welcome atomic.Value // *api.ServerMessage
@ -192,7 +193,7 @@ type Hub struct {
// +checklocks:mu
virtualSessions map[api.PublicSessionId]uint64
decodeCaches []*container.LruCache[*SessionIdData]
decodeCaches []*container.LruCache[*session.SessionIdData]
mcu sfu.SFU
mcuTimeout time.Duration
@ -258,7 +259,7 @@ func NewHub(ctx context.Context, cfg *goconf.ConfigFile, events events.AsyncEven
return nil, fmt.Errorf("the sessions block key must be 16, 24 or 32 bytes but is %d bytes", len(blockKey))
}
sessionIds, err := NewSessionIdCodec([]byte(hashKey), blockBytes)
sessionIds, err := session.NewSessionIdCodec([]byte(hashKey), blockBytes)
if err != nil {
return nil, fmt.Errorf("error creating session id codec: %w", err)
}
@ -320,9 +321,9 @@ func NewHub(ctx context.Context, cfg *goconf.ConfigFile, events events.AsyncEven
logger.Printf("No trusted proxies configured, only allowing for %s", trustedProxiesIps)
}
decodeCaches := make([]*container.LruCache[*SessionIdData], 0, numDecodeCaches)
decodeCaches := make([]*container.LruCache[*session.SessionIdData], 0, numDecodeCaches)
for range numDecodeCaches {
decodeCaches = append(decodeCaches, container.NewLruCache[*SessionIdData](decodeCacheSize))
decodeCaches = append(decodeCaches, container.NewLruCache[*session.SessionIdData](decodeCacheSize))
}
roomSessions, err := NewBuiltinRoomSessions(rpcClients)
@ -632,7 +633,7 @@ func (h *Hub) Reload(ctx context.Context, config *goconf.ConfigFile) {
h.rpcClients.Reload(config)
}
func (h *Hub) getDecodeCache(cache_key string) *container.LruCache[*SessionIdData] {
func (h *Hub) getDecodeCache(cache_key string) *container.LruCache[*session.SessionIdData] {
hash := fnv.New32a()
// Make sure we don't have a temporary allocation for the string -> []byte conversion.
hash.Write(unsafe.Slice(unsafe.StringData(cache_key), len(cache_key))) // nolint
@ -657,15 +658,15 @@ func (h *Hub) invalidateSessionId(id string) {
cache.Remove(id)
}
func (h *Hub) setDecodedPublicSessionId(id api.PublicSessionId, data *SessionIdData) {
func (h *Hub) setDecodedPublicSessionId(id api.PublicSessionId, data *session.SessionIdData) {
h.setDecodedSessionId(string(id), data)
}
func (h *Hub) setDecodedPrivateSessionId(id api.PrivateSessionId, data *SessionIdData) {
func (h *Hub) setDecodedPrivateSessionId(id api.PrivateSessionId, data *session.SessionIdData) {
h.setDecodedSessionId(string(id), data)
}
func (h *Hub) setDecodedSessionId(id string, data *SessionIdData) {
func (h *Hub) setDecodedSessionId(id string, data *session.SessionIdData) {
if len(id) == 0 {
return
}
@ -674,7 +675,7 @@ func (h *Hub) setDecodedSessionId(id string, data *SessionIdData) {
cache.Set(id, data)
}
func (h *Hub) decodePrivateSessionId(id api.PrivateSessionId) *SessionIdData {
func (h *Hub) decodePrivateSessionId(id api.PrivateSessionId) *session.SessionIdData {
if len(id) == 0 {
return nil
}
@ -694,7 +695,7 @@ func (h *Hub) decodePrivateSessionId(id api.PrivateSessionId) *SessionIdData {
return data
}
func (h *Hub) decodePublicSessionId(id api.PublicSessionId) *SessionIdData {
func (h *Hub) decodePublicSessionId(id api.PublicSessionId) *session.SessionIdData {
if len(id) == 0 {
return nil
}
@ -950,12 +951,12 @@ func (h *Hub) unregisterRemoteSession(session *RemoteSession) {
delete(h.remoteSessions, session)
}
func (h *Hub) newSessionIdData(backend *talk.Backend) *SessionIdData {
func (h *Hub) newSessionIdData(backend *talk.Backend) *session.SessionIdData {
sid := h.sid.Add(1)
for sid == 0 {
sid = h.sid.Add(1)
}
sessionIdData := &SessionIdData{
sessionIdData := &session.SessionIdData{
Sid: sid,
Created: time.Now().UnixMicro(),
BackendId: backend.Id(),

View file

@ -63,6 +63,7 @@ import (
"github.com/strukturag/nextcloud-spreed-signaling/log"
"github.com/strukturag/nextcloud-spreed-signaling/mock"
"github.com/strukturag/nextcloud-spreed-signaling/nats"
"github.com/strukturag/nextcloud-spreed-signaling/session"
sfutest "github.com/strukturag/nextcloud-spreed-signaling/sfu/test"
"github.com/strukturag/nextcloud-spreed-signaling/talk"
"github.com/strukturag/nextcloud-spreed-signaling/test"
@ -823,17 +824,17 @@ func registerBackendHandlerUrl(t *testing.T, router *mux.Router, url string) {
func Benchmark_DecodePrivateSessionIdCached(b *testing.B) {
require := require.New(b)
decodeCaches := make([]*container.LruCache[*SessionIdData], 0, numDecodeCaches)
decodeCaches := make([]*container.LruCache[*session.SessionIdData], 0, numDecodeCaches)
for range numDecodeCaches {
decodeCaches = append(decodeCaches, container.NewLruCache[*SessionIdData](decodeCacheSize))
decodeCaches = append(decodeCaches, container.NewLruCache[*session.SessionIdData](decodeCacheSize))
}
backend := talk.NewCompatBackend(nil)
data := &SessionIdData{
data := &session.SessionIdData{
Sid: 1,
Created: time.Now().UnixMicro(),
BackendId: backend.Id(),
}
codec, err := NewSessionIdCodec([]byte("12345678901234567890123456789012"), []byte("09876543210987654321098765432109"))
codec, err := session.NewSessionIdCodec([]byte("12345678901234567890123456789012"), []byte("09876543210987654321098765432109"))
require.NoError(err)
sid, err := codec.EncodePrivate(data)
require.NoError(err, "could not create session id")
@ -850,17 +851,17 @@ func Benchmark_DecodePrivateSessionIdCached(b *testing.B) {
func Benchmark_DecodePublicSessionIdCached(b *testing.B) {
require := require.New(b)
decodeCaches := make([]*container.LruCache[*SessionIdData], 0, numDecodeCaches)
decodeCaches := make([]*container.LruCache[*session.SessionIdData], 0, numDecodeCaches)
for range numDecodeCaches {
decodeCaches = append(decodeCaches, container.NewLruCache[*SessionIdData](decodeCacheSize))
decodeCaches = append(decodeCaches, container.NewLruCache[*session.SessionIdData](decodeCacheSize))
}
backend := talk.NewCompatBackend(nil)
data := &SessionIdData{
data := &session.SessionIdData{
Sid: 1,
Created: time.Now().UnixMicro(),
BackendId: backend.Id(),
}
codec, err := NewSessionIdCodec([]byte("12345678901234567890123456789012"), []byte("09876543210987654321098765432109"))
codec, err := session.NewSessionIdCodec([]byte("12345678901234567890123456789012"), []byte("09876543210987654321098765432109"))
require.NoError(err)
sid, err := codec.EncodePublic(data)
require.NoError(err, "could not create session id")

View file

@ -32,6 +32,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/strukturag/nextcloud-spreed-signaling/api"
"github.com/strukturag/nextcloud-spreed-signaling/session"
"github.com/strukturag/nextcloud-spreed-signaling/talk"
)
@ -55,7 +56,7 @@ func (s *DummySession) ClientType() api.ClientType {
return ""
}
func (s *DummySession) Data() *SessionIdData {
func (s *DummySession) Data() *session.SessionIdData {
return nil
}

View file

@ -29,6 +29,7 @@ import (
"time"
"github.com/strukturag/nextcloud-spreed-signaling/api"
"github.com/strukturag/nextcloud-spreed-signaling/session"
"github.com/strukturag/nextcloud-spreed-signaling/talk"
)
@ -37,7 +38,7 @@ type Session interface {
PrivateId() api.PrivateSessionId
PublicId() api.PublicSessionId
ClientType() api.ClientType
Data() *SessionIdData
Data() *session.SessionIdData
UserId() string
UserData() json.RawMessage

View file

@ -20,9 +20,9 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// Code generated by protoc-gen-go. DO NOT EDIT.
// source: session.proto
// source: session/session.proto
package signaling
package session
import (
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
@ -50,7 +50,7 @@ type SessionIdData struct {
func (x *SessionIdData) Reset() {
*x = SessionIdData{}
mi := &file_session_proto_msgTypes[0]
mi := &file_session_session_proto_msgTypes[0]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@ -62,7 +62,7 @@ func (x *SessionIdData) String() string {
func (*SessionIdData) ProtoMessage() {}
func (x *SessionIdData) ProtoReflect() protoreflect.Message {
mi := &file_session_proto_msgTypes[0]
mi := &file_session_session_proto_msgTypes[0]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@ -75,7 +75,7 @@ func (x *SessionIdData) ProtoReflect() protoreflect.Message {
// Deprecated: Use SessionIdData.ProtoReflect.Descriptor instead.
func (*SessionIdData) Descriptor() ([]byte, []int) {
return file_session_proto_rawDescGZIP(), []int{0}
return file_session_session_proto_rawDescGZIP(), []int{0}
}
func (x *SessionIdData) GetSid() uint64 {
@ -99,33 +99,33 @@ func (x *SessionIdData) GetBackendId() string {
return ""
}
var File_session_proto protoreflect.FileDescriptor
var File_session_session_proto protoreflect.FileDescriptor
const file_session_proto_rawDesc = "" +
const file_session_session_proto_rawDesc = "" +
"\n" +
"\rsession.proto\x12\tsignaling\"Y\n" +
"\x15session/session.proto\x12\asession\"Y\n" +
"\rSessionIdData\x12\x10\n" +
"\x03Sid\x18\x01 \x01(\x04R\x03Sid\x12\x18\n" +
"\aCreated\x18\x02 \x01(\x03R\aCreated\x12\x1c\n" +
"\tBackendId\x18\x03 \x01(\tR\tBackendIdB<Z:github.com/strukturag/nextcloud-spreed-signaling;signalingb\x06proto3"
"\tBackendId\x18\x03 \x01(\tR\tBackendIdB:Z8github.com/strukturag/nextcloud-spreed-signaling/sessionb\x06proto3"
var (
file_session_proto_rawDescOnce sync.Once
file_session_proto_rawDescData []byte
file_session_session_proto_rawDescOnce sync.Once
file_session_session_proto_rawDescData []byte
)
func file_session_proto_rawDescGZIP() []byte {
file_session_proto_rawDescOnce.Do(func() {
file_session_proto_rawDescData = protoimpl.X.CompressGZIP(unsafe.Slice(unsafe.StringData(file_session_proto_rawDesc), len(file_session_proto_rawDesc)))
func file_session_session_proto_rawDescGZIP() []byte {
file_session_session_proto_rawDescOnce.Do(func() {
file_session_session_proto_rawDescData = protoimpl.X.CompressGZIP(unsafe.Slice(unsafe.StringData(file_session_session_proto_rawDesc), len(file_session_session_proto_rawDesc)))
})
return file_session_proto_rawDescData
return file_session_session_proto_rawDescData
}
var file_session_proto_msgTypes = make([]protoimpl.MessageInfo, 1)
var file_session_proto_goTypes = []any{
(*SessionIdData)(nil), // 0: signaling.SessionIdData
var file_session_session_proto_msgTypes = make([]protoimpl.MessageInfo, 1)
var file_session_session_proto_goTypes = []any{
(*SessionIdData)(nil), // 0: session.SessionIdData
}
var file_session_proto_depIdxs = []int32{
var file_session_session_proto_depIdxs = []int32{
0, // [0:0] is the sub-list for method output_type
0, // [0:0] is the sub-list for method input_type
0, // [0:0] is the sub-list for extension type_name
@ -133,26 +133,26 @@ var file_session_proto_depIdxs = []int32{
0, // [0:0] is the sub-list for field type_name
}
func init() { file_session_proto_init() }
func file_session_proto_init() {
if File_session_proto != nil {
func init() { file_session_session_proto_init() }
func file_session_session_proto_init() {
if File_session_session_proto != nil {
return
}
type x struct{}
out := protoimpl.TypeBuilder{
File: protoimpl.DescBuilder{
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: unsafe.Slice(unsafe.StringData(file_session_proto_rawDesc), len(file_session_proto_rawDesc)),
RawDescriptor: unsafe.Slice(unsafe.StringData(file_session_session_proto_rawDesc), len(file_session_session_proto_rawDesc)),
NumEnums: 0,
NumMessages: 1,
NumExtensions: 0,
NumServices: 0,
},
GoTypes: file_session_proto_goTypes,
DependencyIndexes: file_session_proto_depIdxs,
MessageInfos: file_session_proto_msgTypes,
GoTypes: file_session_session_proto_goTypes,
DependencyIndexes: file_session_session_proto_depIdxs,
MessageInfos: file_session_session_proto_msgTypes,
}.Build()
File_session_proto = out.File
file_session_proto_goTypes = nil
file_session_proto_depIdxs = nil
File_session_session_proto = out.File
file_session_session_proto_goTypes = nil
file_session_session_proto_depIdxs = nil
}

View file

@ -21,9 +21,9 @@
*/
syntax = "proto3";
option go_package = "github.com/strukturag/nextcloud-spreed-signaling;signaling";
option go_package = "github.com/strukturag/nextcloud-spreed-signaling/session";
package signaling;
package session;
message SessionIdData {
uint64 Sid = 1;

View file

@ -19,7 +19,7 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package signaling
package session
import (
"crypto/aes"
@ -86,7 +86,7 @@ func (p *bytesPool) Put(b []byte) {
// SessionIdCodec encodes and decodes session ids.
//
// Inspired by https://github.com/gorilla/securecookie
type SessionIdCodec struct {
type SessionIdCodec struct { // nolint
hashKey []byte
cipher cipher.Block

View file

@ -19,7 +19,7 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package signaling
package session
import (
"testing"

View file

@ -44,6 +44,7 @@ import (
"github.com/strukturag/nextcloud-spreed-signaling/api"
"github.com/strukturag/nextcloud-spreed-signaling/internal"
"github.com/strukturag/nextcloud-spreed-signaling/session"
)
var (
@ -71,7 +72,7 @@ func getWebsocketUrl(url string) string {
}
}
func getPubliceSessionIdData(h *Hub, publicId api.PublicSessionId) *SessionIdData {
func getPubliceSessionIdData(h *Hub, publicId api.PublicSessionId) *session.SessionIdData {
decodedPublic := h.decodePublicSessionId(publicId)
if decodedPublic == nil {
panic("invalid public session id")

View file

@ -34,6 +34,7 @@ import (
"github.com/strukturag/nextcloud-spreed-signaling/internal"
"github.com/strukturag/nextcloud-spreed-signaling/log"
"github.com/strukturag/nextcloud-spreed-signaling/nats"
"github.com/strukturag/nextcloud-spreed-signaling/session"
"github.com/strukturag/nextcloud-spreed-signaling/talk"
)
@ -49,7 +50,7 @@ type VirtualSession struct {
session *ClientSession
privateId api.PrivateSessionId
publicId api.PublicSessionId
data *SessionIdData
data *session.SessionIdData
ctx context.Context
closeFunc context.CancelFunc
room atomic.Pointer[Room]
@ -70,7 +71,7 @@ func GetVirtualSessionId(session Session, sessionId api.PublicSessionId) api.Pub
return session.PublicId() + "|" + sessionId
}
func NewVirtualSession(session *ClientSession, privateId api.PrivateSessionId, publicId api.PublicSessionId, data *SessionIdData, msg *api.AddSessionInternalClientMessage) (*VirtualSession, error) {
func NewVirtualSession(session *ClientSession, privateId api.PrivateSessionId, publicId api.PublicSessionId, data *session.SessionIdData, msg *api.AddSessionInternalClientMessage) (*VirtualSession, error) {
ctx := log.NewLoggerContext(session.Context(), session.hub.logger)
ctx, closeFunc := context.WithCancel(ctx)
@ -138,7 +139,7 @@ func (s *VirtualSession) SetInCall(inCall int) bool {
return s.inCall.Set(uint32(inCall))
}
func (s *VirtualSession) Data() *SessionIdData {
func (s *VirtualSession) Data() *session.SessionIdData {
return s.data
}