tunnel.go 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523
  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
  20. import (
  21. "bytes"
  22. "encoding/base64"
  23. "encoding/json"
  24. "errors"
  25. "fmt"
  26. "io"
  27. "net"
  28. "net/url"
  29. "sync"
  30. "sync/atomic"
  31. "time"
  32. "github.com/Psiphon-Inc/goarista/monotime"
  33. regen "github.com/Psiphon-Inc/goregen"
  34. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  35. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/crypto/ssh"
  36. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
  37. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/transferstats"
  38. )
  39. // Tunneler specifies the interface required by components that use a tunnel.
  40. // Components which use this interface may be serviced by a single Tunnel instance,
  41. // or a Controller which manages a pool of tunnels, or any other object which
  42. // implements Tunneler.
  43. // alwaysTunnel indicates that the connection should always be tunneled. If this
  44. // is not set, the connection may be made directly, depending on split tunnel
  45. // classification, when that feature is supported and active.
  46. // downstreamConn is an optional parameter which specifies a connection to be
  47. // explicitly closed when the Dialed connection is closed. For instance, this
  48. // is used to close downstreamConn App<->LocalProxy connections when the related
  49. // LocalProxy<->SshPortForward connections close.
  50. type Tunneler interface {
  51. Dial(remoteAddr string, alwaysTunnel bool, downstreamConn net.Conn) (conn net.Conn, err error)
  52. SignalComponentFailure()
  53. }
  54. // TunnelOwner specifies the interface required by Tunnel to notify its
  55. // owner when it has failed. The owner may, as in the case of the Controller,
  56. // remove the tunnel from its list of active tunnels.
  57. type TunnelOwner interface {
  58. SignalSeededNewSLOK()
  59. SignalTunnelFailure(tunnel *Tunnel)
  60. }
  61. // Tunnel is a connection to a Psiphon server. An established
  62. // tunnel includes a network connection to the specified server
  63. // and an SSH session built on top of that transport.
  64. type Tunnel struct {
  65. mutex *sync.Mutex
  66. config *Config
  67. untunneledDialConfig *DialConfig
  68. isActivated bool
  69. isDiscarded bool
  70. isClosed bool
  71. sessionId string
  72. serverEntry *protocol.ServerEntry
  73. serverContext *ServerContext
  74. protocol string
  75. conn *common.ActivityMonitoredConn
  76. sshClient *ssh.Client
  77. sshServerRequests <-chan *ssh.Request
  78. operateWaitGroup *sync.WaitGroup
  79. shutdownOperateBroadcast chan struct{}
  80. signalPortForwardFailure chan struct{}
  81. totalPortForwardFailures int
  82. adjustedEstablishStartTime monotime.Time
  83. establishDuration time.Duration
  84. establishedTime monotime.Time
  85. dialStats *TunnelDialStats
  86. newClientVerificationPayload chan string
  87. }
  88. // TunnelDialStats records additional dial config that is sent to the server for stats
  89. // recording. This data is used to analyze which configuration settings are successful
  90. // in various circumvention contexts, and includes meek dial params and upstream proxy
  91. // params.
  92. // For upstream proxy, only proxy type and custom header names are recorded; proxy
  93. // address and custom header values are considered PII.
  94. type TunnelDialStats struct {
  95. SelectedSSHClientVersion bool
  96. SSHClientVersion string
  97. UpstreamProxyType string
  98. UpstreamProxyCustomHeaderNames []string
  99. MeekDialAddress string
  100. MeekResolvedIPAddress string
  101. MeekSNIServerName string
  102. MeekHostHeader string
  103. MeekTransformedHostName bool
  104. SelectedUserAgent bool
  105. UserAgent string
  106. SelectedTLSProfile bool
  107. TLSProfile string
  108. }
  109. // ConnectTunnel first makes a network transport connection to the
  110. // Psiphon server and then establishes an SSH client session on top of
  111. // that transport. The SSH server is authenticated using the public
  112. // key in the server entry.
  113. // Depending on the server's capabilities, the connection may use
  114. // plain SSH over TCP, obfuscated SSH over TCP, or obfuscated SSH over
  115. // HTTP (meek protocol).
  116. // When requiredProtocol is not blank, that protocol is used. Otherwise,
  117. // the a random supported protocol is used.
  118. // untunneledDialConfig is used for untunneled final status requests.
  119. //
  120. // Call Activate on a connected tunnel to complete its establishment
  121. // before using.
  122. //
  123. // Tunnel establishment is split into two phases: connection, and
  124. // activation. The Controller will run many ConnectTunnel calls
  125. // concurrently and then, to avoid unnecessary overhead from making
  126. // handshake requests and starting operateTunnel from tunnels which
  127. // may be discarded, call Activate on connected tunnels sequentially
  128. // as necessary.
  129. //
  130. func ConnectTunnel(
  131. config *Config,
  132. untunneledDialConfig *DialConfig,
  133. sessionId string,
  134. pendingConns *common.Conns,
  135. serverEntry *protocol.ServerEntry,
  136. selectedProtocol string,
  137. adjustedEstablishStartTime monotime.Time) (*Tunnel, error) {
  138. if !serverEntry.SupportsProtocol(selectedProtocol) {
  139. return nil, common.ContextError(fmt.Errorf("server does not support selected protocol"))
  140. }
  141. // Build transport layers and establish SSH connection. Note that
  142. // dialConn and monitoredConn are the same network connection.
  143. dialResult, err := dialSsh(
  144. config, pendingConns, serverEntry, selectedProtocol, sessionId)
  145. if err != nil {
  146. return nil, common.ContextError(err)
  147. }
  148. // Now that connection dials are complete, cancel interruptibility
  149. pendingConns.Remove(dialResult.dialConn)
  150. // The tunnel is now connected
  151. return &Tunnel{
  152. mutex: new(sync.Mutex),
  153. config: config,
  154. untunneledDialConfig: untunneledDialConfig,
  155. sessionId: sessionId,
  156. serverEntry: serverEntry,
  157. protocol: selectedProtocol,
  158. conn: dialResult.monitoredConn,
  159. sshClient: dialResult.sshClient,
  160. sshServerRequests: dialResult.sshRequests,
  161. operateWaitGroup: new(sync.WaitGroup),
  162. shutdownOperateBroadcast: make(chan struct{}),
  163. // A buffer allows at least one signal to be sent even when the receiver is
  164. // not listening. Senders should not block.
  165. signalPortForwardFailure: make(chan struct{}, 1),
  166. adjustedEstablishStartTime: adjustedEstablishStartTime,
  167. dialStats: dialResult.dialStats,
  168. // Buffer allows SetClientVerificationPayload to submit one new payload
  169. // without blocking or dropping it.
  170. newClientVerificationPayload: make(chan string, 1),
  171. }, nil
  172. }
  173. // Activate completes the tunnel establishment, performing the handshake
  174. // request and starting operateTunnel, the worker that monitors the tunnel
  175. // and handles periodic management.
  176. func (tunnel *Tunnel) Activate(
  177. tunnelOwner TunnelOwner,
  178. shutdownBroadcast chan struct{}) error {
  179. // Create a new Psiphon API server context for this tunnel. This includes
  180. // performing a handshake request. If the handshake fails, this activation
  181. // fails.
  182. var serverContext *ServerContext
  183. if !tunnel.config.DisableApi {
  184. NoticeInfo("starting server context for %s", tunnel.serverEntry.IpAddress)
  185. // Call NewServerContext in a goroutine, as it blocks on a network operation,
  186. // the handshake request, and would block shutdown. If the shutdown signal is
  187. // received, close the tunnel, which will interrupt the handshake request
  188. // that may be blocking NewServerContext.
  189. //
  190. // Timeout after PsiphonApiServerTimeoutSeconds. NewServerContext may not
  191. // return if the tunnel network connection is unstable during the handshake
  192. // request. At this point, there is no operateTunnel monitor that will detect
  193. // this condition with SSH keep alives.
  194. type newServerContextResult struct {
  195. serverContext *ServerContext
  196. err error
  197. }
  198. resultChannel := make(chan newServerContextResult)
  199. go func() {
  200. serverContext, err := NewServerContext(tunnel)
  201. resultChannel <- newServerContextResult{
  202. serverContext: serverContext,
  203. err: err,
  204. }
  205. }()
  206. var result newServerContextResult
  207. if *tunnel.config.PsiphonApiServerTimeoutSeconds > 0 {
  208. timer := time.NewTimer(
  209. time.Second *
  210. time.Duration(
  211. *tunnel.config.PsiphonApiServerTimeoutSeconds))
  212. select {
  213. case result = <-resultChannel:
  214. case <-timer.C:
  215. result.err = errors.New("timed out")
  216. // Interrupt the Activate goroutine and await its completion.
  217. tunnel.Close(true)
  218. <-resultChannel
  219. case <-shutdownBroadcast:
  220. result.err = errors.New("shutdown")
  221. tunnel.Close(true)
  222. <-resultChannel
  223. }
  224. } else {
  225. select {
  226. case result = <-resultChannel:
  227. case <-shutdownBroadcast:
  228. result.err = errors.New("shutdown")
  229. tunnel.Close(true)
  230. <-resultChannel
  231. }
  232. }
  233. if result.err != nil {
  234. return common.ContextError(
  235. fmt.Errorf("error starting server context for %s: %s",
  236. tunnel.serverEntry.IpAddress, result.err))
  237. }
  238. serverContext = result.serverContext
  239. }
  240. tunnel.mutex.Lock()
  241. // It may happen that the tunnel gets closed while Activate is running.
  242. // In this case, abort here, to ensure that the operateTunnel goroutine
  243. // will not be launched after Close is called.
  244. if tunnel.isClosed {
  245. return common.ContextError(errors.New("tunnel is closed"))
  246. }
  247. tunnel.isActivated = true
  248. tunnel.serverContext = serverContext
  249. // establishDuration is the elapsed time between the controller starting tunnel
  250. // establishment and this tunnel being established. The reported value represents
  251. // how long the user waited between starting the client and having a usable tunnel;
  252. // or how long between the client detecting an unexpected tunnel disconnect and
  253. // completing automatic reestablishment.
  254. //
  255. // This time period may include time spent unsuccessfully connecting to other
  256. // servers. Time spent waiting for network connectivity is excluded.
  257. tunnel.establishDuration = monotime.Since(tunnel.adjustedEstablishStartTime)
  258. tunnel.establishedTime = monotime.Now()
  259. // Spawn the operateTunnel goroutine, which monitors the tunnel and handles periodic
  260. // stats updates.
  261. tunnel.operateWaitGroup.Add(1)
  262. go tunnel.operateTunnel(tunnelOwner)
  263. tunnel.mutex.Unlock()
  264. return nil
  265. }
  266. // Close stops operating the tunnel and closes the underlying connection.
  267. // Supports multiple and/or concurrent calls to Close().
  268. // When isDiscarded is set, operateTunnel will not attempt to send final
  269. // status requests.
  270. func (tunnel *Tunnel) Close(isDiscarded bool) {
  271. tunnel.mutex.Lock()
  272. tunnel.isDiscarded = isDiscarded
  273. isActivated := tunnel.isActivated
  274. isClosed := tunnel.isClosed
  275. tunnel.isClosed = true
  276. tunnel.mutex.Unlock()
  277. if !isClosed {
  278. // Signal operateTunnel to stop before closing the tunnel -- this
  279. // allows a final status request to be made in the case of an orderly
  280. // shutdown.
  281. // A timer is set, so if operateTunnel takes too long to stop, the
  282. // tunnel is closed, which will interrupt any slow final status request.
  283. // In effect, the TUNNEL_OPERATE_SHUTDOWN_TIMEOUT value will take
  284. // precedence over the PSIPHON_API_SERVER_TIMEOUT http.Client.Timeout
  285. // value set in makePsiphonHttpsClient.
  286. if isActivated {
  287. timer := time.AfterFunc(TUNNEL_OPERATE_SHUTDOWN_TIMEOUT, func() { tunnel.conn.Close() })
  288. close(tunnel.shutdownOperateBroadcast)
  289. tunnel.operateWaitGroup.Wait()
  290. timer.Stop()
  291. }
  292. tunnel.sshClient.Close()
  293. // tunnel.conn.Close() may get called multiple times, which is allowed.
  294. tunnel.conn.Close()
  295. err := tunnel.sshClient.Wait()
  296. if err != nil {
  297. NoticeAlert("close tunnel ssh error: %s", err)
  298. }
  299. }
  300. }
  301. // IsActivated returns the tunnel's activated flag.
  302. func (tunnel *Tunnel) IsActivated() bool {
  303. tunnel.mutex.Lock()
  304. defer tunnel.mutex.Unlock()
  305. return tunnel.isActivated
  306. }
  307. // IsDiscarded returns the tunnel's discarded flag.
  308. func (tunnel *Tunnel) IsDiscarded() bool {
  309. tunnel.mutex.Lock()
  310. defer tunnel.mutex.Unlock()
  311. return tunnel.isDiscarded
  312. }
  313. // SendAPIRequest sends an API request as an SSH request through the tunnel.
  314. // This function blocks awaiting a response. Only one request may be in-flight
  315. // at once; a concurrent SendAPIRequest will block until an active request
  316. // receives its response (or the SSH connection is terminated).
  317. func (tunnel *Tunnel) SendAPIRequest(
  318. name string, requestPayload []byte) ([]byte, error) {
  319. ok, responsePayload, err := tunnel.sshClient.Conn.SendRequest(
  320. name, true, requestPayload)
  321. if err != nil {
  322. return nil, common.ContextError(err)
  323. }
  324. if !ok {
  325. return nil, common.ContextError(errors.New("API request rejected"))
  326. }
  327. return responsePayload, nil
  328. }
  329. // Dial establishes a port forward connection through the tunnel
  330. // This Dial doesn't support split tunnel, so alwaysTunnel is not referenced
  331. func (tunnel *Tunnel) Dial(
  332. remoteAddr string, alwaysTunnel bool, downstreamConn net.Conn) (conn net.Conn, err error) {
  333. if !tunnel.IsActivated() {
  334. return nil, common.ContextError(errors.New("tunnel is not activated"))
  335. }
  336. type tunnelDialResult struct {
  337. sshPortForwardConn net.Conn
  338. err error
  339. }
  340. resultChannel := make(chan *tunnelDialResult, 2)
  341. if *tunnel.config.TunnelPortForwardDialTimeoutSeconds > 0 {
  342. time.AfterFunc(time.Duration(*tunnel.config.TunnelPortForwardDialTimeoutSeconds)*time.Second, func() {
  343. resultChannel <- &tunnelDialResult{nil, errors.New("tunnel dial timeout")}
  344. })
  345. }
  346. go func() {
  347. sshPortForwardConn, err := tunnel.sshClient.Dial("tcp", remoteAddr)
  348. resultChannel <- &tunnelDialResult{sshPortForwardConn, err}
  349. }()
  350. result := <-resultChannel
  351. if result.err != nil {
  352. // TODO: conditional on type of error or error message?
  353. select {
  354. case tunnel.signalPortForwardFailure <- *new(struct{}):
  355. default:
  356. }
  357. return nil, common.ContextError(result.err)
  358. }
  359. conn = &TunneledConn{
  360. Conn: result.sshPortForwardConn,
  361. tunnel: tunnel,
  362. downstreamConn: downstreamConn}
  363. return tunnel.wrapWithTransferStats(conn), nil
  364. }
  365. func (tunnel *Tunnel) DialPacketTunnelChannel() (net.Conn, error) {
  366. if !tunnel.IsActivated() {
  367. return nil, common.ContextError(errors.New("tunnel is not activated"))
  368. }
  369. channel, requests, err := tunnel.sshClient.OpenChannel(
  370. protocol.PACKET_TUNNEL_CHANNEL_TYPE, nil)
  371. if err != nil {
  372. // TODO: conditional on type of error or error message?
  373. select {
  374. case tunnel.signalPortForwardFailure <- *new(struct{}):
  375. default:
  376. }
  377. return nil, common.ContextError(err)
  378. }
  379. go ssh.DiscardRequests(requests)
  380. conn := newChannelConn(channel)
  381. // wrapWithTransferStats will track bytes transferred for the
  382. // packet tunnel. It will count packet overhead (TCP/UDP/IP headers).
  383. //
  384. // Since the data in the channel is not HTTP or TLS, no domain bytes
  385. // counting is expected.
  386. //
  387. // transferstats are also used to determine that there's been recent
  388. // activity and skip periodic SSH keep alives; see Tunnel.operateTunnel.
  389. return tunnel.wrapWithTransferStats(conn), nil
  390. }
  391. func (tunnel *Tunnel) wrapWithTransferStats(conn net.Conn) net.Conn {
  392. // Tunnel does not have a serverContext when DisableApi is set. We still use
  393. // transferstats.Conn to count bytes transferred for monitoring tunnel
  394. // quality.
  395. var regexps *transferstats.Regexps
  396. if tunnel.serverContext != nil {
  397. regexps = tunnel.serverContext.StatsRegexps()
  398. }
  399. return transferstats.NewConn(conn, tunnel.serverEntry.IpAddress, regexps)
  400. }
  401. // SignalComponentFailure notifies the tunnel that an associated component has failed.
  402. // This will terminate the tunnel.
  403. func (tunnel *Tunnel) SignalComponentFailure() {
  404. NoticeAlert("tunnel received component failure signal")
  405. tunnel.Close(false)
  406. }
  407. // SetClientVerificationPayload triggers a client verification request, for this
  408. // tunnel, with the specified verifiction payload. If the tunnel is not yet established,
  409. // the payload/request is enqueued. If a payload/request is already eneueued, the
  410. // new payload is dropped.
  411. func (tunnel *Tunnel) SetClientVerificationPayload(clientVerificationPayload string) {
  412. select {
  413. case tunnel.newClientVerificationPayload <- clientVerificationPayload:
  414. default:
  415. }
  416. }
  417. // TunneledConn implements net.Conn and wraps a port forward connection.
  418. // It is used to hook into Read and Write to observe I/O errors and
  419. // report these errors back to the tunnel monitor as port forward failures.
  420. // TunneledConn optionally tracks a peer connection to be explicitly closed
  421. // when the TunneledConn is closed.
  422. type TunneledConn struct {
  423. net.Conn
  424. tunnel *Tunnel
  425. downstreamConn net.Conn
  426. }
  427. func (conn *TunneledConn) Read(buffer []byte) (n int, err error) {
  428. n, err = conn.Conn.Read(buffer)
  429. if err != nil && err != io.EOF {
  430. // Report new failure. Won't block; assumes the receiver
  431. // has a sufficient buffer for the threshold number of reports.
  432. // TODO: conditional on type of error or error message?
  433. select {
  434. case conn.tunnel.signalPortForwardFailure <- *new(struct{}):
  435. default:
  436. }
  437. }
  438. return
  439. }
  440. func (conn *TunneledConn) Write(buffer []byte) (n int, err error) {
  441. n, err = conn.Conn.Write(buffer)
  442. if err != nil && err != io.EOF {
  443. // Same as TunneledConn.Read()
  444. select {
  445. case conn.tunnel.signalPortForwardFailure <- *new(struct{}):
  446. default:
  447. }
  448. }
  449. return
  450. }
  451. func (conn *TunneledConn) Close() error {
  452. if conn.downstreamConn != nil {
  453. conn.downstreamConn.Close()
  454. }
  455. return conn.Conn.Close()
  456. }
  457. var errProtocolNotSupported = errors.New("server does not support required protocol(s)")
  458. // selectProtocol is a helper that picks the tunnel protocol
  459. func selectProtocol(
  460. config *Config,
  461. serverEntry *protocol.ServerEntry,
  462. excludeMeek bool) (selectedProtocol string, err error) {
  463. // TODO: properly handle protocols (e.g. FRONTED-MEEK-OSSH) vs. capabilities (e.g., {FRONTED-MEEK, OSSH})
  464. // for now, the code is simply assuming that MEEK capabilities imply OSSH capability.
  465. if config.TunnelProtocol != "" {
  466. if !serverEntry.SupportsProtocol(config.TunnelProtocol) ||
  467. (excludeMeek && protocol.TunnelProtocolUsesMeek(config.TunnelProtocol)) {
  468. return "", errProtocolNotSupported
  469. }
  470. selectedProtocol = config.TunnelProtocol
  471. } else {
  472. // Pick at random from the supported protocols. This ensures that we'll eventually
  473. // try all possible protocols. Depending on network configuration, it may be the
  474. // case that some protocol is only available through multi-capability servers,
  475. // and a simpler ranked preference of protocols could lead to that protocol never
  476. // being selected.
  477. candidateProtocols := serverEntry.GetSupportedProtocols(excludeMeek)
  478. if len(candidateProtocols) == 0 {
  479. return "", errProtocolNotSupported
  480. }
  481. index, err := common.MakeSecureRandomInt(len(candidateProtocols))
  482. if err != nil {
  483. return "", common.ContextError(err)
  484. }
  485. selectedProtocol = candidateProtocols[index]
  486. }
  487. return selectedProtocol, nil
  488. }
  489. // selectFrontingParameters is a helper which selects/generates meek fronting
  490. // parameters where the server entry provides multiple options or patterns.
  491. func selectFrontingParameters(
  492. serverEntry *protocol.ServerEntry) (frontingAddress, frontingHost string, err error) {
  493. if len(serverEntry.MeekFrontingAddressesRegex) > 0 {
  494. // Generate a front address based on the regex.
  495. frontingAddress, err = regen.Generate(serverEntry.MeekFrontingAddressesRegex)
  496. if err != nil {
  497. return "", "", common.ContextError(err)
  498. }
  499. } else {
  500. // Randomly select, for this connection attempt, one front address for
  501. // fronting-capable servers.
  502. if len(serverEntry.MeekFrontingAddresses) == 0 {
  503. return "", "", common.ContextError(errors.New("MeekFrontingAddresses is empty"))
  504. }
  505. index, err := common.MakeSecureRandomInt(len(serverEntry.MeekFrontingAddresses))
  506. if err != nil {
  507. return "", "", common.ContextError(err)
  508. }
  509. frontingAddress = serverEntry.MeekFrontingAddresses[index]
  510. }
  511. if len(serverEntry.MeekFrontingHosts) > 0 {
  512. index, err := common.MakeSecureRandomInt(len(serverEntry.MeekFrontingHosts))
  513. if err != nil {
  514. return "", "", common.ContextError(err)
  515. }
  516. frontingHost = serverEntry.MeekFrontingHosts[index]
  517. } else {
  518. // Backwards compatibility case
  519. frontingHost = serverEntry.MeekFrontingHost
  520. }
  521. return
  522. }
  523. func doMeekTransformHostName(config *Config) bool {
  524. switch config.TransformHostNames {
  525. case TRANSFORM_HOST_NAMES_ALWAYS:
  526. return true
  527. case TRANSFORM_HOST_NAMES_NEVER:
  528. return false
  529. }
  530. return common.FlipCoin()
  531. }
  532. // initMeekConfig is a helper that creates a MeekConfig suitable for the
  533. // selected meek tunnel protocol.
  534. func initMeekConfig(
  535. config *Config,
  536. serverEntry *protocol.ServerEntry,
  537. selectedProtocol,
  538. sessionId string) (*MeekConfig, error) {
  539. // The meek protocol always uses OSSH
  540. psiphonServerAddress := fmt.Sprintf("%s:%d", serverEntry.IpAddress, serverEntry.SshObfuscatedPort)
  541. var dialAddress string
  542. useHTTPS := false
  543. useObfuscatedSessionTickets := false
  544. var SNIServerName, hostHeader string
  545. transformedHostName := false
  546. switch selectedProtocol {
  547. case protocol.TUNNEL_PROTOCOL_FRONTED_MEEK:
  548. frontingAddress, frontingHost, err := selectFrontingParameters(serverEntry)
  549. if err != nil {
  550. return nil, common.ContextError(err)
  551. }
  552. dialAddress = fmt.Sprintf("%s:443", frontingAddress)
  553. useHTTPS = true
  554. if !serverEntry.MeekFrontingDisableSNI {
  555. SNIServerName = frontingAddress
  556. if doMeekTransformHostName(config) {
  557. SNIServerName = common.GenerateHostName()
  558. transformedHostName = true
  559. }
  560. }
  561. hostHeader = frontingHost
  562. case protocol.TUNNEL_PROTOCOL_FRONTED_MEEK_HTTP:
  563. frontingAddress, frontingHost, err := selectFrontingParameters(serverEntry)
  564. if err != nil {
  565. return nil, common.ContextError(err)
  566. }
  567. dialAddress = fmt.Sprintf("%s:80", frontingAddress)
  568. hostHeader = frontingHost
  569. case protocol.TUNNEL_PROTOCOL_UNFRONTED_MEEK:
  570. dialAddress = fmt.Sprintf("%s:%d", serverEntry.IpAddress, serverEntry.MeekServerPort)
  571. hostname := serverEntry.IpAddress
  572. if doMeekTransformHostName(config) {
  573. hostname = common.GenerateHostName()
  574. transformedHostName = true
  575. }
  576. if serverEntry.MeekServerPort == 80 {
  577. hostHeader = hostname
  578. } else {
  579. hostHeader = fmt.Sprintf("%s:%d", hostname, serverEntry.MeekServerPort)
  580. }
  581. case protocol.TUNNEL_PROTOCOL_UNFRONTED_MEEK_HTTPS,
  582. protocol.TUNNEL_PROTOCOL_UNFRONTED_MEEK_SESSION_TICKET:
  583. dialAddress = fmt.Sprintf("%s:%d", serverEntry.IpAddress, serverEntry.MeekServerPort)
  584. useHTTPS = true
  585. if selectedProtocol == protocol.TUNNEL_PROTOCOL_UNFRONTED_MEEK_SESSION_TICKET {
  586. useObfuscatedSessionTickets = true
  587. }
  588. SNIServerName = serverEntry.IpAddress
  589. if doMeekTransformHostName(config) {
  590. SNIServerName = common.GenerateHostName()
  591. transformedHostName = true
  592. }
  593. if serverEntry.MeekServerPort == 443 {
  594. hostHeader = serverEntry.IpAddress
  595. } else {
  596. hostHeader = fmt.Sprintf("%s:%d", serverEntry.IpAddress, serverEntry.MeekServerPort)
  597. }
  598. default:
  599. return nil, common.ContextError(errors.New("unexpected selectedProtocol"))
  600. }
  601. // The underlying TLS will automatically disable SNI for IP address server name
  602. // values; we have this explicit check here so we record the correct value for stats.
  603. if net.ParseIP(SNIServerName) != nil {
  604. SNIServerName = ""
  605. }
  606. // Pin the TLS profile for the entire meek connection.
  607. selectedTLSProfile := SelectTLSProfile(
  608. config.UseIndistinguishableTLS,
  609. useObfuscatedSessionTickets,
  610. true,
  611. config.TrustedCACertificatesFilename != "")
  612. return &MeekConfig{
  613. LimitBufferSizes: config.LimitMeekBufferSizes,
  614. DialAddress: dialAddress,
  615. UseHTTPS: useHTTPS,
  616. TLSProfile: selectedTLSProfile,
  617. UseObfuscatedSessionTickets: useObfuscatedSessionTickets,
  618. SNIServerName: SNIServerName,
  619. HostHeader: hostHeader,
  620. TransformedHostName: transformedHostName,
  621. PsiphonServerAddress: psiphonServerAddress,
  622. SessionID: sessionId,
  623. ClientTunnelProtocol: selectedProtocol,
  624. MeekCookieEncryptionPublicKey: serverEntry.MeekCookieEncryptionPublicKey,
  625. MeekObfuscatedKey: serverEntry.MeekObfuscatedKey,
  626. }, nil
  627. }
  628. type dialResult struct {
  629. dialConn net.Conn
  630. monitoredConn *common.ActivityMonitoredConn
  631. sshClient *ssh.Client
  632. sshRequests <-chan *ssh.Request
  633. dialStats *TunnelDialStats
  634. }
  635. // dialSsh is a helper that builds the transport layers and establishes the SSH connection.
  636. // When additional dial configuration is used, DialStats are recorded and returned.
  637. //
  638. // The net.Conn return value is the value to be removed from pendingConns; additional
  639. // layering (ThrottledConn, ActivityMonitoredConn) is applied, but this return value is the
  640. // base dial conn. The *ActivityMonitoredConn return value is the layered conn passed into
  641. // the ssh.Client.
  642. func dialSsh(
  643. config *Config,
  644. pendingConns *common.Conns,
  645. serverEntry *protocol.ServerEntry,
  646. selectedProtocol,
  647. sessionId string) (*dialResult, error) {
  648. // The meek protocols tunnel obfuscated SSH. Obfuscated SSH is layered on top of SSH.
  649. // So depending on which protocol is used, multiple layers are initialized.
  650. // Note: when SSHClientVersion is "", a default is supplied by the ssh package:
  651. // https://godoc.org/golang.org/x/crypto/ssh#ClientConfig
  652. var selectedSSHClientVersion bool
  653. SSHClientVersion := ""
  654. useObfuscatedSsh := false
  655. var directTCPDialAddress string
  656. var meekConfig *MeekConfig
  657. var err error
  658. switch selectedProtocol {
  659. case protocol.TUNNEL_PROTOCOL_OBFUSCATED_SSH:
  660. useObfuscatedSsh = true
  661. directTCPDialAddress = fmt.Sprintf("%s:%d", serverEntry.IpAddress, serverEntry.SshObfuscatedPort)
  662. case protocol.TUNNEL_PROTOCOL_SSH:
  663. selectedSSHClientVersion = true
  664. SSHClientVersion = pickSSHClientVersion()
  665. directTCPDialAddress = fmt.Sprintf("%s:%d", serverEntry.IpAddress, serverEntry.SshPort)
  666. default:
  667. useObfuscatedSsh = true
  668. meekConfig, err = initMeekConfig(config, serverEntry, selectedProtocol, sessionId)
  669. if err != nil {
  670. return nil, common.ContextError(err)
  671. }
  672. }
  673. // Set User Agent when using meek or an upstream HTTP proxy
  674. var selectedUserAgent bool
  675. dialCustomHeaders := config.CustomHeaders
  676. var upstreamProxyType string
  677. if config.UpstreamProxyUrl != "" {
  678. // Note: UpstreamProxyUrl will be validated in the dial
  679. proxyURL, err := url.Parse(config.UpstreamProxyUrl)
  680. if err == nil {
  681. upstreamProxyType = proxyURL.Scheme
  682. }
  683. }
  684. if meekConfig != nil || upstreamProxyType == "http" {
  685. dialCustomHeaders, selectedUserAgent = UserAgentIfUnset(dialCustomHeaders)
  686. }
  687. // Use an asynchronous callback to record the resolved IP address when
  688. // dialing a domain name. Note that DialMeek doesn't immediately
  689. // establish any HTTPS connections, so the resolved IP address won't be
  690. // reported until during/after ssh session establishment (the ssh traffic
  691. // is meek payload). So don't Load() the IP address value until after that
  692. // has completed to ensure a result.
  693. var resolvedIPAddress atomic.Value
  694. resolvedIPAddress.Store("")
  695. setResolvedIPAddress := func(IPAddress string) {
  696. resolvedIPAddress.Store(IPAddress)
  697. }
  698. dialConfig := &DialConfig{
  699. UpstreamProxyUrl: config.UpstreamProxyUrl,
  700. CustomHeaders: dialCustomHeaders,
  701. ConnectTimeout: time.Duration(*config.TunnelConnectTimeoutSeconds) * time.Second,
  702. PendingConns: pendingConns,
  703. DeviceBinder: config.DeviceBinder,
  704. DnsServerGetter: config.DnsServerGetter,
  705. IPv6Synthesizer: config.IPv6Synthesizer,
  706. UseIndistinguishableTLS: config.UseIndistinguishableTLS,
  707. TrustedCACertificatesFilename: config.TrustedCACertificatesFilename,
  708. DeviceRegion: config.DeviceRegion,
  709. ResolvedIPCallback: setResolvedIPAddress,
  710. }
  711. // Gather dial parameters for diagnostic logging and stats reporting
  712. dialStats := &TunnelDialStats{}
  713. if selectedSSHClientVersion {
  714. dialStats.SelectedSSHClientVersion = true
  715. dialStats.SSHClientVersion = SSHClientVersion
  716. }
  717. if selectedUserAgent {
  718. dialStats.SelectedUserAgent = true
  719. dialStats.UserAgent = dialConfig.CustomHeaders.Get("User-Agent")
  720. }
  721. if upstreamProxyType != "" {
  722. dialStats.UpstreamProxyType = upstreamProxyType
  723. }
  724. if len(dialConfig.CustomHeaders) > 0 {
  725. dialStats.UpstreamProxyCustomHeaderNames = make([]string, 0)
  726. for name := range dialConfig.CustomHeaders {
  727. if selectedUserAgent && name == "User-Agent" {
  728. continue
  729. }
  730. dialStats.UpstreamProxyCustomHeaderNames = append(dialStats.UpstreamProxyCustomHeaderNames, name)
  731. }
  732. }
  733. if meekConfig != nil {
  734. // Note: dialStats.MeekResolvedIPAddress isn't set until the dial begins,
  735. // so it will always be blank in NoticeConnectingServer.
  736. dialStats.MeekDialAddress = meekConfig.DialAddress
  737. dialStats.MeekResolvedIPAddress = ""
  738. dialStats.MeekSNIServerName = meekConfig.SNIServerName
  739. dialStats.MeekHostHeader = meekConfig.HostHeader
  740. dialStats.MeekTransformedHostName = meekConfig.TransformedHostName
  741. dialStats.SelectedTLSProfile = true
  742. dialStats.TLSProfile = meekConfig.TLSProfile
  743. }
  744. NoticeConnectingServer(
  745. serverEntry.IpAddress,
  746. serverEntry.Region,
  747. selectedProtocol,
  748. dialStats)
  749. // Create the base transport: meek or direct connection
  750. var dialConn net.Conn
  751. if meekConfig != nil {
  752. dialConn, err = DialMeek(meekConfig, dialConfig)
  753. if err != nil {
  754. return nil, common.ContextError(err)
  755. }
  756. } else {
  757. // For some direct connect servers, DialPluginProtocol
  758. // will layer on another obfuscation protocol.
  759. // Use a copy of DialConfig without pendingConns; the
  760. // DialPluginProtocol must supply and manage its own
  761. // for its base network connections.
  762. pluginDialConfig := new(DialConfig)
  763. *pluginDialConfig = *dialConfig
  764. pluginDialConfig.PendingConns = nil
  765. var dialedPlugin bool
  766. dialedPlugin, dialConn, err = DialPluginProtocol(
  767. config,
  768. NewNoticeWriter("DialPluginProtocol"),
  769. pendingConns,
  770. directTCPDialAddress,
  771. dialConfig)
  772. if !dialedPlugin && err != nil {
  773. NoticeInfo("DialPluginProtocol intialization failed: %s", err)
  774. }
  775. if dialedPlugin {
  776. NoticeInfo("using DialPluginProtocol for %s", serverEntry.IpAddress)
  777. } else {
  778. // Standard direct connection.
  779. dialConn, err = DialTCP(directTCPDialAddress, dialConfig)
  780. }
  781. if err != nil {
  782. return nil, common.ContextError(err)
  783. }
  784. }
  785. // If dialConn is not a Closer, tunnel failure detection may be slower
  786. _, ok := dialConn.(common.Closer)
  787. if !ok {
  788. NoticeAlert("tunnel.dialSsh: dialConn is not a Closer")
  789. }
  790. cleanupConn := dialConn
  791. defer func() {
  792. // Cleanup on error
  793. if cleanupConn != nil {
  794. cleanupConn.Close()
  795. pendingConns.Remove(cleanupConn)
  796. }
  797. }()
  798. // Activity monitoring is used to measure tunnel duration
  799. monitoredConn, err := common.NewActivityMonitoredConn(dialConn, 0, false, nil, nil)
  800. if err != nil {
  801. return nil, common.ContextError(err)
  802. }
  803. // Apply throttling (if configured)
  804. throttledConn := common.NewThrottledConn(monitoredConn, config.RateLimits)
  805. // Add obfuscated SSH layer
  806. var sshConn net.Conn = throttledConn
  807. if useObfuscatedSsh {
  808. sshConn, err = common.NewObfuscatedSshConn(
  809. common.OBFUSCATION_CONN_MODE_CLIENT, throttledConn, serverEntry.SshObfuscatedKey)
  810. if err != nil {
  811. return nil, common.ContextError(err)
  812. }
  813. }
  814. // Now establish the SSH session over the conn transport
  815. expectedPublicKey, err := base64.StdEncoding.DecodeString(serverEntry.SshHostKey)
  816. if err != nil {
  817. return nil, common.ContextError(err)
  818. }
  819. sshCertChecker := &ssh.CertChecker{
  820. HostKeyFallback: func(addr string, remote net.Addr, publicKey ssh.PublicKey) error {
  821. if !bytes.Equal(expectedPublicKey, publicKey.Marshal()) {
  822. return common.ContextError(errors.New("unexpected host public key"))
  823. }
  824. return nil
  825. },
  826. }
  827. sshPasswordPayload := &protocol.SSHPasswordPayload{
  828. SessionId: sessionId,
  829. SshPassword: serverEntry.SshPassword,
  830. ClientCapabilities: []string{protocol.CLIENT_CAPABILITY_SERVER_REQUESTS},
  831. }
  832. payload, err := json.Marshal(sshPasswordPayload)
  833. if err != nil {
  834. return nil, common.ContextError(err)
  835. }
  836. sshClientConfig := &ssh.ClientConfig{
  837. User: serverEntry.SshUsername,
  838. Auth: []ssh.AuthMethod{
  839. ssh.Password(string(payload)),
  840. },
  841. HostKeyCallback: sshCertChecker.CheckHostKey,
  842. ClientVersion: SSHClientVersion,
  843. }
  844. // The ssh session establishment (via ssh.NewClientConn) is wrapped
  845. // in a timeout to ensure it won't hang. We've encountered firewalls
  846. // that allow the TCP handshake to complete but then send a RST to the
  847. // server-side and nothing to the client-side, and if that happens
  848. // while ssh.NewClientConn is reading, it may wait forever. The timeout
  849. // closes the conn, which interrupts it.
  850. // Note: TCP handshake timeouts are provided by TCPConn, and session
  851. // timeouts *after* ssh establishment are provided by the ssh keep alive
  852. // in operate tunnel.
  853. // TODO: adjust the timeout to account for time-elapsed-from-start
  854. type sshNewClientResult struct {
  855. sshClient *ssh.Client
  856. sshRequests <-chan *ssh.Request
  857. err error
  858. }
  859. resultChannel := make(chan *sshNewClientResult, 2)
  860. if *config.TunnelConnectTimeoutSeconds > 0 {
  861. time.AfterFunc(time.Duration(*config.TunnelConnectTimeoutSeconds)*time.Second, func() {
  862. resultChannel <- &sshNewClientResult{nil, nil, errors.New("ssh dial timeout")}
  863. })
  864. }
  865. go func() {
  866. // The following is adapted from ssh.Dial(), here using a custom conn
  867. // The sshAddress is passed through to host key verification callbacks; we don't use it.
  868. sshAddress := ""
  869. sshClientConn, sshChannels, sshRequests, err := ssh.NewClientConn(
  870. sshConn, sshAddress, sshClientConfig)
  871. var sshClient *ssh.Client
  872. if err == nil {
  873. // sshRequests is handled by operateTunnel.
  874. // ssh.NewClient also expects to handle the sshRequests
  875. // value from ssh.NewClientConn and will spawn a goroutine
  876. // to handle the <-chan *ssh.Request, so we must provide
  877. // a closed channel to ensure that goroutine halts instead
  878. // of hanging on a nil channel.
  879. noRequests := make(chan *ssh.Request)
  880. close(noRequests)
  881. sshClient = ssh.NewClient(sshClientConn, sshChannels, noRequests)
  882. }
  883. resultChannel <- &sshNewClientResult{sshClient, sshRequests, err}
  884. }()
  885. result := <-resultChannel
  886. if result.err != nil {
  887. return nil, common.ContextError(result.err)
  888. }
  889. // Update dial parameters determined during dial
  890. if dialStats != nil && meekConfig != nil {
  891. dialStats.MeekResolvedIPAddress = resolvedIPAddress.Load().(string)
  892. }
  893. NoticeConnectedServer(
  894. serverEntry.IpAddress,
  895. serverEntry.Region,
  896. selectedProtocol,
  897. dialStats)
  898. cleanupConn = nil
  899. // Note: dialConn may be used to close the underlying network connection
  900. // but should not be used to perform I/O as that would interfere with SSH
  901. // (and also bypasses throttling).
  902. return &dialResult{
  903. dialConn: dialConn,
  904. monitoredConn: monitoredConn,
  905. sshClient: result.sshClient,
  906. sshRequests: result.sshRequests,
  907. dialStats: dialStats},
  908. nil
  909. }
  910. func makeRandomPeriod(min, max time.Duration) time.Duration {
  911. period, err := common.MakeRandomPeriod(min, max)
  912. if err != nil {
  913. NoticeAlert("MakeRandomPeriod failed: %s", err)
  914. // Proceed without random period
  915. period = max
  916. }
  917. return period
  918. }
  919. // operateTunnel monitors the health of the tunnel and performs
  920. // periodic work.
  921. //
  922. // BytesTransferred and TotalBytesTransferred notices are emitted
  923. // for live reporting and diagnostics reporting, respectively.
  924. //
  925. // Status requests are sent to the Psiphon API to report bytes
  926. // transferred.
  927. //
  928. // Periodic SSH keep alive packets are sent to ensure the underlying
  929. // TCP connection isn't terminated by NAT, or other network
  930. // interference -- or test if it has been terminated while the device
  931. // has been asleep. When a keep alive times out, the tunnel is
  932. // considered failed.
  933. //
  934. // An immediate SSH keep alive "probe" is sent to test the tunnel and
  935. // server responsiveness when a port forward failure is detected: a
  936. // failed dial or failed read/write. This keep alive has a shorter
  937. // timeout.
  938. //
  939. // Note that port forward failures may be due to non-failure conditions.
  940. // For example, when the user inputs an invalid domain name and
  941. // resolution is done by the ssh server; or trying to connect to a
  942. // non-white-listed port; and the error message in these cases is not
  943. // distinguishable from a a true server error (a common error message,
  944. // "ssh: rejected: administratively prohibited (open failed)", may be
  945. // returned for these cases but also if the server has run out of
  946. // ephemeral ports, for example).
  947. //
  948. // SSH keep alives are not sent when the tunnel has been recently
  949. // active (not only does tunnel activity obviate the necessity of a keep
  950. // alive, testing has shown that keep alives may time out for "busy"
  951. // tunnels, especially over meek protocol and other high latency
  952. // conditions).
  953. //
  954. // "Recently active" is defined has having received payload bytes. Sent
  955. // bytes are not considered as testing has shown bytes may appear to
  956. // send when certain NAT devices have interfered with the tunnel, while
  957. // no bytes are received. In a pathological case, with DNS implemented
  958. // as tunneled UDP, a browser may wait excessively for a domain name to
  959. // resolve, while no new port forward is attempted which would otherwise
  960. // result in a tunnel failure detection.
  961. //
  962. // TODO: change "recently active" to include having received any
  963. // SSH protocol messages from the server, not just user payload?
  964. //
  965. func (tunnel *Tunnel) operateTunnel(tunnelOwner TunnelOwner) {
  966. defer tunnel.operateWaitGroup.Done()
  967. lastBytesReceivedTime := monotime.Now()
  968. lastTotalBytesTransferedTime := monotime.Now()
  969. totalSent := int64(0)
  970. totalReceived := int64(0)
  971. noticeBytesTransferredTicker := time.NewTicker(1 * time.Second)
  972. defer noticeBytesTransferredTicker.Stop()
  973. // The next status request and ssh keep alive times are picked at random,
  974. // from a range, to make the resulting traffic less fingerprintable,
  975. // Note: not using Tickers since these are not fixed time periods.
  976. nextStatusRequestPeriod := func() time.Duration {
  977. return makeRandomPeriod(
  978. PSIPHON_API_STATUS_REQUEST_PERIOD_MIN,
  979. PSIPHON_API_STATUS_REQUEST_PERIOD_MAX)
  980. }
  981. statsTimer := time.NewTimer(nextStatusRequestPeriod())
  982. defer statsTimer.Stop()
  983. // Schedule an immediate status request to deliver any unreported
  984. // persistent stats.
  985. // Note: this may not be effective when there's an outstanding
  986. // asynchronous untunneled final status request is holding the
  987. // persistent stats records. It may also conflict with other
  988. // tunnel candidates which attempt to send an immediate request
  989. // before being discarded. For now, we mitigate this with a short,
  990. // random delay.
  991. unreported := CountUnreportedPersistentStats()
  992. if unreported > 0 {
  993. NoticeInfo("Unreported persistent stats: %d", unreported)
  994. statsTimer.Reset(makeRandomPeriod(
  995. PSIPHON_API_STATUS_REQUEST_SHORT_PERIOD_MIN,
  996. PSIPHON_API_STATUS_REQUEST_SHORT_PERIOD_MAX))
  997. }
  998. nextSshKeepAlivePeriod := func() time.Duration {
  999. return makeRandomPeriod(
  1000. TUNNEL_SSH_KEEP_ALIVE_PERIOD_MIN,
  1001. TUNNEL_SSH_KEEP_ALIVE_PERIOD_MAX)
  1002. }
  1003. // TODO: don't initialize timer when config.DisablePeriodicSshKeepAlive is set
  1004. sshKeepAliveTimer := time.NewTimer(nextSshKeepAlivePeriod())
  1005. if tunnel.config.DisablePeriodicSshKeepAlive {
  1006. sshKeepAliveTimer.Stop()
  1007. } else {
  1008. defer sshKeepAliveTimer.Stop()
  1009. }
  1010. // Perform network requests in separate goroutines so as not to block
  1011. // other operations.
  1012. requestsWaitGroup := new(sync.WaitGroup)
  1013. requestsWaitGroup.Add(1)
  1014. signalStatusRequest := make(chan struct{})
  1015. go func() {
  1016. defer requestsWaitGroup.Done()
  1017. for range signalStatusRequest {
  1018. sendStats(tunnel)
  1019. }
  1020. }()
  1021. requestsWaitGroup.Add(1)
  1022. signalSshKeepAlive := make(chan time.Duration)
  1023. sshKeepAliveError := make(chan error, 1)
  1024. go func() {
  1025. defer requestsWaitGroup.Done()
  1026. for timeout := range signalSshKeepAlive {
  1027. err := sendSshKeepAlive(tunnel.sshClient, tunnel.conn, timeout)
  1028. if err != nil {
  1029. select {
  1030. case sshKeepAliveError <- err:
  1031. default:
  1032. }
  1033. }
  1034. }
  1035. }()
  1036. requestsWaitGroup.Add(1)
  1037. signalStopClientVerificationRequests := make(chan struct{})
  1038. go func() {
  1039. defer requestsWaitGroup.Done()
  1040. clientVerificationRequestSuccess := true
  1041. clientVerificationPayload := ""
  1042. failCount := 0
  1043. for {
  1044. // TODO: use reflect.SelectCase?
  1045. if clientVerificationRequestSuccess == true {
  1046. failCount = 0
  1047. select {
  1048. case clientVerificationPayload = <-tunnel.newClientVerificationPayload:
  1049. case <-signalStopClientVerificationRequests:
  1050. return
  1051. }
  1052. } else {
  1053. // If sendClientVerification failed to send the payload we
  1054. // will retry after a delay. Will use a new payload instead
  1055. // if that arrives in the meantime.
  1056. // If failures count is more than PSIPHON_API_CLIENT_VERIFICATION_REQUEST_MAX_RETRIES
  1057. // stop retrying for this tunnel.
  1058. failCount += 1
  1059. if failCount > PSIPHON_API_CLIENT_VERIFICATION_REQUEST_MAX_RETRIES {
  1060. return
  1061. }
  1062. timeout := time.After(PSIPHON_API_CLIENT_VERIFICATION_REQUEST_RETRY_PERIOD)
  1063. select {
  1064. case <-timeout:
  1065. case clientVerificationPayload = <-tunnel.newClientVerificationPayload:
  1066. case <-signalStopClientVerificationRequests:
  1067. return
  1068. }
  1069. }
  1070. clientVerificationRequestSuccess = sendClientVerification(tunnel, clientVerificationPayload)
  1071. }
  1072. }()
  1073. shutdown := false
  1074. var err error
  1075. for !shutdown && err == nil {
  1076. select {
  1077. case <-noticeBytesTransferredTicker.C:
  1078. sent, received := transferstats.ReportRecentBytesTransferredForServer(
  1079. tunnel.serverEntry.IpAddress)
  1080. if received > 0 {
  1081. lastBytesReceivedTime = monotime.Now()
  1082. }
  1083. totalSent += sent
  1084. totalReceived += received
  1085. if lastTotalBytesTransferedTime.Add(TOTAL_BYTES_TRANSFERRED_NOTICE_PERIOD).Before(monotime.Now()) {
  1086. NoticeTotalBytesTransferred(tunnel.serverEntry.IpAddress, totalSent, totalReceived)
  1087. lastTotalBytesTransferedTime = monotime.Now()
  1088. }
  1089. // Only emit the frequent BytesTransferred notice when tunnel is not idle.
  1090. if tunnel.config.EmitBytesTransferred && (sent > 0 || received > 0) {
  1091. NoticeBytesTransferred(tunnel.serverEntry.IpAddress, sent, received)
  1092. }
  1093. case <-statsTimer.C:
  1094. select {
  1095. case signalStatusRequest <- *new(struct{}):
  1096. default:
  1097. }
  1098. statsTimer.Reset(nextStatusRequestPeriod())
  1099. case <-sshKeepAliveTimer.C:
  1100. if lastBytesReceivedTime.Add(TUNNEL_SSH_KEEP_ALIVE_PERIODIC_INACTIVE_PERIOD).Before(monotime.Now()) {
  1101. select {
  1102. case signalSshKeepAlive <- time.Duration(*tunnel.config.TunnelSshKeepAlivePeriodicTimeoutSeconds) * time.Second:
  1103. default:
  1104. }
  1105. }
  1106. sshKeepAliveTimer.Reset(nextSshKeepAlivePeriod())
  1107. case <-tunnel.signalPortForwardFailure:
  1108. // Note: no mutex on portForwardFailureTotal; only referenced here
  1109. tunnel.totalPortForwardFailures++
  1110. NoticeInfo("port forward failures for %s: %d",
  1111. tunnel.serverEntry.IpAddress, tunnel.totalPortForwardFailures)
  1112. // If the underlying Conn has closed (meek and other plugin protocols may close
  1113. // themselves in certain error conditions), the tunnel has certainly failed.
  1114. // Otherwise, probe with an SSH keep alive.
  1115. if tunnel.conn.IsClosed() {
  1116. err = errors.New("underlying conn is closed")
  1117. } else {
  1118. if lastBytesReceivedTime.Add(TUNNEL_SSH_KEEP_ALIVE_PROBE_INACTIVE_PERIOD).Before(monotime.Now()) {
  1119. select {
  1120. case signalSshKeepAlive <- time.Duration(*tunnel.config.TunnelSshKeepAliveProbeTimeoutSeconds) * time.Second:
  1121. default:
  1122. }
  1123. }
  1124. if !tunnel.config.DisablePeriodicSshKeepAlive {
  1125. sshKeepAliveTimer.Reset(nextSshKeepAlivePeriod())
  1126. }
  1127. }
  1128. case err = <-sshKeepAliveError:
  1129. case serverRequest := <-tunnel.sshServerRequests:
  1130. if serverRequest != nil {
  1131. err := HandleServerRequest(tunnelOwner, tunnel, serverRequest.Type, serverRequest.Payload)
  1132. if err == nil {
  1133. serverRequest.Reply(true, nil)
  1134. } else {
  1135. NoticeAlert("HandleServerRequest for %s failed: %s", serverRequest.Type, err)
  1136. serverRequest.Reply(false, nil)
  1137. }
  1138. }
  1139. case <-tunnel.shutdownOperateBroadcast:
  1140. shutdown = true
  1141. }
  1142. }
  1143. close(signalSshKeepAlive)
  1144. close(signalStatusRequest)
  1145. close(signalStopClientVerificationRequests)
  1146. requestsWaitGroup.Wait()
  1147. // Capture bytes transferred since the last noticeBytesTransferredTicker tick
  1148. sent, received := transferstats.ReportRecentBytesTransferredForServer(tunnel.serverEntry.IpAddress)
  1149. totalSent += sent
  1150. totalReceived += received
  1151. // Always emit a final NoticeTotalBytesTransferred
  1152. NoticeTotalBytesTransferred(tunnel.serverEntry.IpAddress, totalSent, totalReceived)
  1153. // Tunnel does not have a serverContext when DisableApi is set.
  1154. if tunnel.serverContext != nil && !tunnel.IsDiscarded() {
  1155. // The stats for this tunnel will be reported via the next successful
  1156. // status request.
  1157. // Since client clocks are unreliable, we report the server's timestamp from
  1158. // the handshake response as the absolute tunnel start time. This time
  1159. // will be slightly earlier than the actual tunnel activation time, as the
  1160. // client has to receive and parse the response and activate the tunnel.
  1161. tunnelStartTime := tunnel.serverContext.serverHandshakeTimestamp
  1162. // For the tunnel duration calculation, we use the local clock. The start time
  1163. // is tunnel.establishedTime as recorded when the tunnel was established. For the
  1164. // end time, we do not use the current time as we may now be long past the
  1165. // actual termination time of the tunnel. For example, the host or device may
  1166. // have resumed after a long sleep (it's not clear that the monotonic clock service
  1167. // used to measure elapsed time will or will not stop during device sleep). Instead,
  1168. // we use the last data received time as the estimated tunnel end time.
  1169. //
  1170. // One potential issue with using the last received time is receiving data
  1171. // after an extended sleep because the device sleep occurred with data still in
  1172. // the OS socket read buffer. This is not expected to happen on Android, as the
  1173. // OS will wake a process when it has TCP data available to read. (For this reason,
  1174. // the actual long sleep issue is only with an idle tunnel; in this case the client
  1175. // is responsible for sending SSH keep alives but a device sleep will delay the
  1176. // golang SSH keep alive timer.)
  1177. //
  1178. // Idle tunnels will only read data when a SSH keep alive is sent. As a result,
  1179. // the last-received-time scheme can undercount tunnel durations by up to
  1180. // TUNNEL_SSH_KEEP_ALIVE_PERIOD_MAX for idle tunnels.
  1181. tunnelDuration := tunnel.conn.GetLastActivityMonotime().Sub(tunnel.establishedTime)
  1182. // tunnelDuration can be < 0 when tunnel.establishedTime is recorded after the
  1183. // last tunnel.conn.Read() succeeds. In that case, the last read would be the
  1184. // handshake response, so the tunnel had, essentially, no duration.
  1185. if tunnelDuration < 0 {
  1186. tunnelDuration = 0
  1187. }
  1188. err := RecordTunnelStat(
  1189. tunnel.serverContext.sessionId,
  1190. tunnel.serverContext.tunnelNumber,
  1191. tunnel.serverEntry.IpAddress,
  1192. fmt.Sprintf("%d", tunnel.establishDuration),
  1193. tunnelStartTime,
  1194. fmt.Sprintf("%d", tunnelDuration),
  1195. totalSent,
  1196. totalReceived)
  1197. if err != nil {
  1198. NoticeAlert("RecordTunnelStats failed: %s", common.ContextError(err))
  1199. }
  1200. }
  1201. // Final status request notes:
  1202. //
  1203. // It's highly desirable to send a final status request in order to report
  1204. // domain bytes transferred stats as well as to report tunnel stats as
  1205. // soon as possible. For this reason, we attempt untunneled requests when
  1206. // the tunneled request isn't possible or has failed.
  1207. //
  1208. // In an orderly shutdown (err == nil), the Controller is stopping and
  1209. // everything must be wrapped up quickly. Also, we still have a working
  1210. // tunnel. So we first attempt a tunneled status request (with a short
  1211. // timeout) and then attempt, synchronously -- otherwise the Contoller's
  1212. // runWaitGroup.Wait() will return while a request is still in progress
  1213. // -- untunneled requests (also with short timeouts). Note that in this
  1214. // case the untunneled request will opt out of untunneledPendingConns so
  1215. // that it's not inadvertently canceled by the Controller shutdown
  1216. // sequence (see doUntunneledStatusRequest).
  1217. //
  1218. // If the tunnel has failed, the Controller may continue working. We want
  1219. // to re-establish as soon as possible (so don't want to block on status
  1220. // requests, even for a second). We may have a long time to attempt
  1221. // untunneled requests in the background. And there is no tunnel through
  1222. // which to attempt tunneled requests. So we spawn a goroutine to run the
  1223. // untunneled requests, which are allowed a longer timeout. These requests
  1224. // will be interrupted by the Controller's untunneledPendingConns.CloseAll()
  1225. // in the case of a shutdown.
  1226. if err == nil {
  1227. NoticeInfo("shutdown operate tunnel")
  1228. if !sendStats(tunnel) {
  1229. sendUntunneledStats(tunnel, true)
  1230. }
  1231. } else {
  1232. NoticeAlert("operate tunnel error for %s: %s", tunnel.serverEntry.IpAddress, err)
  1233. go sendUntunneledStats(tunnel, false)
  1234. tunnelOwner.SignalTunnelFailure(tunnel)
  1235. }
  1236. }
  1237. // sendSshKeepAlive is a helper which sends a keepalive@openssh.com request
  1238. // on the specified SSH connections and returns true of the request succeeds
  1239. // within a specified timeout. If the request fails, the associated conn is
  1240. // closed, which will terminate the associated tunnel.
  1241. func sendSshKeepAlive(
  1242. sshClient *ssh.Client, conn net.Conn, timeout time.Duration) error {
  1243. errChannel := make(chan error, 2)
  1244. if timeout > 0 {
  1245. time.AfterFunc(timeout, func() {
  1246. errChannel <- errors.New("timed out")
  1247. })
  1248. }
  1249. go func() {
  1250. // Random padding to frustrate fingerprinting
  1251. randomPadding, err := common.MakeSecureRandomPadding(0, TUNNEL_SSH_KEEP_ALIVE_PAYLOAD_MAX_BYTES)
  1252. if err != nil {
  1253. NoticeAlert("MakeSecureRandomPadding failed: %s", err)
  1254. // Proceed without random padding
  1255. randomPadding = make([]byte, 0)
  1256. }
  1257. // Note: reading a reply is important for last-received-time tunnel
  1258. // duration calculation.
  1259. _, _, err = sshClient.SendRequest("keepalive@openssh.com", true, randomPadding)
  1260. errChannel <- err
  1261. }()
  1262. err := <-errChannel
  1263. if err != nil {
  1264. sshClient.Close()
  1265. conn.Close()
  1266. }
  1267. return common.ContextError(err)
  1268. }
  1269. // sendStats is a helper for sending session stats to the server.
  1270. func sendStats(tunnel *Tunnel) bool {
  1271. // Tunnel does not have a serverContext when DisableApi is set
  1272. if tunnel.serverContext == nil {
  1273. return true
  1274. }
  1275. // Skip when tunnel is discarded
  1276. if tunnel.IsDiscarded() {
  1277. return true
  1278. }
  1279. err := tunnel.serverContext.DoStatusRequest(tunnel)
  1280. if err != nil {
  1281. NoticeAlert("DoStatusRequest failed for %s: %s", tunnel.serverEntry.IpAddress, err)
  1282. }
  1283. return err == nil
  1284. }
  1285. // sendUntunnelStats sends final status requests directly to Psiphon
  1286. // servers after the tunnel has already failed. This is an attempt
  1287. // to retain useful bytes transferred stats.
  1288. func sendUntunneledStats(tunnel *Tunnel, isShutdown bool) {
  1289. // Tunnel does not have a serverContext when DisableApi is set
  1290. if tunnel.serverContext == nil {
  1291. return
  1292. }
  1293. // Skip when tunnel is discarded
  1294. if tunnel.IsDiscarded() {
  1295. return
  1296. }
  1297. err := tunnel.serverContext.TryUntunneledStatusRequest(isShutdown)
  1298. if err != nil {
  1299. NoticeAlert("TryUntunneledStatusRequest failed for %s: %s", tunnel.serverEntry.IpAddress, err)
  1300. }
  1301. }
  1302. // sendClientVerification is a helper for sending a client verification request
  1303. // to the server.
  1304. func sendClientVerification(tunnel *Tunnel, clientVerificationPayload string) bool {
  1305. // Tunnel does not have a serverContext when DisableApi is set
  1306. if tunnel.serverContext == nil {
  1307. return true
  1308. }
  1309. // Skip when tunnel is discarded
  1310. if tunnel.IsDiscarded() {
  1311. return true
  1312. }
  1313. err := tunnel.serverContext.DoClientVerificationRequest(clientVerificationPayload, tunnel.serverEntry.IpAddress)
  1314. if err != nil {
  1315. NoticeAlert("DoClientVerificationRequest failed for %s: %s", tunnel.serverEntry.IpAddress, err)
  1316. }
  1317. return err == nil
  1318. }