mirror of
https://github.com/status-im/status-go.git
synced 2025-01-18 10:42:07 +00:00
Ensured time was a value and implemented explicit RekeyCommunity()
This commit is contained in:
parent
03e28eea0d
commit
a06984aeec
@ -1967,6 +1967,11 @@ func (o *Community) SetActiveMembersCount(activeMembersCount uint64) (updated bo
|
|||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RekeyedAt returns the RekeyedAt value from the underlying Community.config
|
||||||
|
func (o *Community) RekeyedAt() time.Time {
|
||||||
|
return o.config.RekeyedAt
|
||||||
|
}
|
||||||
|
|
||||||
type sortSlice []sorterHelperIdx
|
type sortSlice []sorterHelperIdx
|
||||||
type sorterHelperIdx struct {
|
type sorterHelperIdx struct {
|
||||||
pos int32
|
pos int32
|
||||||
|
@ -84,6 +84,7 @@ type Manager struct {
|
|||||||
torrentTasks map[string]metainfo.Hash
|
torrentTasks map[string]metainfo.Hash
|
||||||
historyArchiveDownloadTasks map[string]*HistoryArchiveDownloadTask
|
historyArchiveDownloadTasks map[string]*HistoryArchiveDownloadTask
|
||||||
stopped bool
|
stopped bool
|
||||||
|
RekeyInterval *time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
type HistoryArchiveDownloadTask struct {
|
type HistoryArchiveDownloadTask struct {
|
||||||
@ -4028,3 +4029,15 @@ func (m *Manager) saveAndPublish(community *Community) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetRekeyedAtClock returns the rekeyed_at time of a given community
|
||||||
|
// exposes persistence level functionality to exported Manager APIs
|
||||||
|
func (m *Manager) GetRekeyedAtClock(id []byte) (time.Time, error) {
|
||||||
|
return m.persistence.GetRekeyedAtClock(id)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetRekeyedAtClock sets the rekeyed_at time value of a given community
|
||||||
|
// exposes persistence level functionality to exported Manager APIs
|
||||||
|
func (m *Manager) SetRekeyedAtClock(id []byte, time time.Time) error {
|
||||||
|
return m.persistence.SetRekeyedAtClock(id, time)
|
||||||
|
}
|
||||||
|
@ -1273,17 +1273,16 @@ func decodeEventsData(eventsBytes []byte, eventsDescriptionBytes []byte) (*Event
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetRekeyedAtClock returns the rekeyed_at time of a given community
|
// GetRekeyedAtClock returns the rekeyed_at time of a given community
|
||||||
func (p *Persistence) GetRekeyedAtClock(id []byte) (*time.Time, error) {
|
func (p *Persistence) GetRekeyedAtClock(id []byte) (rekeyedAt time.Time, err error) {
|
||||||
rekeyedAt := time.Time{}
|
err = p.db.QueryRow(`SELECT rekeyed_at FROM communities_communities WHERE id = ?`, id).Scan(&rekeyedAt)
|
||||||
err := p.db.QueryRow(`SELECT rekeyed_at FROM communities_communities WHERE id = ?`, id).Scan(&rekeyedAt)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return rekeyedAt, err
|
||||||
}
|
}
|
||||||
return &rekeyedAt, nil
|
return rekeyedAt, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetRekeyedAtClock sets the rekeyed_at time value of a given community
|
// SetRekeyedAtClock sets the rekeyed_at time value of a given community
|
||||||
func (p *Persistence) SetRekeyedAtClock(id []byte, time *time.Time) error {
|
func (p *Persistence) SetRekeyedAtClock(id []byte, time time.Time) error {
|
||||||
_, err := p.db.Exec(`UPDATE communities_communities SET rekeyed_at = ? WHERE id = ? AND rekeyed_at < ?`, time, id, time)
|
_, err := p.db.Exec(`UPDATE communities_communities SET rekeyed_at = ? WHERE id = ? AND rekeyed_at < ?`, time, id, time)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -2032,6 +2032,25 @@ func (m *Messenger) SendKeyExchangeMessage(communityID []byte, pubkeys []*ecdsa.
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RekeyCommunity takes a communities.Community and triggers a force rekey event,
|
||||||
|
// after rekeying has be triggered updates the communities.Community rekeyed_at value to now
|
||||||
|
func (m *Messenger) RekeyCommunity(cID types.HexBytes) error {
|
||||||
|
// Get the community as the member list could have changed
|
||||||
|
c, err := m.GetCommunityByID(cID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// RekeyCommunity
|
||||||
|
err = m.SendKeyExchangeMessage(c.ID(), c.GetMemberPubkeys(), common.KeyExMsgRekey)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// update rekey timestamp
|
||||||
|
return m.communitiesManager.SetRekeyedAtClock(c.ID(), time.Now())
|
||||||
|
}
|
||||||
|
|
||||||
func (m *Messenger) UnbanUserFromCommunity(request *requests.UnbanUserFromCommunity) (*MessengerResponse, error) {
|
func (m *Messenger) UnbanUserFromCommunity(request *requests.UnbanUserFromCommunity) (*MessengerResponse, error) {
|
||||||
community, err := m.communitiesManager.UnbanUserFromCommunity(request)
|
community, err := m.communitiesManager.UnbanUserFromCommunity(request)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -4210,9 +4229,13 @@ func chunkAttachmentsByByteSize(slice []*protobuf.DiscordMessageAttachment, maxF
|
|||||||
func (m *Messenger) startCommunityRekeyLoop() error {
|
func (m *Messenger) startCommunityRekeyLoop() error {
|
||||||
logger := m.logger.Named("CommunityRekeyLoop")
|
logger := m.logger.Named("CommunityRekeyLoop")
|
||||||
|
|
||||||
rekeyInterval := time.Hour
|
var rki time.Duration
|
||||||
|
if m.communitiesManager.RekeyInterval == nil {
|
||||||
ticker := time.NewTicker(rekeyInterval)
|
rki = time.Hour
|
||||||
|
} else {
|
||||||
|
rki = *m.communitiesManager.RekeyInterval
|
||||||
|
}
|
||||||
|
ticker := time.NewTicker(rki)
|
||||||
|
|
||||||
cs, err := m.Communities()
|
cs, err := m.Communities()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -4221,24 +4244,24 @@ func (m *Messenger) startCommunityRekeyLoop() error {
|
|||||||
|
|
||||||
for _, c := range cs {
|
for _, c := range cs {
|
||||||
if c.IsAdmin() && c.Encrypted() {
|
if c.IsAdmin() && c.Encrypted() {
|
||||||
// get last rekey timestamp
|
// if rekey time period exceeded, perform a rekey
|
||||||
c.config.RekeyedAt.Before(time.Now().Add(rekeyInterval))
|
if c.RekeyedAt().Add(rki).Before(time.Now()) {
|
||||||
|
err = m.RekeyCommunity(c.ID())
|
||||||
// if rekey time period exceeded perform a rekey and set new last rekey timestamp to now
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
logger.Debug("CommunityRekeyLoop started", zap.Binary("community ID", c.ID()))
|
logger.Debug("CommunityRekeyLoop started", zap.Binary("community ID", c.ID()))
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
// Rekey
|
err = m.RekeyCommunity(c.ID())
|
||||||
err = m.SendKeyExchangeMessage(c.ID(), c.GetMemberPubkeys(), common.KeyExMsgRekey)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("error sending rekey message", zap.Error(err), zap.Binary("community ID", c.ID()))
|
logger.Error("error sending rekey message", zap.Error(err), zap.Binary("community ID", c.ID()))
|
||||||
}
|
}
|
||||||
|
|
||||||
// update rekey timestamp
|
|
||||||
|
|
||||||
case <-m.quit:
|
case <-m.quit:
|
||||||
ticker.Stop()
|
ticker.Stop()
|
||||||
logger.Debug("CommunityRekeyLoop stopped", zap.Binary("community ID", c.ID()))
|
logger.Debug("CommunityRekeyLoop stopped", zap.Binary("community ID", c.ID()))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user