psinet.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (c) 2016, 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/server/psinet implements psinet database services. The psinet
  20. // database is a JSON-format file containing information about the Psiphon network,
  21. // including sponsors, home pages, stats regexes, available upgrades, and other
  22. // servers for discovery. This package also implements the Psiphon discovery algorithm.
  23. package psinet
  24. import (
  25. "sync"
  26. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon"
  27. )
  28. // Database serves Psiphon API data requests. It's safe for
  29. // concurrent usage. The Reload function supports hot reloading
  30. // of Psiphon network data while the server is running.
  31. type Database struct {
  32. sync.RWMutex
  33. // TODO: implement
  34. }
  35. // NewDatabase initializes a Database, calling Load on the specified
  36. // filename.
  37. func NewDatabase(filename string) (*Database, error) {
  38. database := &Database{}
  39. err := database.Reload(filename)
  40. if err != nil {
  41. return nil, psiphon.ContextError(err)
  42. }
  43. return database, nil
  44. }
  45. // Reload [re]initializes the Database with the Psiphon network data
  46. // in the specified file. This function obtains a write lock on
  47. // the database, blocking all readers.
  48. // The input "" is valid and initializes a functional Database
  49. // with no data. When Reload fails, the previous Database state is
  50. // retained.
  51. func (db *Database) Reload(filename string) error {
  52. db.Lock()
  53. defer db.Unlock()
  54. // TODO: implement
  55. return nil
  56. }
  57. // GetHomepages returns a list of home pages for the specified sponsor,
  58. // region, and platform.
  59. func (db *Database) GetHomepages(sponsorID, clientRegion, clientPlatform string) []string {
  60. db.RLock()
  61. defer db.RUnlock()
  62. // TODO: implement
  63. return make([]string, 0)
  64. }
  65. // GetUpgradeClientVersion returns a new client version when an upgrade is
  66. // indicated for the specified client current version. The result is "" when
  67. // no upgrade is available.
  68. func (db *Database) GetUpgradeClientVersion(clientVersion, clientPlatform string) string {
  69. db.RLock()
  70. defer db.RUnlock()
  71. // TODO: implement
  72. return ""
  73. }
  74. // GetHttpsRequestRegexes returns bytes transferred stats regexes for the
  75. // specified sponsor.
  76. func (db *Database) GetHttpsRequestRegexes(sponsorID string) []map[string]string {
  77. db.RLock()
  78. defer db.RUnlock()
  79. return make([]map[string]string, 0)
  80. }
  81. // DiscoverServers selects new encoded server entries to be "discovered" by
  82. // the client, using the discoveryValue as the input into the discovery algorithm.
  83. func (db *Database) DiscoverServers(propagationChannelID string, discoveryValue int) []string {
  84. db.RLock()
  85. defer db.RUnlock()
  86. // TODO: implement
  87. return make([]string, 0)
  88. }