Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/andrioid/ublproxy. andrioid.github.io/ublproxy
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192package main
import ( "sync" "time")
// Activity types for the proxy event log.const ( ActivityBlocked = "blocked" ActivityAllowed = "allowed" ActivityPassthrough = "passthrough" ActivityAutoPassthrough = "auto-passthrough" ActivityElementHidden = "element-hidden" ActivityDNSBlocked = "dns-blocked")
// ActivityEntry represents a single proxy event.type ActivityEntry struct { Timestamp time.Time `json:"timestamp"` Type string `json:"type"` Host string `json:"host"` URL string `json:"url,omitempty"` Rule string `json:"rule,omitempty"` IP string `json:"ip"` User string `json:"user"`}
// ActivityLog is a thread-safe ring buffer of recent proxy events.type ActivityLog struct { mu sync.Mutex entries []ActivityEntry head int count int}
// NewActivityLog creates a ring buffer with the given capacity.func NewActivityLog(capacity int) *ActivityLog { return &ActivityLog{ entries: make([]ActivityEntry, capacity), }}
// Add records a new event in the ring buffer.func (l *ActivityLog) Add(entry ActivityEntry) { if entry.Timestamp.IsZero() { entry.Timestamp = time.Now() } l.mu.Lock() l.entries[l.head] = entry l.head = (l.head + 1) % len(l.entries) if l.count < len(l.entries) { l.count++ } l.mu.Unlock()}
// Recent returns the most recent entries, newest first.// If limit <= 0 or limit > count, returns all stored entries.func (l *ActivityLog) Recent(limit int) []ActivityEntry { l.mu.Lock() defer l.mu.Unlock()
if limit <= 0 || limit > l.count { limit = l.count } if limit == 0 { return nil }
result := make([]ActivityEntry, limit) for i := range limit { // Walk backwards from head idx := (l.head - 1 - i + len(l.entries)) % len(l.entries) result[i] = l.entries[idx] } return result}
// Stats returns summary counts by type from the current buffer contents.func (l *ActivityLog) Stats() map[string]int { l.mu.Lock() defer l.mu.Unlock()
stats := make(map[string]int) for i := range l.count { idx := (l.head - 1 - i + len(l.entries)) % len(l.entries) stats[l.entries[idx].Type]++ } return stats}