peers separate
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
ycc
2024-05-28 16:47:04 +02:00
parent c1883f1524
commit e674a0cb33
11 changed files with 164 additions and 79 deletions

View File

@ -21,7 +21,7 @@ type Identity struct {
Avatars []Avatar `json:"avatars,omitempty"`
RootKp meowlib.KeyPair `json:"id_kp,omitempty"`
Status string `json:"status,omitempty"`
Peers PeerList `json:"peers,omitempty"`
Peers PeerStorage `json:"peers,omitempty"`
HiddenPeers [][]byte `json:"hidden_peers,omitempty"`
Personae PeerList `json:"faces,omitempty"`
Device meowlib.KeyPair `json:"device,omitempty"`
@ -96,7 +96,7 @@ func (id *Identity) InvitePeer(MyName string, ContactName string, MessageServerU
peer.MyPullServers = MessageServerUids
peer.MyName = MyName
peer.InvitationMessage = InvitationMessage
id.Peers = append(id.Peers, &peer)
id.Peers.StorePeer(&peer)
return &peer, nil
}
@ -104,14 +104,15 @@ func (id *Identity) InvitePeer(MyName string, ContactName string, MessageServerU
// Checks if the received contact card is an answer to an invitation, returns true if it is, and the proposed and received nicknames
func (id *Identity) CheckInvitation(ReceivedContact *meowlib.ContactCard) (isAnswer bool, proposedNick string, receivedNick string, invitationMessage string) {
// invitation Id found, this is an answer to an invitation
for _, p := range id.Peers {
/*for _, p := range id.Peers {
if p.InvitationId == ReceivedContact.InvitationId {
return true, p.Name, ReceivedContact.Name, ReceivedContact.InvitationMessage
}
}
// it's an invitation
return false, "", ReceivedContact.Name, ReceivedContact.InvitationMessage
return false, "", ReceivedContact.Name, ReceivedContact.InvitationMessage*/
return id.Peers.CheckInvitation(ReceivedContact)
}
// Answers an invitation, returns the newly created peer including infos to provide a ContactCard
@ -146,14 +147,14 @@ func (id *Identity) AnswerInvitation(MyName string, ContactName string, MessageS
peer.MyPullServers = MessageServerIdxs
peer.MyName = MyName
peer.InvitationId = ReceivedContact.InvitationId
id.Peers = append(id.Peers, &peer)
id.Peers.StorePeer(&peer)
return &peer
}
// Finalizes an invitation, returns nil if successful
func (id *Identity) FinalizeInvitation(ReceivedContact *meowlib.ContactCard) error {
for i, p := range id.Peers {
/*for i, p := range id.Peers {
if p.InvitationId == ReceivedContact.InvitationId {
//id.Peers[i].Name = ReceivedContact.Name
id.Peers[i].ContactEncryption = ReceivedContact.EncryptionPublicKey
@ -167,7 +168,9 @@ func (id *Identity) FinalizeInvitation(ReceivedContact *meowlib.ContactCard) err
return nil
}
}
return errors.New("no matching contact found for invitationId " + ReceivedContact.InvitationId)
return errors.New("no matching contact found for invitationId " + ReceivedContact.InvitationId)*/
return id.Peers.FinalizeInvitation(ReceivedContact)
}
// LoadIdentity loads an identity from an encrypted file
@ -225,6 +228,7 @@ func (id *Identity) TryUnlockHidden(password string) error {
return errors.New("no peer found")
}
/*
func (id *Identity) HidePeer(peerIdx int, password string) error {
serializedPeer, err := json.Marshal(id.Peers[peerIdx])
if err != nil {
@ -239,13 +243,14 @@ func (id *Identity) HidePeer(peerIdx int, password string) error {
// remove clear text peer
id.Peers = append(id.Peers[:peerIdx], id.Peers[peerIdx+1:]...)
return nil
}
}*/
func (id *Identity) generateRandomHiddenStuff() {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
count := r.Intn(maxHiddenCount) + 1
for i := 1; i < count; i++ {
var p Peer
p.Uid = uuid.New().String()
p.Name = randomLenString(4, 20)
p.MyEncryptionKp = meowlib.NewKeyPair()
p.MyIdentity = meowlib.NewKeyPair()
@ -254,9 +259,10 @@ func (id *Identity) generateRandomHiddenStuff() {
p.ContactPublicKey = p.MyLookupKp.Public
p.ContactEncryption = p.MyIdentity.Public
p.ContactLookupKey = p.MyEncryptionKp.Public
p.dbPassword = randomLenString(8, 14)
// p.Contact.AddUrls([]string{randomLenString(14, 60), randomLenString(14, 60)}) // todo add servers
id.Peers = append(id.Peers, &p)
id.HidePeer(0, randomLenString(8, 14))
id.Peers.StorePeer(&p)
//id.HidePeer(0, randomLenString(8, 14))
// TODO Add random conversations
}
}
@ -285,7 +291,11 @@ func (id *Identity) GetRequestJobs() []*RequestsJob {
srvs[server.GetServerCard().GetUid()] = &rj
}
// add ids to the map
for _, peer := range id.Peers {
peers, err := id.Peers.GetPeers()
if err != nil {
return nil
}
for _, peer := range peers {
// check if peer inviation is accepted
for _, server := range peer.MyPullServers {
srvs[server].LookupKeys = append(srvs[server].LookupKeys, peer.MyLookupKp)