diff --git a/protocol/connections/engine.go b/protocol/connections/engine.go index e3153e1..72c7d97 100644 --- a/protocol/connections/engine.go +++ b/protocol/connections/engine.go @@ -135,6 +135,10 @@ func (e *engine) eventHandler() { func (e *engine) createPeerTemplate() *PeerApp { peerAppTemplate := new(PeerApp) + peerAppTemplate.IsBlocked = func(onion string) bool { + _, blocked := e.blocked.Load(onion) + return blocked + } peerAppTemplate.MessageHandler = e.handlePeerMessage peerAppTemplate.OnAcknowledgement = e.ignoreOnShutdown2(e.peerAck) peerAppTemplate.OnAuth = e.ignoreOnShutdown(e.peerAuthed) @@ -163,11 +167,14 @@ func (e *engine) Shutdown() { // peerWithOnion is the entry point for cwtchPeer relationships // needs to be run in a goroutine as will block on Open. func (e *engine) peerWithOnion(onion string) { - e.ignoreOnShutdown(e.peerConnecting)(onion) - connected, err := e.service.Connect(onion, e.createPeerTemplate()) - // Only issue a disconnected error if we are disconnected (Connect will fail if a connection already exists) - if !connected && err != nil { - e.ignoreOnShutdown(e.peerDisconnected)(onion) + _, blocked := e.blocked.Load(onion) + if !blocked { + e.ignoreOnShutdown(e.peerConnecting)(onion) + connected, err := e.service.Connect(onion, e.createPeerTemplate()) + // Only issue a disconnected error if we are disconnected (Connect will fail if a connection already exists) + if !connected && err != nil { + e.ignoreOnShutdown(e.peerDisconnected)(onion) + } } } diff --git a/protocol/connections/peerapp.go b/protocol/connections/peerapp.go index 581bde5..086e5bb 100644 --- a/protocol/connections/peerapp.go +++ b/protocol/connections/peerapp.go @@ -13,6 +13,7 @@ type PeerApp struct { applications.AuthApp connection *tapir.Connection MessageHandler func(string, string, []byte) + IsBlocked func(string) bool OnAcknowledgement func(string, string) OnAuth func(string) OnClose func(string) @@ -30,6 +31,7 @@ type PeerMessage struct { func (pa PeerApp) NewInstance() tapir.Application { newApp := new(PeerApp) newApp.MessageHandler = pa.MessageHandler + newApp.IsBlocked = pa.IsBlocked newApp.OnAcknowledgement = pa.OnAcknowledgement newApp.OnAuth = pa.OnAuth newApp.OnClose = pa.OnClose @@ -44,9 +46,16 @@ func (pa *PeerApp) Init(connection *tapir.Connection) { pa.AuthApp.Init(connection) if connection.HasCapability(applications.AuthCapability) { + pa.connection = connection - pa.OnAuth(connection.Hostname) - go pa.listen() + + if pa.IsBlocked(connection.Hostname) { + pa.connection.Close() + pa.OnClose(connection.Hostname) + } else { + pa.OnAuth(connection.Hostname) + go pa.listen() + } } else { pa.OnClose(connection.Hostname) }