tunnelServer.go 33 KB

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