mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-13 20:11:19 -04:00
Encode client-broker messages as json in HTTP body
Send the client poll request and response in a json-encoded format in the HTTP request body rather than sending the data in HTTP headers. This will pave the way for using domain-fronting alternatives for the Snowflake rendezvous.
This commit is contained in:
parent
ae7cc478fd
commit
270eb21803
7 changed files with 472 additions and 63 deletions
|
@ -176,7 +176,7 @@ func TestSnowflakeClient(t *testing.T) {
|
|||
Convey("Rendezvous", t, func() {
|
||||
transport := &MockTransport{
|
||||
http.StatusOK,
|
||||
[]byte(`{"type":"answer","sdp":"fake"}`),
|
||||
[]byte(`{"answer": "{\"type\":\"answer\",\"sdp\":\"fake\"}" }`),
|
||||
}
|
||||
fakeOffer, err := util.DeserializeSessionDescription(`{"type":"offer","sdp":"test"}`)
|
||||
if err != nil {
|
||||
|
@ -209,26 +209,25 @@ func TestSnowflakeClient(t *testing.T) {
|
|||
So(answer.SDP, ShouldResemble, "fake")
|
||||
})
|
||||
|
||||
Convey("BrokerChannel.Negotiate fails with 503", func() {
|
||||
Convey("BrokerChannel.Negotiate fails", func() {
|
||||
b, err := NewBrokerChannel("test.broker", "",
|
||||
&MockTransport{http.StatusServiceUnavailable, []byte("\n")},
|
||||
&MockTransport{http.StatusOK, []byte(`{"error": "no snowflake proxies currently available"}`)},
|
||||
false)
|
||||
So(err, ShouldBeNil)
|
||||
answer, err := b.Negotiate(fakeOffer)
|
||||
So(err, ShouldNotBeNil)
|
||||
So(answer, ShouldBeNil)
|
||||
So(err.Error(), ShouldResemble, BrokerError503)
|
||||
})
|
||||
|
||||
Convey("BrokerChannel.Negotiate fails with 400", func() {
|
||||
Convey("BrokerChannel.Negotiate fails with unexpected error", func() {
|
||||
b, err := NewBrokerChannel("test.broker", "",
|
||||
&MockTransport{http.StatusBadRequest, []byte("\n")},
|
||||
&MockTransport{http.StatusInternalServerError, []byte("\n")},
|
||||
false)
|
||||
So(err, ShouldBeNil)
|
||||
answer, err := b.Negotiate(fakeOffer)
|
||||
So(err, ShouldNotBeNil)
|
||||
So(answer, ShouldBeNil)
|
||||
So(err.Error(), ShouldResemble, BrokerError400)
|
||||
So(err.Error(), ShouldResemble, BrokerErrorUnexpected)
|
||||
})
|
||||
|
||||
Convey("BrokerChannel.Negotiate fails with large read", func() {
|
||||
|
@ -242,15 +241,6 @@ func TestSnowflakeClient(t *testing.T) {
|
|||
So(err.Error(), ShouldResemble, "unexpected EOF")
|
||||
})
|
||||
|
||||
Convey("BrokerChannel.Negotiate fails with unexpected error", func() {
|
||||
b, err := NewBrokerChannel("test.broker", "",
|
||||
&MockTransport{123, []byte("")}, false)
|
||||
So(err, ShouldBeNil)
|
||||
answer, err := b.Negotiate(fakeOffer)
|
||||
So(err, ShouldNotBeNil)
|
||||
So(answer, ShouldBeNil)
|
||||
So(err.Error(), ShouldResemble, BrokerErrorUnexpected)
|
||||
})
|
||||
})
|
||||
|
||||
}
|
||||
|
|
|
@ -19,14 +19,13 @@ import (
|
|||
"sync"
|
||||
"time"
|
||||
|
||||
"git.torproject.org/pluggable-transports/snowflake.git/common/messages"
|
||||
"git.torproject.org/pluggable-transports/snowflake.git/common/nat"
|
||||
"git.torproject.org/pluggable-transports/snowflake.git/common/util"
|
||||
"github.com/pion/webrtc/v3"
|
||||
)
|
||||
|
||||
const (
|
||||
BrokerError503 string = "No snowflake proxies currently available."
|
||||
BrokerError400 string = "You sent an invalid offer in the request."
|
||||
BrokerErrorUnexpected string = "Unexpected error, no answer."
|
||||
readLimit = 100000 //Maximum number of bytes to be read from an HTTP response
|
||||
)
|
||||
|
@ -107,7 +106,20 @@ func (bc *BrokerChannel) Negotiate(offer *webrtc.SessionDescription) (
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
data := bytes.NewReader([]byte(offerSDP))
|
||||
|
||||
// Encode client poll request
|
||||
bc.lock.Lock()
|
||||
req := &messages.ClientPollRequest{
|
||||
Offer: offerSDP,
|
||||
NAT: bc.NATType,
|
||||
}
|
||||
body, err := req.EncodePollRequest()
|
||||
bc.lock.Unlock()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
data := bytes.NewReader([]byte(body))
|
||||
// Suffix with broker's client registration handler.
|
||||
clientURL := bc.url.ResolveReference(&url.URL{Path: "client"})
|
||||
request, err := http.NewRequest("POST", clientURL.String(), data)
|
||||
|
@ -117,10 +129,6 @@ func (bc *BrokerChannel) Negotiate(offer *webrtc.SessionDescription) (
|
|||
if "" != bc.Host { // Set true host if necessary.
|
||||
request.Host = bc.Host
|
||||
}
|
||||
// include NAT-TYPE
|
||||
bc.lock.Lock()
|
||||
request.Header.Set("Snowflake-NAT-TYPE", bc.NATType)
|
||||
bc.lock.Unlock()
|
||||
resp, err := bc.transport.RoundTrip(request)
|
||||
if nil != err {
|
||||
return nil, err
|
||||
|
@ -135,11 +143,15 @@ func (bc *BrokerChannel) Negotiate(offer *webrtc.SessionDescription) (
|
|||
return nil, err
|
||||
}
|
||||
log.Printf("Received answer: %s", string(body))
|
||||
return util.DeserializeSessionDescription(string(body))
|
||||
case http.StatusServiceUnavailable:
|
||||
return nil, errors.New(BrokerError503)
|
||||
case http.StatusBadRequest:
|
||||
return nil, errors.New(BrokerError400)
|
||||
|
||||
resp, err := messages.DecodeClientPollResponse(body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if resp.Error != "" {
|
||||
return nil, errors.New(resp.Error)
|
||||
}
|
||||
return util.DeserializeSessionDescription(resp.Answer)
|
||||
default:
|
||||
return nil, errors.New(BrokerErrorUnexpected)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue