psi.go 4.2 KB

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