serverEntry.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. // ServerEntry represents a Psiphon server. It contains information
  30. // about how to estalish a tunnel connection to the server through
  31. // several protocols. ServerEntry are JSON records downloaded from
  32. // various sources.
  33. type ServerEntry struct {
  34. IpAddress string `json:"ipAddress"`
  35. WebServerPort string `json:"webServerPort"` // not an int
  36. WebServerSecret string `json:"webServerSecret"`
  37. WebServerCertificate string `json:"webServerCertificate"`
  38. SshPort int `json:"sshPort"`
  39. SshUsername string `json:"sshUsername"`
  40. SshPassword string `json:"sshPassword"`
  41. SshHostKey string `json:"sshHostKey"`
  42. SshObfuscatedPort int `json:"sshObfuscatedPort"`
  43. SshObfuscatedKey string `json:"sshObfuscatedKey"`
  44. Capabilities []string `json:"capabilities"`
  45. Region string `json:"region"`
  46. MeekServerPort int `json:"meekServerPort"`
  47. MeekCookieEncryptionPublicKey string `json:"meekCookieEncryptionPublicKey"`
  48. MeekObfuscatedKey string `json:"meekObfuscatedKey"`
  49. MeekFrontingDomain string `json:"meekFrontingDomain"`
  50. MeekFrontingHost string `json:"meekFrontingHost"`
  51. }
  52. // DecodeServerEntry extracts server entries from the encoding
  53. // used by remote server lists and Psiphon server handshake requests.
  54. func DecodeServerEntry(encodedServerEntry string) (serverEntry *ServerEntry, err error) {
  55. hexDecodedServerEntry, err := hex.DecodeString(encodedServerEntry)
  56. if err != nil {
  57. return nil, ContextError(err)
  58. }
  59. // Skip past legacy format (4 space delimited fields) and just parse the JSON config
  60. fields := bytes.SplitN(hexDecodedServerEntry, []byte(" "), 5)
  61. if len(fields) != 5 {
  62. return nil, ContextError(errors.New("invalid encoded server entry"))
  63. }
  64. serverEntry = new(ServerEntry)
  65. err = json.Unmarshal(fields[4], &serverEntry)
  66. if err != nil {
  67. return nil, ContextError(err)
  68. }
  69. return serverEntry, nil
  70. }
  71. // ValidateServerEntry checks for malformed server entries.
  72. // Currently, it checks for a valid ipAddress. This is important since
  73. // handshake requests submit back to the server a list of known server
  74. // IP addresses and the handshake API expects well-formed inputs.
  75. // TODO: validate more fields
  76. func ValidateServerEntry(serverEntry *ServerEntry) error {
  77. ipAddr := net.ParseIP(serverEntry.IpAddress)
  78. if ipAddr == nil {
  79. errMsg := fmt.Sprintf("server entry has invalid IpAddress: '%s'", serverEntry.IpAddress)
  80. // Some callers skip invalid server entries without propagating
  81. // the error mesage, so issue a notice.
  82. NoticeAlert(errMsg)
  83. return ContextError(errors.New(errMsg))
  84. }
  85. return nil
  86. }
  87. // DecodeAndValidateServerEntryList extracts server entries from the list encoding
  88. // used by remote server lists and Psiphon server handshake requests.
  89. // Each server entry is validated and invalid entries are skipped.
  90. func DecodeAndValidateServerEntryList(encodedServerEntryList string) (serverEntries []*ServerEntry, err error) {
  91. serverEntries = make([]*ServerEntry, 0)
  92. for _, encodedServerEntry := range strings.Split(encodedServerEntryList, "\n") {
  93. if len(encodedServerEntry) == 0 {
  94. continue
  95. }
  96. // TODO: skip this entry and continue if can't decode?
  97. serverEntry, err := DecodeServerEntry(encodedServerEntry)
  98. if err != nil {
  99. return nil, ContextError(err)
  100. }
  101. if ValidateServerEntry(serverEntry) != nil {
  102. // Skip this entry and continue with the next one
  103. continue
  104. }
  105. serverEntries = append(serverEntries, serverEntry)
  106. }
  107. return serverEntries, nil
  108. }