[tor-commits] [snowflake/main] Add Proxy Event Logger
shelikhoo at torproject.org
shelikhoo at torproject.org
Mon Jan 31 12:04:25 UTC 2022
commit 1116bc81c86580316e13b07ecd8d7f0a3ac5ffd5
Author: Shelikhoo <xiaokangwang at outlook.com>
Date: Mon Dec 20 15:42:16 2021 +0000
Add Proxy Event Logger
---
proxy/lib/pt_event_logger.go | 49 ++++++++++++++++++++++++++++++++++++++++++++
proxy/main.go | 10 +++++++++
2 files changed, 59 insertions(+)
diff --git a/proxy/lib/pt_event_logger.go b/proxy/lib/pt_event_logger.go
new file mode 100644
index 0000000..5804614
--- /dev/null
+++ b/proxy/lib/pt_event_logger.go
@@ -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()
+}
diff --git a/proxy/main.go b/proxy/main.go
index b85dde0..de31913 100644
--- a/proxy/main.go
+++ b/proxy/main.go
@@ -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
More information about the tor-commits
mailing list