forked from cwtch.im/cwtch
Correctly Handle Messages from Unknown Peers
This commit is contained in:
parent
c414a518d6
commit
62866da84c
|
@ -573,6 +573,9 @@ func (cp *cwtchPeer) Shutdown() {
|
|||
}
|
||||
|
||||
func (cp *cwtchPeer) StoreMessage(onion string, messageTxt string, sent time.Time) {
|
||||
if cp.GetContact(onion) == nil {
|
||||
cp.AddContact(onion, onion, model.AuthUnknown)
|
||||
}
|
||||
cp.mutex.Lock()
|
||||
cp.Profile.AddMessageToContactTimeline(onion, messageTxt, sent)
|
||||
cp.mutex.Unlock()
|
||||
|
@ -620,6 +623,8 @@ func (cp *cwtchPeer) eventHandler() {
|
|||
|
||||
log.Debugf("NewGetValMessageFromPeer for %v%v from %v\n", scope, path, onion)
|
||||
|
||||
remotePeer := cp.GetContact(onion)
|
||||
if remotePeer != nil && remotePeer.Authorization == model.AuthApproved {
|
||||
if scope == attr.PublicScope {
|
||||
val, exists := cp.GetAttribute(attr.GetPublicScope(path))
|
||||
resp := event.NewEvent(event.SendRetValMessageToPeer, map[event.Field]string{event.RemotePeer: onion, event.Exists: strconv.FormatBool(exists)})
|
||||
|
@ -633,6 +638,7 @@ func (cp *cwtchPeer) eventHandler() {
|
|||
|
||||
cp.eventBus.Publish(resp)
|
||||
}
|
||||
}
|
||||
|
||||
/***** Non default but requestable handlable events *****/
|
||||
|
||||
|
|
|
@ -195,18 +195,22 @@ func (e *engine) isBlocked(onion string) bool {
|
|||
return authorization.(model.Authorization) == model.AuthBlocked
|
||||
}
|
||||
|
||||
func (e *engine) isApproved(onion string) bool {
|
||||
func (e *engine) isAllowed(onion string) bool {
|
||||
authorization, known := e.authorizations.Load(onion)
|
||||
if !known {
|
||||
log.Errorf("attempted to lookup authorization of onion not in map...that should never happen")
|
||||
return false
|
||||
}
|
||||
if e.blockUnknownContacts {
|
||||
return authorization.(model.Authorization) == model.AuthApproved
|
||||
}
|
||||
return authorization.(model.Authorization) != model.AuthBlocked
|
||||
}
|
||||
|
||||
func (e *engine) createPeerTemplate() *PeerApp {
|
||||
peerAppTemplate := new(PeerApp)
|
||||
peerAppTemplate.IsBlocked = e.isBlocked
|
||||
peerAppTemplate.IsApproved = e.isApproved
|
||||
peerAppTemplate.IsAllowed = e.isAllowed
|
||||
peerAppTemplate.MessageHandler = e.handlePeerMessage
|
||||
peerAppTemplate.OnAcknowledgement = e.ignoreOnShutdown2(e.peerAck)
|
||||
peerAppTemplate.OnAuth = e.ignoreOnShutdown(e.peerAuthed)
|
||||
|
|
|
@ -18,7 +18,7 @@ type PeerApp struct {
|
|||
MessageHandler func(string, string, string, []byte)
|
||||
RetValHandler func(string, []byte, []byte)
|
||||
IsBlocked func(string) bool
|
||||
IsApproved func(string) bool
|
||||
IsAllowed func(string) bool
|
||||
OnAcknowledgement func(string, string)
|
||||
OnAuth func(string)
|
||||
OnClose func(string)
|
||||
|
@ -48,7 +48,7 @@ func (pa *PeerApp) NewInstance() tapir.Application {
|
|||
newApp := new(PeerApp)
|
||||
newApp.MessageHandler = pa.MessageHandler
|
||||
newApp.IsBlocked = pa.IsBlocked
|
||||
newApp.IsApproved = pa.IsApproved
|
||||
newApp.IsAllowed = pa.IsAllowed
|
||||
newApp.OnAcknowledgement = pa.OnAcknowledgement
|
||||
newApp.OnAuth = pa.OnAuth
|
||||
newApp.OnClose = pa.OnClose
|
||||
|
@ -101,7 +101,7 @@ func (pa *PeerApp) listen() {
|
|||
pa.getValRequests.Delete(peerMessage.ID)
|
||||
}
|
||||
default:
|
||||
if pa.IsApproved(pa.connection.Hostname()) {
|
||||
if pa.IsAllowed(pa.connection.Hostname()) {
|
||||
pa.MessageHandler(pa.connection.Hostname(), peerMessage.ID, peerMessage.Context, peerMessage.Data)
|
||||
|
||||
// Acknowledge the message
|
||||
|
|
Loading…
Reference in New Issue