Have util.{Serialize,Deserialize}SessionDescription return an error

https://bugs.torproject.org/33897#comment:4
This commit is contained in:
David Fifield 2020-04-27 18:45:10 -06:00
parent 76732155e7
commit b48fb781ee
3 changed files with 26 additions and 27 deletions

View file

@ -231,7 +231,8 @@ func TestSnowflakeClient(t *testing.T) {
So(err, ShouldBeNil) So(err, ShouldBeNil)
c.offerChannel <- nil c.offerChannel <- nil
answer := util.DeserializeSessionDescription(sampleAnswer) answer, err := util.DeserializeSessionDescription(sampleAnswer)
So(err, ShouldBeNil)
So(answer, ShouldNotBeNil) So(answer, ShouldNotBeNil)
c.answerChannel <- answer c.answerChannel <- answer
err = c.exchangeSDP() err = c.exchangeSDP()
@ -256,7 +257,8 @@ func TestSnowflakeClient(t *testing.T) {
ctx.So(err, ShouldBeNil) ctx.So(err, ShouldBeNil)
wg.Done() wg.Done()
}() }()
answer := util.DeserializeSessionDescription(sampleAnswer) answer, err := util.DeserializeSessionDescription(sampleAnswer)
So(err, ShouldBeNil)
c.answerChannel <- answer c.answerChannel <- answer
wg.Wait() wg.Wait()
}) })
@ -286,7 +288,10 @@ func TestSnowflakeClient(t *testing.T) {
http.StatusOK, http.StatusOK,
[]byte(`{"type":"answer","sdp":"fake"}`), []byte(`{"type":"answer","sdp":"fake"}`),
} }
fakeOffer := util.DeserializeSessionDescription(`{"type":"offer","sdp":"test"}`) fakeOffer, err := util.DeserializeSessionDescription(`{"type":"offer","sdp":"test"}`)
if err != nil {
panic(err)
}
Convey("Construct BrokerChannel with no front domain", func() { Convey("Construct BrokerChannel with no front domain", func() {
b, err := NewBrokerChannel("test.broker", "", transport, false) b, err := NewBrokerChannel("test.broker", "", transport, false)

View file

@ -96,7 +96,11 @@ func (bc *BrokerChannel) Negotiate(offer *webrtc.SessionDescription) (
SDP: util.StripLocalAddresses(offer.SDP), SDP: util.StripLocalAddresses(offer.SDP),
} }
} }
data := bytes.NewReader([]byte(util.SerializeSessionDescription(offer))) offerSDP, err := util.SerializeSessionDescription(offer)
if err != nil {
return nil, err
}
data := bytes.NewReader([]byte(offerSDP))
// Suffix with broker's client registration handler. // Suffix with broker's client registration handler.
clientURL := bc.url.ResolveReference(&url.URL{Path: "client"}) clientURL := bc.url.ResolveReference(&url.URL{Path: "client"})
request, err := http.NewRequest("POST", clientURL.String(), data) request, err := http.NewRequest("POST", clientURL.String(), data)
@ -119,8 +123,7 @@ func (bc *BrokerChannel) Negotiate(offer *webrtc.SessionDescription) (
if nil != err { if nil != err {
return nil, err return nil, err
} }
answer := util.DeserializeSessionDescription(string(body)) return util.DeserializeSessionDescription(string(body))
return answer, nil
case http.StatusServiceUnavailable: case http.StatusServiceUnavailable:
return nil, errors.New(BrokerError503) return nil, errors.New(BrokerError503)
case http.StatusBadRequest: case http.StatusBadRequest:

View file

@ -2,43 +2,38 @@ package util
import ( import (
"encoding/json" "encoding/json"
"log" "errors"
"net" "net"
"github.com/pion/sdp/v2" "github.com/pion/sdp/v2"
"github.com/pion/webrtc/v2" "github.com/pion/webrtc/v2"
) )
func SerializeSessionDescription(desc *webrtc.SessionDescription) string { func SerializeSessionDescription(desc *webrtc.SessionDescription) (string, error) {
bytes, err := json.Marshal(*desc) bytes, err := json.Marshal(*desc)
if nil != err { if err != nil {
log.Println(err) return "", err
return ""
} }
return string(bytes) return string(bytes), nil
} }
func DeserializeSessionDescription(msg string) *webrtc.SessionDescription { func DeserializeSessionDescription(msg string) (*webrtc.SessionDescription, error) {
var parsed map[string]interface{} var parsed map[string]interface{}
err := json.Unmarshal([]byte(msg), &parsed) err := json.Unmarshal([]byte(msg), &parsed)
if nil != err { if err != nil {
log.Println(err) return nil, err
return nil
} }
if _, ok := parsed["type"]; !ok { if _, ok := parsed["type"]; !ok {
log.Println("Cannot deserialize SessionDescription without type field.") return nil, errors.New("cannot deserialize SessionDescription without type field")
return nil
} }
if _, ok := parsed["sdp"]; !ok { if _, ok := parsed["sdp"]; !ok {
log.Println("Cannot deserialize SessionDescription without sdp field.") return nil, errors.New("cannot deserialize SessionDescription without sdp field")
return nil
} }
var stype webrtc.SDPType var stype webrtc.SDPType
switch parsed["type"].(string) { switch parsed["type"].(string) {
default: default:
log.Println("Unknown SDP type") return nil, errors.New("Unknown SDP type")
return nil
case "offer": case "offer":
stype = webrtc.SDPTypeOffer stype = webrtc.SDPTypeOffer
case "pranswer": case "pranswer":
@ -49,14 +44,10 @@ func DeserializeSessionDescription(msg string) *webrtc.SessionDescription {
stype = webrtc.SDPTypeRollback stype = webrtc.SDPTypeRollback
} }
if err != nil {
log.Println(err)
return nil
}
return &webrtc.SessionDescription{ return &webrtc.SessionDescription{
Type: stype, Type: stype,
SDP: parsed["sdp"].(string), SDP: parsed["sdp"].(string),
} }, nil
} }
// Stolen from https://github.com/golang/go/pull/30278 // Stolen from https://github.com/golang/go/pull/30278