psi.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  29. )
  30. type PsiphonProvider interface {
  31. Notice(noticeJSON string)
  32. HasNetworkConnectivity() int
  33. BindToDevice(fileDescriptor int) error
  34. GetPrimaryDnsServer() string
  35. GetSecondaryDnsServer() string
  36. }
  37. var controllerMutex sync.Mutex
  38. var controller *psiphon.Controller
  39. var shutdownBroadcast chan struct{}
  40. var controllerWaitGroup *sync.WaitGroup
  41. func Start(
  42. configJson, embeddedServerEntryList string,
  43. provider PsiphonProvider,
  44. useDeviceBinder bool) error {
  45. controllerMutex.Lock()
  46. defer controllerMutex.Unlock()
  47. if controller != nil {
  48. return fmt.Errorf("already started")
  49. }
  50. config, err := psiphon.LoadConfig([]byte(configJson))
  51. if err != nil {
  52. return fmt.Errorf("error loading configuration file: %s", err)
  53. }
  54. config.NetworkConnectivityChecker = provider
  55. if useDeviceBinder {
  56. config.DeviceBinder = provider
  57. config.DnsServerGetter = provider
  58. }
  59. psiphon.SetNoticeOutput(psiphon.NewNoticeReceiver(
  60. func(notice []byte) {
  61. provider.Notice(string(notice))
  62. }))
  63. psiphon.EmitNoticeBuildInfo()
  64. // TODO: should following errors be Notices?
  65. err = psiphon.InitDataStore(config)
  66. if err != nil {
  67. return fmt.Errorf("error initializing datastore: %s", err)
  68. }
  69. serverEntries, err := psiphon.DecodeAndValidateServerEntryList(
  70. embeddedServerEntryList,
  71. common.GetCurrentTimestamp(),
  72. common.SERVER_ENTRY_SOURCE_EMBEDDED)
  73. if err != nil {
  74. return fmt.Errorf("error decoding embedded server entry list: %s", err)
  75. }
  76. err = psiphon.StoreServerEntries(serverEntries, false)
  77. if err != nil {
  78. return fmt.Errorf("error storing embedded server entry list: %s", err)
  79. }
  80. controller, err = psiphon.NewController(config)
  81. if err != nil {
  82. return fmt.Errorf("error initializing controller: %s", err)
  83. }
  84. shutdownBroadcast = make(chan struct{})
  85. controllerWaitGroup = new(sync.WaitGroup)
  86. controllerWaitGroup.Add(1)
  87. go func() {
  88. defer controllerWaitGroup.Done()
  89. controller.Run(shutdownBroadcast)
  90. }()
  91. return nil
  92. }
  93. func Stop() {
  94. controllerMutex.Lock()
  95. defer controllerMutex.Unlock()
  96. if controller != nil {
  97. close(shutdownBroadcast)
  98. controllerWaitGroup.Wait()
  99. controller = nil
  100. shutdownBroadcast = nil
  101. controllerWaitGroup = nil
  102. }
  103. }
  104. // This is a passthrough to Controller.SetClientVerificationPayloadForActiveTunnels.
  105. // Note: should only be called after Start() and before Stop(); otherwise,
  106. // will silently take no action.
  107. func SetClientVerificationPayload(clientVerificationPayload string) {
  108. controllerMutex.Lock()
  109. defer controllerMutex.Unlock()
  110. if controller != nil {
  111. controller.SetClientVerificationPayloadForActiveTunnels(clientVerificationPayload)
  112. }
  113. }