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
type Event struct {
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"`
}
@@ -88,6 +91,7 @@ func ParseEvent(jsonData []byte, event *Event) error {
var temp struct {
Name EventName `json:"name"`
Timestamp int64 `json:"timestamp"`
UUID string `json:"uuid"`
}
if err := json.Unmarshal(jsonData, &temp); err != nil {
return err
@@ -96,6 +100,7 @@ func ParseEvent(jsonData []byte, event *Event) error {
// Set the event name and timestamp
event.Name = temp.Name
event.Timestamp = temp.Timestamp
event.UUID = temp.UUID
// Now, based on the event type, unmarshal the specific payload
switch temp.Name {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -6,6 +6,7 @@ import (
"testing"
"time"
"github.com/google/uuid"
"imuslab.com/zoraxy/mod/plugins/zoraxy_plugin/events"
)
@@ -18,6 +19,7 @@ func TestEventDeSerialization(t *testing.T) {
}
timestamp := time.Now().Unix()
uuid := uuid.New().String()
tests := []SerializationTest{
{
@@ -25,6 +27,7 @@ func TestEventDeSerialization(t *testing.T) {
event: events.Event{
Name: events.EventBlacklistedIPBlocked,
Timestamp: timestamp,
UUID: uuid,
Data: &events.BlacklistedIPBlockedEvent{
IP: "192.168.1.1",
Comment: "Test comment",
@@ -34,25 +37,27 @@ func TestEventDeSerialization(t *testing.T) {
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",
event: events.Event{
Name: events.EventBlacklistToggled,
Timestamp: timestamp,
UUID: uuid,
Data: &events.BlacklistToggledEvent{
RuleID: "rule123",
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",
event: events.Event{
Name: events.EventAccessRuleCreated,
Timestamp: timestamp,
UUID: uuid,
Data: &events.AccessRuleCreatedEvent{
ID: "rule456",
Name: "New Access Rule",
@@ -61,7 +66,7 @@ func TestEventDeSerialization(t *testing.T) {
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
type Event struct {
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"`
}
@@ -88,6 +91,7 @@ func ParseEvent(jsonData []byte, event *Event) error {
var temp struct {
Name EventName `json:"name"`
Timestamp int64 `json:"timestamp"`
UUID string `json:"uuid"`
}
if err := json.Unmarshal(jsonData, &temp); err != nil {
return err
@@ -96,6 +100,7 @@ func ParseEvent(jsonData []byte, event *Event) error {
// Set the event name and timestamp
event.Name = temp.Name
event.Timestamp = temp.Timestamp
event.UUID = temp.UUID
// Now, based on the event type, unmarshal the specific payload
switch temp.Name {