improvement: use SetIPFilter for local addrs

Closes https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/-/issues/40271.
Supersedes https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/-/merge_requests/417.

This simplifies the code and (probably) removes the need for
`StripLocalAddresses`, although makes us more dependent on Pion.

Signed-off-by: Cecylia Bocovich <cohosh@torproject.org>
This commit is contained in:
WofWca 2024-11-25 01:43:29 +04:00 committed by Cecylia Bocovich
parent 43799819a1
commit ae5bd52821
No known key found for this signature in database
GPG key ID: 009DE379FD9B7B90
6 changed files with 56 additions and 32 deletions

View file

@ -14,6 +14,7 @@ import (
"fmt"
"io"
"log"
"net"
"net/http"
"os"
"strings"
@ -54,6 +55,17 @@ func makePeerConnectionFromOffer(stunURL string, sdp *webrtc.SessionDescription,
dataChanOpen chan struct{}, dataChanClosed chan struct{}, iceGatheringTimeout time.Duration) (*webrtc.PeerConnection, error) {
settingsEngine := webrtc.SettingEngine{}
settingsEngine.SetIPFilter(func(ip net.IP) (keep bool) {
// `IsLoopback()` and `IsUnspecified` are likely not neded here,
// but let's keep them just in case.
// FYI there is similar code in other files in this project.
keep = !util.IsLocal(ip) && !ip.IsLoopback() && !ip.IsUnspecified()
return
})
// FYI this is `false` by default anyway as of pion/webrtc@4
settingsEngine.SetIncludeLoopbackCandidate(false)
// Use the SetNet setting https://pkg.go.dev/github.com/pion/webrtc/v3#SettingEngine.SetNet
// to functionally revert a new change in pion by silently ignoring
// when net.Interfaces() fails, rather than throwing an error
@ -168,11 +180,7 @@ func probeHandler(stunURL string, w http.ResponseWriter, r *http.Request) {
// Otherwise it must be closed below, wherever `closePcOnReturn` is set to `false`.
}()
sdp = &webrtc.SessionDescription{
Type: pc.LocalDescription().Type,
SDP: util.StripLocalAddresses(pc.LocalDescription().SDP),
}
answer, err := util.SerializeSessionDescription(sdp)
answer, err := util.SerializeSessionDescription(pc.LocalDescription())
if err != nil {
log.Printf("Error making WebRTC connection: %s", err)
w.WriteHeader(http.StatusInternalServerError)