mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-13 20:11:19 -04:00
Move StripLocalAddresses to a common util
Trac: 19026
This commit is contained in:
parent
5fa7578655
commit
670e4ba438
4 changed files with 72 additions and 63 deletions
|
@ -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)
|
|
||||||
})
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,12 +14,10 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
|
||||||
"git.torproject.org/pluggable-transports/snowflake.git/common/util"
|
"git.torproject.org/pluggable-transports/snowflake.git/common/util"
|
||||||
"github.com/pion/sdp/v2"
|
|
||||||
"github.com/pion/webrtc/v2"
|
"github.com/pion/webrtc/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -81,49 +79,6 @@ func limitedRead(r io.Reader, limit int64) ([]byte, error) {
|
||||||
return p, err
|
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.
|
// Roundtrip HTTP POST using WebRTC SessionDescriptions.
|
||||||
//
|
//
|
||||||
// Send an SDP offer to the broker, which assigns a proxy and responds
|
// 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 {
|
if !bc.keepLocalAddresses {
|
||||||
offer = &webrtc.SessionDescription{
|
offer = &webrtc.SessionDescription{
|
||||||
Type: offer.Type,
|
Type: offer.Type,
|
||||||
SDP: stripLocalAddresses(offer.SDP),
|
SDP: util.StripLocalAddresses(offer.SDP),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
data := bytes.NewReader([]byte(util.SerializeSessionDescription(offer)))
|
data := bytes.NewReader([]byte(util.SerializeSessionDescription(offer)))
|
||||||
|
|
|
@ -3,7 +3,9 @@ package util
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"log"
|
"log"
|
||||||
|
"net"
|
||||||
|
|
||||||
|
"github.com/pion/sdp/v2"
|
||||||
"github.com/pion/webrtc/v2"
|
"github.com/pion/webrtc/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -56,3 +58,46 @@ func DeserializeSessionDescription(msg string) *webrtc.SessionDescription {
|
||||||
SDP: parsed["sdp"].(string),
|
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)
|
||||||
|
}
|
||||||
|
|
26
common/util/util_test.go
Normal file
26
common/util/util_test.go
Normal file
|
@ -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)
|
||||||
|
})
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue