client.go 18 KB

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