Make the proxy type configurable for users of the library

Closes: #40104
This commit is contained in:
meskio 2022-03-11 16:42:05 +01:00
parent b265bd3092
commit b73add1550
No known key found for this signature in database
GPG key ID: 52B8F5AC97A2DA86
2 changed files with 13 additions and 7 deletions

View file

@ -365,7 +365,7 @@ func TestBrokerInteractions(t *testing.T) {
b,
}
sdp := broker.pollOffer(sampleOffer, nil)
sdp := broker.pollOffer(sampleOffer, DefaultProxyType, nil)
expectedSDP, _ := strconv.Unquote(sampleSDP)
So(sdp.SDP, ShouldResemble, expectedSDP)
})
@ -379,7 +379,7 @@ func TestBrokerInteractions(t *testing.T) {
b,
}
sdp := broker.pollOffer(sampleOffer, nil)
sdp := broker.pollOffer(sampleOffer, DefaultProxyType, nil)
So(sdp, ShouldBeNil)
})
Convey("sends answer to broker", func() {

View file

@ -56,6 +56,7 @@ const DefaultNATProbeURL = "https://snowflake-broker.torproject.net:8443/probe"
const DefaultRelayURL = "wss://snowflake.bamsoftware.com/"
const DefaultSTUNURL = "stun:stun.stunprotocol.org:3478"
const DefaultProxyType = "standalone"
const pollInterval = 5 * time.Second
const (
@ -115,8 +116,10 @@ type SnowflakeProxy struct {
NATProbeURL string
// NATTypeMeasurementInterval is time before NAT type is retested
NATTypeMeasurementInterval time.Duration
EventDispatcher event.SnowflakeEventDispatcher
shutdown chan struct{}
// ProxyType is the type reported to the broker, if not provided it "standalone" will be used
ProxyType string
EventDispatcher event.SnowflakeEventDispatcher
shutdown chan struct{}
}
// Checks whether an IP address is a remote address for the client
@ -185,7 +188,7 @@ func (s *SignalingServer) Post(path string, payload io.Reader) ([]byte, error) {
return limitedRead(resp.Body, readLimit)
}
func (s *SignalingServer) pollOffer(sid string, shutdown chan struct{}) *webrtc.SessionDescription {
func (s *SignalingServer) pollOffer(sid string, proxyType string, shutdown chan struct{}) *webrtc.SessionDescription {
brokerPath := s.url.ResolveReference(&url.URL{Path: "proxy"})
ticker := time.NewTicker(pollInterval)
@ -199,7 +202,7 @@ func (s *SignalingServer) pollOffer(sid string, shutdown chan struct{}) *webrtc.
default:
numClients := int((tokens.count() / 8) * 8) // Round down to 8
currentNATTypeLoaded := getCurrentNATType()
body, err := messages.EncodeProxyPollRequest(sid, "standalone", currentNATTypeLoaded, numClients)
body, err := messages.EncodeProxyPollRequest(sid, proxyType, currentNATTypeLoaded, numClients)
if err != nil {
log.Printf("Error encoding poll message: %s", err.Error())
return nil
@ -467,7 +470,7 @@ func (sf *SnowflakeProxy) makeNewPeerConnection(config webrtc.Configuration,
}
func (sf *SnowflakeProxy) runSession(sid string) {
offer := broker.pollOffer(sid, sf.shutdown)
offer := broker.pollOffer(sid, sf.ProxyType, sf.shutdown)
if offer == nil {
log.Printf("bad offer from broker")
tokens.ret()
@ -525,6 +528,9 @@ func (sf *SnowflakeProxy) Start() error {
if sf.NATProbeURL == "" {
sf.NATProbeURL = DefaultNATProbeURL
}
if sf.ProxyType == "" {
sf.ProxyType = DefaultProxyType
}
if sf.EventDispatcher == nil {
sf.EventDispatcher = event.NewSnowflakeEventDispatcher()
}