protocol.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  22. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/osl"
  23. )
  24. const (
  25. TUNNEL_PROTOCOL_SSH = "SSH"
  26. TUNNEL_PROTOCOL_OBFUSCATED_SSH = "OSSH"
  27. TUNNEL_PROTOCOL_UNFRONTED_MEEK = "UNFRONTED-MEEK-OSSH"
  28. TUNNEL_PROTOCOL_UNFRONTED_MEEK_HTTPS = "UNFRONTED-MEEK-HTTPS-OSSH"
  29. TUNNEL_PROTOCOL_UNFRONTED_MEEK_SESSION_TICKET = "UNFRONTED-MEEK-SESSION-TICKET-OSSH"
  30. TUNNEL_PROTOCOL_FRONTED_MEEK = "FRONTED-MEEK-OSSH"
  31. TUNNEL_PROTOCOL_FRONTED_MEEK_HTTP = "FRONTED-MEEK-HTTP-OSSH"
  32. SERVER_ENTRY_SOURCE_EMBEDDED = "EMBEDDED"
  33. SERVER_ENTRY_SOURCE_REMOTE = "REMOTE"
  34. SERVER_ENTRY_SOURCE_DISCOVERY = "DISCOVERY"
  35. SERVER_ENTRY_SOURCE_TARGET = "TARGET"
  36. SERVER_ENTRY_SOURCE_OBFUSCATED = "OBFUSCATED"
  37. CAPABILITY_SSH_API_REQUESTS = "ssh-api-requests"
  38. CAPABILITY_UNTUNNELED_WEB_API_REQUESTS = "handshake"
  39. CLIENT_CAPABILITY_SERVER_REQUESTS = "server-requests"
  40. PSIPHON_API_HANDSHAKE_REQUEST_NAME = "psiphon-handshake"
  41. PSIPHON_API_CONNECTED_REQUEST_NAME = "psiphon-connected"
  42. PSIPHON_API_STATUS_REQUEST_NAME = "psiphon-status"
  43. PSIPHON_API_CLIENT_VERIFICATION_REQUEST_NAME = "psiphon-client-verification"
  44. PSIPHON_API_OSL_REQUEST_NAME = "psiphon-osl"
  45. PSIPHON_API_CLIENT_SESSION_ID_LENGTH = 16
  46. PSIPHON_SSH_API_PROTOCOL = "ssh"
  47. PSIPHON_WEB_API_PROTOCOL = "web"
  48. PACKET_TUNNEL_CHANNEL_TYPE = "tun@psiphon.ca"
  49. )
  50. var SupportedTunnelProtocols = []string{
  51. TUNNEL_PROTOCOL_FRONTED_MEEK,
  52. TUNNEL_PROTOCOL_FRONTED_MEEK_HTTP,
  53. TUNNEL_PROTOCOL_UNFRONTED_MEEK,
  54. TUNNEL_PROTOCOL_UNFRONTED_MEEK_HTTPS,
  55. TUNNEL_PROTOCOL_UNFRONTED_MEEK_SESSION_TICKET,
  56. TUNNEL_PROTOCOL_OBFUSCATED_SSH,
  57. TUNNEL_PROTOCOL_SSH,
  58. }
  59. var SupportedServerEntrySources = []string{
  60. SERVER_ENTRY_SOURCE_EMBEDDED,
  61. SERVER_ENTRY_SOURCE_REMOTE,
  62. SERVER_ENTRY_SOURCE_DISCOVERY,
  63. SERVER_ENTRY_SOURCE_TARGET,
  64. SERVER_ENTRY_SOURCE_OBFUSCATED,
  65. }
  66. func TunnelProtocolUsesSSH(protocol string) bool {
  67. return true
  68. }
  69. func TunnelProtocolUsesObfuscatedSSH(protocol string) bool {
  70. return protocol != TUNNEL_PROTOCOL_SSH
  71. }
  72. func TunnelProtocolUsesMeek(protocol string) bool {
  73. return TunnelProtocolUsesMeekHTTP(protocol) ||
  74. TunnelProtocolUsesMeekHTTPS(protocol)
  75. }
  76. func TunnelProtocolUsesMeekHTTP(protocol string) bool {
  77. return protocol == TUNNEL_PROTOCOL_UNFRONTED_MEEK ||
  78. protocol == TUNNEL_PROTOCOL_FRONTED_MEEK_HTTP
  79. }
  80. func TunnelProtocolUsesMeekHTTPS(protocol string) bool {
  81. return protocol == TUNNEL_PROTOCOL_FRONTED_MEEK ||
  82. protocol == TUNNEL_PROTOCOL_UNFRONTED_MEEK_HTTPS ||
  83. protocol == TUNNEL_PROTOCOL_UNFRONTED_MEEK_SESSION_TICKET
  84. }
  85. func TunnelProtocolUsesObfuscatedSessionTickets(protocol string) bool {
  86. return protocol == TUNNEL_PROTOCOL_UNFRONTED_MEEK_SESSION_TICKET
  87. }
  88. func UseClientTunnelProtocol(
  89. clientProtocol string,
  90. serverProtocols []string) bool {
  91. // When the server is running _both_ fronted HTTP and
  92. // fronted HTTPS, use the client's reported tunnel
  93. // protocol since some CDNs forward both to the same
  94. // server port; in this case the server port is not
  95. // sufficient to distinguish these protocols.
  96. if (clientProtocol == TUNNEL_PROTOCOL_FRONTED_MEEK ||
  97. clientProtocol == TUNNEL_PROTOCOL_FRONTED_MEEK_HTTP) &&
  98. common.Contains(serverProtocols, TUNNEL_PROTOCOL_FRONTED_MEEK) &&
  99. common.Contains(serverProtocols, TUNNEL_PROTOCOL_FRONTED_MEEK_HTTP) {
  100. return true
  101. }
  102. return false
  103. }
  104. type HandshakeResponse struct {
  105. SSHSessionID string `json:"ssh_session_id"`
  106. Homepages []string `json:"homepages"`
  107. UpgradeClientVersion string `json:"upgrade_client_version"`
  108. PageViewRegexes []map[string]string `json:"page_view_regexes"`
  109. HttpsRequestRegexes []map[string]string `json:"https_request_regexes"`
  110. EncodedServerList []string `json:"encoded_server_list"`
  111. ClientRegion string `json:"client_region"`
  112. ServerTimestamp string `json:"server_timestamp"`
  113. }
  114. type ConnectedResponse struct {
  115. ConnectedTimestamp string `json:"connected_timestamp"`
  116. }
  117. type OSLRequest struct {
  118. ClearLocalSLOKs bool `json:"clear_local_sloks"`
  119. SeedPayload *osl.SeedPayload `json:"seed_payload"`
  120. }
  121. type SSHPasswordPayload struct {
  122. SessionId string `json:"SessionId"`
  123. SshPassword string `json:"SshPassword"`
  124. ClientCapabilities []string `json:"ClientCapabilities"`
  125. }
  126. type MeekCookieData struct {
  127. ServerAddress string `json:"p"`
  128. SessionID string `json:"s"`
  129. MeekProtocolVersion int `json:"v"`
  130. ClientTunnelProtocol string `json:"t"`
  131. }