mirror of
https://github.com/tobychui/zoraxy.git
synced 2025-09-17 01:29:41 +02:00
feat(plugins): Implement event system w/ POC events
Implements the partially created event system with 3 events implemented as proof of concepts. The 3 events are: - `blacklistedIpBlocked`: emitted when a request from a blacklisted IP - `accessRuleCreated`: emitted when a new access rule is created - `blacklistToggled`: emitted when the blacklist is toggled for a given access rule Why these events? Because these are the ones I forsee myself needing in the next version of the zoraxy_crowdsec_bouncer Events are dispatched via a global event manager `plugins.EventSystem.Emit`
This commit is contained in:
@@ -151,6 +151,19 @@ func (m *Manager) StartPlugin(pluginID string) error {
|
||||
thisPlugin.process = cmd
|
||||
thisPlugin.Enabled = true
|
||||
|
||||
// Register event subscriptions
|
||||
if thisPlugin.Spec.SubscriptionsEvents != nil {
|
||||
for eventName := range thisPlugin.Spec.SubscriptionsEvents {
|
||||
eventType := zoraxyPlugin.EventName(eventName)
|
||||
err := EventSystem.Subscribe(thisPlugin.Spec.ID, eventType)
|
||||
if err != nil {
|
||||
m.Log("Failed to subscribe plugin "+thisPlugin.Spec.Name+" to event "+eventName, err)
|
||||
} else {
|
||||
m.Log("Subscribed plugin "+thisPlugin.Spec.Name+" to event "+eventName, nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Create a new static forwarder router for each of the static capture paths
|
||||
thisPlugin.StartAllStaticPathRouters()
|
||||
|
||||
@@ -288,6 +301,11 @@ func (m *Manager) StopPlugin(pluginID string) error {
|
||||
if err != nil {
|
||||
m.Log("Failed to revoke API keys for plugin "+thisPlugin.Spec.Name, err)
|
||||
}
|
||||
//Unsubscribe from all events
|
||||
err = EventSystem.UnsubscribeAll(thisPlugin.Spec.ID)
|
||||
if err != nil {
|
||||
m.Log("Failed to unsubscribe plugin "+thisPlugin.Spec.Name+" from events", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user