tunnelServer.go 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  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"
  34. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  35. )
  36. const (
  37. SSH_HANDSHAKE_TIMEOUT = 30 * time.Second
  38. SSH_CONNECTION_READ_DEADLINE = 5 * time.Minute
  39. SSH_TCP_PORT_FORWARD_DIAL_TIMEOUT = 30 * time.Second
  40. SSH_TCP_PORT_FORWARD_COPY_BUFFER_SIZE = 8192
  41. )
  42. // Disallowed port forward hosts is a failsafe. The server should
  43. // be run on a host with correctly configured firewall rules, or
  44. // containerization, or both.
  45. var SSH_DISALLOWED_PORT_FORWARD_HOSTS = []string{"localhost", "127.0.0.1"}
  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. func (server *TunnelServer) Run() error {
  93. type sshListener struct {
  94. net.Listener
  95. localAddress string
  96. tunnelProtocol string
  97. }
  98. // TODO: should TunnelServer hold its own support pointer?
  99. support := server.sshServer.support
  100. // First bind all listeners; once all are successful,
  101. // start accepting connections on each.
  102. var listeners []*sshListener
  103. for tunnelProtocol, listenPort := range support.Config.TunnelProtocolPorts {
  104. localAddress := fmt.Sprintf(
  105. "%s:%d", support.Config.ServerIPAddress, listenPort)
  106. listener, err := net.Listen("tcp", localAddress)
  107. if err != nil {
  108. for _, existingListener := range listeners {
  109. existingListener.Listener.Close()
  110. }
  111. return common.ContextError(err)
  112. }
  113. log.WithContextFields(
  114. LogFields{
  115. "localAddress": localAddress,
  116. "tunnelProtocol": tunnelProtocol,
  117. }).Info("listening")
  118. listeners = append(
  119. listeners,
  120. &sshListener{
  121. Listener: listener,
  122. localAddress: localAddress,
  123. tunnelProtocol: tunnelProtocol,
  124. })
  125. }
  126. for _, listener := range listeners {
  127. server.runWaitGroup.Add(1)
  128. go func(listener *sshListener) {
  129. defer server.runWaitGroup.Done()
  130. log.WithContextFields(
  131. LogFields{
  132. "localAddress": listener.localAddress,
  133. "tunnelProtocol": listener.tunnelProtocol,
  134. }).Info("running")
  135. server.sshServer.runListener(
  136. listener.Listener,
  137. server.listenerError,
  138. listener.tunnelProtocol)
  139. log.WithContextFields(
  140. LogFields{
  141. "localAddress": listener.localAddress,
  142. "tunnelProtocol": listener.tunnelProtocol,
  143. }).Info("stopped")
  144. }(listener)
  145. }
  146. var err error
  147. select {
  148. case <-server.shutdownBroadcast:
  149. case err = <-server.listenerError:
  150. }
  151. for _, listener := range listeners {
  152. listener.Close()
  153. }
  154. server.sshServer.stopClients()
  155. server.runWaitGroup.Wait()
  156. log.WithContext().Info("stopped")
  157. return err
  158. }
  159. // GetLoadStats returns load stats for the tunnel server. The stats are
  160. // broken down by protocol ("SSH", "OSSH", etc.) and type. Types of stats
  161. // include current connected client count, total number of current port
  162. // forwards.
  163. func (server *TunnelServer) GetLoadStats() map[string]map[string]int64 {
  164. return server.sshServer.getLoadStats()
  165. }
  166. // ResetAllClientTrafficRules resets all established client traffic rules
  167. // to use the latest server config and client state.
  168. func (server *TunnelServer) ResetAllClientTrafficRules() {
  169. server.sshServer.resetAllClientTrafficRules()
  170. }
  171. // SetClientHandshakeState sets the handshake state -- that it completed and
  172. // what paramaters were passed -- in sshClient. This state is used for allowing
  173. // port forwards and for future traffic rule selection. SetClientHandshakeState
  174. // also triggers an immediate traffic rule re-selection, as the rules selected
  175. // upon tunnel establishment may no longer apply now that handshake values are
  176. // set.
  177. func (server *TunnelServer) SetClientHandshakeState(
  178. sessionID string, state handshakeState) error {
  179. return server.sshServer.setClientHandshakeState(sessionID, state)
  180. }
  181. // SetEstablishTunnels sets whether new tunnels may be established or not.
  182. // When not establishing, incoming connections are immediately closed.
  183. func (server *TunnelServer) SetEstablishTunnels(establish bool) {
  184. server.sshServer.setEstablishTunnels(establish)
  185. }
  186. // GetEstablishTunnels returns whether new tunnels may be established or not.
  187. func (server *TunnelServer) GetEstablishTunnels() bool {
  188. return server.sshServer.getEstablishTunnels()
  189. }
  190. type sshServer struct {
  191. support *SupportServices
  192. establishTunnels int32
  193. shutdownBroadcast <-chan struct{}
  194. sshHostKey ssh.Signer
  195. clientsMutex sync.Mutex
  196. stoppingClients bool
  197. acceptedClientCounts map[string]int64
  198. clients map[string]*sshClient
  199. }
  200. func newSSHServer(
  201. support *SupportServices,
  202. shutdownBroadcast <-chan struct{}) (*sshServer, error) {
  203. privateKey, err := ssh.ParseRawPrivateKey([]byte(support.Config.SSHPrivateKey))
  204. if err != nil {
  205. return nil, common.ContextError(err)
  206. }
  207. // TODO: use cert (ssh.NewCertSigner) for anti-fingerprint?
  208. signer, err := ssh.NewSignerFromKey(privateKey)
  209. if err != nil {
  210. return nil, common.ContextError(err)
  211. }
  212. return &sshServer{
  213. support: support,
  214. establishTunnels: 1,
  215. shutdownBroadcast: shutdownBroadcast,
  216. sshHostKey: signer,
  217. acceptedClientCounts: make(map[string]int64),
  218. clients: make(map[string]*sshClient),
  219. }, nil
  220. }
  221. func (sshServer *sshServer) setEstablishTunnels(establish bool) {
  222. // Do nothing when the setting is already correct. This avoids
  223. // spurious log messages when setEstablishTunnels is called
  224. // periodically with the same setting.
  225. if establish == sshServer.getEstablishTunnels() {
  226. return
  227. }
  228. establishFlag := int32(1)
  229. if !establish {
  230. establishFlag = 0
  231. }
  232. atomic.StoreInt32(&sshServer.establishTunnels, establishFlag)
  233. log.WithContextFields(
  234. LogFields{"establish": establish}).Info("establishing tunnels")
  235. }
  236. func (sshServer *sshServer) getEstablishTunnels() bool {
  237. return atomic.LoadInt32(&sshServer.establishTunnels) == 1
  238. }
  239. // runListener is intended to run an a goroutine; it blocks
  240. // running a particular listener. If an unrecoverable error
  241. // occurs, it will send the error to the listenerError channel.
  242. func (sshServer *sshServer) runListener(
  243. listener net.Listener,
  244. listenerError chan<- error,
  245. tunnelProtocol string) {
  246. handleClient := func(clientConn net.Conn) {
  247. // Note: establish tunnel limiter cannot simply stop TCP
  248. // listeners in all cases (e.g., meek) since SSH tunnel can
  249. // span multiple TCP connections.
  250. if !sshServer.getEstablishTunnels() {
  251. log.WithContext().Debug("not establishing tunnels")
  252. clientConn.Close()
  253. return
  254. }
  255. // process each client connection concurrently
  256. go sshServer.handleClient(tunnelProtocol, clientConn)
  257. }
  258. // Note: when exiting due to a unrecoverable error, be sure
  259. // to try to send the error to listenerError so that the outer
  260. // TunnelServer.Run will properly shut down instead of remaining
  261. // running.
  262. if common.TunnelProtocolUsesMeekHTTP(tunnelProtocol) ||
  263. common.TunnelProtocolUsesMeekHTTPS(tunnelProtocol) {
  264. meekServer, err := NewMeekServer(
  265. sshServer.support,
  266. listener,
  267. common.TunnelProtocolUsesMeekHTTPS(tunnelProtocol),
  268. handleClient,
  269. sshServer.shutdownBroadcast)
  270. if err != nil {
  271. select {
  272. case listenerError <- common.ContextError(err):
  273. default:
  274. }
  275. return
  276. }
  277. meekServer.Run()
  278. } else {
  279. for {
  280. conn, err := listener.Accept()
  281. select {
  282. case <-sshServer.shutdownBroadcast:
  283. if err == nil {
  284. conn.Close()
  285. }
  286. return
  287. default:
  288. }
  289. if err != nil {
  290. if e, ok := err.(net.Error); ok && e.Temporary() {
  291. log.WithContextFields(LogFields{"error": err}).Error("accept failed")
  292. // Temporary error, keep running
  293. continue
  294. }
  295. select {
  296. case listenerError <- common.ContextError(err):
  297. default:
  298. }
  299. return
  300. }
  301. handleClient(conn)
  302. }
  303. }
  304. }
  305. // An accepted client has completed a direct TCP or meek connection and has a net.Conn. Registration
  306. // is for tracking the number of connections.
  307. func (sshServer *sshServer) registerAcceptedClient(tunnelProtocol string) {
  308. sshServer.clientsMutex.Lock()
  309. defer sshServer.clientsMutex.Unlock()
  310. sshServer.acceptedClientCounts[tunnelProtocol] += 1
  311. }
  312. func (sshServer *sshServer) unregisterAcceptedClient(tunnelProtocol string) {
  313. sshServer.clientsMutex.Lock()
  314. defer sshServer.clientsMutex.Unlock()
  315. sshServer.acceptedClientCounts[tunnelProtocol] -= 1
  316. }
  317. // An established client has completed its SSH handshake and has a ssh.Conn. Registration is
  318. // for tracking the number of fully established clients and for maintaining a list of running
  319. // clients (for stopping at shutdown time).
  320. func (sshServer *sshServer) registerEstablishedClient(client *sshClient) bool {
  321. sshServer.clientsMutex.Lock()
  322. if sshServer.stoppingClients {
  323. sshServer.clientsMutex.Unlock()
  324. return false
  325. }
  326. // In the case of a duplicate client sessionID, the previous client is closed.
  327. // - Well-behaved clients generate pick a random sessionID that should be
  328. // unique (won't accidentally conflict) and hard to guess (can't be targetted
  329. // by a malicious client).
  330. // - Clients reuse the same sessionID when a tunnel is unexpectedly disconnected
  331. // and resestablished. In this case, when the same server is selected, this logic
  332. // will be hit; closing the old, dangling client is desirable.
  333. // - Multi-tunnel clients should not normally use one server for multiple tunnels.
  334. existingClient := sshServer.clients[client.sessionID]
  335. sshServer.clients[client.sessionID] = client
  336. sshServer.clientsMutex.Unlock()
  337. // Call stop() outside the mutex to avoid deadlock.
  338. if existingClient != nil {
  339. existingClient.stop()
  340. }
  341. return true
  342. }
  343. func (sshServer *sshServer) unregisterEstablishedClient(sessionID string) {
  344. sshServer.clientsMutex.Lock()
  345. client := sshServer.clients[sessionID]
  346. delete(sshServer.clients, sessionID)
  347. sshServer.clientsMutex.Unlock()
  348. // Call stop() outside the mutex to avoid deadlock.
  349. if client != nil {
  350. client.stop()
  351. }
  352. }
  353. func (sshServer *sshServer) getLoadStats() map[string]map[string]int64 {
  354. sshServer.clientsMutex.Lock()
  355. defer sshServer.clientsMutex.Unlock()
  356. loadStats := make(map[string]map[string]int64)
  357. // Explicitly populate with zeros to get 0 counts in log messages derived from getLoadStats()
  358. for tunnelProtocol, _ := range sshServer.support.Config.TunnelProtocolPorts {
  359. loadStats[tunnelProtocol] = make(map[string]int64)
  360. loadStats[tunnelProtocol]["accepted_clients"] = 0
  361. loadStats[tunnelProtocol]["established_clients"] = 0
  362. loadStats[tunnelProtocol]["tcp_port_forwards"] = 0
  363. loadStats[tunnelProtocol]["total_tcp_port_forwards"] = 0
  364. loadStats[tunnelProtocol]["udp_port_forwards"] = 0
  365. loadStats[tunnelProtocol]["total_udp_port_forwards"] = 0
  366. }
  367. // Note: as currently tracked/counted, each established client is also an accepted client
  368. for tunnelProtocol, acceptedClientCount := range sshServer.acceptedClientCounts {
  369. loadStats[tunnelProtocol]["accepted_clients"] = acceptedClientCount
  370. }
  371. var aggregatedQualityMetrics qualityMetrics
  372. for _, client := range sshServer.clients {
  373. // Note: can't sum trafficState.peakConcurrentPortForwardCount to get a global peak
  374. loadStats[client.tunnelProtocol]["established_clients"] += 1
  375. client.Lock()
  376. loadStats[client.tunnelProtocol]["tcp_port_forwards"] += client.tcpTrafficState.concurrentPortForwardCount
  377. loadStats[client.tunnelProtocol]["total_tcp_port_forwards"] += client.tcpTrafficState.totalPortForwardCount
  378. loadStats[client.tunnelProtocol]["udp_port_forwards"] += client.udpTrafficState.concurrentPortForwardCount
  379. loadStats[client.tunnelProtocol]["total_udp_port_forwards"] += client.udpTrafficState.totalPortForwardCount
  380. aggregatedQualityMetrics.tcpPortForwardDialedCount += client.qualityMetrics.tcpPortForwardDialedCount
  381. aggregatedQualityMetrics.tcpPortForwardDialedDuration +=
  382. client.qualityMetrics.tcpPortForwardDialedDuration / time.Millisecond
  383. aggregatedQualityMetrics.tcpPortForwardFailedCount += client.qualityMetrics.tcpPortForwardFailedCount
  384. aggregatedQualityMetrics.tcpPortForwardFailedDuration +=
  385. client.qualityMetrics.tcpPortForwardFailedDuration / time.Millisecond
  386. client.qualityMetrics.tcpPortForwardDialedCount = 0
  387. client.qualityMetrics.tcpPortForwardDialedDuration = 0
  388. client.qualityMetrics.tcpPortForwardFailedCount = 0
  389. client.qualityMetrics.tcpPortForwardFailedDuration = 0
  390. client.Unlock()
  391. }
  392. // Calculate and report totals across all protocols. It's easier to do this here
  393. // than futher down the stats stack. Also useful for glancing at log files.
  394. allProtocolsStats := make(map[string]int64)
  395. for _, stats := range loadStats {
  396. for name, value := range stats {
  397. allProtocolsStats[name] += value
  398. }
  399. }
  400. loadStats["ALL"] = allProtocolsStats
  401. loadStats["ALL"]["tcp_port_forward_dialed_count"] = aggregatedQualityMetrics.tcpPortForwardDialedCount
  402. loadStats["ALL"]["tcp_port_forward_dialed_duration"] = int64(aggregatedQualityMetrics.tcpPortForwardDialedDuration)
  403. loadStats["ALL"]["tcp_port_forward_failed_count"] = aggregatedQualityMetrics.tcpPortForwardFailedCount
  404. loadStats["ALL"]["tcp_port_forward_failed_duration"] = int64(aggregatedQualityMetrics.tcpPortForwardFailedDuration)
  405. return loadStats
  406. }
  407. func (sshServer *sshServer) resetAllClientTrafficRules() {
  408. sshServer.clientsMutex.Lock()
  409. clients := make(map[string]*sshClient)
  410. for sessionID, client := range sshServer.clients {
  411. clients[sessionID] = client
  412. }
  413. sshServer.clientsMutex.Unlock()
  414. for _, client := range clients {
  415. client.setTrafficRules()
  416. }
  417. }
  418. func (sshServer *sshServer) setClientHandshakeState(
  419. sessionID string, state handshakeState) error {
  420. sshServer.clientsMutex.Lock()
  421. client := sshServer.clients[sessionID]
  422. sshServer.clientsMutex.Unlock()
  423. if client == nil {
  424. return common.ContextError(errors.New("unknown session ID"))
  425. }
  426. err := client.setHandshakeState(state)
  427. if err != nil {
  428. return common.ContextError(err)
  429. }
  430. client.setTrafficRules()
  431. return nil
  432. }
  433. func (sshServer *sshServer) stopClients() {
  434. sshServer.clientsMutex.Lock()
  435. sshServer.stoppingClients = true
  436. clients := sshServer.clients
  437. sshServer.clients = make(map[string]*sshClient)
  438. sshServer.clientsMutex.Unlock()
  439. for _, client := range clients {
  440. client.stop()
  441. }
  442. }
  443. func (sshServer *sshServer) handleClient(tunnelProtocol string, clientConn net.Conn) {
  444. sshServer.registerAcceptedClient(tunnelProtocol)
  445. defer sshServer.unregisterAcceptedClient(tunnelProtocol)
  446. geoIPData := sshServer.support.GeoIPService.Lookup(
  447. common.IPAddressFromAddr(clientConn.RemoteAddr()))
  448. sshClient := newSshClient(sshServer, tunnelProtocol, geoIPData)
  449. // Set initial traffic rules, pre-handshake, based on currently known info.
  450. sshClient.setTrafficRules()
  451. // Wrap the base client connection with an ActivityMonitoredConn which will
  452. // terminate the connection if no data is received before the deadline. This
  453. // timeout is in effect for the entire duration of the SSH connection. Clients
  454. // must actively use the connection or send SSH keep alive requests to keep
  455. // the connection active. Writes are not considered reliable activity indicators
  456. // due to buffering.
  457. activityConn, err := common.NewActivityMonitoredConn(
  458. clientConn,
  459. SSH_CONNECTION_READ_DEADLINE,
  460. false,
  461. nil)
  462. if err != nil {
  463. clientConn.Close()
  464. log.WithContextFields(LogFields{"error": err}).Error("NewActivityMonitoredConn failed")
  465. return
  466. }
  467. clientConn = activityConn
  468. // Further wrap the connection in a rate limiting ThrottledConn.
  469. throttledConn := common.NewThrottledConn(clientConn, sshClient.rateLimits())
  470. clientConn = throttledConn
  471. // Run the initial [obfuscated] SSH handshake in a goroutine so we can both
  472. // respect shutdownBroadcast and implement a specific handshake timeout.
  473. // The timeout is to reclaim network resources in case the handshake takes
  474. // too long.
  475. type sshNewServerConnResult struct {
  476. conn net.Conn
  477. sshConn *ssh.ServerConn
  478. channels <-chan ssh.NewChannel
  479. requests <-chan *ssh.Request
  480. err error
  481. }
  482. resultChannel := make(chan *sshNewServerConnResult, 2)
  483. if SSH_HANDSHAKE_TIMEOUT > 0 {
  484. time.AfterFunc(time.Duration(SSH_HANDSHAKE_TIMEOUT), func() {
  485. resultChannel <- &sshNewServerConnResult{err: errors.New("ssh handshake timeout")}
  486. })
  487. }
  488. go func(conn net.Conn) {
  489. sshServerConfig := &ssh.ServerConfig{
  490. PasswordCallback: sshClient.passwordCallback,
  491. AuthLogCallback: sshClient.authLogCallback,
  492. ServerVersion: sshServer.support.Config.SSHServerVersion,
  493. }
  494. sshServerConfig.AddHostKey(sshServer.sshHostKey)
  495. result := &sshNewServerConnResult{}
  496. // Wrap the connection in an SSH deobfuscator when required.
  497. if common.TunnelProtocolUsesObfuscatedSSH(tunnelProtocol) {
  498. // Note: NewObfuscatedSshConn blocks on network I/O
  499. // TODO: ensure this won't block shutdown
  500. conn, result.err = psiphon.NewObfuscatedSshConn(
  501. psiphon.OBFUSCATION_CONN_MODE_SERVER,
  502. conn,
  503. sshServer.support.Config.ObfuscatedSSHKey)
  504. if result.err != nil {
  505. result.err = common.ContextError(result.err)
  506. }
  507. }
  508. if result.err == nil {
  509. result.sshConn, result.channels, result.requests, result.err =
  510. ssh.NewServerConn(conn, sshServerConfig)
  511. }
  512. resultChannel <- result
  513. }(clientConn)
  514. var result *sshNewServerConnResult
  515. select {
  516. case result = <-resultChannel:
  517. case <-sshServer.shutdownBroadcast:
  518. // Close() will interrupt an ongoing handshake
  519. // TODO: wait for goroutine to exit before returning?
  520. clientConn.Close()
  521. return
  522. }
  523. if result.err != nil {
  524. clientConn.Close()
  525. // This is a Debug log due to noise. The handshake often fails due to I/O
  526. // errors as clients frequently interrupt connections in progress when
  527. // client-side load balancing completes a connection to a different server.
  528. log.WithContextFields(LogFields{"error": result.err}).Debug("handshake failed")
  529. return
  530. }
  531. sshClient.Lock()
  532. sshClient.sshConn = result.sshConn
  533. sshClient.activityConn = activityConn
  534. sshClient.throttledConn = throttledConn
  535. sshClient.Unlock()
  536. if !sshServer.registerEstablishedClient(sshClient) {
  537. clientConn.Close()
  538. log.WithContext().Warning("register failed")
  539. return
  540. }
  541. defer sshServer.unregisterEstablishedClient(sshClient.sessionID)
  542. sshClient.runClient(result.channels, result.requests)
  543. // Note: sshServer.unregisterClient calls sshClient.Close(),
  544. // which also closes underlying transport Conn.
  545. }
  546. type sshClient struct {
  547. sync.Mutex
  548. sshServer *sshServer
  549. tunnelProtocol string
  550. sshConn ssh.Conn
  551. activityConn *common.ActivityMonitoredConn
  552. throttledConn *common.ThrottledConn
  553. geoIPData GeoIPData
  554. sessionID string
  555. handshakeState handshakeState
  556. udpChannel ssh.Channel
  557. trafficRules TrafficRules
  558. tcpTrafficState trafficState
  559. udpTrafficState trafficState
  560. qualityMetrics qualityMetrics
  561. channelHandlerWaitGroup *sync.WaitGroup
  562. tcpPortForwardLRU *common.LRUConns
  563. stopBroadcast chan struct{}
  564. }
  565. type trafficState struct {
  566. bytesUp int64
  567. bytesDown int64
  568. concurrentPortForwardCount int64
  569. peakConcurrentPortForwardCount int64
  570. totalPortForwardCount int64
  571. }
  572. // qualityMetrics records upstream TCP dial attempts and
  573. // elapsed time. Elapsed time includes the full TCP handshake
  574. // and, in aggregate, is a measure of the quality of the
  575. // upstream link. These stats are recorded by each sshClient
  576. // and then reported and reset in sshServer.getLoadStats().
  577. type qualityMetrics struct {
  578. tcpPortForwardDialedCount int64
  579. tcpPortForwardDialedDuration time.Duration
  580. tcpPortForwardFailedCount int64
  581. tcpPortForwardFailedDuration time.Duration
  582. }
  583. type handshakeState struct {
  584. completed bool
  585. apiProtocol string
  586. apiParams requestJSONObject
  587. }
  588. func newSshClient(
  589. sshServer *sshServer, tunnelProtocol string, geoIPData GeoIPData) *sshClient {
  590. return &sshClient{
  591. sshServer: sshServer,
  592. tunnelProtocol: tunnelProtocol,
  593. geoIPData: geoIPData,
  594. channelHandlerWaitGroup: new(sync.WaitGroup),
  595. tcpPortForwardLRU: common.NewLRUConns(),
  596. stopBroadcast: make(chan struct{}),
  597. }
  598. }
  599. func (sshClient *sshClient) passwordCallback(conn ssh.ConnMetadata, password []byte) (*ssh.Permissions, error) {
  600. expectedSessionIDLength := 2 * common.PSIPHON_API_CLIENT_SESSION_ID_LENGTH
  601. expectedSSHPasswordLength := 2 * SSH_PASSWORD_BYTE_LENGTH
  602. var sshPasswordPayload struct {
  603. SessionId string `json:"SessionId"`
  604. SshPassword string `json:"SshPassword"`
  605. }
  606. err := json.Unmarshal(password, &sshPasswordPayload)
  607. if err != nil {
  608. // Backwards compatibility case: instead of a JSON payload, older clients
  609. // send the hex encoded session ID prepended to the SSH password.
  610. // Note: there's an even older case where clients don't send any session ID,
  611. // but that's no longer supported.
  612. if len(password) == expectedSessionIDLength+expectedSSHPasswordLength {
  613. sshPasswordPayload.SessionId = string(password[0:expectedSessionIDLength])
  614. sshPasswordPayload.SshPassword = string(password[expectedSSHPasswordLength:len(password)])
  615. } else {
  616. return nil, common.ContextError(fmt.Errorf("invalid password payload for %q", conn.User()))
  617. }
  618. }
  619. if !isHexDigits(sshClient.sshServer.support, sshPasswordPayload.SessionId) ||
  620. len(sshPasswordPayload.SessionId) != expectedSessionIDLength {
  621. return nil, common.ContextError(fmt.Errorf("invalid session ID for %q", conn.User()))
  622. }
  623. userOk := (subtle.ConstantTimeCompare(
  624. []byte(conn.User()), []byte(sshClient.sshServer.support.Config.SSHUserName)) == 1)
  625. passwordOk := (subtle.ConstantTimeCompare(
  626. []byte(sshPasswordPayload.SshPassword), []byte(sshClient.sshServer.support.Config.SSHPassword)) == 1)
  627. if !userOk || !passwordOk {
  628. return nil, common.ContextError(fmt.Errorf("invalid password for %q", conn.User()))
  629. }
  630. sessionID := sshPasswordPayload.SessionId
  631. sshClient.Lock()
  632. sshClient.sessionID = sessionID
  633. geoIPData := sshClient.geoIPData
  634. sshClient.Unlock()
  635. // Store the GeoIP data associated with the session ID. This makes the GeoIP data
  636. // available to the web server for web transport Psiphon API requests. To allow for
  637. // post-tunnel final status requests, the lifetime of cached GeoIP records exceeds
  638. // the lifetime of the sshClient, and that's why this distinct session cache exists.
  639. sshClient.sshServer.support.GeoIPService.SetSessionCache(sessionID, geoIPData)
  640. return nil, nil
  641. }
  642. func (sshClient *sshClient) authLogCallback(conn ssh.ConnMetadata, method string, err error) {
  643. if err != nil {
  644. if method == "none" && err.Error() == "no auth passed yet" {
  645. // In this case, the callback invocation is noise from auth negotiation
  646. return
  647. }
  648. // Note: here we previously logged messages for fail2ban to act on. This is no longer
  649. // done as the complexity outweighs the benefits.
  650. //
  651. // - The SSH credential is not secret -- it's in the server entry. Attackers targetting
  652. // the server likely already have the credential. On the other hand, random scanning and
  653. // brute forcing is mitigated with high entropy random passwords, rate limiting
  654. // (implemented on the host via iptables), and limited capabilities (the SSH session can
  655. // only port forward).
  656. //
  657. // - fail2ban coverage was inconsistent; in the case of an unfronted meek protocol through
  658. // an upstream proxy, the remote address is the upstream proxy, which should not be blocked.
  659. // The X-Forwarded-For header cant be used instead as it may be forged and used to get IPs
  660. // deliberately blocked; and in any case fail2ban adds iptables rules which can only block
  661. // by direct remote IP, not by original client IP. Fronted meek has the same iptables issue.
  662. //
  663. // TODO: random scanning and brute forcing of port 22 will result in log noise. To eliminate
  664. // this, and to also cover meek protocols, and bad obfuscation keys, and bad inputs to the web
  665. // server, consider implementing fail2ban-type logic directly in this server, with the ability
  666. // to use X-Forwarded-For (when trustworthy; e.g, from a CDN).
  667. log.WithContextFields(LogFields{"error": err, "method": method}).Warning("authentication failed")
  668. } else {
  669. log.WithContextFields(LogFields{"error": err, "method": method}).Debug("authentication success")
  670. }
  671. }
  672. func (sshClient *sshClient) stop() {
  673. sshClient.sshConn.Close()
  674. sshClient.sshConn.Wait()
  675. close(sshClient.stopBroadcast)
  676. sshClient.channelHandlerWaitGroup.Wait()
  677. // Note: reporting duration based on last confirmed data transfer, which
  678. // is reads for sshClient.activityConn.GetActiveDuration(), and not
  679. // connection closing is important for protocols such as meek. For
  680. // meek, the connection remains open until the HTTP session expires,
  681. // which may be some time after the tunnel has closed. (The meek
  682. // protocol has no allowance for signalling payload EOF, and even if
  683. // it did the client may not have the opportunity to send a final
  684. // request with an EOF flag set.)
  685. sshClient.Lock()
  686. logFields := getRequestLogFields(
  687. sshClient.sshServer.support,
  688. "server_tunnel",
  689. sshClient.geoIPData,
  690. sshClient.handshakeState.apiParams,
  691. baseRequestParams)
  692. logFields["handshake_completed"] = sshClient.handshakeState.completed
  693. logFields["start_time"] = sshClient.activityConn.GetStartTime()
  694. logFields["duration"] = sshClient.activityConn.GetActiveDuration() / time.Millisecond
  695. logFields["bytes_up_tcp"] = sshClient.tcpTrafficState.bytesUp
  696. logFields["bytes_down_tcp"] = sshClient.tcpTrafficState.bytesDown
  697. logFields["peak_concurrent_port_forward_count_tcp"] = sshClient.tcpTrafficState.peakConcurrentPortForwardCount
  698. logFields["total_port_forward_count_tcp"] = sshClient.tcpTrafficState.totalPortForwardCount
  699. logFields["bytes_up_udp"] = sshClient.udpTrafficState.bytesUp
  700. logFields["bytes_down_udp"] = sshClient.udpTrafficState.bytesDown
  701. logFields["peak_concurrent_port_forward_count_udp"] = sshClient.udpTrafficState.peakConcurrentPortForwardCount
  702. logFields["total_port_forward_count_udp"] = sshClient.udpTrafficState.totalPortForwardCount
  703. sshClient.Unlock()
  704. log.LogRawFieldsWithTimestamp(logFields)
  705. }
  706. // runClient handles/dispatches new channel and new requests from the client.
  707. // When the SSH client connection closes, both the channels and requests channels
  708. // will close and runClient will exit.
  709. func (sshClient *sshClient) runClient(
  710. channels <-chan ssh.NewChannel, requests <-chan *ssh.Request) {
  711. requestsWaitGroup := new(sync.WaitGroup)
  712. requestsWaitGroup.Add(1)
  713. go func() {
  714. defer requestsWaitGroup.Done()
  715. for request := range requests {
  716. // Requests are processed serially; API responses must be sent in request order.
  717. var responsePayload []byte
  718. var err error
  719. if request.Type == "keepalive@openssh.com" {
  720. // Keepalive requests have an empty response.
  721. } else {
  722. // All other requests are assumed to be API requests.
  723. responsePayload, err = sshAPIRequestHandler(
  724. sshClient.sshServer.support,
  725. sshClient.geoIPData,
  726. request.Type,
  727. request.Payload)
  728. }
  729. if err == nil {
  730. err = request.Reply(true, responsePayload)
  731. } else {
  732. log.WithContextFields(LogFields{"error": err}).Warning("request failed")
  733. err = request.Reply(false, nil)
  734. }
  735. if err != nil {
  736. log.WithContextFields(LogFields{"error": err}).Warning("response failed")
  737. }
  738. }
  739. }()
  740. for newChannel := range channels {
  741. if newChannel.ChannelType() != "direct-tcpip" {
  742. sshClient.rejectNewChannel(newChannel, ssh.Prohibited, "unknown or unsupported channel type")
  743. continue
  744. }
  745. // process each port forward concurrently
  746. sshClient.channelHandlerWaitGroup.Add(1)
  747. go sshClient.handleNewPortForwardChannel(newChannel)
  748. }
  749. requestsWaitGroup.Wait()
  750. }
  751. func (sshClient *sshClient) rejectNewChannel(newChannel ssh.NewChannel, reason ssh.RejectionReason, logMessage string) {
  752. // Note: Debug level, as logMessage may contain user traffic destination address information
  753. log.WithContextFields(
  754. LogFields{
  755. "channelType": newChannel.ChannelType(),
  756. "logMessage": logMessage,
  757. "rejectReason": reason.String(),
  758. }).Debug("reject new channel")
  759. // Note: logMessage is internal, for logging only; just the RejectionReason is sent to the client
  760. newChannel.Reject(reason, reason.String())
  761. }
  762. func (sshClient *sshClient) handleNewPortForwardChannel(newChannel ssh.NewChannel) {
  763. defer sshClient.channelHandlerWaitGroup.Done()
  764. // http://tools.ietf.org/html/rfc4254#section-7.2
  765. var directTcpipExtraData struct {
  766. HostToConnect string
  767. PortToConnect uint32
  768. OriginatorIPAddress string
  769. OriginatorPort uint32
  770. }
  771. err := ssh.Unmarshal(newChannel.ExtraData(), &directTcpipExtraData)
  772. if err != nil {
  773. sshClient.rejectNewChannel(newChannel, ssh.Prohibited, "invalid extra data")
  774. return
  775. }
  776. // Intercept TCP port forwards to a specified udpgw server and handle directly.
  777. // TODO: also support UDP explicitly, e.g. with a custom "direct-udp" channel type?
  778. isUDPChannel := sshClient.sshServer.support.Config.UDPInterceptUdpgwServerAddress != "" &&
  779. sshClient.sshServer.support.Config.UDPInterceptUdpgwServerAddress ==
  780. net.JoinHostPort(directTcpipExtraData.HostToConnect, strconv.Itoa(int(directTcpipExtraData.PortToConnect)))
  781. if isUDPChannel {
  782. sshClient.handleUDPChannel(newChannel)
  783. } else {
  784. sshClient.handleTCPChannel(
  785. directTcpipExtraData.HostToConnect, int(directTcpipExtraData.PortToConnect), newChannel)
  786. }
  787. }
  788. // setHandshakeState records that a client has completed a handshake API request.
  789. // Some parameters from the handshake request may be used in future traffic rule
  790. // selection. Port forwards are disallowed until a handshake is complete. The
  791. // handshake parameters are included in the session summary log recorded in
  792. // sshClient.stop().
  793. func (sshClient *sshClient) setHandshakeState(state handshakeState) error {
  794. sshClient.Lock()
  795. defer sshClient.Unlock()
  796. // Client must only perform one handshake
  797. if sshClient.handshakeState.completed {
  798. return common.ContextError(errors.New("handshake already completed"))
  799. }
  800. sshClient.handshakeState = state
  801. return nil
  802. }
  803. // setTrafficRules resets the client's traffic rules based on the latest server config
  804. // and client state. As sshClient.trafficRules may be reset by a concurrent goroutine,
  805. // trafficRules must only be accessed within the sshClient mutex.
  806. func (sshClient *sshClient) setTrafficRules() {
  807. sshClient.Lock()
  808. defer sshClient.Unlock()
  809. sshClient.trafficRules = sshClient.sshServer.support.TrafficRulesSet.GetTrafficRules(
  810. sshClient.tunnelProtocol, sshClient.geoIPData, sshClient.handshakeState)
  811. if sshClient.throttledConn != nil {
  812. sshClient.throttledConn.SetLimits(
  813. sshClient.trafficRules.RateLimits.CommonRateLimits())
  814. }
  815. }
  816. func (sshClient *sshClient) rateLimits() common.RateLimits {
  817. sshClient.Lock()
  818. defer sshClient.Unlock()
  819. return sshClient.trafficRules.RateLimits.CommonRateLimits()
  820. }
  821. func (sshClient *sshClient) idleTCPPortForwardTimeout() time.Duration {
  822. sshClient.Lock()
  823. defer sshClient.Unlock()
  824. return time.Duration(*sshClient.trafficRules.IdleTCPPortForwardTimeoutMilliseconds) * time.Millisecond
  825. }
  826. func (sshClient *sshClient) idleUDPPortForwardTimeout() time.Duration {
  827. sshClient.Lock()
  828. defer sshClient.Unlock()
  829. return time.Duration(*sshClient.trafficRules.IdleUDPPortForwardTimeoutMilliseconds) * time.Millisecond
  830. }
  831. const (
  832. portForwardTypeTCP = iota
  833. portForwardTypeUDP
  834. )
  835. func (sshClient *sshClient) isPortForwardPermitted(
  836. portForwardType int, host string, port int) bool {
  837. sshClient.Lock()
  838. defer sshClient.Unlock()
  839. if !sshClient.handshakeState.completed {
  840. return false
  841. }
  842. if common.Contains(SSH_DISALLOWED_PORT_FORWARD_HOSTS, host) {
  843. return false
  844. }
  845. var allowPorts []int
  846. if portForwardType == portForwardTypeTCP {
  847. allowPorts = sshClient.trafficRules.AllowTCPPorts
  848. } else {
  849. allowPorts = sshClient.trafficRules.AllowUDPPorts
  850. }
  851. if len(allowPorts) == 0 {
  852. return true
  853. }
  854. // TODO: faster lookup?
  855. if len(allowPorts) > 0 {
  856. for _, allowPort := range allowPorts {
  857. if port == allowPort {
  858. return true
  859. }
  860. }
  861. }
  862. // TODO: AllowSubnets won't match when host is a domain.
  863. // Callers should resolve domain host before checking
  864. // isPortForwardPermitted.
  865. if ip := net.ParseIP(host); ip != nil {
  866. for _, subnet := range sshClient.trafficRules.AllowSubnets {
  867. // Note: ignoring error as config has been validated
  868. _, network, _ := net.ParseCIDR(subnet)
  869. if network.Contains(ip) {
  870. return true
  871. }
  872. }
  873. }
  874. return false
  875. }
  876. func (sshClient *sshClient) isPortForwardLimitExceeded(
  877. portForwardType int) (int, bool) {
  878. sshClient.Lock()
  879. defer sshClient.Unlock()
  880. var maxPortForwardCount int
  881. var state *trafficState
  882. if portForwardType == portForwardTypeTCP {
  883. maxPortForwardCount = *sshClient.trafficRules.MaxTCPPortForwardCount
  884. state = &sshClient.tcpTrafficState
  885. } else {
  886. maxPortForwardCount = *sshClient.trafficRules.MaxUDPPortForwardCount
  887. state = &sshClient.udpTrafficState
  888. }
  889. if maxPortForwardCount > 0 && state.concurrentPortForwardCount >= int64(maxPortForwardCount) {
  890. return maxPortForwardCount, true
  891. }
  892. return maxPortForwardCount, false
  893. }
  894. func (sshClient *sshClient) openedPortForward(
  895. portForwardType int) {
  896. sshClient.Lock()
  897. defer sshClient.Unlock()
  898. var state *trafficState
  899. if portForwardType == portForwardTypeTCP {
  900. state = &sshClient.tcpTrafficState
  901. } else {
  902. state = &sshClient.udpTrafficState
  903. }
  904. state.concurrentPortForwardCount += 1
  905. if state.concurrentPortForwardCount > state.peakConcurrentPortForwardCount {
  906. state.peakConcurrentPortForwardCount = state.concurrentPortForwardCount
  907. }
  908. state.totalPortForwardCount += 1
  909. }
  910. func (sshClient *sshClient) updateQualityMetrics(
  911. tcpPortForwardDialSuccess bool, dialDuration time.Duration) {
  912. sshClient.Lock()
  913. defer sshClient.Unlock()
  914. if tcpPortForwardDialSuccess {
  915. sshClient.qualityMetrics.tcpPortForwardDialedCount += 1
  916. sshClient.qualityMetrics.tcpPortForwardDialedDuration += dialDuration
  917. } else {
  918. sshClient.qualityMetrics.tcpPortForwardFailedCount += 1
  919. sshClient.qualityMetrics.tcpPortForwardFailedDuration += dialDuration
  920. }
  921. }
  922. func (sshClient *sshClient) closedPortForward(
  923. portForwardType int, bytesUp, bytesDown int64) {
  924. sshClient.Lock()
  925. defer sshClient.Unlock()
  926. var state *trafficState
  927. if portForwardType == portForwardTypeTCP {
  928. state = &sshClient.tcpTrafficState
  929. } else {
  930. state = &sshClient.udpTrafficState
  931. }
  932. state.concurrentPortForwardCount -= 1
  933. state.bytesUp += bytesUp
  934. state.bytesDown += bytesDown
  935. }
  936. func (sshClient *sshClient) handleTCPChannel(
  937. hostToConnect string,
  938. portToConnect int,
  939. newChannel ssh.NewChannel) {
  940. isWebServerPortForward := false
  941. config := sshClient.sshServer.support.Config
  942. if config.WebServerPortForwardAddress != "" {
  943. destination := net.JoinHostPort(hostToConnect, strconv.Itoa(portToConnect))
  944. if destination == config.WebServerPortForwardAddress {
  945. isWebServerPortForward = true
  946. if config.WebServerPortForwardRedirectAddress != "" {
  947. // Note: redirect format is validated when config is loaded
  948. host, portStr, _ := net.SplitHostPort(config.WebServerPortForwardRedirectAddress)
  949. port, _ := strconv.Atoi(portStr)
  950. hostToConnect = host
  951. portToConnect = port
  952. }
  953. }
  954. }
  955. if !isWebServerPortForward && !sshClient.isPortForwardPermitted(
  956. portForwardTypeTCP, hostToConnect, portToConnect) {
  957. sshClient.rejectNewChannel(
  958. newChannel, ssh.Prohibited, "port forward not permitted")
  959. return
  960. }
  961. var bytesUp, bytesDown int64
  962. sshClient.openedPortForward(portForwardTypeTCP)
  963. defer func() {
  964. sshClient.closedPortForward(
  965. portForwardTypeTCP, atomic.LoadInt64(&bytesUp), atomic.LoadInt64(&bytesDown))
  966. }()
  967. // TOCTOU note: important to increment the port forward count (via
  968. // openPortForward) _before_ checking isPortForwardLimitExceeded
  969. // otherwise, the client could potentially consume excess resources
  970. // by initiating many port forwards concurrently.
  971. // TODO: close LRU connection (after successful Dial) instead of
  972. // rejecting new connection?
  973. if maxCount, exceeded := sshClient.isPortForwardLimitExceeded(portForwardTypeTCP); exceeded {
  974. // Close the oldest TCP port forward. CloseOldest() closes
  975. // the conn and the port forward's goroutine will complete
  976. // the cleanup asynchronously.
  977. //
  978. // Some known limitations:
  979. //
  980. // - Since CloseOldest() closes the upstream socket but does not
  981. // clean up all resources associated with the port forward. These
  982. // include the goroutine(s) relaying traffic as well as the SSH
  983. // channel. Closing the socket will interrupt the goroutines which
  984. // will then complete the cleanup. But, since the full cleanup is
  985. // asynchronous, there exists a possibility that a client can consume
  986. // more than max port forward resources -- just not upstream sockets.
  987. //
  988. // - An LRU list entry for this port forward is not added until
  989. // after the dial completes, but the port forward is counted
  990. // towards max limits. This means many dials in progress will
  991. // put established connections in jeopardy.
  992. //
  993. // - We're closing the oldest open connection _before_ successfully
  994. // dialing the new port forward. This means we are potentially
  995. // discarding a good connection to make way for a failed connection.
  996. // We cannot simply dial first and still maintain a limit on
  997. // resources used, so to address this we'd need to add some
  998. // accounting for connections still establishing.
  999. sshClient.tcpPortForwardLRU.CloseOldest()
  1000. log.WithContextFields(
  1001. LogFields{
  1002. "maxCount": maxCount,
  1003. }).Debug("closed LRU TCP port forward")
  1004. }
  1005. // Dial the target remote address. This is done in a goroutine to
  1006. // ensure the shutdown signal is handled immediately.
  1007. remoteAddr := fmt.Sprintf("%s:%d", hostToConnect, portToConnect)
  1008. log.WithContextFields(LogFields{"remoteAddr": remoteAddr}).Debug("dialing")
  1009. type dialTcpResult struct {
  1010. conn net.Conn
  1011. err error
  1012. }
  1013. resultChannel := make(chan *dialTcpResult, 1)
  1014. dialStartTime := monotime.Now()
  1015. go func() {
  1016. // TODO: on EADDRNOTAVAIL, temporarily suspend new clients
  1017. // TODO: IPv6 support
  1018. conn, err := net.DialTimeout(
  1019. "tcp4", remoteAddr, SSH_TCP_PORT_FORWARD_DIAL_TIMEOUT)
  1020. resultChannel <- &dialTcpResult{conn, err}
  1021. }()
  1022. var result *dialTcpResult
  1023. select {
  1024. case result = <-resultChannel:
  1025. case <-sshClient.stopBroadcast:
  1026. // Note: may leave dial in progress (TODO: use DialContext to cancel)
  1027. return
  1028. }
  1029. sshClient.updateQualityMetrics(
  1030. result.err == nil, monotime.Since(dialStartTime))
  1031. if result.err != nil {
  1032. sshClient.rejectNewChannel(newChannel, ssh.ConnectionFailed, result.err.Error())
  1033. return
  1034. }
  1035. // The upstream TCP port forward connection has been established. Schedule
  1036. // some cleanup and notify the SSH client that the channel is accepted.
  1037. fwdConn := result.conn
  1038. defer fwdConn.Close()
  1039. fwdChannel, requests, err := newChannel.Accept()
  1040. if err != nil {
  1041. log.WithContextFields(LogFields{"error": err}).Warning("accept new channel failed")
  1042. return
  1043. }
  1044. go ssh.DiscardRequests(requests)
  1045. defer fwdChannel.Close()
  1046. // ActivityMonitoredConn monitors the TCP port forward I/O and updates
  1047. // its LRU status. ActivityMonitoredConn also times out I/O on the port
  1048. // forward if both reads and writes have been idle for the specified
  1049. // duration.
  1050. lruEntry := sshClient.tcpPortForwardLRU.Add(fwdConn)
  1051. defer lruEntry.Remove()
  1052. fwdConn, err = common.NewActivityMonitoredConn(
  1053. fwdConn,
  1054. sshClient.idleTCPPortForwardTimeout(),
  1055. true,
  1056. lruEntry)
  1057. if result.err != nil {
  1058. log.WithContextFields(LogFields{"error": err}).Error("NewActivityMonitoredConn failed")
  1059. return
  1060. }
  1061. // Relay channel to forwarded connection.
  1062. log.WithContextFields(LogFields{"remoteAddr": remoteAddr}).Debug("relaying")
  1063. // TODO: relay errors to fwdChannel.Stderr()?
  1064. relayWaitGroup := new(sync.WaitGroup)
  1065. relayWaitGroup.Add(1)
  1066. go func() {
  1067. defer relayWaitGroup.Done()
  1068. // io.Copy allocates a 32K temporary buffer, and each port forward relay uses
  1069. // two of these buffers; using io.CopyBuffer with a smaller buffer reduces the
  1070. // overall memory footprint.
  1071. bytes, err := io.CopyBuffer(
  1072. fwdChannel, fwdConn, make([]byte, SSH_TCP_PORT_FORWARD_COPY_BUFFER_SIZE))
  1073. atomic.AddInt64(&bytesDown, bytes)
  1074. if err != nil && err != io.EOF {
  1075. // Debug since errors such as "connection reset by peer" occur during normal operation
  1076. log.WithContextFields(LogFields{"error": err}).Debug("downstream TCP relay failed")
  1077. }
  1078. // Interrupt upstream io.Copy when downstream is shutting down.
  1079. // TODO: this is done to quickly cleanup the port forward when
  1080. // fwdConn has a read timeout, but is it clean -- upstream may still
  1081. // be flowing?
  1082. fwdChannel.Close()
  1083. }()
  1084. bytes, err := io.CopyBuffer(
  1085. fwdConn, fwdChannel, make([]byte, SSH_TCP_PORT_FORWARD_COPY_BUFFER_SIZE))
  1086. atomic.AddInt64(&bytesUp, bytes)
  1087. if err != nil && err != io.EOF {
  1088. log.WithContextFields(LogFields{"error": err}).Debug("upstream TCP relay failed")
  1089. }
  1090. // Shutdown special case: fwdChannel will be closed and return EOF when
  1091. // the SSH connection is closed, but we need to explicitly close fwdConn
  1092. // to interrupt the downstream io.Copy, which may be blocked on a
  1093. // fwdConn.Read().
  1094. fwdConn.Close()
  1095. relayWaitGroup.Wait()
  1096. log.WithContextFields(
  1097. LogFields{
  1098. "remoteAddr": remoteAddr,
  1099. "bytesUp": atomic.LoadInt64(&bytesUp),
  1100. "bytesDown": atomic.LoadInt64(&bytesDown)}).Debug("exiting")
  1101. }