psi.go 4.3 KB

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