feat: expose pollInterval in CLI

Closes https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/-/issues/40373
This commit is contained in:
WofWca 2024-08-22 11:18:42 +04:00 committed by Cecylia Bocovich
parent 8f429666a8
commit f4db64612c
No known key found for this signature in database
GPG key ID: 009DE379FD9B7B90
4 changed files with 27 additions and 12 deletions

View file

@ -35,6 +35,8 @@ Usage of ./proxy:
a pattern to specify allowed hostname pattern for relay URL. (default "snowflake.torproject.net$") a pattern to specify allowed hostname pattern for relay URL. (default "snowflake.torproject.net$")
-broker string -broker string
broker URL (default "https://snowflake-broker.torproject.net/") broker URL (default "https://snowflake-broker.torproject.net/")
-poll-interval duration
how often to ask the broker for a new client. Keep in mind that asking for a client will not always result in getting one. Minumum value is 2s. Valid time units are "ms", "s", "m", "h". (default 5s)
-capacity uint -capacity uint
maximum concurrent clients (default is to accept an unlimited number of clients) maximum concurrent clients (default is to accept an unlimited number of clients)
-disableStatsLogger -disableStatsLogger

View file

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

View file

@ -53,11 +53,12 @@ import (
) )
const ( const (
DefaultBrokerURL = "https://snowflake-broker.torproject.net/" DefaultPollInterval = 5 * time.Second
DefaultNATProbeURL = "https://snowflake-broker.torproject.net:8443/probe" DefaultBrokerURL = "https://snowflake-broker.torproject.net/"
DefaultRelayURL = "wss://snowflake.torproject.net/" DefaultNATProbeURL = "https://snowflake-broker.torproject.net:8443/probe"
DefaultSTUNURL = "stun:stun.l.google.com:19302" DefaultRelayURL = "wss://snowflake.torproject.net/"
DefaultProxyType = "standalone" DefaultSTUNURL = "stun:stun.l.google.com:19302"
DefaultProxyType = "standalone"
) )
const ( const (
@ -72,8 +73,6 @@ const (
) )
const ( const (
pollInterval = 5 * time.Second
// Amount of time after sending an SDP answer before the proxy assumes the // Amount of time after sending an SDP answer before the proxy assumes the
// client is not going to connect // client is not going to connect
dataChannelTimeout = 20 * time.Second dataChannelTimeout = 20 * time.Second
@ -115,6 +114,8 @@ var (
// SnowflakeProxy is used to configure an embedded // SnowflakeProxy is used to configure an embedded
// Snowflake in another Go application. // Snowflake in another Go application.
type SnowflakeProxy struct { type SnowflakeProxy struct {
// How often to ask the broker for a new client
PollInterval time.Duration
// Capacity is the maximum number of clients a Snowflake will serve. // Capacity is the maximum number of clients a Snowflake will serve.
// Proxies with a capacity of 0 will accept an unlimited number of clients. // Proxies with a capacity of 0 will accept an unlimited number of clients.
Capacity uint Capacity uint
@ -221,7 +222,7 @@ func (s *SignalingServer) Post(path string, payload io.Reader) ([]byte, error) {
// pollOffer communicates the proxy's capabilities with broker // pollOffer communicates the proxy's capabilities with broker
// and retrieves a compatible SDP offer // and retrieves a compatible SDP offer
func (s *SignalingServer) pollOffer(sid string, proxyType string, acceptedRelayPattern string, shutdown chan struct{}) (*webrtc.SessionDescription, string) { func (s *SignalingServer) pollOffer(sid string, proxyType string, acceptedRelayPattern string, pollInterval time.Duration, shutdown chan struct{}) (*webrtc.SessionDescription, string) {
brokerPath := s.url.ResolveReference(&url.URL{Path: "proxy"}) brokerPath := s.url.ResolveReference(&url.URL{Path: "proxy"})
ticker := time.NewTicker(pollInterval) ticker := time.NewTicker(pollInterval)
@ -591,7 +592,7 @@ func (sf *SnowflakeProxy) makeNewPeerConnection(
} }
func (sf *SnowflakeProxy) runSession(sid string) { func (sf *SnowflakeProxy) runSession(sid string) {
offer, relayURL := broker.pollOffer(sid, sf.ProxyType, sf.RelayDomainNamePattern, sf.shutdown) offer, relayURL := broker.pollOffer(sid, sf.ProxyType, sf.RelayDomainNamePattern, sf.PollInterval, sf.shutdown)
if offer == nil { if offer == nil {
log.Printf("bad offer from broker") log.Printf("bad offer from broker")
tokens.ret() tokens.ret()
@ -655,6 +656,9 @@ func (sf *SnowflakeProxy) Start() error {
sf.shutdown = make(chan struct{}) sf.shutdown = make(chan struct{})
// blank configurations revert to default // blank configurations revert to default
if sf.PollInterval == 0 {
sf.PollInterval = DefaultPollInterval
}
if sf.BrokerURL == "" { if sf.BrokerURL == "" {
sf.BrokerURL = DefaultBrokerURL sf.BrokerURL = DefaultBrokerURL
} }
@ -729,7 +733,7 @@ func (sf *SnowflakeProxy) Start() error {
defer NatRetestTask.Close() defer NatRetestTask.Close()
} }
ticker := time.NewTicker(pollInterval) ticker := time.NewTicker(sf.PollInterval)
defer ticker.Stop() defer ticker.Stop()
for ; true; <-ticker.C { for ; true; <-ticker.C {

View file

@ -17,7 +17,11 @@ import (
sf "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/proxy/lib" sf "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/proxy/lib"
) )
const minPollInterval = 2 * time.Second
func main() { func main() {
pollInterval := flag.Duration("poll-interval", sf.DefaultPollInterval,
fmt.Sprint("how often to ask the broker for a new client. Keep in mind that asking for a client will not always result in getting one. Minumum value is ", minPollInterval, ". Valid time units are \"ms\", \"s\", \"m\", \"h\"."))
capacity := flag.Uint("capacity", 0, "maximum concurrent clients (default is to accept an unlimited number of clients)") capacity := flag.Uint("capacity", 0, "maximum concurrent clients (default is to accept an unlimited number of clients)")
stunURL := flag.String("stun", sf.DefaultSTUNURL, "STUN URL") stunURL := flag.String("stun", sf.DefaultSTUNURL, "STUN URL")
logFilename := flag.String("log", "", "log filename") logFilename := flag.String("log", "", "log filename")
@ -50,6 +54,10 @@ func main() {
os.Exit(0) os.Exit(0)
} }
if *pollInterval < minPollInterval {
log.Fatalf("poll-interval must be >= %v", minPollInterval)
}
if *outboundAddress != "" && *keepLocalAddresses { if *outboundAddress != "" && *keepLocalAddresses {
log.Fatal("Cannot keep local address candidates when outbound address is specified") log.Fatal("Cannot keep local address candidates when outbound address is specified")
} }
@ -83,6 +91,7 @@ func main() {
} }
proxy := sf.SnowflakeProxy{ proxy := sf.SnowflakeProxy{
PollInterval: *pollInterval,
Capacity: uint(*capacity), Capacity: uint(*capacity),
STUNURL: *stunURL, STUNURL: *stunURL,
BrokerURL: *rawBrokerURL, BrokerURL: *rawBrokerURL,