serverEntry.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright (c) 2014, 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. )
  26. // ServerEntry represents a Psiphon server. It contains information
  27. // about how to estalish a tunnel connection to the server through
  28. // several protocols. ServerEntry are JSON records downloaded from
  29. // various sources.
  30. type ServerEntry struct {
  31. IpAddress string `json:"ipAddress"`
  32. WebServerPort string `json:"webServerPort"` // not an int
  33. WebServerSecret string `json:"webServerSecret"`
  34. WebServerCertificate string `json:"webServerCertificate"`
  35. SshPort int `json:"sshPort"`
  36. SshUsername string `json:"sshUsername"`
  37. SshPassword string `json:"sshPassword"`
  38. SshHostKey string `json:"sshHostKey"`
  39. SshObfuscatedPort int `json:"sshObfuscatedPort"`
  40. SshObfuscatedKey string `json:"sshObfuscatedKey"`
  41. Capabilities []string `json:"capabilities"`
  42. Region string `json:"region"`
  43. MeekServerPort int `json:"meekServerPort"`
  44. MeekCookieEncryptionPublicKey string `json:"meekCookieEncryptionPublicKey"`
  45. MeekObfuscatedKey string `json:"meekObfuscatedKey"`
  46. MeekFrontingDomain string `json:"meekFrontingDomain"`
  47. MeekFrontingHost string `json:"meekFrontingHost"`
  48. }
  49. // DecodeServerEntry extracts server entries from the encoding
  50. // used by remote server lists and Psiphon server handshake requests.
  51. func DecodeServerEntry(encodedServerEntry string) (serverEntry *ServerEntry, err error) {
  52. hexDecodedServerEntry, err := hex.DecodeString(encodedServerEntry)
  53. if err != nil {
  54. return nil, ContextError(err)
  55. }
  56. // Skip past legacy format (4 space delimited fields) and just parse the JSON config
  57. fields := bytes.SplitN(hexDecodedServerEntry, []byte(" "), 5)
  58. if len(fields) != 5 {
  59. return nil, ContextError(errors.New("invalid encoded server entry"))
  60. }
  61. serverEntry = new(ServerEntry)
  62. err = json.Unmarshal(fields[4], &serverEntry)
  63. if err != nil {
  64. return nil, ContextError(err)
  65. }
  66. return serverEntry, nil
  67. }