serverEntry.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright (c) 2015, 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 psiphon
  20. import (
  21. "bytes"
  22. "encoding/hex"
  23. "encoding/json"
  24. "errors"
  25. "fmt"
  26. "net"
  27. "strings"
  28. )
  29. const (
  30. TUNNEL_PROTOCOL_SSH = "SSH"
  31. TUNNEL_PROTOCOL_OBFUSCATED_SSH = "OSSH"
  32. TUNNEL_PROTOCOL_UNFRONTED_MEEK = "UNFRONTED-MEEK-OSSH"
  33. TUNNEL_PROTOCOL_UNFRONTED_MEEK_HTTPS = "UNFRONTED-MEEK-HTTPS-OSSH"
  34. TUNNEL_PROTOCOL_FRONTED_MEEK = "FRONTED-MEEK-OSSH"
  35. )
  36. var SupportedTunnelProtocols = []string{
  37. TUNNEL_PROTOCOL_FRONTED_MEEK,
  38. TUNNEL_PROTOCOL_UNFRONTED_MEEK,
  39. TUNNEL_PROTOCOL_UNFRONTED_MEEK_HTTPS,
  40. TUNNEL_PROTOCOL_OBFUSCATED_SSH,
  41. TUNNEL_PROTOCOL_SSH,
  42. }
  43. // ServerEntry represents a Psiphon server. It contains information
  44. // about how to estalish a tunnel connection to the server through
  45. // several protocols. ServerEntry are JSON records downloaded from
  46. // various sources.
  47. type ServerEntry struct {
  48. IpAddress string `json:"ipAddress"`
  49. WebServerPort string `json:"webServerPort"` // not an int
  50. WebServerSecret string `json:"webServerSecret"`
  51. WebServerCertificate string `json:"webServerCertificate"`
  52. SshPort int `json:"sshPort"`
  53. SshUsername string `json:"sshUsername"`
  54. SshPassword string `json:"sshPassword"`
  55. SshHostKey string `json:"sshHostKey"`
  56. SshObfuscatedPort int `json:"sshObfuscatedPort"`
  57. SshObfuscatedKey string `json:"sshObfuscatedKey"`
  58. Capabilities []string `json:"capabilities"`
  59. Region string `json:"region"`
  60. MeekServerPort int `json:"meekServerPort"`
  61. MeekCookieEncryptionPublicKey string `json:"meekCookieEncryptionPublicKey"`
  62. MeekObfuscatedKey string `json:"meekObfuscatedKey"`
  63. MeekFrontingHost string `json:"meekFrontingHost"`
  64. MeekFrontingDomain string `json:"meekFrontingDomain"`
  65. MeekFrontingAddresses []string `json:"meekFrontingAddresses"`
  66. MeekFrontingAddressesRegex string `json:"meekFrontingAddressesRegex"`
  67. }
  68. // SupportsProtocol returns true if and only if the ServerEntry has
  69. // the necessary capability to support the specified tunnel protocol.
  70. func (serverEntry *ServerEntry) SupportsProtocol(protocol string) bool {
  71. requiredCapability := strings.TrimSuffix(protocol, "-OSSH")
  72. return Contains(serverEntry.Capabilities, requiredCapability)
  73. }
  74. // GetSupportedProtocols returns a list of tunnel protocols supported
  75. // by the ServerEntry's capabilities.
  76. func (serverEntry *ServerEntry) GetSupportedProtocols() []string {
  77. supportedProtocols := make([]string, 0)
  78. for _, protocol := range SupportedTunnelProtocols {
  79. if serverEntry.SupportsProtocol(protocol) {
  80. supportedProtocols = append(supportedProtocols, protocol)
  81. }
  82. }
  83. return supportedProtocols
  84. }
  85. // DisableImpairedProtocols modifies the ServerEntry to disable
  86. // the specified protocols.
  87. // Note: this assumes that protocol capabilities are 1-to-1.
  88. func (serverEntry *ServerEntry) DisableImpairedProtocols(impairedProtocols []string) {
  89. capabilities := make([]string, 0)
  90. for _, capability := range serverEntry.Capabilities {
  91. omit := false
  92. for _, protocol := range impairedProtocols {
  93. requiredCapability := strings.TrimSuffix(protocol, "-OSSH")
  94. if capability == requiredCapability {
  95. omit = true
  96. break
  97. }
  98. }
  99. if !omit {
  100. capabilities = append(capabilities, capability)
  101. }
  102. }
  103. serverEntry.Capabilities = capabilities
  104. }
  105. func (serverEntry *ServerEntry) GetDirectWebRequestPorts() []string {
  106. ports := make([]string, 0)
  107. if Contains(serverEntry.Capabilities, "handshake") {
  108. // Server-side configuration quirk: there's a port forward from
  109. // port 443 to the web server, which we can try, except on servers
  110. // running FRONTED_MEEK, which listens on port 443.
  111. if !serverEntry.SupportsProtocol(TUNNEL_PROTOCOL_FRONTED_MEEK) {
  112. ports = append(ports, "443")
  113. }
  114. ports = append(ports, serverEntry.WebServerPort)
  115. }
  116. return ports
  117. }
  118. // DecodeServerEntry extracts server entries from the encoding
  119. // used by remote server lists and Psiphon server handshake requests.
  120. func DecodeServerEntry(encodedServerEntry string) (serverEntry *ServerEntry, err error) {
  121. hexDecodedServerEntry, err := hex.DecodeString(encodedServerEntry)
  122. if err != nil {
  123. return nil, ContextError(err)
  124. }
  125. // Skip past legacy format (4 space delimited fields) and just parse the JSON config
  126. fields := bytes.SplitN(hexDecodedServerEntry, []byte(" "), 5)
  127. if len(fields) != 5 {
  128. return nil, ContextError(errors.New("invalid encoded server entry"))
  129. }
  130. serverEntry = new(ServerEntry)
  131. err = json.Unmarshal(fields[4], &serverEntry)
  132. if err != nil {
  133. return nil, ContextError(err)
  134. }
  135. return serverEntry, nil
  136. }
  137. // ValidateServerEntry checks for malformed server entries.
  138. // Currently, it checks for a valid ipAddress. This is important since
  139. // handshake requests submit back to the server a list of known server
  140. // IP addresses and the handshake API expects well-formed inputs.
  141. // TODO: validate more fields
  142. func ValidateServerEntry(serverEntry *ServerEntry) error {
  143. ipAddr := net.ParseIP(serverEntry.IpAddress)
  144. if ipAddr == nil {
  145. errMsg := fmt.Sprintf("server entry has invalid IpAddress: '%s'", serverEntry.IpAddress)
  146. // Some callers skip invalid server entries without propagating
  147. // the error mesage, so issue a notice.
  148. NoticeAlert(errMsg)
  149. return ContextError(errors.New(errMsg))
  150. }
  151. return nil
  152. }
  153. // DecodeAndValidateServerEntryList extracts server entries from the list encoding
  154. // used by remote server lists and Psiphon server handshake requests.
  155. // Each server entry is validated and invalid entries are skipped.
  156. func DecodeAndValidateServerEntryList(encodedServerEntryList string) (serverEntries []*ServerEntry, err error) {
  157. serverEntries = make([]*ServerEntry, 0)
  158. for _, encodedServerEntry := range strings.Split(encodedServerEntryList, "\n") {
  159. if len(encodedServerEntry) == 0 {
  160. continue
  161. }
  162. // TODO: skip this entry and continue if can't decode?
  163. serverEntry, err := DecodeServerEntry(encodedServerEntry)
  164. if err != nil {
  165. return nil, ContextError(err)
  166. }
  167. if ValidateServerEntry(serverEntry) != nil {
  168. // Skip this entry and continue with the next one
  169. continue
  170. }
  171. serverEntries = append(serverEntries, serverEntry)
  172. }
  173. return serverEntries, nil
  174. }