tunnelServer.go 53 KB

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