tunnelServer.go 34 KB

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