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

[tor-commits] [snowflake/main] Extract function getCurrentNATType()



commit 2547883cf91839c40b937e354408e41fdf34c24e
Author: Shelikhoo <xiaokangwang@xxxxxxxxxxx>
Date:   Tue Nov 9 19:34:16 2021 +0000

    Extract function getCurrentNATType()
    
    Adopted the change in according to the recommendation from
    
    https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/-/merge_requests/62#note_2759900
---
 proxy/lib/snowflake.go | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/proxy/lib/snowflake.go b/proxy/lib/snowflake.go
index b133c67..4cf25ee 100644
--- a/proxy/lib/snowflake.go
+++ b/proxy/lib/snowflake.go
@@ -80,6 +80,12 @@ var currentNATTypeAccess = &sync.RWMutex{}
 // Obtain currentNATTypeAccess before access.
 var currentNATType = NATUnknown
 
+func getCurrentNATType() string {
+	currentNATTypeAccess.RLock()
+	defer currentNATTypeAccess.RUnlock()
+	return currentNATType
+}
+
 const (
 	sessionIDLength = 16
 )
@@ -190,9 +196,7 @@ func (s *SignalingServer) pollOffer(sid string, shutdown chan struct{}) *webrtc.
 			return nil
 		default:
 			numClients := int((tokens.count() / 8) * 8) // Round down to 8
-			currentNATTypeAccess.RLock()
-			currentNATTypeLoaded := currentNATType
-			currentNATTypeAccess.RUnlock()
+			currentNATTypeLoaded := getCurrentNATType()
 			body, err := messages.EncodePollRequest(sid, "standalone", currentNATTypeLoaded, numClients)
 			if err != nil {
 				log.Printf("Error encoding poll message: %s", err.Error())
@@ -541,9 +545,7 @@ func (sf *SnowflakeProxy) Start() error {
 	// use probetest to determine NAT compatability
 	sf.checkNATType(config, sf.NATProbeURL)
 
-	currentNATTypeAccess.RLock()
-	currentNATTypeLoaded := currentNATType
-	currentNATTypeAccess.RUnlock()
+	currentNATTypeLoaded := getCurrentNATType()
 
 	log.Printf("NAT type: %s", currentNATTypeLoaded)
 
@@ -632,9 +634,7 @@ func (sf *SnowflakeProxy) checkNATType(config webrtc.Configuration, probeURL str
 		return
 	}
 
-	currentNATTypeAccess.RLock()
-	currentNATTypeLoaded := currentNATType
-	currentNATTypeAccess.RUnlock()
+	currentNATTypeLoaded := getCurrentNATType()
 
 	currentNATTypeTestResult := NATUnknown
 	select {



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