mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-13 20:11:19 -04:00
Add Proxy Event Logger
This commit is contained in:
parent
9208364475
commit
1116bc81c8
2 changed files with 59 additions and 0 deletions
49
proxy/lib/pt_event_logger.go
Normal file
49
proxy/lib/pt_event_logger.go
Normal file
|
@ -0,0 +1,49 @@
|
|||
package snowflake_proxy
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.torproject.org/pluggable-transports/snowflake.git/v2/common/task"
|
||||
"time"
|
||||
|
||||
"git.torproject.org/pluggable-transports/snowflake.git/v2/common/event"
|
||||
)
|
||||
|
||||
func NewProxyEventLogger(logPeriod time.Duration) event.SnowflakeEventReceiver {
|
||||
el := &logEventLogger{}
|
||||
el.task = &task.Periodic{Interval: logPeriod, Execute: el.logTick}
|
||||
el.task.Start()
|
||||
return el
|
||||
}
|
||||
|
||||
type logEventLogger struct {
|
||||
inboundSum int
|
||||
outboundSum int
|
||||
connectionCount int
|
||||
logPeriod time.Duration
|
||||
task *task.Periodic
|
||||
}
|
||||
|
||||
func (p *logEventLogger) OnNewSnowflakeEvent(e event.SnowflakeEvent) {
|
||||
switch e.(type) {
|
||||
case event.EventOnProxyConnectionOver:
|
||||
e := e.(event.EventOnProxyConnectionOver)
|
||||
p.inboundSum += e.InboundTraffic
|
||||
p.outboundSum += e.OutboundTraffic
|
||||
p.connectionCount += 1
|
||||
}
|
||||
}
|
||||
|
||||
func (p *logEventLogger) logTick() error {
|
||||
inbound, inboundUnit := formatTraffic(p.inboundSum)
|
||||
outbound, outboundUnit := formatTraffic(p.inboundSum)
|
||||
fmt.Printf("In the last %v, there are %v connections. Traffic Relaied ↑ %v %v, ↓ %v %v.",
|
||||
p.logPeriod.String(), p.connectionCount, inbound, inboundUnit, outbound, outboundUnit)
|
||||
p.outboundSum = 0
|
||||
p.inboundSum = 0
|
||||
p.connectionCount = 0
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *logEventLogger) Close() error {
|
||||
return p.task.Close()
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue