socksProxy.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (c) 2015, 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. "net"
  23. "sync"
  24. socks "github.com/Psiphon-Inc/goptlib"
  25. )
  26. // SocksProxy is a SOCKS server that accepts local host connections
  27. // and, for each connection, establishes a port forward through
  28. // the tunnel SSH client and relays traffic through the port
  29. // forward.
  30. type SocksProxy struct {
  31. tunneler Tunneler
  32. listener *socks.SocksListener
  33. serveWaitGroup *sync.WaitGroup
  34. openConns *Conns
  35. stopListeningBroadcast chan struct{}
  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(config *Config, tunneler Tunneler) (proxy *SocksProxy, err error) {
  41. listener, err := socks.ListenSocks(
  42. "tcp", fmt.Sprintf("127.0.0.1:%d", config.LocalSocksProxyPort))
  43. if err != nil {
  44. if IsAddressInUseError(err) {
  45. NoticeSocksProxyPortInUse(config.LocalSocksProxyPort)
  46. }
  47. return nil, ContextError(err)
  48. }
  49. proxy = &SocksProxy{
  50. tunneler: tunneler,
  51. listener: listener,
  52. serveWaitGroup: new(sync.WaitGroup),
  53. openConns: new(Conns),
  54. stopListeningBroadcast: make(chan struct{}),
  55. }
  56. proxy.serveWaitGroup.Add(1)
  57. go proxy.serve()
  58. NoticeListeningSocksProxyPort(proxy.listener.Addr().(*net.TCPAddr).Port)
  59. return proxy, nil
  60. }
  61. // Close terminates the listener and waits for the accept loop
  62. // goroutine to complete.
  63. func (proxy *SocksProxy) Close() {
  64. close(proxy.stopListeningBroadcast)
  65. proxy.listener.Close()
  66. proxy.serveWaitGroup.Wait()
  67. proxy.openConns.CloseAll()
  68. }
  69. func (proxy *SocksProxy) socksConnectionHandler(localConn *socks.SocksConn) (err error) {
  70. defer localConn.Close()
  71. defer proxy.openConns.Remove(localConn)
  72. proxy.openConns.Add(localConn)
  73. // Using downstreamConn so localConn.Close() will be called when remoteConn.Close() is called.
  74. // This ensures that the downstream client (e.g., web browser) doesn't keep waiting on the
  75. // open connection for data which will never arrive.
  76. remoteConn, err := proxy.tunneler.Dial(localConn.Req.Target, false, localConn)
  77. if err != nil {
  78. return ContextError(err)
  79. }
  80. defer remoteConn.Close()
  81. err = localConn.Grant(&net.TCPAddr{IP: net.ParseIP("0.0.0.0"), Port: 0})
  82. if err != nil {
  83. return ContextError(err)
  84. }
  85. Relay(localConn, remoteConn)
  86. return nil
  87. }
  88. func (proxy *SocksProxy) serve() {
  89. defer proxy.listener.Close()
  90. defer proxy.serveWaitGroup.Done()
  91. loop:
  92. for {
  93. // Note: will be interrupted by listener.Close() call made by proxy.Close()
  94. socksConnection, err := proxy.listener.AcceptSocks()
  95. // Can't check for the exact error that Close() will cause in Accept(),
  96. // (see: https://code.google.com/p/go/issues/detail?id=4373). So using an
  97. // explicit stop signal to stop gracefully.
  98. select {
  99. case <-proxy.stopListeningBroadcast:
  100. break loop
  101. default:
  102. }
  103. if err != nil {
  104. NoticeAlert("SOCKS proxy accept error: %s", err)
  105. if e, ok := err.(net.Error); ok && e.Temporary() {
  106. // Temporary error, keep running
  107. continue
  108. }
  109. // Fatal error, stop the proxy
  110. proxy.tunneler.SignalComponentFailure()
  111. break loop
  112. }
  113. go func() {
  114. err := proxy.socksConnectionHandler(socksConnection)
  115. if err != nil {
  116. NoticeAlert("%s", ContextError(err))
  117. }
  118. }()
  119. }
  120. NoticeInfo("SOCKS proxy stopped")
  121. }