tunnelServer.go 50 KB

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