Add Proxy Event Logger

This commit is contained in:
Shelikhoo 2021-12-20 15:42:16 +00:00
parent 9208364475
commit 1116bc81c8
No known key found for this signature in database
GPG key ID: C4D5E79D22B25316
2 changed files with 59 additions and 0 deletions

View 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()
}

View file

@ -2,6 +2,7 @@ package main
import (
"flag"
"git.torproject.org/pluggable-transports/snowflake.git/v2/common/event"
"io"
"log"
"os"
@ -21,9 +22,17 @@ func main() {
relayURL := flag.String("relay", sf.DefaultRelayURL, "websocket relay URL")
NATTypeMeasurementInterval := flag.Duration("nat-retest-interval", time.Hour*24,
"the time interval in second before NAT type is retested, 0s disables retest. Valid time units are \"s\", \"m\", \"h\". ")
SummaryInterval := flag.Duration("summary-interval", time.Hour,
"the time interval to output summary, 0s disables retest. Valid time units are \"s\", \"m\", \"h\". ")
flag.Parse()
periodicEventLogger := sf.NewProxyEventLogger(*SummaryInterval)
eventLogger := event.NewSnowflakeEventDispatcher()
eventLogger.AddSnowflakeEventListener(periodicEventLogger)
proxy := sf.SnowflakeProxy{
Capacity: uint(*capacity),
STUNURL: *stunURL,
@ -32,6 +41,7 @@ func main() {
RelayURL: *relayURL,
NATTypeMeasurementInterval: *NATTypeMeasurementInterval,
EventDispatcher: eventLogger,
}
var logOutput io.Writer = os.Stderr