serverEntry.go 3.4 KB

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