tunnelServer.go 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  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. // Note: 64-bit ints used with atomic operations are at placed
  453. // at the start of struct to ensure 64-bit alignment.
  454. // (https://golang.org/pkg/sync/atomic/#pkg-note-BUG)
  455. bytesUp int64
  456. bytesDown int64
  457. concurrentPortForwardCount int64
  458. peakConcurrentPortForwardCount int64
  459. totalPortForwardCount int64
  460. }
  461. func newSshClient(
  462. sshServer *sshServer, tunnelProtocol string, geoIPData GeoIPData, trafficRules TrafficRules) *sshClient {
  463. return &sshClient{
  464. sshServer: sshServer,
  465. tunnelProtocol: tunnelProtocol,
  466. geoIPData: geoIPData,
  467. trafficRules: trafficRules,
  468. tcpTrafficState: &trafficState{},
  469. udpTrafficState: &trafficState{},
  470. channelHandlerWaitGroup: new(sync.WaitGroup),
  471. tcpPortForwardLRU: NewLRUConns(),
  472. stopBroadcast: make(chan struct{}),
  473. }
  474. }
  475. func (sshClient *sshClient) passwordCallback(conn ssh.ConnMetadata, password []byte) (*ssh.Permissions, error) {
  476. var sshPasswordPayload struct {
  477. SessionId string `json:"SessionId"`
  478. SshPassword string `json:"SshPassword"`
  479. }
  480. err := json.Unmarshal(password, &sshPasswordPayload)
  481. if err != nil {
  482. // Backwards compatibility case: instead of a JSON payload, older clients
  483. // send the hex encoded session ID prepended to the SSH password.
  484. // Note: there's an even older case where clients don't send any session ID,
  485. // but that's no longer supported.
  486. if len(password) == 2*common.PSIPHON_API_CLIENT_SESSION_ID_LENGTH+2*SSH_PASSWORD_BYTE_LENGTH {
  487. sshPasswordPayload.SessionId = string(password[0 : 2*common.PSIPHON_API_CLIENT_SESSION_ID_LENGTH])
  488. sshPasswordPayload.SshPassword = string(password[2*common.PSIPHON_API_CLIENT_SESSION_ID_LENGTH : len(password)])
  489. } else {
  490. return nil, common.ContextError(fmt.Errorf("invalid password payload for %q", conn.User()))
  491. }
  492. }
  493. if !isHexDigits(sshClient.sshServer.support, sshPasswordPayload.SessionId) {
  494. return nil, common.ContextError(fmt.Errorf("invalid session ID for %q", conn.User()))
  495. }
  496. userOk := (subtle.ConstantTimeCompare(
  497. []byte(conn.User()), []byte(sshClient.sshServer.support.Config.SSHUserName)) == 1)
  498. passwordOk := (subtle.ConstantTimeCompare(
  499. []byte(sshPasswordPayload.SshPassword), []byte(sshClient.sshServer.support.Config.SSHPassword)) == 1)
  500. if !userOk || !passwordOk {
  501. return nil, common.ContextError(fmt.Errorf("invalid password for %q", conn.User()))
  502. }
  503. psiphonSessionID := sshPasswordPayload.SessionId
  504. sshClient.Lock()
  505. sshClient.psiphonSessionID = psiphonSessionID
  506. geoIPData := sshClient.geoIPData
  507. sshClient.Unlock()
  508. // Store the GeoIP data associated with the session ID. This makes the GeoIP data
  509. // available to the web server for web transport Psiphon API requests.
  510. sshClient.sshServer.support.GeoIPService.SetSessionCache(
  511. psiphonSessionID, geoIPData)
  512. return nil, nil
  513. }
  514. func (sshClient *sshClient) authLogCallback(conn ssh.ConnMetadata, method string, err error) {
  515. if err != nil {
  516. if method == "none" && err.Error() == "no auth passed yet" {
  517. // In this case, the callback invocation is noise from auth negotiation
  518. return
  519. }
  520. // Note: here we previously logged messages for fail2ban to act on. This is no longer
  521. // done as the complexity outweighs the benefits.
  522. //
  523. // - The SSH credential is not secret -- it's in the server entry. Attackers targetting
  524. // the server likely already have the credential. On the other hand, random scanning and
  525. // brute forcing is mitigated with high entropy random passwords, rate limiting
  526. // (implemented on the host via iptables), and limited capabilities (the SSH session can
  527. // only port forward).
  528. //
  529. // - fail2ban coverage was inconsistent; in the case of an unfronted meek protocol through
  530. // an upstream proxy, the remote address is the upstream proxy, which should not be blocked.
  531. // The X-Forwarded-For header cant be used instead as it may be forged and used to get IPs
  532. // deliberately blocked; and in any case fail2ban adds iptables rules which can only block
  533. // by direct remote IP, not by original client IP. Fronted meek has the same iptables issue.
  534. //
  535. // TODO: random scanning and brute forcing of port 22 will result in log noise. To eliminate
  536. // this, and to also cover meek protocols, and bad obfuscation keys, and bad inputs to the web
  537. // server, consider implementing fail2ban-type logic directly in this server, with the ability
  538. // to use X-Forwarded-For (when trustworthy; e.g, from a CDN).
  539. log.WithContextFields(LogFields{"error": err, "method": method}).Warning("authentication failed")
  540. } else {
  541. log.WithContextFields(LogFields{"error": err, "method": method}).Debug("authentication success")
  542. }
  543. }
  544. func (sshClient *sshClient) stop() {
  545. sshClient.sshConn.Close()
  546. sshClient.sshConn.Wait()
  547. close(sshClient.stopBroadcast)
  548. sshClient.channelHandlerWaitGroup.Wait()
  549. // Note: reporting duration based on last confirmed data transfer, which
  550. // is reads for sshClient.activityConn.GetActiveDuration(), and not
  551. // connection closing is important for protocols such as meek. For
  552. // meek, the connection remains open until the HTTP session expires,
  553. // which may be some time after the tunnel has closed. (The meek
  554. // protocol has no allowance for signalling payload EOF, and even if
  555. // it did the client may not have the opportunity to send a final
  556. // request with an EOF flag set.)
  557. sshClient.Lock()
  558. log.WithContextFields(
  559. LogFields{
  560. "startTime": sshClient.activityConn.GetStartTime(),
  561. "duration": sshClient.activityConn.GetActiveDuration(),
  562. "psiphonSessionID": sshClient.psiphonSessionID,
  563. "country": sshClient.geoIPData.Country,
  564. "city": sshClient.geoIPData.City,
  565. "ISP": sshClient.geoIPData.ISP,
  566. "bytesUpTCP": sshClient.tcpTrafficState.bytesUp,
  567. "bytesDownTCP": sshClient.tcpTrafficState.bytesDown,
  568. "peakConcurrentPortForwardCountTCP": sshClient.tcpTrafficState.peakConcurrentPortForwardCount,
  569. "totalPortForwardCountTCP": sshClient.tcpTrafficState.totalPortForwardCount,
  570. "bytesUpUDP": sshClient.udpTrafficState.bytesUp,
  571. "bytesDownUDP": sshClient.udpTrafficState.bytesDown,
  572. "peakConcurrentPortForwardCountUDP": sshClient.udpTrafficState.peakConcurrentPortForwardCount,
  573. "totalPortForwardCountUDP": sshClient.udpTrafficState.totalPortForwardCount,
  574. }).Info("tunnel closed")
  575. sshClient.Unlock()
  576. }
  577. // runClient handles/dispatches new channel and new requests from the client.
  578. // When the SSH client connection closes, both the channels and requests channels
  579. // will close and runClient will exit.
  580. func (sshClient *sshClient) runClient(
  581. channels <-chan ssh.NewChannel, requests <-chan *ssh.Request) {
  582. requestsWaitGroup := new(sync.WaitGroup)
  583. requestsWaitGroup.Add(1)
  584. go func() {
  585. defer requestsWaitGroup.Done()
  586. for request := range requests {
  587. // Requests are processed serially; API responses must be sent in request order.
  588. var responsePayload []byte
  589. var err error
  590. if request.Type == "keepalive@openssh.com" {
  591. // Keepalive requests have an empty response.
  592. } else {
  593. // All other requests are assumed to be API requests.
  594. responsePayload, err = sshAPIRequestHandler(
  595. sshClient.sshServer.support,
  596. sshClient.geoIPData,
  597. request.Type,
  598. request.Payload)
  599. }
  600. if err == nil {
  601. err = request.Reply(true, responsePayload)
  602. } else {
  603. log.WithContextFields(LogFields{"error": err}).Warning("request failed")
  604. err = request.Reply(false, nil)
  605. }
  606. if err != nil {
  607. log.WithContextFields(LogFields{"error": err}).Warning("response failed")
  608. }
  609. }
  610. }()
  611. for newChannel := range channels {
  612. if newChannel.ChannelType() != "direct-tcpip" {
  613. sshClient.rejectNewChannel(newChannel, ssh.Prohibited, "unknown or unsupported channel type")
  614. continue
  615. }
  616. // process each port forward concurrently
  617. sshClient.channelHandlerWaitGroup.Add(1)
  618. go sshClient.handleNewPortForwardChannel(newChannel)
  619. }
  620. requestsWaitGroup.Wait()
  621. }
  622. func (sshClient *sshClient) rejectNewChannel(newChannel ssh.NewChannel, reason ssh.RejectionReason, logMessage string) {
  623. log.WithContextFields(
  624. LogFields{
  625. "channelType": newChannel.ChannelType(),
  626. "logMessage": logMessage,
  627. "rejectReason": reason.String(),
  628. }).Warning("reject new channel")
  629. // Note: logMessage is internal, for logging only; just the RejectionReason is sent to the client
  630. newChannel.Reject(reason, reason.String())
  631. }
  632. func (sshClient *sshClient) handleNewPortForwardChannel(newChannel ssh.NewChannel) {
  633. defer sshClient.channelHandlerWaitGroup.Done()
  634. // http://tools.ietf.org/html/rfc4254#section-7.2
  635. var directTcpipExtraData struct {
  636. HostToConnect string
  637. PortToConnect uint32
  638. OriginatorIPAddress string
  639. OriginatorPort uint32
  640. }
  641. err := ssh.Unmarshal(newChannel.ExtraData(), &directTcpipExtraData)
  642. if err != nil {
  643. sshClient.rejectNewChannel(newChannel, ssh.Prohibited, "invalid extra data")
  644. return
  645. }
  646. // Intercept TCP port forwards to a specified udpgw server and handle directly.
  647. // TODO: also support UDP explicitly, e.g. with a custom "direct-udp" channel type?
  648. isUDPChannel := sshClient.sshServer.support.Config.UDPInterceptUdpgwServerAddress != "" &&
  649. sshClient.sshServer.support.Config.UDPInterceptUdpgwServerAddress ==
  650. fmt.Sprintf("%s:%d",
  651. directTcpipExtraData.HostToConnect,
  652. directTcpipExtraData.PortToConnect)
  653. if isUDPChannel {
  654. sshClient.handleUDPChannel(newChannel)
  655. } else {
  656. sshClient.handleTCPChannel(
  657. directTcpipExtraData.HostToConnect, int(directTcpipExtraData.PortToConnect), newChannel)
  658. }
  659. }
  660. func (sshClient *sshClient) isPortForwardPermitted(
  661. host string, port int, allowPorts []int, denyPorts []int) bool {
  662. if common.Contains(SSH_DISALLOWED_PORT_FORWARD_HOSTS, host) {
  663. return false
  664. }
  665. // TODO: faster lookup?
  666. if len(allowPorts) > 0 {
  667. for _, allowPort := range allowPorts {
  668. if port == allowPort {
  669. return true
  670. }
  671. }
  672. return false
  673. }
  674. if len(denyPorts) > 0 {
  675. for _, denyPort := range denyPorts {
  676. if port == denyPort {
  677. return false
  678. }
  679. }
  680. }
  681. return true
  682. }
  683. func (sshClient *sshClient) isPortForwardLimitExceeded(
  684. state *trafficState, maxPortForwardCount int) bool {
  685. limitExceeded := false
  686. if maxPortForwardCount > 0 {
  687. sshClient.Lock()
  688. limitExceeded = state.concurrentPortForwardCount >= int64(maxPortForwardCount)
  689. sshClient.Unlock()
  690. }
  691. return limitExceeded
  692. }
  693. func (sshClient *sshClient) openedPortForward(
  694. state *trafficState) {
  695. sshClient.Lock()
  696. state.concurrentPortForwardCount += 1
  697. if state.concurrentPortForwardCount > state.peakConcurrentPortForwardCount {
  698. state.peakConcurrentPortForwardCount = state.concurrentPortForwardCount
  699. }
  700. state.totalPortForwardCount += 1
  701. sshClient.Unlock()
  702. }
  703. func (sshClient *sshClient) closedPortForward(
  704. state *trafficState, bytesUp, bytesDown int64) {
  705. sshClient.Lock()
  706. state.concurrentPortForwardCount -= 1
  707. state.bytesUp += bytesUp
  708. state.bytesDown += bytesDown
  709. sshClient.Unlock()
  710. }
  711. func (sshClient *sshClient) handleTCPChannel(
  712. hostToConnect string,
  713. portToConnect int,
  714. newChannel ssh.NewChannel) {
  715. if !sshClient.isPortForwardPermitted(
  716. hostToConnect,
  717. portToConnect,
  718. sshClient.trafficRules.AllowTCPPorts,
  719. sshClient.trafficRules.DenyTCPPorts) {
  720. sshClient.rejectNewChannel(
  721. newChannel, ssh.Prohibited, "port forward not permitted")
  722. return
  723. }
  724. var bytesUp, bytesDown int64
  725. sshClient.openedPortForward(sshClient.tcpTrafficState)
  726. defer func() {
  727. sshClient.closedPortForward(
  728. sshClient.tcpTrafficState,
  729. atomic.LoadInt64(&bytesUp),
  730. atomic.LoadInt64(&bytesDown))
  731. }()
  732. // TOCTOU note: important to increment the port forward count (via
  733. // openPortForward) _before_ checking isPortForwardLimitExceeded
  734. // otherwise, the client could potentially consume excess resources
  735. // by initiating many port forwards concurrently.
  736. // TODO: close LRU connection (after successful Dial) instead of
  737. // rejecting new connection?
  738. if sshClient.isPortForwardLimitExceeded(
  739. sshClient.tcpTrafficState,
  740. sshClient.trafficRules.MaxTCPPortForwardCount) {
  741. // Close the oldest TCP port forward. CloseOldest() closes
  742. // the conn and the port forward's goroutine will complete
  743. // the cleanup asynchronously.
  744. //
  745. // Some known limitations:
  746. //
  747. // - Since CloseOldest() closes the upstream socket but does not
  748. // clean up all resources associated with the port forward. These
  749. // include the goroutine(s) relaying traffic as well as the SSH
  750. // channel. Closing the socket will interrupt the goroutines which
  751. // will then complete the cleanup. But, since the full cleanup is
  752. // asynchronous, there exists a possibility that a client can consume
  753. // more than max port forward resources -- just not upstream sockets.
  754. //
  755. // - An LRU list entry for this port forward is not added until
  756. // after the dial completes, but the port forward is counted
  757. // towards max limits. This means many dials in progress will
  758. // put established connections in jeopardy.
  759. //
  760. // - We're closing the oldest open connection _before_ successfully
  761. // dialing the new port forward. This means we are potentially
  762. // discarding a good connection to make way for a failed connection.
  763. // We cannot simply dial first and still maintain a limit on
  764. // resources used, so to address this we'd need to add some
  765. // accounting for connections still establishing.
  766. sshClient.tcpPortForwardLRU.CloseOldest()
  767. log.WithContextFields(
  768. LogFields{
  769. "maxCount": sshClient.trafficRules.MaxTCPPortForwardCount,
  770. }).Debug("closed LRU TCP port forward")
  771. }
  772. // Dial the target remote address. This is done in a goroutine to
  773. // ensure the shutdown signal is handled immediately.
  774. remoteAddr := fmt.Sprintf("%s:%d", hostToConnect, portToConnect)
  775. log.WithContextFields(LogFields{"remoteAddr": remoteAddr}).Debug("dialing")
  776. type dialTcpResult struct {
  777. conn net.Conn
  778. err error
  779. }
  780. resultChannel := make(chan *dialTcpResult, 1)
  781. go func() {
  782. // TODO: on EADDRNOTAVAIL, temporarily suspend new clients
  783. // TODO: IPv6 support
  784. conn, err := net.DialTimeout(
  785. "tcp4", remoteAddr, SSH_TCP_PORT_FORWARD_DIAL_TIMEOUT)
  786. resultChannel <- &dialTcpResult{conn, err}
  787. }()
  788. var result *dialTcpResult
  789. select {
  790. case result = <-resultChannel:
  791. case <-sshClient.stopBroadcast:
  792. // Note: may leave dial in progress
  793. return
  794. }
  795. if result.err != nil {
  796. sshClient.rejectNewChannel(newChannel, ssh.ConnectionFailed, result.err.Error())
  797. return
  798. }
  799. // The upstream TCP port forward connection has been established. Schedule
  800. // some cleanup and notify the SSH client that the channel is accepted.
  801. fwdConn := result.conn
  802. defer fwdConn.Close()
  803. fwdChannel, requests, err := newChannel.Accept()
  804. if err != nil {
  805. log.WithContextFields(LogFields{"error": err}).Warning("accept new channel failed")
  806. return
  807. }
  808. go ssh.DiscardRequests(requests)
  809. defer fwdChannel.Close()
  810. // ActivityMonitoredConn monitors the TCP port forward I/O and updates
  811. // its LRU status. ActivityMonitoredConn also times out I/O on the port
  812. // forward if both reads and writes have been idle for the specified
  813. // duration.
  814. lruEntry := sshClient.tcpPortForwardLRU.Add(fwdConn)
  815. defer lruEntry.Remove()
  816. fwdConn, err = NewActivityMonitoredConn(
  817. fwdConn,
  818. time.Duration(sshClient.trafficRules.IdleTCPPortForwardTimeoutMilliseconds)*time.Millisecond,
  819. true,
  820. lruEntry)
  821. if result.err != nil {
  822. log.WithContextFields(LogFields{"error": err}).Error("NewActivityMonitoredConn failed")
  823. return
  824. }
  825. // Relay channel to forwarded connection.
  826. log.WithContextFields(LogFields{"remoteAddr": remoteAddr}).Debug("relaying")
  827. // TODO: relay errors to fwdChannel.Stderr()?
  828. relayWaitGroup := new(sync.WaitGroup)
  829. relayWaitGroup.Add(1)
  830. go func() {
  831. defer relayWaitGroup.Done()
  832. // io.Copy allocates a 32K temporary buffer, and each port forward relay uses
  833. // two of these buffers; using io.CopyBuffer with a smaller buffer reduces the
  834. // overall memory footprint.
  835. bytes, err := io.CopyBuffer(
  836. fwdChannel, fwdConn, make([]byte, SSH_TCP_PORT_FORWARD_COPY_BUFFER_SIZE))
  837. atomic.AddInt64(&bytesDown, bytes)
  838. if err != nil && err != io.EOF {
  839. // Debug since errors such as "connection reset by peer" occur during normal operation
  840. log.WithContextFields(LogFields{"error": err}).Debug("downstream TCP relay failed")
  841. }
  842. // Interrupt upstream io.Copy when downstream is shutting down.
  843. // TODO: this is done to quickly cleanup the port forward when
  844. // fwdConn has a read timeout, but is it clean -- upstream may still
  845. // be flowing?
  846. fwdChannel.Close()
  847. }()
  848. bytes, err := io.CopyBuffer(
  849. fwdConn, fwdChannel, make([]byte, SSH_TCP_PORT_FORWARD_COPY_BUFFER_SIZE))
  850. atomic.AddInt64(&bytesUp, bytes)
  851. if err != nil && err != io.EOF {
  852. log.WithContextFields(LogFields{"error": err}).Debug("upstream TCP relay failed")
  853. }
  854. // Shutdown special case: fwdChannel will be closed and return EOF when
  855. // the SSH connection is closed, but we need to explicitly close fwdConn
  856. // to interrupt the downstream io.Copy, which may be blocked on a
  857. // fwdConn.Read().
  858. fwdConn.Close()
  859. relayWaitGroup.Wait()
  860. log.WithContextFields(
  861. LogFields{
  862. "remoteAddr": remoteAddr,
  863. "bytesUp": atomic.LoadInt64(&bytesUp),
  864. "bytesDown": atomic.LoadInt64(&bytesDown)}).Debug("exiting")
  865. }