util.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package util
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "log"
  6. "net"
  7. "net/http"
  8. "slices"
  9. "sort"
  10. "github.com/pion/ice/v4"
  11. "github.com/pion/sdp/v3"
  12. "github.com/pion/webrtc/v4"
  13. "github.com/realclientip/realclientip-go"
  14. )
  15. func SerializeSessionDescription(desc *webrtc.SessionDescription) (string, error) {
  16. bytes, err := json.Marshal(*desc)
  17. if err != nil {
  18. return "", err
  19. }
  20. return string(bytes), nil
  21. }
  22. func DeserializeSessionDescription(msg string) (*webrtc.SessionDescription, error) {
  23. var parsed map[string]interface{}
  24. err := json.Unmarshal([]byte(msg), &parsed)
  25. if err != nil {
  26. return nil, err
  27. }
  28. if _, ok := parsed["type"]; !ok {
  29. return nil, errors.New("cannot deserialize SessionDescription without type field")
  30. }
  31. if _, ok := parsed["sdp"]; !ok {
  32. return nil, errors.New("cannot deserialize SessionDescription without sdp field")
  33. }
  34. var stype webrtc.SDPType
  35. switch parsed["type"].(string) {
  36. default:
  37. return nil, errors.New("Unknown SDP type")
  38. case "offer":
  39. stype = webrtc.SDPTypeOffer
  40. case "pranswer":
  41. stype = webrtc.SDPTypePranswer
  42. case "answer":
  43. stype = webrtc.SDPTypeAnswer
  44. case "rollback":
  45. stype = webrtc.SDPTypeRollback
  46. }
  47. return &webrtc.SessionDescription{
  48. Type: stype,
  49. SDP: parsed["sdp"].(string),
  50. }, nil
  51. }
  52. func IsLocal(ip net.IP) bool {
  53. if ip.IsPrivate() {
  54. return true
  55. }
  56. // Dynamic Configuration as per https://tools.ietf.org/htm/rfc3927
  57. if ip.IsLinkLocalUnicast() {
  58. return true
  59. }
  60. if ip4 := ip.To4(); ip4 != nil {
  61. // Carrier-Grade NAT as per https://tools.ietf.org/htm/rfc6598
  62. if ip4[0] == 100 && ip4[1]&0xc0 == 64 {
  63. return true
  64. }
  65. }
  66. return false
  67. }
  68. // Removes local LAN address ICE candidates
  69. //
  70. // This is unused after https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/-/merge_requests/442,
  71. // but come in handy later for https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/-/issues/40322
  72. // Also this is exported, so let's not remove it at least until
  73. // the next major release.
  74. func StripLocalAddresses(str string) string {
  75. var desc sdp.SessionDescription
  76. err := desc.Unmarshal([]byte(str))
  77. if err != nil {
  78. return str
  79. }
  80. for _, m := range desc.MediaDescriptions {
  81. attrs := make([]sdp.Attribute, 0)
  82. for _, a := range m.Attributes {
  83. if a.IsICECandidate() {
  84. c, err := ice.UnmarshalCandidate(a.Value)
  85. if err == nil && c.Type() == ice.CandidateTypeHost {
  86. ip := net.ParseIP(c.Address())
  87. if ip != nil && (IsLocal(ip) || ip.IsUnspecified() || ip.IsLoopback()) {
  88. /* no append in this case */
  89. continue
  90. }
  91. }
  92. }
  93. attrs = append(attrs, a)
  94. }
  95. m.Attributes = attrs
  96. }
  97. bts, err := desc.Marshal()
  98. if err != nil {
  99. return str
  100. }
  101. return string(bts)
  102. }
  103. // Attempts to retrieve the client IP of where the HTTP request originating.
  104. // There is no standard way to do this since the original client IP can be included in a number of different headers,
  105. // depending on the proxies and load balancers between the client and the server. We attempt to check as many of these
  106. // headers as possible to determine a "best guess" of the client IP
  107. // Using this as a reference: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Forwarded
  108. func GetClientIp(req *http.Request) string {
  109. // We check the "Fowarded" header first, followed by the "X-Forwarded-For" header, and then use the "RemoteAddr" as
  110. // a last resort. We use the leftmost address since it is the closest one to the client.
  111. strat := realclientip.NewChainStrategy(
  112. realclientip.Must(realclientip.NewLeftmostNonPrivateStrategy("Forwarded")),
  113. realclientip.Must(realclientip.NewLeftmostNonPrivateStrategy("X-Forwarded-For")),
  114. realclientip.RemoteAddrStrategy{},
  115. )
  116. clientIp := strat.ClientIP(req.Header, req.RemoteAddr)
  117. return clientIp
  118. }
  119. // Returns a list of IP addresses of ICE candidates, roughly in descending order for accuracy for geolocation
  120. func GetCandidateAddrs(sdpStr string) []net.IP {
  121. var desc sdp.SessionDescription
  122. err := desc.Unmarshal([]byte(sdpStr))
  123. if err != nil {
  124. log.Printf("GetCandidateAddrs: failed to unmarshal SDP: %v\n", err)
  125. return []net.IP{}
  126. }
  127. iceCandidates := make([]ice.Candidate, 0)
  128. for _, m := range desc.MediaDescriptions {
  129. for _, a := range m.Attributes {
  130. if a.IsICECandidate() {
  131. c, err := ice.UnmarshalCandidate(a.Value)
  132. if err == nil {
  133. iceCandidates = append(iceCandidates, c)
  134. }
  135. }
  136. }
  137. }
  138. // ICE candidates are first sorted in asecending order of priority, to match convention of providing a custom Less
  139. // function to sort
  140. sort.Slice(iceCandidates, func(i, j int) bool {
  141. if iceCandidates[i].Type() != iceCandidates[j].Type() {
  142. // Sort by candidate type first, in the order specified in https://datatracker.ietf.org/doc/html/rfc8445#section-5.1.2.2
  143. // Higher priority candidate types are more efficient, which likely means they are closer to the client
  144. // itself, providing a more accurate result for geolocation
  145. return ice.CandidateType(iceCandidates[i].Type().Preference()) < ice.CandidateType(iceCandidates[j].Type().Preference())
  146. }
  147. // Break ties with the ICE candidate's priority property
  148. return iceCandidates[i].Priority() < iceCandidates[j].Priority()
  149. })
  150. slices.Reverse(iceCandidates)
  151. sortedIpAddr := make([]net.IP, 0)
  152. for _, c := range iceCandidates {
  153. ip := net.ParseIP(c.Address())
  154. if ip != nil {
  155. sortedIpAddr = append(sortedIpAddr, ip)
  156. }
  157. }
  158. return sortedIpAddr
  159. }