psi.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. "log"
  27. "sync"
  28. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon"
  29. )
  30. type PsiphonProvider interface {
  31. Notice(noticeJSON string)
  32. // TODO: return 'error'; at the moment gobind doesn't
  33. // work with interface function return values.
  34. BindToDevice(fileDescriptor int)
  35. }
  36. var controller *psiphon.Controller
  37. var shutdownBroadcast chan struct{}
  38. var controllerWaitGroup *sync.WaitGroup
  39. func Start(configJson, embeddedServerEntryList string, provider PsiphonProvider) error {
  40. if controller != nil {
  41. return fmt.Errorf("already started")
  42. }
  43. config, err := psiphon.LoadConfig([]byte(configJson))
  44. if err != nil {
  45. return fmt.Errorf("error loading configuration file: %s", err)
  46. }
  47. config.BindToDeviceProvider = provider
  48. err = psiphon.InitDataStore(config)
  49. if err != nil {
  50. return fmt.Errorf("error initializing datastore: %s", err)
  51. }
  52. psiphon.SetNoticeOutput(psiphon.NewNoticeReceiver(
  53. func(notice []byte) {
  54. provider.Notice(string(notice))
  55. }))
  56. serverEntries, err := psiphon.DecodeAndValidateServerEntryList(embeddedServerEntryList)
  57. if err != nil {
  58. log.Fatalf("error decoding embedded server entry list: %s", err)
  59. }
  60. err = psiphon.StoreServerEntries(serverEntries, false)
  61. if err != nil {
  62. log.Fatalf("error storing embedded server entry list: %s", err)
  63. }
  64. controller, err = psiphon.NewController(config)
  65. if err != nil {
  66. return fmt.Errorf("error initializing controller: %s", err)
  67. }
  68. shutdownBroadcast = make(chan struct{})
  69. controllerWaitGroup = new(sync.WaitGroup)
  70. controllerWaitGroup.Add(1)
  71. go func() {
  72. defer controllerWaitGroup.Done()
  73. controller.Run(shutdownBroadcast)
  74. }()
  75. return nil
  76. }
  77. func Stop() {
  78. if controller != nil {
  79. close(shutdownBroadcast)
  80. controllerWaitGroup.Wait()
  81. controller = nil
  82. shutdownBroadcast = nil
  83. controllerWaitGroup = nil
  84. }
  85. }