controller.go 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889
  1. /*
  2. * Copyright (c) 2015, 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 psiphon implements the core tunnel functionality of a Psiphon client.
  20. // The main function is RunForever, which runs a Controller that obtains lists of
  21. // servers, establishes tunnel connections, and runs local proxies through which
  22. // tunneled traffic may be sent.
  23. package psiphon
  24. import (
  25. "context"
  26. "errors"
  27. "fmt"
  28. "math/rand"
  29. "net"
  30. "sync"
  31. "time"
  32. "github.com/Psiphon-Inc/goarista/monotime"
  33. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  34. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/parameters"
  35. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
  36. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/tactics"
  37. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/tun"
  38. )
  39. // Controller is a tunnel lifecycle coordinator. It manages lists of servers to
  40. // connect to; establishes and monitors tunnels; and runs local proxies which
  41. // route traffic through the tunnels.
  42. type Controller struct {
  43. config *Config
  44. sessionId string
  45. runCtx context.Context
  46. stopRunning context.CancelFunc
  47. runWaitGroup *sync.WaitGroup
  48. connectedTunnels chan *Tunnel
  49. failedTunnels chan *Tunnel
  50. tunnelMutex sync.Mutex
  51. establishedOnce bool
  52. tunnels []*Tunnel
  53. nextTunnel int
  54. startedConnectedReporter bool
  55. isEstablishing bool
  56. concurrentEstablishTunnelsMutex sync.Mutex
  57. concurrentEstablishTunnels int
  58. concurrentMeekEstablishTunnels int
  59. peakConcurrentEstablishTunnels int
  60. peakConcurrentMeekEstablishTunnels int
  61. establishCtx context.Context
  62. stopEstablish context.CancelFunc
  63. establishWaitGroup *sync.WaitGroup
  64. candidateServerEntries chan *candidateServerEntry
  65. untunneledDialConfig *DialConfig
  66. splitTunnelClassifier *SplitTunnelClassifier
  67. signalFetchCommonRemoteServerList chan struct{}
  68. signalFetchObfuscatedServerLists chan struct{}
  69. signalDownloadUpgrade chan string
  70. impairedProtocolClassification map[string]int
  71. signalReportConnected chan struct{}
  72. serverAffinityDoneBroadcast chan struct{}
  73. newClientVerificationPayload chan string
  74. packetTunnelClient *tun.Client
  75. packetTunnelTransport *PacketTunnelTransport
  76. }
  77. type candidateServerEntry struct {
  78. serverEntry *protocol.ServerEntry
  79. isServerAffinityCandidate bool
  80. usePriorityProtocol bool
  81. impairedProtocols []string
  82. adjustedEstablishStartTime monotime.Time
  83. }
  84. // NewController initializes a new controller.
  85. func NewController(config *Config) (controller *Controller, err error) {
  86. if !config.IsCommitted() {
  87. return nil, common.ContextError(errors.New("uncommitted config"))
  88. }
  89. // Needed by regen, at least
  90. rand.Seed(int64(time.Now().Nanosecond()))
  91. // The session ID for the Psiphon server API is used across all
  92. // tunnels established by the controller.
  93. NoticeSessionId(config.SessionID)
  94. untunneledDialConfig := &DialConfig{
  95. UpstreamProxyURL: config.UpstreamProxyURL,
  96. CustomHeaders: config.CustomHeaders,
  97. DeviceBinder: config.deviceBinder,
  98. DnsServerGetter: config.DnsServerGetter,
  99. IPv6Synthesizer: config.IPv6Synthesizer,
  100. UseIndistinguishableTLS: config.UseIndistinguishableTLS,
  101. TrustedCACertificatesFilename: config.TrustedCACertificatesFilename,
  102. DeviceRegion: config.DeviceRegion,
  103. }
  104. controller = &Controller{
  105. config: config,
  106. sessionId: config.SessionID,
  107. runWaitGroup: new(sync.WaitGroup),
  108. // connectedTunnels and failedTunnels buffer sizes are large enough to
  109. // receive full pools of tunnels without blocking. Senders should not block.
  110. connectedTunnels: make(chan *Tunnel, config.TunnelPoolSize),
  111. failedTunnels: make(chan *Tunnel, config.TunnelPoolSize),
  112. tunnels: make([]*Tunnel, 0),
  113. establishedOnce: false,
  114. startedConnectedReporter: false,
  115. isEstablishing: false,
  116. untunneledDialConfig: untunneledDialConfig,
  117. impairedProtocolClassification: make(map[string]int),
  118. // TODO: Add a buffer of 1 so we don't miss a signal while receiver is
  119. // starting? Trade-off is potential back-to-back fetch remotes. As-is,
  120. // establish will eventually signal another fetch remote.
  121. signalFetchCommonRemoteServerList: make(chan struct{}),
  122. signalFetchObfuscatedServerLists: make(chan struct{}),
  123. signalDownloadUpgrade: make(chan string),
  124. signalReportConnected: make(chan struct{}),
  125. // Buffer allows SetClientVerificationPayloadForActiveTunnels to submit one
  126. // new payload without blocking or dropping it.
  127. newClientVerificationPayload: make(chan string, 1),
  128. }
  129. controller.splitTunnelClassifier = NewSplitTunnelClassifier(config, controller)
  130. if config.PacketTunnelTunFileDescriptor > 0 {
  131. // Run a packet tunnel client. The lifetime of the tun.Client is the
  132. // lifetime of the Controller, so it exists across tunnel establishments
  133. // and reestablishments. The PacketTunnelTransport provides a layer
  134. // that presents a continuosuly existing transport to the tun.Client;
  135. // it's set to use new SSH channels after new SSH tunnel establishes.
  136. packetTunnelTransport := NewPacketTunnelTransport()
  137. packetTunnelClient, err := tun.NewClient(&tun.ClientConfig{
  138. Logger: NoticeCommonLogger(),
  139. TunFileDescriptor: config.PacketTunnelTunFileDescriptor,
  140. Transport: packetTunnelTransport,
  141. })
  142. if err != nil {
  143. return nil, common.ContextError(err)
  144. }
  145. controller.packetTunnelClient = packetTunnelClient
  146. controller.packetTunnelTransport = packetTunnelTransport
  147. }
  148. return controller, nil
  149. }
  150. // Run executes the controller. Run exits if a controller
  151. // component fails or the parent context is canceled.
  152. func (controller *Controller) Run(ctx context.Context) {
  153. // Ensure fresh repetitive notice state for each run, so the
  154. // client will always get an AvailableEgressRegions notice,
  155. // an initial instance of any repetitive error notice, etc.
  156. ResetRepetitiveNotices()
  157. runCtx, stopRunning := context.WithCancel(ctx)
  158. defer stopRunning()
  159. controller.runCtx = runCtx
  160. controller.stopRunning = stopRunning
  161. // Start components
  162. // TODO: IPv6 support
  163. var listenIP string
  164. if controller.config.ListenInterface == "" {
  165. listenIP = "127.0.0.1"
  166. } else if controller.config.ListenInterface == "any" {
  167. listenIP = "0.0.0.0"
  168. } else {
  169. IPv4Address, _, err := common.GetInterfaceIPAddresses(controller.config.ListenInterface)
  170. if err == nil && IPv4Address == nil {
  171. err = fmt.Errorf("no IPv4 address for interface %s", controller.config.ListenInterface)
  172. }
  173. if err != nil {
  174. NoticeError("error getting listener IP: %s", err)
  175. return
  176. }
  177. listenIP = IPv4Address.String()
  178. }
  179. if !controller.config.DisableLocalSocksProxy {
  180. socksProxy, err := NewSocksProxy(controller.config, controller, listenIP)
  181. if err != nil {
  182. NoticeAlert("error initializing local SOCKS proxy: %s", err)
  183. return
  184. }
  185. defer socksProxy.Close()
  186. }
  187. if !controller.config.DisableLocalHTTPProxy {
  188. httpProxy, err := NewHttpProxy(controller.config, controller, listenIP)
  189. if err != nil {
  190. NoticeAlert("error initializing local HTTP proxy: %s", err)
  191. return
  192. }
  193. defer httpProxy.Close()
  194. }
  195. if !controller.config.DisableRemoteServerListFetcher {
  196. if controller.config.RemoteServerListURLs != nil {
  197. controller.runWaitGroup.Add(1)
  198. go controller.remoteServerListFetcher(
  199. "common",
  200. FetchCommonRemoteServerList,
  201. controller.signalFetchCommonRemoteServerList)
  202. }
  203. if controller.config.ObfuscatedServerListRootURLs != nil {
  204. controller.runWaitGroup.Add(1)
  205. go controller.remoteServerListFetcher(
  206. "obfuscated",
  207. FetchObfuscatedServerLists,
  208. controller.signalFetchObfuscatedServerLists)
  209. }
  210. }
  211. if controller.config.UpgradeDownloadURLs != nil {
  212. controller.runWaitGroup.Add(1)
  213. go controller.upgradeDownloader()
  214. }
  215. /// Note: the connected reporter isn't started until a tunnel is
  216. // established
  217. controller.runWaitGroup.Add(1)
  218. go controller.runTunnels()
  219. controller.runWaitGroup.Add(1)
  220. go controller.establishTunnelWatcher()
  221. if controller.packetTunnelClient != nil {
  222. controller.packetTunnelClient.Start()
  223. }
  224. // Wait while running
  225. <-controller.runCtx.Done()
  226. NoticeInfo("controller stopped")
  227. if controller.packetTunnelClient != nil {
  228. controller.packetTunnelClient.Stop()
  229. }
  230. // All workers -- runTunnels, establishment workers, and auxilliary
  231. // workers such as fetch remote server list and untunneled uprade
  232. // download -- operate with the controller run context and will all
  233. // be interrupted when the run context is done.
  234. controller.runWaitGroup.Wait()
  235. controller.splitTunnelClassifier.Shutdown()
  236. NoticeInfo("exiting controller")
  237. NoticeExiting()
  238. }
  239. // SignalComponentFailure notifies the controller that an associated component has failed.
  240. // This will terminate the controller.
  241. func (controller *Controller) SignalComponentFailure() {
  242. NoticeAlert("controller shutdown due to component failure")
  243. controller.stopRunning()
  244. }
  245. // SetClientVerificationPayloadForActiveTunnels sets the client verification
  246. // payload that is to be sent in client verification requests to all established
  247. // tunnels.
  248. //
  249. // Client verification is used to verify that the client is a
  250. // valid Psiphon client, which will determine how the server treats
  251. // the client traffic. The proof-of-validity is platform-specific
  252. // and the payload is opaque to this function but assumed to be JSON.
  253. //
  254. // Since, in some cases, verification payload cannot be determined until
  255. // after tunnel-core starts, the payload cannot be simply specified in
  256. // the Config.
  257. //
  258. // SetClientVerificationPayloadForActiveTunnels will not block enqueuing a new verification
  259. // payload. One new payload can be enqueued, after which additional payloads
  260. // will be dropped if a payload is still enqueued.
  261. func (controller *Controller) SetClientVerificationPayloadForActiveTunnels(clientVerificationPayload string) {
  262. select {
  263. case controller.newClientVerificationPayload <- clientVerificationPayload:
  264. default:
  265. }
  266. }
  267. // remoteServerListFetcher fetches an out-of-band list of server entries
  268. // for more tunnel candidates. It fetches when signalled, with retries
  269. // on failure.
  270. func (controller *Controller) remoteServerListFetcher(
  271. name string,
  272. fetcher RemoteServerListFetcher,
  273. signal <-chan struct{}) {
  274. defer controller.runWaitGroup.Done()
  275. var lastFetchTime monotime.Time
  276. fetcherLoop:
  277. for {
  278. // Wait for a signal before fetching
  279. select {
  280. case <-signal:
  281. case <-controller.runCtx.Done():
  282. break fetcherLoop
  283. }
  284. // Skip fetch entirely (i.e., send no request at all, even when ETag would save
  285. // on response size) when a recent fetch was successful
  286. stalePeriod := controller.config.clientParameters.Get().Duration(
  287. parameters.FetchRemoteServerListStalePeriod)
  288. if lastFetchTime != 0 &&
  289. lastFetchTime.Add(stalePeriod).After(monotime.Now()) {
  290. continue
  291. }
  292. retryLoop:
  293. for attempt := 0; ; attempt++ {
  294. // Don't attempt to fetch while there is no network connectivity,
  295. // to avoid alert notice noise.
  296. if !WaitForNetworkConnectivity(
  297. controller.runCtx,
  298. controller.config.NetworkConnectivityChecker) {
  299. break fetcherLoop
  300. }
  301. // Pick any active tunnel and make the next fetch attempt. If there's
  302. // no active tunnel, the untunneledDialConfig will be used.
  303. tunnel := controller.getNextActiveTunnel()
  304. err := fetcher(
  305. controller.runCtx,
  306. controller.config,
  307. attempt,
  308. tunnel,
  309. controller.untunneledDialConfig)
  310. if err == nil {
  311. lastFetchTime = monotime.Now()
  312. break retryLoop
  313. }
  314. NoticeAlert("failed to fetch %s remote server list: %s", name, err)
  315. retryPeriod := controller.config.clientParameters.Get().Duration(
  316. parameters.FetchRemoteServerListRetryPeriod)
  317. timer := time.NewTimer(retryPeriod)
  318. select {
  319. case <-timer.C:
  320. case <-controller.runCtx.Done():
  321. timer.Stop()
  322. break fetcherLoop
  323. }
  324. }
  325. }
  326. NoticeInfo("exiting %s remote server list fetcher", name)
  327. }
  328. // establishTunnelWatcher terminates the controller if a tunnel
  329. // has not been established in the configured time period. This
  330. // is regardless of how many tunnels are presently active -- meaning
  331. // that if an active tunnel was established and lost the controller
  332. // is left running (to re-establish).
  333. func (controller *Controller) establishTunnelWatcher() {
  334. defer controller.runWaitGroup.Done()
  335. timeout := controller.config.clientParameters.Get().Duration(
  336. parameters.EstablishTunnelTimeout)
  337. if timeout > 0 {
  338. timer := time.NewTimer(timeout)
  339. defer timer.Stop()
  340. select {
  341. case <-timer.C:
  342. if !controller.hasEstablishedOnce() {
  343. NoticeAlert("failed to establish tunnel before timeout")
  344. controller.SignalComponentFailure()
  345. }
  346. case <-controller.runCtx.Done():
  347. }
  348. }
  349. NoticeInfo("exiting establish tunnel watcher")
  350. }
  351. // connectedReporter sends periodic "connected" requests to the Psiphon API.
  352. // These requests are for server-side unique user stats calculation. See the
  353. // comment in DoConnectedRequest for a description of the request mechanism.
  354. // To ensure we don't over- or under-count unique users, only one connected
  355. // request is made across all simultaneous multi-tunnels; and the connected
  356. // request is repeated periodically for very long-lived tunnels.
  357. // The signalReportConnected mechanism is used to trigger another connected
  358. // request immediately after a reconnect.
  359. func (controller *Controller) connectedReporter() {
  360. defer controller.runWaitGroup.Done()
  361. loop:
  362. for {
  363. // Pick any active tunnel and make the next connected request. No error
  364. // is logged if there's no active tunnel, as that's not an unexpected condition.
  365. reported := false
  366. tunnel := controller.getNextActiveTunnel()
  367. if tunnel != nil {
  368. err := tunnel.serverContext.DoConnectedRequest()
  369. if err == nil {
  370. reported = true
  371. } else {
  372. NoticeAlert("failed to make connected request: %s", err)
  373. }
  374. }
  375. // Schedule the next connected request and wait.
  376. // Note: this duration is not a dynamic ClientParameter as
  377. // the daily unique user stats logic specifically requires
  378. // a "connected" request no more or less often than every
  379. // 24 hours.
  380. var duration time.Duration
  381. if reported {
  382. duration = 24 * time.Hour
  383. } else {
  384. duration = controller.config.clientParameters.Get().Duration(
  385. parameters.PsiphonAPIConnectedRequestRetryPeriod)
  386. }
  387. timer := time.NewTimer(duration)
  388. doBreak := false
  389. select {
  390. case <-controller.signalReportConnected:
  391. case <-timer.C:
  392. // Make another connected request
  393. case <-controller.runCtx.Done():
  394. doBreak = true
  395. }
  396. timer.Stop()
  397. if doBreak {
  398. break loop
  399. }
  400. }
  401. NoticeInfo("exiting connected reporter")
  402. }
  403. func (controller *Controller) startOrSignalConnectedReporter() {
  404. // session is nil when DisableApi is set
  405. if controller.config.DisableApi {
  406. return
  407. }
  408. // Start the connected reporter after the first tunnel is established.
  409. // Concurrency note: only the runTunnels goroutine may access startedConnectedReporter.
  410. if !controller.startedConnectedReporter {
  411. controller.startedConnectedReporter = true
  412. controller.runWaitGroup.Add(1)
  413. go controller.connectedReporter()
  414. } else {
  415. select {
  416. case controller.signalReportConnected <- *new(struct{}):
  417. default:
  418. }
  419. }
  420. }
  421. // upgradeDownloader makes periodic attempts to complete a client upgrade
  422. // download. DownloadUpgrade() is resumable, so each attempt has potential for
  423. // getting closer to completion, even in conditions where the download or
  424. // tunnel is repeatedly interrupted.
  425. // An upgrade download is triggered by either a handshake response indicating
  426. // that a new version is available; or after failing to connect, in which case
  427. // it's useful to check, out-of-band, for an upgrade with new circumvention
  428. // capabilities.
  429. // Once the download operation completes successfully, the downloader exits
  430. // and is not run again: either there is not a newer version, or the upgrade
  431. // has been downloaded and is ready to be applied.
  432. // We're assuming that the upgrade will be applied and the entire system
  433. // restarted before another upgrade is to be downloaded.
  434. //
  435. // TODO: refactor upgrade downloader and remote server list fetcher to use
  436. // common code (including the resumable download routines).
  437. //
  438. func (controller *Controller) upgradeDownloader() {
  439. defer controller.runWaitGroup.Done()
  440. var lastDownloadTime monotime.Time
  441. downloadLoop:
  442. for {
  443. // Wait for a signal before downloading
  444. var handshakeVersion string
  445. select {
  446. case handshakeVersion = <-controller.signalDownloadUpgrade:
  447. case <-controller.runCtx.Done():
  448. break downloadLoop
  449. }
  450. stalePeriod := controller.config.clientParameters.Get().Duration(
  451. parameters.FetchUpgradeStalePeriod)
  452. // Unless handshake is explicitly advertizing a new version, skip
  453. // checking entirely when a recent download was successful.
  454. if handshakeVersion == "" &&
  455. lastDownloadTime != 0 &&
  456. lastDownloadTime.Add(stalePeriod).After(monotime.Now()) {
  457. continue
  458. }
  459. retryLoop:
  460. for attempt := 0; ; attempt++ {
  461. // Don't attempt to download while there is no network connectivity,
  462. // to avoid alert notice noise.
  463. if !WaitForNetworkConnectivity(
  464. controller.runCtx,
  465. controller.config.NetworkConnectivityChecker) {
  466. break downloadLoop
  467. }
  468. // Pick any active tunnel and make the next download attempt. If there's
  469. // no active tunnel, the untunneledDialConfig will be used.
  470. tunnel := controller.getNextActiveTunnel()
  471. err := DownloadUpgrade(
  472. controller.runCtx,
  473. controller.config,
  474. attempt,
  475. handshakeVersion,
  476. tunnel,
  477. controller.untunneledDialConfig)
  478. if err == nil {
  479. lastDownloadTime = monotime.Now()
  480. break retryLoop
  481. }
  482. NoticeAlert("failed to download upgrade: %s", err)
  483. timeout := controller.config.clientParameters.Get().Duration(
  484. parameters.FetchUpgradeRetryPeriod)
  485. timer := time.NewTimer(timeout)
  486. select {
  487. case <-timer.C:
  488. case <-controller.runCtx.Done():
  489. timer.Stop()
  490. break downloadLoop
  491. }
  492. }
  493. }
  494. NoticeInfo("exiting upgrade downloader")
  495. }
  496. // runTunnels is the controller tunnel management main loop. It starts and stops
  497. // establishing tunnels based on the target tunnel pool size and the current size
  498. // of the pool. Tunnels are established asynchronously using worker goroutines.
  499. //
  500. // When there are no server entries for the target region/protocol, the
  501. // establishCandidateGenerator will yield no candidates and wait before
  502. // trying again. In the meantime, a remote server entry fetch may supply
  503. // valid candidates.
  504. //
  505. // When a tunnel is established, it's added to the active pool. The tunnel's
  506. // operateTunnel goroutine monitors the tunnel.
  507. //
  508. // When a tunnel fails, it's removed from the pool and the establish process is
  509. // restarted to fill the pool.
  510. func (controller *Controller) runTunnels() {
  511. defer controller.runWaitGroup.Done()
  512. var clientVerificationPayload string
  513. // Start running
  514. controller.startEstablishing()
  515. loop:
  516. for {
  517. select {
  518. case failedTunnel := <-controller.failedTunnels:
  519. NoticeAlert("tunnel failed: %s", failedTunnel.serverEntry.IpAddress)
  520. controller.terminateTunnel(failedTunnel)
  521. controller.classifyImpairedProtocol(failedTunnel)
  522. // Clear the reference to this tunnel before calling startEstablishing,
  523. // which will invoke a garbage collection.
  524. failedTunnel = nil
  525. // Concurrency note: only this goroutine may call startEstablishing/stopEstablishing,
  526. // which reference controller.isEstablishing.
  527. controller.startEstablishing()
  528. case connectedTunnel := <-controller.connectedTunnels:
  529. if controller.isImpairedProtocol(connectedTunnel.protocol) {
  530. // Protocol was classified as impaired while this tunnel established.
  531. // This is most likely to occur with TunnelPoolSize > 0. We log the
  532. // event but take no action. Discarding the tunnel would break the
  533. // impaired logic unless we did that (a) only if there are other
  534. // unimpaired protocols; (b) only during the first iteration of the
  535. // ESTABLISH_TUNNEL_WORK_TIME loop. By not discarding here, a true
  536. // impaired protocol may require an extra reconnect.
  537. NoticeAlert("connected tunnel with impaired protocol: %s", connectedTunnel.protocol)
  538. }
  539. // Tunnel establishment has two phases: connection and activation.
  540. //
  541. // Connection is run concurrently by the establishTunnelWorkers, to minimize
  542. // delay when it's not yet known which server and protocol will be available
  543. // and unblocked.
  544. //
  545. // Activation is run serially, here, to minimize the overhead of making a
  546. // handshake request and starting the operateTunnel management worker for a
  547. // tunnel which may be discarded.
  548. //
  549. // When the active tunnel will complete establishment, establishment is
  550. // stopped before activation. This interrupts all connecting tunnels and
  551. // garbage collects their memory. The purpose is to minimize memory
  552. // pressure when the handshake request is made. In the unlikely case that the
  553. // handshake fails, establishment is restarted.
  554. //
  555. // Any delays in stopEstablishing will delay the handshake for the last
  556. // active tunnel.
  557. //
  558. // In the typical case of TunnelPoolSize of 1, only a single handshake is
  559. // performed and the homepages notices file, when used, will not be modifed
  560. // after the NoticeTunnels(1) [i.e., connected] until NoticeTunnels(0) [i.e.,
  561. // disconnected]. For TunnelPoolSize > 1, serial handshakes only ensures that
  562. // each set of emitted NoticeHomepages is contiguous.
  563. active, outstanding := controller.numTunnels()
  564. // discardTunnel will be true here when already fully established.
  565. discardTunnel := (outstanding <= 0)
  566. isFirstTunnel := (active == 0)
  567. isLastTunnel := (outstanding == 1)
  568. if !discardTunnel {
  569. if isLastTunnel {
  570. controller.stopEstablishing()
  571. }
  572. err := connectedTunnel.Activate(controller.runCtx, controller)
  573. if err != nil {
  574. // Assume the Activate failed due to a broken tunnel connection,
  575. // currently the most likely case, and classify as impaired, as in
  576. // the failed tunnel case above.
  577. // TODO: distinguish between network and other errors
  578. controller.classifyImpairedProtocol(connectedTunnel)
  579. NoticeAlert("failed to activate %s: %s", connectedTunnel.serverEntry.IpAddress, err)
  580. discardTunnel = true
  581. } else {
  582. // It's unlikely that registerTunnel will fail, since only this goroutine
  583. // calls registerTunnel -- and after checking numTunnels; so failure is not
  584. // expected.
  585. if !controller.registerTunnel(connectedTunnel) {
  586. NoticeAlert("failed to register %s: %s", connectedTunnel.serverEntry.IpAddress)
  587. discardTunnel = true
  588. }
  589. }
  590. // May need to replace this tunnel
  591. if isLastTunnel && discardTunnel {
  592. controller.startEstablishing()
  593. }
  594. }
  595. if discardTunnel {
  596. controller.discardTunnel(connectedTunnel)
  597. // Clear the reference to this discarded tunnel and immediately run
  598. // a garbage collection to reclaim its memory.
  599. connectedTunnel = nil
  600. defaultGarbageCollection()
  601. // Skip the rest of this case
  602. break
  603. }
  604. NoticeActiveTunnel(
  605. connectedTunnel.serverEntry.IpAddress,
  606. connectedTunnel.protocol,
  607. connectedTunnel.serverEntry.SupportsSSHAPIRequests())
  608. if isFirstTunnel {
  609. // The split tunnel classifier is started once the first tunnel is
  610. // established. This first tunnel is passed in to be used to make
  611. // the routes data request.
  612. // A long-running controller may run while the host device is present
  613. // in different regions. In this case, we want the split tunnel logic
  614. // to switch to routes for new regions and not classify traffic based
  615. // on routes installed for older regions.
  616. // We assume that when regions change, the host network will also
  617. // change, and so all tunnels will fail and be re-established. Under
  618. // that assumption, the classifier will be re-Start()-ed here when
  619. // the region has changed.
  620. controller.splitTunnelClassifier.Start(connectedTunnel)
  621. // Signal a connected request on each 1st tunnel establishment. For
  622. // multi-tunnels, the session is connected as long as at least one
  623. // tunnel is established.
  624. controller.startOrSignalConnectedReporter()
  625. // If the handshake indicated that a new client version is available,
  626. // trigger an upgrade download.
  627. // Note: serverContext is nil when DisableApi is set
  628. if connectedTunnel.serverContext != nil &&
  629. connectedTunnel.serverContext.clientUpgradeVersion != "" {
  630. handshakeVersion := connectedTunnel.serverContext.clientUpgradeVersion
  631. select {
  632. case controller.signalDownloadUpgrade <- handshakeVersion:
  633. default:
  634. }
  635. }
  636. }
  637. // Set the new tunnel as the transport for the packet tunnel. The packet tunnel
  638. // client remains up when reestablishing, but no packets are relayed while there
  639. // is no connected tunnel. UseTunnel will establish a new packet tunnel SSH
  640. // channel over the new SSH tunnel and configure the packet tunnel client to use
  641. // the new SSH channel as its transport.
  642. //
  643. // Note: as is, this logic is suboptimal for TunnelPoolSize > 1, as this would
  644. // continuously initialize new packet tunnel sessions for each established
  645. // server. For now, config validation requires TunnelPoolSize == 1 when
  646. // the packet tunnel is used.
  647. if controller.packetTunnelTransport != nil {
  648. controller.packetTunnelTransport.UseTunnel(connectedTunnel)
  649. }
  650. // TODO: design issue -- might not be enough server entries with region/caps to ever fill tunnel slots;
  651. // possible solution is establish target MIN(CountServerEntries(region, protocol), TunnelPoolSize)
  652. if controller.isFullyEstablished() {
  653. controller.stopEstablishing()
  654. }
  655. case clientVerificationPayload = <-controller.newClientVerificationPayload:
  656. controller.setClientVerificationPayloadForActiveTunnels(clientVerificationPayload)
  657. case <-controller.runCtx.Done():
  658. break loop
  659. }
  660. }
  661. // Stop running
  662. controller.stopEstablishing()
  663. controller.terminateAllTunnels()
  664. // Drain tunnel channels
  665. close(controller.connectedTunnels)
  666. for tunnel := range controller.connectedTunnels {
  667. controller.discardTunnel(tunnel)
  668. }
  669. close(controller.failedTunnels)
  670. for tunnel := range controller.failedTunnels {
  671. controller.discardTunnel(tunnel)
  672. }
  673. NoticeInfo("exiting run tunnels")
  674. }
  675. // TerminateNextActiveTunnel is a support routine for
  676. // test code that must terminate the active tunnel and
  677. // restart establishing. This function is not guaranteed
  678. // to be safe for use in other cases.
  679. func (controller *Controller) TerminateNextActiveTunnel() {
  680. tunnel := controller.getNextActiveTunnel()
  681. if tunnel != nil {
  682. controller.SignalTunnelFailure(tunnel)
  683. NoticeInfo("terminated tunnel: %s", tunnel.serverEntry.IpAddress)
  684. }
  685. }
  686. // classifyImpairedProtocol tracks "impaired" protocol classifications for failed
  687. // tunnels. A protocol is classified as impaired if a tunnel using that protocol
  688. // fails, repeatedly, shortly after the start of the connection. During tunnel
  689. // establishment, impaired protocols are briefly skipped.
  690. //
  691. // One purpose of this measure is to defend against an attack where the adversary,
  692. // for example, tags an OSSH TCP connection as an "unidentified" protocol; allows
  693. // it to connect; but then kills the underlying TCP connection after a short time.
  694. // Since OSSH has less latency than other protocols that may bypass an "unidentified"
  695. // filter, these other protocols might never be selected for use.
  696. //
  697. // Concurrency note: only the runTunnels() goroutine may call classifyImpairedProtocol
  698. func (controller *Controller) classifyImpairedProtocol(failedTunnel *Tunnel) {
  699. // If the tunnel failed while activating, its establishedTime will be 0.
  700. duration := controller.config.clientParameters.Get().Duration(
  701. parameters.ImpairedProtocolClassificationDuration)
  702. if failedTunnel.establishedTime == 0 ||
  703. failedTunnel.establishedTime.Add(duration).After(monotime.Now()) {
  704. controller.impairedProtocolClassification[failedTunnel.protocol] += 1
  705. } else {
  706. controller.impairedProtocolClassification[failedTunnel.protocol] = 0
  707. }
  708. // Reset classification once all known protocols are classified as impaired, as
  709. // there is now no way to proceed with only unimpaired protocols. The network
  710. // situation (or attack) resulting in classification may not be protocol-specific.
  711. if CountNonImpairedProtocols(
  712. controller.config.EgressRegion,
  713. controller.config.clientParameters.Get().TunnelProtocols(
  714. parameters.LimitTunnelProtocols),
  715. controller.getImpairedProtocols()) == 0 {
  716. controller.impairedProtocolClassification = make(map[string]int)
  717. }
  718. }
  719. // getImpairedProtocols returns a list of protocols that have sufficient
  720. // classifications to be considered impaired protocols.
  721. //
  722. // Concurrency note: only the runTunnels() goroutine may call getImpairedProtocols
  723. func (controller *Controller) getImpairedProtocols() []string {
  724. NoticeImpairedProtocolClassification(controller.impairedProtocolClassification)
  725. threshold := controller.config.clientParameters.Get().Int(
  726. parameters.ImpairedProtocolClassificationThreshold)
  727. impairedProtocols := make([]string, 0)
  728. for protocol, count := range controller.impairedProtocolClassification {
  729. if count >= threshold {
  730. impairedProtocols = append(impairedProtocols, protocol)
  731. }
  732. }
  733. return impairedProtocols
  734. }
  735. // isImpairedProtocol checks if the specified protocol is classified as impaired.
  736. //
  737. // Concurrency note: only the runTunnels() goroutine may call isImpairedProtocol
  738. func (controller *Controller) isImpairedProtocol(protocol string) bool {
  739. threshold := controller.config.clientParameters.Get().Int(
  740. parameters.ImpairedProtocolClassificationThreshold)
  741. count, ok := controller.impairedProtocolClassification[protocol]
  742. return ok && count >= threshold
  743. }
  744. // SignalSeededNewSLOK implements the TunnelOwner interface. This function
  745. // is called by Tunnel.operateTunnel when the tunnel has received a new,
  746. // previously unknown SLOK from the server. The Controller triggers an OSL
  747. // fetch, as the new SLOK may be sufficient to access new OSLs.
  748. func (controller *Controller) SignalSeededNewSLOK() {
  749. select {
  750. case controller.signalFetchObfuscatedServerLists <- *new(struct{}):
  751. default:
  752. }
  753. }
  754. // SignalTunnelFailure implements the TunnelOwner interface. This function
  755. // is called by Tunnel.operateTunnel when the tunnel has detected that it
  756. // has failed. The Controller will signal runTunnels to create a new
  757. // tunnel and/or remove the tunnel from the list of active tunnels.
  758. func (controller *Controller) SignalTunnelFailure(tunnel *Tunnel) {
  759. // Don't block. Assumes the receiver has a buffer large enough for
  760. // the typical number of operated tunnels. In case there's no room,
  761. // terminate the tunnel (runTunnels won't get a signal in this case,
  762. // but the tunnel will be removed from the list of active tunnels).
  763. select {
  764. case controller.failedTunnels <- tunnel:
  765. default:
  766. controller.terminateTunnel(tunnel)
  767. }
  768. }
  769. // discardTunnel disposes of a successful connection that is no longer required.
  770. func (controller *Controller) discardTunnel(tunnel *Tunnel) {
  771. NoticeInfo("discard tunnel: %s", tunnel.serverEntry.IpAddress)
  772. // TODO: not calling PromoteServerEntry, since that would rank the
  773. // discarded tunnel before fully active tunnels. Can a discarded tunnel
  774. // be promoted (since it connects), but with lower rank than all active
  775. // tunnels?
  776. tunnel.Close(true)
  777. }
  778. // registerTunnel adds the connected tunnel to the pool of active tunnels
  779. // which are candidates for port forwarding. Returns true if the pool has an
  780. // empty slot and false if the pool is full (caller should discard the tunnel).
  781. func (controller *Controller) registerTunnel(tunnel *Tunnel) bool {
  782. controller.tunnelMutex.Lock()
  783. defer controller.tunnelMutex.Unlock()
  784. if len(controller.tunnels) >= controller.config.TunnelPoolSize {
  785. return false
  786. }
  787. // Perform a final check just in case we've established
  788. // a duplicate connection.
  789. for _, activeTunnel := range controller.tunnels {
  790. if activeTunnel.serverEntry.IpAddress == tunnel.serverEntry.IpAddress {
  791. NoticeAlert("duplicate tunnel: %s", tunnel.serverEntry.IpAddress)
  792. return false
  793. }
  794. }
  795. controller.establishedOnce = true
  796. controller.tunnels = append(controller.tunnels, tunnel)
  797. NoticeTunnels(len(controller.tunnels))
  798. // Promote this successful tunnel to first rank so it's one
  799. // of the first candidates next time establish runs.
  800. // Connecting to a TargetServerEntry does not change the
  801. // ranking.
  802. if controller.config.TargetServerEntry == "" {
  803. PromoteServerEntry(controller.config, tunnel.serverEntry.IpAddress)
  804. }
  805. return true
  806. }
  807. // hasEstablishedOnce indicates if at least one active tunnel has
  808. // been established up to this point. This is regardeless of how many
  809. // tunnels are presently active.
  810. func (controller *Controller) hasEstablishedOnce() bool {
  811. controller.tunnelMutex.Lock()
  812. defer controller.tunnelMutex.Unlock()
  813. return controller.establishedOnce
  814. }
  815. // isFullyEstablished indicates if the pool of active tunnels is full.
  816. func (controller *Controller) isFullyEstablished() bool {
  817. controller.tunnelMutex.Lock()
  818. defer controller.tunnelMutex.Unlock()
  819. return len(controller.tunnels) >= controller.config.TunnelPoolSize
  820. }
  821. // numTunnels returns the number of active and outstanding tunnels.
  822. // Oustanding is the number of tunnels required to fill the pool of
  823. // active tunnels.
  824. func (controller *Controller) numTunnels() (int, int) {
  825. controller.tunnelMutex.Lock()
  826. defer controller.tunnelMutex.Unlock()
  827. active := len(controller.tunnels)
  828. outstanding := controller.config.TunnelPoolSize - len(controller.tunnels)
  829. return active, outstanding
  830. }
  831. // terminateTunnel removes a tunnel from the pool of active tunnels
  832. // and closes the tunnel. The next-tunnel state used by getNextActiveTunnel
  833. // is adjusted as required.
  834. func (controller *Controller) terminateTunnel(tunnel *Tunnel) {
  835. controller.tunnelMutex.Lock()
  836. defer controller.tunnelMutex.Unlock()
  837. for index, activeTunnel := range controller.tunnels {
  838. if tunnel == activeTunnel {
  839. controller.tunnels = append(
  840. controller.tunnels[:index], controller.tunnels[index+1:]...)
  841. if controller.nextTunnel > index {
  842. controller.nextTunnel--
  843. }
  844. if controller.nextTunnel >= len(controller.tunnels) {
  845. controller.nextTunnel = 0
  846. }
  847. activeTunnel.Close(false)
  848. NoticeTunnels(len(controller.tunnels))
  849. break
  850. }
  851. }
  852. }
  853. // terminateAllTunnels empties the tunnel pool, closing all active tunnels.
  854. // This is used when shutting down the controller.
  855. func (controller *Controller) terminateAllTunnels() {
  856. controller.tunnelMutex.Lock()
  857. defer controller.tunnelMutex.Unlock()
  858. // Closing all tunnels in parallel. In an orderly shutdown, each tunnel
  859. // may take a few seconds to send a final status request. We only want
  860. // to wait as long as the single slowest tunnel.
  861. closeWaitGroup := new(sync.WaitGroup)
  862. closeWaitGroup.Add(len(controller.tunnels))
  863. for _, activeTunnel := range controller.tunnels {
  864. tunnel := activeTunnel
  865. go func() {
  866. defer closeWaitGroup.Done()
  867. tunnel.Close(false)
  868. }()
  869. }
  870. closeWaitGroup.Wait()
  871. controller.tunnels = make([]*Tunnel, 0)
  872. controller.nextTunnel = 0
  873. NoticeTunnels(len(controller.tunnels))
  874. }
  875. // getNextActiveTunnel returns the next tunnel from the pool of active
  876. // tunnels. Currently, tunnel selection order is simple round-robin.
  877. func (controller *Controller) getNextActiveTunnel() (tunnel *Tunnel) {
  878. controller.tunnelMutex.Lock()
  879. defer controller.tunnelMutex.Unlock()
  880. for i := len(controller.tunnels); i > 0; i-- {
  881. tunnel = controller.tunnels[controller.nextTunnel]
  882. controller.nextTunnel =
  883. (controller.nextTunnel + 1) % len(controller.tunnels)
  884. return tunnel
  885. }
  886. return nil
  887. }
  888. // isActiveTunnelServerEntry is used to check if there's already
  889. // an existing tunnel to a candidate server.
  890. func (controller *Controller) isActiveTunnelServerEntry(
  891. serverEntry *protocol.ServerEntry) bool {
  892. controller.tunnelMutex.Lock()
  893. defer controller.tunnelMutex.Unlock()
  894. for _, activeTunnel := range controller.tunnels {
  895. if activeTunnel.serverEntry.IpAddress == serverEntry.IpAddress {
  896. return true
  897. }
  898. }
  899. return false
  900. }
  901. // setClientVerificationPayloadForActiveTunnels triggers the client verification
  902. // request for all active tunnels.
  903. func (controller *Controller) setClientVerificationPayloadForActiveTunnels(
  904. clientVerificationPayload string) {
  905. controller.tunnelMutex.Lock()
  906. defer controller.tunnelMutex.Unlock()
  907. for _, activeTunnel := range controller.tunnels {
  908. activeTunnel.SetClientVerificationPayload(clientVerificationPayload)
  909. }
  910. }
  911. // Dial selects an active tunnel and establishes a port forward
  912. // connection through the selected tunnel. Failure to connect is considered
  913. // a port forward failure, for the purpose of monitoring tunnel health.
  914. func (controller *Controller) Dial(
  915. remoteAddr string, alwaysTunnel bool, downstreamConn net.Conn) (conn net.Conn, err error) {
  916. tunnel := controller.getNextActiveTunnel()
  917. if tunnel == nil {
  918. return nil, common.ContextError(errors.New("no active tunnels"))
  919. }
  920. // Perform split tunnel classification when feature is enabled, and if the remote
  921. // address is classified as untunneled, dial directly.
  922. if !alwaysTunnel && controller.config.SplitTunnelDNSServer != "" {
  923. host, _, err := net.SplitHostPort(remoteAddr)
  924. if err != nil {
  925. return nil, common.ContextError(err)
  926. }
  927. // Note: a possible optimization, when split tunnel is active and IsUntunneled performs
  928. // a DNS resolution in order to make its classification, is to reuse that IP address in
  929. // the following Dials so they do not need to make their own resolutions. However, the
  930. // way this is currently implemented ensures that, e.g., DNS geo load balancing occurs
  931. // relative to the outbound network.
  932. if controller.splitTunnelClassifier.IsUntunneled(host) {
  933. return controller.DirectDial(remoteAddr)
  934. }
  935. }
  936. tunneledConn, err := tunnel.Dial(remoteAddr, alwaysTunnel, downstreamConn)
  937. if err != nil {
  938. return nil, common.ContextError(err)
  939. }
  940. return tunneledConn, nil
  941. }
  942. // DirectDial dials an untunneled TCP connection within the controller run context.
  943. func (controller *Controller) DirectDial(remoteAddr string) (conn net.Conn, err error) {
  944. return DialTCP(controller.runCtx, remoteAddr, controller.untunneledDialConfig)
  945. }
  946. // startEstablishing creates a pool of worker goroutines which will
  947. // attempt to establish tunnels to candidate servers. The candidates
  948. // are generated by another goroutine.
  949. func (controller *Controller) startEstablishing() {
  950. if controller.isEstablishing {
  951. return
  952. }
  953. NoticeInfo("start establishing")
  954. controller.concurrentEstablishTunnelsMutex.Lock()
  955. controller.concurrentEstablishTunnels = 0
  956. controller.concurrentMeekEstablishTunnels = 0
  957. controller.peakConcurrentEstablishTunnels = 0
  958. controller.peakConcurrentMeekEstablishTunnels = 0
  959. controller.concurrentEstablishTunnelsMutex.Unlock()
  960. aggressiveGarbageCollection()
  961. emitMemoryMetrics()
  962. // Note: the establish context cancelFunc, controller.stopEstablish,
  963. // is called in controller.stopEstablishing.
  964. controller.isEstablishing = true
  965. controller.establishCtx, controller.stopEstablish = context.WithCancel(controller.runCtx)
  966. controller.establishWaitGroup = new(sync.WaitGroup)
  967. controller.candidateServerEntries = make(chan *candidateServerEntry)
  968. // The server affinity mechanism attempts to favor the previously
  969. // used server when reconnecting. This is beneficial for user
  970. // applications which expect consistency in user IP address (for
  971. // example, a web site which prompts for additional user
  972. // authentication when the IP address changes).
  973. //
  974. // Only the very first server, as determined by
  975. // datastore.PromoteServerEntry(), is the server affinity candidate.
  976. // Concurrent connections attempts to many servers are launched
  977. // without delay, in case the affinity server connection fails.
  978. // While the affinity server connection is outstanding, when any
  979. // other connection is established, there is a short grace period
  980. // delay before delivering the established tunnel; this allows some
  981. // time for the affinity server connection to succeed first.
  982. // When the affinity server connection fails, any other established
  983. // tunnel is registered without delay.
  984. //
  985. // Note: the establishTunnelWorker that receives the affinity
  986. // candidate is solely resonsible for closing
  987. // controller.serverAffinityDoneBroadcast.
  988. controller.serverAffinityDoneBroadcast = make(chan struct{})
  989. controller.establishWaitGroup.Add(1)
  990. go controller.launchEstablishing()
  991. }
  992. func (controller *Controller) launchEstablishing() {
  993. defer controller.establishWaitGroup.Done()
  994. // Before starting the establish tunnel workers, get and apply
  995. // tactics, launching a tactics request if required.
  996. //
  997. // Wait only TacticsWaitPeriod for the tactics request to complete (or
  998. // fail) before proceeding with tunnel establishment, in case the tactics
  999. // request is blocked or takes very long to complete.
  1000. //
  1001. // An in-flight tactics request uses meek in round tripper mode, which
  1002. // uses less resources than meek tunnel relay mode. For this reason, the
  1003. // tactics request is not counted in concurrentMeekEstablishTunnels.
  1004. //
  1005. // TODO: HTTP/2 uses significantly more memory, so perhaps
  1006. // concurrentMeekEstablishTunnels should be counted in that case.
  1007. //
  1008. // Any in-flight tactics request or pending retry will be
  1009. // canceled when establishment is stopped.
  1010. doTactics := !controller.config.DisableTactics &&
  1011. controller.config.networkIDGetter != nil
  1012. if doTactics {
  1013. timeout := controller.config.clientParameters.Get().Duration(
  1014. parameters.TacticsWaitPeriod)
  1015. tacticsDone := make(chan struct{})
  1016. tacticsWaitPeriod := time.NewTimer(timeout)
  1017. defer tacticsWaitPeriod.Stop()
  1018. controller.establishWaitGroup.Add(1)
  1019. go controller.getTactics(tacticsDone)
  1020. select {
  1021. case <-tacticsDone:
  1022. case <-tacticsWaitPeriod.C:
  1023. }
  1024. tacticsWaitPeriod.Stop()
  1025. if controller.isStopEstablishing() {
  1026. // This check isn't strictly required by avoids the
  1027. // overhead of launching workers if establishment
  1028. // stopped while awaiting a tactics request.
  1029. return
  1030. }
  1031. }
  1032. // Unconditionally report available egress regions. After a fresh install,
  1033. // the outer client may not have a list of regions to display, so we
  1034. // always report here. Events that trigger ReportAvailableRegions,
  1035. // including storing new server entries and applying tactics, are not
  1036. // guaranteed to occur.
  1037. //
  1038. // This report is delayed until after tactics are likely to be applied, as
  1039. // tactics can impact the list of available regions; this avoids a
  1040. // ReportAvailableRegions reporting too many regions, followed shortly by
  1041. // a ReportAvailableRegions reporting fewer regions. That sequence could
  1042. // cause issues in the outer client UI.
  1043. ReportAvailableRegions(controller.config)
  1044. // The ConnectionWorkerPoolSize may be set by tactics.
  1045. size := controller.config.clientParameters.Get().Int(
  1046. parameters.ConnectionWorkerPoolSize)
  1047. for i := 0; i < size; i++ {
  1048. controller.establishWaitGroup.Add(1)
  1049. go controller.establishTunnelWorker()
  1050. }
  1051. controller.establishWaitGroup.Add(1)
  1052. go controller.establishCandidateGenerator(
  1053. controller.getImpairedProtocols())
  1054. }
  1055. // stopEstablishing signals the establish goroutines to stop and waits
  1056. // for the group to halt.
  1057. func (controller *Controller) stopEstablishing() {
  1058. if !controller.isEstablishing {
  1059. return
  1060. }
  1061. NoticeInfo("stop establishing")
  1062. controller.stopEstablish()
  1063. // Note: establishCandidateGenerator closes controller.candidateServerEntries
  1064. // (as it may be sending to that channel).
  1065. controller.establishWaitGroup.Wait()
  1066. NoticeInfo("stopped establishing")
  1067. controller.isEstablishing = false
  1068. controller.establishCtx = nil
  1069. controller.stopEstablish = nil
  1070. controller.establishWaitGroup = nil
  1071. controller.candidateServerEntries = nil
  1072. controller.serverAffinityDoneBroadcast = nil
  1073. controller.concurrentEstablishTunnelsMutex.Lock()
  1074. peakConcurrent := controller.peakConcurrentEstablishTunnels
  1075. peakConcurrentMeek := controller.peakConcurrentMeekEstablishTunnels
  1076. controller.concurrentEstablishTunnels = 0
  1077. controller.concurrentMeekEstablishTunnels = 0
  1078. controller.peakConcurrentEstablishTunnels = 0
  1079. controller.peakConcurrentMeekEstablishTunnels = 0
  1080. controller.concurrentEstablishTunnelsMutex.Unlock()
  1081. NoticeInfo("peak concurrent establish tunnels: %d", peakConcurrent)
  1082. NoticeInfo("peak concurrent meek establish tunnels: %d", peakConcurrentMeek)
  1083. emitMemoryMetrics()
  1084. standardGarbageCollection()
  1085. }
  1086. func (controller *Controller) getTactics(done chan struct{}) {
  1087. defer controller.establishWaitGroup.Done()
  1088. defer close(done)
  1089. tacticsRecord, err := tactics.UseStoredTactics(
  1090. GetTacticsStorer(),
  1091. controller.config.networkIDGetter.GetNetworkID())
  1092. if err != nil {
  1093. NoticeAlert("get stored tactics failed: %s", err)
  1094. // The error will be due to a local datastore problem.
  1095. // While we could proceed with the tactics request, this
  1096. // could result in constant tactics requests. So, abort.
  1097. return
  1098. }
  1099. if tacticsRecord == nil {
  1100. iterator, err := NewTacticsServerEntryIterator(
  1101. controller.config)
  1102. if err != nil {
  1103. NoticeAlert("tactics iterator failed: %s", err)
  1104. return
  1105. }
  1106. defer iterator.Close()
  1107. for iteration := 0; ; iteration++ {
  1108. if !WaitForNetworkConnectivity(
  1109. controller.runCtx,
  1110. controller.config.NetworkConnectivityChecker) {
  1111. return
  1112. }
  1113. serverEntry, err := iterator.Next()
  1114. if err != nil {
  1115. NoticeAlert("tactics iterator failed: %s", err)
  1116. return
  1117. }
  1118. if serverEntry == nil {
  1119. if iteration == 0 {
  1120. NoticeAlert("tactics request skipped: no capable servers")
  1121. return
  1122. }
  1123. iterator.Reset()
  1124. continue
  1125. }
  1126. tacticsRecord, err = controller.doFetchTactics(serverEntry)
  1127. if err == nil {
  1128. break
  1129. }
  1130. NoticeAlert("tactics request failed: %s", err)
  1131. // On error, proceed with a retry, as the error is likely
  1132. // due to a network failure.
  1133. //
  1134. // TODO: distinguish network and local errors and abort
  1135. // on local errors.
  1136. p := controller.config.clientParameters.Get()
  1137. timeout := common.JitterDuration(
  1138. p.Duration(parameters.TacticsRetryPeriod),
  1139. p.Float(parameters.TacticsRetryPeriodJitter))
  1140. p = nil
  1141. tacticsRetryDelay := time.NewTimer(timeout)
  1142. select {
  1143. case <-controller.establishCtx.Done():
  1144. return
  1145. case <-tacticsRetryDelay.C:
  1146. }
  1147. tacticsRetryDelay.Stop()
  1148. }
  1149. }
  1150. if tacticsRecord != nil &&
  1151. common.FlipWeightedCoin(tacticsRecord.Tactics.Probability) {
  1152. err := controller.config.SetClientParameters(
  1153. tacticsRecord.Tag, true, tacticsRecord.Tactics.Parameters)
  1154. if err != nil {
  1155. NoticeAlert("apply tactics failed: %s", err)
  1156. // The error will be due to invalid tactics values from
  1157. // the server. When ApplyClientParameters fails, all
  1158. // previous tactics values are left in place. Abort
  1159. // without retry since the server is highly unlikely
  1160. // to return different values immediately.
  1161. return
  1162. }
  1163. }
  1164. // Reclaim memory from the completed tactics request as we're likely
  1165. // to be proceeding to the memory-intensive tunnel establishment phase.
  1166. aggressiveGarbageCollection()
  1167. emitMemoryMetrics()
  1168. }
  1169. func (controller *Controller) doFetchTactics(
  1170. serverEntry *protocol.ServerEntry) (*tactics.Record, error) {
  1171. tacticsProtocols := serverEntry.GetSupportedTacticsProtocols()
  1172. index, err := common.MakeSecureRandomInt(len(tacticsProtocols))
  1173. if err != nil {
  1174. return nil, common.ContextError(err)
  1175. }
  1176. tacticsProtocol := tacticsProtocols[index]
  1177. meekConfig, err := initMeekConfig(
  1178. controller.config,
  1179. serverEntry,
  1180. tacticsProtocol,
  1181. "")
  1182. if err != nil {
  1183. return nil, common.ContextError(err)
  1184. }
  1185. meekConfig.RoundTripperOnly = true
  1186. dialConfig, dialStats := initDialConfig(controller.config, meekConfig)
  1187. NoticeRequestingTactics(
  1188. serverEntry.IpAddress,
  1189. serverEntry.Region,
  1190. tacticsProtocol,
  1191. dialStats)
  1192. // TacticsTimeout should be a very long timeout, since it's not
  1193. // adjusted by tactics in a new network context, and so clients
  1194. // with very slow connections must be accomodated. This long
  1195. // timeout will not entirely block the beginning of tunnel
  1196. // establishment, which beings after the shorter TacticsWaitPeriod.
  1197. //
  1198. // Using controller.establishCtx will cancel FetchTactics
  1199. // if tunnel establishment completes first.
  1200. timeout := controller.config.clientParameters.Get().Duration(
  1201. parameters.TacticsTimeout)
  1202. ctx, cancelFunc := context.WithTimeout(
  1203. controller.establishCtx,
  1204. timeout)
  1205. defer cancelFunc()
  1206. // DialMeek completes the TCP/TLS handshakes for HTTPS
  1207. // meek protocols but _not_ for HTTP meek protocols.
  1208. //
  1209. // TODO: pre-dial HTTP protocols to conform with speed
  1210. // test RTT spec.
  1211. //
  1212. // TODO: ensure that meek in round trip mode will fail
  1213. // the request when the pre-dial connection is broken,
  1214. // to minimize the possibility of network ID mismatches.
  1215. meekConn, err := DialMeek(ctx, meekConfig, dialConfig)
  1216. if err != nil {
  1217. return nil, common.ContextError(err)
  1218. }
  1219. defer meekConn.Close()
  1220. apiParams := getBaseAPIParameters(
  1221. controller.config,
  1222. controller.sessionId,
  1223. serverEntry,
  1224. tacticsProtocol,
  1225. dialStats)
  1226. tacticsRecord, err := tactics.FetchTactics(
  1227. ctx,
  1228. controller.config.clientParameters,
  1229. GetTacticsStorer(),
  1230. controller.config.networkIDGetter.GetNetworkID,
  1231. apiParams,
  1232. serverEntry.Region,
  1233. tacticsProtocol,
  1234. serverEntry.TacticsRequestPublicKey,
  1235. serverEntry.TacticsRequestObfuscatedKey,
  1236. meekConn.RoundTrip)
  1237. if err != nil {
  1238. return nil, common.ContextError(err)
  1239. }
  1240. NoticeRequestedTactics(
  1241. serverEntry.IpAddress,
  1242. serverEntry.Region,
  1243. tacticsProtocol,
  1244. dialStats)
  1245. return tacticsRecord, nil
  1246. }
  1247. // establishCandidateGenerator populates the candidate queue with server entries
  1248. // from the data store. Server entries are iterated in rank order, so that promoted
  1249. // servers with higher rank are priority candidates.
  1250. func (controller *Controller) establishCandidateGenerator(impairedProtocols []string) {
  1251. defer controller.establishWaitGroup.Done()
  1252. defer close(controller.candidateServerEntries)
  1253. // establishStartTime is used to calculate and report the
  1254. // client's tunnel establishment duration.
  1255. //
  1256. // networkWaitDuration is the elapsed time spent waiting
  1257. // for network connectivity. This duration will be excluded
  1258. // from reported tunnel establishment duration.
  1259. establishStartTime := monotime.Now()
  1260. var networkWaitDuration time.Duration
  1261. applyServerAffinity, iterator, err := NewServerEntryIterator(controller.config)
  1262. if err != nil {
  1263. NoticeAlert("failed to iterate over candidates: %s", err)
  1264. controller.SignalComponentFailure()
  1265. return
  1266. }
  1267. defer iterator.Close()
  1268. // TODO: reconcile server affinity scheme with multi-tunnel mode
  1269. if controller.config.TunnelPoolSize > 1 {
  1270. applyServerAffinity = false
  1271. }
  1272. isServerAffinityCandidate := true
  1273. if !applyServerAffinity {
  1274. isServerAffinityCandidate = false
  1275. close(controller.serverAffinityDoneBroadcast)
  1276. }
  1277. candidateCount := 0
  1278. loop:
  1279. // Repeat until stopped
  1280. for i := 0; ; i++ {
  1281. networkWaitStartTime := monotime.Now()
  1282. if !WaitForNetworkConnectivity(
  1283. controller.establishCtx,
  1284. controller.config.NetworkConnectivityChecker) {
  1285. break loop
  1286. }
  1287. networkWaitDuration += monotime.Since(networkWaitStartTime)
  1288. // Send each iterator server entry to the establish workers
  1289. startTime := monotime.Now()
  1290. for {
  1291. serverEntry, err := iterator.Next()
  1292. if err != nil {
  1293. NoticeAlert("failed to get next candidate: %s", err)
  1294. controller.SignalComponentFailure()
  1295. break loop
  1296. }
  1297. if serverEntry == nil {
  1298. // Completed this iteration
  1299. break
  1300. }
  1301. if controller.config.TargetApiProtocol == protocol.PSIPHON_SSH_API_PROTOCOL &&
  1302. !serverEntry.SupportsSSHAPIRequests() {
  1303. continue
  1304. }
  1305. // Use a prioritized tunnel protocol for the first
  1306. // PrioritizeTunnelProtocolsCandidateCount candidates.
  1307. // This facility can be used to favor otherwise slower
  1308. // protocols.
  1309. prioritizeCandidateCount := controller.config.clientParameters.Get().Int(
  1310. parameters.PrioritizeTunnelProtocolsCandidateCount)
  1311. usePriorityProtocol := candidateCount < prioritizeCandidateCount
  1312. // Disable impaired protocols. This is only done for the
  1313. // first iteration of the EstablishTunnelWorkTime
  1314. // loop since (a) one iteration should be sufficient to
  1315. // evade the attack; (b) there's a good chance of false
  1316. // positives (such as short tunnel durations due to network
  1317. // hopping on a mobile device).
  1318. var candidateImpairedProtocols []string
  1319. if i == 0 {
  1320. candidateImpairedProtocols = impairedProtocols
  1321. }
  1322. // adjustedEstablishStartTime is establishStartTime shifted
  1323. // to exclude time spent waiting for network connectivity.
  1324. adjustedEstablishStartTime := establishStartTime.Add(networkWaitDuration)
  1325. candidate := &candidateServerEntry{
  1326. serverEntry: serverEntry,
  1327. isServerAffinityCandidate: isServerAffinityCandidate,
  1328. usePriorityProtocol: usePriorityProtocol,
  1329. impairedProtocols: candidateImpairedProtocols,
  1330. adjustedEstablishStartTime: adjustedEstablishStartTime,
  1331. }
  1332. wasServerAffinityCandidate := isServerAffinityCandidate
  1333. // Note: there must be only one server affinity candidate, as it
  1334. // closes the serverAffinityDoneBroadcast channel.
  1335. isServerAffinityCandidate = false
  1336. // TODO: here we could generate multiple candidates from the
  1337. // server entry when there are many MeekFrontingAddresses.
  1338. candidateCount++
  1339. select {
  1340. case controller.candidateServerEntries <- candidate:
  1341. case <-controller.establishCtx.Done():
  1342. break loop
  1343. }
  1344. workTime := controller.config.clientParameters.Get().Duration(
  1345. parameters.EstablishTunnelWorkTime)
  1346. if startTime.Add(workTime).Before(monotime.Now()) {
  1347. // Start over, after a brief pause, with a new shuffle of the server
  1348. // entries, and potentially some newly fetched server entries.
  1349. break
  1350. }
  1351. if wasServerAffinityCandidate {
  1352. // Don't start the next candidate until either the server affinity
  1353. // candidate has completed (success or failure) or is still working
  1354. // and the grace period has elapsed.
  1355. gracePeriod := controller.config.clientParameters.Get().Duration(
  1356. parameters.EstablishTunnelServerAffinityGracePeriod)
  1357. if gracePeriod > 0 {
  1358. timer := time.NewTimer(gracePeriod)
  1359. select {
  1360. case <-timer.C:
  1361. case <-controller.serverAffinityDoneBroadcast:
  1362. case <-controller.establishCtx.Done():
  1363. timer.Stop()
  1364. break loop
  1365. }
  1366. timer.Stop()
  1367. }
  1368. } else {
  1369. p := controller.config.clientParameters.Get()
  1370. staggerPeriod := p.Duration(parameters.StaggerConnectionWorkersPeriod)
  1371. staggerJitter := p.Float(parameters.StaggerConnectionWorkersJitter)
  1372. p = nil
  1373. if staggerPeriod != 0 {
  1374. // Stagger concurrent connection workers.
  1375. timeout := common.JitterDuration(staggerPeriod, staggerJitter)
  1376. timer := time.NewTimer(timeout)
  1377. select {
  1378. case <-timer.C:
  1379. case <-controller.establishCtx.Done():
  1380. timer.Stop()
  1381. break loop
  1382. }
  1383. timer.Stop()
  1384. }
  1385. }
  1386. }
  1387. // Free up resources now, but don't reset until after the pause.
  1388. iterator.Close()
  1389. // Trigger a common remote server list fetch, since we may have failed
  1390. // to connect with all known servers. Don't block sending signal, since
  1391. // this signal may have already been sent.
  1392. // Don't wait for fetch remote to succeed, since it may fail and
  1393. // enter a retry loop and we're better off trying more known servers.
  1394. // TODO: synchronize the fetch response, so it can be incorporated
  1395. // into the server entry iterator as soon as available.
  1396. select {
  1397. case controller.signalFetchCommonRemoteServerList <- *new(struct{}):
  1398. default:
  1399. }
  1400. // Trigger an OSL fetch in parallel. Both fetches are run in parallel
  1401. // so that if one out of the common RLS and OSL set is large, it doesn't
  1402. // doesn't entirely block fetching the other.
  1403. select {
  1404. case controller.signalFetchObfuscatedServerLists <- *new(struct{}):
  1405. default:
  1406. }
  1407. // Trigger an out-of-band upgrade availability check and download.
  1408. // Since we may have failed to connect, we may benefit from upgrading
  1409. // to a new client version with new circumvention capabilities.
  1410. select {
  1411. case controller.signalDownloadUpgrade <- "":
  1412. default:
  1413. }
  1414. // After a complete iteration of candidate servers, pause before iterating again.
  1415. // This helps avoid some busy wait loop conditions, and also allows some time for
  1416. // network conditions to change. Also allows for fetch remote to complete,
  1417. // in typical conditions (it isn't strictly necessary to wait for this, there will
  1418. // be more rounds if required).
  1419. p := controller.config.clientParameters.Get()
  1420. timeout := common.JitterDuration(
  1421. p.Duration(parameters.EstablishTunnelPausePeriod),
  1422. p.Float(parameters.EstablishTunnelPausePeriodJitter))
  1423. p = nil
  1424. timer := time.NewTimer(timeout)
  1425. select {
  1426. case <-timer.C:
  1427. // Retry iterating
  1428. case <-controller.establishCtx.Done():
  1429. timer.Stop()
  1430. break loop
  1431. }
  1432. timer.Stop()
  1433. iterator.Reset()
  1434. }
  1435. }
  1436. // establishTunnelWorker pulls candidates from the candidate queue, establishes
  1437. // a connection to the tunnel server, and delivers the connected tunnel to a channel.
  1438. func (controller *Controller) establishTunnelWorker() {
  1439. defer controller.establishWaitGroup.Done()
  1440. loop:
  1441. for candidateServerEntry := range controller.candidateServerEntries {
  1442. // Note: don't receive from candidateServerEntries and isStopEstablishing
  1443. // in the same select, since we want to prioritize receiving the stop signal
  1444. if controller.isStopEstablishing() {
  1445. break loop
  1446. }
  1447. // There may already be a tunnel to this candidate. If so, skip it.
  1448. if controller.isActiveTunnelServerEntry(candidateServerEntry.serverEntry) {
  1449. continue
  1450. }
  1451. // ConnectTunnel will allocate significant memory, so first attempt to
  1452. // reclaim as much as possible.
  1453. defaultGarbageCollection()
  1454. // Select the tunnel protocol. The selection will be made at random from
  1455. // protocols supported by the server entry, optionally limited by
  1456. // LimitTunnelProtocols.
  1457. //
  1458. // When limiting concurrent meek connection workers, and at the limit,
  1459. // do not select meek since otherwise the candidate must be skipped.
  1460. //
  1461. // If at the limit and unabled to select a non-meek protocol, skip the
  1462. // candidate entirely and move on to the next. Since candidates are shuffled
  1463. // it's probable that the next candidate is not meek. In this case, a
  1464. // StaggerConnectionWorkersMilliseconds delay may still be incurred.
  1465. limitMeekConnectionWorkers := controller.config.clientParameters.Get().Int(
  1466. parameters.LimitMeekConnectionWorkers)
  1467. excludeMeek := false
  1468. controller.concurrentEstablishTunnelsMutex.Lock()
  1469. if limitMeekConnectionWorkers > 0 &&
  1470. controller.concurrentMeekEstablishTunnels >=
  1471. limitMeekConnectionWorkers {
  1472. excludeMeek = true
  1473. }
  1474. controller.concurrentEstablishTunnelsMutex.Unlock()
  1475. selectedProtocol, err := selectProtocol(
  1476. controller.config,
  1477. candidateServerEntry.serverEntry,
  1478. candidateServerEntry.impairedProtocols,
  1479. excludeMeek,
  1480. candidateServerEntry.usePriorityProtocol)
  1481. if err == errNoProtocolSupported {
  1482. // selectProtocol returns errNoProtocolSupported when the server
  1483. // does not support any protocol that remains after applying the
  1484. // LimitTunnelProtocols parameter, the impaired protocol filter,
  1485. // and the excludeMeek flag.
  1486. // Skip this candidate.
  1487. // Unblock other candidates immediately when
  1488. // server affinity candidate is skipped.
  1489. if candidateServerEntry.isServerAffinityCandidate {
  1490. close(controller.serverAffinityDoneBroadcast)
  1491. }
  1492. continue
  1493. }
  1494. var tunnel *Tunnel
  1495. if err == nil {
  1496. isMeek := protocol.TunnelProtocolUsesMeek(selectedProtocol)
  1497. controller.concurrentEstablishTunnelsMutex.Lock()
  1498. if isMeek {
  1499. // Recheck the limit now that we know we're selecting meek and
  1500. // adjusting concurrentMeekEstablishTunnels.
  1501. if limitMeekConnectionWorkers > 0 &&
  1502. controller.concurrentMeekEstablishTunnels >=
  1503. limitMeekConnectionWorkers {
  1504. // Skip this candidate.
  1505. controller.concurrentEstablishTunnelsMutex.Unlock()
  1506. continue
  1507. }
  1508. controller.concurrentMeekEstablishTunnels += 1
  1509. if controller.concurrentMeekEstablishTunnels > controller.peakConcurrentMeekEstablishTunnels {
  1510. controller.peakConcurrentMeekEstablishTunnels = controller.concurrentMeekEstablishTunnels
  1511. }
  1512. }
  1513. controller.concurrentEstablishTunnels += 1
  1514. if controller.concurrentEstablishTunnels > controller.peakConcurrentEstablishTunnels {
  1515. controller.peakConcurrentEstablishTunnels = controller.concurrentEstablishTunnels
  1516. }
  1517. controller.concurrentEstablishTunnelsMutex.Unlock()
  1518. tunnel, err = ConnectTunnel(
  1519. controller.establishCtx,
  1520. controller.config,
  1521. controller.sessionId,
  1522. candidateServerEntry.serverEntry,
  1523. selectedProtocol,
  1524. candidateServerEntry.adjustedEstablishStartTime)
  1525. controller.concurrentEstablishTunnelsMutex.Lock()
  1526. if isMeek {
  1527. controller.concurrentMeekEstablishTunnels -= 1
  1528. }
  1529. controller.concurrentEstablishTunnels -= 1
  1530. controller.concurrentEstablishTunnelsMutex.Unlock()
  1531. }
  1532. // Periodically emit memory metrics during the establishment cycle.
  1533. if !controller.isStopEstablishing() {
  1534. emitMemoryMetrics()
  1535. }
  1536. // Immediately reclaim memory allocated by the establishment. In the case
  1537. // of failure, first clear the reference to the tunnel. In the case of
  1538. // success, the garbage collection may still be effective as the initial
  1539. // phases of some protocols involve significant memory allocation that
  1540. // could now be reclaimed.
  1541. if err != nil {
  1542. tunnel = nil
  1543. }
  1544. defaultGarbageCollection()
  1545. if err != nil {
  1546. // Unblock other candidates immediately when
  1547. // server affinity candidate fails.
  1548. if candidateServerEntry.isServerAffinityCandidate {
  1549. close(controller.serverAffinityDoneBroadcast)
  1550. }
  1551. // Before emitting error, check if establish interrupted, in which
  1552. // case the error is noise.
  1553. if controller.isStopEstablishing() {
  1554. break loop
  1555. }
  1556. NoticeInfo("failed to connect to %s: %s", candidateServerEntry.serverEntry.IpAddress, err)
  1557. continue
  1558. }
  1559. // Deliver connected tunnel.
  1560. // Don't block. Assumes the receiver has a buffer large enough for
  1561. // the number of desired tunnels. If there's no room, the tunnel must
  1562. // not be required so it's discarded.
  1563. select {
  1564. case controller.connectedTunnels <- tunnel:
  1565. default:
  1566. controller.discardTunnel(tunnel)
  1567. // Clear the reference to this discarded tunnel and immediately run
  1568. // a garbage collection to reclaim its memory.
  1569. tunnel = nil
  1570. defaultGarbageCollection()
  1571. }
  1572. // Unblock other candidates only after delivering when
  1573. // server affinity candidate succeeds.
  1574. if candidateServerEntry.isServerAffinityCandidate {
  1575. close(controller.serverAffinityDoneBroadcast)
  1576. }
  1577. }
  1578. }
  1579. func (controller *Controller) isStopEstablishing() bool {
  1580. select {
  1581. case <-controller.establishCtx.Done():
  1582. return true
  1583. default:
  1584. }
  1585. return false
  1586. }