mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-13 20:11:19 -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)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue