psi.go 4.7 KB

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