diff --git a/server.conf.in b/server.conf.in index 07bf5f80..21983109 100644 --- a/server.conf.in +++ b/server.conf.in @@ -16,6 +16,7 @@ listen = 127.0.0.1:8080 #stunURIs = stun.l.google.com:19302 #turnURIs = turn:turnserver:port?transport=udp turn:anotherturnserver:port?transport=tcp turns:turnserver:443?transport=tcp #turnSecret = the-default-turn-shared-secret-do-not-keep +#turnUsernameFormat = id:time # Set the format for turn username generator. Valid values are time:id, id:time (default). sessionSecret = the-default-secret-do-not-keep #tokenFile = tokens.txt # If set, everyone needs to give one of the tokens to launch the web client. One token per line in the file. #globalRoom = global # Enables a global room. Users in that room are in all rooms. diff --git a/src/app/spreed-speakfreely-server/hub.go b/src/app/spreed-speakfreely-server/hub.go index 2a2ea3f9..e38aa096 100644 --- a/src/app/spreed-speakfreely-server/hub.go +++ b/src/app/spreed-speakfreely-server/hub.go @@ -24,6 +24,7 @@ import ( "bytes" "crypto/hmac" "crypto/sha1" + "crypto/sha256" "encoding/base64" "encoding/json" "fmt" @@ -68,6 +69,7 @@ type Hub struct { config *Config sessionSecret []byte turnSecret []byte + turnUsernameFormat string tickets *securecookie.SecureCookie count uint64 mutex sync.RWMutex @@ -76,16 +78,17 @@ type Hub struct { unicastChatMessages uint64 } -func NewHub(version string, config *Config, sessionSecret string, turnSecret string) *Hub { +func NewHub(version string, config *Config, sessionSecret, turnSecret, turnUsernameFormat string) *Hub { h := &Hub{ - connectionTable: make(map[string]*Connection), - userTable: make(map[string]*User), - roomTable: make(map[string]*RoomWorker), - version: version, - config: config, - sessionSecret: []byte(sessionSecret), - turnSecret: []byte(turnSecret), + connectionTable: make(map[string]*Connection), + userTable: make(map[string]*User), + roomTable: make(map[string]*RoomWorker), + version: version, + config: config, + sessionSecret: []byte(sessionSecret), + turnSecret: []byte(turnSecret), + turnUsernameFormat: turnUsernameFormat, } h.tickets = securecookie.New(h.sessionSecret, nil) @@ -138,8 +141,18 @@ func (h *Hub) CreateTurnData(id string) *DataTurn { if len(h.turnSecret) == 0 { return &DataTurn{} } + var user string + bar := sha256.New() + bar.Write([]byte(id)) + id = base64.StdEncoding.EncodeToString(bar.Sum(nil)) foo := hmac.New(sha1.New, h.turnSecret) - user := fmt.Sprintf("%s:%d", id, int32(time.Now().Unix())) + now := int32(time.Now().Unix()) + switch h.turnUsernameFormat { + case "time:id": + user = fmt.Sprintf("%d:%s", now, id) + default: + user = fmt.Sprintf("%s:%d", id, now) + } foo.Write([]byte(user)) password := base64.StdEncoding.EncodeToString(foo.Sum(nil)) return &DataTurn{user, password, turnTTL, h.config.TurnURIs} diff --git a/src/app/spreed-speakfreely-server/main.go b/src/app/spreed-speakfreely-server/main.go index 9265af23..c7cd406d 100644 --- a/src/app/spreed-speakfreely-server/main.go +++ b/src/app/spreed-speakfreely-server/main.go @@ -211,6 +211,11 @@ func runner(runtime phoenix.Runtime) error { turnSecret = "" } + turnUsernameFormat, err := runtime.GetString("app", "turnUsernameFormat") + if err != nil { + turnUsernameFormat = "id:time" + } + stunURIsString, err := runtime.GetString("app", "stunURIs") if err != nil { stunURIsString = "" @@ -269,7 +274,7 @@ func runner(runtime phoenix.Runtime) error { } // Create our hub instance. - hub := NewHub(runtimeVersion, config, sessionSecret, turnSecret) + hub := NewHub(runtimeVersion, config, sessionSecret, turnSecret, turnUsernameFormat) // Set number of go routines if it is 1 if goruntime.GOMAXPROCS(0) == 1 {