iceserver.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. //go:build !js
  4. // +build !js
  5. package webrtc
  6. import (
  7. "encoding/json"
  8. "github.com/pion/stun"
  9. "github.com/pion/webrtc/v3/pkg/rtcerr"
  10. )
  11. // ICEServer describes a single STUN and TURN server that can be used by
  12. // the ICEAgent to establish a connection with a peer.
  13. type ICEServer struct {
  14. URLs []string `json:"urls"`
  15. Username string `json:"username,omitempty"`
  16. Credential interface{} `json:"credential,omitempty"`
  17. CredentialType ICECredentialType `json:"credentialType,omitempty"`
  18. }
  19. func (s ICEServer) parseURL(i int) (*stun.URI, error) {
  20. return stun.ParseURI(s.URLs[i])
  21. }
  22. func (s ICEServer) validate() error {
  23. _, err := s.urls()
  24. return err
  25. }
  26. func (s ICEServer) urls() ([]*stun.URI, error) {
  27. urls := []*stun.URI{}
  28. for i := range s.URLs {
  29. url, err := s.parseURL(i)
  30. if err != nil {
  31. return nil, &rtcerr.InvalidAccessError{Err: err}
  32. }
  33. if url.Scheme == stun.SchemeTypeTURN || url.Scheme == stun.SchemeTypeTURNS {
  34. // https://www.w3.org/TR/webrtc/#set-the-configuration (step #11.3.2)
  35. if s.Username == "" || s.Credential == nil {
  36. return nil, &rtcerr.InvalidAccessError{Err: ErrNoTurnCredentials}
  37. }
  38. url.Username = s.Username
  39. switch s.CredentialType {
  40. case ICECredentialTypePassword:
  41. // https://www.w3.org/TR/webrtc/#set-the-configuration (step #11.3.3)
  42. password, ok := s.Credential.(string)
  43. if !ok {
  44. return nil, &rtcerr.InvalidAccessError{Err: ErrTurnCredentials}
  45. }
  46. url.Password = password
  47. case ICECredentialTypeOauth:
  48. // https://www.w3.org/TR/webrtc/#set-the-configuration (step #11.3.4)
  49. if _, ok := s.Credential.(OAuthCredential); !ok {
  50. return nil, &rtcerr.InvalidAccessError{Err: ErrTurnCredentials}
  51. }
  52. default:
  53. return nil, &rtcerr.InvalidAccessError{Err: ErrTurnCredentials}
  54. }
  55. }
  56. urls = append(urls, url)
  57. }
  58. return urls, nil
  59. }
  60. func iceserverUnmarshalUrls(val interface{}) (*[]string, error) {
  61. s, ok := val.([]interface{})
  62. if !ok {
  63. return nil, errInvalidICEServer
  64. }
  65. out := make([]string, len(s))
  66. for idx, url := range s {
  67. out[idx], ok = url.(string)
  68. if !ok {
  69. return nil, errInvalidICEServer
  70. }
  71. }
  72. return &out, nil
  73. }
  74. func iceserverUnmarshalOauth(val interface{}) (*OAuthCredential, error) {
  75. c, ok := val.(map[string]interface{})
  76. if !ok {
  77. return nil, errInvalidICEServer
  78. }
  79. MACKey, ok := c["MACKey"].(string)
  80. if !ok {
  81. return nil, errInvalidICEServer
  82. }
  83. AccessToken, ok := c["AccessToken"].(string)
  84. if !ok {
  85. return nil, errInvalidICEServer
  86. }
  87. return &OAuthCredential{
  88. MACKey: MACKey,
  89. AccessToken: AccessToken,
  90. }, nil
  91. }
  92. func (s *ICEServer) iceserverUnmarshalFields(m map[string]interface{}) error {
  93. if val, ok := m["urls"]; ok {
  94. u, err := iceserverUnmarshalUrls(val)
  95. if err != nil {
  96. return err
  97. }
  98. s.URLs = *u
  99. } else {
  100. s.URLs = []string{}
  101. }
  102. if val, ok := m["username"]; ok {
  103. s.Username, ok = val.(string)
  104. if !ok {
  105. return errInvalidICEServer
  106. }
  107. }
  108. if val, ok := m["credentialType"]; ok {
  109. ct, ok := val.(string)
  110. if !ok {
  111. return errInvalidICEServer
  112. }
  113. tpe, err := newICECredentialType(ct)
  114. if err != nil {
  115. return err
  116. }
  117. s.CredentialType = tpe
  118. } else {
  119. s.CredentialType = ICECredentialTypePassword
  120. }
  121. if val, ok := m["credential"]; ok {
  122. switch s.CredentialType {
  123. case ICECredentialTypePassword:
  124. s.Credential = val
  125. case ICECredentialTypeOauth:
  126. c, err := iceserverUnmarshalOauth(val)
  127. if err != nil {
  128. return err
  129. }
  130. s.Credential = *c
  131. default:
  132. return errInvalidICECredentialTypeString
  133. }
  134. }
  135. return nil
  136. }
  137. // UnmarshalJSON parses the JSON-encoded data and stores the result
  138. func (s *ICEServer) UnmarshalJSON(b []byte) error {
  139. var tmp interface{}
  140. err := json.Unmarshal(b, &tmp)
  141. if err != nil {
  142. return err
  143. }
  144. if m, ok := tmp.(map[string]interface{}); ok {
  145. return s.iceserverUnmarshalFields(m)
  146. }
  147. return errInvalidICEServer
  148. }
  149. // MarshalJSON returns the JSON encoding
  150. func (s ICEServer) MarshalJSON() ([]byte, error) {
  151. m := make(map[string]interface{})
  152. m["urls"] = s.URLs
  153. if s.Username != "" {
  154. m["username"] = s.Username
  155. }
  156. if s.Credential != nil {
  157. m["credential"] = s.Credential
  158. }
  159. m["credentialType"] = s.CredentialType
  160. return json.Marshal(m)
  161. }