proxy.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. //Package for communication with the snowflake broker
  2. // import "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/messages"
  3. package messages
  4. import (
  5. "encoding/json"
  6. "errors"
  7. "fmt"
  8. "strings"
  9. "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/nat"
  10. )
  11. const (
  12. version = "1.3"
  13. ProxyUnknown = "unknown"
  14. )
  15. var KnownProxyTypes = map[string]bool{
  16. "standalone": true,
  17. "webext": true,
  18. "badge": true,
  19. "iptproxy": true,
  20. }
  21. /* Version 1.3 specification:
  22. == ProxyPollRequest ==
  23. {
  24. Sid: [generated session id of proxy],
  25. Version: 1.3,
  26. Type: ["badge"|"webext"|"standalone"],
  27. NAT: ["unknown"|"restricted"|"unrestricted"],
  28. Clients: [number of current clients, rounded down to multiples of 8],
  29. AcceptedRelayPattern: [a pattern representing accepted set of relay domains]
  30. }
  31. == ProxyPollResponse ==
  32. 1) If a client is matched:
  33. HTTP 200 OK
  34. {
  35. Status: "client match",
  36. {
  37. type: offer,
  38. sdp: [WebRTC SDP]
  39. },
  40. NAT: ["unknown"|"restricted"|"unrestricted"],
  41. RelayURL: [the WebSocket URL proxy should connect to relay Snowflake traffic]
  42. }
  43. 2) If a client is not matched:
  44. HTTP 200 OK
  45. {
  46. Status: "no match"
  47. }
  48. 3) If the request is malformed:
  49. HTTP 400 BadRequest
  50. == ProxyAnswerRequest ==
  51. {
  52. Sid: [generated session id of proxy],
  53. Version: 1.3,
  54. Answer:
  55. {
  56. type: answer,
  57. sdp: [WebRTC SDP]
  58. }
  59. }
  60. == ProxyAnswerResponse ==
  61. 1) If the client retrieved the answer:
  62. HTTP 200 OK
  63. {
  64. Status: "success"
  65. }
  66. 2) If the client left:
  67. HTTP 200 OK
  68. {
  69. Status: "client gone"
  70. }
  71. 3) If the request is malformed:
  72. HTTP 400 BadRequest
  73. */
  74. type ProxyPollRequest struct {
  75. Sid string
  76. Version string
  77. Type string
  78. NAT string
  79. Clients int
  80. AcceptedRelayPattern *string
  81. }
  82. func EncodeProxyPollRequest(sid string, proxyType string, natType string, clients int) ([]byte, error) {
  83. return EncodeProxyPollRequestWithRelayPrefix(sid, proxyType, natType, clients, "")
  84. }
  85. func EncodeProxyPollRequestWithRelayPrefix(sid string, proxyType string, natType string, clients int, relayPattern string) ([]byte, error) {
  86. return json.Marshal(ProxyPollRequest{
  87. Sid: sid,
  88. Version: version,
  89. Type: proxyType,
  90. NAT: natType,
  91. Clients: clients,
  92. AcceptedRelayPattern: &relayPattern,
  93. })
  94. }
  95. func DecodeProxyPollRequest(data []byte) (sid string, proxyType string, natType string, clients int, err error) {
  96. var relayPrefix string
  97. sid, proxyType, natType, clients, relayPrefix, _, err = DecodeProxyPollRequestWithRelayPrefix(data)
  98. if relayPrefix != "" {
  99. return "", "", "", 0, ErrExtraInfo
  100. }
  101. return
  102. }
  103. // Decodes a poll message from a snowflake proxy and returns the
  104. // sid, proxy type, nat type and clients of the proxy on success
  105. // and an error if it failed
  106. func DecodeProxyPollRequestWithRelayPrefix(data []byte) (
  107. sid string, proxyType string, natType string, clients int, relayPrefix string, relayPrefixAware bool, err error) {
  108. var message ProxyPollRequest
  109. err = json.Unmarshal(data, &message)
  110. if err != nil {
  111. return
  112. }
  113. majorVersion := strings.Split(message.Version, ".")[0]
  114. if majorVersion != "1" {
  115. err = fmt.Errorf("using unknown version")
  116. return
  117. }
  118. // Version 1.x requires an Sid
  119. if message.Sid == "" {
  120. err = fmt.Errorf("no supplied session id")
  121. return
  122. }
  123. switch message.NAT {
  124. case "":
  125. message.NAT = nat.NATUnknown
  126. case nat.NATUnknown:
  127. case nat.NATRestricted:
  128. case nat.NATUnrestricted:
  129. default:
  130. err = fmt.Errorf("invalid NAT type")
  131. return
  132. }
  133. // we don't reject polls with an unknown proxy type because we encourage
  134. // projects that embed proxy code to include their own type
  135. if !KnownProxyTypes[message.Type] {
  136. message.Type = ProxyUnknown
  137. }
  138. var acceptedRelayPattern = ""
  139. if message.AcceptedRelayPattern != nil {
  140. acceptedRelayPattern = *message.AcceptedRelayPattern
  141. }
  142. return message.Sid, message.Type, message.NAT, message.Clients,
  143. acceptedRelayPattern, message.AcceptedRelayPattern != nil, nil
  144. }
  145. type ProxyPollResponse struct {
  146. Status string
  147. Offer string
  148. NAT string
  149. RelayURL string
  150. }
  151. func EncodePollResponse(offer string, success bool, natType string) ([]byte, error) {
  152. return EncodePollResponseWithRelayURL(offer, success, natType, "", "no match")
  153. }
  154. func EncodePollResponseWithRelayURL(offer string, success bool, natType, relayURL, failReason string) ([]byte, error) {
  155. if success {
  156. return json.Marshal(ProxyPollResponse{
  157. Status: "client match",
  158. Offer: offer,
  159. NAT: natType,
  160. RelayURL: relayURL,
  161. })
  162. }
  163. return json.Marshal(ProxyPollResponse{
  164. Status: failReason,
  165. })
  166. }
  167. func DecodePollResponse(data []byte) (string, string, error) {
  168. offer, natType, relayURL, err := DecodePollResponseWithRelayURL(data)
  169. if relayURL != "" {
  170. return "", "", ErrExtraInfo
  171. }
  172. return offer, natType, err
  173. }
  174. // Decodes a poll response from the broker and returns an offer and the client's NAT type
  175. // If there is a client match, the returned offer string will be non-empty
  176. func DecodePollResponseWithRelayURL(data []byte) (string, string, string, error) {
  177. var message ProxyPollResponse
  178. err := json.Unmarshal(data, &message)
  179. if err != nil {
  180. return "", "", "", err
  181. }
  182. if message.Status == "" {
  183. return "", "", "", fmt.Errorf("received invalid data")
  184. }
  185. err = nil
  186. if message.Status == "client match" {
  187. if message.Offer == "" {
  188. return "", "", "", fmt.Errorf("no supplied offer")
  189. }
  190. } else {
  191. message.Offer = ""
  192. if message.Status != "no match" {
  193. err = errors.New(message.Status)
  194. }
  195. }
  196. natType := message.NAT
  197. if natType == "" {
  198. natType = "unknown"
  199. }
  200. return message.Offer, natType, message.RelayURL, err
  201. }
  202. type ProxyAnswerRequest struct {
  203. Version string
  204. Sid string
  205. Answer string
  206. }
  207. func EncodeAnswerRequest(answer string, sid string) ([]byte, error) {
  208. return json.Marshal(ProxyAnswerRequest{
  209. Version: version,
  210. Sid: sid,
  211. Answer: answer,
  212. })
  213. }
  214. // Returns the sdp answer and proxy sid
  215. func DecodeAnswerRequest(data []byte) (string, string, error) {
  216. var message ProxyAnswerRequest
  217. err := json.Unmarshal(data, &message)
  218. if err != nil {
  219. return "", "", err
  220. }
  221. majorVersion := strings.Split(message.Version, ".")[0]
  222. if majorVersion != "1" {
  223. return "", "", fmt.Errorf("using unknown version")
  224. }
  225. if message.Sid == "" || message.Answer == "" {
  226. return "", "", fmt.Errorf("no supplied sid or answer")
  227. }
  228. return message.Answer, message.Sid, nil
  229. }
  230. type ProxyAnswerResponse struct {
  231. Status string
  232. }
  233. func EncodeAnswerResponse(success bool) ([]byte, error) {
  234. if success {
  235. return json.Marshal(ProxyAnswerResponse{
  236. Status: "success",
  237. })
  238. }
  239. return json.Marshal(ProxyAnswerResponse{
  240. Status: "client gone",
  241. })
  242. }
  243. func DecodeAnswerResponse(data []byte) (bool, error) {
  244. var message ProxyAnswerResponse
  245. var success bool
  246. err := json.Unmarshal(data, &message)
  247. if err != nil {
  248. return success, err
  249. }
  250. if message.Status == "" {
  251. return success, fmt.Errorf("received invalid data")
  252. }
  253. if message.Status == "success" {
  254. success = true
  255. }
  256. return success, nil
  257. }