serverEntry.go 6.8 KB

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