psi.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 controller *psiphon.Controller
  37. var shutdownBroadcast chan struct{}
  38. var controllerWaitGroup *sync.WaitGroup
  39. func Start(
  40. configJson, embeddedServerEntryList string,
  41. provider PsiphonProvider,
  42. useDeviceBinder bool) error {
  43. if controller != nil {
  44. return fmt.Errorf("already started")
  45. }
  46. config, err := psiphon.LoadConfig([]byte(configJson))
  47. if err != nil {
  48. return fmt.Errorf("error loading configuration file: %s", err)
  49. }
  50. config.NetworkConnectivityChecker = provider
  51. if useDeviceBinder {
  52. config.DeviceBinder = provider
  53. config.DnsServerGetter = provider
  54. }
  55. psiphon.SetNoticeOutput(psiphon.NewNoticeReceiver(
  56. func(notice []byte) {
  57. provider.Notice(string(notice))
  58. }))
  59. psiphon.EmitNoticeBuildInfo()
  60. // TODO: should following errors be Notices?
  61. err = psiphon.InitDataStore(config)
  62. if err != nil {
  63. return fmt.Errorf("error initializing datastore: %s", err)
  64. }
  65. serverEntries, err := psiphon.DecodeAndValidateServerEntryList(
  66. embeddedServerEntryList,
  67. psiphon.GetCurrentTimestamp(),
  68. psiphon.SERVER_ENTRY_SOURCE_EMBEDDED)
  69. if err != nil {
  70. return fmt.Errorf("error decoding embedded server entry list: %s", err)
  71. }
  72. err = psiphon.StoreServerEntries(serverEntries, false)
  73. if err != nil {
  74. return fmt.Errorf("error storing embedded server entry list: %s", err)
  75. }
  76. controller, err = psiphon.NewController(config)
  77. if err != nil {
  78. return fmt.Errorf("error initializing controller: %s", err)
  79. }
  80. shutdownBroadcast = make(chan struct{})
  81. controllerWaitGroup = new(sync.WaitGroup)
  82. controllerWaitGroup.Add(1)
  83. go func() {
  84. defer controllerWaitGroup.Done()
  85. controller.Run(shutdownBroadcast)
  86. }()
  87. return nil
  88. }
  89. func Stop() {
  90. if controller != nil {
  91. close(shutdownBroadcast)
  92. controllerWaitGroup.Wait()
  93. controller = nil
  94. shutdownBroadcast = nil
  95. controllerWaitGroup = nil
  96. }
  97. }