serverEntry.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. MeekFrontingHost string `json:"meekFrontingHost"`
  50. MeekFrontingDomain string `json:"meekFrontingDomain"`
  51. MeekFrontingAddresses []string `json:"meekFrontingAddresses"`
  52. }
  53. // DecodeServerEntry extracts server entries from the encoding
  54. // used by remote server lists and Psiphon server handshake requests.
  55. func DecodeServerEntry(encodedServerEntry string) (serverEntry *ServerEntry, err error) {
  56. hexDecodedServerEntry, err := hex.DecodeString(encodedServerEntry)
  57. if err != nil {
  58. return nil, ContextError(err)
  59. }
  60. // Skip past legacy format (4 space delimited fields) and just parse the JSON config
  61. fields := bytes.SplitN(hexDecodedServerEntry, []byte(" "), 5)
  62. if len(fields) != 5 {
  63. return nil, ContextError(errors.New("invalid encoded server entry"))
  64. }
  65. serverEntry = new(ServerEntry)
  66. err = json.Unmarshal(fields[4], &serverEntry)
  67. if err != nil {
  68. return nil, ContextError(err)
  69. }
  70. return serverEntry, nil
  71. }
  72. // ValidateServerEntry checks for malformed server entries.
  73. // Currently, it checks for a valid ipAddress. This is important since
  74. // handshake requests submit back to the server a list of known server
  75. // IP addresses and the handshake API expects well-formed inputs.
  76. // TODO: validate more fields
  77. func ValidateServerEntry(serverEntry *ServerEntry) error {
  78. ipAddr := net.ParseIP(serverEntry.IpAddress)
  79. if ipAddr == nil {
  80. errMsg := fmt.Sprintf("server entry has invalid IpAddress: '%s'", serverEntry.IpAddress)
  81. // Some callers skip invalid server entries without propagating
  82. // the error mesage, so issue a notice.
  83. NoticeAlert(errMsg)
  84. return ContextError(errors.New(errMsg))
  85. }
  86. return nil
  87. }
  88. // DecodeAndValidateServerEntryList extracts server entries from the list encoding
  89. // used by remote server lists and Psiphon server handshake requests.
  90. // Each server entry is validated and invalid entries are skipped.
  91. func DecodeAndValidateServerEntryList(encodedServerEntryList string) (serverEntries []*ServerEntry, err error) {
  92. serverEntries = make([]*ServerEntry, 0)
  93. for _, encodedServerEntry := range strings.Split(encodedServerEntryList, "\n") {
  94. if len(encodedServerEntry) == 0 {
  95. continue
  96. }
  97. // TODO: skip this entry and continue if can't decode?
  98. serverEntry, err := DecodeServerEntry(encodedServerEntry)
  99. if err != nil {
  100. return nil, ContextError(err)
  101. }
  102. if ValidateServerEntry(serverEntry) != nil {
  103. // Skip this entry and continue with the next one
  104. continue
  105. }
  106. serverEntries = append(serverEntries, serverEntry)
  107. }
  108. return serverEntries, nil
  109. }