tunnelServer.go 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  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-Inc/crypto/ssh"
  31. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon"
  32. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  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. // Calculate and report totals across all protocols. It's easier to do this here
  318. // than futher down the stats stack. Also useful for glancing at log files.
  319. allProtocolsStats := make(map[string]int64)
  320. for _, stats := range loadStats {
  321. for name, value := range stats {
  322. allProtocolsStats[name] += value
  323. }
  324. }
  325. loadStats["ALL"] = allProtocolsStats
  326. return loadStats
  327. }
  328. func (sshServer *sshServer) stopClients() {
  329. sshServer.clientsMutex.Lock()
  330. sshServer.stoppingClients = true
  331. clients := sshServer.clients
  332. sshServer.clients = make(map[sshClientID]*sshClient)
  333. sshServer.clientsMutex.Unlock()
  334. for _, client := range clients {
  335. client.stop()
  336. }
  337. }
  338. func (sshServer *sshServer) handleClient(tunnelProtocol string, clientConn net.Conn) {
  339. sshServer.registerAcceptedClient(tunnelProtocol)
  340. defer sshServer.unregisterAcceptedClient(tunnelProtocol)
  341. geoIPData := sshServer.support.GeoIPService.Lookup(
  342. common.IPAddressFromAddr(clientConn.RemoteAddr()))
  343. // TODO: apply reload of TrafficRulesSet to existing clients
  344. sshClient := newSshClient(
  345. sshServer,
  346. tunnelProtocol,
  347. geoIPData,
  348. sshServer.support.TrafficRulesSet.GetTrafficRules(geoIPData.Country))
  349. // Wrap the base client connection with an ActivityMonitoredConn which will
  350. // terminate the connection if no data is received before the deadline. This
  351. // timeout is in effect for the entire duration of the SSH connection. Clients
  352. // must actively use the connection or send SSH keep alive requests to keep
  353. // the connection active. Writes are not considered reliable activity indicators
  354. // due to buffering.
  355. activityConn, err := NewActivityMonitoredConn(
  356. clientConn,
  357. SSH_CONNECTION_READ_DEADLINE,
  358. false,
  359. nil)
  360. if err != nil {
  361. clientConn.Close()
  362. log.WithContextFields(LogFields{"error": err}).Error("NewActivityMonitoredConn failed")
  363. return
  364. }
  365. clientConn = activityConn
  366. // Further wrap the connection in a rate limiting ThrottledConn.
  367. clientConn = common.NewThrottledConn(
  368. clientConn, sshClient.trafficRules.GetRateLimits(tunnelProtocol))
  369. // Run the initial [obfuscated] SSH handshake in a goroutine so we can both
  370. // respect shutdownBroadcast and implement a specific handshake timeout.
  371. // The timeout is to reclaim network resources in case the handshake takes
  372. // too long.
  373. type sshNewServerConnResult struct {
  374. conn net.Conn
  375. sshConn *ssh.ServerConn
  376. channels <-chan ssh.NewChannel
  377. requests <-chan *ssh.Request
  378. err error
  379. }
  380. resultChannel := make(chan *sshNewServerConnResult, 2)
  381. if SSH_HANDSHAKE_TIMEOUT > 0 {
  382. time.AfterFunc(time.Duration(SSH_HANDSHAKE_TIMEOUT), func() {
  383. resultChannel <- &sshNewServerConnResult{err: errors.New("ssh handshake timeout")}
  384. })
  385. }
  386. go func(conn net.Conn) {
  387. sshServerConfig := &ssh.ServerConfig{
  388. PasswordCallback: sshClient.passwordCallback,
  389. AuthLogCallback: sshClient.authLogCallback,
  390. ServerVersion: sshServer.support.Config.SSHServerVersion,
  391. }
  392. sshServerConfig.AddHostKey(sshServer.sshHostKey)
  393. result := &sshNewServerConnResult{}
  394. // Wrap the connection in an SSH deobfuscator when required.
  395. if common.TunnelProtocolUsesObfuscatedSSH(tunnelProtocol) {
  396. // Note: NewObfuscatedSshConn blocks on network I/O
  397. // TODO: ensure this won't block shutdown
  398. conn, result.err = psiphon.NewObfuscatedSshConn(
  399. psiphon.OBFUSCATION_CONN_MODE_SERVER,
  400. conn,
  401. sshServer.support.Config.ObfuscatedSSHKey)
  402. if result.err != nil {
  403. result.err = common.ContextError(result.err)
  404. }
  405. }
  406. if result.err == nil {
  407. result.sshConn, result.channels, result.requests, result.err =
  408. ssh.NewServerConn(conn, sshServerConfig)
  409. }
  410. resultChannel <- result
  411. }(clientConn)
  412. var result *sshNewServerConnResult
  413. select {
  414. case result = <-resultChannel:
  415. case <-sshServer.shutdownBroadcast:
  416. // Close() will interrupt an ongoing handshake
  417. // TODO: wait for goroutine to exit before returning?
  418. clientConn.Close()
  419. return
  420. }
  421. if result.err != nil {
  422. clientConn.Close()
  423. // This is a Debug log due to noise. The handshake often fails due to I/O
  424. // errors as clients frequently interrupt connections in progress when
  425. // client-side load balancing completes a connection to a different server.
  426. log.WithContextFields(LogFields{"error": result.err}).Debug("handshake failed")
  427. return
  428. }
  429. sshClient.Lock()
  430. sshClient.sshConn = result.sshConn
  431. sshClient.activityConn = activityConn
  432. sshClient.Unlock()
  433. clientID, ok := sshServer.registerEstablishedClient(sshClient)
  434. if !ok {
  435. clientConn.Close()
  436. log.WithContext().Warning("register failed")
  437. return
  438. }
  439. defer sshServer.unregisterEstablishedClient(clientID)
  440. sshClient.runClient(result.channels, result.requests)
  441. // Note: sshServer.unregisterClient calls sshClient.Close(),
  442. // which also closes underlying transport Conn.
  443. }
  444. type sshClient struct {
  445. sync.Mutex
  446. sshServer *sshServer
  447. tunnelProtocol string
  448. sshConn ssh.Conn
  449. activityConn *ActivityMonitoredConn
  450. geoIPData GeoIPData
  451. psiphonSessionID string
  452. udpChannel ssh.Channel
  453. trafficRules TrafficRules
  454. tcpTrafficState *trafficState
  455. udpTrafficState *trafficState
  456. channelHandlerWaitGroup *sync.WaitGroup
  457. tcpPortForwardLRU *LRUConns
  458. stopBroadcast chan struct{}
  459. }
  460. type trafficState struct {
  461. // Note: 64-bit ints used with atomic operations are at placed
  462. // at the start of struct to ensure 64-bit alignment.
  463. // (https://golang.org/pkg/sync/atomic/#pkg-note-BUG)
  464. bytesUp int64
  465. bytesDown int64
  466. concurrentPortForwardCount int64
  467. peakConcurrentPortForwardCount int64
  468. totalPortForwardCount int64
  469. }
  470. func newSshClient(
  471. sshServer *sshServer, tunnelProtocol string, geoIPData GeoIPData, trafficRules TrafficRules) *sshClient {
  472. return &sshClient{
  473. sshServer: sshServer,
  474. tunnelProtocol: tunnelProtocol,
  475. geoIPData: geoIPData,
  476. trafficRules: trafficRules,
  477. tcpTrafficState: &trafficState{},
  478. udpTrafficState: &trafficState{},
  479. channelHandlerWaitGroup: new(sync.WaitGroup),
  480. tcpPortForwardLRU: NewLRUConns(),
  481. stopBroadcast: make(chan struct{}),
  482. }
  483. }
  484. func (sshClient *sshClient) passwordCallback(conn ssh.ConnMetadata, password []byte) (*ssh.Permissions, error) {
  485. var sshPasswordPayload struct {
  486. SessionId string `json:"SessionId"`
  487. SshPassword string `json:"SshPassword"`
  488. }
  489. err := json.Unmarshal(password, &sshPasswordPayload)
  490. if err != nil {
  491. // Backwards compatibility case: instead of a JSON payload, older clients
  492. // send the hex encoded session ID prepended to the SSH password.
  493. // Note: there's an even older case where clients don't send any session ID,
  494. // but that's no longer supported.
  495. if len(password) == 2*common.PSIPHON_API_CLIENT_SESSION_ID_LENGTH+2*SSH_PASSWORD_BYTE_LENGTH {
  496. sshPasswordPayload.SessionId = string(password[0 : 2*common.PSIPHON_API_CLIENT_SESSION_ID_LENGTH])
  497. sshPasswordPayload.SshPassword = string(password[2*common.PSIPHON_API_CLIENT_SESSION_ID_LENGTH : len(password)])
  498. } else {
  499. return nil, common.ContextError(fmt.Errorf("invalid password payload for %q", conn.User()))
  500. }
  501. }
  502. if !isHexDigits(sshClient.sshServer.support, sshPasswordPayload.SessionId) {
  503. return nil, common.ContextError(fmt.Errorf("invalid session ID for %q", conn.User()))
  504. }
  505. userOk := (subtle.ConstantTimeCompare(
  506. []byte(conn.User()), []byte(sshClient.sshServer.support.Config.SSHUserName)) == 1)
  507. passwordOk := (subtle.ConstantTimeCompare(
  508. []byte(sshPasswordPayload.SshPassword), []byte(sshClient.sshServer.support.Config.SSHPassword)) == 1)
  509. if !userOk || !passwordOk {
  510. return nil, common.ContextError(fmt.Errorf("invalid password for %q", conn.User()))
  511. }
  512. psiphonSessionID := sshPasswordPayload.SessionId
  513. sshClient.Lock()
  514. sshClient.psiphonSessionID = psiphonSessionID
  515. geoIPData := sshClient.geoIPData
  516. sshClient.Unlock()
  517. // Store the GeoIP data associated with the session ID. This makes the GeoIP data
  518. // available to the web server for web transport Psiphon API requests.
  519. sshClient.sshServer.support.GeoIPService.SetSessionCache(
  520. psiphonSessionID, geoIPData)
  521. return nil, nil
  522. }
  523. func (sshClient *sshClient) authLogCallback(conn ssh.ConnMetadata, method string, err error) {
  524. if err != nil {
  525. if method == "none" && err.Error() == "no auth passed yet" {
  526. // In this case, the callback invocation is noise from auth negotiation
  527. return
  528. }
  529. // Note: here we previously logged messages for fail2ban to act on. This is no longer
  530. // done as the complexity outweighs the benefits.
  531. //
  532. // - The SSH credential is not secret -- it's in the server entry. Attackers targetting
  533. // the server likely already have the credential. On the other hand, random scanning and
  534. // brute forcing is mitigated with high entropy random passwords, rate limiting
  535. // (implemented on the host via iptables), and limited capabilities (the SSH session can
  536. // only port forward).
  537. //
  538. // - fail2ban coverage was inconsistent; in the case of an unfronted meek protocol through
  539. // an upstream proxy, the remote address is the upstream proxy, which should not be blocked.
  540. // The X-Forwarded-For header cant be used instead as it may be forged and used to get IPs
  541. // deliberately blocked; and in any case fail2ban adds iptables rules which can only block
  542. // by direct remote IP, not by original client IP. Fronted meek has the same iptables issue.
  543. //
  544. // TODO: random scanning and brute forcing of port 22 will result in log noise. To eliminate
  545. // this, and to also cover meek protocols, and bad obfuscation keys, and bad inputs to the web
  546. // server, consider implementing fail2ban-type logic directly in this server, with the ability
  547. // to use X-Forwarded-For (when trustworthy; e.g, from a CDN).
  548. log.WithContextFields(LogFields{"error": err, "method": method}).Warning("authentication failed")
  549. } else {
  550. log.WithContextFields(LogFields{"error": err, "method": method}).Debug("authentication success")
  551. }
  552. }
  553. func (sshClient *sshClient) stop() {
  554. sshClient.sshConn.Close()
  555. sshClient.sshConn.Wait()
  556. close(sshClient.stopBroadcast)
  557. sshClient.channelHandlerWaitGroup.Wait()
  558. // Note: reporting duration based on last confirmed data transfer, which
  559. // is reads for sshClient.activityConn.GetActiveDuration(), and not
  560. // connection closing is important for protocols such as meek. For
  561. // meek, the connection remains open until the HTTP session expires,
  562. // which may be some time after the tunnel has closed. (The meek
  563. // protocol has no allowance for signalling payload EOF, and even if
  564. // it did the client may not have the opportunity to send a final
  565. // request with an EOF flag set.)
  566. sshClient.Lock()
  567. log.WithContextFields(
  568. LogFields{
  569. "startTime": sshClient.activityConn.GetStartTime(),
  570. "duration": sshClient.activityConn.GetActiveDuration(),
  571. "psiphonSessionID": sshClient.psiphonSessionID,
  572. "country": sshClient.geoIPData.Country,
  573. "city": sshClient.geoIPData.City,
  574. "ISP": sshClient.geoIPData.ISP,
  575. "bytesUpTCP": sshClient.tcpTrafficState.bytesUp,
  576. "bytesDownTCP": sshClient.tcpTrafficState.bytesDown,
  577. "peakConcurrentPortForwardCountTCP": sshClient.tcpTrafficState.peakConcurrentPortForwardCount,
  578. "totalPortForwardCountTCP": sshClient.tcpTrafficState.totalPortForwardCount,
  579. "bytesUpUDP": sshClient.udpTrafficState.bytesUp,
  580. "bytesDownUDP": sshClient.udpTrafficState.bytesDown,
  581. "peakConcurrentPortForwardCountUDP": sshClient.udpTrafficState.peakConcurrentPortForwardCount,
  582. "totalPortForwardCountUDP": sshClient.udpTrafficState.totalPortForwardCount,
  583. }).Info("tunnel closed")
  584. sshClient.Unlock()
  585. }
  586. // runClient handles/dispatches new channel and new requests from the client.
  587. // When the SSH client connection closes, both the channels and requests channels
  588. // will close and runClient will exit.
  589. func (sshClient *sshClient) runClient(
  590. channels <-chan ssh.NewChannel, requests <-chan *ssh.Request) {
  591. requestsWaitGroup := new(sync.WaitGroup)
  592. requestsWaitGroup.Add(1)
  593. go func() {
  594. defer requestsWaitGroup.Done()
  595. for request := range requests {
  596. // Requests are processed serially; API responses must be sent in request order.
  597. var responsePayload []byte
  598. var err error
  599. if request.Type == "keepalive@openssh.com" {
  600. // Keepalive requests have an empty response.
  601. } else {
  602. // All other requests are assumed to be API requests.
  603. responsePayload, err = sshAPIRequestHandler(
  604. sshClient.sshServer.support,
  605. sshClient.geoIPData,
  606. request.Type,
  607. request.Payload)
  608. }
  609. if err == nil {
  610. err = request.Reply(true, responsePayload)
  611. } else {
  612. log.WithContextFields(LogFields{"error": err}).Warning("request failed")
  613. err = request.Reply(false, nil)
  614. }
  615. if err != nil {
  616. log.WithContextFields(LogFields{"error": err}).Warning("response failed")
  617. }
  618. }
  619. }()
  620. for newChannel := range channels {
  621. if newChannel.ChannelType() != "direct-tcpip" {
  622. sshClient.rejectNewChannel(newChannel, ssh.Prohibited, "unknown or unsupported channel type")
  623. continue
  624. }
  625. // process each port forward concurrently
  626. sshClient.channelHandlerWaitGroup.Add(1)
  627. go sshClient.handleNewPortForwardChannel(newChannel)
  628. }
  629. requestsWaitGroup.Wait()
  630. }
  631. func (sshClient *sshClient) rejectNewChannel(newChannel ssh.NewChannel, reason ssh.RejectionReason, logMessage string) {
  632. log.WithContextFields(
  633. LogFields{
  634. "channelType": newChannel.ChannelType(),
  635. "logMessage": logMessage,
  636. "rejectReason": reason.String(),
  637. }).Warning("reject new channel")
  638. // Note: logMessage is internal, for logging only; just the RejectionReason is sent to the client
  639. newChannel.Reject(reason, reason.String())
  640. }
  641. func (sshClient *sshClient) handleNewPortForwardChannel(newChannel ssh.NewChannel) {
  642. defer sshClient.channelHandlerWaitGroup.Done()
  643. // http://tools.ietf.org/html/rfc4254#section-7.2
  644. var directTcpipExtraData struct {
  645. HostToConnect string
  646. PortToConnect uint32
  647. OriginatorIPAddress string
  648. OriginatorPort uint32
  649. }
  650. err := ssh.Unmarshal(newChannel.ExtraData(), &directTcpipExtraData)
  651. if err != nil {
  652. sshClient.rejectNewChannel(newChannel, ssh.Prohibited, "invalid extra data")
  653. return
  654. }
  655. // Intercept TCP port forwards to a specified udpgw server and handle directly.
  656. // TODO: also support UDP explicitly, e.g. with a custom "direct-udp" channel type?
  657. isUDPChannel := sshClient.sshServer.support.Config.UDPInterceptUdpgwServerAddress != "" &&
  658. sshClient.sshServer.support.Config.UDPInterceptUdpgwServerAddress ==
  659. fmt.Sprintf("%s:%d",
  660. directTcpipExtraData.HostToConnect,
  661. directTcpipExtraData.PortToConnect)
  662. if isUDPChannel {
  663. sshClient.handleUDPChannel(newChannel)
  664. } else {
  665. sshClient.handleTCPChannel(
  666. directTcpipExtraData.HostToConnect, int(directTcpipExtraData.PortToConnect), newChannel)
  667. }
  668. }
  669. func (sshClient *sshClient) isPortForwardPermitted(
  670. host string, port int, allowPorts []int, denyPorts []int) bool {
  671. if common.Contains(SSH_DISALLOWED_PORT_FORWARD_HOSTS, host) {
  672. return false
  673. }
  674. // TODO: faster lookup?
  675. if len(allowPorts) > 0 {
  676. for _, allowPort := range allowPorts {
  677. if port == allowPort {
  678. return true
  679. }
  680. }
  681. return false
  682. }
  683. if len(denyPorts) > 0 {
  684. for _, denyPort := range denyPorts {
  685. if port == denyPort {
  686. return false
  687. }
  688. }
  689. }
  690. return true
  691. }
  692. func (sshClient *sshClient) isPortForwardLimitExceeded(
  693. state *trafficState, maxPortForwardCount int) bool {
  694. limitExceeded := false
  695. if maxPortForwardCount > 0 {
  696. sshClient.Lock()
  697. limitExceeded = state.concurrentPortForwardCount >= int64(maxPortForwardCount)
  698. sshClient.Unlock()
  699. }
  700. return limitExceeded
  701. }
  702. func (sshClient *sshClient) openedPortForward(
  703. state *trafficState) {
  704. sshClient.Lock()
  705. state.concurrentPortForwardCount += 1
  706. if state.concurrentPortForwardCount > state.peakConcurrentPortForwardCount {
  707. state.peakConcurrentPortForwardCount = state.concurrentPortForwardCount
  708. }
  709. state.totalPortForwardCount += 1
  710. sshClient.Unlock()
  711. }
  712. func (sshClient *sshClient) closedPortForward(
  713. state *trafficState, bytesUp, bytesDown int64) {
  714. sshClient.Lock()
  715. state.concurrentPortForwardCount -= 1
  716. state.bytesUp += bytesUp
  717. state.bytesDown += bytesDown
  718. sshClient.Unlock()
  719. }
  720. func (sshClient *sshClient) handleTCPChannel(
  721. hostToConnect string,
  722. portToConnect int,
  723. newChannel ssh.NewChannel) {
  724. if !sshClient.isPortForwardPermitted(
  725. hostToConnect,
  726. portToConnect,
  727. sshClient.trafficRules.AllowTCPPorts,
  728. sshClient.trafficRules.DenyTCPPorts) {
  729. sshClient.rejectNewChannel(
  730. newChannel, ssh.Prohibited, "port forward not permitted")
  731. return
  732. }
  733. var bytesUp, bytesDown int64
  734. sshClient.openedPortForward(sshClient.tcpTrafficState)
  735. defer func() {
  736. sshClient.closedPortForward(
  737. sshClient.tcpTrafficState,
  738. atomic.LoadInt64(&bytesUp),
  739. atomic.LoadInt64(&bytesDown))
  740. }()
  741. // TOCTOU note: important to increment the port forward count (via
  742. // openPortForward) _before_ checking isPortForwardLimitExceeded
  743. // otherwise, the client could potentially consume excess resources
  744. // by initiating many port forwards concurrently.
  745. // TODO: close LRU connection (after successful Dial) instead of
  746. // rejecting new connection?
  747. if sshClient.isPortForwardLimitExceeded(
  748. sshClient.tcpTrafficState,
  749. sshClient.trafficRules.MaxTCPPortForwardCount) {
  750. // Close the oldest TCP port forward. CloseOldest() closes
  751. // the conn and the port forward's goroutine will complete
  752. // the cleanup asynchronously.
  753. //
  754. // Some known limitations:
  755. //
  756. // - Since CloseOldest() closes the upstream socket but does not
  757. // clean up all resources associated with the port forward. These
  758. // include the goroutine(s) relaying traffic as well as the SSH
  759. // channel. Closing the socket will interrupt the goroutines which
  760. // will then complete the cleanup. But, since the full cleanup is
  761. // asynchronous, there exists a possibility that a client can consume
  762. // more than max port forward resources -- just not upstream sockets.
  763. //
  764. // - An LRU list entry for this port forward is not added until
  765. // after the dial completes, but the port forward is counted
  766. // towards max limits. This means many dials in progress will
  767. // put established connections in jeopardy.
  768. //
  769. // - We're closing the oldest open connection _before_ successfully
  770. // dialing the new port forward. This means we are potentially
  771. // discarding a good connection to make way for a failed connection.
  772. // We cannot simply dial first and still maintain a limit on
  773. // resources used, so to address this we'd need to add some
  774. // accounting for connections still establishing.
  775. sshClient.tcpPortForwardLRU.CloseOldest()
  776. log.WithContextFields(
  777. LogFields{
  778. "maxCount": sshClient.trafficRules.MaxTCPPortForwardCount,
  779. }).Debug("closed LRU TCP port forward")
  780. }
  781. // Dial the target remote address. This is done in a goroutine to
  782. // ensure the shutdown signal is handled immediately.
  783. remoteAddr := fmt.Sprintf("%s:%d", hostToConnect, portToConnect)
  784. log.WithContextFields(LogFields{"remoteAddr": remoteAddr}).Debug("dialing")
  785. type dialTcpResult struct {
  786. conn net.Conn
  787. err error
  788. }
  789. resultChannel := make(chan *dialTcpResult, 1)
  790. go func() {
  791. // TODO: on EADDRNOTAVAIL, temporarily suspend new clients
  792. // TODO: IPv6 support
  793. conn, err := net.DialTimeout(
  794. "tcp4", remoteAddr, SSH_TCP_PORT_FORWARD_DIAL_TIMEOUT)
  795. resultChannel <- &dialTcpResult{conn, err}
  796. }()
  797. var result *dialTcpResult
  798. select {
  799. case result = <-resultChannel:
  800. case <-sshClient.stopBroadcast:
  801. // Note: may leave dial in progress
  802. return
  803. }
  804. if result.err != nil {
  805. sshClient.rejectNewChannel(newChannel, ssh.ConnectionFailed, result.err.Error())
  806. return
  807. }
  808. // The upstream TCP port forward connection has been established. Schedule
  809. // some cleanup and notify the SSH client that the channel is accepted.
  810. fwdConn := result.conn
  811. defer fwdConn.Close()
  812. fwdChannel, requests, err := newChannel.Accept()
  813. if err != nil {
  814. log.WithContextFields(LogFields{"error": err}).Warning("accept new channel failed")
  815. return
  816. }
  817. go ssh.DiscardRequests(requests)
  818. defer fwdChannel.Close()
  819. // ActivityMonitoredConn monitors the TCP port forward I/O and updates
  820. // its LRU status. ActivityMonitoredConn also times out I/O on the port
  821. // forward if both reads and writes have been idle for the specified
  822. // duration.
  823. lruEntry := sshClient.tcpPortForwardLRU.Add(fwdConn)
  824. defer lruEntry.Remove()
  825. fwdConn, err = NewActivityMonitoredConn(
  826. fwdConn,
  827. time.Duration(sshClient.trafficRules.IdleTCPPortForwardTimeoutMilliseconds)*time.Millisecond,
  828. true,
  829. lruEntry)
  830. if result.err != nil {
  831. log.WithContextFields(LogFields{"error": err}).Error("NewActivityMonitoredConn failed")
  832. return
  833. }
  834. // Relay channel to forwarded connection.
  835. log.WithContextFields(LogFields{"remoteAddr": remoteAddr}).Debug("relaying")
  836. // TODO: relay errors to fwdChannel.Stderr()?
  837. relayWaitGroup := new(sync.WaitGroup)
  838. relayWaitGroup.Add(1)
  839. go func() {
  840. defer relayWaitGroup.Done()
  841. // io.Copy allocates a 32K temporary buffer, and each port forward relay uses
  842. // two of these buffers; using io.CopyBuffer with a smaller buffer reduces the
  843. // overall memory footprint.
  844. bytes, err := io.CopyBuffer(
  845. fwdChannel, fwdConn, make([]byte, SSH_TCP_PORT_FORWARD_COPY_BUFFER_SIZE))
  846. atomic.AddInt64(&bytesDown, bytes)
  847. if err != nil && err != io.EOF {
  848. // Debug since errors such as "connection reset by peer" occur during normal operation
  849. log.WithContextFields(LogFields{"error": err}).Debug("downstream TCP relay failed")
  850. }
  851. // Interrupt upstream io.Copy when downstream is shutting down.
  852. // TODO: this is done to quickly cleanup the port forward when
  853. // fwdConn has a read timeout, but is it clean -- upstream may still
  854. // be flowing?
  855. fwdChannel.Close()
  856. }()
  857. bytes, err := io.CopyBuffer(
  858. fwdConn, fwdChannel, make([]byte, SSH_TCP_PORT_FORWARD_COPY_BUFFER_SIZE))
  859. atomic.AddInt64(&bytesUp, bytes)
  860. if err != nil && err != io.EOF {
  861. log.WithContextFields(LogFields{"error": err}).Debug("upstream TCP relay failed")
  862. }
  863. // Shutdown special case: fwdChannel will be closed and return EOF when
  864. // the SSH connection is closed, but we need to explicitly close fwdConn
  865. // to interrupt the downstream io.Copy, which may be blocked on a
  866. // fwdConn.Read().
  867. fwdConn.Close()
  868. relayWaitGroup.Wait()
  869. log.WithContextFields(
  870. LogFields{
  871. "remoteAddr": remoteAddr,
  872. "bytesUp": atomic.LoadInt64(&bytesUp),
  873. "bytesDown": atomic.LoadInt64(&bytesDown)}).Debug("exiting")
  874. }