tunnelServer.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  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. "sync"
  28. "sync/atomic"
  29. "time"
  30. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon"
  31. "golang.org/x/crypto/ssh"
  32. )
  33. // TunnelServer is the main server that accepts Psiphon client
  34. // connections, via various obfuscation protocols, and provides
  35. // port forwarding (TCP and UDP) services to the Psiphon client.
  36. // At its core, TunnelServer is an SSH server. SSH is the base
  37. // protocol that provides port forward multiplexing, and transport
  38. // security. Layered on top of SSH, optionally, is Obfuscated SSH
  39. // and meek protocols, which provide further circumvention
  40. // capabilities.
  41. type TunnelServer struct {
  42. runWaitGroup *sync.WaitGroup
  43. listenerError chan error
  44. shutdownBroadcast <-chan struct{}
  45. sshServer *sshServer
  46. }
  47. // NewTunnelServer initializes a new tunnel server.
  48. func NewTunnelServer(
  49. support *SupportServices,
  50. shutdownBroadcast <-chan struct{}) (*TunnelServer, error) {
  51. sshServer, err := newSSHServer(support, shutdownBroadcast)
  52. if err != nil {
  53. return nil, psiphon.ContextError(err)
  54. }
  55. return &TunnelServer{
  56. runWaitGroup: new(sync.WaitGroup),
  57. listenerError: make(chan error),
  58. shutdownBroadcast: shutdownBroadcast,
  59. sshServer: sshServer,
  60. }, nil
  61. }
  62. // GetLoadStats returns load stats for the tunnel server. The stats are
  63. // broken down by protocol ("SSH", "OSSH", etc.) and type. Types of stats
  64. // include current connected client count, total number of current port
  65. // forwards.
  66. func (server *TunnelServer) GetLoadStats() map[string]map[string]int64 {
  67. return server.sshServer.getLoadStats()
  68. }
  69. // Run runs the tunnel server; this function blocks while running a selection of
  70. // listeners that handle connection using various obfuscation protocols.
  71. //
  72. // Run listens on each designated tunnel port and spawns new goroutines to handle
  73. // each client connection. It halts when shutdownBroadcast is signaled. A list of active
  74. // clients is maintained, and when halting all clients are cleanly shutdown.
  75. //
  76. // Each client goroutine handles its own obfuscation (optional), SSH handshake, SSH
  77. // authentication, and then looping on client new channel requests. "direct-tcpip"
  78. // channels, dynamic port fowards, are supported. When the UDPInterceptUdpgwServerAddress
  79. // config parameter is configured, UDP port forwards over a TCP stream, following
  80. // the udpgw protocol, are handled.
  81. //
  82. // A new goroutine is spawned to handle each port forward for each client. Each port
  83. // forward tracks its bytes transferred. Overall per-client stats for connection duration,
  84. // GeoIP, number of port forwards, and bytes transferred are tracked and logged when the
  85. // client shuts down.
  86. func (server *TunnelServer) Run() error {
  87. type sshListener struct {
  88. net.Listener
  89. localAddress string
  90. tunnelProtocol string
  91. }
  92. // TODO: should TunnelServer hold its own support pointer?
  93. support := server.sshServer.support
  94. // First bind all listeners; once all are successful,
  95. // start accepting connections on each.
  96. var listeners []*sshListener
  97. for tunnelProtocol, listenPort := range support.Config.TunnelProtocolPorts {
  98. localAddress := fmt.Sprintf(
  99. "%s:%d", support.Config.ServerIPAddress, listenPort)
  100. listener, err := net.Listen("tcp", localAddress)
  101. if err != nil {
  102. for _, existingListener := range listeners {
  103. existingListener.Listener.Close()
  104. }
  105. return psiphon.ContextError(err)
  106. }
  107. log.WithContextFields(
  108. LogFields{
  109. "localAddress": localAddress,
  110. "tunnelProtocol": tunnelProtocol,
  111. }).Info("listening")
  112. listeners = append(
  113. listeners,
  114. &sshListener{
  115. Listener: listener,
  116. localAddress: localAddress,
  117. tunnelProtocol: tunnelProtocol,
  118. })
  119. }
  120. for _, listener := range listeners {
  121. server.runWaitGroup.Add(1)
  122. go func(listener *sshListener) {
  123. defer server.runWaitGroup.Done()
  124. log.WithContextFields(
  125. LogFields{
  126. "localAddress": listener.localAddress,
  127. "tunnelProtocol": listener.tunnelProtocol,
  128. }).Info("running")
  129. server.sshServer.runListener(
  130. listener.Listener,
  131. server.listenerError,
  132. listener.tunnelProtocol)
  133. log.WithContextFields(
  134. LogFields{
  135. "localAddress": listener.localAddress,
  136. "tunnelProtocol": listener.tunnelProtocol,
  137. }).Info("stopped")
  138. }(listener)
  139. }
  140. var err error
  141. select {
  142. case <-server.shutdownBroadcast:
  143. case err = <-server.listenerError:
  144. }
  145. for _, listener := range listeners {
  146. listener.Close()
  147. }
  148. server.sshServer.stopClients()
  149. server.runWaitGroup.Wait()
  150. log.WithContext().Info("stopped")
  151. return err
  152. }
  153. type sshClientID uint64
  154. type sshServer struct {
  155. support *SupportServices
  156. shutdownBroadcast <-chan struct{}
  157. sshHostKey ssh.Signer
  158. nextClientID sshClientID
  159. clientsMutex sync.Mutex
  160. stoppingClients bool
  161. clients map[sshClientID]*sshClient
  162. }
  163. func newSSHServer(
  164. support *SupportServices,
  165. shutdownBroadcast <-chan struct{}) (*sshServer, error) {
  166. privateKey, err := ssh.ParseRawPrivateKey([]byte(support.Config.SSHPrivateKey))
  167. if err != nil {
  168. return nil, psiphon.ContextError(err)
  169. }
  170. // TODO: use cert (ssh.NewCertSigner) for anti-fingerprint?
  171. signer, err := ssh.NewSignerFromKey(privateKey)
  172. if err != nil {
  173. return nil, psiphon.ContextError(err)
  174. }
  175. return &sshServer{
  176. support: support,
  177. shutdownBroadcast: shutdownBroadcast,
  178. sshHostKey: signer,
  179. nextClientID: 1,
  180. clients: make(map[sshClientID]*sshClient),
  181. }, nil
  182. }
  183. // runListener is intended to run an a goroutine; it blocks
  184. // running a particular listener. If an unrecoverable error
  185. // occurs, it will send the error to the listenerError channel.
  186. func (sshServer *sshServer) runListener(
  187. listener net.Listener,
  188. listenerError chan<- error,
  189. tunnelProtocol string) {
  190. handleClient := func(clientConn net.Conn) {
  191. // process each client connection concurrently
  192. go sshServer.handleClient(tunnelProtocol, clientConn)
  193. }
  194. // Note: when exiting due to a unrecoverable error, be sure
  195. // to try to send the error to listenerError so that the outer
  196. // TunnelServer.Run will properly shut down instead of remaining
  197. // running.
  198. if psiphon.TunnelProtocolUsesMeekHTTP(tunnelProtocol) ||
  199. psiphon.TunnelProtocolUsesMeekHTTPS(tunnelProtocol) {
  200. meekServer, err := NewMeekServer(
  201. sshServer.support,
  202. listener,
  203. psiphon.TunnelProtocolUsesMeekHTTPS(tunnelProtocol),
  204. handleClient,
  205. sshServer.shutdownBroadcast)
  206. if err != nil {
  207. select {
  208. case listenerError <- psiphon.ContextError(err):
  209. default:
  210. }
  211. return
  212. }
  213. meekServer.Run()
  214. } else {
  215. for {
  216. conn, err := listener.Accept()
  217. select {
  218. case <-sshServer.shutdownBroadcast:
  219. if err == nil {
  220. conn.Close()
  221. }
  222. return
  223. default:
  224. }
  225. if err != nil {
  226. if e, ok := err.(net.Error); ok && e.Temporary() {
  227. log.WithContextFields(LogFields{"error": err}).Error("accept failed")
  228. // Temporary error, keep running
  229. continue
  230. }
  231. select {
  232. case listenerError <- psiphon.ContextError(err):
  233. default:
  234. }
  235. return
  236. }
  237. handleClient(conn)
  238. }
  239. }
  240. }
  241. func (sshServer *sshServer) registerClient(client *sshClient) (sshClientID, bool) {
  242. sshServer.clientsMutex.Lock()
  243. defer sshServer.clientsMutex.Unlock()
  244. if sshServer.stoppingClients {
  245. return 0, false
  246. }
  247. clientID := sshServer.nextClientID
  248. sshServer.nextClientID += 1
  249. sshServer.clients[clientID] = client
  250. return clientID, true
  251. }
  252. func (sshServer *sshServer) unregisterClient(clientID sshClientID) {
  253. sshServer.clientsMutex.Lock()
  254. client := sshServer.clients[clientID]
  255. delete(sshServer.clients, clientID)
  256. sshServer.clientsMutex.Unlock()
  257. if client != nil {
  258. client.stop()
  259. }
  260. }
  261. func (sshServer *sshServer) getLoadStats() map[string]map[string]int64 {
  262. sshServer.clientsMutex.Lock()
  263. defer sshServer.clientsMutex.Unlock()
  264. loadStats := make(map[string]map[string]int64)
  265. for _, client := range sshServer.clients {
  266. if loadStats[client.tunnelProtocol] == nil {
  267. loadStats[client.tunnelProtocol] = make(map[string]int64)
  268. }
  269. // Note: can't sum trafficState.peakConcurrentPortForwardCount to get a global peak
  270. loadStats[client.tunnelProtocol]["CurrentClients"] += 1
  271. client.Lock()
  272. loadStats[client.tunnelProtocol]["CurrentTCPPortForwards"] += client.tcpTrafficState.concurrentPortForwardCount
  273. loadStats[client.tunnelProtocol]["TotalTCPPortForwards"] += client.tcpTrafficState.totalPortForwardCount
  274. loadStats[client.tunnelProtocol]["CurrentUDPPortForwards"] += client.udpTrafficState.concurrentPortForwardCount
  275. loadStats[client.tunnelProtocol]["TotalUDPPortForwards"] += client.udpTrafficState.totalPortForwardCount
  276. client.Unlock()
  277. }
  278. return loadStats
  279. }
  280. func (sshServer *sshServer) stopClients() {
  281. sshServer.clientsMutex.Lock()
  282. sshServer.stoppingClients = true
  283. clients := sshServer.clients
  284. sshServer.clients = make(map[sshClientID]*sshClient)
  285. sshServer.clientsMutex.Unlock()
  286. for _, client := range clients {
  287. client.stop()
  288. }
  289. }
  290. func (sshServer *sshServer) handleClient(tunnelProtocol string, clientConn net.Conn) {
  291. geoIPData := sshServer.support.GeoIPService.Lookup(
  292. psiphon.IPAddressFromAddr(clientConn.RemoteAddr()))
  293. // TODO: apply reload of TrafficRulesSet to existing clients
  294. sshClient := newSshClient(
  295. sshServer,
  296. tunnelProtocol,
  297. geoIPData,
  298. sshServer.support.TrafficRulesSet.GetTrafficRules(geoIPData.Country))
  299. // Wrap the base client connection with an ActivityMonitoredConn which will
  300. // terminate the connection if no data is received before the deadline. This
  301. // timeout is in effect for the entire duration of the SSH connection. Clients
  302. // must actively use the connection or send SSH keep alive requests to keep
  303. // the connection active.
  304. activityConn := psiphon.NewActivityMonitoredConn(
  305. clientConn,
  306. SSH_CONNECTION_READ_DEADLINE,
  307. false,
  308. nil)
  309. clientConn = activityConn
  310. // Further wrap the connection in a rate limiting ThrottledConn.
  311. rateLimits := sshClient.trafficRules.GetRateLimits(tunnelProtocol)
  312. clientConn = psiphon.NewThrottledConn(
  313. clientConn,
  314. rateLimits.DownstreamUnlimitedBytes,
  315. int64(rateLimits.DownstreamBytesPerSecond),
  316. rateLimits.UpstreamUnlimitedBytes,
  317. int64(rateLimits.UpstreamBytesPerSecond))
  318. // Run the initial [obfuscated] SSH handshake in a goroutine so we can both
  319. // respect shutdownBroadcast and implement a specific handshake timeout.
  320. // The timeout is to reclaim network resources in case the handshake takes
  321. // too long.
  322. type sshNewServerConnResult struct {
  323. conn net.Conn
  324. sshConn *ssh.ServerConn
  325. channels <-chan ssh.NewChannel
  326. requests <-chan *ssh.Request
  327. err error
  328. }
  329. resultChannel := make(chan *sshNewServerConnResult, 2)
  330. if SSH_HANDSHAKE_TIMEOUT > 0 {
  331. time.AfterFunc(time.Duration(SSH_HANDSHAKE_TIMEOUT), func() {
  332. resultChannel <- &sshNewServerConnResult{err: errors.New("ssh handshake timeout")}
  333. })
  334. }
  335. go func(conn net.Conn) {
  336. sshServerConfig := &ssh.ServerConfig{
  337. PasswordCallback: sshClient.passwordCallback,
  338. AuthLogCallback: sshClient.authLogCallback,
  339. ServerVersion: sshServer.support.Config.SSHServerVersion,
  340. }
  341. sshServerConfig.AddHostKey(sshServer.sshHostKey)
  342. result := &sshNewServerConnResult{}
  343. // Wrap the connection in an SSH deobfuscator when required.
  344. if psiphon.TunnelProtocolUsesObfuscatedSSH(tunnelProtocol) {
  345. // Note: NewObfuscatedSshConn blocks on network I/O
  346. // TODO: ensure this won't block shutdown
  347. conn, result.err = psiphon.NewObfuscatedSshConn(
  348. psiphon.OBFUSCATION_CONN_MODE_SERVER,
  349. clientConn,
  350. sshServer.support.Config.ObfuscatedSSHKey)
  351. if result.err != nil {
  352. result.err = psiphon.ContextError(result.err)
  353. }
  354. }
  355. if result.err == nil {
  356. result.sshConn, result.channels, result.requests, result.err =
  357. ssh.NewServerConn(conn, sshServerConfig)
  358. }
  359. resultChannel <- result
  360. }(clientConn)
  361. var result *sshNewServerConnResult
  362. select {
  363. case result = <-resultChannel:
  364. case <-sshServer.shutdownBroadcast:
  365. // Close() will interrupt an ongoing handshake
  366. // TODO: wait for goroutine to exit before returning?
  367. clientConn.Close()
  368. return
  369. }
  370. if result.err != nil {
  371. clientConn.Close()
  372. // This is a Debug log due to noise. The handshake often fails due to I/O
  373. // errors as clients frequently interrupt connections in progress when
  374. // client-side load balancing completes a connection to a different server.
  375. log.WithContextFields(LogFields{"error": result.err}).Debug("handshake failed")
  376. return
  377. }
  378. sshClient.Lock()
  379. sshClient.sshConn = result.sshConn
  380. sshClient.activityConn = activityConn
  381. sshClient.Unlock()
  382. clientID, ok := sshServer.registerClient(sshClient)
  383. if !ok {
  384. clientConn.Close()
  385. log.WithContext().Warning("register failed")
  386. return
  387. }
  388. defer sshServer.unregisterClient(clientID)
  389. sshClient.runClient(result.channels, result.requests)
  390. // TODO: clientConn.Close()?
  391. }
  392. type sshClient struct {
  393. sync.Mutex
  394. sshServer *sshServer
  395. tunnelProtocol string
  396. sshConn ssh.Conn
  397. activityConn *psiphon.ActivityMonitoredConn
  398. geoIPData GeoIPData
  399. psiphonSessionID string
  400. udpChannel ssh.Channel
  401. trafficRules TrafficRules
  402. tcpTrafficState *trafficState
  403. udpTrafficState *trafficState
  404. channelHandlerWaitGroup *sync.WaitGroup
  405. tcpPortForwardLRU *psiphon.LRUConns
  406. stopBroadcast chan struct{}
  407. }
  408. type trafficState struct {
  409. bytesUp int64
  410. bytesDown int64
  411. concurrentPortForwardCount int64
  412. peakConcurrentPortForwardCount int64
  413. totalPortForwardCount int64
  414. }
  415. func newSshClient(
  416. sshServer *sshServer, tunnelProtocol string, geoIPData GeoIPData, trafficRules TrafficRules) *sshClient {
  417. return &sshClient{
  418. sshServer: sshServer,
  419. tunnelProtocol: tunnelProtocol,
  420. geoIPData: geoIPData,
  421. trafficRules: trafficRules,
  422. tcpTrafficState: &trafficState{},
  423. udpTrafficState: &trafficState{},
  424. channelHandlerWaitGroup: new(sync.WaitGroup),
  425. tcpPortForwardLRU: psiphon.NewLRUConns(),
  426. stopBroadcast: make(chan struct{}),
  427. }
  428. }
  429. func (sshClient *sshClient) passwordCallback(conn ssh.ConnMetadata, password []byte) (*ssh.Permissions, error) {
  430. var sshPasswordPayload struct {
  431. SessionId string `json:"SessionId"`
  432. SshPassword string `json:"SshPassword"`
  433. }
  434. err := json.Unmarshal(password, &sshPasswordPayload)
  435. if err != nil {
  436. // Backwards compatibility case: instead of a JSON payload, older clients
  437. // send the hex encoded session ID prepended to the SSH password.
  438. // Note: there's an even older case where clients don't send any session ID,
  439. // but that's no longer supported.
  440. if len(password) == 2*psiphon.PSIPHON_API_CLIENT_SESSION_ID_LENGTH+2*SSH_PASSWORD_BYTE_LENGTH {
  441. sshPasswordPayload.SessionId = string(password[0 : 2*psiphon.PSIPHON_API_CLIENT_SESSION_ID_LENGTH])
  442. sshPasswordPayload.SshPassword = string(password[2*psiphon.PSIPHON_API_CLIENT_SESSION_ID_LENGTH : len(password)])
  443. } else {
  444. return nil, psiphon.ContextError(fmt.Errorf("invalid password payload for %q", conn.User()))
  445. }
  446. }
  447. if !isHexDigits(sshClient.sshServer.support, sshPasswordPayload.SessionId) {
  448. return nil, psiphon.ContextError(fmt.Errorf("invalid session ID for %q", conn.User()))
  449. }
  450. userOk := (subtle.ConstantTimeCompare(
  451. []byte(conn.User()), []byte(sshClient.sshServer.support.Config.SSHUserName)) == 1)
  452. passwordOk := (subtle.ConstantTimeCompare(
  453. []byte(sshPasswordPayload.SshPassword), []byte(sshClient.sshServer.support.Config.SSHPassword)) == 1)
  454. if !userOk || !passwordOk {
  455. return nil, psiphon.ContextError(fmt.Errorf("invalid password for %q", conn.User()))
  456. }
  457. psiphonSessionID := sshPasswordPayload.SessionId
  458. sshClient.Lock()
  459. sshClient.psiphonSessionID = psiphonSessionID
  460. geoIPData := sshClient.geoIPData
  461. sshClient.Unlock()
  462. // Store the GeoIP data associated with the session ID. This makes the GeoIP data
  463. // available to the web server for web transport Psiphon API requests.
  464. sshClient.sshServer.support.GeoIPService.SetSessionCache(
  465. psiphonSessionID, geoIPData)
  466. return nil, nil
  467. }
  468. func (sshClient *sshClient) authLogCallback(conn ssh.ConnMetadata, method string, err error) {
  469. if err != nil {
  470. logFields := LogFields{"error": err, "method": method}
  471. if sshClient.sshServer.support.Config.UseFail2Ban() {
  472. clientIPAddress := psiphon.IPAddressFromAddr(conn.RemoteAddr())
  473. if clientIPAddress != "" {
  474. logFields["fail2ban"] = fmt.Sprintf(
  475. sshClient.sshServer.support.Config.Fail2BanFormat, clientIPAddress)
  476. }
  477. }
  478. log.WithContextFields(LogFields{"error": err, "method": method}).Error("authentication failed")
  479. } else {
  480. log.WithContextFields(LogFields{"error": err, "method": method}).Debug("authentication success")
  481. }
  482. }
  483. func (sshClient *sshClient) stop() {
  484. sshClient.sshConn.Close()
  485. sshClient.sshConn.Wait()
  486. close(sshClient.stopBroadcast)
  487. sshClient.channelHandlerWaitGroup.Wait()
  488. // Note: reporting duration based on last confirmed data transfer, which
  489. // is reads for sshClient.activityConn.GetActiveDuration(), and not
  490. // connection closing is important for protocols such as meek. For
  491. // meek, the connection remains open until the HTTP session expires,
  492. // which may be some time after the tunnel has closed. (The meek
  493. // protocol has no allowance for signalling payload EOF, and even if
  494. // it did the client may not have the opportunity to send a final
  495. // request with an EOF flag set.)
  496. sshClient.Lock()
  497. log.WithContextFields(
  498. LogFields{
  499. "startTime": sshClient.activityConn.GetStartTime(),
  500. "duration": sshClient.activityConn.GetActiveDuration(),
  501. "psiphonSessionID": sshClient.psiphonSessionID,
  502. "country": sshClient.geoIPData.Country,
  503. "city": sshClient.geoIPData.City,
  504. "ISP": sshClient.geoIPData.ISP,
  505. "bytesUpTCP": sshClient.tcpTrafficState.bytesUp,
  506. "bytesDownTCP": sshClient.tcpTrafficState.bytesDown,
  507. "peakConcurrentPortForwardCountTCP": sshClient.tcpTrafficState.peakConcurrentPortForwardCount,
  508. "totalPortForwardCountTCP": sshClient.tcpTrafficState.totalPortForwardCount,
  509. "bytesUpUDP": sshClient.udpTrafficState.bytesUp,
  510. "bytesDownUDP": sshClient.udpTrafficState.bytesDown,
  511. "peakConcurrentPortForwardCountUDP": sshClient.udpTrafficState.peakConcurrentPortForwardCount,
  512. "totalPortForwardCountUDP": sshClient.udpTrafficState.totalPortForwardCount,
  513. }).Info("tunnel closed")
  514. sshClient.Unlock()
  515. }
  516. // runClient handles/dispatches new channel and new requests from the client.
  517. // When the SSH client connection closes, both the channels and requests channels
  518. // will close and runClient will exit.
  519. func (sshClient *sshClient) runClient(
  520. channels <-chan ssh.NewChannel, requests <-chan *ssh.Request) {
  521. requestsWaitGroup := new(sync.WaitGroup)
  522. requestsWaitGroup.Add(1)
  523. go func() {
  524. defer requestsWaitGroup.Done()
  525. for request := range requests {
  526. // requests are processed serially; responses must be sent in request order.
  527. responsePayload, err := sshAPIRequestHandler(
  528. sshClient.sshServer.support,
  529. sshClient.geoIPData,
  530. request.Type,
  531. request.Payload)
  532. if err == nil {
  533. err = request.Reply(true, responsePayload)
  534. } else {
  535. log.WithContextFields(LogFields{"error": err}).Warning("request failed")
  536. err = request.Reply(false, nil)
  537. }
  538. if err != nil {
  539. log.WithContextFields(LogFields{"error": err}).Warning("response failed")
  540. }
  541. }
  542. }()
  543. for newChannel := range channels {
  544. if newChannel.ChannelType() != "direct-tcpip" {
  545. sshClient.rejectNewChannel(newChannel, ssh.Prohibited, "unknown or unsupported channel type")
  546. continue
  547. }
  548. // process each port forward concurrently
  549. sshClient.channelHandlerWaitGroup.Add(1)
  550. go sshClient.handleNewPortForwardChannel(newChannel)
  551. }
  552. requestsWaitGroup.Wait()
  553. }
  554. func (sshClient *sshClient) rejectNewChannel(newChannel ssh.NewChannel, reason ssh.RejectionReason, message string) {
  555. // TODO: log more details?
  556. log.WithContextFields(
  557. LogFields{
  558. "channelType": newChannel.ChannelType(),
  559. "rejectMessage": message,
  560. "rejectReason": reason,
  561. }).Warning("reject new channel")
  562. newChannel.Reject(reason, message)
  563. }
  564. func (sshClient *sshClient) handleNewPortForwardChannel(newChannel ssh.NewChannel) {
  565. defer sshClient.channelHandlerWaitGroup.Done()
  566. // http://tools.ietf.org/html/rfc4254#section-7.2
  567. var directTcpipExtraData struct {
  568. HostToConnect string
  569. PortToConnect uint32
  570. OriginatorIPAddress string
  571. OriginatorPort uint32
  572. }
  573. err := ssh.Unmarshal(newChannel.ExtraData(), &directTcpipExtraData)
  574. if err != nil {
  575. sshClient.rejectNewChannel(newChannel, ssh.Prohibited, "invalid extra data")
  576. return
  577. }
  578. // Intercept TCP port forwards to a specified udpgw server and handle directly.
  579. // TODO: also support UDP explicitly, e.g. with a custom "direct-udp" channel type?
  580. isUDPChannel := sshClient.sshServer.support.Config.UDPInterceptUdpgwServerAddress != "" &&
  581. sshClient.sshServer.support.Config.UDPInterceptUdpgwServerAddress ==
  582. fmt.Sprintf("%s:%d",
  583. directTcpipExtraData.HostToConnect,
  584. directTcpipExtraData.PortToConnect)
  585. if isUDPChannel {
  586. sshClient.handleUDPChannel(newChannel)
  587. } else {
  588. sshClient.handleTCPChannel(
  589. directTcpipExtraData.HostToConnect, int(directTcpipExtraData.PortToConnect), newChannel)
  590. }
  591. }
  592. func (sshClient *sshClient) isPortForwardPermitted(
  593. port int, allowPorts []int, denyPorts []int) bool {
  594. // TODO: faster lookup?
  595. if len(allowPorts) > 0 {
  596. for _, allowPort := range allowPorts {
  597. if port == allowPort {
  598. return true
  599. }
  600. }
  601. return false
  602. }
  603. if len(denyPorts) > 0 {
  604. for _, denyPort := range denyPorts {
  605. if port == denyPort {
  606. return false
  607. }
  608. }
  609. }
  610. return true
  611. }
  612. func (sshClient *sshClient) isPortForwardLimitExceeded(
  613. state *trafficState, maxPortForwardCount int) bool {
  614. limitExceeded := false
  615. if maxPortForwardCount > 0 {
  616. sshClient.Lock()
  617. limitExceeded = state.concurrentPortForwardCount >= int64(maxPortForwardCount)
  618. sshClient.Unlock()
  619. }
  620. return limitExceeded
  621. }
  622. func (sshClient *sshClient) openedPortForward(
  623. state *trafficState) {
  624. sshClient.Lock()
  625. state.concurrentPortForwardCount += 1
  626. if state.concurrentPortForwardCount > state.peakConcurrentPortForwardCount {
  627. state.peakConcurrentPortForwardCount = state.concurrentPortForwardCount
  628. }
  629. state.totalPortForwardCount += 1
  630. sshClient.Unlock()
  631. }
  632. func (sshClient *sshClient) closedPortForward(
  633. state *trafficState, bytesUp, bytesDown int64) {
  634. sshClient.Lock()
  635. state.concurrentPortForwardCount -= 1
  636. state.bytesUp += bytesUp
  637. state.bytesDown += bytesDown
  638. sshClient.Unlock()
  639. }
  640. func (sshClient *sshClient) handleTCPChannel(
  641. hostToConnect string,
  642. portToConnect int,
  643. newChannel ssh.NewChannel) {
  644. if !sshClient.isPortForwardPermitted(
  645. portToConnect,
  646. sshClient.trafficRules.AllowTCPPorts,
  647. sshClient.trafficRules.DenyTCPPorts) {
  648. sshClient.rejectNewChannel(
  649. newChannel, ssh.Prohibited, "port forward not permitted")
  650. return
  651. }
  652. var bytesUp, bytesDown int64
  653. sshClient.openedPortForward(sshClient.tcpTrafficState)
  654. defer func() {
  655. sshClient.closedPortForward(
  656. sshClient.tcpTrafficState,
  657. atomic.LoadInt64(&bytesUp),
  658. atomic.LoadInt64(&bytesDown))
  659. }()
  660. // TOCTOU note: important to increment the port forward count (via
  661. // openPortForward) _before_ checking isPortForwardLimitExceeded
  662. // otherwise, the client could potentially consume excess resources
  663. // by initiating many port forwards concurrently.
  664. // TODO: close LRU connection (after successful Dial) instead of
  665. // rejecting new connection?
  666. if sshClient.isPortForwardLimitExceeded(
  667. sshClient.tcpTrafficState,
  668. sshClient.trafficRules.MaxTCPPortForwardCount) {
  669. // Close the oldest TCP port forward. CloseOldest() closes
  670. // the conn and the port forward's goroutine will complete
  671. // the cleanup asynchronously.
  672. //
  673. // Some known limitations:
  674. //
  675. // - Since CloseOldest() closes the upstream socket but does not
  676. // clean up all resources associated with the port forward. These
  677. // include the goroutine(s) relaying traffic as well as the SSH
  678. // channel. Closing the socket will interrupt the goroutines which
  679. // will then complete the cleanup. But, since the full cleanup is
  680. // asynchronous, there exists a possibility that a client can consume
  681. // more than max port forward resources -- just not upstream sockets.
  682. //
  683. // - An LRU list entry for this port forward is not added until
  684. // after the dial completes, but the port forward is counted
  685. // towards max limits. This means many dials in progress will
  686. // put established connections in jeopardy.
  687. //
  688. // - We're closing the oldest open connection _before_ successfully
  689. // dialing the new port forward. This means we are potentially
  690. // discarding a good connection to make way for a failed connection.
  691. // We cannot simply dial first and still maintain a limit on
  692. // resources used, so to address this we'd need to add some
  693. // accounting for connections still establishing.
  694. sshClient.tcpPortForwardLRU.CloseOldest()
  695. log.WithContextFields(
  696. LogFields{
  697. "maxCount": sshClient.trafficRules.MaxTCPPortForwardCount,
  698. }).Debug("closed LRU TCP port forward")
  699. }
  700. // Dial the target remote address. This is done in a goroutine to
  701. // ensure the shutdown signal is handled immediately.
  702. remoteAddr := fmt.Sprintf("%s:%d", hostToConnect, portToConnect)
  703. log.WithContextFields(LogFields{"remoteAddr": remoteAddr}).Debug("dialing")
  704. type dialTcpResult struct {
  705. conn net.Conn
  706. err error
  707. }
  708. resultChannel := make(chan *dialTcpResult, 1)
  709. go func() {
  710. // TODO: on EADDRNOTAVAIL, temporarily suspend new clients
  711. // TODO: IPv6 support
  712. conn, err := net.DialTimeout(
  713. "tcp4", remoteAddr, SSH_TCP_PORT_FORWARD_DIAL_TIMEOUT)
  714. resultChannel <- &dialTcpResult{conn, err}
  715. }()
  716. var result *dialTcpResult
  717. select {
  718. case result = <-resultChannel:
  719. case <-sshClient.stopBroadcast:
  720. // Note: may leave dial in progress
  721. return
  722. }
  723. if result.err != nil {
  724. sshClient.rejectNewChannel(newChannel, ssh.ConnectionFailed, result.err.Error())
  725. return
  726. }
  727. // The upstream TCP port forward connection has been established. Schedule
  728. // some cleanup and notify the SSH client that the channel is accepted.
  729. fwdConn := result.conn
  730. defer fwdConn.Close()
  731. lruEntry := sshClient.tcpPortForwardLRU.Add(fwdConn)
  732. defer lruEntry.Remove()
  733. // ActivityMonitoredConn monitors the TCP port forward I/O and updates
  734. // its LRU status. ActivityMonitoredConn also times out read on the port
  735. // forward if both reads and writes have been idle for the specified
  736. // duration.
  737. fwdConn = psiphon.NewActivityMonitoredConn(
  738. fwdConn,
  739. time.Duration(sshClient.trafficRules.IdleTCPPortForwardTimeoutMilliseconds)*time.Millisecond,
  740. true,
  741. lruEntry)
  742. fwdChannel, requests, err := newChannel.Accept()
  743. if err != nil {
  744. log.WithContextFields(LogFields{"error": err}).Warning("accept new channel failed")
  745. return
  746. }
  747. go ssh.DiscardRequests(requests)
  748. defer fwdChannel.Close()
  749. log.WithContextFields(LogFields{"remoteAddr": remoteAddr}).Debug("relaying")
  750. // Relay channel to forwarded connection.
  751. // TODO: relay errors to fwdChannel.Stderr()?
  752. relayWaitGroup := new(sync.WaitGroup)
  753. relayWaitGroup.Add(1)
  754. go func() {
  755. defer relayWaitGroup.Done()
  756. // io.Copy allocates a 32K temporary buffer, and each port forward relay uses
  757. // two of these buffers; using io.CopyBuffer with a smaller buffer reduces the
  758. // overall memory footprint.
  759. bytes, err := io.CopyBuffer(
  760. fwdChannel, fwdConn, make([]byte, SSH_TCP_PORT_FORWARD_COPY_BUFFER_SIZE))
  761. atomic.AddInt64(&bytesDown, bytes)
  762. if err != nil && err != io.EOF {
  763. // Debug since errors such as "connection reset by peer" occur during normal operation
  764. log.WithContextFields(LogFields{"error": err}).Debug("downstream TCP relay failed")
  765. }
  766. // Interrupt upstream io.Copy when downstream is shutting down.
  767. // TODO: this is done to quickly cleanup the port forward when
  768. // fwdConn has a read timeout, but is it clean -- upstream may still
  769. // be flowing?
  770. fwdChannel.Close()
  771. }()
  772. bytes, err := io.CopyBuffer(
  773. fwdConn, fwdChannel, make([]byte, SSH_TCP_PORT_FORWARD_COPY_BUFFER_SIZE))
  774. atomic.AddInt64(&bytesUp, bytes)
  775. if err != nil && err != io.EOF {
  776. log.WithContextFields(LogFields{"error": err}).Debug("upstream TCP relay failed")
  777. }
  778. // Shutdown special case: fwdChannel will be closed and return EOF when
  779. // the SSH connection is closed, but we need to explicitly close fwdConn
  780. // to interrupt the downstream io.Copy, which may be blocked on a
  781. // fwdConn.Read().
  782. fwdConn.Close()
  783. relayWaitGroup.Wait()
  784. log.WithContextFields(
  785. LogFields{
  786. "remoteAddr": remoteAddr,
  787. "bytesUp": atomic.LoadInt64(&bytesUp),
  788. "bytesDown": atomic.LoadInt64(&bytesDown)}).Debug("exiting")
  789. }