remoteServerList.go 2.2 KB

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