commit d10af300c128955599aefabba10ac8db7027e063 Author: Arlo Breault arlolra@gmail.com Date: Tue Mar 17 15:18:25 2020 -0400
Refactor (De)SerializeSessionDescription as common utils --- client/lib/lib_test.go | 7 +++--- client/lib/rendezvous.go | 5 ++-- client/lib/util.go | 52 ------------------------------------------ common/util/util.go | 58 +++++++++++++++++++++++++++++++++++++++++++++++ proxy-go/proxy-go_test.go | 7 +++--- proxy-go/snowflake.go | 52 +++--------------------------------------- 6 files changed, 72 insertions(+), 109 deletions(-)
diff --git a/client/lib/lib_test.go b/client/lib/lib_test.go index a0e77cb..4b1a9fa 100644 --- a/client/lib/lib_test.go +++ b/client/lib/lib_test.go @@ -9,6 +9,7 @@ import ( "sync" "testing"
+ "git.torproject.org/pluggable-transports/snowflake.git/common/util" "github.com/pion/webrtc/v2" . "github.com/smartystreets/goconvey/convey" ) @@ -230,7 +231,7 @@ func TestSnowflakeClient(t *testing.T) { So(err, ShouldBeNil)
c.offerChannel <- nil - answer := deserializeSessionDescription(sampleAnswer) + answer := util.DeserializeSessionDescription(sampleAnswer) So(answer, ShouldNotBeNil) c.answerChannel <- answer err = c.exchangeSDP() @@ -255,7 +256,7 @@ func TestSnowflakeClient(t *testing.T) { ctx.So(err, ShouldBeNil) wg.Done() }() - answer := deserializeSessionDescription(sampleAnswer) + answer := util.DeserializeSessionDescription(sampleAnswer) c.answerChannel <- answer wg.Wait() }) @@ -285,7 +286,7 @@ func TestSnowflakeClient(t *testing.T) { http.StatusOK, []byte(`{"type":"answer","sdp":"fake"}`), } - fakeOffer := deserializeSessionDescription(`{"type":"offer","sdp":"test"}`) + fakeOffer := util.DeserializeSessionDescription(`{"type":"offer","sdp":"test"}`)
Convey("Construct BrokerChannel with no front domain", func() { b, err := NewBrokerChannel("test.broker", "", transport, false) diff --git a/client/lib/rendezvous.go b/client/lib/rendezvous.go index d35c813..85f6f1a 100644 --- a/client/lib/rendezvous.go +++ b/client/lib/rendezvous.go @@ -18,6 +18,7 @@ import ( "net/http" "net/url"
+ "git.torproject.org/pluggable-transports/snowflake.git/common/util" "github.com/pion/sdp/v2" "github.com/pion/webrtc/v2" ) @@ -140,7 +141,7 @@ func (bc *BrokerChannel) Negotiate(offer *webrtc.SessionDescription) ( SDP: stripLocalAddresses(offer.SDP), } } - data := bytes.NewReader([]byte(serializeSessionDescription(offer))) + data := bytes.NewReader([]byte(util.SerializeSessionDescription(offer))) // Suffix with broker's client registration handler. clientURL := bc.url.ResolveReference(&url.URL{Path: "client"}) request, err := http.NewRequest("POST", clientURL.String(), data) @@ -163,7 +164,7 @@ func (bc *BrokerChannel) Negotiate(offer *webrtc.SessionDescription) ( if nil != err { return nil, err } - answer := deserializeSessionDescription(string(body)) + answer := util.DeserializeSessionDescription(string(body)) return answer, nil case http.StatusServiceUnavailable: return nil, errors.New(BrokerError503) diff --git a/client/lib/util.go b/client/lib/util.go index 1b5f592..cacf1d7 100644 --- a/client/lib/util.go +++ b/client/lib/util.go @@ -1,11 +1,8 @@ package lib
import ( - "encoding/json" "log" "time" - - "github.com/pion/webrtc/v2" )
const ( @@ -86,52 +83,3 @@ func (b *BytesSyncLogger) AddInbound(amount int) { } b.InboundChan <- amount } -func deserializeSessionDescription(msg string) *webrtc.SessionDescription { - var parsed map[string]interface{} - err := json.Unmarshal([]byte(msg), &parsed) - if nil != err { - log.Println(err) - return nil - } - if _, ok := parsed["type"]; !ok { - log.Println("Cannot deserialize SessionDescription without type field.") - return nil - } - if _, ok := parsed["sdp"]; !ok { - log.Println("Cannot deserialize SessionDescription without sdp field.") - return nil - } - - var stype webrtc.SDPType - switch parsed["type"].(string) { - default: - log.Println("Unknown SDP type") - return nil - case "offer": - stype = webrtc.SDPTypeOffer - case "pranswer": - stype = webrtc.SDPTypePranswer - case "answer": - stype = webrtc.SDPTypeAnswer - case "rollback": - stype = webrtc.SDPTypeRollback - } - - if err != nil { - log.Println(err) - return nil - } - return &webrtc.SessionDescription{ - Type: stype, - SDP: parsed["sdp"].(string), - } -} - -func serializeSessionDescription(desc *webrtc.SessionDescription) string { - bytes, err := json.Marshal(*desc) - if nil != err { - log.Println(err) - return "" - } - return string(bytes) -} diff --git a/common/util/util.go b/common/util/util.go new file mode 100644 index 0000000..0a86241 --- /dev/null +++ b/common/util/util.go @@ -0,0 +1,58 @@ +package util + +import ( + "encoding/json" + "log" + + "github.com/pion/webrtc/v2" +) + +func SerializeSessionDescription(desc *webrtc.SessionDescription) string { + bytes, err := json.Marshal(*desc) + if nil != err { + log.Println(err) + return "" + } + return string(bytes) +} + +func DeserializeSessionDescription(msg string) *webrtc.SessionDescription { + var parsed map[string]interface{} + err := json.Unmarshal([]byte(msg), &parsed) + if nil != err { + log.Println(err) + return nil + } + if _, ok := parsed["type"]; !ok { + log.Println("Cannot deserialize SessionDescription without type field.") + return nil + } + if _, ok := parsed["sdp"]; !ok { + log.Println("Cannot deserialize SessionDescription without sdp field.") + return nil + } + + var stype webrtc.SDPType + switch parsed["type"].(string) { + default: + log.Println("Unknown SDP type") + return nil + case "offer": + stype = webrtc.SDPTypeOffer + case "pranswer": + stype = webrtc.SDPTypePranswer + case "answer": + stype = webrtc.SDPTypeAnswer + case "rollback": + stype = webrtc.SDPTypeRollback + } + + if err != nil { + log.Println(err) + return nil + } + return &webrtc.SessionDescription{ + Type: stype, + SDP: parsed["sdp"].(string), + } +} diff --git a/proxy-go/proxy-go_test.go b/proxy-go/proxy-go_test.go index 2429d1e..bed00f2 100644 --- a/proxy-go/proxy-go_test.go +++ b/proxy-go/proxy-go_test.go @@ -13,6 +13,7 @@ import ( "testing"
"git.torproject.org/pluggable-transports/snowflake.git/common/messages" + "git.torproject.org/pluggable-transports/snowflake.git/common/util" "github.com/pion/webrtc/v2" . "github.com/smartystreets/goconvey/convey" ) @@ -197,7 +198,7 @@ func TestSessionDescriptions(t *testing.T) { }, }, } { - desc := deserializeSessionDescription(test.msg) + desc := util.DeserializeSessionDescription(test.msg) So(desc, ShouldResemble, test.ret) } }) @@ -214,7 +215,7 @@ func TestSessionDescriptions(t *testing.T) { `{"type":"offer","sdp":"test"}`, }, } { - msg := serializeSessionDescription(test.desc) + msg := util.SerializeSessionDescription(test.desc) So(msg, ShouldResemble, test.ret) } }) @@ -239,7 +240,7 @@ func TestBrokerInteractions(t *testing.T) { }, } pc, _ := webrtc.NewPeerConnection(config) - offer := deserializeSessionDescription(sampleOffer) + offer := util.DeserializeSessionDescription(sampleOffer) pc.SetRemoteDescription(*offer) answer, _ := pc.CreateAnswer(nil) pc.SetLocalDescription(answer) diff --git a/proxy-go/snowflake.go b/proxy-go/snowflake.go index 0b91059..264d4f2 100644 --- a/proxy-go/snowflake.go +++ b/proxy-go/snowflake.go @@ -4,7 +4,6 @@ import ( "bytes" "crypto/rand" "encoding/base64" - "encoding/json" "flag" "fmt" "io" @@ -21,6 +20,7 @@ import (
"git.torproject.org/pluggable-transports/snowflake.git/common/messages" "git.torproject.org/pluggable-transports/snowflake.git/common/safelog" + "git.torproject.org/pluggable-transports/snowflake.git/common/util" "git.torproject.org/pluggable-transports/snowflake.git/common/websocketconn" "github.com/gorilla/websocket" "github.com/pion/webrtc/v2" @@ -199,7 +199,7 @@ func (b *Broker) pollOffer(sid string) *webrtc.SessionDescription { return nil } if offer != "" { - return deserializeSessionDescription(offer) + return util.DeserializeSessionDescription(offer) } } } @@ -209,7 +209,7 @@ func (b *Broker) pollOffer(sid string) *webrtc.SessionDescription {
func (b *Broker) sendAnswer(sid string, pc *webrtc.PeerConnection) error { brokerPath := b.url.ResolveReference(&url.URL{Path: "answer"}) - answer := string([]byte(serializeSessionDescription(pc.LocalDescription()))) + answer := string([]byte(util.SerializeSessionDescription(pc.LocalDescription()))) body, err := messages.EncodeAnswerRequest(answer, sid) if err != nil { return err @@ -465,49 +465,3 @@ func main() { runSession(sessionID) } } - -func deserializeSessionDescription(msg string) *webrtc.SessionDescription { - var parsed map[string]interface{} - err := json.Unmarshal([]byte(msg), &parsed) - if nil != err { - log.Println(err) - return nil - } - if _, ok := parsed["type"]; !ok { - log.Println("Cannot deserialize SessionDescription without type field.") - return nil - } - if _, ok := parsed["sdp"]; !ok { - log.Println("Cannot deserialize SessionDescription without sdp field.") - return nil - } - - var stype webrtc.SDPType - switch parsed["type"].(string) { - default: - log.Println("Unknown SDP type") - return nil - case "offer": - stype = webrtc.SDPTypeOffer - case "pranswer": - stype = webrtc.SDPTypePranswer - case "answer": - stype = webrtc.SDPTypeAnswer - case "rollback": - stype = webrtc.SDPTypeRollback - } - - return &webrtc.SessionDescription{ - Type: stype, - SDP: parsed["sdp"].(string), - } -} - -func serializeSessionDescription(desc *webrtc.SessionDescription) string { - bytes, err := json.Marshal(*desc) - if nil != err { - log.Println(err) - return "" - } - return string(bytes) -}