socksProxy.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (c) 2014, 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 psiphon
  20. import (
  21. "fmt"
  22. socks "git.torproject.org/pluggable-transports/goptlib.git"
  23. "io"
  24. "net"
  25. "sync"
  26. )
  27. // SocksProxy is a SOCKS server that accepts local host connections
  28. // and, for each connection, establishes a port forward through
  29. // the tunnel SSH client and relays traffic through the port
  30. // forward.
  31. type SocksProxy struct {
  32. tunnel *Tunnel
  33. stoppedSignal chan struct{}
  34. listener *socks.SocksListener
  35. waitGroup *sync.WaitGroup
  36. }
  37. // NewSocksProxy initializes a new SOCKS server. It begins listening for
  38. // connections, starts a goroutine that runs an accept loop, and returns
  39. // leaving the accept loop running.
  40. func NewSocksProxy(listenPort int, tunnel *Tunnel, stoppedSignal chan struct{}) (proxy *SocksProxy, err error) {
  41. listener, err := socks.ListenSocks("tcp", fmt.Sprintf("127.0.0.1:%d", listenPort))
  42. if err != nil {
  43. return nil, err
  44. }
  45. proxy = &SocksProxy{
  46. tunnel: tunnel,
  47. stoppedSignal: stoppedSignal,
  48. listener: listener,
  49. waitGroup: new(sync.WaitGroup)}
  50. proxy.waitGroup.Add(1)
  51. go proxy.acceptSocksConnections()
  52. Notice(NOTICE_SOCKS_PROXY, "local SOCKS proxy running at address %s", proxy.listener.Addr().String())
  53. return proxy, nil
  54. }
  55. // Close terminates the listener and waits for the accept loop
  56. // goroutine to complete.
  57. func (proxy *SocksProxy) Close() {
  58. proxy.listener.Close()
  59. proxy.waitGroup.Wait()
  60. }
  61. func socksConnectionHandler(tunnel *Tunnel, localSocksConn *socks.SocksConn) (err error) {
  62. defer localSocksConn.Close()
  63. remoteSshForward, err := tunnel.sshClient.Dial("tcp", localSocksConn.Req.Target)
  64. if err != nil {
  65. return ContextError(err)
  66. }
  67. defer remoteSshForward.Close()
  68. err = localSocksConn.Grant(&net.TCPAddr{IP: net.ParseIP("0.0.0.0"), Port: 0})
  69. if err != nil {
  70. return ContextError(err)
  71. }
  72. relayPortForward(localSocksConn, remoteSshForward)
  73. return nil
  74. }
  75. // relayPortForward is also used by HttpProxy
  76. func relayPortForward(local, remote net.Conn) {
  77. // TODO: page view stats would be done here
  78. // TODO: interrupt and stop on proxy.Close()
  79. waitGroup := new(sync.WaitGroup)
  80. waitGroup.Add(1)
  81. go func() {
  82. defer waitGroup.Done()
  83. _, err := io.Copy(local, remote)
  84. if err != nil {
  85. Notice(NOTICE_ALERT, "%s", ContextError(err))
  86. }
  87. }()
  88. _, err := io.Copy(remote, local)
  89. if err != nil {
  90. Notice(NOTICE_ALERT, "%s", ContextError(err))
  91. }
  92. waitGroup.Wait()
  93. }
  94. func (proxy *SocksProxy) acceptSocksConnections() {
  95. defer proxy.listener.Close()
  96. defer proxy.waitGroup.Done()
  97. for {
  98. // Note: will be interrupted by listener.Close() call made by proxy.Close()
  99. socksConnection, err := proxy.listener.AcceptSocks()
  100. if err != nil {
  101. Notice(NOTICE_ALERT, "SOCKS proxy accept error: %s", err)
  102. if e, ok := err.(net.Error); ok && !e.Temporary() {
  103. select {
  104. case proxy.stoppedSignal <- *new(struct{}):
  105. default:
  106. }
  107. // Fatal error, stop the proxy
  108. break
  109. }
  110. // Temporary error, keep running
  111. continue
  112. }
  113. go func() {
  114. err := socksConnectionHandler(proxy.tunnel, socksConnection)
  115. if err != nil {
  116. Notice(NOTICE_ALERT, "%s", ContextError(err))
  117. }
  118. }()
  119. }
  120. Notice(NOTICE_SOCKS_PROXY, "SOCKS proxy stopped")
  121. }