[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [snowflake/main] Refactor event logger setting into function call



commit 657aaa6ba8b5baadc0d34d9bd0df0914cbc813e3
Author: Shelikhoo <xiaokangwang@xxxxxxxxxxx>
Date:   Thu Jan 20 13:17:34 2022 +0000

    Refactor event logger setting into function call
    
    See also:
    https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/-/merge_requests/67#note_2770482
---
 client/lib/snowflake.go | 17 +++++++++++++----
 client/snowflake.go     |  7 +------
 2 files changed, 14 insertions(+), 10 deletions(-)

diff --git a/client/lib/snowflake.go b/client/lib/snowflake.go
index 4f9e663..0f637f2 100644
--- a/client/lib/snowflake.go
+++ b/client/lib/snowflake.go
@@ -71,6 +71,10 @@ func (addr dummyAddr) String() string  { return "dummy" }
 // https://github.com/Pluggable-Transports/Pluggable-Transports-spec/blob/master/releases/PTSpecV2.1/Pluggable%20Transport%20Specification%20v2.1%20-%20Go%20Transport%20API.pdf
 type Transport struct {
 	dialer *WebRTCDialer
+
+	// EventDispatcher is the event bus for snowflake events.
+	// When an important event happens, it will be distributed here.
+	eventDispatcher event.SnowflakeEventDispatcher
 }
 
 // ClientConfig defines how the SnowflakeClient will connect to the broker and Snowflake proxies.
@@ -93,9 +97,6 @@ type ClientConfig struct {
 	// Max is the maximum number of snowflake proxy peers that the client should attempt to
 	// connect to. Defaults to 1.
 	Max int
-	// EventDispatcher is the event bus for snowflake events.
-	// When an important event happens, it will be distributed here.
-	EventDispatcher event.SnowflakeEventDispatcher
 }
 
 // NewSnowflakeClient creates a new Snowflake transport client that can spawn multiple
@@ -135,7 +136,8 @@ func NewSnowflakeClient(config ClientConfig) (*Transport, error) {
 	if config.Max > max {
 		max = config.Max
 	}
-	transport := &Transport{dialer: NewWebRTCDialer4E(broker, iceServers, max, config.EventDispatcher)}
+	eventsLogger := event.NewSnowflakeEventDispatcher()
+	transport := &Transport{dialer: NewWebRTCDialer4E(broker, iceServers, max, eventsLogger), eventDispatcher: eventsLogger}
 
 	return transport, nil
 }
@@ -191,6 +193,13 @@ func (t *Transport) Dial() (net.Conn, error) {
 	cleanup = nil
 	return &SnowflakeConn{Stream: stream, sess: sess, pconn: pconn, snowflakes: snowflakes}, nil
 }
+func (t *Transport) AddSnowflakeEventListener(receiver event.SnowflakeEventReceiver) {
+	t.eventDispatcher.AddSnowflakeEventListener(receiver)
+}
+
+func (t *Transport) RemoveSnowflakeEventListener(receiver event.SnowflakeEventReceiver) {
+	t.eventDispatcher.RemoveSnowflakeEventListener(receiver)
+}
 
 // SetRendezvousMethod sets the rendezvous method to the Snowflake broker.
 func (t *Transport) SetRendezvousMethod(r RendezvousMethod) {
diff --git a/client/snowflake.go b/client/snowflake.go
index 0ab71a7..5a00206 100644
--- a/client/snowflake.go
+++ b/client/snowflake.go
@@ -17,7 +17,6 @@ import (
 
 	pt "git.torproject.org/pluggable-transports/goptlib.git"
 	sf "git.torproject.org/pluggable-transports/snowflake.git/v2/client/lib"
-	"git.torproject.org/pluggable-transports/snowflake.git/v2/common/event"
 	"git.torproject.org/pluggable-transports/snowflake.git/v2/common/safelog"
 )
 
@@ -91,6 +90,7 @@ func socksAcceptLoop(ln *pt.SocksListener, config sf.ClientConfig, shutdown chan
 				log.Println("Failed to start snowflake transport: ", err)
 				return
 			}
+			transport.AddSnowflakeEventListener(sf.NewPTEventLogger())
 			err = conn.Grant(&net.TCPAddr{IP: net.IPv4zero, Port: 0})
 			if err != nil {
 				log.Printf("conn.Grant error: %s", err)
@@ -171,10 +171,6 @@ func main() {
 
 	iceAddresses := strings.Split(strings.TrimSpace(*iceServersCommas), ",")
 
-	eventLogger := event.NewSnowflakeEventDispatcher()
-
-	eventLogger.AddSnowflakeEventListener(sf.NewPTEventLogger())
-
 	config := sf.ClientConfig{
 		BrokerURL:          *brokerURL,
 		AmpCacheURL:        *ampCacheURL,
@@ -182,7 +178,6 @@ func main() {
 		ICEAddresses:       iceAddresses,
 		KeepLocalAddresses: *keepLocalAddresses || *oldKeepLocalAddresses,
 		Max:                *max,
-		EventDispatcher:    eventLogger,
 	}
 
 	// Begin goptlib client process.



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits