- Added functionality to handle blacklisted IPs, allowing connections to remain open until a timeout is reached. - Introduced periodic fetching of admin follow lists to improve synchronization with relay data. - Updated WebSocket message size limits to accommodate larger payloads. - Enhanced logging for better traceability during follow list fetching and event processing. - Refactored event subscription logic to improve clarity and maintainability.
117 lines
3.3 KiB
Go
117 lines
3.3 KiB
Go
package app
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"lol.mleku.dev/chk"
|
|
"lol.mleku.dev/log"
|
|
"next.orly.dev/pkg/encoders/envelopes"
|
|
"next.orly.dev/pkg/encoders/envelopes/authenvelope"
|
|
"next.orly.dev/pkg/encoders/envelopes/closeenvelope"
|
|
"next.orly.dev/pkg/encoders/envelopes/countenvelope"
|
|
"next.orly.dev/pkg/encoders/envelopes/eventenvelope"
|
|
"next.orly.dev/pkg/encoders/envelopes/noticeenvelope"
|
|
"next.orly.dev/pkg/encoders/envelopes/reqenvelope"
|
|
)
|
|
|
|
func (l *Listener) HandleMessage(msg []byte, remote string) {
|
|
// Ignore all messages from self-connections
|
|
if l.isSelfConnection {
|
|
log.D.F("ignoring message from self-connection %s", remote)
|
|
return
|
|
}
|
|
|
|
// Handle blacklisted IPs - discard messages but keep connection open until timeout
|
|
if l.isBlacklisted {
|
|
// Check if timeout has been reached
|
|
if time.Now().After(l.blacklistTimeout) {
|
|
log.W.F("blacklisted IP %s timeout reached, closing connection", remote)
|
|
// Close the connection by cancelling the context
|
|
// The websocket handler will detect this and close the connection
|
|
return
|
|
}
|
|
log.D.F("discarding message from blacklisted IP %s (timeout in %v)", remote, time.Until(l.blacklistTimeout))
|
|
return
|
|
}
|
|
|
|
msgPreview := string(msg)
|
|
if len(msgPreview) > 150 {
|
|
msgPreview = msgPreview[:150] + "..."
|
|
}
|
|
// log.D.F("%s processing message (len=%d): %s", remote, len(msg), msgPreview)
|
|
|
|
l.msgCount++
|
|
var err error
|
|
var t string
|
|
var rem []byte
|
|
|
|
// Attempt to identify the envelope type
|
|
if t, rem, err = envelopes.Identify(msg); err != nil {
|
|
log.E.F(
|
|
"%s envelope identification FAILED (len=%d): %v", remote, len(msg),
|
|
err,
|
|
)
|
|
log.T.F("%s malformed message content: %q", remote, msgPreview)
|
|
chk.E(err)
|
|
// Send error notice to client
|
|
if noticeErr := noticeenvelope.NewFrom("malformed message: " + err.Error()).Write(l); noticeErr != nil {
|
|
log.E.F(
|
|
"%s failed to send malformed message notice: %v", remote,
|
|
noticeErr,
|
|
)
|
|
}
|
|
return
|
|
}
|
|
|
|
log.T.F(
|
|
"%s identified envelope type: %s (payload_len=%d)", remote, t, len(rem),
|
|
)
|
|
|
|
// Process the identified envelope type
|
|
switch t {
|
|
case eventenvelope.L:
|
|
log.T.F("%s processing EVENT envelope", remote)
|
|
l.eventCount++
|
|
err = l.HandleEvent(rem)
|
|
case reqenvelope.L:
|
|
log.T.F("%s processing REQ envelope", remote)
|
|
l.reqCount++
|
|
err = l.HandleReq(rem)
|
|
case closeenvelope.L:
|
|
log.T.F("%s processing CLOSE envelope", remote)
|
|
err = l.HandleClose(rem)
|
|
case authenvelope.L:
|
|
log.T.F("%s processing AUTH envelope", remote)
|
|
err = l.HandleAuth(rem)
|
|
case countenvelope.L:
|
|
log.T.F("%s processing COUNT envelope", remote)
|
|
err = l.HandleCount(rem)
|
|
default:
|
|
err = fmt.Errorf("unknown envelope type %s", t)
|
|
log.E.F(
|
|
"%s unknown envelope type: %s (payload: %q)", remote, t,
|
|
string(rem),
|
|
)
|
|
}
|
|
|
|
// Handle any processing errors
|
|
if err != nil {
|
|
log.E.F("%s message processing FAILED (type=%s): %v", remote, t, err)
|
|
log.T.F("%s error context - original message: %q", remote, msgPreview)
|
|
|
|
// Send error notice to client
|
|
noticeMsg := fmt.Sprintf("%s: %s", t, err.Error())
|
|
if noticeErr := noticeenvelope.NewFrom(noticeMsg).Write(l); noticeErr != nil {
|
|
log.E.F(
|
|
"%s failed to send error notice after %s processing failure: %v",
|
|
remote, t, noticeErr,
|
|
)
|
|
return
|
|
}
|
|
log.T.F("%s sent error notice for %s processing failure", remote, t)
|
|
} else {
|
|
log.T.F("%s message processing SUCCESS (type=%s)", remote, t)
|
|
}
|
|
}
|