From 74d2b9211d1237d4df89c8f4d0125656040e194d Mon Sep 17 00:00:00 2001 From: rachel-mp4 Date: Sun, 20 Jul 2025 12:56:15 -0400 Subject: [PATCH] add nonces to init message based on secret/uri option --- crypto.go | 16 ++++++++++++++++ go.mod | 2 +- go.sum | 4 ++-- options.go | 10 ++++++++++ server.go | 6 ++++++ 5 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 crypto.go diff --git a/crypto.go b/crypto.go new file mode 100644 index 0000000..c3434ad --- /dev/null +++ b/crypto.go @@ -0,0 +1,16 @@ +package lrcd + +import ( + "crypto/sha256" + "encoding/binary" +) + +func GenerateNonce(messageID uint32, channelURI string, secret string) []byte { + h := sha256.New() + idBytes := make([]byte, 4) + binary.LittleEndian.PutUint32(idBytes, messageID) + h.Write(idBytes) + h.Write([]byte(channelURI)) + h.Write([]byte(secret)) + return h.Sum(nil) +} diff --git a/go.mod b/go.mod index 1f21e80..bef815a 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,6 @@ go 1.24.2 require ( github.com/gorilla/websocket v1.5.3 - github.com/rachel-mp4/lrcproto v0.0.0-20250527205756-58da8216f98c + github.com/rachel-mp4/lrcproto v0.0.0-20250720164211-c6162669b709 google.golang.org/protobuf v1.36.6 ) diff --git a/go.sum b/go.sum index cf19f68..7b822c6 100644 --- a/go.sum +++ b/go.sum @@ -2,8 +2,8 @@ github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= -github.com/rachel-mp4/lrcproto v0.0.0-20250527205756-58da8216f98c h1:nOWeKeE7wph0IcwUyUBi0YBynUnAo4JW/J5DM88x4KM= -github.com/rachel-mp4/lrcproto v0.0.0-20250527205756-58da8216f98c/go.mod h1:hQzO36tQELGbkmRnUtKeM6NMU34t79ZcTlhM+MO7pHw= +github.com/rachel-mp4/lrcproto v0.0.0-20250720164211-c6162669b709 h1:P//gJE0zFv9Qvfn8dvp9ZrnG0FZh2MVcAX+uOP2flRw= +github.com/rachel-mp4/lrcproto v0.0.0-20250720164211-c6162669b709/go.mod h1:hQzO36tQELGbkmRnUtKeM6NMU34t79ZcTlhM+MO7pHw= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY= diff --git a/options.go b/options.go index 6f4418e..c0d73a0 100644 --- a/options.go +++ b/options.go @@ -9,6 +9,8 @@ import ( ) type options struct { + uri string + secret string welcome *string writer *io.Writer verbose bool @@ -66,3 +68,11 @@ func WithPubChannel(pubChan chan PubEvent) Option { return nil } } + +func WithServerURIAndSecret(uri string, secret string) Option { + return func(options *options) error { + options.secret = secret + options.uri = uri + return nil + } +} diff --git a/server.go b/server.go index 80b1b7e..0e6f701 100644 --- a/server.go +++ b/server.go @@ -14,6 +14,8 @@ import ( ) type Server struct { + secret string + uri string eventBus chan clientEvent ctx context.Context cancel context.CancelFunc @@ -88,6 +90,8 @@ func NewServer(opts ...Option) (*Server, error) { if options.initialID != nil { s.lastID = *options.initialID } + s.uri = options.uri + s.secret = options.secret s.clients = make(map[*client]bool) s.clientsMu = sync.Mutex{} @@ -331,6 +335,7 @@ func (s *Server) handleInit(msg *lrcpb.Event_Init, client *client) { msg.Init.Color = client.color echoed := false msg.Init.Echoed = &echoed + msg.Init.Nonce = nil if s.initChan != nil { select { case s.initChan <- *msg: @@ -348,6 +353,7 @@ func (s *Server) broadcastInit(msg *lrcpb.Event_Init, client *client) { stdData, _ := proto.Marshal(stdEvent) echoed := true msg.Init.Echoed = &echoed + msg.Init.Nonce = GenerateNonce(*msg.Init.Id, s.uri, s.secret) echoEvent := &lrcpb.Event{Msg: msg} echoData, _ := proto.Marshal(echoEvent) muteEvent := &lrcpb.Event{Msg: &lrcpb.Event_Mute{Mute: &lrcpb.Mute{Id: msg.Init.GetId()}}} -- 2.51.2