discovery.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (c) 2024, 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 server
  20. import (
  21. "net"
  22. "sync"
  23. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  24. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/parameters"
  25. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/server/discovery"
  26. )
  27. const (
  28. DISCOVERY_STRATEGY_CLASSIC = "classic"
  29. DISCOVERY_STRATEGY_CONSISTENT = "consistent"
  30. )
  31. // Discovery handles the discovery step of the "handshake" API request. It's
  32. // safe for concurrent usage.
  33. type Discovery struct {
  34. support *SupportServices
  35. currentStrategy string
  36. discovery *discovery.Discovery
  37. sync.RWMutex
  38. }
  39. func makeDiscovery(support *SupportServices) *Discovery {
  40. return &Discovery{
  41. support: support,
  42. }
  43. }
  44. // Start starts discovery.
  45. func (d *Discovery) Start() error {
  46. err := d.reload(false)
  47. if err != nil {
  48. return errors.Trace(err)
  49. }
  50. return nil
  51. }
  52. // reload reinitializes the underlying discovery component. If reloadedTactics
  53. // is set and the target discovery strategy has not changed, then the
  54. // underlying discovery component is not reinitialized.
  55. func (d *Discovery) reload(reloadedTactics bool) error {
  56. // Determine which discovery strategy to use. Assumes no GeoIP targeting
  57. // for the ServerDiscoveryStrategy tactic.
  58. p, err := d.support.ServerTacticsParametersCache.Get(NewGeoIPData())
  59. if err != nil {
  60. return errors.Trace(err)
  61. }
  62. strategy := ""
  63. if !p.IsNil() {
  64. strategy = p.String(parameters.ServerDiscoveryStrategy)
  65. }
  66. if strategy == "" {
  67. // No tactics are configured; default to consistent discovery.
  68. strategy = DISCOVERY_STRATEGY_CONSISTENT
  69. }
  70. // Do not reinitialize underlying discovery component if only tactics have
  71. // been reloaded and the discovery strategy has not changed.
  72. if reloadedTactics && d.support.discovery.currentStrategy == strategy {
  73. return nil
  74. }
  75. // Initialize new discovery strategy.
  76. // TODO: do not reinitialize discovery if the discovery strategy and
  77. // discovery servers have not changed.
  78. var discoveryStrategy discovery.DiscoveryStrategy
  79. if strategy == DISCOVERY_STRATEGY_CONSISTENT {
  80. discoveryStrategy, err = discovery.NewConsistentHashingDiscovery()
  81. if err != nil {
  82. return errors.Trace(err)
  83. }
  84. } else if strategy == DISCOVERY_STRATEGY_CLASSIC {
  85. discoveryStrategy, err = discovery.NewClassicDiscovery(
  86. d.support.Config.DiscoveryValueHMACKey)
  87. if err != nil {
  88. return errors.Trace(err)
  89. }
  90. } else {
  91. return errors.Tracef("unknown strategy %s", strategy)
  92. }
  93. // Initialize and set underlying discovery component. Replaces old
  94. // component if discovery is already initialized.
  95. discovery := discovery.MakeDiscovery(
  96. d.support.PsinetDatabase.GetDiscoveryServers(),
  97. discoveryStrategy)
  98. discovery.Start()
  99. d.Lock()
  100. oldDiscovery := d.discovery
  101. d.discovery = discovery
  102. d.currentStrategy = strategy
  103. d.Unlock()
  104. // Ensure resources used by previous underlying discovery component are
  105. // cleaned up.
  106. // Note: a more efficient impementation would not recreate the underlying
  107. // discovery instance if the discovery strategy has not changed, but
  108. // instead would update the underlying set of discovery servers if the set
  109. // of discovery servers has changed.
  110. if oldDiscovery != nil {
  111. oldDiscovery.Stop()
  112. }
  113. log.WithTraceFields(
  114. LogFields{"discovery_strategy": strategy}).Infof("reloaded discovery")
  115. return nil
  116. }
  117. // Stop stops discovery and cleans up underlying resources.
  118. func (d *Discovery) Stop() {
  119. d.Lock()
  120. defer d.Unlock()
  121. d.discovery.Stop()
  122. }
  123. // DiscoverServers selects new encoded server entries to be "discovered" by
  124. // the client, using the client's IP address as the input into the discovery
  125. // algorithm.
  126. func (d *Discovery) DiscoverServers(clientIP net.IP) []string {
  127. d.RLock()
  128. defer d.RUnlock()
  129. servers := d.discovery.SelectServers(clientIP)
  130. encodedServerEntries := make([]string, 0)
  131. for _, server := range servers {
  132. encodedServerEntries = append(encodedServerEntries, server.EncodedServerEntry)
  133. }
  134. return encodedServerEntries
  135. }