tunnelServer.go 33 KB

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