socksProxy.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 "github.com/Psiphon-Inc/goptlib"
  23. "net"
  24. "sync"
  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. return nil, ContextError(err)
  45. }
  46. proxy = &SocksProxy{
  47. tunneler: tunneler,
  48. listener: listener,
  49. serveWaitGroup: new(sync.WaitGroup),
  50. openConns: new(Conns),
  51. stopListeningBroadcast: make(chan struct{}),
  52. }
  53. proxy.serveWaitGroup.Add(1)
  54. go proxy.serve()
  55. Notice(NOTICE_SOCKS_PROXY, "local SOCKS proxy running at address %s", proxy.listener.Addr().String())
  56. return proxy, nil
  57. }
  58. // Close terminates the listener and waits for the accept loop
  59. // goroutine to complete.
  60. func (proxy *SocksProxy) Close() {
  61. close(proxy.stopListeningBroadcast)
  62. proxy.listener.Close()
  63. proxy.serveWaitGroup.Wait()
  64. proxy.openConns.CloseAll()
  65. }
  66. func (proxy *SocksProxy) socksConnectionHandler(localConn *socks.SocksConn) (err error) {
  67. defer localConn.Close()
  68. defer proxy.openConns.Remove(localConn)
  69. proxy.openConns.Add(localConn)
  70. remoteConn, err := proxy.tunneler.Dial(localConn.Req.Target)
  71. if err != nil {
  72. return ContextError(err)
  73. }
  74. defer remoteConn.Close()
  75. err = localConn.Grant(&net.TCPAddr{IP: net.ParseIP("0.0.0.0"), Port: 0})
  76. if err != nil {
  77. return ContextError(err)
  78. }
  79. Relay(localConn, remoteConn)
  80. return nil
  81. }
  82. func (proxy *SocksProxy) serve() {
  83. defer proxy.listener.Close()
  84. defer proxy.serveWaitGroup.Done()
  85. loop:
  86. for {
  87. // Note: will be interrupted by listener.Close() call made by proxy.Close()
  88. socksConnection, err := proxy.listener.AcceptSocks()
  89. // Can't check for the exact error that Close() will cause in Accept(),
  90. // (see: https://code.google.com/p/go/issues/detail?id=4373). So using an
  91. // explicit stop signal to stop gracefully.
  92. select {
  93. case <-proxy.stopListeningBroadcast:
  94. break loop
  95. default:
  96. }
  97. if err != nil {
  98. Notice(NOTICE_ALERT, "SOCKS proxy accept error: %s", err)
  99. if e, ok := err.(net.Error); ok && !e.Temporary() {
  100. proxy.tunneler.SignalFailure()
  101. // Fatal error, stop the proxy
  102. break loop
  103. }
  104. // Temporary error, keep running
  105. continue
  106. }
  107. go func() {
  108. err := proxy.socksConnectionHandler(socksConnection)
  109. if err != nil {
  110. Notice(NOTICE_ALERT, "%s", ContextError(err))
  111. }
  112. }()
  113. }
  114. Notice(NOTICE_SOCKS_PROXY, "SOCKS proxy stopped")
  115. }