remoteServerList.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. "io/ioutil"
  22. "net/http"
  23. )
  24. // FetchRemoteServerList downloads a remote server list JSON record from
  25. // config.RemoteServerListUrl; validates its digital signature using the
  26. // public key config.RemoteServerListSignaturePublicKey; and parses the
  27. // data field into ServerEntry records.
  28. func FetchRemoteServerList(config *Config, dialConfig *DialConfig) (err error) {
  29. NoticeInfo("fetching remote server list")
  30. transport := &http.Transport{
  31. Dial: NewTCPDialer(dialConfig),
  32. }
  33. httpClient := http.Client{
  34. Timeout: FETCH_REMOTE_SERVER_LIST_TIMEOUT,
  35. Transport: transport,
  36. }
  37. response, err := httpClient.Get(config.RemoteServerListUrl)
  38. if err != nil {
  39. return ContextError(err)
  40. }
  41. defer response.Body.Close()
  42. body, err := ioutil.ReadAll(response.Body)
  43. if err != nil {
  44. return ContextError(err)
  45. }
  46. remoteServerList, err := ReadAuthenticatedDataPackage(
  47. body, config.RemoteServerListSignaturePublicKey)
  48. if err != nil {
  49. return ContextError(err)
  50. }
  51. serverEntries, err := DecodeAndValidateServerEntryList(remoteServerList)
  52. if err != nil {
  53. return ContextError(err)
  54. }
  55. err = StoreServerEntries(serverEntries, true)
  56. if err != nil {
  57. return ContextError(err)
  58. }
  59. return nil
  60. }