dsl.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2025, 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. "context"
  22. "time"
  23. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  24. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/dsl"
  25. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  26. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/parameters"
  27. )
  28. func dslMakeGetServiceAddress(
  29. support *SupportServices) func(common.GeoIPData) (string, error) {
  30. return func(clientGeoIPData common.GeoIPData) (string, error) {
  31. // Defaults to the config value, unless a non-blank address is
  32. // configured in tactics.
  33. address := support.Config.DSLRelayServiceAddress
  34. p, err := support.ServerTacticsParametersCache.Get(GeoIPData(clientGeoIPData))
  35. if err != nil {
  36. return "", errors.Trace(err)
  37. }
  38. defer p.Close()
  39. if p.IsNil() {
  40. return address, nil
  41. }
  42. tacticsAddress := p.String(parameters.DSLRelayServiceAddress)
  43. if tacticsAddress == "" {
  44. return address, nil
  45. }
  46. return tacticsAddress, nil
  47. }
  48. }
  49. func dslReloadRelayTactics(support *SupportServices) error {
  50. // Assumes no GeoIP targeting for DSL relay tactics.
  51. dslRelay := support.dslRelay
  52. if dslRelay == nil {
  53. return nil
  54. }
  55. p, err := support.ServerTacticsParametersCache.Get(NewGeoIPData())
  56. if err != nil {
  57. return errors.Trace(err)
  58. }
  59. defer p.Close()
  60. if p.IsNil() {
  61. return nil
  62. }
  63. dslRelay.SetRequestParameters(
  64. p.Int(parameters.DSLRelayMaxHttpConns),
  65. p.Int(parameters.DSLRelayMaxHttpIdleConns),
  66. p.Duration(parameters.DSLRelayHttpIdleConnTimeout),
  67. p.Duration(parameters.DSLRelayRequestTimeout),
  68. p.Int(parameters.DSLRelayRetryCount))
  69. dslRelay.SetCacheParameters(
  70. p.Duration(parameters.DSLRelayCacheTTL),
  71. p.Int(parameters.DSLRelayCacheMaxSize))
  72. return nil
  73. }
  74. func dslHandleRequest(
  75. ctx context.Context,
  76. support *SupportServices,
  77. extendTimeout func(time.Duration),
  78. clientIP string,
  79. clientGeoIPData common.GeoIPData,
  80. isClientTunneled bool,
  81. requestPayload []byte) ([]byte, error) {
  82. relay := support.dslRelay
  83. if relay == nil {
  84. return dsl.GetRelayGenericErrorResponse(),
  85. errors.TraceNew("DSL relay not configured")
  86. }
  87. responsePayload, err := relay.HandleRequest(
  88. ctx,
  89. extendTimeout,
  90. clientIP,
  91. clientGeoIPData,
  92. isClientTunneled,
  93. requestPayload)
  94. if err != nil {
  95. return dsl.GetRelayGenericErrorResponse(),
  96. errors.Trace(err)
  97. }
  98. return responsePayload, nil
  99. }