mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-14 05:11:19 -04:00
more complete and improved client rendezvous tests
This commit is contained in:
parent
361da32ecb
commit
00196bbd74
3 changed files with 58 additions and 20 deletions
|
@ -35,13 +35,14 @@ func (m *MockResponse) Close() error {
|
|||
}
|
||||
|
||||
type MockTransport struct {
|
||||
statusOverride int
|
||||
}
|
||||
|
||||
// Just returns a response with fake SDP answer.
|
||||
func (m *MockTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
s := ioutil.NopCloser(strings.NewReader(`{"type":"answer","sdp":"fake"}`))
|
||||
r := &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
StatusCode: m.statusOverride,
|
||||
Body: s,
|
||||
}
|
||||
return r, nil
|
||||
|
@ -119,16 +120,19 @@ func TestConnect(t *testing.T) {
|
|||
})
|
||||
|
||||
Convey("Rendezvous", t, func() {
|
||||
webrtc.SetLoggingVerbosity(0)
|
||||
transport := &MockTransport{http.StatusOK}
|
||||
fakeOffer := webrtc.DeserializeSessionDescription("test")
|
||||
|
||||
Convey("BrokerChannel with no front domain", func() {
|
||||
b := NewBrokerChannel("test.broker", "")
|
||||
b := NewBrokerChannel("test.broker", "", transport)
|
||||
So(b.url, ShouldNotBeNil)
|
||||
So(b.url.Path, ShouldResemble, "test.broker")
|
||||
So(b.transport, ShouldNotBeNil)
|
||||
})
|
||||
|
||||
Convey("BrokerChannel with front domain", func() {
|
||||
b := NewBrokerChannel("test.broker", "front")
|
||||
b := NewBrokerChannel("test.broker", "front", transport)
|
||||
So(b.url, ShouldNotBeNil)
|
||||
So(b.url.Path, ShouldResemble, "test.broker")
|
||||
So(b.url.Host, ShouldResemble, "front")
|
||||
|
@ -136,15 +140,38 @@ func TestConnect(t *testing.T) {
|
|||
})
|
||||
|
||||
Convey("BrokerChannel Negotiate responds with answer", func() {
|
||||
b := NewBrokerChannel("test.broker", "")
|
||||
sdp := webrtc.DeserializeSessionDescription("test")
|
||||
// Replace transport with a mock.
|
||||
b.transport = &MockTransport{}
|
||||
answer, err := b.Negotiate(sdp)
|
||||
b := NewBrokerChannel("test.broker", "", transport)
|
||||
answer, err := b.Negotiate(fakeOffer)
|
||||
So(err, ShouldBeNil)
|
||||
So(answer, ShouldNotBeNil)
|
||||
So(answer.Sdp, ShouldResemble, "fake")
|
||||
})
|
||||
|
||||
Convey("BrokerChannel Negotiate fails with 503", func() {
|
||||
b := NewBrokerChannel("test.broker", "",
|
||||
&MockTransport{http.StatusServiceUnavailable})
|
||||
answer, err := b.Negotiate(fakeOffer)
|
||||
So(err, ShouldNotBeNil)
|
||||
So(answer, ShouldBeNil)
|
||||
So(err.Error(), ShouldResemble, BrokerError503)
|
||||
})
|
||||
|
||||
Convey("BrokerChannel Negotiate fails with 400", func() {
|
||||
b := NewBrokerChannel("test.broker", "",
|
||||
&MockTransport{http.StatusBadRequest})
|
||||
answer, err := b.Negotiate(fakeOffer)
|
||||
So(err, ShouldNotBeNil)
|
||||
So(answer, ShouldBeNil)
|
||||
So(err.Error(), ShouldResemble, BrokerError400)
|
||||
})
|
||||
|
||||
Convey("BrokerChannel Negotiate fails with unexpected", func() {
|
||||
b := NewBrokerChannel("test.broker", "",
|
||||
&MockTransport{123})
|
||||
answer, err := b.Negotiate(fakeOffer)
|
||||
So(err, ShouldNotBeNil)
|
||||
So(answer, ShouldBeNil)
|
||||
So(err.Error(), ShouldResemble, BrokerErrorUnexpected)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
|
|
@ -14,6 +14,12 @@ import (
|
|||
"github.com/keroserene/go-webrtc"
|
||||
)
|
||||
|
||||
const (
|
||||
BrokerError503 string = "No snowflake proxies currently available."
|
||||
BrokerError400 string = "You sent an invalid offer in the request."
|
||||
BrokerErrorUnexpected string = "Unexpected error, no answer."
|
||||
)
|
||||
|
||||
// Signalling Channel to the Broker.
|
||||
type BrokerChannel struct {
|
||||
// The Host header to put in the HTTP request (optional and may be
|
||||
|
@ -23,10 +29,19 @@ type BrokerChannel struct {
|
|||
transport http.RoundTripper // Used to make all requests.
|
||||
}
|
||||
|
||||
// We make a copy of DefaultTransport because we want the default Dial
|
||||
// and TLSHandshakeTimeout settings. But we want to disable the default
|
||||
// ProxyFromEnvironment setting.
|
||||
func CreateBrokerTransport() http.RoundTripper {
|
||||
transport := http.DefaultTransport.(*http.Transport)
|
||||
transport.Proxy = nil
|
||||
return transport
|
||||
}
|
||||
|
||||
// Construct a new BrokerChannel, where:
|
||||
// |broker| is the full URL of the facilitating program which assigns proxies
|
||||
// to clients, and |front| is the option fronting domain.
|
||||
func NewBrokerChannel(broker string, front string) *BrokerChannel {
|
||||
func NewBrokerChannel(broker string, front string, transport http.RoundTripper) *BrokerChannel {
|
||||
targetURL, err := url.Parse(broker)
|
||||
if nil != err {
|
||||
return nil
|
||||
|
@ -38,11 +53,6 @@ func NewBrokerChannel(broker string, front string) *BrokerChannel {
|
|||
bc.url.Host = front
|
||||
}
|
||||
|
||||
// We make a copy of DefaultTransport because we want the default Dial
|
||||
// and TLSHandshakeTimeout settings. But we want to disable the default
|
||||
// ProxyFromEnvironment setting.
|
||||
transport := http.DefaultTransport.(*http.Transport)
|
||||
transport.Proxy = nil
|
||||
bc.transport = transport
|
||||
return bc
|
||||
}
|
||||
|
@ -79,10 +89,10 @@ func (bc *BrokerChannel) Negotiate(offer *webrtc.SessionDescription) (
|
|||
return answer, nil
|
||||
|
||||
case http.StatusServiceUnavailable:
|
||||
return nil, errors.New("No snowflake proxies currently available.")
|
||||
return nil, errors.New(BrokerError503)
|
||||
case http.StatusBadRequest:
|
||||
return nil, errors.New("You sent an invalid offer in the request.")
|
||||
return nil, errors.New(BrokerError400)
|
||||
default:
|
||||
return nil, errors.New("Unexpected error, no answer.")
|
||||
return nil, errors.New(BrokerErrorUnexpected)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -59,7 +59,8 @@ type SnowflakeChannel interface {
|
|||
// Maintain |SnowflakeCapacity| number of available WebRTC connections, to
|
||||
// transfer to the Tor SOCKS handler when needed.
|
||||
func SnowflakeConnectLoop() {
|
||||
broker = NewBrokerChannel(brokerURL, frontDomain)
|
||||
transport := CreateBrokerTransport()
|
||||
broker = NewBrokerChannel(brokerURL, frontDomain, transport)
|
||||
for {
|
||||
numRemotes := len(webrtcRemotes)
|
||||
if numRemotes >= SnowflakeCapacity {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue