client.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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. "fmt"
  23. "net"
  24. "net/netip"
  25. "sync"
  26. "time"
  27. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  28. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  29. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
  30. )
  31. const (
  32. clientOfferRetryDelay = 1 * time.Second
  33. clientOfferRetryJitter = 0.3
  34. )
  35. // ClientConn is a network connection to an in-proxy, which is relayed to a
  36. // Psiphon server destination. Psiphon clients use a ClientConn in place of a
  37. // physical TCP or UDP socket connection, passing the ClientConn into tunnel
  38. // protocol dials. ClientConn implements both net.Conn and net.PacketConn,
  39. // with net.PacketConn's ReadFrom/WriteTo behaving as if connected to the
  40. // initial dial address.
  41. type ClientConn struct {
  42. config *ClientConfig
  43. webRTCConn *webRTCConn
  44. connectionID ID
  45. remoteAddr net.Addr
  46. metrics common.LogFields
  47. relayMutex sync.Mutex
  48. initialRelayPacket []byte
  49. }
  50. // ClientConfig specifies the configuration for a ClientConn dial.
  51. type ClientConfig struct {
  52. // Logger is used to log events.
  53. Logger common.Logger
  54. // EnableWebRTCDebugLogging indicates whether to emit WebRTC debug logs.
  55. EnableWebRTCDebugLogging bool
  56. // BaseAPIParameters should be populated with Psiphon handshake metrics
  57. // parameters. These will be sent to and logger by the broker.
  58. BaseAPIParameters common.APIParameters
  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. // WebRTCDialCoordinator specifies specific WebRTC dial strategies and
  64. // settings; WebRTCDialCoordinator also facilities dial replay by
  65. // receiving callbacks when individual dial steps succeed or fail.
  66. WebRTCDialCoordinator WebRTCDialCoordinator
  67. // ReliableTransport specifies whether to use reliable delivery with the
  68. // underlying WebRTC DataChannel that relays the ClientConn traffic. When
  69. // using a ClientConn to proxy traffic that expects reliable delivery, as
  70. // if the physical network protocol were TCP, specify true. When using a
  71. // ClientConn to proxy traffic that expects unreliable delivery, such as
  72. // QUIC protocols expecting the physical network protocol UDP, specify
  73. // false.
  74. ReliableTransport bool
  75. // DialNetworkProtocol specifies whether the in-proxy will relay TCP or UDP
  76. // traffic.
  77. DialNetworkProtocol NetworkProtocol
  78. // DialAddress is the host:port destination network address the in-proxy
  79. // will relay traffic to.
  80. DialAddress string
  81. // RemoteAddrOverride, when specified, is the address to be returned by
  82. // ClientConn.RemoteAddr. When not specified, ClientConn.RemoteAddr
  83. // returns a zero-value address.
  84. RemoteAddrOverride string
  85. // PackedDestinationServerEntry is a signed Psiphon server entry
  86. // corresponding to the destination dial address. This signed server
  87. // entry is sent to the broker, which will use it to validate that the
  88. // server is a valid in-proxy destination.
  89. //
  90. // The expected format is CBOR-encoded protoco.PackedServerEntryFields,
  91. // with the caller invoking ServerEntryFields.RemoveUnsignedFields to
  92. // prune local, unnsigned fields before sending.
  93. PackedDestinationServerEntry []byte
  94. // MustUpgrade is a callback that is invoked when a MustUpgrade flag is
  95. // received from the broker. When MustUpgrade is received, the client
  96. // should be stopped and the user should be prompted to upgrade before
  97. // restarting the client.
  98. //
  99. // In Psiphon, MustUpgrade may be ignored when not running in
  100. // in-proxy-only personal pairing mode, as other tunnel protocols remain
  101. // available.
  102. MustUpgrade func()
  103. }
  104. // DialClient establishes an in-proxy connection for relaying traffic to the
  105. // specified destination. DialClient first contacts the broker and initiates
  106. // an in-proxy pairing. config.BrokerClient may be shared by multiple dials,
  107. // and may have a preexisting connection and session with the broker.
  108. func DialClient(
  109. ctx context.Context,
  110. config *ClientConfig) (retConn *ClientConn, retErr error) {
  111. startTime := time.Now()
  112. metrics := common.LogFields{}
  113. // Configure the value returned by ClientConn.RemoteAddr. If no
  114. // config.RemoteAddrOverride is specified, RemoteAddr will return a
  115. // zero-value, non-nil net.Addr. The underlying webRTCConn.RemoteAddr
  116. // returns only nil.
  117. var remoteAddr net.Addr
  118. var addrPort netip.AddrPort
  119. if config.RemoteAddrOverride != "" {
  120. // ParseAddrPort does not perform any domain resolution. The addr
  121. // portion must be an IP address.
  122. var err error
  123. addrPort, err = netip.ParseAddrPort(config.RemoteAddrOverride)
  124. if err != nil {
  125. return nil, errors.Trace(err)
  126. }
  127. }
  128. switch config.DialNetworkProtocol {
  129. case NetworkProtocolTCP:
  130. remoteAddr = net.TCPAddrFromAddrPort(addrPort)
  131. case NetworkProtocolUDP:
  132. remoteAddr = net.UDPAddrFromAddrPort(addrPort)
  133. default:
  134. return nil, errors.TraceNew("unexpected DialNetworkProtocol")
  135. }
  136. // Reset and configure port mapper component, as required. See
  137. // initPortMapper comment.
  138. initPortMapper(config.WebRTCDialCoordinator)
  139. // Future improvements:
  140. //
  141. // - The broker connection and session, when not already established,
  142. // could be established concurrent with the WebRTC offer setup
  143. // (STUN/ICE gathering).
  144. //
  145. // - The STUN state used for NAT discovery could be reused for the WebRTC
  146. // dial.
  147. //
  148. // - A subsequent WebRTC offer setup could be run concurrent with the
  149. // client offer request, in case that request or WebRTC connections
  150. // fails, so that the offer is immediately ready for a retry.
  151. if config.WebRTCDialCoordinator.DiscoverNAT() {
  152. // NAT discovery, using the RFC5780 algorithms is optional and
  153. // conditional on the DiscoverNAT flag. Discovery is performed
  154. // synchronously, so that NAT topology metrics can be reported to the
  155. // broker in the ClientOffer request. For clients, NAT discovery is
  156. // intended to be performed at a low sampling rate, since the RFC5780
  157. // traffic may be unusual (differs from standard STUN requests for
  158. // ICE), the port mapping probe traffic may be unusual, and since
  159. // this step delays the dial. Clients should to cache their NAT
  160. // discovery outcomes, associated with the current network by network
  161. // ID, so metrics can be reported even without a discovery step; this
  162. // is facilitated by WebRTCDialCoordinator.
  163. //
  164. // NAT topology metrics are used by the broker to optimize client and
  165. // in-proxy matching.
  166. NATDiscover(
  167. ctx,
  168. &NATDiscoverConfig{
  169. Logger: config.Logger,
  170. WebRTCDialCoordinator: config.WebRTCDialCoordinator,
  171. })
  172. duration := time.Since(startTime)
  173. metrics["inproxy_dial_nat_discovery_duration"] = fmt.Sprintf("%d", duration/time.Millisecond)
  174. config.Logger.WithTraceFields(
  175. common.LogFields{"duration": duration.String()}).Info("NAT discovery complete")
  176. startTime = time.Now()
  177. }
  178. var result *clientWebRTCDialResult
  179. for attempt := 0; ; attempt += 1 {
  180. previousAttemptsDuration := time.Since(startTime)
  181. // Repeatedly try to establish in-proxy/WebRTC connection until the
  182. // dial context is canceled or times out.
  183. //
  184. // If a broker request fails, the WebRTCDialCoordinator
  185. // BrokerClientRoundTripperFailed callback will be invoked, so the
  186. // Psiphon client will have an opportunity to select new broker
  187. // connection parameters before a retry. Similarly, when STUN servers
  188. // fail, WebRTCDialCoordinator STUNServerAddressFailed will be
  189. // invoked, giving the Psiphon client an opportunity to select new
  190. // STUN server parameter -- although, in this failure case, the
  191. // WebRTC connection attempt can succeed with other ICE candidates or
  192. // no ICE candidates.
  193. err := ctx.Err()
  194. if err != nil {
  195. return nil, errors.Trace(err)
  196. }
  197. var retry bool
  198. result, retry, err = dialClientWebRTCConn(ctx, config)
  199. if err == nil {
  200. if attempt > 0 {
  201. // Record the time elapsed in previous attempts.
  202. metrics["inproxy_dial_failed_attempts_duration"] =
  203. fmt.Sprintf("%d", previousAttemptsDuration/time.Millisecond)
  204. config.Logger.WithTraceFields(
  205. common.LogFields{
  206. "duration": previousAttemptsDuration.String()}).Info("previous failed attempts")
  207. }
  208. break
  209. }
  210. if retry {
  211. config.Logger.WithTraceFields(common.LogFields{"error": err}).Warning("dial failed")
  212. // This delay is intended avoid overloading the broker with
  213. // repeated requests. A jitter is applied to mitigate a traffic
  214. // fingerprint.
  215. brokerCoordinator := config.BrokerClient.GetBrokerDialCoordinator()
  216. common.SleepWithJitter(
  217. ctx,
  218. common.ValueOrDefault(brokerCoordinator.OfferRetryDelay(), clientOfferRetryDelay),
  219. common.ValueOrDefault(brokerCoordinator.OfferRetryJitter(), clientOfferRetryJitter))
  220. continue
  221. }
  222. return nil, errors.Trace(err)
  223. }
  224. metrics.Add(result.metrics)
  225. return &ClientConn{
  226. config: config,
  227. webRTCConn: result.conn,
  228. connectionID: result.connectionID,
  229. remoteAddr: remoteAddr,
  230. metrics: metrics,
  231. initialRelayPacket: result.relayPacket,
  232. }, nil
  233. }
  234. // GetConnectionID returns the in-proxy connection ID, which the client should
  235. // include with its Psiphon handshake parameters.
  236. func (conn *ClientConn) GetConnectionID() ID {
  237. return conn.connectionID
  238. }
  239. // InitialRelayPacket returns the initial packet in the broker->server
  240. // messaging session. The client must relay these packets to facilitate this
  241. // message exchange. Session security ensures clients cannot decrypt, modify,
  242. // or replay these session packets. The Psiphon client will sent the initial
  243. // packet as a parameter in the Psiphon server handshake request.
  244. func (conn *ClientConn) InitialRelayPacket() []byte {
  245. conn.relayMutex.Lock()
  246. defer conn.relayMutex.Unlock()
  247. relayPacket := conn.initialRelayPacket
  248. conn.initialRelayPacket = nil
  249. return relayPacket
  250. }
  251. // RelayPacket takes any server->broker messaging session packets the client
  252. // receives and relays them back to the broker. RelayPacket returns the next
  253. // broker->server packet, if any, or nil when the message exchange is
  254. // complete. Psiphon clients receive a server->broker packet in the Psiphon
  255. // server handshake response and exchange additional packets in a
  256. // post-handshake Psiphon server request.
  257. //
  258. // If RelayPacket fails, the client should close the ClientConn and redial.
  259. func (conn *ClientConn) RelayPacket(
  260. ctx context.Context, in []byte) ([]byte, error) {
  261. // Future improvement: the client relaying these packets back to the
  262. // broker is potentially an inter-flow fingerprint, alternating between
  263. // the WebRTC flow and the client's broker connection. It may be possible
  264. // to avoid this by having the client connect to the broker via the
  265. // tunnel, resuming its broker session and relaying any further packets.
  266. // Limitation: here, this mutex only ensures that this ClientConn doesn't
  267. // make concurrent ClientRelayedPacket requests. The client must still
  268. // ensure that the packets are delivered in the correct relay sequence.
  269. conn.relayMutex.Lock()
  270. defer conn.relayMutex.Unlock()
  271. // ClientRelayedPacket applies
  272. // BrokerDialCoordinator.RelayedPacketRequestTimeout as the request
  273. // timeout.
  274. relayResponse, err := conn.config.BrokerClient.ClientRelayedPacket(
  275. ctx,
  276. &ClientRelayedPacketRequest{
  277. ConnectionID: conn.connectionID,
  278. PacketFromServer: in,
  279. })
  280. if err != nil {
  281. return nil, errors.Trace(err)
  282. }
  283. return relayResponse.PacketToServer, nil
  284. }
  285. type clientWebRTCDialResult struct {
  286. conn *webRTCConn
  287. connectionID ID
  288. relayPacket []byte
  289. metrics common.LogFields
  290. }
  291. func dialClientWebRTCConn(
  292. ctx context.Context,
  293. config *ClientConfig) (retResult *clientWebRTCDialResult, retRetry bool, retErr error) {
  294. startTime := time.Now()
  295. metrics := common.LogFields{}
  296. brokerCoordinator := config.BrokerClient.GetBrokerDialCoordinator()
  297. personalCompartmentIDs := brokerCoordinator.PersonalCompartmentIDs()
  298. // In personal pairing mode, RFC 1918/4193 private IP addresses are
  299. // included in SDPs.
  300. hasPersonalCompartmentIDs := len(personalCompartmentIDs) > 0
  301. // Initialize the WebRTC offer
  302. doTLSRandomization := config.WebRTCDialCoordinator.DoDTLSRandomization()
  303. useMediaStreams := config.WebRTCDialCoordinator.UseMediaStreams()
  304. trafficShapingParameters := config.WebRTCDialCoordinator.TrafficShapingParameters()
  305. clientRootObfuscationSecret := config.WebRTCDialCoordinator.ClientRootObfuscationSecret()
  306. webRTCConn, SDP, SDPMetrics, err := newWebRTCConnForOffer(
  307. ctx, &webRTCConfig{
  308. Logger: config.Logger,
  309. EnableDebugLogging: config.EnableWebRTCDebugLogging,
  310. WebRTCDialCoordinator: config.WebRTCDialCoordinator,
  311. ClientRootObfuscationSecret: clientRootObfuscationSecret,
  312. DoDTLSRandomization: doTLSRandomization,
  313. UseMediaStreams: useMediaStreams,
  314. TrafficShapingParameters: trafficShapingParameters,
  315. ReliableTransport: config.ReliableTransport,
  316. },
  317. hasPersonalCompartmentIDs)
  318. if err != nil {
  319. return nil, true, errors.Trace(err)
  320. }
  321. defer func() {
  322. // Cleanup on early return
  323. if retErr != nil {
  324. webRTCConn.Close()
  325. }
  326. }()
  327. duration := time.Since(startTime)
  328. metrics["inproxy_dial_webrtc_ice_gathering_duration"] = fmt.Sprintf("%d", duration/time.Millisecond)
  329. config.Logger.WithTraceFields(
  330. common.LogFields{"duration": duration.String()}).Info("ICE gathering complete")
  331. startTime = time.Now()
  332. // Send the ClientOffer request to the broker
  333. apiParams := common.APIParameters{}
  334. apiParams.Add(config.BaseAPIParameters)
  335. apiParams.Add(common.APIParameters(brokerCoordinator.MetricsForBrokerRequests()))
  336. packedParams, err := protocol.EncodePackedAPIParameters(apiParams)
  337. if err != nil {
  338. return nil, false, errors.Trace(err)
  339. }
  340. // Here, WebRTCDialCoordinator.NATType may be populated from discovery, or
  341. // replayed from a previous run on the same network ID.
  342. // WebRTCDialCoordinator.PortMappingTypes/PortMappingProbe may be
  343. // populated via the optional NATDiscover run above or in a previous dial.
  344. // ClientOffer applies BrokerDialCoordinator.OfferRequestTimeout or
  345. // OfferRequestPersonalTimeout as the request timeout.
  346. offerResponse, err := config.BrokerClient.ClientOffer(
  347. ctx,
  348. &ClientOfferRequest{
  349. Metrics: &ClientMetrics{
  350. BaseAPIParameters: packedParams,
  351. ProtocolVersion: LatestProtocolVersion,
  352. NATType: config.WebRTCDialCoordinator.NATType(),
  353. PortMappingTypes: config.WebRTCDialCoordinator.PortMappingTypes(),
  354. },
  355. CommonCompartmentIDs: brokerCoordinator.CommonCompartmentIDs(),
  356. PersonalCompartmentIDs: personalCompartmentIDs,
  357. ClientOfferSDP: SDP,
  358. ICECandidateTypes: SDPMetrics.iceCandidateTypes,
  359. ClientRootObfuscationSecret: clientRootObfuscationSecret,
  360. DoDTLSRandomization: doTLSRandomization,
  361. UseMediaStreams: useMediaStreams,
  362. TrafficShapingParameters: trafficShapingParameters,
  363. PackedDestinationServerEntry: config.PackedDestinationServerEntry,
  364. NetworkProtocol: config.DialNetworkProtocol,
  365. DestinationAddress: config.DialAddress,
  366. },
  367. hasPersonalCompartmentIDs)
  368. if err != nil {
  369. return nil, false, errors.Trace(err)
  370. }
  371. duration = time.Since(startTime)
  372. metrics["inproxy_dial_broker_offer_duration"] = fmt.Sprintf("%d", duration/time.Millisecond)
  373. config.Logger.WithTraceFields(
  374. common.LogFields{"duration": duration.String()}).Info("Broker offer complete")
  375. startTime = time.Now()
  376. // MustUpgrade has precedence over other cases to ensure the callback is
  377. // invoked. No retry when rate/entry limited or must upgrade; do retry on
  378. // no-match, as a match may soon appear.
  379. if offerResponse.MustUpgrade {
  380. if config.MustUpgrade != nil {
  381. config.MustUpgrade()
  382. }
  383. return nil, false, errors.TraceNew("must upgrade")
  384. } else if offerResponse.Limited {
  385. return nil, false, errors.TraceNew("limited")
  386. } else if offerResponse.NoMatch {
  387. return nil, true, errors.TraceNew("no match")
  388. }
  389. if offerResponse.SelectedProtocolVersion < ProtocolVersion1 ||
  390. (useMediaStreams &&
  391. offerResponse.SelectedProtocolVersion < ProtocolVersion2) ||
  392. offerResponse.SelectedProtocolVersion > LatestProtocolVersion {
  393. return nil, false, errors.Tracef(
  394. "Unsupported protocol version: %d",
  395. offerResponse.SelectedProtocolVersion)
  396. }
  397. // Establish the WebRTC DataChannel connection
  398. err = webRTCConn.SetRemoteSDP(
  399. offerResponse.ProxyAnswerSDP, hasPersonalCompartmentIDs)
  400. if err != nil {
  401. return nil, true, errors.Trace(err)
  402. }
  403. awaitReadyToProxyCtx, awaitReadyToProxyCancelFunc := context.WithTimeout(
  404. ctx,
  405. common.ValueOrDefault(
  406. config.WebRTCDialCoordinator.WebRTCAwaitReadyToProxyTimeout(), readyToProxyAwaitTimeout))
  407. defer awaitReadyToProxyCancelFunc()
  408. err = webRTCConn.AwaitReadyToProxy(awaitReadyToProxyCtx, offerResponse.ConnectionID)
  409. if err != nil {
  410. return nil, true, errors.Trace(err)
  411. }
  412. duration = time.Since(startTime)
  413. metrics["inproxy_dial_webrtc_connection_duration"] = fmt.Sprintf("%d", duration/time.Millisecond)
  414. config.Logger.WithTraceFields(
  415. common.LogFields{"duration": duration.String()}).Info("WebRTC connection complete")
  416. return &clientWebRTCDialResult{
  417. conn: webRTCConn,
  418. connectionID: offerResponse.ConnectionID,
  419. relayPacket: offerResponse.RelayPacketToServer,
  420. metrics: metrics,
  421. }, false, nil
  422. }
  423. // GetMetrics implements the common.MetricsSource interface.
  424. func (conn *ClientConn) GetMetrics() common.LogFields {
  425. metrics := common.LogFields{}
  426. metrics.Add(conn.metrics)
  427. metrics.Add(conn.webRTCConn.GetMetrics())
  428. return metrics
  429. }
  430. func (conn *ClientConn) Close() error {
  431. return errors.Trace(conn.webRTCConn.Close())
  432. }
  433. func (conn *ClientConn) IsClosed() bool {
  434. return conn.webRTCConn.IsClosed()
  435. }
  436. func (conn *ClientConn) Read(p []byte) (int, error) {
  437. n, err := conn.webRTCConn.Read(p)
  438. return n, errors.Trace(err)
  439. }
  440. // Write relays p through the in-proxy connection. len(p) should be under
  441. // 32K.
  442. func (conn *ClientConn) Write(p []byte) (int, error) {
  443. n, err := conn.webRTCConn.Write(p)
  444. return n, errors.Trace(err)
  445. }
  446. func (conn *ClientConn) LocalAddr() net.Addr {
  447. return conn.webRTCConn.LocalAddr()
  448. }
  449. func (conn *ClientConn) RemoteAddr() net.Addr {
  450. // Do not return conn.webRTCConn.RemoteAddr(), which is always nil.
  451. return conn.remoteAddr
  452. }
  453. func (conn *ClientConn) SetDeadline(t time.Time) error {
  454. return conn.webRTCConn.SetDeadline(t)
  455. }
  456. func (conn *ClientConn) SetReadDeadline(t time.Time) error {
  457. return conn.webRTCConn.SetReadDeadline(t)
  458. }
  459. func (conn *ClientConn) SetWriteDeadline(t time.Time) error {
  460. // Limitation: this is a workaround; webRTCConn doesn't support
  461. // SetWriteDeadline, but common/quic calls SetWriteDeadline on
  462. // net.PacketConns to avoid hanging on EAGAIN when the conn is an actual
  463. // UDP socket. See the comment in common/quic.writeTimeoutUDPConn. In
  464. // this case, the conn is not a UDP socket and that particular
  465. // SetWriteDeadline use case doesn't apply. Silently ignore the deadline
  466. // and report no error.
  467. return nil
  468. }
  469. func (conn *ClientConn) ReadFrom(b []byte) (int, net.Addr, error) {
  470. n, err := conn.webRTCConn.Read(b)
  471. return n, conn.webRTCConn.RemoteAddr(), err
  472. }
  473. func (conn *ClientConn) WriteTo(b []byte, _ net.Addr) (int, error) {
  474. n, err := conn.webRTCConn.Write(b)
  475. return n, err
  476. }