Browse Source

Added attestation data.

pull/51/head
Simon Eisenmann 11 years ago
parent
commit
b65d2e10df
  1. 1
      src/app/spreed-webrtc-server/channeling.go
  2. 19
      src/app/spreed-webrtc-server/hub.go
  3. 52
      src/app/spreed-webrtc-server/server.go
  4. 45
      src/app/spreed-webrtc-server/session.go
  5. 6
      static/js/mediastream/api.js
  6. 50
      static/js/services/buddydata.js

1
src/app/spreed-webrtc-server/channeling.go

@ -158,6 +158,7 @@ type DataOutgoing struct { @@ -158,6 +158,7 @@ type DataOutgoing struct {
From string
To string
Iid string `json:",omitempty"`
A string `json:",omitempty"`
}
type DataSessions struct {

19
src/app/spreed-webrtc-server/hub.go

@ -46,6 +46,7 @@ const ( @@ -46,6 +46,7 @@ const (
maxUsersLength = 5000
)
// TODO(longsleep): Get rid of MessageRequest type.
type MessageRequest struct {
From string
To string
@ -79,6 +80,7 @@ type Hub struct { @@ -79,6 +80,7 @@ type Hub struct {
encryptionSecret []byte
turnSecret []byte
tickets *securecookie.SecureCookie
attestations *securecookie.SecureCookie
count uint64
mutex sync.RWMutex
buffers BufferCache
@ -114,11 +116,14 @@ func NewHub(version string, config *Config, sessionSecret, encryptionSecret, tur @@ -114,11 +116,14 @@ func NewHub(version string, config *Config, sessionSecret, encryptionSecret, tur
h.tickets.MaxAge(86400 * 30) // 30 days
h.tickets.HashFunc(sha256.New)
h.tickets.BlockFunc(aes.NewCipher)
h.attestations = securecookie.New(h.sessionSecret, nil)
h.attestations.MaxAge(300) // 5 minutes
h.tickets.HashFunc(sha256.New)
h.buffers = NewBufferCache(1024, bytes.MinRead)
h.buddyImages = NewImageCache()
h.tokenName = fmt.Sprintf("token@%s", h.realm)
h.contacts = securecookie.New(h.sessionSecret, h.encryptionSecret)
h.contacts.MaxAge(0)
h.contacts.MaxAge(0) // Forever
h.contacts.HashFunc(sha256.New)
h.contacts.BlockFunc(aes.NewCipher)
return h
@ -226,6 +231,8 @@ func (h *Hub) CreateSession(request *http.Request, st *SessionToken) *Session { @@ -226,6 +231,8 @@ func (h *Hub) CreateSession(request *http.Request, st *SessionToken) *Session {
session = NewSession(st.Id, st.Sid)
}
h.EncodeAttestation(session)
if userid != "" {
h.authenticateHandler(session, st, userid)
}
@ -250,6 +257,16 @@ func (h *Hub) ValidateSession(id, sid string) bool { @@ -250,6 +257,16 @@ func (h *Hub) ValidateSession(id, sid string) bool {
}
func (h *Hub) EncodeAttestation(session *Session) (string, error) {
attestation, err := h.attestations.Encode("attestation", session.Id)
if err == nil {
session.UpdateAttestation(attestation)
}
return attestation, err
}
func (h *Hub) EncodeSessionToken(st *SessionToken) (string, error) {
return h.tickets.Encode(h.tokenName, st)

52
src/app/spreed-webrtc-server/server.go

@ -180,7 +180,7 @@ func (s *Server) Unicast(c *Connection, to string, m interface{}) { @@ -180,7 +180,7 @@ func (s *Server) Unicast(c *Connection, to string, m interface{}) {
b := c.h.buffers.New()
encoder := json.NewEncoder(b)
err := encoder.Encode(&DataOutgoing{From: c.Id, To: to, Data: m})
err := encoder.Encode(&DataOutgoing{From: c.Id, To: to, Data: m, A: c.Session.Attestation()})
if err != nil {
b.Decref()
log.Println("Unicast error while encoding JSON", err)
@ -192,6 +192,31 @@ func (s *Server) Unicast(c *Connection, to string, m interface{}) { @@ -192,6 +192,31 @@ func (s *Server) Unicast(c *Connection, to string, m interface{}) {
b.Decref()
}
func (s *Server) Broadcast(c *Connection, m interface{}) {
b := c.h.buffers.New()
encoder := json.NewEncoder(b)
err := encoder.Encode(&DataOutgoing{From: c.Id, Data: m, A: c.Session.Attestation()})
if err != nil {
b.Decref()
log.Println("Broadcast error while encoding JSON", err)
return
}
if c.h.isGlobalRoomid(c.Roomid) {
c.h.RunForAllRooms(func(room *RoomWorker) {
var msg = &MessageRequest{From: c.Id, Message: b, Id: room.Id}
room.broadcastHandler(msg)
})
} else {
var msg = &MessageRequest{From: c.Id, Message: b, Id: c.Roomid}
room := c.h.GetRoom(c.Roomid)
room.broadcastHandler(msg)
}
b.Decref()
}
func (s *Server) Alive(c *Connection, alive *DataAlive, iid string) {
c.h.aliveHandler(c, alive, iid)
@ -217,31 +242,6 @@ func (s *Server) ContactRequest(c *Connection, to string, cr *DataContactRequest @@ -217,31 +242,6 @@ func (s *Server) ContactRequest(c *Connection, to string, cr *DataContactRequest
}
func (s *Server) Broadcast(c *Connection, m interface{}) {
b := c.h.buffers.New()
encoder := json.NewEncoder(b)
err := encoder.Encode(&DataOutgoing{From: c.Id, Data: m})
if err != nil {
b.Decref()
log.Println("Broadcast error while encoding JSON", err)
return
}
if c.h.isGlobalRoomid(c.Roomid) {
c.h.RunForAllRooms(func(room *RoomWorker) {
var msg = &MessageRequest{From: c.Id, Message: b, Id: room.Id}
room.broadcastHandler(msg)
})
} else {
var msg = &MessageRequest{From: c.Id, Message: b, Id: c.Roomid}
room := c.h.GetRoom(c.Roomid)
room.broadcastHandler(msg)
}
b.Decref()
}
func (s *Server) Users(c *Connection) {
room := c.h.GetRoom(c.Roomid)

45
src/app/spreed-webrtc-server/session.go

@ -32,16 +32,17 @@ import ( @@ -32,16 +32,17 @@ import (
var sessionNonces *securecookie.SecureCookie
type Session struct {
Id string
Sid string
Ua string
UpdateRev uint64
Status interface{}
Nonce string
Prio int
mutex sync.RWMutex
userid string
stamp int64
Id string
Sid string
Ua string
UpdateRev uint64
Status interface{}
Nonce string
Prio int
mutex sync.RWMutex
userid string
stamp int64
attestation string
}
func NewSession(id, sid string) *Session {
@ -153,9 +154,29 @@ func (s *Session) Data() *DataSession { @@ -153,9 +154,29 @@ func (s *Session) Data() *DataSession {
}
func (s *Session) Userid() string {
func (s *Session) Userid() (userid string) {
return s.userid
s.mutex.RLock()
userid = s.userid
s.mutex.RUnlock()
return
}
func (s *Session) Attestation() (attestation string) {
s.mutex.RLock()
attestation = s.attestation
s.mutex.RUnlock()
return
}
func (s *Session) UpdateAttestation(attestation string) {
s.mutex.Lock()
s.attestation = attestation
s.mutex.Unlock()
}

6
static/js/mediastream/api.js

@ -121,10 +121,16 @@ define(['jquery', 'underscore'], function($, _) { @@ -121,10 +121,16 @@ define(['jquery', 'underscore'], function($, _) {
this.last_receive = now;
this.last_receive_overdue = false;
var attestation = d.A;
var iid = d.Iid;
var data = d.Data;
var dataType = data.Type;
if (attestation && d.From) {
// Trigger received attestations.
this.e.triggerHandler("received.attestation", [d.From, attestation]);
}
if (iid) {
// Shortcut for iid registered responses.
this.e.triggerHandler(iid+".request", [dataType, data]);

50
static/js/services/buddydata.js

@ -21,11 +21,12 @@ @@ -21,11 +21,12 @@
define(['underscore'], function(underscore) {
// buddyData
return ["contactData", function(contactData) {
return ["contactData", "mediaStream", function(contactData, mediaStream) {
var scopes = {};
var brain = {};
var pushed = {};
var attestations = {};
var count = 0;
var buddyData = {
@ -131,8 +132,55 @@ define(['underscore'], function(underscore) { @@ -131,8 +132,55 @@ define(['underscore'], function(underscore) {
},
set: function(id, scope) {
scopes[id] = scope;
},
attestation: function(id) {
var data = attestations[id];
if (data) {
return data.a;
}
return null;
}
};
// attestation support
(function() {
// Listen for attestation events.
mediaStream.api.e.on("received.attestation", function(event, from, attestation) {
var current = attestations[from];
var create = false;
if (!current) {
create = true;
} else {
if (current.a !== attestation) {
create = true;
}
}
if (create) {
//console.log("Created attestation entry", from);
attestations[from] = {
a: attestation,
t: (new Date().getTime())
}
}
});
var expire = function() {
var expired = (new Date().getTime()) - 240000;
_.each(attestations, function(data, id) {
if (data.t < expired) {
delete attestations[id];
//console.log("expired attestation", id);
}
})
setTimeout(expire, 120000);
};
expire();
})();
return buddyData;
}];

Loading…
Cancel
Save