mirror of
https://github.com/tobychui/zoraxy.git
synced 2025-09-13 23:59:35 +02:00
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.
This commit is contained in:
@@ -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)
|
||||
|
@@ -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"`
|
||||
|
@@ -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
|
||||
|
Reference in New Issue
Block a user