tunnelServer.go 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014
  1. /*
  2. * Copyright (c) 2016, 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 server
  20. import (
  21. "context"
  22. "crypto/subtle"
  23. "encoding/json"
  24. "errors"
  25. "fmt"
  26. "io"
  27. "net"
  28. "strconv"
  29. "sync"
  30. "sync/atomic"
  31. "syscall"
  32. "time"
  33. "github.com/Psiphon-Inc/crypto/ssh"
  34. cache "github.com/Psiphon-Inc/go-cache"
  35. "github.com/Psiphon-Inc/goarista/monotime"
  36. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  37. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/osl"
  38. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
  39. )
  40. const (
  41. SSH_HANDSHAKE_TIMEOUT = 30 * time.Second
  42. SSH_CONNECTION_READ_DEADLINE = 5 * time.Minute
  43. SSH_TCP_PORT_FORWARD_COPY_BUFFER_SIZE = 8192
  44. SSH_TCP_PORT_FORWARD_QUEUE_SIZE = 1024
  45. SSH_SEND_OSL_INITIAL_RETRY_DELAY = 30 * time.Second
  46. SSH_SEND_OSL_RETRY_FACTOR = 2
  47. OSL_SESSION_CACHE_TTL = 5 * time.Minute
  48. )
  49. // TunnelServer is the main server that accepts Psiphon client
  50. // connections, via various obfuscation protocols, and provides
  51. // port forwarding (TCP and UDP) services to the Psiphon client.
  52. // At its core, TunnelServer is an SSH server. SSH is the base
  53. // protocol that provides port forward multiplexing, and transport
  54. // security. Layered on top of SSH, optionally, is Obfuscated SSH
  55. // and meek protocols, which provide further circumvention
  56. // capabilities.
  57. type TunnelServer struct {
  58. runWaitGroup *sync.WaitGroup
  59. listenerError chan error
  60. shutdownBroadcast <-chan struct{}
  61. sshServer *sshServer
  62. }
  63. // NewTunnelServer initializes a new tunnel server.
  64. func NewTunnelServer(
  65. support *SupportServices,
  66. shutdownBroadcast <-chan struct{}) (*TunnelServer, error) {
  67. sshServer, err := newSSHServer(support, shutdownBroadcast)
  68. if err != nil {
  69. return nil, common.ContextError(err)
  70. }
  71. return &TunnelServer{
  72. runWaitGroup: new(sync.WaitGroup),
  73. listenerError: make(chan error),
  74. shutdownBroadcast: shutdownBroadcast,
  75. sshServer: sshServer,
  76. }, nil
  77. }
  78. // Run runs the tunnel server; this function blocks while running a selection of
  79. // listeners that handle connection using various obfuscation protocols.
  80. //
  81. // Run listens on each designated tunnel port and spawns new goroutines to handle
  82. // each client connection. It halts when shutdownBroadcast is signaled. A list of active
  83. // clients is maintained, and when halting all clients are cleanly shutdown.
  84. //
  85. // Each client goroutine handles its own obfuscation (optional), SSH handshake, SSH
  86. // authentication, and then looping on client new channel requests. "direct-tcpip"
  87. // channels, dynamic port fowards, are supported. When the UDPInterceptUdpgwServerAddress
  88. // config parameter is configured, UDP port forwards over a TCP stream, following
  89. // the udpgw protocol, are handled.
  90. //
  91. // A new goroutine is spawned to handle each port forward for each client. Each port
  92. // forward tracks its bytes transferred. Overall per-client stats for connection duration,
  93. // GeoIP, number of port forwards, and bytes transferred are tracked and logged when the
  94. // client shuts down.
  95. //
  96. // Note: client handler goroutines may still be shutting down after Run() returns. See
  97. // comment in sshClient.stop(). TODO: fully synchronized shutdown.
  98. func (server *TunnelServer) Run() error {
  99. type sshListener struct {
  100. net.Listener
  101. localAddress string
  102. tunnelProtocol string
  103. }
  104. // TODO: should TunnelServer hold its own support pointer?
  105. support := server.sshServer.support
  106. // First bind all listeners; once all are successful,
  107. // start accepting connections on each.
  108. var listeners []*sshListener
  109. for tunnelProtocol, listenPort := range support.Config.TunnelProtocolPorts {
  110. localAddress := fmt.Sprintf(
  111. "%s:%d", support.Config.ServerIPAddress, listenPort)
  112. listener, err := net.Listen("tcp", localAddress)
  113. if err != nil {
  114. for _, existingListener := range listeners {
  115. existingListener.Listener.Close()
  116. }
  117. return common.ContextError(err)
  118. }
  119. log.WithContextFields(
  120. LogFields{
  121. "localAddress": localAddress,
  122. "tunnelProtocol": tunnelProtocol,
  123. }).Info("listening")
  124. listeners = append(
  125. listeners,
  126. &sshListener{
  127. Listener: listener,
  128. localAddress: localAddress,
  129. tunnelProtocol: tunnelProtocol,
  130. })
  131. }
  132. for _, listener := range listeners {
  133. server.runWaitGroup.Add(1)
  134. go func(listener *sshListener) {
  135. defer server.runWaitGroup.Done()
  136. log.WithContextFields(
  137. LogFields{
  138. "localAddress": listener.localAddress,
  139. "tunnelProtocol": listener.tunnelProtocol,
  140. }).Info("running")
  141. server.sshServer.runListener(
  142. listener.Listener,
  143. server.listenerError,
  144. listener.tunnelProtocol)
  145. log.WithContextFields(
  146. LogFields{
  147. "localAddress": listener.localAddress,
  148. "tunnelProtocol": listener.tunnelProtocol,
  149. }).Info("stopped")
  150. }(listener)
  151. }
  152. var err error
  153. select {
  154. case <-server.shutdownBroadcast:
  155. case err = <-server.listenerError:
  156. }
  157. for _, listener := range listeners {
  158. listener.Close()
  159. }
  160. server.sshServer.stopClients()
  161. server.runWaitGroup.Wait()
  162. log.WithContext().Info("stopped")
  163. return err
  164. }
  165. // GetLoadStats returns load stats for the tunnel server. The stats are
  166. // broken down by protocol ("SSH", "OSSH", etc.) and type. Types of stats
  167. // include current connected client count, total number of current port
  168. // forwards.
  169. func (server *TunnelServer) GetLoadStats() (ProtocolStats, RegionStats) {
  170. return server.sshServer.getLoadStats()
  171. }
  172. // ResetAllClientTrafficRules resets all established client traffic rules
  173. // to use the latest config and client properties. Any existing traffic
  174. // rule state is lost, including throttling state.
  175. func (server *TunnelServer) ResetAllClientTrafficRules() {
  176. server.sshServer.resetAllClientTrafficRules()
  177. }
  178. // ResetAllClientOSLConfigs resets all established client OSL state to use
  179. // the latest OSL config. Any existing OSL state is lost, including partial
  180. // progress towards SLOKs.
  181. func (server *TunnelServer) ResetAllClientOSLConfigs() {
  182. server.sshServer.resetAllClientOSLConfigs()
  183. }
  184. // SetClientHandshakeState sets the handshake state -- that it completed and
  185. // what paramaters were passed -- in sshClient. This state is used for allowing
  186. // port forwards and for future traffic rule selection. SetClientHandshakeState
  187. // also triggers an immediate traffic rule re-selection, as the rules selected
  188. // upon tunnel establishment may no longer apply now that handshake values are
  189. // set.
  190. func (server *TunnelServer) SetClientHandshakeState(
  191. sessionID string, state handshakeState) error {
  192. return server.sshServer.setClientHandshakeState(sessionID, state)
  193. }
  194. // SetEstablishTunnels sets whether new tunnels may be established or not.
  195. // When not establishing, incoming connections are immediately closed.
  196. func (server *TunnelServer) SetEstablishTunnels(establish bool) {
  197. server.sshServer.setEstablishTunnels(establish)
  198. }
  199. // GetEstablishTunnels returns whether new tunnels may be established or not.
  200. func (server *TunnelServer) GetEstablishTunnels() bool {
  201. return server.sshServer.getEstablishTunnels()
  202. }
  203. type sshServer struct {
  204. support *SupportServices
  205. establishTunnels int32
  206. shutdownBroadcast <-chan struct{}
  207. sshHostKey ssh.Signer
  208. clientsMutex sync.Mutex
  209. stoppingClients bool
  210. acceptedClientCounts map[string]map[string]int64
  211. clients map[string]*sshClient
  212. oslSessionCacheMutex sync.Mutex
  213. oslSessionCache *cache.Cache
  214. }
  215. func newSSHServer(
  216. support *SupportServices,
  217. shutdownBroadcast <-chan struct{}) (*sshServer, error) {
  218. privateKey, err := ssh.ParseRawPrivateKey([]byte(support.Config.SSHPrivateKey))
  219. if err != nil {
  220. return nil, common.ContextError(err)
  221. }
  222. // TODO: use cert (ssh.NewCertSigner) for anti-fingerprint?
  223. signer, err := ssh.NewSignerFromKey(privateKey)
  224. if err != nil {
  225. return nil, common.ContextError(err)
  226. }
  227. // The OSL session cache temporarily retains OSL seed state
  228. // progress for disconnected clients. This enables clients
  229. // that disconnect and immediately reconnect to the same
  230. // server to resume their OSL progress. Cached progress
  231. // is referenced by session ID and is retained for
  232. // OSL_SESSION_CACHE_TTL after disconnect.
  233. //
  234. // Note: session IDs are assumed to be unpredictable. If a
  235. // rogue client could guess the session ID of another client,
  236. // it could resume its OSL progress and, if the OSL config
  237. // were known, infer some activity.
  238. oslSessionCache := cache.New(OSL_SESSION_CACHE_TTL, 1*time.Minute)
  239. return &sshServer{
  240. support: support,
  241. establishTunnels: 1,
  242. shutdownBroadcast: shutdownBroadcast,
  243. sshHostKey: signer,
  244. acceptedClientCounts: make(map[string]map[string]int64),
  245. clients: make(map[string]*sshClient),
  246. oslSessionCache: oslSessionCache,
  247. }, nil
  248. }
  249. func (sshServer *sshServer) setEstablishTunnels(establish bool) {
  250. // Do nothing when the setting is already correct. This avoids
  251. // spurious log messages when setEstablishTunnels is called
  252. // periodically with the same setting.
  253. if establish == sshServer.getEstablishTunnels() {
  254. return
  255. }
  256. establishFlag := int32(1)
  257. if !establish {
  258. establishFlag = 0
  259. }
  260. atomic.StoreInt32(&sshServer.establishTunnels, establishFlag)
  261. log.WithContextFields(
  262. LogFields{"establish": establish}).Info("establishing tunnels")
  263. }
  264. func (sshServer *sshServer) getEstablishTunnels() bool {
  265. return atomic.LoadInt32(&sshServer.establishTunnels) == 1
  266. }
  267. // runListener is intended to run an a goroutine; it blocks
  268. // running a particular listener. If an unrecoverable error
  269. // occurs, it will send the error to the listenerError channel.
  270. func (sshServer *sshServer) runListener(
  271. listener net.Listener,
  272. listenerError chan<- error,
  273. tunnelProtocol string) {
  274. handleClient := func(clientConn net.Conn) {
  275. // Note: establish tunnel limiter cannot simply stop TCP
  276. // listeners in all cases (e.g., meek) since SSH tunnel can
  277. // span multiple TCP connections.
  278. if !sshServer.getEstablishTunnels() {
  279. log.WithContext().Debug("not establishing tunnels")
  280. clientConn.Close()
  281. return
  282. }
  283. // process each client connection concurrently
  284. go sshServer.handleClient(tunnelProtocol, clientConn)
  285. }
  286. // Note: when exiting due to a unrecoverable error, be sure
  287. // to try to send the error to listenerError so that the outer
  288. // TunnelServer.Run will properly shut down instead of remaining
  289. // running.
  290. if protocol.TunnelProtocolUsesMeekHTTP(tunnelProtocol) ||
  291. protocol.TunnelProtocolUsesMeekHTTPS(tunnelProtocol) {
  292. meekServer, err := NewMeekServer(
  293. sshServer.support,
  294. listener,
  295. protocol.TunnelProtocolUsesMeekHTTPS(tunnelProtocol),
  296. protocol.TunnelProtocolUsesObfuscatedSessionTickets(tunnelProtocol),
  297. handleClient,
  298. sshServer.shutdownBroadcast)
  299. if err != nil {
  300. select {
  301. case listenerError <- common.ContextError(err):
  302. default:
  303. }
  304. return
  305. }
  306. meekServer.Run()
  307. } else {
  308. for {
  309. conn, err := listener.Accept()
  310. select {
  311. case <-sshServer.shutdownBroadcast:
  312. if err == nil {
  313. conn.Close()
  314. }
  315. return
  316. default:
  317. }
  318. if err != nil {
  319. if e, ok := err.(net.Error); ok && e.Temporary() {
  320. log.WithContextFields(LogFields{"error": err}).Error("accept failed")
  321. // Temporary error, keep running
  322. continue
  323. }
  324. select {
  325. case listenerError <- common.ContextError(err):
  326. default:
  327. }
  328. return
  329. }
  330. handleClient(conn)
  331. }
  332. }
  333. }
  334. // An accepted client has completed a direct TCP or meek connection and has a net.Conn. Registration
  335. // is for tracking the number of connections.
  336. func (sshServer *sshServer) registerAcceptedClient(tunnelProtocol, region string) {
  337. sshServer.clientsMutex.Lock()
  338. defer sshServer.clientsMutex.Unlock()
  339. if sshServer.acceptedClientCounts[tunnelProtocol] == nil {
  340. sshServer.acceptedClientCounts[tunnelProtocol] = make(map[string]int64)
  341. }
  342. sshServer.acceptedClientCounts[tunnelProtocol][region] += 1
  343. }
  344. func (sshServer *sshServer) unregisterAcceptedClient(tunnelProtocol, region string) {
  345. sshServer.clientsMutex.Lock()
  346. defer sshServer.clientsMutex.Unlock()
  347. sshServer.acceptedClientCounts[tunnelProtocol][region] -= 1
  348. }
  349. // An established client has completed its SSH handshake and has a ssh.Conn. Registration is
  350. // for tracking the number of fully established clients and for maintaining a list of running
  351. // clients (for stopping at shutdown time).
  352. func (sshServer *sshServer) registerEstablishedClient(client *sshClient) bool {
  353. sshServer.clientsMutex.Lock()
  354. if sshServer.stoppingClients {
  355. sshServer.clientsMutex.Unlock()
  356. return false
  357. }
  358. // In the case of a duplicate client sessionID, the previous client is closed.
  359. // - Well-behaved clients generate pick a random sessionID that should be
  360. // unique (won't accidentally conflict) and hard to guess (can't be targetted
  361. // by a malicious client).
  362. // - Clients reuse the same sessionID when a tunnel is unexpectedly disconnected
  363. // and resestablished. In this case, when the same server is selected, this logic
  364. // will be hit; closing the old, dangling client is desirable.
  365. // - Multi-tunnel clients should not normally use one server for multiple tunnels.
  366. existingClient := sshServer.clients[client.sessionID]
  367. sshServer.clients[client.sessionID] = client
  368. sshServer.clientsMutex.Unlock()
  369. // Call stop() outside the mutex to avoid deadlock.
  370. if existingClient != nil {
  371. existingClient.stop()
  372. log.WithContext().Info(
  373. "stopped existing client with duplicate session ID")
  374. }
  375. return true
  376. }
  377. func (sshServer *sshServer) unregisterEstablishedClient(client *sshClient) {
  378. sshServer.clientsMutex.Lock()
  379. registeredClient := sshServer.clients[client.sessionID]
  380. // registeredClient will differ from client when client
  381. // is the existingClient terminated in registerEstablishedClient.
  382. // In that case, registeredClient remains connected, and
  383. // the sshServer.clients entry should be retained.
  384. if registeredClient == client {
  385. delete(sshServer.clients, client.sessionID)
  386. }
  387. sshServer.clientsMutex.Unlock()
  388. // Call stop() outside the mutex to avoid deadlock.
  389. client.stop()
  390. }
  391. type ProtocolStats map[string]map[string]int64
  392. type RegionStats map[string]map[string]map[string]int64
  393. func (sshServer *sshServer) getLoadStats() (ProtocolStats, RegionStats) {
  394. sshServer.clientsMutex.Lock()
  395. defer sshServer.clientsMutex.Unlock()
  396. // Explicitly populate with zeros to ensure 0 counts in log messages
  397. zeroStats := func() map[string]int64 {
  398. stats := make(map[string]int64)
  399. stats["accepted_clients"] = 0
  400. stats["established_clients"] = 0
  401. stats["dialing_tcp_port_forwards"] = 0
  402. stats["tcp_port_forwards"] = 0
  403. stats["total_tcp_port_forwards"] = 0
  404. stats["udp_port_forwards"] = 0
  405. stats["total_udp_port_forwards"] = 0
  406. stats["tcp_port_forward_dialed_count"] = 0
  407. stats["tcp_port_forward_dialed_duration"] = 0
  408. stats["tcp_port_forward_failed_count"] = 0
  409. stats["tcp_port_forward_failed_duration"] = 0
  410. stats["tcp_port_forward_rejected_dialing_limit_count"] = 0
  411. return stats
  412. }
  413. zeroProtocolStats := func() map[string]map[string]int64 {
  414. stats := make(map[string]map[string]int64)
  415. stats["ALL"] = zeroStats()
  416. for tunnelProtocol, _ := range sshServer.support.Config.TunnelProtocolPorts {
  417. stats[tunnelProtocol] = zeroStats()
  418. }
  419. return stats
  420. }
  421. // [<protocol or ALL>][<stat name>] -> count
  422. protocolStats := zeroProtocolStats()
  423. // [<region][<protocol or ALL>][<stat name>] -> count
  424. regionStats := make(RegionStats)
  425. // Note: as currently tracked/counted, each established client is also an accepted client
  426. for tunnelProtocol, regionAcceptedClientCounts := range sshServer.acceptedClientCounts {
  427. for region, acceptedClientCount := range regionAcceptedClientCounts {
  428. if acceptedClientCount > 0 {
  429. if regionStats[region] == nil {
  430. regionStats[region] = zeroProtocolStats()
  431. }
  432. protocolStats["ALL"]["accepted_clients"] += acceptedClientCount
  433. protocolStats[tunnelProtocol]["accepted_clients"] += acceptedClientCount
  434. regionStats[region]["ALL"]["accepted_clients"] += acceptedClientCount
  435. regionStats[region][tunnelProtocol]["accepted_clients"] += acceptedClientCount
  436. }
  437. }
  438. }
  439. for _, client := range sshServer.clients {
  440. client.Lock()
  441. tunnelProtocol := client.tunnelProtocol
  442. region := client.geoIPData.Country
  443. if regionStats[region] == nil {
  444. regionStats[region] = zeroProtocolStats()
  445. }
  446. stats := []map[string]int64{
  447. protocolStats["ALL"],
  448. protocolStats[tunnelProtocol],
  449. regionStats[region]["ALL"],
  450. regionStats[region][tunnelProtocol]}
  451. for _, stat := range stats {
  452. stat["established_clients"] += 1
  453. // Note: can't sum trafficState.peakConcurrentPortForwardCount to get a global peak
  454. stat["dialing_tcp_port_forwards"] += client.tcpTrafficState.concurrentDialingPortForwardCount
  455. stat["tcp_port_forwards"] += client.tcpTrafficState.concurrentPortForwardCount
  456. stat["total_tcp_port_forwards"] += client.tcpTrafficState.totalPortForwardCount
  457. // client.udpTrafficState.concurrentDialingPortForwardCount isn't meaningful
  458. stat["udp_port_forwards"] += client.udpTrafficState.concurrentPortForwardCount
  459. stat["total_udp_port_forwards"] += client.udpTrafficState.totalPortForwardCount
  460. stat["tcp_port_forward_dialed_count"] += client.qualityMetrics.tcpPortForwardDialedCount
  461. stat["tcp_port_forward_dialed_duration"] +=
  462. int64(client.qualityMetrics.tcpPortForwardDialedDuration / time.Millisecond)
  463. stat["tcp_port_forward_failed_count"] += client.qualityMetrics.tcpPortForwardFailedCount
  464. stat["tcp_port_forward_failed_duration"] +=
  465. int64(client.qualityMetrics.tcpPortForwardFailedDuration / time.Millisecond)
  466. stat["tcp_port_forward_rejected_dialing_limit_count"] +=
  467. client.qualityMetrics.tcpPortForwardRejectedDialingLimitCount
  468. }
  469. client.qualityMetrics.tcpPortForwardDialedCount = 0
  470. client.qualityMetrics.tcpPortForwardDialedDuration = 0
  471. client.qualityMetrics.tcpPortForwardFailedCount = 0
  472. client.qualityMetrics.tcpPortForwardFailedDuration = 0
  473. client.qualityMetrics.tcpPortForwardRejectedDialingLimitCount = 0
  474. client.Unlock()
  475. }
  476. return protocolStats, regionStats
  477. }
  478. func (sshServer *sshServer) resetAllClientTrafficRules() {
  479. sshServer.clientsMutex.Lock()
  480. clients := make(map[string]*sshClient)
  481. for sessionID, client := range sshServer.clients {
  482. clients[sessionID] = client
  483. }
  484. sshServer.clientsMutex.Unlock()
  485. for _, client := range clients {
  486. client.setTrafficRules()
  487. }
  488. }
  489. func (sshServer *sshServer) resetAllClientOSLConfigs() {
  490. // Flush cached seed state. This has the same effect
  491. // and same limitations as calling setOSLConfig for
  492. // currently connected clients -- all progress is lost.
  493. sshServer.oslSessionCacheMutex.Lock()
  494. sshServer.oslSessionCache.Flush()
  495. sshServer.oslSessionCacheMutex.Unlock()
  496. sshServer.clientsMutex.Lock()
  497. clients := make(map[string]*sshClient)
  498. for sessionID, client := range sshServer.clients {
  499. clients[sessionID] = client
  500. }
  501. sshServer.clientsMutex.Unlock()
  502. for _, client := range clients {
  503. client.setOSLConfig()
  504. }
  505. }
  506. func (sshServer *sshServer) setClientHandshakeState(
  507. sessionID string, state handshakeState) error {
  508. sshServer.clientsMutex.Lock()
  509. client := sshServer.clients[sessionID]
  510. sshServer.clientsMutex.Unlock()
  511. if client == nil {
  512. return common.ContextError(errors.New("unknown session ID"))
  513. }
  514. err := client.setHandshakeState(state)
  515. if err != nil {
  516. return common.ContextError(err)
  517. }
  518. return nil
  519. }
  520. func (sshServer *sshServer) stopClients() {
  521. sshServer.clientsMutex.Lock()
  522. sshServer.stoppingClients = true
  523. clients := sshServer.clients
  524. sshServer.clients = make(map[string]*sshClient)
  525. sshServer.clientsMutex.Unlock()
  526. for _, client := range clients {
  527. client.stop()
  528. }
  529. }
  530. func (sshServer *sshServer) handleClient(tunnelProtocol string, clientConn net.Conn) {
  531. geoIPData := sshServer.support.GeoIPService.Lookup(
  532. common.IPAddressFromAddr(clientConn.RemoteAddr()))
  533. sshServer.registerAcceptedClient(tunnelProtocol, geoIPData.Country)
  534. defer sshServer.unregisterAcceptedClient(tunnelProtocol, geoIPData.Country)
  535. sshClient := newSshClient(sshServer, tunnelProtocol, geoIPData)
  536. sshClient.run(clientConn)
  537. }
  538. func (sshServer *sshServer) monitorPortForwardDialError(err error) {
  539. // "err" is the error returned from a failed TCP or UDP port
  540. // forward dial. Certain system error codes indicate low resource
  541. // conditions: insufficient file descriptors, ephemeral ports, or
  542. // memory. For these cases, log an alert.
  543. // TODO: also temporarily suspend new clients
  544. // Note: don't log net.OpError.Error() as the full error string
  545. // may contain client destination addresses.
  546. opErr, ok := err.(*net.OpError)
  547. if ok {
  548. if opErr.Err == syscall.EADDRNOTAVAIL ||
  549. opErr.Err == syscall.EAGAIN ||
  550. opErr.Err == syscall.ENOMEM ||
  551. opErr.Err == syscall.EMFILE ||
  552. opErr.Err == syscall.ENFILE {
  553. log.WithContextFields(
  554. LogFields{"error": opErr.Err}).Error(
  555. "port forward dial failed due to unavailable resource")
  556. }
  557. }
  558. }
  559. type sshClient struct {
  560. sync.Mutex
  561. sshServer *sshServer
  562. tunnelProtocol string
  563. sshConn ssh.Conn
  564. activityConn *common.ActivityMonitoredConn
  565. throttledConn *common.ThrottledConn
  566. geoIPData GeoIPData
  567. sessionID string
  568. supportsServerRequests bool
  569. handshakeState handshakeState
  570. udpChannel ssh.Channel
  571. trafficRules TrafficRules
  572. tcpTrafficState trafficState
  573. udpTrafficState trafficState
  574. qualityMetrics qualityMetrics
  575. tcpPortForwardLRU *common.LRUConns
  576. oslClientSeedState *osl.ClientSeedState
  577. signalIssueSLOKs chan struct{}
  578. runContext context.Context
  579. stopRunning context.CancelFunc
  580. tcpPortForwardDialingAvailableSignal context.CancelFunc
  581. }
  582. type trafficState struct {
  583. bytesUp int64
  584. bytesDown int64
  585. concurrentDialingPortForwardCount int64
  586. peakConcurrentDialingPortForwardCount int64
  587. concurrentPortForwardCount int64
  588. peakConcurrentPortForwardCount int64
  589. totalPortForwardCount int64
  590. }
  591. // qualityMetrics records upstream TCP dial attempts and
  592. // elapsed time. Elapsed time includes the full TCP handshake
  593. // and, in aggregate, is a measure of the quality of the
  594. // upstream link. These stats are recorded by each sshClient
  595. // and then reported and reset in sshServer.getLoadStats().
  596. type qualityMetrics struct {
  597. tcpPortForwardDialedCount int64
  598. tcpPortForwardDialedDuration time.Duration
  599. tcpPortForwardFailedCount int64
  600. tcpPortForwardFailedDuration time.Duration
  601. tcpPortForwardRejectedDialingLimitCount int64
  602. }
  603. type handshakeState struct {
  604. completed bool
  605. apiProtocol string
  606. apiParams requestJSONObject
  607. }
  608. func newSshClient(
  609. sshServer *sshServer, tunnelProtocol string, geoIPData GeoIPData) *sshClient {
  610. runContext, stopRunning := context.WithCancel(context.Background())
  611. return &sshClient{
  612. sshServer: sshServer,
  613. tunnelProtocol: tunnelProtocol,
  614. geoIPData: geoIPData,
  615. tcpPortForwardLRU: common.NewLRUConns(),
  616. signalIssueSLOKs: make(chan struct{}, 1),
  617. runContext: runContext,
  618. stopRunning: stopRunning,
  619. }
  620. }
  621. func (sshClient *sshClient) run(clientConn net.Conn) {
  622. // Set initial traffic rules, pre-handshake, based on currently known info.
  623. sshClient.setTrafficRules()
  624. // Wrap the base client connection with an ActivityMonitoredConn which will
  625. // terminate the connection if no data is received before the deadline. This
  626. // timeout is in effect for the entire duration of the SSH connection. Clients
  627. // must actively use the connection or send SSH keep alive requests to keep
  628. // the connection active. Writes are not considered reliable activity indicators
  629. // due to buffering.
  630. activityConn, err := common.NewActivityMonitoredConn(
  631. clientConn,
  632. SSH_CONNECTION_READ_DEADLINE,
  633. false,
  634. nil,
  635. nil)
  636. if err != nil {
  637. clientConn.Close()
  638. log.WithContextFields(LogFields{"error": err}).Error("NewActivityMonitoredConn failed")
  639. return
  640. }
  641. clientConn = activityConn
  642. // Further wrap the connection in a rate limiting ThrottledConn.
  643. throttledConn := common.NewThrottledConn(clientConn, sshClient.rateLimits())
  644. clientConn = throttledConn
  645. // Run the initial [obfuscated] SSH handshake in a goroutine so we can both
  646. // respect shutdownBroadcast and implement a specific handshake timeout.
  647. // The timeout is to reclaim network resources in case the handshake takes
  648. // too long.
  649. type sshNewServerConnResult struct {
  650. conn net.Conn
  651. sshConn *ssh.ServerConn
  652. channels <-chan ssh.NewChannel
  653. requests <-chan *ssh.Request
  654. err error
  655. }
  656. resultChannel := make(chan *sshNewServerConnResult, 2)
  657. if SSH_HANDSHAKE_TIMEOUT > 0 {
  658. time.AfterFunc(time.Duration(SSH_HANDSHAKE_TIMEOUT), func() {
  659. resultChannel <- &sshNewServerConnResult{err: errors.New("ssh handshake timeout")}
  660. })
  661. }
  662. go func(conn net.Conn) {
  663. sshServerConfig := &ssh.ServerConfig{
  664. PasswordCallback: sshClient.passwordCallback,
  665. AuthLogCallback: sshClient.authLogCallback,
  666. ServerVersion: sshClient.sshServer.support.Config.SSHServerVersion,
  667. }
  668. sshServerConfig.AddHostKey(sshClient.sshServer.sshHostKey)
  669. result := &sshNewServerConnResult{}
  670. // Wrap the connection in an SSH deobfuscator when required.
  671. if protocol.TunnelProtocolUsesObfuscatedSSH(sshClient.tunnelProtocol) {
  672. // Note: NewObfuscatedSshConn blocks on network I/O
  673. // TODO: ensure this won't block shutdown
  674. conn, result.err = common.NewObfuscatedSshConn(
  675. common.OBFUSCATION_CONN_MODE_SERVER,
  676. conn,
  677. sshClient.sshServer.support.Config.ObfuscatedSSHKey)
  678. if result.err != nil {
  679. result.err = common.ContextError(result.err)
  680. }
  681. }
  682. if result.err == nil {
  683. result.sshConn, result.channels, result.requests, result.err =
  684. ssh.NewServerConn(conn, sshServerConfig)
  685. }
  686. resultChannel <- result
  687. }(clientConn)
  688. var result *sshNewServerConnResult
  689. select {
  690. case result = <-resultChannel:
  691. case <-sshClient.sshServer.shutdownBroadcast:
  692. // Close() will interrupt an ongoing handshake
  693. // TODO: wait for goroutine to exit before returning?
  694. clientConn.Close()
  695. return
  696. }
  697. if result.err != nil {
  698. clientConn.Close()
  699. // This is a Debug log due to noise. The handshake often fails due to I/O
  700. // errors as clients frequently interrupt connections in progress when
  701. // client-side load balancing completes a connection to a different server.
  702. log.WithContextFields(LogFields{"error": result.err}).Debug("handshake failed")
  703. return
  704. }
  705. sshClient.Lock()
  706. sshClient.sshConn = result.sshConn
  707. sshClient.activityConn = activityConn
  708. sshClient.throttledConn = throttledConn
  709. sshClient.Unlock()
  710. if !sshClient.sshServer.registerEstablishedClient(sshClient) {
  711. clientConn.Close()
  712. log.WithContext().Warning("register failed")
  713. return
  714. }
  715. sshClient.runTunnel(result.channels, result.requests)
  716. // Note: sshServer.unregisterEstablishedClient calls sshClient.stop(),
  717. // which also closes underlying transport Conn.
  718. sshClient.sshServer.unregisterEstablishedClient(sshClient)
  719. sshClient.logTunnel()
  720. // Transfer OSL seed state -- the OSL progress -- from the closing
  721. // client to the session cache so the client can resume its progress
  722. // if it reconnects to this same server.
  723. // Note: following setOSLConfig order of locking.
  724. sshClient.Lock()
  725. if sshClient.oslClientSeedState != nil {
  726. sshClient.sshServer.oslSessionCacheMutex.Lock()
  727. sshClient.oslClientSeedState.Hibernate()
  728. sshClient.sshServer.oslSessionCache.Set(
  729. sshClient.sessionID, sshClient.oslClientSeedState, cache.DefaultExpiration)
  730. sshClient.sshServer.oslSessionCacheMutex.Unlock()
  731. sshClient.oslClientSeedState = nil
  732. }
  733. sshClient.Unlock()
  734. // Initiate cleanup of the GeoIP session cache. To allow for post-tunnel
  735. // final status requests, the lifetime of cached GeoIP records exceeds the
  736. // lifetime of the sshClient.
  737. sshClient.sshServer.support.GeoIPService.MarkSessionCacheToExpire(sshClient.sessionID)
  738. }
  739. func (sshClient *sshClient) passwordCallback(conn ssh.ConnMetadata, password []byte) (*ssh.Permissions, error) {
  740. expectedSessionIDLength := 2 * protocol.PSIPHON_API_CLIENT_SESSION_ID_LENGTH
  741. expectedSSHPasswordLength := 2 * SSH_PASSWORD_BYTE_LENGTH
  742. var sshPasswordPayload protocol.SSHPasswordPayload
  743. err := json.Unmarshal(password, &sshPasswordPayload)
  744. if err != nil {
  745. // Backwards compatibility case: instead of a JSON payload, older clients
  746. // send the hex encoded session ID prepended to the SSH password.
  747. // Note: there's an even older case where clients don't send any session ID,
  748. // but that's no longer supported.
  749. if len(password) == expectedSessionIDLength+expectedSSHPasswordLength {
  750. sshPasswordPayload.SessionId = string(password[0:expectedSessionIDLength])
  751. sshPasswordPayload.SshPassword = string(password[expectedSSHPasswordLength:len(password)])
  752. } else {
  753. return nil, common.ContextError(fmt.Errorf("invalid password payload for %q", conn.User()))
  754. }
  755. }
  756. if !isHexDigits(sshClient.sshServer.support, sshPasswordPayload.SessionId) ||
  757. len(sshPasswordPayload.SessionId) != expectedSessionIDLength {
  758. return nil, common.ContextError(fmt.Errorf("invalid session ID for %q", conn.User()))
  759. }
  760. userOk := (subtle.ConstantTimeCompare(
  761. []byte(conn.User()), []byte(sshClient.sshServer.support.Config.SSHUserName)) == 1)
  762. passwordOk := (subtle.ConstantTimeCompare(
  763. []byte(sshPasswordPayload.SshPassword), []byte(sshClient.sshServer.support.Config.SSHPassword)) == 1)
  764. if !userOk || !passwordOk {
  765. return nil, common.ContextError(fmt.Errorf("invalid password for %q", conn.User()))
  766. }
  767. sessionID := sshPasswordPayload.SessionId
  768. supportsServerRequests := common.Contains(
  769. sshPasswordPayload.ClientCapabilities, protocol.CLIENT_CAPABILITY_SERVER_REQUESTS)
  770. sshClient.Lock()
  771. sshClient.sessionID = sessionID
  772. sshClient.supportsServerRequests = supportsServerRequests
  773. geoIPData := sshClient.geoIPData
  774. sshClient.Unlock()
  775. // Store the GeoIP data associated with the session ID. This makes
  776. // the GeoIP data available to the web server for web API requests.
  777. // A cache that's distinct from the sshClient record is used to allow
  778. // for or post-tunnel final status requests.
  779. // If the client is reconnecting with the same session ID, this call
  780. // will undo the expiry set by MarkSessionCacheToExpire.
  781. sshClient.sshServer.support.GeoIPService.SetSessionCache(sessionID, geoIPData)
  782. return nil, nil
  783. }
  784. func (sshClient *sshClient) authLogCallback(conn ssh.ConnMetadata, method string, err error) {
  785. if err != nil {
  786. if method == "none" && err.Error() == "no auth passed yet" {
  787. // In this case, the callback invocation is noise from auth negotiation
  788. return
  789. }
  790. // Note: here we previously logged messages for fail2ban to act on. This is no longer
  791. // done as the complexity outweighs the benefits.
  792. //
  793. // - The SSH credential is not secret -- it's in the server entry. Attackers targetting
  794. // the server likely already have the credential. On the other hand, random scanning and
  795. // brute forcing is mitigated with high entropy random passwords, rate limiting
  796. // (implemented on the host via iptables), and limited capabilities (the SSH session can
  797. // only port forward).
  798. //
  799. // - fail2ban coverage was inconsistent; in the case of an unfronted meek protocol through
  800. // an upstream proxy, the remote address is the upstream proxy, which should not be blocked.
  801. // The X-Forwarded-For header cant be used instead as it may be forged and used to get IPs
  802. // deliberately blocked; and in any case fail2ban adds iptables rules which can only block
  803. // by direct remote IP, not by original client IP. Fronted meek has the same iptables issue.
  804. //
  805. // TODO: random scanning and brute forcing of port 22 will result in log noise. To eliminate
  806. // this, and to also cover meek protocols, and bad obfuscation keys, and bad inputs to the web
  807. // server, consider implementing fail2ban-type logic directly in this server, with the ability
  808. // to use X-Forwarded-For (when trustworthy; e.g, from a CDN).
  809. log.WithContextFields(LogFields{"error": err, "method": method}).Warning("authentication failed")
  810. } else {
  811. log.WithContextFields(LogFields{"error": err, "method": method}).Debug("authentication success")
  812. }
  813. }
  814. // stop signals the ssh connection to shutdown. After sshConn() returns,
  815. // the connection has terminated but sshClient.run() may still be
  816. // running and in the process of exiting.
  817. func (sshClient *sshClient) stop() {
  818. sshClient.sshConn.Close()
  819. sshClient.sshConn.Wait()
  820. }
  821. // runTunnel handles/dispatches new channels and new requests from the client.
  822. // When the SSH client connection closes, both the channels and requests channels
  823. // will close and runTunnel will exit.
  824. func (sshClient *sshClient) runTunnel(
  825. channels <-chan ssh.NewChannel, requests <-chan *ssh.Request) {
  826. waitGroup := new(sync.WaitGroup)
  827. // Start client SSH API request handler
  828. waitGroup.Add(1)
  829. go func() {
  830. defer waitGroup.Done()
  831. for request := range requests {
  832. // Requests are processed serially; API responses must be sent in request order.
  833. var responsePayload []byte
  834. var err error
  835. if request.Type == "keepalive@openssh.com" {
  836. // Keepalive requests have an empty response.
  837. } else {
  838. // All other requests are assumed to be API requests.
  839. responsePayload, err = sshAPIRequestHandler(
  840. sshClient.sshServer.support,
  841. sshClient.geoIPData,
  842. request.Type,
  843. request.Payload)
  844. }
  845. if err == nil {
  846. err = request.Reply(true, responsePayload)
  847. } else {
  848. log.WithContextFields(LogFields{"error": err}).Warning("request failed")
  849. err = request.Reply(false, nil)
  850. }
  851. if err != nil {
  852. log.WithContextFields(LogFields{"error": err}).Warning("response failed")
  853. }
  854. }
  855. }()
  856. // Start OSL sender
  857. if sshClient.supportsServerRequests {
  858. waitGroup.Add(1)
  859. go func() {
  860. defer waitGroup.Done()
  861. sshClient.runOSLSender()
  862. }()
  863. }
  864. // Lifecycle of a TCP port forward:
  865. //
  866. // 1. A "direct-tcpip" SSH request is received from the client.
  867. //
  868. // A new TCP port forward request is enqueued. The queue delivers TCP port
  869. // forward requests to the TCP port forward manager, which enforces the TCP
  870. // port forward dial limit.
  871. //
  872. // Enqueuing new requests allows for reading further SSH requests from the
  873. // client without blocking when the dial limit is hit; this is to permit new
  874. // UDP/udpgw port forwards to be restablished without delay. The maximum size
  875. // of the queue enforces a hard cap on resources consumed by a client in the
  876. // pre-dial phase. When the queue is full, new TCP port forwards are
  877. // immediately rejected.
  878. //
  879. // 2. The TCP port forward manager dequeues the request.
  880. //
  881. // The manager calls dialingTCPPortForward(), which increments
  882. // concurrentDialingPortForwardCount, and calls
  883. // isTCPDialingPortForwardLimitExceeded() to check the concurrent dialing
  884. // count.
  885. //
  886. // The manager enforces the concurrent TCP dial limit: when at the limit, the
  887. // manager blocks waiting for the number of dials to drop below the limit before
  888. // dispatching the request to handleTCPPortForward(), which will run in its own
  889. // goroutine and will dial and relay the port forward.
  890. //
  891. // The block delays the current request and also halts dequeuing of subsequent
  892. // requests and could ultimately cause requests to be immediately rejected if
  893. // the queue fills. These actions are intended to apply back pressure when
  894. // upstream network resources are impaired.
  895. //
  896. // The time spent in the queue is deducted from the port forward's dial timeout.
  897. // The time spent blocking while at the dial limit is similarly deducted from
  898. // the dial timeout. If the dial timeout has expired before the dial begins, the
  899. // port forward is rejected and a stat is recorded.
  900. //
  901. // 3. handleTCPPortForward() performs the port forward dial and relaying.
  902. //
  903. // a. Dial the target, using the dial timeout remaining after queue and blocking
  904. // time is deducted.
  905. //
  906. // b. If the dial fails, call abortedTCPPortForward() to decrement
  907. // concurrentDialingPortForwardCount, freeing up a dial slot.
  908. //
  909. // c. If the dial succeeds, call establishedPortForward(), which decrements
  910. // concurrentDialingPortForwardCount and increments concurrentPortForwardCount,
  911. // the "established" port forward count.
  912. //
  913. // d. Check isPortForwardLimitExceeded(), which enforces the configurable limit on
  914. // concurrentPortForwardCount, the number of _established_ TCP port forwards.
  915. // If the limit is exceeded, the LRU established TCP port forward is closed and
  916. // the newly established TCP port forward proceeds. This LRU logic allows some
  917. // dangling resource consumption (e.g., TIME_WAIT) while providing a better
  918. // experience for clients.
  919. //
  920. // e. Relay data.
  921. //
  922. // f. Call closedPortForward() which decrements concurrentPortForwardCount and
  923. // records bytes transferred.
  924. // Start the TCP port forward manager
  925. type newTCPPortForward struct {
  926. enqueueTime monotime.Time
  927. hostToConnect string
  928. portToConnect int
  929. newChannel ssh.NewChannel
  930. }
  931. // The queue size is set to the traffic rules (MaxTCPPortForwardCount +
  932. // MaxTCPDialingPortForwardCount), which is a reasonable indication of resource
  933. // limits per client; when that value is not set, a default is used.
  934. // A limitation: this queue size is set once and doesn't change, for this client,
  935. // when traffic rules are reloaded.
  936. queueSize := sshClient.getTCPPortForwardQueueSize()
  937. if queueSize == 0 {
  938. queueSize = SSH_TCP_PORT_FORWARD_QUEUE_SIZE
  939. }
  940. newTCPPortForwards := make(chan *newTCPPortForward, queueSize)
  941. waitGroup.Add(1)
  942. go func() {
  943. defer waitGroup.Done()
  944. for newPortForward := range newTCPPortForwards {
  945. remainingDialTimeout :=
  946. time.Duration(sshClient.getDialTCPPortForwardTimeoutMilliseconds())*time.Millisecond -
  947. monotime.Since(newPortForward.enqueueTime)
  948. if remainingDialTimeout <= 0 {
  949. sshClient.updateQualityMetricsWithRejectedDialingLimit()
  950. sshClient.rejectNewChannel(
  951. newPortForward.newChannel, ssh.Prohibited, "TCP port forward timed out in queue")
  952. continue
  953. }
  954. // Reserve a TCP dialing slot.
  955. //
  956. // TOCTOU note: important to increment counts _before_ checking limits; otherwise,
  957. // the client could potentially consume excess resources by initiating many port
  958. // forwards concurrently.
  959. sshClient.dialingTCPPortForward()
  960. // When max dials are in progress, wait up to remainingDialTimeout for dialing
  961. // to become available. This blocks all dequeing.
  962. if sshClient.isTCPDialingPortForwardLimitExceeded() {
  963. blockStartTime := monotime.Now()
  964. ctx, cancelFunc := context.WithTimeout(sshClient.runContext, remainingDialTimeout)
  965. sshClient.setTCPPortForwardDialingAvailableSignal(cancelFunc)
  966. <-ctx.Done()
  967. sshClient.setTCPPortForwardDialingAvailableSignal(nil)
  968. remainingDialTimeout -= monotime.Since(blockStartTime)
  969. }
  970. if remainingDialTimeout <= 0 {
  971. // Release the dialing slot here since handleTCPChannel() won't be called.
  972. sshClient.abortedTCPPortForward()
  973. sshClient.updateQualityMetricsWithRejectedDialingLimit()
  974. sshClient.rejectNewChannel(
  975. newPortForward.newChannel, ssh.Prohibited, "TCP port forward timed out before dialing")
  976. continue
  977. }
  978. // Dial and relay the TCP port forward. handleTCPChannel is run in its own worker goroutine.
  979. // handleTCPChannel will release the dialing slot reserved by dialingTCPPortForward(); and
  980. // will deal with remainingDialTimeout <= 0.
  981. waitGroup.Add(1)
  982. go func(remainingDialTimeout time.Duration, newPortForward *newTCPPortForward) {
  983. defer waitGroup.Done()
  984. sshClient.handleTCPChannel(
  985. remainingDialTimeout,
  986. newPortForward.hostToConnect,
  987. newPortForward.portToConnect,
  988. newPortForward.newChannel)
  989. }(remainingDialTimeout, newPortForward)
  990. }
  991. }()
  992. // Handle new channel (port forward) requests from the client.
  993. //
  994. // udpgw client connections are dispatched immediately (clients use this for
  995. // DNS, so it's essential to not block; and only one udpgw connection is
  996. // retained at a time).
  997. //
  998. // All other TCP port forwards are dispatched via the TCP port forward
  999. // manager queue.
  1000. for newChannel := range channels {
  1001. if newChannel.ChannelType() != "direct-tcpip" {
  1002. sshClient.rejectNewChannel(newChannel, ssh.Prohibited, "unknown or unsupported channel type")
  1003. continue
  1004. }
  1005. // http://tools.ietf.org/html/rfc4254#section-7.2
  1006. var directTcpipExtraData struct {
  1007. HostToConnect string
  1008. PortToConnect uint32
  1009. OriginatorIPAddress string
  1010. OriginatorPort uint32
  1011. }
  1012. err := ssh.Unmarshal(newChannel.ExtraData(), &directTcpipExtraData)
  1013. if err != nil {
  1014. sshClient.rejectNewChannel(newChannel, ssh.Prohibited, "invalid extra data")
  1015. continue
  1016. }
  1017. // Intercept TCP port forwards to a specified udpgw server and handle directly.
  1018. // TODO: also support UDP explicitly, e.g. with a custom "direct-udp" channel type?
  1019. isUDPChannel := sshClient.sshServer.support.Config.UDPInterceptUdpgwServerAddress != "" &&
  1020. sshClient.sshServer.support.Config.UDPInterceptUdpgwServerAddress ==
  1021. net.JoinHostPort(directTcpipExtraData.HostToConnect, strconv.Itoa(int(directTcpipExtraData.PortToConnect)))
  1022. if isUDPChannel {
  1023. // Dispatch immediately. handleUDPChannel runs the udpgw protocol in its
  1024. // own worker goroutine.
  1025. waitGroup.Add(1)
  1026. go func(channel ssh.NewChannel) {
  1027. defer waitGroup.Done()
  1028. sshClient.handleUDPChannel(channel)
  1029. }(newChannel)
  1030. } else {
  1031. // Dispatch via TCP port forward manager. When the queue is full, the channel
  1032. // is immediately rejected.
  1033. tcpPortForward := &newTCPPortForward{
  1034. enqueueTime: monotime.Now(),
  1035. hostToConnect: directTcpipExtraData.HostToConnect,
  1036. portToConnect: int(directTcpipExtraData.PortToConnect),
  1037. newChannel: newChannel,
  1038. }
  1039. select {
  1040. case newTCPPortForwards <- tcpPortForward:
  1041. default:
  1042. sshClient.updateQualityMetricsWithRejectedDialingLimit()
  1043. sshClient.rejectNewChannel(newChannel, ssh.Prohibited, "TCP port forward dial queue full")
  1044. }
  1045. }
  1046. }
  1047. // The channel loop is interrupted by a client
  1048. // disconnect or by calling sshClient.stop().
  1049. // Stop the TCP port forward manager
  1050. close(newTCPPortForwards)
  1051. // Stop all other worker goroutines
  1052. sshClient.stopRunning()
  1053. waitGroup.Wait()
  1054. }
  1055. func (sshClient *sshClient) logTunnel() {
  1056. // Note: reporting duration based on last confirmed data transfer, which
  1057. // is reads for sshClient.activityConn.GetActiveDuration(), and not
  1058. // connection closing is important for protocols such as meek. For
  1059. // meek, the connection remains open until the HTTP session expires,
  1060. // which may be some time after the tunnel has closed. (The meek
  1061. // protocol has no allowance for signalling payload EOF, and even if
  1062. // it did the client may not have the opportunity to send a final
  1063. // request with an EOF flag set.)
  1064. sshClient.Lock()
  1065. logFields := getRequestLogFields(
  1066. sshClient.sshServer.support,
  1067. "server_tunnel",
  1068. sshClient.geoIPData,
  1069. sshClient.handshakeState.apiParams,
  1070. baseRequestParams)
  1071. logFields["handshake_completed"] = sshClient.handshakeState.completed
  1072. logFields["start_time"] = sshClient.activityConn.GetStartTime()
  1073. logFields["duration"] = sshClient.activityConn.GetActiveDuration() / time.Millisecond
  1074. logFields["bytes_up_tcp"] = sshClient.tcpTrafficState.bytesUp
  1075. logFields["bytes_down_tcp"] = sshClient.tcpTrafficState.bytesDown
  1076. logFields["peak_concurrent_dialing_port_forward_count_tcp"] = sshClient.tcpTrafficState.peakConcurrentDialingPortForwardCount
  1077. logFields["peak_concurrent_port_forward_count_tcp"] = sshClient.tcpTrafficState.peakConcurrentPortForwardCount
  1078. logFields["total_port_forward_count_tcp"] = sshClient.tcpTrafficState.totalPortForwardCount
  1079. logFields["bytes_up_udp"] = sshClient.udpTrafficState.bytesUp
  1080. logFields["bytes_down_udp"] = sshClient.udpTrafficState.bytesDown
  1081. // sshClient.udpTrafficState.peakConcurrentDialingPortForwardCount isn't meaningful
  1082. logFields["peak_concurrent_port_forward_count_udp"] = sshClient.udpTrafficState.peakConcurrentPortForwardCount
  1083. logFields["total_port_forward_count_udp"] = sshClient.udpTrafficState.totalPortForwardCount
  1084. sshClient.Unlock()
  1085. log.LogRawFieldsWithTimestamp(logFields)
  1086. }
  1087. func (sshClient *sshClient) runOSLSender() {
  1088. for {
  1089. // Await a signal that there are SLOKs to send
  1090. // TODO: use reflect.SelectCase, and optionally await timer here?
  1091. select {
  1092. case <-sshClient.signalIssueSLOKs:
  1093. case <-sshClient.runContext.Done():
  1094. return
  1095. }
  1096. retryDelay := SSH_SEND_OSL_INITIAL_RETRY_DELAY
  1097. for {
  1098. err := sshClient.sendOSLRequest()
  1099. if err == nil {
  1100. break
  1101. }
  1102. log.WithContextFields(LogFields{"error": err}).Warning("sendOSLRequest failed")
  1103. // If the request failed, retry after a delay (with exponential backoff)
  1104. // or when signaled that there are additional SLOKs to send
  1105. retryTimer := time.NewTimer(retryDelay)
  1106. select {
  1107. case <-retryTimer.C:
  1108. case <-sshClient.signalIssueSLOKs:
  1109. case <-sshClient.runContext.Done():
  1110. retryTimer.Stop()
  1111. return
  1112. }
  1113. retryTimer.Stop()
  1114. retryDelay *= SSH_SEND_OSL_RETRY_FACTOR
  1115. }
  1116. }
  1117. }
  1118. // sendOSLRequest will invoke osl.GetSeedPayload to issue SLOKs and
  1119. // generate a payload, and send an OSL request to the client when
  1120. // there are new SLOKs in the payload.
  1121. func (sshClient *sshClient) sendOSLRequest() error {
  1122. seedPayload := sshClient.getOSLSeedPayload()
  1123. // Don't send when no SLOKs. This will happen when signalIssueSLOKs
  1124. // is received but no new SLOKs are issued.
  1125. if len(seedPayload.SLOKs) == 0 {
  1126. return nil
  1127. }
  1128. oslRequest := protocol.OSLRequest{
  1129. SeedPayload: seedPayload,
  1130. }
  1131. requestPayload, err := json.Marshal(oslRequest)
  1132. if err != nil {
  1133. return common.ContextError(err)
  1134. }
  1135. ok, _, err := sshClient.sshConn.SendRequest(
  1136. protocol.PSIPHON_API_OSL_REQUEST_NAME,
  1137. true,
  1138. requestPayload)
  1139. if err != nil {
  1140. return common.ContextError(err)
  1141. }
  1142. if !ok {
  1143. return common.ContextError(errors.New("client rejected request"))
  1144. }
  1145. sshClient.clearOSLSeedPayload()
  1146. return nil
  1147. }
  1148. func (sshClient *sshClient) rejectNewChannel(newChannel ssh.NewChannel, reason ssh.RejectionReason, logMessage string) {
  1149. // Note: Debug level, as logMessage may contain user traffic destination address information
  1150. log.WithContextFields(
  1151. LogFields{
  1152. "channelType": newChannel.ChannelType(),
  1153. "logMessage": logMessage,
  1154. "rejectReason": reason.String(),
  1155. }).Debug("reject new channel")
  1156. // Note: logMessage is internal, for logging only; just the RejectionReason is sent to the client
  1157. newChannel.Reject(reason, reason.String())
  1158. }
  1159. // setHandshakeState records that a client has completed a handshake API request.
  1160. // Some parameters from the handshake request may be used in future traffic rule
  1161. // selection. Port forwards are disallowed until a handshake is complete. The
  1162. // handshake parameters are included in the session summary log recorded in
  1163. // sshClient.stop().
  1164. func (sshClient *sshClient) setHandshakeState(state handshakeState) error {
  1165. sshClient.Lock()
  1166. completed := sshClient.handshakeState.completed
  1167. if !completed {
  1168. sshClient.handshakeState = state
  1169. }
  1170. sshClient.Unlock()
  1171. // Client must only perform one handshake
  1172. if completed {
  1173. return common.ContextError(errors.New("handshake already completed"))
  1174. }
  1175. sshClient.setTrafficRules()
  1176. sshClient.setOSLConfig()
  1177. return nil
  1178. }
  1179. // setTrafficRules resets the client's traffic rules based on the latest server config
  1180. // and client properties. As sshClient.trafficRules may be reset by a concurrent
  1181. // goroutine, trafficRules must only be accessed within the sshClient mutex.
  1182. func (sshClient *sshClient) setTrafficRules() {
  1183. sshClient.Lock()
  1184. defer sshClient.Unlock()
  1185. sshClient.trafficRules = sshClient.sshServer.support.TrafficRulesSet.GetTrafficRules(
  1186. sshClient.tunnelProtocol, sshClient.geoIPData, sshClient.handshakeState)
  1187. if sshClient.throttledConn != nil {
  1188. // Any existing throttling state is reset.
  1189. sshClient.throttledConn.SetLimits(
  1190. sshClient.trafficRules.RateLimits.CommonRateLimits())
  1191. }
  1192. }
  1193. // setOSLConfig resets the client's OSL seed state based on the latest OSL config
  1194. // As sshClient.oslClientSeedState may be reset by a concurrent goroutine,
  1195. // oslClientSeedState must only be accessed within the sshClient mutex.
  1196. func (sshClient *sshClient) setOSLConfig() {
  1197. sshClient.Lock()
  1198. defer sshClient.Unlock()
  1199. propagationChannelID, err := getStringRequestParam(
  1200. sshClient.handshakeState.apiParams, "propagation_channel_id")
  1201. if err != nil {
  1202. // This should not fail as long as client has sent valid handshake
  1203. return
  1204. }
  1205. // Use a cached seed state if one is found for the client's
  1206. // session ID. This enables resuming progress made in a previous
  1207. // tunnel.
  1208. // Note: go-cache is already concurency safe; the additional mutex
  1209. // is necessary to guarantee that Get/Delete is atomic; although in
  1210. // practice no two concurrent clients should ever supply the same
  1211. // session ID.
  1212. sshClient.sshServer.oslSessionCacheMutex.Lock()
  1213. oslClientSeedState, found := sshClient.sshServer.oslSessionCache.Get(sshClient.sessionID)
  1214. if found {
  1215. sshClient.sshServer.oslSessionCache.Delete(sshClient.sessionID)
  1216. sshClient.sshServer.oslSessionCacheMutex.Unlock()
  1217. sshClient.oslClientSeedState = oslClientSeedState.(*osl.ClientSeedState)
  1218. sshClient.oslClientSeedState.Resume(sshClient.signalIssueSLOKs)
  1219. return
  1220. }
  1221. sshClient.sshServer.oslSessionCacheMutex.Unlock()
  1222. // Two limitations when setOSLConfig() is invoked due to an
  1223. // OSL config hot reload:
  1224. //
  1225. // 1. any partial progress towards SLOKs is lost.
  1226. //
  1227. // 2. all existing osl.ClientSeedPortForwards for existing
  1228. // port forwards will not send progress to the new client
  1229. // seed state.
  1230. sshClient.oslClientSeedState = sshClient.sshServer.support.OSLConfig.NewClientSeedState(
  1231. sshClient.geoIPData.Country,
  1232. propagationChannelID,
  1233. sshClient.signalIssueSLOKs)
  1234. }
  1235. // newClientSeedPortForward will return nil when no seeding is
  1236. // associated with the specified ipAddress.
  1237. func (sshClient *sshClient) newClientSeedPortForward(ipAddress net.IP) *osl.ClientSeedPortForward {
  1238. sshClient.Lock()
  1239. defer sshClient.Unlock()
  1240. // Will not be initialized before handshake.
  1241. if sshClient.oslClientSeedState == nil {
  1242. return nil
  1243. }
  1244. return sshClient.oslClientSeedState.NewClientSeedPortForward(ipAddress)
  1245. }
  1246. // getOSLSeedPayload returns a payload containing all seeded SLOKs for
  1247. // this client's session.
  1248. func (sshClient *sshClient) getOSLSeedPayload() *osl.SeedPayload {
  1249. sshClient.Lock()
  1250. defer sshClient.Unlock()
  1251. // Will not be initialized before handshake.
  1252. if sshClient.oslClientSeedState == nil {
  1253. return &osl.SeedPayload{SLOKs: make([]*osl.SLOK, 0)}
  1254. }
  1255. return sshClient.oslClientSeedState.GetSeedPayload()
  1256. }
  1257. func (sshClient *sshClient) clearOSLSeedPayload() {
  1258. sshClient.Lock()
  1259. defer sshClient.Unlock()
  1260. sshClient.oslClientSeedState.ClearSeedPayload()
  1261. }
  1262. func (sshClient *sshClient) rateLimits() common.RateLimits {
  1263. sshClient.Lock()
  1264. defer sshClient.Unlock()
  1265. return sshClient.trafficRules.RateLimits.CommonRateLimits()
  1266. }
  1267. func (sshClient *sshClient) idleTCPPortForwardTimeout() time.Duration {
  1268. sshClient.Lock()
  1269. defer sshClient.Unlock()
  1270. return time.Duration(*sshClient.trafficRules.IdleTCPPortForwardTimeoutMilliseconds) * time.Millisecond
  1271. }
  1272. func (sshClient *sshClient) idleUDPPortForwardTimeout() time.Duration {
  1273. sshClient.Lock()
  1274. defer sshClient.Unlock()
  1275. return time.Duration(*sshClient.trafficRules.IdleUDPPortForwardTimeoutMilliseconds) * time.Millisecond
  1276. }
  1277. func (sshClient *sshClient) setTCPPortForwardDialingAvailableSignal(signal context.CancelFunc) {
  1278. sshClient.Lock()
  1279. defer sshClient.Unlock()
  1280. sshClient.tcpPortForwardDialingAvailableSignal = signal
  1281. }
  1282. const (
  1283. portForwardTypeTCP = iota
  1284. portForwardTypeUDP
  1285. portForwardTypeTransparentDNS
  1286. )
  1287. func (sshClient *sshClient) isPortForwardPermitted(
  1288. portForwardType int,
  1289. isTransparentDNSForwarding bool,
  1290. remoteIP net.IP,
  1291. port int) bool {
  1292. sshClient.Lock()
  1293. defer sshClient.Unlock()
  1294. if !sshClient.handshakeState.completed {
  1295. return false
  1296. }
  1297. // Disallow connection to loopback. This is a failsafe. The server
  1298. // should be run on a host with correctly configured firewall rules.
  1299. // And exception is made in the case of tranparent DNS forwarding,
  1300. // where the remoteIP has been rewritten.
  1301. if !isTransparentDNSForwarding && remoteIP.IsLoopback() {
  1302. return false
  1303. }
  1304. var allowPorts []int
  1305. if portForwardType == portForwardTypeTCP {
  1306. allowPorts = sshClient.trafficRules.AllowTCPPorts
  1307. } else {
  1308. allowPorts = sshClient.trafficRules.AllowUDPPorts
  1309. }
  1310. if len(allowPorts) == 0 {
  1311. return true
  1312. }
  1313. // TODO: faster lookup?
  1314. if len(allowPorts) > 0 {
  1315. for _, allowPort := range allowPorts {
  1316. if port == allowPort {
  1317. return true
  1318. }
  1319. }
  1320. }
  1321. for _, subnet := range sshClient.trafficRules.AllowSubnets {
  1322. // Note: ignoring error as config has been validated
  1323. _, network, _ := net.ParseCIDR(subnet)
  1324. if network.Contains(remoteIP) {
  1325. return true
  1326. }
  1327. }
  1328. return false
  1329. }
  1330. func (sshClient *sshClient) isTCPDialingPortForwardLimitExceeded() bool {
  1331. sshClient.Lock()
  1332. defer sshClient.Unlock()
  1333. state := &sshClient.tcpTrafficState
  1334. max := *sshClient.trafficRules.MaxTCPDialingPortForwardCount
  1335. if max > 0 && state.concurrentDialingPortForwardCount >= int64(max) {
  1336. return true
  1337. }
  1338. return false
  1339. }
  1340. func (sshClient *sshClient) isPortForwardLimitExceeded(
  1341. portForwardType int) bool {
  1342. sshClient.Lock()
  1343. defer sshClient.Unlock()
  1344. var max int
  1345. var state *trafficState
  1346. if portForwardType == portForwardTypeTCP {
  1347. max = *sshClient.trafficRules.MaxTCPPortForwardCount
  1348. state = &sshClient.tcpTrafficState
  1349. } else {
  1350. max = *sshClient.trafficRules.MaxUDPPortForwardCount
  1351. state = &sshClient.udpTrafficState
  1352. }
  1353. if max > 0 && state.concurrentPortForwardCount >= int64(max) {
  1354. return true
  1355. }
  1356. return false
  1357. }
  1358. func (sshClient *sshClient) getTCPPortForwardQueueSize() int {
  1359. sshClient.Lock()
  1360. defer sshClient.Unlock()
  1361. return *sshClient.trafficRules.MaxTCPPortForwardCount +
  1362. *sshClient.trafficRules.MaxTCPDialingPortForwardCount
  1363. }
  1364. func (sshClient *sshClient) getDialTCPPortForwardTimeoutMilliseconds() int {
  1365. sshClient.Lock()
  1366. defer sshClient.Unlock()
  1367. return *sshClient.trafficRules.DialTCPPortForwardTimeoutMilliseconds
  1368. }
  1369. func (sshClient *sshClient) dialingTCPPortForward() {
  1370. sshClient.Lock()
  1371. defer sshClient.Unlock()
  1372. state := &sshClient.tcpTrafficState
  1373. state.concurrentDialingPortForwardCount += 1
  1374. if state.concurrentDialingPortForwardCount > state.peakConcurrentDialingPortForwardCount {
  1375. state.peakConcurrentDialingPortForwardCount = state.concurrentDialingPortForwardCount
  1376. }
  1377. }
  1378. func (sshClient *sshClient) abortedTCPPortForward() {
  1379. sshClient.Lock()
  1380. defer sshClient.Unlock()
  1381. sshClient.tcpTrafficState.concurrentDialingPortForwardCount -= 1
  1382. }
  1383. func (sshClient *sshClient) establishedPortForward(
  1384. portForwardType int) {
  1385. sshClient.Lock()
  1386. defer sshClient.Unlock()
  1387. var state *trafficState
  1388. if portForwardType == portForwardTypeTCP {
  1389. state = &sshClient.tcpTrafficState
  1390. // Assumes TCP port forwards called dialingTCPPortForward
  1391. state.concurrentDialingPortForwardCount -= 1
  1392. if sshClient.tcpPortForwardDialingAvailableSignal != nil {
  1393. max := *sshClient.trafficRules.MaxTCPDialingPortForwardCount
  1394. if max <= 0 || state.concurrentDialingPortForwardCount < int64(max) {
  1395. sshClient.tcpPortForwardDialingAvailableSignal()
  1396. }
  1397. }
  1398. } else {
  1399. state = &sshClient.udpTrafficState
  1400. }
  1401. state.concurrentPortForwardCount += 1
  1402. if state.concurrentPortForwardCount > state.peakConcurrentPortForwardCount {
  1403. state.peakConcurrentPortForwardCount = state.concurrentPortForwardCount
  1404. }
  1405. state.totalPortForwardCount += 1
  1406. }
  1407. func (sshClient *sshClient) closedPortForward(
  1408. portForwardType int, bytesUp, bytesDown int64) {
  1409. sshClient.Lock()
  1410. defer sshClient.Unlock()
  1411. var state *trafficState
  1412. if portForwardType == portForwardTypeTCP {
  1413. state = &sshClient.tcpTrafficState
  1414. } else {
  1415. state = &sshClient.udpTrafficState
  1416. }
  1417. state.concurrentPortForwardCount -= 1
  1418. state.bytesUp += bytesUp
  1419. state.bytesDown += bytesDown
  1420. }
  1421. func (sshClient *sshClient) updateQualityMetricsWithDialResult(
  1422. tcpPortForwardDialSuccess bool, dialDuration time.Duration) {
  1423. sshClient.Lock()
  1424. defer sshClient.Unlock()
  1425. if tcpPortForwardDialSuccess {
  1426. sshClient.qualityMetrics.tcpPortForwardDialedCount += 1
  1427. sshClient.qualityMetrics.tcpPortForwardDialedDuration += dialDuration
  1428. } else {
  1429. sshClient.qualityMetrics.tcpPortForwardFailedCount += 1
  1430. sshClient.qualityMetrics.tcpPortForwardFailedDuration += dialDuration
  1431. }
  1432. }
  1433. func (sshClient *sshClient) updateQualityMetricsWithRejectedDialingLimit() {
  1434. sshClient.Lock()
  1435. defer sshClient.Unlock()
  1436. sshClient.qualityMetrics.tcpPortForwardRejectedDialingLimitCount += 1
  1437. }
  1438. func (sshClient *sshClient) handleTCPChannel(
  1439. remainingDialTimeout time.Duration,
  1440. hostToConnect string,
  1441. portToConnect int,
  1442. newChannel ssh.NewChannel) {
  1443. // Assumptions:
  1444. // - sshClient.dialingTCPPortForward() has been called
  1445. // - remainingDialTimeout > 0
  1446. established := false
  1447. defer func() {
  1448. if !established {
  1449. sshClient.abortedTCPPortForward()
  1450. }
  1451. }()
  1452. // Transparently redirect web API request connections.
  1453. isWebServerPortForward := false
  1454. config := sshClient.sshServer.support.Config
  1455. if config.WebServerPortForwardAddress != "" {
  1456. destination := net.JoinHostPort(hostToConnect, strconv.Itoa(portToConnect))
  1457. if destination == config.WebServerPortForwardAddress {
  1458. isWebServerPortForward = true
  1459. if config.WebServerPortForwardRedirectAddress != "" {
  1460. // Note: redirect format is validated when config is loaded
  1461. host, portStr, _ := net.SplitHostPort(config.WebServerPortForwardRedirectAddress)
  1462. port, _ := strconv.Atoi(portStr)
  1463. hostToConnect = host
  1464. portToConnect = port
  1465. }
  1466. }
  1467. }
  1468. // Dial the remote address.
  1469. //
  1470. // Hostname resolution is performed explicitly, as a seperate step, as the target IP
  1471. // address is used for traffic rules (AllowSubnets) and OSL seed progress.
  1472. //
  1473. // Contexts are used for cancellation (via sshClient.runContext, which is cancelled
  1474. // when the client is stopping) and timeouts.
  1475. dialStartTime := monotime.Now()
  1476. log.WithContextFields(LogFields{"hostToConnect": hostToConnect}).Debug("resolving")
  1477. ctx, _ := context.WithTimeout(sshClient.runContext, remainingDialTimeout)
  1478. IPs, err := (&net.Resolver{}).LookupIPAddr(ctx, hostToConnect)
  1479. // TODO: shuffle list to try other IPs?
  1480. // TODO: IPv6 support
  1481. var IP net.IP
  1482. for _, ip := range IPs {
  1483. if ip.IP.To4() != nil {
  1484. IP = ip.IP
  1485. break
  1486. }
  1487. }
  1488. if err == nil && IP == nil {
  1489. err = errors.New("no IP address")
  1490. }
  1491. resolveElapsedTime := monotime.Since(dialStartTime)
  1492. if err != nil {
  1493. // Record a port forward failure
  1494. sshClient.updateQualityMetricsWithDialResult(true, resolveElapsedTime)
  1495. sshClient.rejectNewChannel(
  1496. newChannel, ssh.ConnectionFailed, fmt.Sprintf("LookupIP failed: %s", err))
  1497. return
  1498. }
  1499. remainingDialTimeout -= resolveElapsedTime
  1500. if remainingDialTimeout <= 0 {
  1501. sshClient.rejectNewChannel(
  1502. newChannel, ssh.Prohibited, "TCP port forward timed out resolving")
  1503. return
  1504. }
  1505. // Enforce traffic rules, using the resolved IP address.
  1506. if !isWebServerPortForward &&
  1507. !sshClient.isPortForwardPermitted(
  1508. portForwardTypeTCP,
  1509. false,
  1510. IP,
  1511. portToConnect) {
  1512. // Note: not recording a port forward failure in this case
  1513. sshClient.rejectNewChannel(
  1514. newChannel, ssh.Prohibited, "port forward not permitted")
  1515. return
  1516. }
  1517. // TCP dial.
  1518. remoteAddr := net.JoinHostPort(IP.String(), strconv.Itoa(portToConnect))
  1519. log.WithContextFields(LogFields{"remoteAddr": remoteAddr}).Debug("dialing")
  1520. ctx, _ = context.WithTimeout(sshClient.runContext, remainingDialTimeout)
  1521. fwdConn, err := (&net.Dialer{}).DialContext(ctx, "tcp", remoteAddr)
  1522. // Record port forward success or failure
  1523. sshClient.updateQualityMetricsWithDialResult(err == nil, monotime.Since(dialStartTime))
  1524. if err != nil {
  1525. // Monitor for low resource error conditions
  1526. sshClient.sshServer.monitorPortForwardDialError(err)
  1527. sshClient.rejectNewChannel(
  1528. newChannel, ssh.ConnectionFailed, fmt.Sprintf("DialTimeout failed: %s", err))
  1529. return
  1530. }
  1531. // The upstream TCP port forward connection has been established. Schedule
  1532. // some cleanup and notify the SSH client that the channel is accepted.
  1533. defer fwdConn.Close()
  1534. fwdChannel, requests, err := newChannel.Accept()
  1535. if err != nil {
  1536. log.WithContextFields(LogFields{"error": err}).Warning("accept new channel failed")
  1537. return
  1538. }
  1539. go ssh.DiscardRequests(requests)
  1540. defer fwdChannel.Close()
  1541. // Release the dialing slot and acquire an established slot.
  1542. // "established = true" cancels the deferred abortedTCPPortForward()
  1543. established = true
  1544. sshClient.establishedPortForward(portForwardTypeTCP)
  1545. var bytesUp, bytesDown int64
  1546. defer func() {
  1547. sshClient.closedPortForward(
  1548. portForwardTypeTCP, atomic.LoadInt64(&bytesUp), atomic.LoadInt64(&bytesDown))
  1549. }()
  1550. if exceeded := sshClient.isPortForwardLimitExceeded(portForwardTypeTCP); exceeded {
  1551. // Close the oldest TCP port forward. CloseOldest() closes
  1552. // the conn and the port forward's goroutines will complete
  1553. // the cleanup asynchronously.
  1554. //
  1555. // Some known limitations:
  1556. //
  1557. // - Since CloseOldest() closes the upstream socket but does not
  1558. // clean up all resources associated with the port forward. These
  1559. // include the goroutine(s) relaying traffic as well as the SSH
  1560. // channel. Closing the socket will interrupt the goroutines which
  1561. // will then complete the cleanup. But, since the full cleanup is
  1562. // asynchronous, there exists a possibility that a client can consume
  1563. // more than max port forward resources -- just not upstream sockets.
  1564. //
  1565. // - Closed sockets will enter the TIME_WAIT state, consuming some
  1566. // resources.
  1567. sshClient.tcpPortForwardLRU.CloseOldest()
  1568. log.WithContext().Debug("closed LRU TCP port forward")
  1569. }
  1570. lruEntry := sshClient.tcpPortForwardLRU.Add(fwdConn)
  1571. defer lruEntry.Remove()
  1572. // ActivityMonitoredConn monitors the TCP port forward I/O and updates
  1573. // its LRU status. ActivityMonitoredConn also times out I/O on the port
  1574. // forward if both reads and writes have been idle for the specified
  1575. // duration.
  1576. // Ensure nil interface if newClientSeedPortForward returns nil
  1577. var updater common.ActivityUpdater
  1578. seedUpdater := sshClient.newClientSeedPortForward(IP)
  1579. if seedUpdater != nil {
  1580. updater = seedUpdater
  1581. }
  1582. fwdConn, err = common.NewActivityMonitoredConn(
  1583. fwdConn,
  1584. sshClient.idleTCPPortForwardTimeout(),
  1585. true,
  1586. updater,
  1587. lruEntry)
  1588. if err != nil {
  1589. log.WithContextFields(LogFields{"error": err}).Error("NewActivityMonitoredConn failed")
  1590. return
  1591. }
  1592. // Relay channel to forwarded connection.
  1593. log.WithContextFields(LogFields{"remoteAddr": remoteAddr}).Debug("relaying")
  1594. // TODO: relay errors to fwdChannel.Stderr()?
  1595. relayWaitGroup := new(sync.WaitGroup)
  1596. relayWaitGroup.Add(1)
  1597. go func() {
  1598. defer relayWaitGroup.Done()
  1599. // io.Copy allocates a 32K temporary buffer, and each port forward relay uses
  1600. // two of these buffers; using io.CopyBuffer with a smaller buffer reduces the
  1601. // overall memory footprint.
  1602. bytes, err := io.CopyBuffer(
  1603. fwdChannel, fwdConn, make([]byte, SSH_TCP_PORT_FORWARD_COPY_BUFFER_SIZE))
  1604. atomic.AddInt64(&bytesDown, bytes)
  1605. if err != nil && err != io.EOF {
  1606. // Debug since errors such as "connection reset by peer" occur during normal operation
  1607. log.WithContextFields(LogFields{"error": err}).Debug("downstream TCP relay failed")
  1608. }
  1609. // Interrupt upstream io.Copy when downstream is shutting down.
  1610. // TODO: this is done to quickly cleanup the port forward when
  1611. // fwdConn has a read timeout, but is it clean -- upstream may still
  1612. // be flowing?
  1613. fwdChannel.Close()
  1614. }()
  1615. bytes, err := io.CopyBuffer(
  1616. fwdConn, fwdChannel, make([]byte, SSH_TCP_PORT_FORWARD_COPY_BUFFER_SIZE))
  1617. atomic.AddInt64(&bytesUp, bytes)
  1618. if err != nil && err != io.EOF {
  1619. log.WithContextFields(LogFields{"error": err}).Debug("upstream TCP relay failed")
  1620. }
  1621. // Shutdown special case: fwdChannel will be closed and return EOF when
  1622. // the SSH connection is closed, but we need to explicitly close fwdConn
  1623. // to interrupt the downstream io.Copy, which may be blocked on a
  1624. // fwdConn.Read().
  1625. fwdConn.Close()
  1626. relayWaitGroup.Wait()
  1627. log.WithContextFields(
  1628. LogFields{
  1629. "remoteAddr": remoteAddr,
  1630. "bytesUp": atomic.LoadInt64(&bytesUp),
  1631. "bytesDown": atomic.LoadInt64(&bytesDown)}).Debug("exiting")
  1632. }