util.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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/v2"
  11. "github.com/pion/sdp/v3"
  12. "github.com/pion/webrtc/v3"
  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. // Stolen from https://github.com/golang/go/pull/30278
  53. func IsLocal(ip net.IP) bool {
  54. if ip4 := ip.To4(); ip4 != nil {
  55. // Local IPv4 addresses are defined in https://tools.ietf.org/html/rfc1918
  56. return ip4[0] == 10 ||
  57. (ip4[0] == 172 && ip4[1]&0xf0 == 16) ||
  58. (ip4[0] == 192 && ip4[1] == 168) ||
  59. // Carrier-Grade NAT as per https://tools.ietf.org/htm/rfc6598
  60. (ip4[0] == 100 && ip4[1]&0xc0 == 64) ||
  61. // Dynamic Configuration as per https://tools.ietf.org/htm/rfc3927
  62. (ip4[0] == 169 && ip4[1] == 254)
  63. }
  64. // Local IPv6 addresses are defined in https://tools.ietf.org/html/rfc4193
  65. return len(ip) == net.IPv6len && ip[0]&0xfe == 0xfc
  66. }
  67. // Removes local LAN address ICE candidates
  68. func StripLocalAddresses(str string) string {
  69. var desc sdp.SessionDescription
  70. err := desc.Unmarshal([]byte(str))
  71. if err != nil {
  72. return str
  73. }
  74. for _, m := range desc.MediaDescriptions {
  75. attrs := make([]sdp.Attribute, 0)
  76. for _, a := range m.Attributes {
  77. if a.IsICECandidate() {
  78. c, err := ice.UnmarshalCandidate(a.Value)
  79. if err == nil && c.Type() == ice.CandidateTypeHost {
  80. ip := net.ParseIP(c.Address())
  81. if ip != nil && (IsLocal(ip) || ip.IsUnspecified() || ip.IsLoopback()) {
  82. /* no append in this case */
  83. continue
  84. }
  85. }
  86. }
  87. attrs = append(attrs, a)
  88. }
  89. m.Attributes = attrs
  90. }
  91. bts, err := desc.Marshal()
  92. if err != nil {
  93. return str
  94. }
  95. return string(bts)
  96. }
  97. // Attempts to retrieve the client IP of where the HTTP request originating.
  98. // There is no standard way to do this since the original client IP can be included in a number of different headers,
  99. // depending on the proxies and load balancers between the client and the server. We attempt to check as many of these
  100. // headers as possible to determine a "best guess" of the client IP
  101. // Using this as a reference: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Forwarded
  102. func GetClientIp(req *http.Request) string {
  103. // We check the "Fowarded" header first, followed by the "X-Forwarded-For" header, and then use the "RemoteAddr" as
  104. // a last resort. We use the leftmost address since it is the closest one to the client.
  105. strat := realclientip.NewChainStrategy(
  106. realclientip.Must(realclientip.NewLeftmostNonPrivateStrategy("Forwarded")),
  107. realclientip.Must(realclientip.NewLeftmostNonPrivateStrategy("X-Forwarded-For")),
  108. realclientip.RemoteAddrStrategy{},
  109. )
  110. clientIp := strat.ClientIP(req.Header, req.RemoteAddr)
  111. return clientIp
  112. }
  113. // Returns a list of IP addresses of ICE candidates, roughly in descending order for accuracy for geolocation
  114. func GetCandidateAddrs(sdpStr string) []net.IP {
  115. var desc sdp.SessionDescription
  116. err := desc.Unmarshal([]byte(sdpStr))
  117. if err != nil {
  118. log.Printf("GetCandidateAddrs: failed to unmarshal SDP: %v\n", err)
  119. return []net.IP{}
  120. }
  121. iceCandidates := make([]ice.Candidate, 0)
  122. for _, m := range desc.MediaDescriptions {
  123. for _, a := range m.Attributes {
  124. if a.IsICECandidate() {
  125. c, err := ice.UnmarshalCandidate(a.Value)
  126. if err == nil {
  127. iceCandidates = append(iceCandidates, c)
  128. }
  129. }
  130. }
  131. }
  132. // ICE candidates are first sorted in asecending order of priority, to match convention of providing a custom Less
  133. // function to sort
  134. sort.Slice(iceCandidates, func(i, j int) bool {
  135. if iceCandidates[i].Type() != iceCandidates[j].Type() {
  136. // Sort by candidate type first, in the order specified in https://datatracker.ietf.org/doc/html/rfc8445#section-5.1.2.2
  137. // Higher priority candidate types are more efficient, which likely means they are closer to the client
  138. // itself, providing a more accurate result for geolocation
  139. return ice.CandidateType(iceCandidates[i].Type().Preference()) < ice.CandidateType(iceCandidates[j].Type().Preference())
  140. }
  141. // Break ties with the ICE candidate's priority property
  142. return iceCandidates[i].Priority() < iceCandidates[j].Priority()
  143. })
  144. slices.Reverse(iceCandidates)
  145. sortedIpAddr := make([]net.IP, 0)
  146. for _, c := range iceCandidates {
  147. ip := net.ParseIP(c.Address())
  148. if ip != nil {
  149. sortedIpAddr = append(sortedIpAddr, ip)
  150. }
  151. }
  152. return sortedIpAddr
  153. }