socksProxy.go 4.2 KB

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