server.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (c) 2023, 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 inproxy
  20. import (
  21. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  22. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  23. )
  24. // MaxRelayRoundTrips is a sanity/anti-DoS check against clients that attempt
  25. // to relay more packets than are required for both a session handshake and
  26. // application-level request round trip.
  27. const MaxRelayRoundTrips = 10
  28. // ServerBrokerSessions manages the secure sessions that handle
  29. // BrokerServerReports from brokers. Each in-proxy-capable Psiphon server
  30. // maintains a ServerBrokerSessions, with a set of established sessions for
  31. // each broker. Session messages are relayed between the broker and the
  32. // server by the client.
  33. type ServerBrokerSessions struct {
  34. sessions *ResponderSessions
  35. proxyMetricsValidator common.APIParameterValidator
  36. proxyMetricsFormatter common.APIParameterLogFieldFormatter
  37. proxyMetricsPrefix string
  38. }
  39. // NewServerBrokerSessions create a new ServerBrokerSessions, with the
  40. // specified key material. The expected brokers are authenticated with
  41. // brokerPublicKeys, an allow list.
  42. func NewServerBrokerSessions(
  43. serverPrivateKey SessionPrivateKey,
  44. serverRootObfuscationSecret ObfuscationSecret,
  45. brokerPublicKeys []SessionPublicKey,
  46. proxyMetricsValidator common.APIParameterValidator,
  47. proxyMetricsFormatter common.APIParameterLogFieldFormatter,
  48. proxyMetricsPrefix string) (*ServerBrokerSessions, error) {
  49. sessions, err := NewResponderSessionsForKnownInitiators(
  50. serverPrivateKey, serverRootObfuscationSecret, brokerPublicKeys)
  51. if err != nil {
  52. return nil, errors.Trace(err)
  53. }
  54. return &ServerBrokerSessions{
  55. sessions: sessions,
  56. proxyMetricsValidator: proxyMetricsValidator,
  57. proxyMetricsFormatter: proxyMetricsFormatter,
  58. proxyMetricsPrefix: proxyMetricsPrefix,
  59. }, nil
  60. }
  61. // SetKnownBrokerPublicKeys updates the set of broker public keys which are
  62. // allowed to establish sessions with the server. Any existing sessions with
  63. // keys not in the new list are deleted. Existing sessions with keys which
  64. // remain in the list are retained.
  65. func (s *ServerBrokerSessions) SetKnownBrokerPublicKeys(
  66. brokerPublicKeys []SessionPublicKey) error {
  67. return errors.Trace(s.sessions.SetKnownInitiatorPublicKeys(brokerPublicKeys))
  68. }
  69. // ProxiedConnectionHandler is a callback, provided by the Psiphon server,
  70. // that receives information from a BrokerServerReport for the client
  71. // associated with the callback.
  72. //
  73. // The server must use the brokerVerifiedOriginalClientIP for all GeoIP
  74. // operations associated with the client, including traffic rule selection
  75. // and client-side tactics selection.
  76. //
  77. // Since the BrokerServerReport may be delivered later than the Psiphon
  78. // handshake request -- in the case where the broker/server session needs to
  79. // be established there will be additional round trips -- the server should
  80. // delay traffic rule application, tactics responses, and allowing tunneled
  81. // traffic until after the ProxiedConnectionHandler callback is invoked for
  82. // the client. As a consequence, Psiphon Servers should be configured to
  83. // require Proxies to be used for designated protocols. It's expected that
  84. // server-side tactics such as packet manipulation will be applied based on
  85. // the proxy's IP address.
  86. //
  87. // The fields in logFields should be added to server_tunnel logs.
  88. type ProxiedConnectionHandler func(
  89. brokerVerifiedOriginalClientIP string,
  90. logFields common.LogFields)
  91. // HandlePacket handles a broker/server session packet, which are relayed by
  92. // clients. In Psiphon, the packets may be exchanged in the Psiphon
  93. // handshake, or in subsequent SSH requests and responses. When the
  94. // broker/server session is already established, it's expected that the
  95. // BrokerServerReport arrives in the packet that accompanies the Psiphon
  96. // handshake, and so no additional round trip is required.
  97. //
  98. // Once the session is established and a verified BrokerServerReport arrives,
  99. // the information from that report is sent to the ProxiedConnectionHandler
  100. // callback. The callback should be associated with the client that is
  101. // relaying the packets.
  102. //
  103. // clientConnectionID is the in-proxy connection ID specified by the client in
  104. // its Psiphon handshake.
  105. //
  106. // When the retOut return value is not nil, it should be relayed back to the
  107. // client in the handshake response or other tunneled response. When retOut
  108. // is nil, the relay is complete.
  109. //
  110. // In the session reset token case, HandlePacket will return a non-nil retOut
  111. // along with a retErr; the server should both log retErr and also relay the
  112. // packet to the broker.
  113. func (s *ServerBrokerSessions) HandlePacket(
  114. logger common.Logger,
  115. in []byte,
  116. clientConnectionID ID,
  117. handler ProxiedConnectionHandler) (retOut []byte, retErr error) {
  118. handleUnwrappedReport := func(initiatorID ID, unwrappedReportPayload []byte) ([]byte, error) {
  119. brokerReport, err := UnmarshalBrokerServerReport(unwrappedReportPayload)
  120. if err != nil {
  121. return nil, errors.Trace(err)
  122. }
  123. logFields, err := brokerReport.ValidateAndGetLogFields(
  124. s.proxyMetricsValidator, s.proxyMetricsFormatter, s.proxyMetricsPrefix)
  125. if err != nil {
  126. return nil, errors.Trace(err)
  127. }
  128. // The initiatorID is the broker's public key.
  129. logFields["inproxy_broker_id"] = initiatorID
  130. ok := true
  131. // The client must supply same connection ID to server that the broker
  132. // sends to the server.
  133. if brokerReport.ConnectionID != clientConnectionID {
  134. // Limitation: as the BrokerServerReport is a one-way message with
  135. // no response, the broker will not be notified of tunnel failure
  136. // errors including "connection ID mismatch", and cannot log this
  137. // connection attempt outcome.
  138. logger.WithTraceFields(common.LogFields{
  139. "client_inproxy_connection_id": clientConnectionID,
  140. "broker_inproxy_connection_id": brokerReport.ConnectionID,
  141. }).Error("connection ID mismatch")
  142. ok = false
  143. }
  144. if ok {
  145. handler(brokerReport.ClientIP, logFields)
  146. }
  147. // Returns nil, as there is no response to the report, and so no
  148. // additional packet to relay.
  149. return nil, nil
  150. }
  151. out, err := s.sessions.HandlePacket(in, handleUnwrappedReport)
  152. return out, errors.Trace(err)
  153. }