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.
This commit is contained in:
David Fifield 2017-08-01 15:51:34 -07:00
parent 461dbeba80
commit 1eb5c66428

View file

@ -28,7 +28,6 @@ const defaultSTUNURL = "stun:stun.l.google.com:19302"
type snowflakeOptions struct { type snowflakeOptions struct {
capacity uint capacity uint
broker string broker string
brokerURL *url.URL
stun string stun string
relay string relay string
} }
@ -102,7 +101,17 @@ func genSessionID() string {
return strings.TrimRight(base64.StdEncoding.EncodeToString(buf), "=") 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" broker.Path = "/proxy"
for { for {
req, _ := http.NewRequest("POST", broker.String(), bytes.NewBuffer([]byte(sid))) 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" broker.Path = "/answer"
body := bytes.NewBuffer([]byte(pc.LocalDescription().Serialize())) body := bytes.NewBuffer([]byte(pc.LocalDescription().Serialize()))
req, _ := http.NewRequest("POST", broker.String(), body) req, _ := http.NewRequest("POST", broker.String(), body)
@ -272,7 +282,7 @@ func makePeerConnectionFromOffer(sdp *webrtc.SessionDescription, config *webrtc.
} }
func runSession(sid string) { func runSession(sid string) {
offer := pollOffer(sid, opt.brokerURL) offer := pollOffer(sid)
if offer == nil { if offer == nil {
log.Printf("bad offer from broker") log.Printf("bad offer from broker")
retToken() retToken()
@ -284,7 +294,7 @@ func runSession(sid string) {
retToken() retToken()
return return
} }
err = sendAnswer(sid, opt.brokerURL, pc) err = sendAnswer(sid, pc)
if err != nil { if err != nil {
log.Printf("error sending answer to client through broker: %s", err) log.Printf("error sending answer to client through broker: %s", err)
pc.Close() pc.Close()
@ -314,7 +324,7 @@ func main() {
} }
var err error var err error
opt.brokerURL, err = url.Parse(opt.broker) _, err = url.Parse(opt.broker)
if err != nil { if err != nil {
log.Fatalf("invalid broker url: %s", err) log.Fatalf("invalid broker url: %s", err)
} }