client.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. "context"
  22. "net"
  23. "sync"
  24. "time"
  25. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  26. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  27. )
  28. // clientOfferRequestTimeout should be set to no more than brokerClientOfferTimeout
  29. const (
  30. clientOfferRequestTimeout = 10 * time.Second
  31. clientOfferRetryDelay = 1 * time.Second
  32. clientOfferRetryJitter = 0.3
  33. )
  34. // ClientConn is a network connection to an in-proxy, which is relayed to a
  35. // Psiphon server destination. Psiphon clients use a ClientConn in place of a
  36. // physical TCP or UDP socket connection, passing the ClientConn into tunnel
  37. // protocol dials. ClientConn implements both net.Conn and net.PacketConn,
  38. // with net.PacketConn's ReadFrom/WriteTo behaving as if connected to the
  39. // initial dial address.
  40. type ClientConn struct {
  41. config *ClientConfig
  42. brokerClient *BrokerClient
  43. webRTCConn *WebRTCConn
  44. connectionID ID
  45. relayMutex sync.Mutex
  46. initialRelayPacket []byte
  47. }
  48. // ClientConfig specifies the configuration for a ClientConn dial.
  49. type ClientConfig struct {
  50. // Logger is used to log events.
  51. Logger common.Logger
  52. // BaseMetrics should be populated with Psiphon handshake metrics
  53. // parameters. These will be sent to and logger by the broker.
  54. BaseMetrics common.APIParameters
  55. // DialParameters specifies specific WebRTC dial strategies and
  56. // settings; DialParameters also facilities dial replay by receiving
  57. // callbacks when individual dial steps succeed or fail.
  58. DialParameters DialParameters
  59. // BrokerClient is the BrokerClient to use for broker API calls. The
  60. // BrokerClient may be shared with other client dials, allowing for
  61. // connection and session reuse.
  62. BrokerClient *BrokerClient
  63. // ReliableTransport specifies whether to use reliable delivery with the
  64. // underlying WebRTC DataChannel that relays the ClientConn traffic. When
  65. // using a ClientConn to proxy traffic that expects reliable delivery, as
  66. // if the physical network protocol were TCP, specify true. When using a
  67. // ClientConn to proxy traffic that expects unreliable delivery, such as
  68. // QUIC protocols expecting the physical network protocol UDP, specify
  69. // false.
  70. ReliableTransport bool
  71. // DialNetworkProtocol specifies whether the in-proxy will relay TCP or UDP
  72. // traffic.
  73. DialNetworkProtocol NetworkProtocol
  74. // DialAddress is the host:port destination network address the in-proxy
  75. // will relay traffic to.
  76. DialAddress string
  77. // DestinationServerEntryJSON is a signed Psiphon server entry
  78. // corresponding to the destination dial address. This signed server
  79. // entry is sent to the broker, which will use it to validate that the
  80. // server is a valid in-proxy destination.
  81. // ServerEntryFields.RemoveUnsignedFields can be called to prune local
  82. // fields before sending.
  83. DestinationServerEntryJSON []byte
  84. }
  85. // DialClient establishes an in-proxy connection for relaying traffic to the
  86. // specified destination. DialClient first contacts the broker and initiates
  87. // an in-proxy pairing. config.BrokerClient may be shared by multiple dials,
  88. // and may have a preexisting connection and session with the broker.
  89. func DialClient(
  90. ctx context.Context,
  91. config *ClientConfig) (retConn *ClientConn, retErr error) {
  92. // Reset and configure port mapper component, as required. See
  93. // initPortMapper comment.
  94. initPortMapper(config.DialParameters)
  95. // Future improvements:
  96. //
  97. // - The broker connection and session, when not already established,
  98. // could be established concurrent with the WebRTC offer setup
  99. // (STUN/ICE gathering).
  100. //
  101. // - The STUN state used for NAT discovery could be reused for the WebRTC
  102. // dial.
  103. //
  104. // - A subsequent WebRTC offer setup could be run concurrent with the
  105. // client offer request, in case that request or WebRTC connections
  106. // fails, so that the offer is immediately ready for a retry.
  107. if config.DialParameters.DiscoverNAT() {
  108. // NAT discovery, using the RFC5780 algorithms is optional and
  109. // conditional on the DiscoverNAT flag. Discovery is performed
  110. // synchronously, so that NAT topology metrics can be reported to the
  111. // broker in the ClientOffer request. For clients, NAT discovery is
  112. // intended to be performed at a low sampling rate, since the RFC5780
  113. // traffic may be unusual(differs from standard STUN requests for
  114. // ICE) and since this step delays the dial. Clients should to cache
  115. // their NAT discovery outcomes, associated with the current network
  116. // by network ID, so metrics can be reported even without a discovery
  117. // step; this is facilitated by DialParameters.
  118. //
  119. // NAT topology metrics are used by the broker to optimize client and
  120. // in-proxy matching.
  121. //
  122. // For client NAT discovery, port mapping type discovery is skipped
  123. // since port mappings are attempted when preparing the WebRTC offer,
  124. // which also happens before the ClientOffer request.
  125. NATDiscover(
  126. ctx,
  127. &NATDiscoverConfig{
  128. Logger: config.Logger,
  129. DialParameters: config.DialParameters,
  130. SkipPortMapping: true,
  131. })
  132. }
  133. var result *clientWebRTCDialResult
  134. for {
  135. // Repeatedly try to establish in-proxy/WebRTC connection until the
  136. // dial context is canceled or times out.
  137. //
  138. // If a broker request fails, the
  139. // DialParameters.BrokerClientRoundTripperFailed callback will be
  140. // invoked, so the Psiphon client will have an opportunity to select
  141. // new broker connection parameters before a retry. Similarly, when
  142. // STUN servers fail, DialParameters.STUNServerAddressFailed will be
  143. // invoked, giving the Psiphon client an opportunity to select new
  144. // STUN server parameter -- although, in this failure case, the
  145. // WebRTC connection attemp can succeed with other ICE candidates or
  146. // no ICE candidates.
  147. err := ctx.Err()
  148. if err != nil {
  149. return nil, errors.Trace(err)
  150. }
  151. var retry bool
  152. result, retry, err = dialClientWebRTCConn(ctx, config)
  153. if err == nil {
  154. break
  155. }
  156. if retry {
  157. config.Logger.WithTraceFields(common.LogFields{"error": err}).Warning("dial failed")
  158. // This delay is intended avoid overloading the broker with
  159. // repeated requests. A jitter is applied to mitigate a traffic
  160. // fingerprint.
  161. common.SleepWithJitter(
  162. ctx,
  163. common.ValueOrDefault(config.DialParameters.OfferRetryDelay(), clientOfferRetryDelay),
  164. common.ValueOrDefault(config.DialParameters.OfferRetryJitter(), clientOfferRetryJitter))
  165. continue
  166. }
  167. return nil, errors.Trace(err)
  168. }
  169. return &ClientConn{
  170. config: config,
  171. webRTCConn: result.conn,
  172. connectionID: result.connectionID,
  173. initialRelayPacket: result.relayPacket,
  174. }, nil
  175. }
  176. // GetConnectionID returns the in-proxy connection ID, which the client should
  177. // include with its Psiphon handshake parameters.
  178. func (conn *ClientConn) GetConnectionID() ID {
  179. return conn.connectionID
  180. }
  181. // InitialRelayPacket returns the initial packet in the broker->server
  182. // messaging session. The client must relay these packets to facilitate this
  183. // message exchange. Session security ensures clients cannot decrypt, modify,
  184. // or replay these session packets. The Psiphon client will sent the initial
  185. // packet as a parameter in the Psiphon server handshake request.
  186. func (conn *ClientConn) InitialRelayPacket() []byte {
  187. conn.relayMutex.Lock()
  188. defer conn.relayMutex.Unlock()
  189. relayPacket := conn.initialRelayPacket
  190. conn.initialRelayPacket = nil
  191. return relayPacket
  192. }
  193. // RelayPacket takes any server->broker messaging session packets the client
  194. // receives and relays them back to the broker. RelayPacket returns the next
  195. // broker->server packet, if any, or nil when the message exchange is
  196. // complete. Psiphon clients receive a server->broker packet in the Psiphon
  197. // server handshake response and exchange additional packets in a
  198. // post-handshake Psiphon server request.
  199. //
  200. // If RelayPacket fails, the client should close the ClientConn and redial.
  201. func (conn *ClientConn) RelayPacket(
  202. ctx context.Context, in []byte, sessionInvalid bool) ([]byte, error) {
  203. // Future improvement: the client relaying these packets back to the
  204. // broker is potentially an inter-flow fingerprint, alternating between
  205. // the WebRTC flow and the client's broker connection. It may be possible
  206. // to avoid this by having the client connect to the broker via the
  207. // tunnel, resuming its broker session and relaying any further packets.
  208. // Limitation: here, this mutex only ensures that this ClientConn doesn't
  209. // make concurrent ClientRelayedPacket requests. The client must still
  210. // ensure that the packets are delivered in the correct relay sequence.
  211. conn.relayMutex.Lock()
  212. defer conn.relayMutex.Unlock()
  213. relayResponse, err := conn.config.BrokerClient.ClientRelayedPacket(
  214. ctx,
  215. &ClientRelayedPacketRequest{
  216. ConnectionID: conn.connectionID,
  217. PacketFromServer: in,
  218. SessionInvalid: sessionInvalid,
  219. })
  220. if err != nil {
  221. return nil, errors.Trace(err)
  222. }
  223. return relayResponse.PacketToServer, nil
  224. }
  225. type clientWebRTCDialResult struct {
  226. conn *WebRTCConn
  227. connectionID ID
  228. relayPacket []byte
  229. }
  230. func dialClientWebRTCConn(
  231. ctx context.Context,
  232. config *ClientConfig) (retResult *clientWebRTCDialResult, retRetry bool, retErr error) {
  233. // Initialize the WebRTC offer
  234. clientRootObfuscationSecret := config.DialParameters.ClientRootObfuscationSecret()
  235. webRTCConn, SDP, SDPMetrics, err := NewWebRTCConnWithOffer(
  236. ctx, &WebRTCConfig{
  237. Logger: config.Logger,
  238. DialParameters: config.DialParameters,
  239. ClientRootObfuscationSecret: clientRootObfuscationSecret,
  240. ReliableTransport: config.ReliableTransport,
  241. })
  242. if err != nil {
  243. return nil, true, errors.Trace(err)
  244. }
  245. defer func() {
  246. // Cleanup on early return
  247. if retErr != nil {
  248. webRTCConn.Close()
  249. }
  250. }()
  251. // Send the ClientOffer request to the broker
  252. offerRequestCtx, offerRequestCancelFunc := context.WithTimeout(
  253. ctx, common.ValueOrDefault(config.DialParameters.OfferRequestTimeout(), clientOfferRequestTimeout))
  254. defer offerRequestCancelFunc()
  255. baseMetrics, err := EncodeBaseMetrics(config.BaseMetrics)
  256. if err != nil {
  257. return nil, false, errors.Trace(err)
  258. }
  259. // Here, DialParameters.NATType may be populated from discovery, or
  260. // replayed from a previous run on the same network ID.
  261. // DialParameters.PortMappingTypes may be populated via
  262. // newWebRTCConnWithOffer.
  263. offerResponse, err := config.BrokerClient.ClientOffer(
  264. offerRequestCtx,
  265. &ClientOfferRequest{
  266. Metrics: &ClientMetrics{
  267. BaseMetrics: baseMetrics,
  268. ProxyProtocolVersion: ProxyProtocolVersion1,
  269. NATType: config.DialParameters.NATType(),
  270. PortMappingTypes: config.DialParameters.PortMappingTypes(),
  271. },
  272. CommonCompartmentIDs: config.DialParameters.CommonCompartmentIDs(),
  273. PersonalCompartmentIDs: config.DialParameters.PersonalCompartmentIDs(),
  274. ClientOfferSDP: SDP,
  275. ICECandidateTypes: SDPMetrics.ICECandidateTypes,
  276. ClientRootObfuscationSecret: clientRootObfuscationSecret,
  277. DestinationServerEntryJSON: config.DestinationServerEntryJSON,
  278. NetworkProtocol: config.DialNetworkProtocol,
  279. DestinationAddress: config.DialAddress,
  280. })
  281. if err != nil {
  282. return nil, false, errors.Trace(err)
  283. }
  284. if offerResponse.SelectedProxyProtocolVersion != ProxyProtocolVersion1 {
  285. return nil, false, errors.Tracef(
  286. "Unsupported proxy protocol version: %d",
  287. offerResponse.SelectedProxyProtocolVersion)
  288. }
  289. // Establish the WebRTC DataChannel connection
  290. err = webRTCConn.SetRemoteSDP(offerResponse.ProxyAnswerSDP)
  291. if err != nil {
  292. return nil, true, errors.Trace(err)
  293. }
  294. err = webRTCConn.AwaitInitialDataChannel(ctx)
  295. if err != nil {
  296. return nil, true, errors.Trace(err)
  297. }
  298. return &clientWebRTCDialResult{
  299. conn: webRTCConn,
  300. connectionID: offerResponse.ConnectionID,
  301. relayPacket: offerResponse.RelayPacketToServer,
  302. }, false, nil
  303. }
  304. // GetMetrics implements the common.MetricsSource interface.
  305. func (conn *ClientConn) GetMetrics() common.LogFields {
  306. // TODO: determine which WebRTC ICE candidate was chosen, and log its
  307. // type (host, server reflexive, etc.), and whether it's IPv6.
  308. return common.LogFields{}
  309. }
  310. func (conn *ClientConn) Close() error {
  311. return errors.Trace(conn.webRTCConn.Close())
  312. }
  313. func (conn *ClientConn) Read(p []byte) (int, error) {
  314. n, err := conn.webRTCConn.Read(p)
  315. return n, errors.Trace(err)
  316. }
  317. // Write relays p through the in-proxy connection. len(p) should be under
  318. // 32K.
  319. func (conn *ClientConn) Write(p []byte) (int, error) {
  320. n, err := conn.webRTCConn.Write(p)
  321. return n, errors.Trace(err)
  322. }
  323. func (conn *ClientConn) LocalAddr() net.Addr {
  324. return conn.webRTCConn.LocalAddr()
  325. }
  326. func (conn *ClientConn) RemoteAddr() net.Addr {
  327. return conn.webRTCConn.RemoteAddr()
  328. }
  329. func (conn *ClientConn) SetDeadline(t time.Time) error {
  330. return conn.webRTCConn.SetDeadline(t)
  331. }
  332. func (conn *ClientConn) SetReadDeadline(t time.Time) error {
  333. return conn.webRTCConn.SetReadDeadline(t)
  334. }
  335. func (conn *ClientConn) SetWriteDeadline(t time.Time) error {
  336. // Limitation: this is a workaround; webRTCConn doesn't support
  337. // SetWriteDeadline, but common/quic calls SetWriteDeadline on
  338. // net.PacketConns to avoid hanging on EAGAIN when the conn is an actual
  339. // UDP socket. See the comment in common/quic.writeTimeoutUDPConn. In
  340. // this case, the conn is not a UDP socket and that particular
  341. // SetWriteDeadline use case doesn't apply. Silently ignore the deadline
  342. // and report no error.
  343. return nil
  344. }
  345. func (conn *ClientConn) ReadFrom(b []byte) (int, net.Addr, error) {
  346. n, err := conn.webRTCConn.Read(b)
  347. return n, conn.webRTCConn.RemoteAddr(), err
  348. }
  349. func (conn *ClientConn) WriteTo(b []byte, _ net.Addr) (int, error) {
  350. n, err := conn.webRTCConn.Write(b)
  351. return n, err
  352. }