pluginProtocol.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Copyright (c) 2017, 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. "io"
  22. "net"
  23. "sync/atomic"
  24. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  25. )
  26. var registeredPluginProtocolDialer atomic.Value
  27. // PluginProtocolNetDialer is a base network dialer that's used
  28. // by PluginProtocolDialer to make its IP network connections. This
  29. // is used, for example, to create TCPConns as the base TCP
  30. // connections used by the plugin protocol.
  31. type PluginProtocolNetDialer func(network, addr string) (net.Conn, error)
  32. // PluginProtocolDialer creates a connection to addr over a
  33. // plugin protocol. It uses netDialer to create its base network
  34. // connection(s) and sends its log messages to loggerOutput.
  35. // PluginProtocolDialer returns true if it attempts to create
  36. // a connection, or false if it decides not to attempt a connection.
  37. // PluginProtocolDialer must add its connection to pendingConns
  38. // before the initial dial to allow for interruption.
  39. type PluginProtocolDialer func(
  40. config *Config,
  41. loggerOutput io.Writer,
  42. pendingConns *common.Conns,
  43. netDialer PluginProtocolNetDialer,
  44. addr string) (
  45. bool, net.Conn, error)
  46. // RegisterPluginProtocol sets the current plugin protocol
  47. // dialer.
  48. func RegisterPluginProtocol(protcolDialer PluginProtocolDialer) {
  49. registeredPluginProtocolDialer.Store(protcolDialer)
  50. }
  51. // DialPluginProtocol uses the current plugin protocol dialer,
  52. // if set, to connect to addr over the plugin protocol.
  53. func DialPluginProtocol(
  54. config *Config,
  55. loggerOutput io.Writer,
  56. pendingConns *common.Conns,
  57. netDialer PluginProtocolNetDialer,
  58. addr string) (
  59. bool, net.Conn, error) {
  60. dialer := registeredPluginProtocolDialer.Load()
  61. if dialer != nil {
  62. return dialer.(PluginProtocolDialer)(
  63. config, loggerOutput, pendingConns, netDialer, addr)
  64. }
  65. return false, nil, nil
  66. }