fix(probetest): NAT check timing out sometimes

if ICE gathering on the probetest server is taking long
to complete.

Related: https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/-/issues/40230
This commit is contained in:
WofWca 2024-09-07 15:50:54 +04:00 committed by Shelikhoo
parent 2d13e2a5d1
commit 55c4c90a3a
No known key found for this signature in database
GPG key ID: 4C9764E9FE80A3DC

View file

@ -46,7 +46,7 @@ func (h ProbeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Create a PeerConnection from an SDP offer. Blocks until the gathering of ICE // Create a PeerConnection from an SDP offer. Blocks until the gathering of ICE
// candidates is complete and the answer is available in LocalDescription. // candidates is complete and the answer is available in LocalDescription.
func makePeerConnectionFromOffer(stunURL string, sdp *webrtc.SessionDescription, func makePeerConnectionFromOffer(stunURL string, sdp *webrtc.SessionDescription,
dataChan chan struct{}) (*webrtc.PeerConnection, error) { dataChan chan struct{}, iceGatheringTimeout time.Duration) (*webrtc.PeerConnection, error) {
settingsEngine := webrtc.SettingEngine{} settingsEngine := webrtc.SettingEngine{}
// Use the SetNet setting https://pkg.go.dev/github.com/pion/webrtc/v3#SettingEngine.SetNet // Use the SetNet setting https://pkg.go.dev/github.com/pion/webrtc/v3#SettingEngine.SetNet
@ -102,8 +102,14 @@ func makePeerConnectionFromOffer(stunURL string, sdp *webrtc.SessionDescription,
} }
return nil, err return nil, err
} }
// Wait for ICE candidate gathering to complete
<-done // Wait for ICE candidate gathering to complete,
// or for whatever we managed to gather before the client times out.
// See https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/-/issues/40230
select {
case <-done:
case <-time.After(iceGatheringTimeout):
}
return pc, nil return pc, nil
} }
@ -135,7 +141,10 @@ func probeHandler(stunURL string, w http.ResponseWriter, r *http.Request) {
} }
dataChan := make(chan struct{}) dataChan := make(chan struct{})
pc, err := makePeerConnectionFromOffer(stunURL, sdp, dataChan) // TODO refactor: DRY this must be below `ResponseHeaderTimeout` in proxy
// https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/-/blob/e1d9b4ace69897521cc29585b5084c5f4d1ce874/proxy/lib/snowflake.go#L207
iceGatheringTimeout := 10 * time.Second
pc, err := makePeerConnectionFromOffer(stunURL, sdp, dataChan, iceGatheringTimeout)
if err != nil { if err != nil {
log.Printf("Error making WebRTC connection: %s", err) log.Printf("Error making WebRTC connection: %s", err)
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)