From 1c84a8f9cf25872c50d58f310ec475da98f32228 Mon Sep 17 00:00:00 2001 From: Anthony Rubick <68485672+AnthonyMichaelTDM@users.noreply.github.com> Date: Sat, 6 Sep 2025 16:45:02 -0500 Subject: [PATCH] fix: remove events import from zoraxy_plugin The import, when the code is copied to develop a plugin, results an invalid path. Fixing the path manually as a plugin developer is easy, but it shouldn't be necessary. To fix that, the type is replaced with a string in zoraxy_plugin.IntroSpect and validation is added to lifecycle.go to ensure all subscribed events are valid. A downside is that the list of validEventNames has to be updated whenever a new event is created, but this is mitigated by placing definitions of that list and the actual event names right next to each other. --- src/mod/plugins/lifecycle.go | 4 ++++ src/mod/plugins/zoraxy_plugin/events/events.go | 13 +++++++++++++ src/mod/plugins/zoraxy_plugin/zoraxy_plugin.go | 6 ++---- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/mod/plugins/lifecycle.go b/src/mod/plugins/lifecycle.go index cf8282f..d2d9752 100644 --- a/src/mod/plugins/lifecycle.go +++ b/src/mod/plugins/lifecycle.go @@ -157,6 +157,10 @@ func (m *Manager) StartPlugin(pluginID string) error { if thisPlugin.Spec.SubscriptionsEvents != nil { for eventName := range thisPlugin.Spec.SubscriptionsEvents { eventType := events.EventName(eventName) + if !eventType.IsValid() { + m.Log("Invalid event name: "+string(eventName), nil) + continue + } err := eventsystem.Publisher.RegisterSubscriberToEvent(thisPlugin, eventType) if err != nil { m.Log("Failed to subscribe plugin "+thisPlugin.Spec.Name+" to event "+string(eventName), err) diff --git a/src/mod/plugins/zoraxy_plugin/events/events.go b/src/mod/plugins/zoraxy_plugin/events/events.go index 3f54b2e..2bd6536 100644 --- a/src/mod/plugins/zoraxy_plugin/events/events.go +++ b/src/mod/plugins/zoraxy_plugin/events/events.go @@ -32,6 +32,19 @@ const ( // Add more event types as needed ) +var validEventNames = map[EventName]bool{ + EventBlacklistedIPBlocked: true, + EventBlacklistToggled: true, + EventAccessRuleCreated: true, + // Add more event types as needed + // NOTE: Keep up-to-date with event names specified above +} + +// Check if the event name is valid +func (name EventName) IsValid() bool { + return validEventNames[name] +} + // BlacklistedIPBlockedEvent represents an event when a blacklisted IP is blocked type BlacklistedIPBlockedEvent struct { IP string `json:"ip"` diff --git a/src/mod/plugins/zoraxy_plugin/zoraxy_plugin.go b/src/mod/plugins/zoraxy_plugin/zoraxy_plugin.go index 17180bb..761b313 100644 --- a/src/mod/plugins/zoraxy_plugin/zoraxy_plugin.go +++ b/src/mod/plugins/zoraxy_plugin/zoraxy_plugin.go @@ -5,8 +5,6 @@ import ( "fmt" "os" "strings" - - "imuslab.com/zoraxy/mod/plugins/zoraxy_plugin/events" ) /* @@ -104,8 +102,8 @@ type IntroSpect struct { UIPath string `json:"ui_path"` //UI path of your plugin (e.g. /ui), will proxy the whole subpath tree to Zoraxy Web UI as plugin UI /* Subscriptions Settings */ - SubscriptionPath string `json:"subscription_path"` //Subscription event path of your plugin (e.g. /notifyme), a POST request with SubscriptionEvent as body will be sent to this path when the event is triggered - SubscriptionsEvents map[events.EventName]string `json:"subscriptions_events"` //Subscriptions events of your plugin, paired with comments describing how the event is used, see Zoraxy documentation for more details + SubscriptionPath string `json:"subscription_path"` //Subscription event path of your plugin (e.g. /notifyme), a POST request with SubscriptionEvent as body will be sent to this path when the event is triggered + SubscriptionsEvents map[string]string `json:"subscriptions_events"` //Subscriptions events of your plugin, paired with comments describing how the event is used, see Zoraxy documentation for more details /* API Access Control */ PermittedAPIEndpoints []PermittedAPIEndpoint `json:"permitted_api_endpoints"` //List of API endpoints this plugin can access, and a description of why the plugin needs to access this endpoint