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

@ -29,7 +29,8 @@ HTTP 200 OK
{
type: offer,
sdp: [WebRTC SDP]
}
},
NAT: ["unknown"|"restricted"|"unrestricted"]
}
2) If a client is not matched:
@ -120,13 +121,15 @@ func DecodePollRequest(data []byte) (string, string, string, error) {
type ProxyPollResponse struct {
Status string
Offer string
NAT string
}
func EncodePollResponse(offer string, success bool) ([]byte, error) {
func EncodePollResponse(offer string, success bool, natType string) ([]byte, error) {
if success {
return json.Marshal(ProxyPollResponse{
Status: "client match",
Offer: offer,
NAT: natType,
})
}
@ -135,28 +138,33 @@ func EncodePollResponse(offer string, success bool) ([]byte, error) {
})
}
// Decodes a poll response from the broker and returns an offer
// Decodes a poll response from the broker and returns an offer and the client's NAT type
// If there is a client match, the returned offer string will be non-empty
func DecodePollResponse(data []byte) (string, error) {
func DecodePollResponse(data []byte) (string, string, error) {
var message ProxyPollResponse
err := json.Unmarshal(data, &message)
if err != nil {
return "", err
return "", "", err
}
if message.Status == "" {
return "", fmt.Errorf("received invalid data")
return "", "", fmt.Errorf("received invalid data")
}
if message.Status == "client match" {
if message.Offer == "" {
return "", fmt.Errorf("no supplied offer")
return "", "", fmt.Errorf("no supplied offer")
}
} else {
message.Offer = ""
}
return message.Offer, nil
natType := message.NAT
if natType == "" {
natType = "unknown"
}
return message.Offer, natType, nil
}
type ProxyAnswerRequest struct {

View file

@ -109,7 +109,7 @@ func TestDecodeProxyPollResponse(t *testing.T) {
}{
{
"fake offer",
`{"Status":"client match","Offer":"fake offer"}`,
`{"Status":"client match","Offer":"fake offer","NAT":"unknown"}`,
nil,
},
{
@ -128,9 +128,9 @@ func TestDecodeProxyPollResponse(t *testing.T) {
fmt.Errorf(""),
},
} {
offer, err := DecodePollResponse([]byte(test.data))
So(offer, ShouldResemble, test.offer)
offer, _, err := DecodePollResponse([]byte(test.data))
So(err, ShouldHaveSameTypeAs, test.err)
So(offer, ShouldResemble, test.offer)
}
})
@ -138,16 +138,18 @@ func TestDecodeProxyPollResponse(t *testing.T) {
func TestEncodeProxyPollResponse(t *testing.T) {
Convey("Context", t, func() {
b, err := EncodePollResponse("fake offer", true)
b, err := EncodePollResponse("fake offer", true, "restricted")
So(err, ShouldEqual, nil)
offer, err := DecodePollResponse(b)
offer, natType, err := DecodePollResponse(b)
So(offer, ShouldEqual, "fake offer")
So(natType, ShouldEqual, "restricted")
So(err, ShouldEqual, nil)
b, err = EncodePollResponse("", false)
b, err = EncodePollResponse("", false, "unknown")
So(err, ShouldEqual, nil)
offer, err = DecodePollResponse(b)
offer, natType, err = DecodePollResponse(b)
So(offer, ShouldEqual, "")
So(natType, ShouldEqual, "unknown")
So(err, ShouldEqual, nil)
})
}