|
|
|
@ -24,6 +24,7 @@ import (
@@ -24,6 +24,7 @@ import (
|
|
|
|
|
"bytes" |
|
|
|
|
"crypto/hmac" |
|
|
|
|
"crypto/sha1" |
|
|
|
|
"crypto/sha256" |
|
|
|
|
"encoding/base64" |
|
|
|
|
"encoding/json" |
|
|
|
|
"fmt" |
|
|
|
@ -68,6 +69,7 @@ type Hub struct {
@@ -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 {
@@ -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 {
@@ -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} |
|
|
|
|