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

[tor-commits] [snowflake/master] Stop writing to global opt.brokerURL variable.



commit 1eb5c66428e0e6cce8ffc0ebf7c4ec8e65f56e30
Author: David Fifield <david@xxxxxxxxxxxxxxx>
Date:   Tue Aug 1 15:51:34 2017 -0700

    Stop writing to global opt.brokerURL variable.
    
    This variable was being written to by multiple goroutines, overwriting
    the Path to either "/proxy" or "/answer" as necessary, which was racy.
---
 proxy-go/snowflake.go | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)

diff --git a/proxy-go/snowflake.go b/proxy-go/snowflake.go
index 5111e8e..f1dc1f0 100644
--- a/proxy-go/snowflake.go
+++ b/proxy-go/snowflake.go
@@ -28,7 +28,6 @@ const defaultSTUNURL = "stun:stun.l.google.com:19302"
 type snowflakeOptions struct {
 	capacity  uint
 	broker    string
-	brokerURL *url.URL
 	stun      string
 	relay     string
 }
@@ -102,7 +101,17 @@ func genSessionID() string {
 	return strings.TrimRight(base64.StdEncoding.EncodeToString(buf), "=")
 }
 
-func pollOffer(sid string, broker *url.URL) *webrtc.SessionDescription {
+// Parses a URL with url.Parse and panics on any error.
+func mustParseURL(rawurl string) *url.URL {
+	u, err := url.Parse(rawurl)
+	if err != nil {
+		panic(err)
+	}
+	return u
+}
+
+func pollOffer(sid string) *webrtc.SessionDescription {
+	broker := mustParseURL(opt.broker)
 	broker.Path = "/proxy"
 	for {
 		req, _ := http.NewRequest("POST", broker.String(), bytes.NewBuffer([]byte(sid)))
@@ -126,7 +135,8 @@ func pollOffer(sid string, broker *url.URL) *webrtc.SessionDescription {
 	}
 }
 
-func sendAnswer(sid string, broker *url.URL, pc *webrtc.PeerConnection) error {
+func sendAnswer(sid string, pc *webrtc.PeerConnection) error {
+	broker := mustParseURL(opt.broker)
 	broker.Path = "/answer"
 	body := bytes.NewBuffer([]byte(pc.LocalDescription().Serialize()))
 	req, _ := http.NewRequest("POST", broker.String(), body)
@@ -272,7 +282,7 @@ func makePeerConnectionFromOffer(sdp *webrtc.SessionDescription, config *webrtc.
 }
 
 func runSession(sid string) {
-	offer := pollOffer(sid, opt.brokerURL)
+	offer := pollOffer(sid)
 	if offer == nil {
 		log.Printf("bad offer from broker")
 		retToken()
@@ -284,7 +294,7 @@ func runSession(sid string) {
 		retToken()
 		return
 	}
-	err = sendAnswer(sid, opt.brokerURL, pc)
+	err = sendAnswer(sid, pc)
 	if err != nil {
 		log.Printf("error sending answer to client through broker: %s", err)
 		pc.Close()
@@ -314,7 +324,7 @@ func main() {
 	}
 
 	var err error
-	opt.brokerURL, err = url.Parse(opt.broker)
+	_, err = url.Parse(opt.broker)
 	if err != nil {
 		log.Fatalf("invalid broker url: %s", err)
 	}



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