Browse Source

use UUIDs as IDs in all entities (#1234)

pull/1239/head
Alessandro Ros 3 years ago committed by GitHub
parent
commit
423bb61daa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      go.mod
  2. 10
      internal/core/rtmp_conn.go
  3. 37
      internal/core/rtmp_server.go
  4. 6
      internal/core/rtsp_conn.go
  5. 72
      internal/core/rtsp_server.go
  6. 14
      internal/core/rtsp_session.go

2
go.mod

@ -9,6 +9,7 @@ require (
github.com/asticode/go-astits v1.10.1-0.20220319093903-4abe66a9b757 github.com/asticode/go-astits v1.10.1-0.20220319093903-4abe66a9b757
github.com/fsnotify/fsnotify v1.4.9 github.com/fsnotify/fsnotify v1.4.9
github.com/gin-gonic/gin v1.8.1 github.com/gin-gonic/gin v1.8.1
github.com/google/uuid v1.1.2
github.com/gookit/color v1.4.2 github.com/gookit/color v1.4.2
github.com/grafov/m3u8 v0.11.1 github.com/grafov/m3u8 v0.11.1
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
@ -31,7 +32,6 @@ require (
github.com/go-playground/universal-translator v0.18.0 // indirect github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.10.0 // indirect github.com/go-playground/validator/v10 v10.10.0 // indirect
github.com/goccy/go-json v0.9.7 // indirect github.com/goccy/go-json v0.9.7 // indirect
github.com/google/uuid v1.1.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect github.com/leodido/go-urn v1.2.1 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect github.com/mattn/go-isatty v0.0.14 // indirect

10
internal/core/rtmp_conn.go

@ -14,6 +14,7 @@ import (
"github.com/aler9/gortsplib/pkg/h264" "github.com/aler9/gortsplib/pkg/h264"
"github.com/aler9/gortsplib/pkg/mpeg4audio" "github.com/aler9/gortsplib/pkg/mpeg4audio"
"github.com/aler9/gortsplib/pkg/ringbuffer" "github.com/aler9/gortsplib/pkg/ringbuffer"
"github.com/google/uuid"
"github.com/notedit/rtmp/format/flv/flvio" "github.com/notedit/rtmp/format/flv/flvio"
"github.com/aler9/rtsp-simple-server/internal/conf" "github.com/aler9/rtsp-simple-server/internal/conf"
@ -56,7 +57,6 @@ type rtmpConnParent interface {
type rtmpConn struct { type rtmpConn struct {
isTLS bool isTLS bool
id string
externalAuthenticationURL string externalAuthenticationURL string
rtspAddress string rtspAddress string
readTimeout conf.StringDuration readTimeout conf.StringDuration
@ -73,6 +73,7 @@ type rtmpConn struct {
ctx context.Context ctx context.Context
ctxCancel func() ctxCancel func()
uuid uuid.UUID
created time.Time created time.Time
path *path path *path
ringBuffer *ringbuffer.RingBuffer // read ringBuffer *ringbuffer.RingBuffer // read
@ -83,7 +84,6 @@ type rtmpConn struct {
func newRTMPConn( func newRTMPConn(
parentCtx context.Context, parentCtx context.Context,
isTLS bool, isTLS bool,
id string,
externalAuthenticationURL string, externalAuthenticationURL string,
rtspAddress string, rtspAddress string,
readTimeout conf.StringDuration, readTimeout conf.StringDuration,
@ -101,7 +101,6 @@ func newRTMPConn(
c := &rtmpConn{ c := &rtmpConn{
isTLS: isTLS, isTLS: isTLS,
id: id,
externalAuthenticationURL: externalAuthenticationURL, externalAuthenticationURL: externalAuthenticationURL,
rtspAddress: rtspAddress, rtspAddress: rtspAddress,
readTimeout: readTimeout, readTimeout: readTimeout,
@ -117,6 +116,7 @@ func newRTMPConn(
parent: parent, parent: parent,
ctx: ctx, ctx: ctx,
ctxCancel: ctxCancel, ctxCancel: ctxCancel,
uuid: uuid.New(),
created: time.Now(), created: time.Now(),
} }
@ -683,7 +683,7 @@ func (c *rtmpConn) apiReaderDescribe() interface{} {
return struct { return struct {
Type string `json:"type"` Type string `json:"type"`
ID string `json:"id"` ID string `json:"id"`
}{"rtmpConn", c.id} }{"rtmpConn", c.uuid.String()}
} }
// apiSourceDescribe implements source. // apiSourceDescribe implements source.
@ -698,5 +698,5 @@ func (c *rtmpConn) apiSourceDescribe() interface{} {
return struct { return struct {
Type string `json:"type"` Type string `json:"type"`
ID string `json:"id"` ID string `json:"id"`
}{typ, c.id} }{typ, c.uuid.String()}
} }

37
internal/core/rtmp_server.go

@ -2,11 +2,9 @@ package core
import ( import (
"context" "context"
"crypto/rand"
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"net" "net"
"strconv"
"sync" "sync"
"time" "time"
@ -196,12 +194,9 @@ outer:
break outer break outer
case nconn := <-connNew: case nconn := <-connNew:
id, _ := s.newConnID()
c := newRTMPConn( c := newRTMPConn(
s.ctx, s.ctx,
s.isTLS, s.isTLS,
id,
s.externalAuthenticationURL, s.externalAuthenticationURL,
s.rtspAddress, s.rtspAddress,
s.readTimeout, s.readTimeout,
@ -228,7 +223,7 @@ outer:
} }
for c := range s.conns { for c := range s.conns {
data.Items[c.id] = rtmpServerAPIConnsListItem{ data.Items[c.uuid.String()] = rtmpServerAPIConnsListItem{
Created: c.created, Created: c.created,
RemoteAddr: c.remoteAddr().String(), RemoteAddr: c.remoteAddr().String(),
State: func() string { State: func() string {
@ -249,7 +244,7 @@ outer:
case req := <-s.chAPIConnsKick: case req := <-s.chAPIConnsKick:
res := func() bool { res := func() bool {
for c := range s.conns { for c := range s.conns {
if c.id == req.id { if c.uuid.String() == req.id {
delete(s.conns, c) delete(s.conns, c)
c.close() c.close()
return true return true
@ -277,34 +272,6 @@ outer:
} }
} }
func (s *rtmpServer) newConnID() (string, error) {
for {
b := make([]byte, 4)
_, err := rand.Read(b)
if err != nil {
return "", err
}
u := uint32(b[3])<<24 | uint32(b[2])<<16 | uint32(b[1])<<8 | uint32(b[0])
u %= 899999999
u += 100000000
id := strconv.FormatUint(uint64(u), 10)
alreadyPresent := func() bool {
for c := range s.conns {
if c.id == id {
return true
}
}
return false
}()
if !alreadyPresent {
return id, nil
}
}
}
// connClose is called by rtmpConn. // connClose is called by rtmpConn.
func (s *rtmpServer) connClose(c *rtmpConn) { func (s *rtmpServer) connClose(c *rtmpConn) {
select { select {

6
internal/core/rtsp_conn.go

@ -10,6 +10,7 @@ import (
"github.com/aler9/gortsplib/pkg/auth" "github.com/aler9/gortsplib/pkg/auth"
"github.com/aler9/gortsplib/pkg/base" "github.com/aler9/gortsplib/pkg/base"
"github.com/aler9/gortsplib/pkg/headers" "github.com/aler9/gortsplib/pkg/headers"
"github.com/google/uuid"
"github.com/aler9/rtsp-simple-server/internal/conf" "github.com/aler9/rtsp-simple-server/internal/conf"
"github.com/aler9/rtsp-simple-server/internal/externalcmd" "github.com/aler9/rtsp-simple-server/internal/externalcmd"
@ -25,7 +26,6 @@ type rtspConnParent interface {
} }
type rtspConn struct { type rtspConn struct {
id string
externalAuthenticationURL string externalAuthenticationURL string
rtspAddress string rtspAddress string
authMethods []headers.AuthMethod authMethods []headers.AuthMethod
@ -37,6 +37,7 @@ type rtspConn struct {
conn *gortsplib.ServerConn conn *gortsplib.ServerConn
parent rtspConnParent parent rtspConnParent
uuid uuid.UUID
created time.Time created time.Time
onConnectCmd *externalcmd.Cmd onConnectCmd *externalcmd.Cmd
authUser string authUser string
@ -46,7 +47,6 @@ type rtspConn struct {
} }
func newRTSPConn( func newRTSPConn(
id string,
externalAuthenticationURL string, externalAuthenticationURL string,
rtspAddress string, rtspAddress string,
authMethods []headers.AuthMethod, authMethods []headers.AuthMethod,
@ -59,7 +59,6 @@ func newRTSPConn(
parent rtspConnParent, parent rtspConnParent,
) *rtspConn { ) *rtspConn {
c := &rtspConn{ c := &rtspConn{
id: id,
externalAuthenticationURL: externalAuthenticationURL, externalAuthenticationURL: externalAuthenticationURL,
rtspAddress: rtspAddress, rtspAddress: rtspAddress,
authMethods: authMethods, authMethods: authMethods,
@ -70,6 +69,7 @@ func newRTSPConn(
pathManager: pathManager, pathManager: pathManager,
conn: conn, conn: conn,
parent: parent, parent: parent,
uuid: uuid.New(),
created: time.Now(), created: time.Now(),
} }

72
internal/core/rtsp_server.go

@ -2,10 +2,8 @@ package core
import ( import (
"context" "context"
"crypto/rand"
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"strconv"
"strings" "strings"
"sync" "sync"
"time" "time"
@ -239,68 +237,9 @@ outer:
} }
} }
func (s *rtspServer) newSessionID() (string, error) {
for {
b := make([]byte, 4)
_, err := rand.Read(b)
if err != nil {
return "", err
}
u := uint32(b[3])<<24 | uint32(b[2])<<16 | uint32(b[1])<<8 | uint32(b[0])
u %= 899999999
u += 100000000
id := strconv.FormatUint(uint64(u), 10)
alreadyPresent := func() bool {
for _, s := range s.sessions {
if s.id == id {
return true
}
}
return false
}()
if !alreadyPresent {
return id, nil
}
}
}
func (s *rtspServer) newConnID() (string, error) {
for {
b := make([]byte, 4)
_, err := rand.Read(b)
if err != nil {
return "", err
}
u := uint32(b[3])<<24 | uint32(b[2])<<16 | uint32(b[1])<<8 | uint32(b[0])
u %= 899999999
u += 100000000
id := strconv.FormatUint(uint64(u), 10)
alreadyPresent := func() bool {
for _, c := range s.conns {
if c.id == id {
return true
}
}
return false
}()
if !alreadyPresent {
return id, nil
}
}
}
// OnConnOpen implements gortsplib.ServerHandlerOnConnOpen. // OnConnOpen implements gortsplib.ServerHandlerOnConnOpen.
func (s *rtspServer) OnConnOpen(ctx *gortsplib.ServerHandlerOnConnOpenCtx) { func (s *rtspServer) OnConnOpen(ctx *gortsplib.ServerHandlerOnConnOpenCtx) {
s.mutex.Lock()
id, _ := s.newConnID()
c := newRTSPConn( c := newRTSPConn(
id,
s.externalAuthenticationURL, s.externalAuthenticationURL,
s.rtspAddress, s.rtspAddress,
s.authMethods, s.authMethods,
@ -311,6 +250,7 @@ func (s *rtspServer) OnConnOpen(ctx *gortsplib.ServerHandlerOnConnOpenCtx) {
s.pathManager, s.pathManager,
ctx.Conn, ctx.Conn,
s) s)
s.mutex.Lock()
s.conns[ctx.Conn] = c s.conns[ctx.Conn] = c
s.mutex.Unlock() s.mutex.Unlock()
@ -340,17 +280,15 @@ func (s *rtspServer) OnResponse(sc *gortsplib.ServerConn, res *base.Response) {
// OnSessionOpen implements gortsplib.ServerHandlerOnSessionOpen. // OnSessionOpen implements gortsplib.ServerHandlerOnSessionOpen.
func (s *rtspServer) OnSessionOpen(ctx *gortsplib.ServerHandlerOnSessionOpenCtx) { func (s *rtspServer) OnSessionOpen(ctx *gortsplib.ServerHandlerOnSessionOpenCtx) {
s.mutex.Lock()
id, _ := s.newSessionID()
se := newRTSPSession( se := newRTSPSession(
s.isTLS, s.isTLS,
s.protocols, s.protocols,
id,
ctx.Session, ctx.Session,
ctx.Conn, ctx.Conn,
s.externalCmdPool, s.externalCmdPool,
s.pathManager, s.pathManager,
s) s)
s.mutex.Lock()
s.sessions[ctx.Session] = se s.sessions[ctx.Session] = se
s.mutex.Unlock() s.mutex.Unlock()
ctx.Session.SetUserData(se) ctx.Session.SetUserData(se)
@ -435,7 +373,7 @@ func (s *rtspServer) apiConnsList() rtspServerAPIConnsListRes {
} }
for _, c := range s.conns { for _, c := range s.conns {
data.Items[c.id] = rtspServerAPIConnsListItem{ data.Items[c.uuid.String()] = rtspServerAPIConnsListItem{
Created: c.created, Created: c.created,
RemoteAddr: c.remoteAddr().String(), RemoteAddr: c.remoteAddr().String(),
} }
@ -460,7 +398,7 @@ func (s *rtspServer) apiSessionsList() rtspServerAPISessionsListRes {
} }
for _, s := range s.sessions { for _, s := range s.sessions {
data.Items[s.id] = rtspServerAPISessionsListItem{ data.Items[s.uuid.String()] = rtspServerAPISessionsListItem{
Created: s.created, Created: s.created,
RemoteAddr: s.remoteAddr().String(), RemoteAddr: s.remoteAddr().String(),
State: func() string { State: func() string {
@ -493,7 +431,7 @@ func (s *rtspServer) apiSessionsKick(id string) rtspServerAPISessionsKickRes {
defer s.mutex.RUnlock() defer s.mutex.RUnlock()
for key, se := range s.sessions { for key, se := range s.sessions {
if se.id == id { if se.uuid.String() == id {
se.close() se.close()
delete(s.sessions, key) delete(s.sessions, key)
se.onClose(liberrors.ErrServerTerminated{}) se.onClose(liberrors.ErrServerTerminated{})

14
internal/core/rtsp_session.go

@ -1,6 +1,7 @@
package core package core
import ( import (
"encoding/hex"
"errors" "errors"
"fmt" "fmt"
"net" "net"
@ -9,6 +10,7 @@ import (
"github.com/aler9/gortsplib" "github.com/aler9/gortsplib"
"github.com/aler9/gortsplib/pkg/base" "github.com/aler9/gortsplib/pkg/base"
"github.com/google/uuid"
"github.com/pion/rtp" "github.com/pion/rtp"
"github.com/aler9/rtsp-simple-server/internal/conf" "github.com/aler9/rtsp-simple-server/internal/conf"
@ -32,13 +34,13 @@ type rtspSessionParent interface {
type rtspSession struct { type rtspSession struct {
isTLS bool isTLS bool
protocols map[conf.Protocol]struct{} protocols map[conf.Protocol]struct{}
id string
ss *gortsplib.ServerSession ss *gortsplib.ServerSession
author *gortsplib.ServerConn author *gortsplib.ServerConn
externalCmdPool *externalcmd.Pool externalCmdPool *externalcmd.Pool
pathManager rtspSessionPathManager pathManager rtspSessionPathManager
parent rtspSessionParent parent rtspSessionParent
uuid uuid.UUID
created time.Time created time.Time
path *path path *path
stream *stream stream *stream
@ -50,7 +52,6 @@ type rtspSession struct {
func newRTSPSession( func newRTSPSession(
isTLS bool, isTLS bool,
protocols map[conf.Protocol]struct{}, protocols map[conf.Protocol]struct{},
id string,
ss *gortsplib.ServerSession, ss *gortsplib.ServerSession,
sc *gortsplib.ServerConn, sc *gortsplib.ServerConn,
externalCmdPool *externalcmd.Pool, externalCmdPool *externalcmd.Pool,
@ -60,12 +61,12 @@ func newRTSPSession(
s := &rtspSession{ s := &rtspSession{
isTLS: isTLS, isTLS: isTLS,
protocols: protocols, protocols: protocols,
id: id,
ss: ss, ss: ss,
author: sc, author: sc,
externalCmdPool: externalCmdPool, externalCmdPool: externalCmdPool,
pathManager: pathManager, pathManager: pathManager,
parent: parent, parent: parent,
uuid: uuid.New(),
created: time.Now(), created: time.Now(),
} }
@ -93,7 +94,8 @@ func (s *rtspSession) remoteAddr() net.Addr {
} }
func (s *rtspSession) log(level logger.Level, format string, args ...interface{}) { func (s *rtspSession) log(level logger.Level, format string, args ...interface{}) {
s.parent.log(level, "[session %s] "+format, append([]interface{}{s.id}, args...)...) id := hex.EncodeToString(s.uuid[:4])
s.parent.log(level, "[session %s] "+format, append([]interface{}{id}, args...)...)
} }
// onClose is called by rtspServer. // onClose is called by rtspServer.
@ -357,7 +359,7 @@ func (s *rtspSession) apiReaderDescribe() interface{} {
return struct { return struct {
Type string `json:"type"` Type string `json:"type"`
ID string `json:"id"` ID string `json:"id"`
}{typ, s.id} }{typ, s.uuid.String()}
} }
// apiSourceDescribe implements source. // apiSourceDescribe implements source.
@ -372,7 +374,7 @@ func (s *rtspSession) apiSourceDescribe() interface{} {
return struct { return struct {
Type string `json:"type"` Type string `json:"type"`
ID string `json:"id"` ID string `json:"id"`
}{typ, s.id} }{typ, s.uuid.String()}
} }
// onPacketRTP is called by rtspServer. // onPacketRTP is called by rtspServer.

Loading…
Cancel
Save