Official cwtch.im peer and server implementations. https://cwtch.im
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package app
  2. import (
  3. "cwtch.im/cwtch/connectivity/tor"
  4. "cwtch.im/cwtch/peer"
  5. "errors"
  6. "fmt"
  7. "log"
  8. "os"
  9. "path"
  10. )
  11. // Application is a facade over a cwtchPeer that provides some wrapping logic.
  12. type Application struct {
  13. Peer peer.CwtchPeerInterface
  14. TorManager *tor.Manager
  15. directory string
  16. }
  17. // NewApp creates a new app with some environment awareness and initializes a Tor Manager
  18. func NewApp(appDirectory string, torPath string) (*Application, error) {
  19. log.Printf("NewApp(%v, %v)\n", appDirectory, torPath)
  20. app := &Application{Peer: nil, directory: appDirectory}
  21. os.MkdirAll(path.Join(appDirectory, "tor"), 0700)
  22. err := app.startTor(torPath)
  23. if err != nil {
  24. return nil, err
  25. }
  26. return app, nil
  27. }
  28. // NewProfile creates a new cwtchPeer with a given name.
  29. func (app *Application) NewProfile(name string, password string) error {
  30. log.Printf("NewProfile(%v, %v)\n", name, password)
  31. if app.Peer != nil {
  32. return errors.New("Profile already created")
  33. }
  34. app.Peer = peer.NewCwtchPeer(name, password, path.Join(app.directory, name+".json"))
  35. err := app.Peer.Save()
  36. if err == nil {
  37. err = app.startPeer()
  38. }
  39. return err
  40. }
  41. // startTor will create a local torrc if needed
  42. func (app *Application) startTor(torPath string) error {
  43. // Creating a local cwtch tor server config for the user
  44. // creating $app.directory/torrc file
  45. // SOCKSPort socksPort
  46. // ControlPort controlPort
  47. torrc := path.Join(app.directory, "tor", "torrc")
  48. if _, err := os.Stat(torrc); os.IsNotExist(err) {
  49. log.Printf("writing torrc to: %v\n", torrc)
  50. file, err := os.Create(torrc)
  51. if err != nil {
  52. return err
  53. }
  54. fmt.Fprintf(file, "SOCKSPort %d\nControlPort %d\nCookieAuthentication 0\nSafeSocks 1\n", 9050, 9051)
  55. file.Close()
  56. }
  57. tm, err := tor.NewTorManager(9050, 9051, torPath, torrc)
  58. if err != nil {
  59. return err
  60. }
  61. app.TorManager = tm
  62. return nil
  63. }
  64. // SetProfile loads an existing profile from the given filename.
  65. func (app *Application) SetProfile(filename string, password string) error {
  66. if app.Peer == nil {
  67. profile, err := peer.LoadCwtchPeer(path.Join(app.directory, filename), password)
  68. if err != nil {
  69. return err
  70. }
  71. app.Peer = profile
  72. return app.startPeer()
  73. }
  74. return errors.New("profile is already loaded, to load a different profile you will need to restart the application")
  75. }
  76. func (app *Application) startPeer() error {
  77. go func() {
  78. e := app.Peer.Listen()
  79. if e != nil {
  80. log.Panic(e)
  81. }
  82. }()
  83. return nil
  84. }
  85. // PeerRequest attempts to setup peer relationship with the given onion address.`
  86. func (app *Application) PeerRequest(onion string) {
  87. app.Peer.PeerWithOnion(onion)
  88. }