psi.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) 2014, 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(message string)
  32. // TODO: return 'error'; at the moment gobind doesn't
  33. // work with interface function return values.
  34. BindToDevice(fileDescriptor int)
  35. }
  36. type logRelay struct {
  37. provider PsiphonProvider
  38. }
  39. func (lr *logRelay) Write(p []byte) (n int, err error) {
  40. // TODO: buffer incomplete lines
  41. lr.provider.Notice(string(p))
  42. return len(p), nil
  43. }
  44. var controller *psiphon.Controller
  45. var shutdownBroadcast chan struct{}
  46. var controllerWaitGroup *sync.WaitGroup
  47. func Start(configJson string, provider PsiphonProvider) error {
  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. err = psiphon.InitDataStore(config)
  56. if err != nil {
  57. return fmt.Errorf("error initializing datastore: %s", err)
  58. }
  59. log.SetOutput(&logRelay{provider: provider})
  60. config.BindToDeviceProvider = provider
  61. controller = psiphon.NewController(config)
  62. shutdownBroadcast = make(chan struct{})
  63. controllerWaitGroup = new(sync.WaitGroup)
  64. controllerWaitGroup.Add(1)
  65. go func() {
  66. defer controllerWaitGroup.Done()
  67. controller.Run(shutdownBroadcast)
  68. }()
  69. return nil
  70. }
  71. func Stop() {
  72. if controller != nil {
  73. close(shutdownBroadcast)
  74. controllerWaitGroup.Wait()
  75. controller = nil
  76. shutdownBroadcast = nil
  77. controllerWaitGroup = nil
  78. }
  79. }