protocol.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (c) 2016, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package protocol
  20. import (
  21. "encoding/json"
  22. "fmt"
  23. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  24. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/osl"
  25. )
  26. const (
  27. TUNNEL_PROTOCOL_SSH = "SSH"
  28. TUNNEL_PROTOCOL_OBFUSCATED_SSH = "OSSH"
  29. TUNNEL_PROTOCOL_UNFRONTED_MEEK = "UNFRONTED-MEEK-OSSH"
  30. TUNNEL_PROTOCOL_UNFRONTED_MEEK_HTTPS = "UNFRONTED-MEEK-HTTPS-OSSH"
  31. TUNNEL_PROTOCOL_UNFRONTED_MEEK_SESSION_TICKET = "UNFRONTED-MEEK-SESSION-TICKET-OSSH"
  32. TUNNEL_PROTOCOL_FRONTED_MEEK = "FRONTED-MEEK-OSSH"
  33. TUNNEL_PROTOCOL_FRONTED_MEEK_HTTP = "FRONTED-MEEK-HTTP-OSSH"
  34. TUNNEL_PROTOCOL_QUIC_OBFUSCATED_SSH = "QUIC-OSSH"
  35. SERVER_ENTRY_SOURCE_EMBEDDED = "EMBEDDED"
  36. SERVER_ENTRY_SOURCE_REMOTE = "REMOTE"
  37. SERVER_ENTRY_SOURCE_DISCOVERY = "DISCOVERY"
  38. SERVER_ENTRY_SOURCE_TARGET = "TARGET"
  39. SERVER_ENTRY_SOURCE_OBFUSCATED = "OBFUSCATED"
  40. CAPABILITY_SSH_API_REQUESTS = "ssh-api-requests"
  41. CAPABILITY_UNTUNNELED_WEB_API_REQUESTS = "handshake"
  42. CLIENT_CAPABILITY_SERVER_REQUESTS = "server-requests"
  43. PSIPHON_API_HANDSHAKE_REQUEST_NAME = "psiphon-handshake"
  44. PSIPHON_API_CONNECTED_REQUEST_NAME = "psiphon-connected"
  45. PSIPHON_API_STATUS_REQUEST_NAME = "psiphon-status"
  46. PSIPHON_API_OSL_REQUEST_NAME = "psiphon-osl"
  47. // PSIPHON_API_CLIENT_VERIFICATION_REQUEST_NAME may still be used by older Android clients
  48. PSIPHON_API_CLIENT_VERIFICATION_REQUEST_NAME = "psiphon-client-verification"
  49. PSIPHON_API_CLIENT_SESSION_ID_LENGTH = 16
  50. PSIPHON_SSH_API_PROTOCOL = "ssh"
  51. PSIPHON_WEB_API_PROTOCOL = "web"
  52. PACKET_TUNNEL_CHANNEL_TYPE = "tun@psiphon.ca"
  53. PSIPHON_API_HANDSHAKE_AUTHORIZATIONS = "authorizations"
  54. )
  55. type TunnelProtocols []string
  56. func (t TunnelProtocols) Validate() error {
  57. for _, p := range t {
  58. if !common.Contains(SupportedTunnelProtocols, p) {
  59. return common.ContextError(fmt.Errorf("invalid tunnel protocol: %s", p))
  60. }
  61. }
  62. return nil
  63. }
  64. var SupportedTunnelProtocols = TunnelProtocols{
  65. TUNNEL_PROTOCOL_SSH,
  66. TUNNEL_PROTOCOL_OBFUSCATED_SSH,
  67. TUNNEL_PROTOCOL_UNFRONTED_MEEK,
  68. TUNNEL_PROTOCOL_UNFRONTED_MEEK_HTTPS,
  69. TUNNEL_PROTOCOL_UNFRONTED_MEEK_SESSION_TICKET,
  70. TUNNEL_PROTOCOL_FRONTED_MEEK,
  71. TUNNEL_PROTOCOL_FRONTED_MEEK_HTTP,
  72. TUNNEL_PROTOCOL_QUIC_OBFUSCATED_SSH,
  73. }
  74. var SupportedServerEntrySources = TunnelProtocols{
  75. SERVER_ENTRY_SOURCE_EMBEDDED,
  76. SERVER_ENTRY_SOURCE_REMOTE,
  77. SERVER_ENTRY_SOURCE_DISCOVERY,
  78. SERVER_ENTRY_SOURCE_TARGET,
  79. SERVER_ENTRY_SOURCE_OBFUSCATED,
  80. }
  81. func TunnelProtocolUsesSSH(protocol string) bool {
  82. return true
  83. }
  84. func TunnelProtocolUsesObfuscatedSSH(protocol string) bool {
  85. return protocol != TUNNEL_PROTOCOL_SSH
  86. }
  87. func TunnelProtocolUsesMeek(protocol string) bool {
  88. return TunnelProtocolUsesMeekHTTP(protocol) ||
  89. TunnelProtocolUsesMeekHTTPS(protocol)
  90. }
  91. func TunnelProtocolUsesMeekHTTP(protocol string) bool {
  92. return protocol == TUNNEL_PROTOCOL_UNFRONTED_MEEK ||
  93. protocol == TUNNEL_PROTOCOL_FRONTED_MEEK_HTTP
  94. }
  95. func TunnelProtocolUsesMeekHTTPS(protocol string) bool {
  96. return protocol == TUNNEL_PROTOCOL_FRONTED_MEEK ||
  97. protocol == TUNNEL_PROTOCOL_UNFRONTED_MEEK_HTTPS ||
  98. protocol == TUNNEL_PROTOCOL_UNFRONTED_MEEK_SESSION_TICKET
  99. }
  100. func TunnelProtocolUsesObfuscatedSessionTickets(protocol string) bool {
  101. return protocol == TUNNEL_PROTOCOL_UNFRONTED_MEEK_SESSION_TICKET
  102. }
  103. func TunnelProtocolUsesQUIC(protocol string) bool {
  104. return protocol == TUNNEL_PROTOCOL_QUIC_OBFUSCATED_SSH
  105. }
  106. func UseClientTunnelProtocol(
  107. clientProtocol string,
  108. serverProtocols TunnelProtocols) bool {
  109. // When the server is running _both_ fronted HTTP and
  110. // fronted HTTPS, use the client's reported tunnel
  111. // protocol since some CDNs forward both to the same
  112. // server port; in this case the server port is not
  113. // sufficient to distinguish these protocols.
  114. if (clientProtocol == TUNNEL_PROTOCOL_FRONTED_MEEK ||
  115. clientProtocol == TUNNEL_PROTOCOL_FRONTED_MEEK_HTTP) &&
  116. common.Contains(serverProtocols, TUNNEL_PROTOCOL_FRONTED_MEEK) &&
  117. common.Contains(serverProtocols, TUNNEL_PROTOCOL_FRONTED_MEEK_HTTP) {
  118. return true
  119. }
  120. return false
  121. }
  122. const (
  123. TLS_PROFILE_IOS_1131 = "iOS-Safari-11.3.1"
  124. TLS_PROFILE_ANDROID_60 = "Android-6.0"
  125. TLS_PROFILE_ANDROID_51 = "Android-5.1"
  126. TLS_PROFILE_CHROME_58 = "Chrome-58"
  127. TLS_PROFILE_CHROME_57 = "Chrome-57"
  128. TLS_PROFILE_FIREFOX_56 = "Firefox-56"
  129. TLS_PROFILE_RANDOMIZED = "Randomized"
  130. )
  131. var SupportedTLSProfiles = TLSProfiles{
  132. TLS_PROFILE_IOS_1131,
  133. TLS_PROFILE_ANDROID_60,
  134. TLS_PROFILE_ANDROID_51,
  135. TLS_PROFILE_CHROME_58,
  136. TLS_PROFILE_CHROME_57,
  137. TLS_PROFILE_FIREFOX_56,
  138. TLS_PROFILE_RANDOMIZED,
  139. }
  140. type TLSProfiles []string
  141. func (profiles TLSProfiles) Validate() error {
  142. for _, p := range profiles {
  143. if !common.Contains(SupportedTLSProfiles, p) {
  144. return common.ContextError(fmt.Errorf("invalid TLS profile: %s", p))
  145. }
  146. }
  147. return nil
  148. }
  149. type HandshakeResponse struct {
  150. SSHSessionID string `json:"ssh_session_id"`
  151. Homepages []string `json:"homepages"`
  152. UpgradeClientVersion string `json:"upgrade_client_version"`
  153. PageViewRegexes []map[string]string `json:"page_view_regexes"`
  154. HttpsRequestRegexes []map[string]string `json:"https_request_regexes"`
  155. EncodedServerList []string `json:"encoded_server_list"`
  156. ClientRegion string `json:"client_region"`
  157. ServerTimestamp string `json:"server_timestamp"`
  158. ActiveAuthorizationIDs []string `json:"active_authorization_ids"`
  159. TacticsPayload json.RawMessage `json:"tactics_payload"`
  160. }
  161. type ConnectedResponse struct {
  162. ConnectedTimestamp string `json:"connected_timestamp"`
  163. }
  164. type OSLRequest struct {
  165. ClearLocalSLOKs bool `json:"clear_local_sloks"`
  166. SeedPayload *osl.SeedPayload `json:"seed_payload"`
  167. }
  168. type SSHPasswordPayload struct {
  169. SessionId string `json:"SessionId"`
  170. SshPassword string `json:"SshPassword"`
  171. ClientCapabilities []string `json:"ClientCapabilities"`
  172. }
  173. type MeekCookieData struct {
  174. MeekProtocolVersion int `json:"v"`
  175. ClientTunnelProtocol string `json:"t"`
  176. EndPoint string `json:"e"`
  177. }