[snowflake/master] Move StripLocalAddresses to a common util

commit 670e4ba4380b3fa5cf82043559dcb8c2ca790a7d Author: Arlo Breault <arlolra@gmail.com> Date: Thu Mar 26 13:05:24 2020 -0400 Move StripLocalAddresses to a common util Trac: 19026 --- client/lib/lib_test.go | 17 ----------------- client/lib/rendezvous.go | 47 +---------------------------------------------- common/util/util.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ common/util/util_test.go | 26 ++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 63 deletions(-) diff --git a/client/lib/lib_test.go b/client/lib/lib_test.go index 4b1a9fa..1cdc2c6 100644 --- a/client/lib/lib_test.go +++ b/client/lib/lib_test.go @@ -358,21 +358,4 @@ func TestSnowflakeClient(t *testing.T) { }) }) - Convey("Strip", t, func() { - const offerStart = "v=0\r\no=- 4358805017720277108 2 IN IP4 8.8.8.8\r\ns=-\r\nt=0 0\r\na=group:BUNDLE data\r\na=msid-semantic: WMS\r\nm=application 56688 DTLS/SCTP 5000\r\nc=IN IP4 8.8.8.8\r\n" - const goodCandidate = "a=candidate:3769337065 1 udp 2122260223 8.8.8.8 56688 typ host generation 0 network-id 1 network-cost 50\r\n" - const offerEnd = "a=ice-ufrag:aMAZ\r\na=ice-pwd:jcHb08Jjgrazp2dzjdrvPPvV\r\na=ice-options:trickle\r\na=fingerprint:sha-256 C8:88:EE:B9:E7:02:2E:21:37:ED:7A:D1:EB:2B:A3:15:A2:3B:5B:1C:3D:D4:D5:1F:06:CF:52:40:03:F8:DD:66\r\na=setup:actpass\r\na=mid:data\r\na=sctpmap:5000 webrtc-datachannel 1024\r\n" - - offer := offerStart + goodCandidate + - "a=candidate:3769337065 1 udp 2122260223 192.168.0.100 56688 typ host generation 0 network-id 1 network-cost 50\r\n" + // IsLocal IPv4 - "a=candidate:3769337065 1 udp 2122260223 fdf8:f53b:82e4::53 56688 typ host generation 0 network-id 1 network-cost 50\r\n" + // IsLocal IPv6 - "a=candidate:3769337065 1 udp 2122260223 0.0.0.0 56688 typ host generation 0 network-id 1 network-cost 50\r\n" + // IsUnspecified IPv4 - "a=candidate:3769337065 1 udp 2122260223 :: 56688 typ host generation 0 network-id 1 network-cost 50\r\n" + // IsUnspecified IPv6 - "a=candidate:3769337065 1 udp 2122260223 127.0.0.1 56688 typ host generation 0 network-id 1 network-cost 50\r\n" + // IsLoopback IPv4 - "a=candidate:3769337065 1 udp 2122260223 ::1 56688 typ host generation 0 network-id 1 network-cost 50\r\n" + // IsLoopback IPv6 - offerEnd - - So(stripLocalAddresses(offer), ShouldEqual, offerStart+goodCandidate+offerEnd) - }) - } diff --git a/client/lib/rendezvous.go b/client/lib/rendezvous.go index 85f6f1a..1f98e26 100644 --- a/client/lib/rendezvous.go +++ b/client/lib/rendezvous.go @@ -14,12 +14,10 @@ import ( "io" "io/ioutil" "log" - "net" "net/http" "net/url" "git.torproject.org/pluggable-transports/snowflake.git/common/util" - "github.com/pion/sdp/v2" "github.com/pion/webrtc/v2" ) @@ -81,49 +79,6 @@ func limitedRead(r io.Reader, limit int64) ([]byte, error) { return p, err } -// Stolen from https://github.com/golang/go/pull/30278 -func IsLocal(ip net.IP) bool { - if ip4 := ip.To4(); ip4 != nil { - // Local IPv4 addresses are defined in https://tools.ietf.org/html/rfc1918 - return ip4[0] == 10 || - (ip4[0] == 172 && ip4[1]&0xf0 == 16) || - (ip4[0] == 192 && ip4[1] == 168) - } - // Local IPv6 addresses are defined in https://tools.ietf.org/html/rfc4193 - return len(ip) == net.IPv6len && ip[0]&0xfe == 0xfc -} - -// Removes local LAN address ICE candidates -func stripLocalAddresses(str string) string { - var desc sdp.SessionDescription - err := desc.Unmarshal([]byte(str)) - if err != nil { - return str - } - for _, m := range desc.MediaDescriptions { - attrs := make([]sdp.Attribute, 0) - for _, a := range m.Attributes { - if a.IsICECandidate() { - ice, err := a.ToICECandidate() - if err == nil && ice.Typ == "host" { - ip := net.ParseIP(ice.Address) - if ip != nil && (IsLocal(ip) || ip.IsUnspecified() || ip.IsLoopback()) { - /* no append in this case */ - continue - } - } - } - attrs = append(attrs, a) - } - m.Attributes = attrs - } - bts, err := desc.Marshal() - if err != nil { - return str - } - return string(bts) -} - // Roundtrip HTTP POST using WebRTC SessionDescriptions. // // Send an SDP offer to the broker, which assigns a proxy and responds @@ -138,7 +93,7 @@ func (bc *BrokerChannel) Negotiate(offer *webrtc.SessionDescription) ( if !bc.keepLocalAddresses { offer = &webrtc.SessionDescription{ Type: offer.Type, - SDP: stripLocalAddresses(offer.SDP), + SDP: util.StripLocalAddresses(offer.SDP), } } data := bytes.NewReader([]byte(util.SerializeSessionDescription(offer))) diff --git a/common/util/util.go b/common/util/util.go index 0a86241..fa62fd7 100644 --- a/common/util/util.go +++ b/common/util/util.go @@ -3,7 +3,9 @@ package util import ( "encoding/json" "log" + "net" + "github.com/pion/sdp/v2" "github.com/pion/webrtc/v2" ) @@ -56,3 +58,46 @@ func DeserializeSessionDescription(msg string) *webrtc.SessionDescription { SDP: parsed["sdp"].(string), } } + +// Stolen from https://github.com/golang/go/pull/30278 +func IsLocal(ip net.IP) bool { + if ip4 := ip.To4(); ip4 != nil { + // Local IPv4 addresses are defined in https://tools.ietf.org/html/rfc1918 + return ip4[0] == 10 || + (ip4[0] == 172 && ip4[1]&0xf0 == 16) || + (ip4[0] == 192 && ip4[1] == 168) + } + // Local IPv6 addresses are defined in https://tools.ietf.org/html/rfc4193 + return len(ip) == net.IPv6len && ip[0]&0xfe == 0xfc +} + +// Removes local LAN address ICE candidates +func StripLocalAddresses(str string) string { + var desc sdp.SessionDescription + err := desc.Unmarshal([]byte(str)) + if err != nil { + return str + } + for _, m := range desc.MediaDescriptions { + attrs := make([]sdp.Attribute, 0) + for _, a := range m.Attributes { + if a.IsICECandidate() { + ice, err := a.ToICECandidate() + if err == nil && ice.Typ == "host" { + ip := net.ParseIP(ice.Address) + if ip != nil && (IsLocal(ip) || ip.IsUnspecified() || ip.IsLoopback()) { + /* no append in this case */ + continue + } + } + } + attrs = append(attrs, a) + } + m.Attributes = attrs + } + bts, err := desc.Marshal() + if err != nil { + return str + } + return string(bts) +} diff --git a/common/util/util_test.go b/common/util/util_test.go new file mode 100644 index 0000000..271619a --- /dev/null +++ b/common/util/util_test.go @@ -0,0 +1,26 @@ +package util + +import ( + "testing" + + . "github.com/smartystreets/goconvey/convey" +) + +func TestUtil(t *testing.T) { + Convey("Strip", t, func() { + const offerStart = "v=0\r\no=- 4358805017720277108 2 IN IP4 8.8.8.8\r\ns=-\r\nt=0 0\r\na=group:BUNDLE data\r\na=msid-semantic: WMS\r\nm=application 56688 DTLS/SCTP 5000\r\nc=IN IP4 8.8.8.8\r\n" + const goodCandidate = "a=candidate:3769337065 1 udp 2122260223 8.8.8.8 56688 typ host generation 0 network-id 1 network-cost 50\r\n" + const offerEnd = "a=ice-ufrag:aMAZ\r\na=ice-pwd:jcHb08Jjgrazp2dzjdrvPPvV\r\na=ice-options:trickle\r\na=fingerprint:sha-256 C8:88:EE:B9:E7:02:2E:21:37:ED:7A:D1:EB:2B:A3:15:A2:3B:5B:1C:3D:D4:D5:1F:06:CF:52:40:03:F8:DD:66\r\na=setup:actpass\r\na=mid:data\r\na=sctpmap:5000 webrtc-datachannel 1024\r\n" + + offer := offerStart + goodCandidate + + "a=candidate:3769337065 1 udp 2122260223 192.168.0.100 56688 typ host generation 0 network-id 1 network-cost 50\r\n" + // IsLocal IPv4 + "a=candidate:3769337065 1 udp 2122260223 fdf8:f53b:82e4::53 56688 typ host generation 0 network-id 1 network-cost 50\r\n" + // IsLocal IPv6 + "a=candidate:3769337065 1 udp 2122260223 0.0.0.0 56688 typ host generation 0 network-id 1 network-cost 50\r\n" + // IsUnspecified IPv4 + "a=candidate:3769337065 1 udp 2122260223 :: 56688 typ host generation 0 network-id 1 network-cost 50\r\n" + // IsUnspecified IPv6 + "a=candidate:3769337065 1 udp 2122260223 127.0.0.1 56688 typ host generation 0 network-id 1 network-cost 50\r\n" + // IsLoopback IPv4 + "a=candidate:3769337065 1 udp 2122260223 ::1 56688 typ host generation 0 network-id 1 network-cost 50\r\n" + // IsLoopback IPv6 + offerEnd + + So(StripLocalAddresses(offer), ShouldEqual, offerStart+goodCandidate+offerEnd) + }) +}
participants (1)
-
arlo@torproject.org