|
|
|
@ -46,6 +46,7 @@ const ( |
|
|
|
maxUsersLength = 5000 |
|
|
|
maxUsersLength = 5000 |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO(longsleep): Get rid of MessageRequest type.
|
|
|
|
type MessageRequest struct { |
|
|
|
type MessageRequest struct { |
|
|
|
From string |
|
|
|
From string |
|
|
|
To string |
|
|
|
To string |
|
|
|
@ -79,6 +80,7 @@ type Hub struct { |
|
|
|
encryptionSecret []byte |
|
|
|
encryptionSecret []byte |
|
|
|
turnSecret []byte |
|
|
|
turnSecret []byte |
|
|
|
tickets *securecookie.SecureCookie |
|
|
|
tickets *securecookie.SecureCookie |
|
|
|
|
|
|
|
attestations *securecookie.SecureCookie |
|
|
|
count uint64 |
|
|
|
count uint64 |
|
|
|
mutex sync.RWMutex |
|
|
|
mutex sync.RWMutex |
|
|
|
buffers BufferCache |
|
|
|
buffers BufferCache |
|
|
|
@ -114,11 +116,14 @@ func NewHub(version string, config *Config, sessionSecret, encryptionSecret, tur |
|
|
|
h.tickets.MaxAge(86400 * 30) // 30 days
|
|
|
|
h.tickets.MaxAge(86400 * 30) // 30 days
|
|
|
|
h.tickets.HashFunc(sha256.New) |
|
|
|
h.tickets.HashFunc(sha256.New) |
|
|
|
h.tickets.BlockFunc(aes.NewCipher) |
|
|
|
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.buffers = NewBufferCache(1024, bytes.MinRead) |
|
|
|
h.buddyImages = NewImageCache() |
|
|
|
h.buddyImages = NewImageCache() |
|
|
|
h.tokenName = fmt.Sprintf("token@%s", h.realm) |
|
|
|
h.tokenName = fmt.Sprintf("token@%s", h.realm) |
|
|
|
h.contacts = securecookie.New(h.sessionSecret, h.encryptionSecret) |
|
|
|
h.contacts = securecookie.New(h.sessionSecret, h.encryptionSecret) |
|
|
|
h.contacts.MaxAge(0) |
|
|
|
h.contacts.MaxAge(0) // Forever
|
|
|
|
h.contacts.HashFunc(sha256.New) |
|
|
|
h.contacts.HashFunc(sha256.New) |
|
|
|
h.contacts.BlockFunc(aes.NewCipher) |
|
|
|
h.contacts.BlockFunc(aes.NewCipher) |
|
|
|
return h |
|
|
|
return h |
|
|
|
@ -214,7 +219,7 @@ func (h *Hub) CreateSession(request *http.Request, st *SessionToken) *Session { |
|
|
|
if st == nil { |
|
|
|
if st == nil { |
|
|
|
sid := NewRandomString(32) |
|
|
|
sid := NewRandomString(32) |
|
|
|
id, _ := h.tickets.Encode("id", sid) |
|
|
|
id, _ := h.tickets.Encode("id", sid) |
|
|
|
session = NewSession(id, sid) |
|
|
|
session = NewSession(h, id, sid) |
|
|
|
log.Println("Created new session id", len(id), id, sid) |
|
|
|
log.Println("Created new session id", len(id), id, sid) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
if userid == "" { |
|
|
|
if userid == "" { |
|
|
|
@ -223,7 +228,7 @@ func (h *Hub) CreateSession(request *http.Request, st *SessionToken) *Session { |
|
|
|
if !usersEnabled { |
|
|
|
if !usersEnabled { |
|
|
|
userid = "" |
|
|
|
userid = "" |
|
|
|
} |
|
|
|
} |
|
|
|
session = NewSession(st.Id, st.Sid) |
|
|
|
session = NewSession(h, st.Id, st.Sid) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if userid != "" { |
|
|
|
if userid != "" { |
|
|
|
@ -250,6 +255,25 @@ 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) DecodeAttestation(token string) (string, error) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var id string |
|
|
|
|
|
|
|
err := h.attestations.Decode("attestation", token, &id) |
|
|
|
|
|
|
|
return id, err |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}*/ |
|
|
|
|
|
|
|
|
|
|
|
func (h *Hub) EncodeSessionToken(st *SessionToken) (string, error) { |
|
|
|
func (h *Hub) EncodeSessionToken(st *SessionToken) (string, error) { |
|
|
|
|
|
|
|
|
|
|
|
return h.tickets.Encode(h.tokenName, st) |
|
|
|
return h.tickets.Encode(h.tokenName, st) |
|
|
|
@ -448,13 +472,27 @@ func (h *Hub) sessionsHandler(c *Connection, srq *DataSessionsRequest, iid strin |
|
|
|
} |
|
|
|
} |
|
|
|
// Find foreign user.
|
|
|
|
// Find foreign user.
|
|
|
|
h.mutex.RLock() |
|
|
|
h.mutex.RLock() |
|
|
|
defer h.mutex.RUnlock() |
|
|
|
|
|
|
|
user, ok := h.userTable[userid] |
|
|
|
user, ok := h.userTable[userid] |
|
|
|
|
|
|
|
h.mutex.RUnlock() |
|
|
|
if !ok { |
|
|
|
if !ok { |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
// Add sessions for forein user.
|
|
|
|
// Add sessions for forein user.
|
|
|
|
users = user.SessionsData() |
|
|
|
users = user.SessionsData() |
|
|
|
|
|
|
|
case "session": |
|
|
|
|
|
|
|
id, err := c.Session.attestation.Decode(srq.Token) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
log.Println("Failed to decode incoming attestation", err, srq.Token) |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
h.mutex.RLock() |
|
|
|
|
|
|
|
session, ok := h.sessionTable[id] |
|
|
|
|
|
|
|
h.mutex.RUnlock() |
|
|
|
|
|
|
|
if !ok { |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
users = make([]*DataSession, 1, 1) |
|
|
|
|
|
|
|
users[0] = session.Data() |
|
|
|
default: |
|
|
|
default: |
|
|
|
log.Println("Unkown incoming sessions request type", srq.Type) |
|
|
|
log.Println("Unkown incoming sessions request type", srq.Type) |
|
|
|
} |
|
|
|
} |
|
|
|
|