psi.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 psi
  20. // This package is a shim between Java and the "psiphon" package. Due to limitations
  21. // on what Go types may be exposed (http://godoc.org/golang.org/x/mobile/cmd/gobind),
  22. // a psiphon.Controller cannot be directly used by Java. This shim exposes a trivial
  23. // Start/Stop interface on top of a single Controller instance.
  24. import (
  25. "fmt"
  26. "sync"
  27. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon"
  28. )
  29. type PsiphonProvider interface {
  30. Notice(noticeJSON string)
  31. HasNetworkConnectivity() int
  32. BindToDevice(fileDescriptor int) error
  33. GetDnsServer() string
  34. }
  35. var controller *psiphon.Controller
  36. var shutdownBroadcast chan struct{}
  37. var controllerWaitGroup *sync.WaitGroup
  38. func Start(
  39. configJson, embeddedServerEntryList string,
  40. provider PsiphonProvider,
  41. useDeviceBinder bool) error {
  42. if controller != nil {
  43. return fmt.Errorf("already started")
  44. }
  45. config, err := psiphon.LoadConfig([]byte(configJson))
  46. if err != nil {
  47. return fmt.Errorf("error loading configuration file: %s", err)
  48. }
  49. config.NetworkConnectivityChecker = provider
  50. if useDeviceBinder {
  51. config.DeviceBinder = provider
  52. config.DnsServerGetter = provider
  53. }
  54. psiphon.SetNoticeOutput(psiphon.NewNoticeReceiver(
  55. func(notice []byte) {
  56. provider.Notice(string(notice))
  57. }))
  58. // TODO: should following errors be Notices?
  59. err = psiphon.InitDataStore(config)
  60. if err != nil {
  61. return fmt.Errorf("error initializing datastore: %s", err)
  62. }
  63. // If specified, the embedded server list is loaded and stored. When there
  64. // are no server candidates at all, we wait for this import to complete
  65. // before starting the Psiphon controller. Otherwise, we import while
  66. // concurrently starting the controller to minimize delay before attempting
  67. // to connect to existing candidate servers.
  68. // If the import fails, an error notice is emitted, but the controller is
  69. // still started: either existing candidate servers may suffice, or the
  70. // remote server list fetch may obtain candidate servers.
  71. // TODO: duplicates logic in psiphonClient.go -- refactor?
  72. if embeddedServerEntryList != "" {
  73. embeddedServerListWaitGroup := new(sync.WaitGroup)
  74. embeddedServerListWaitGroup.Add(1)
  75. go func() {
  76. defer embeddedServerListWaitGroup.Done()
  77. // TODO: stream embedded server list data?
  78. serverEntries, err := psiphon.DecodeAndValidateServerEntryList(embeddedServerEntryList)
  79. if err != nil {
  80. psiphon.NoticeError("error decoding embedded server entry list file: %s", err)
  81. return
  82. }
  83. // Since embedded server list entries may become stale, they will not
  84. // overwrite existing stored entries for the same server.
  85. err = psiphon.StoreServerEntries(serverEntries, false)
  86. if err != nil {
  87. psiphon.NoticeError("error storing embedded server entry list data: %s", err)
  88. return
  89. }
  90. }()
  91. if psiphon.CountServerEntries(config.EgressRegion, config.TunnelProtocol) == 0 {
  92. embeddedServerListWaitGroup.Wait()
  93. } else {
  94. defer embeddedServerListWaitGroup.Wait()
  95. }
  96. }
  97. controller, err = psiphon.NewController(config)
  98. if err != nil {
  99. return fmt.Errorf("error initializing controller: %s", err)
  100. }
  101. shutdownBroadcast = make(chan struct{})
  102. controllerWaitGroup = new(sync.WaitGroup)
  103. controllerWaitGroup.Add(1)
  104. go func() {
  105. defer controllerWaitGroup.Done()
  106. controller.Run(shutdownBroadcast)
  107. }()
  108. return nil
  109. }
  110. func Stop() {
  111. if controller != nil {
  112. close(shutdownBroadcast)
  113. controllerWaitGroup.Wait()
  114. controller = nil
  115. shutdownBroadcast = nil
  116. controllerWaitGroup = nil
  117. }
  118. }