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