feat(event): add UUID field to Event

This commit is contained in:
Anthony Rubick
2025-09-07 16:41:39 -05:00
parent dbf4648646
commit fd70b7d2dc
11 changed files with 64 additions and 13 deletions

View File

@@ -17,7 +17,10 @@ type EventPayload interface {
// Event represents a system event // Event represents a system event
type Event struct { type Event struct {
Name EventName `json:"name"` Name EventName `json:"name"`
Timestamp int64 `json:"timestamp"` // Unix timestamp // Unix timestamp
Timestamp int64 `json:"timestamp"`
// UUID for the event
UUID string `json:"uuid"`
Data EventPayload `json:"data"` Data EventPayload `json:"data"`
} }
@@ -88,6 +91,7 @@ func ParseEvent(jsonData []byte, event *Event) error {
var temp struct { var temp struct {
Name EventName `json:"name"` Name EventName `json:"name"`
Timestamp int64 `json:"timestamp"` Timestamp int64 `json:"timestamp"`
UUID string `json:"uuid"`
} }
if err := json.Unmarshal(jsonData, &temp); err != nil { if err := json.Unmarshal(jsonData, &temp); err != nil {
return err return err
@@ -96,6 +100,7 @@ func ParseEvent(jsonData []byte, event *Event) error {
// Set the event name and timestamp // Set the event name and timestamp
event.Name = temp.Name event.Name = temp.Name
event.Timestamp = temp.Timestamp event.Timestamp = temp.Timestamp
event.UUID = temp.UUID
// Now, based on the event type, unmarshal the specific payload // Now, based on the event type, unmarshal the specific payload
switch temp.Name { switch temp.Name {

View File

@@ -17,7 +17,10 @@ type EventPayload interface {
// Event represents a system event // Event represents a system event
type Event struct { type Event struct {
Name EventName `json:"name"` Name EventName `json:"name"`
Timestamp int64 `json:"timestamp"` // Unix timestamp // Unix timestamp
Timestamp int64 `json:"timestamp"`
// UUID for the event
UUID string `json:"uuid"`
Data EventPayload `json:"data"` Data EventPayload `json:"data"`
} }
@@ -88,6 +91,7 @@ func ParseEvent(jsonData []byte, event *Event) error {
var temp struct { var temp struct {
Name EventName `json:"name"` Name EventName `json:"name"`
Timestamp int64 `json:"timestamp"` Timestamp int64 `json:"timestamp"`
UUID string `json:"uuid"`
} }
if err := json.Unmarshal(jsonData, &temp); err != nil { if err := json.Unmarshal(jsonData, &temp); err != nil {
return err return err
@@ -96,6 +100,7 @@ func ParseEvent(jsonData []byte, event *Event) error {
// Set the event name and timestamp // Set the event name and timestamp
event.Name = temp.Name event.Name = temp.Name
event.Timestamp = temp.Timestamp event.Timestamp = temp.Timestamp
event.UUID = temp.UUID
// Now, based on the event type, unmarshal the specific payload // Now, based on the event type, unmarshal the specific payload
switch temp.Name { switch temp.Name {

View File

@@ -17,7 +17,10 @@ type EventPayload interface {
// Event represents a system event // Event represents a system event
type Event struct { type Event struct {
Name EventName `json:"name"` Name EventName `json:"name"`
Timestamp int64 `json:"timestamp"` // Unix timestamp // Unix timestamp
Timestamp int64 `json:"timestamp"`
// UUID for the event
UUID string `json:"uuid"`
Data EventPayload `json:"data"` Data EventPayload `json:"data"`
} }
@@ -88,6 +91,7 @@ func ParseEvent(jsonData []byte, event *Event) error {
var temp struct { var temp struct {
Name EventName `json:"name"` Name EventName `json:"name"`
Timestamp int64 `json:"timestamp"` Timestamp int64 `json:"timestamp"`
UUID string `json:"uuid"`
} }
if err := json.Unmarshal(jsonData, &temp); err != nil { if err := json.Unmarshal(jsonData, &temp); err != nil {
return err return err
@@ -96,6 +100,7 @@ func ParseEvent(jsonData []byte, event *Event) error {
// Set the event name and timestamp // Set the event name and timestamp
event.Name = temp.Name event.Name = temp.Name
event.Timestamp = temp.Timestamp event.Timestamp = temp.Timestamp
event.UUID = temp.UUID
// Now, based on the event type, unmarshal the specific payload // Now, based on the event type, unmarshal the specific payload
switch temp.Name { switch temp.Name {

View File

@@ -17,7 +17,10 @@ type EventPayload interface {
// Event represents a system event // Event represents a system event
type Event struct { type Event struct {
Name EventName `json:"name"` Name EventName `json:"name"`
Timestamp int64 `json:"timestamp"` // Unix timestamp // Unix timestamp
Timestamp int64 `json:"timestamp"`
// UUID for the event
UUID string `json:"uuid"`
Data EventPayload `json:"data"` Data EventPayload `json:"data"`
} }
@@ -88,6 +91,7 @@ func ParseEvent(jsonData []byte, event *Event) error {
var temp struct { var temp struct {
Name EventName `json:"name"` Name EventName `json:"name"`
Timestamp int64 `json:"timestamp"` Timestamp int64 `json:"timestamp"`
UUID string `json:"uuid"`
} }
if err := json.Unmarshal(jsonData, &temp); err != nil { if err := json.Unmarshal(jsonData, &temp); err != nil {
return err return err
@@ -96,6 +100,7 @@ func ParseEvent(jsonData []byte, event *Event) error {
// Set the event name and timestamp // Set the event name and timestamp
event.Name = temp.Name event.Name = temp.Name
event.Timestamp = temp.Timestamp event.Timestamp = temp.Timestamp
event.UUID = temp.UUID
// Now, based on the event type, unmarshal the specific payload // Now, based on the event type, unmarshal the specific payload
switch temp.Name { switch temp.Name {

View File

@@ -17,7 +17,10 @@ type EventPayload interface {
// Event represents a system event // Event represents a system event
type Event struct { type Event struct {
Name EventName `json:"name"` Name EventName `json:"name"`
Timestamp int64 `json:"timestamp"` // Unix timestamp // Unix timestamp
Timestamp int64 `json:"timestamp"`
// UUID for the event
UUID string `json:"uuid"`
Data EventPayload `json:"data"` Data EventPayload `json:"data"`
} }
@@ -88,6 +91,7 @@ func ParseEvent(jsonData []byte, event *Event) error {
var temp struct { var temp struct {
Name EventName `json:"name"` Name EventName `json:"name"`
Timestamp int64 `json:"timestamp"` Timestamp int64 `json:"timestamp"`
UUID string `json:"uuid"`
} }
if err := json.Unmarshal(jsonData, &temp); err != nil { if err := json.Unmarshal(jsonData, &temp); err != nil {
return err return err
@@ -96,6 +100,7 @@ func ParseEvent(jsonData []byte, event *Event) error {
// Set the event name and timestamp // Set the event name and timestamp
event.Name = temp.Name event.Name = temp.Name
event.Timestamp = temp.Timestamp event.Timestamp = temp.Timestamp
event.UUID = temp.UUID
// Now, based on the event type, unmarshal the specific payload // Now, based on the event type, unmarshal the specific payload
switch temp.Name { switch temp.Name {

View File

@@ -17,7 +17,10 @@ type EventPayload interface {
// Event represents a system event // Event represents a system event
type Event struct { type Event struct {
Name EventName `json:"name"` Name EventName `json:"name"`
Timestamp int64 `json:"timestamp"` // Unix timestamp // Unix timestamp
Timestamp int64 `json:"timestamp"`
// UUID for the event
UUID string `json:"uuid"`
Data EventPayload `json:"data"` Data EventPayload `json:"data"`
} }
@@ -88,6 +91,7 @@ func ParseEvent(jsonData []byte, event *Event) error {
var temp struct { var temp struct {
Name EventName `json:"name"` Name EventName `json:"name"`
Timestamp int64 `json:"timestamp"` Timestamp int64 `json:"timestamp"`
UUID string `json:"uuid"`
} }
if err := json.Unmarshal(jsonData, &temp); err != nil { if err := json.Unmarshal(jsonData, &temp); err != nil {
return err return err
@@ -96,6 +100,7 @@ func ParseEvent(jsonData []byte, event *Event) error {
// Set the event name and timestamp // Set the event name and timestamp
event.Name = temp.Name event.Name = temp.Name
event.Timestamp = temp.Timestamp event.Timestamp = temp.Timestamp
event.UUID = temp.UUID
// Now, based on the event type, unmarshal the specific payload // Now, based on the event type, unmarshal the specific payload
switch temp.Name { switch temp.Name {

View File

@@ -17,7 +17,10 @@ type EventPayload interface {
// Event represents a system event // Event represents a system event
type Event struct { type Event struct {
Name EventName `json:"name"` Name EventName `json:"name"`
Timestamp int64 `json:"timestamp"` // Unix timestamp // Unix timestamp
Timestamp int64 `json:"timestamp"`
// UUID for the event
UUID string `json:"uuid"`
Data EventPayload `json:"data"` Data EventPayload `json:"data"`
} }
@@ -88,6 +91,7 @@ func ParseEvent(jsonData []byte, event *Event) error {
var temp struct { var temp struct {
Name EventName `json:"name"` Name EventName `json:"name"`
Timestamp int64 `json:"timestamp"` Timestamp int64 `json:"timestamp"`
UUID string `json:"uuid"`
} }
if err := json.Unmarshal(jsonData, &temp); err != nil { if err := json.Unmarshal(jsonData, &temp); err != nil {
return err return err
@@ -96,6 +100,7 @@ func ParseEvent(jsonData []byte, event *Event) error {
// Set the event name and timestamp // Set the event name and timestamp
event.Name = temp.Name event.Name = temp.Name
event.Timestamp = temp.Timestamp event.Timestamp = temp.Timestamp
event.UUID = temp.UUID
// Now, based on the event type, unmarshal the specific payload // Now, based on the event type, unmarshal the specific payload
switch temp.Name { switch temp.Name {

View File

@@ -17,7 +17,10 @@ type EventPayload interface {
// Event represents a system event // Event represents a system event
type Event struct { type Event struct {
Name EventName `json:"name"` Name EventName `json:"name"`
Timestamp int64 `json:"timestamp"` // Unix timestamp // Unix timestamp
Timestamp int64 `json:"timestamp"`
// UUID for the event
UUID string `json:"uuid"`
Data EventPayload `json:"data"` Data EventPayload `json:"data"`
} }
@@ -88,6 +91,7 @@ func ParseEvent(jsonData []byte, event *Event) error {
var temp struct { var temp struct {
Name EventName `json:"name"` Name EventName `json:"name"`
Timestamp int64 `json:"timestamp"` Timestamp int64 `json:"timestamp"`
UUID string `json:"uuid"`
} }
if err := json.Unmarshal(jsonData, &temp); err != nil { if err := json.Unmarshal(jsonData, &temp); err != nil {
return err return err
@@ -96,6 +100,7 @@ func ParseEvent(jsonData []byte, event *Event) error {
// Set the event name and timestamp // Set the event name and timestamp
event.Name = temp.Name event.Name = temp.Name
event.Timestamp = temp.Timestamp event.Timestamp = temp.Timestamp
event.UUID = temp.UUID
// Now, based on the event type, unmarshal the specific payload // Now, based on the event type, unmarshal the specific payload
switch temp.Name { switch temp.Name {

View File

@@ -4,8 +4,8 @@ import (
"sync" "sync"
"time" "time"
"github.com/google/uuid"
"imuslab.com/zoraxy/mod/info/logger" "imuslab.com/zoraxy/mod/info/logger"
// "imuslab.com/zoraxy/mod/plugins"
"imuslab.com/zoraxy/mod/plugins/zoraxy_plugin/events" "imuslab.com/zoraxy/mod/plugins/zoraxy_plugin/events"
) )
@@ -102,6 +102,7 @@ func (em *eventManager) Emit(payload events.EventPayload) error {
event := events.Event{ event := events.Event{
Name: eventName, Name: eventName,
Timestamp: time.Now().Unix(), Timestamp: time.Now().Unix(),
UUID: uuid.New().String(),
Data: payload, Data: payload,
} }

View File

@@ -6,6 +6,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/google/uuid"
"imuslab.com/zoraxy/mod/plugins/zoraxy_plugin/events" "imuslab.com/zoraxy/mod/plugins/zoraxy_plugin/events"
) )
@@ -18,6 +19,7 @@ func TestEventDeSerialization(t *testing.T) {
} }
timestamp := time.Now().Unix() timestamp := time.Now().Unix()
uuid := uuid.New().String()
tests := []SerializationTest{ tests := []SerializationTest{
{ {
@@ -25,6 +27,7 @@ func TestEventDeSerialization(t *testing.T) {
event: events.Event{ event: events.Event{
Name: events.EventBlacklistedIPBlocked, Name: events.EventBlacklistedIPBlocked,
Timestamp: timestamp, Timestamp: timestamp,
UUID: uuid,
Data: &events.BlacklistedIPBlockedEvent{ Data: &events.BlacklistedIPBlockedEvent{
IP: "192.168.1.1", IP: "192.168.1.1",
Comment: "Test comment", Comment: "Test comment",
@@ -34,25 +37,27 @@ func TestEventDeSerialization(t *testing.T) {
Method: "GET", Method: "GET",
}, },
}, },
expectedJson: `{"name":"blacklistedIpBlocked","timestamp":` + fmt.Sprintf("%d", timestamp) + `,"data":{"ip":"192.168.1.1","comment":"Test comment","requested_url":"http://example.com","hostname":"example.com","user_agent":"TestUserAgent","method":"GET"}}`, expectedJson: `{"name":"blacklistedIpBlocked","timestamp":` + fmt.Sprintf("%d", timestamp) + `,"uuid":"` + uuid + `","data":{"ip":"192.168.1.1","comment":"Test comment","requested_url":"http://example.com","hostname":"example.com","user_agent":"TestUserAgent","method":"GET"}}`,
}, },
{ {
name: "BlacklistToggled", name: "BlacklistToggled",
event: events.Event{ event: events.Event{
Name: events.EventBlacklistToggled, Name: events.EventBlacklistToggled,
Timestamp: timestamp, Timestamp: timestamp,
UUID: uuid,
Data: &events.BlacklistToggledEvent{ Data: &events.BlacklistToggledEvent{
RuleID: "rule123", RuleID: "rule123",
Enabled: true, Enabled: true,
}, },
}, },
expectedJson: `{"name":"blacklistToggled","timestamp":` + fmt.Sprintf("%d", timestamp) + `,"data":{"rule_id":"rule123","enabled":true}}`, expectedJson: `{"name":"blacklistToggled","timestamp":` + fmt.Sprintf("%d", timestamp) + `,"uuid":"` + uuid + `","data":{"rule_id":"rule123","enabled":true}}`,
}, },
{ {
name: "AccessRuleCreated", name: "AccessRuleCreated",
event: events.Event{ event: events.Event{
Name: events.EventAccessRuleCreated, Name: events.EventAccessRuleCreated,
Timestamp: timestamp, Timestamp: timestamp,
UUID: uuid,
Data: &events.AccessRuleCreatedEvent{ Data: &events.AccessRuleCreatedEvent{
ID: "rule456", ID: "rule456",
Name: "New Access Rule", Name: "New Access Rule",
@@ -61,7 +66,7 @@ func TestEventDeSerialization(t *testing.T) {
WhitelistEnabled: false, WhitelistEnabled: false,
}, },
}, },
expectedJson: `{"name":"accessRuleCreated","timestamp":` + fmt.Sprintf("%d", timestamp) + `,"data":{"id":"rule456","name":"New Access Rule","desc":"A dummy access rule","blacklist_enabled":true,"whitelist_enabled":false}}`, expectedJson: `{"name":"accessRuleCreated","timestamp":` + fmt.Sprintf("%d", timestamp) + `,"uuid":"` + uuid + `","data":{"id":"rule456","name":"New Access Rule","desc":"A dummy access rule","blacklist_enabled":true,"whitelist_enabled":false}}`,
}, },
} }

View File

@@ -17,7 +17,10 @@ type EventPayload interface {
// Event represents a system event // Event represents a system event
type Event struct { type Event struct {
Name EventName `json:"name"` Name EventName `json:"name"`
Timestamp int64 `json:"timestamp"` // Unix timestamp // Unix timestamp
Timestamp int64 `json:"timestamp"`
// UUID for the event
UUID string `json:"uuid"`
Data EventPayload `json:"data"` Data EventPayload `json:"data"`
} }
@@ -88,6 +91,7 @@ func ParseEvent(jsonData []byte, event *Event) error {
var temp struct { var temp struct {
Name EventName `json:"name"` Name EventName `json:"name"`
Timestamp int64 `json:"timestamp"` Timestamp int64 `json:"timestamp"`
UUID string `json:"uuid"`
} }
if err := json.Unmarshal(jsonData, &temp); err != nil { if err := json.Unmarshal(jsonData, &temp); err != nil {
return err return err
@@ -96,6 +100,7 @@ func ParseEvent(jsonData []byte, event *Event) error {
// Set the event name and timestamp // Set the event name and timestamp
event.Name = temp.Name event.Name = temp.Name
event.Timestamp = temp.Timestamp event.Timestamp = temp.Timestamp
event.UUID = temp.UUID
// Now, based on the event type, unmarshal the specific payload // Now, based on the event type, unmarshal the specific payload
switch temp.Name { switch temp.Name {