Have broker pass client NAT type to proxy

This will allow browser-based proxies that are unable to determine their
NAT type to conservatively label themselves as restricted NATs if they
fail to work with clients that have restricted NATs.
This commit is contained in:
Cecylia Bocovich 2020-06-22 14:04:29 -04:00
parent 0052c0e10c
commit 046dab865f
7 changed files with 61 additions and 42 deletions

View file

@ -111,17 +111,17 @@ type ProxyPoll struct {
id string
proxyType string
natType string
offerChannel chan []byte
offerChannel chan *ClientOffer
}
// Registers a Snowflake and waits for some Client to send an offer,
// as part of the polling logic of the proxy handler.
func (ctx *BrokerContext) RequestOffer(id string, proxyType string, natType string) []byte {
func (ctx *BrokerContext) RequestOffer(id string, proxyType string, natType string) *ClientOffer {
request := new(ProxyPoll)
request.id = id
request.proxyType = proxyType
request.natType = natType
request.offerChannel = make(chan []byte)
request.offerChannel = make(chan *ClientOffer)
ctx.proxyPolls <- request
// Block until an offer is available, or timeout which sends a nil offer.
offer := <-request.offerChannel
@ -165,7 +165,7 @@ func (ctx *BrokerContext) AddSnowflake(id string, proxyType string, natType stri
snowflake.id = id
snowflake.clients = 0
snowflake.proxyType = proxyType
snowflake.offerChannel = make(chan []byte)
snowflake.offerChannel = make(chan *ClientOffer)
snowflake.answerChannel = make(chan []byte)
ctx.snowflakeLock.Lock()
if natType == NATRestricted {
@ -213,7 +213,7 @@ func proxyPolls(ctx *BrokerContext, w http.ResponseWriter, r *http.Request) {
ctx.metrics.proxyIdleCount++
ctx.metrics.lock.Unlock()
b, err = messages.EncodePollResponse("", false)
b, err = messages.EncodePollResponse("", false, "")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
@ -222,7 +222,7 @@ func proxyPolls(ctx *BrokerContext, w http.ResponseWriter, r *http.Request) {
w.Write(b)
return
}
b, err = messages.EncodePollResponse(string(offer), true)
b, err = messages.EncodePollResponse(string(offer.sdp), true, offer.natType)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
@ -232,28 +232,37 @@ func proxyPolls(ctx *BrokerContext, w http.ResponseWriter, r *http.Request) {
}
}
// Client offer contains an SDP and the NAT type of the client
type ClientOffer struct {
natType string
sdp []byte
}
/*
Expects a WebRTC SDP offer in the Request to give to an assigned
snowflake proxy, which responds with the SDP answer to be sent in
the HTTP response back to the client.
*/
func clientOffers(ctx *BrokerContext, w http.ResponseWriter, r *http.Request) {
var err error
startTime := time.Now()
offer, err := ioutil.ReadAll(http.MaxBytesReader(w, r.Body, readLimit))
offer := &ClientOffer{}
offer.sdp, err = ioutil.ReadAll(http.MaxBytesReader(w, r.Body, readLimit))
if nil != err {
log.Println("Invalid data.")
w.WriteHeader(http.StatusBadRequest)
return
}
natType := r.Header.Get("Snowflake-NAT-Type")
if natType == "" {
natType = NATUnknown
offer.natType = r.Header.Get("Snowflake-NAT-Type")
if offer.natType == "" {
offer.natType = NATUnknown
}
// Only hand out known restricted snowflakes to unrestricted clients
var snowflakeHeap *SnowflakeHeap
if natType == NATUnrestricted {
if offer.natType == NATUnrestricted {
snowflakeHeap = ctx.restrictedSnowflakes
} else {
snowflakeHeap = ctx.snowflakes

View file

@ -37,7 +37,7 @@ func TestBroker(t *testing.T) {
Convey("Broker goroutine matches clients with proxies", func() {
p := new(ProxyPoll)
p.id = "test"
p.offerChannel = make(chan []byte)
p.offerChannel = make(chan *ClientOffer)
go func(ctx *BrokerContext) {
ctx.proxyPolls <- p
close(ctx.proxyPolls)
@ -45,23 +45,23 @@ func TestBroker(t *testing.T) {
ctx.Broker()
So(ctx.snowflakes.Len(), ShouldEqual, 1)
snowflake := heap.Pop(ctx.snowflakes).(*Snowflake)
snowflake.offerChannel <- []byte("test offer")
snowflake.offerChannel <- &ClientOffer{sdp: []byte("test offer")}
offer := <-p.offerChannel
So(ctx.idToSnowflake["test"], ShouldNotBeNil)
So(offer, ShouldResemble, []byte("test offer"))
So(offer.sdp, ShouldResemble, []byte("test offer"))
So(ctx.snowflakes.Len(), ShouldEqual, 0)
})
Convey("Request an offer from the Snowflake Heap", func() {
done := make(chan []byte)
done := make(chan *ClientOffer)
go func() {
offer := ctx.RequestOffer("test", "", NATUnknown)
done <- offer
}()
request := <-ctx.proxyPolls
request.offerChannel <- []byte("test offer")
request.offerChannel <- &ClientOffer{sdp: []byte("test offer")}
offer := <-done
So(offer, ShouldResemble, []byte("test offer"))
So(offer.sdp, ShouldResemble, []byte("test offer"))
})
Convey("Responds to client offers...", func() {
@ -85,7 +85,7 @@ func TestBroker(t *testing.T) {
done <- true
}()
offer := <-snowflake.offerChannel
So(offer, ShouldResemble, []byte("test"))
So(offer.sdp, ShouldResemble, []byte("test"))
snowflake.answerChannel <- []byte("fake answer")
<-done
So(w.Body.String(), ShouldEqual, "fake answer")
@ -104,7 +104,7 @@ func TestBroker(t *testing.T) {
done <- true
}()
offer := <-snowflake.offerChannel
So(offer, ShouldResemble, []byte("test"))
So(offer.sdp, ShouldResemble, []byte("test"))
<-done
So(w.Code, ShouldEqual, http.StatusGatewayTimeout)
})
@ -125,10 +125,10 @@ func TestBroker(t *testing.T) {
// Pass a fake client offer to this proxy
p := <-ctx.proxyPolls
So(p.id, ShouldEqual, "ymbcCMto7KHNGYlp")
p.offerChannel <- []byte("fake offer")
p.offerChannel <- &ClientOffer{sdp: []byte("fake offer")}
<-done
So(w.Code, ShouldEqual, http.StatusOK)
So(w.Body.String(), ShouldEqual, `{"Status":"client match","Offer":"fake offer"}`)
So(w.Body.String(), ShouldEqual, `{"Status":"client match","Offer":"fake offer","NAT":""}`)
})
Convey("return empty 200 OK when no client offer is available.", func() {
@ -141,7 +141,7 @@ func TestBroker(t *testing.T) {
// nil means timeout
p.offerChannel <- nil
<-done
So(w.Body.String(), ShouldEqual, `{"Status":"no match","Offer":""}`)
So(w.Body.String(), ShouldEqual, `{"Status":"no match","Offer":"","NAT":""}`)
So(w.Code, ShouldEqual, http.StatusOK)
})
})
@ -279,7 +279,7 @@ func TestBroker(t *testing.T) {
<-polled
So(wP.Code, ShouldEqual, http.StatusOK)
So(wP.Body.String(), ShouldResemble, `{"Status":"client match","Offer":"fake offer"}`)
So(wP.Body.String(), ShouldResemble, `{"Status":"client match","Offer":"fake offer","NAT":"unknown"}`)
So(ctx.idToSnowflake["ymbcCMto7KHNGYlp"], ShouldNotBeNil)
// Follow up with the answer request afterwards
wA := httptest.NewRecorder()
@ -543,7 +543,7 @@ func TestMetrics(t *testing.T) {
done <- true
}()
offer := <-snowflake.offerChannel
So(offer, ShouldResemble, []byte("test"))
So(offer.sdp, ShouldResemble, []byte("test"))
snowflake.answerChannel <- []byte("fake answer")
<-done

View file

@ -11,7 +11,7 @@ over the offer and answer channels.
type Snowflake struct {
id string
proxyType string
offerChannel chan []byte
offerChannel chan *ClientOffer
answerChannel chan []byte
clients int
index int