mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-13 11:11:30 -04:00
Refactor (De)SerializeSessionDescription as common utils
This commit is contained in:
parent
c11461d339
commit
d10af300c1
6 changed files with 72 additions and 109 deletions
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
58
common/util/util.go
Normal file
58
common/util/util.go
Normal file
|
@ -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),
|
||||
}
|
||||
}
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue