clientlib.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * Copyright (c) 2018, 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 clientlib
  20. import (
  21. "context"
  22. "encoding/json"
  23. std_errors "errors"
  24. "fmt"
  25. "io"
  26. "net"
  27. "path/filepath"
  28. "sync"
  29. "sync/atomic"
  30. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon"
  31. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  32. )
  33. // Parameters provide an easier way to modify the tunnel config at runtime.
  34. type Parameters struct {
  35. // Used as the directory for the datastore, remote server list, and obfuscasted
  36. // server list.
  37. // Empty string means the default will be used (current working directory).
  38. // nil means the values in the config file will be used.
  39. // Optional, but strongly recommended.
  40. DataRootDirectory *string
  41. // Overrides config.ClientPlatform. See config.go for details.
  42. // nil means the value in the config file will be used.
  43. // Optional, but strongly recommended.
  44. ClientPlatform *string
  45. // Overrides config.NetworkID. For details see:
  46. // https://godoc.org/github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon#NetworkIDGetter
  47. // nil means the value in the config file will be used. (If not set in the config,
  48. // an error will result.)
  49. // Empty string will produce an error.
  50. // Optional, but strongly recommended.
  51. NetworkID *string
  52. // Overrides config.EstablishTunnelTimeoutSeconds. See config.go for details.
  53. // nil means the EstablishTunnelTimeoutSeconds value in the config file will be used.
  54. // If there's no such value in the config file, the default will be used.
  55. // Zero means there will be no timeout.
  56. // Optional.
  57. EstablishTunnelTimeoutSeconds *int
  58. // EmitDiagnosticNoticesToFile indicates whether to use the rotating log file
  59. // facility to record diagnostic notices instead of sending diagnostic
  60. // notices to noticeReceiver. Has no effect unless the tunnel
  61. // config.EmitDiagnosticNotices flag is set.
  62. EmitDiagnosticNoticesToFiles bool
  63. // DisableLocalSocksProxy disables running the local SOCKS proxy.
  64. DisableLocalSocksProxy *bool
  65. // DisableLocalHTTPProxy disables running the local HTTP proxy.
  66. DisableLocalHTTPProxy *bool
  67. }
  68. // PsiphonTunnel is the tunnel object. It can be used for stopping the tunnel and
  69. // retrieving proxy ports.
  70. type PsiphonTunnel struct {
  71. mu sync.Mutex
  72. stop func()
  73. embeddedServerListWaitGroup sync.WaitGroup
  74. controllerWaitGroup sync.WaitGroup
  75. controllerDial func(string, net.Conn) (net.Conn, error)
  76. // The port on which the HTTP proxy is running
  77. HTTPProxyPort int
  78. // The port on which the SOCKS proxy is running
  79. SOCKSProxyPort int
  80. }
  81. // ParametersDelta allows for fine-grained modification of parameters.Parameters.
  82. // NOTE: Ordinary users of this library should never need this.
  83. type ParametersDelta map[string]interface{}
  84. // NoticeEvent represents the notices emitted by tunnel core. It will be passed to
  85. // noticeReceiver, if supplied.
  86. // NOTE: Ordinary users of this library should never need this.
  87. type NoticeEvent struct {
  88. Data map[string]interface{} `json:"data"`
  89. Type string `json:"noticeType"`
  90. Timestamp string `json:"timestamp"`
  91. }
  92. // ErrTimeout is returned when the tunnel establishment attempt fails due to timeout
  93. var ErrTimeout = std_errors.New("clientlib: tunnel establishment timeout")
  94. var errMultipleStart = std_errors.New("clientlib: StartTunnel called multiple times")
  95. // started is used to ensure that only one tunnel is started at a time
  96. var started atomic.Bool
  97. // StartTunnel establishes a Psiphon tunnel. It returns an error if the establishment
  98. // was not successful. If the returned error is nil, the returned tunnel can be used
  99. // to find out the proxy ports and subsequently stop the tunnel.
  100. //
  101. // ctx may be cancelable, if the caller wants to be able to interrupt the establishment
  102. // attempt, or context.Background().
  103. //
  104. // configJSON will be passed to psiphon.LoadConfig to configure the tunnel. Required.
  105. //
  106. // embeddedServerEntryList is the encoded embedded server entry list. It is optional.
  107. //
  108. // params are config values that typically need to be overridden at runtime.
  109. //
  110. // paramsDelta contains changes that will be applied to the Parameters.
  111. // NOTE: Ordinary users of this library should never need this and should pass nil.
  112. //
  113. // noticeReceiver, if non-nil, will be called for each notice emitted by tunnel core.
  114. // NOTE: Ordinary users of this library should never need this and should pass nil.
  115. func StartTunnel(
  116. ctx context.Context,
  117. configJSON []byte,
  118. embeddedServerEntryList string,
  119. params Parameters,
  120. paramsDelta ParametersDelta,
  121. noticeReceiver func(NoticeEvent)) (retTunnel *PsiphonTunnel, retErr error) {
  122. if !started.CompareAndSwap(false, true) {
  123. return nil, errMultipleStart
  124. }
  125. config, err := psiphon.LoadConfig(configJSON)
  126. if err != nil {
  127. return nil, errors.TraceMsg(err, "failed to load config file")
  128. }
  129. // Use params.DataRootDirectory to set related config values.
  130. if params.DataRootDirectory != nil {
  131. config.DataRootDirectory = *params.DataRootDirectory
  132. // Migrate old fields
  133. config.MigrateDataStoreDirectory = *params.DataRootDirectory
  134. config.MigrateObfuscatedServerListDownloadDirectory = *params.DataRootDirectory
  135. config.MigrateRemoteServerListDownloadFilename = filepath.Join(*params.DataRootDirectory, "server_list_compressed")
  136. }
  137. if params.NetworkID != nil {
  138. config.NetworkID = *params.NetworkID
  139. }
  140. if params.ClientPlatform != nil {
  141. config.ClientPlatform = *params.ClientPlatform
  142. } // else use the value in config
  143. if params.EstablishTunnelTimeoutSeconds != nil {
  144. config.EstablishTunnelTimeoutSeconds = params.EstablishTunnelTimeoutSeconds
  145. } // else use the value in config
  146. if config.UseNoticeFiles == nil && config.EmitDiagnosticNotices && params.EmitDiagnosticNoticesToFiles {
  147. config.UseNoticeFiles = &psiphon.UseNoticeFiles{
  148. RotatingFileSize: 0,
  149. RotatingSyncFrequency: 0,
  150. }
  151. } // else use the value in the config
  152. if params.DisableLocalSocksProxy != nil {
  153. config.DisableLocalSocksProxy = *params.DisableLocalSocksProxy
  154. } // else use the value in the config
  155. if params.DisableLocalHTTPProxy != nil {
  156. config.DisableLocalHTTPProxy = *params.DisableLocalHTTPProxy
  157. } // else use the value in the config
  158. // config.Commit must be called before calling config.SetParameters
  159. // or attempting to connect.
  160. err = config.Commit(true)
  161. if err != nil {
  162. return nil, errors.TraceMsg(err, "config.Commit failed")
  163. }
  164. // If supplied, apply the parameters delta
  165. if len(paramsDelta) > 0 {
  166. err = config.SetParameters("", false, paramsDelta)
  167. if err != nil {
  168. return nil, errors.TraceMsg(err, fmt.Sprintf("SetParameters failed for delta: %v", paramsDelta))
  169. }
  170. }
  171. // Will be closed when the tunnel has successfully connected
  172. connectedSignal := make(chan struct{})
  173. // Will receive a value if an error occurs during the connection sequence
  174. erroredCh := make(chan error, 1)
  175. // Create the tunnel object
  176. tunnel := new(PsiphonTunnel)
  177. // Set up notice handling
  178. psiphon.SetNoticeWriter(psiphon.NewNoticeReceiver(
  179. func(notice []byte) {
  180. var event NoticeEvent
  181. err := json.Unmarshal(notice, &event)
  182. if err != nil {
  183. // This is unexpected and probably indicates something fatal has occurred.
  184. // We'll interpret it as a connection error and abort.
  185. err = errors.TraceMsg(err, "failed to unmarshal notice JSON")
  186. select {
  187. case erroredCh <- err:
  188. default:
  189. }
  190. return
  191. }
  192. if event.Type == "ListeningHttpProxyPort" {
  193. port := event.Data["port"].(float64)
  194. tunnel.HTTPProxyPort = int(port)
  195. } else if event.Type == "ListeningSocksProxyPort" {
  196. port := event.Data["port"].(float64)
  197. tunnel.SOCKSProxyPort = int(port)
  198. } else if event.Type == "EstablishTunnelTimeout" {
  199. select {
  200. case erroredCh <- ErrTimeout:
  201. default:
  202. }
  203. } else if event.Type == "Tunnels" {
  204. count := event.Data["count"].(float64)
  205. if count > 0 {
  206. close(connectedSignal)
  207. }
  208. }
  209. // Some users of this package may need to add special processing of notices.
  210. // If the caller has requested it, we'll pass on the notices.
  211. if noticeReceiver != nil {
  212. noticeReceiver(event)
  213. }
  214. }))
  215. err = psiphon.OpenDataStore(config)
  216. if err != nil {
  217. return nil, errors.TraceMsg(err, "failed to open data store")
  218. }
  219. // Create a cancelable context that will be used for stopping the tunnel
  220. tunnelCtx, cancelTunnelCtx := context.WithCancel(ctx)
  221. // Because the tunnel object is only returned on success, there are at least two
  222. // problems that we don't need to worry about:
  223. // 1. This stop function is called both by the error-defer here and by a call to the
  224. // tunnel's Stop method.
  225. // 2. This stop function is called via the tunnel's Stop method before the WaitGroups
  226. // are incremented (causing a race condition).
  227. tunnel.stop = func() {
  228. cancelTunnelCtx()
  229. tunnel.embeddedServerListWaitGroup.Wait()
  230. tunnel.controllerWaitGroup.Wait()
  231. psiphon.CloseDataStore()
  232. started.Store(false)
  233. }
  234. defer func() {
  235. if retErr != nil {
  236. tunnel.stop()
  237. }
  238. }()
  239. // If specified, the embedded server list is loaded and stored. When there
  240. // are no server candidates at all, we wait for this import to complete
  241. // before starting the Psiphon controller. Otherwise, we import while
  242. // concurrently starting the controller to minimize delay before attempting
  243. // to connect to existing candidate servers.
  244. //
  245. // If the import fails, an error notice is emitted, but the controller is
  246. // still started: either existing candidate servers may suffice, or the
  247. // remote server list fetch may obtain candidate servers.
  248. //
  249. // The import will be interrupted if it's still running when the controller
  250. // is stopped.
  251. tunnel.embeddedServerListWaitGroup.Add(1)
  252. go func() {
  253. defer tunnel.embeddedServerListWaitGroup.Done()
  254. err := psiphon.ImportEmbeddedServerEntries(
  255. tunnelCtx,
  256. config,
  257. "",
  258. embeddedServerEntryList)
  259. if err != nil {
  260. psiphon.NoticeError("error importing embedded server entry list: %s", err)
  261. return
  262. }
  263. }()
  264. if !psiphon.HasServerEntries() {
  265. psiphon.NoticeInfo("awaiting embedded server entry list import")
  266. tunnel.embeddedServerListWaitGroup.Wait()
  267. }
  268. // Create the Psiphon controller
  269. controller, err := psiphon.NewController(config)
  270. if err != nil {
  271. return nil, errors.TraceMsg(err, "psiphon.NewController failed")
  272. }
  273. tunnel.controllerDial = controller.Dial
  274. // Begin tunnel connection
  275. tunnel.controllerWaitGroup.Add(1)
  276. go func() {
  277. defer tunnel.controllerWaitGroup.Done()
  278. // Start the tunnel. Only returns on error (or internal timeout).
  279. controller.Run(tunnelCtx)
  280. // controller.Run does not exit until the goroutine that posts
  281. // EstablishTunnelTimeout has terminated; so, if there was a
  282. // EstablishTunnelTimeout event, ErrTimeout is guaranteed to be sent to
  283. // errored before this next error and will be the StartTunnel return value.
  284. err := ctx.Err()
  285. switch err {
  286. case context.DeadlineExceeded:
  287. err = ErrTimeout
  288. case context.Canceled:
  289. err = errors.TraceMsg(err, "StartTunnel canceled")
  290. default:
  291. err = errors.TraceMsg(err, "controller.Run exited unexpectedly")
  292. }
  293. select {
  294. case erroredCh <- err:
  295. default:
  296. }
  297. }()
  298. // Wait for an active tunnel or error
  299. select {
  300. case <-connectedSignal:
  301. return tunnel, nil
  302. case err := <-erroredCh:
  303. if err != ErrTimeout {
  304. err = errors.TraceMsg(err, "tunnel start produced error")
  305. }
  306. return nil, err
  307. }
  308. }
  309. // Stop stops/disconnects/shuts down the tunnel.
  310. // It is safe to call Stop multiple times.
  311. // It is safe to call concurrently with Dial and with itself.
  312. func (tunnel *PsiphonTunnel) Stop() {
  313. // Holding a lock while calling the stop function ensures that any concurrent call
  314. // to Stop will wait for the first call to finish before returning, rather than
  315. // returning immediately (because tunnel.stop is nil) and thereby indicating
  316. // (erroneously) that the tunnel has been stopped.
  317. // Stopping a tunnel happens quickly enough that this processing block shouldn't be
  318. // a problem.
  319. tunnel.mu.Lock()
  320. defer tunnel.mu.Unlock()
  321. if tunnel.stop == nil {
  322. return
  323. }
  324. tunnel.stop()
  325. tunnel.stop = nil
  326. tunnel.controllerDial = nil
  327. // Clear our notice receiver, as it is no longer needed and we should let it be
  328. // garbage-collected.
  329. psiphon.SetNoticeWriter(io.Discard)
  330. }
  331. // Dial connects to the specified address through the Psiphon tunnel.
  332. // It is safe to call Dial after the tunnel has been stopped.
  333. // It is safe to call Dial concurrently with Stop.
  334. func (tunnel *PsiphonTunnel) Dial(remoteAddr string) (conn net.Conn, err error) {
  335. // Ensure the dial is accessed in a thread-safe manner, without holding the lock
  336. // while calling the dial function.
  337. // Note that it is safe for controller.Dial to be called even after or during a tunnel
  338. // shutdown (i.e., if the context has been canceled).
  339. tunnel.mu.Lock()
  340. dial := tunnel.controllerDial
  341. tunnel.mu.Unlock()
  342. if dial == nil {
  343. return nil, errors.TraceNew("tunnel not started")
  344. }
  345. return dial(remoteAddr, nil)
  346. }