util_test.go 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package util
  2. import (
  3. "net"
  4. "net/http"
  5. "testing"
  6. . "github.com/smartystreets/goconvey/convey"
  7. )
  8. func TestUtil(t *testing.T) {
  9. Convey("Strip", t, func() {
  10. 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"
  11. 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"
  12. 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"
  13. offer := offerStart + goodCandidate +
  14. "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
  15. "a=candidate:3769337065 1 udp 2122260223 100.127.50.5 56688 typ host generation 0 network-id 1 network-cost 50\r\n" + // IsLocal IPv4
  16. "a=candidate:3769337065 1 udp 2122260223 169.254.250.88 56688 typ host generation 0 network-id 1 network-cost 50\r\n" + // IsLocal IPv4
  17. "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
  18. "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
  19. "a=candidate:3769337065 1 udp 2122260223 :: 56688 typ host generation 0 network-id 1 network-cost 50\r\n" + // IsUnspecified IPv6
  20. "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
  21. "a=candidate:3769337065 1 udp 2122260223 ::1 56688 typ host generation 0 network-id 1 network-cost 50\r\n" + // IsLoopback IPv6
  22. offerEnd
  23. So(StripLocalAddresses(offer), ShouldEqual, offerStart+goodCandidate+offerEnd)
  24. })
  25. Convey("GetClientIp", t, func() {
  26. // Should use Forwarded header
  27. req1, _ := http.NewRequest("GET", "https://example.com", nil)
  28. req1.Header.Add("X-Forwarded-For", "1.1.1.1, 2001:db8:cafe::99%eth0, 3.3.3.3, 192.168.1.1")
  29. req1.Header.Add("Forwarded", `For=fe80::abcd;By=fe80::1234, Proto=https;For=::ffff:188.0.2.128, For="[2001:db8:cafe::17]:4848", For=fc00::1`)
  30. req1.RemoteAddr = "192.168.1.2:8888"
  31. So(GetClientIp(req1), ShouldEqual, "188.0.2.128")
  32. // Should use X-Forwarded-For header
  33. req2, _ := http.NewRequest("GET", "https://example.com", nil)
  34. req2.Header.Add("X-Forwarded-For", "1.1.1.1, 2001:db8:cafe::99%eth0, 3.3.3.3, 192.168.1.1")
  35. req2.RemoteAddr = "192.168.1.2:8888"
  36. So(GetClientIp(req2), ShouldEqual, "1.1.1.1")
  37. // Should use RemoteAddr
  38. req3, _ := http.NewRequest("GET", "https://example.com", nil)
  39. req3.RemoteAddr = "192.168.1.2:8888"
  40. So(GetClientIp(req3), ShouldEqual, "192.168.1.2")
  41. // Should return empty client IP
  42. req4, _ := http.NewRequest("GET", "https://example.com", nil)
  43. So(GetClientIp(req4), ShouldEqual, "")
  44. })
  45. Convey("GetCandidateAddrs", t, func() {
  46. // Should prioritize type in the following order: https://datatracker.ietf.org/doc/html/rfc8445#section-5.1.2.2
  47. // Break ties using priority value
  48. 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"
  49. 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"
  50. const sdp = offerStart + "a=candidate:3769337065 1 udp 2122260223 8.8.8.8 56688 typ prflx\r\n" +
  51. "a=candidate:3769337065 1 udp 2122260223 129.97.124.13 56688 typ relay\r\n" +
  52. "a=candidate:3769337065 1 udp 2122260223 129.97.124.14 56688 typ srflx\r\n" +
  53. "a=candidate:3769337065 1 udp 2122260223 129.97.124.15 56688 typ host\r\n" +
  54. "a=candidate:3769337065 1 udp 2122260224 129.97.124.16 56688 typ host\r\n" + offerEnd
  55. So(GetCandidateAddrs(sdp), ShouldEqual, []net.IP{
  56. net.ParseIP("129.97.124.16"),
  57. net.ParseIP("129.97.124.15"),
  58. net.ParseIP("8.8.8.8"),
  59. net.ParseIP("129.97.124.14"),
  60. net.ParseIP("129.97.124.13"),
  61. })
  62. })
  63. }