Official cwtch.im peer and server implementations. https://cwtch.im
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.go 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package main
  2. import (
  3. "cwtch.im/cwtch/event"
  4. "cwtch.im/cwtch/peer"
  5. "cwtch.im/cwtch/protocol/connections"
  6. "errors"
  7. "fmt"
  8. "git.openprivacy.ca/openprivacy/libricochet-go/connectivity"
  9. "os"
  10. "time"
  11. )
  12. func waitForPeerGroupConnection(peer peer.CwtchPeer, groupID string) error {
  13. for {
  14. _, ok := peer.GetProfile().Groups[groupID]
  15. if ok {
  16. state := peer.GetGroupState(groupID)
  17. if state == connections.FAILED {
  18. return errors.New("Connection to group " + groupID + " failed!")
  19. }
  20. if state != connections.AUTHENTICATED {
  21. fmt.Printf("peer %v waiting to authenticate with group %v 's server, current state: %v\n", peer.GetProfile().Onion, groupID, connections.ConnectionStateName[state])
  22. time.Sleep(time.Second * 10)
  23. continue
  24. }
  25. } else {
  26. return errors.New("peer server connections should have entry for server but do not")
  27. }
  28. break
  29. }
  30. return nil
  31. }
  32. func main() {
  33. if len(os.Args) != 2 {
  34. fmt.Printf("Usage: ./servermon SERVER_ADDRESS\n")
  35. os.Exit(1)
  36. }
  37. serverAddr := os.Args[1]
  38. acn, err := connectivity.StartTor(".", "")
  39. if err != nil {
  40. fmt.Printf("Could not start tor: %v\n", err)
  41. os.Exit(1)
  42. }
  43. botPeer := peer.NewCwtchPeer("servermon")
  44. botPeer.Init(acn, event.NewEventManager())
  45. fmt.Printf("Connecting to %v...\n", serverAddr)
  46. botPeer.JoinServer(serverAddr)
  47. groupID, _, err := botPeer.StartGroup(serverAddr)
  48. if err != nil {
  49. fmt.Printf("Error creating group on server %v: %v\n", serverAddr, err)
  50. os.Exit(1)
  51. }
  52. err = waitForPeerGroupConnection(botPeer, groupID)
  53. if err != nil {
  54. fmt.Printf("Could not connect to server %v: %v\n", serverAddr, err)
  55. os.Exit(1)
  56. }
  57. timeout := 1 * time.Second
  58. timeElapsed := 0 * time.Second
  59. for {
  60. err := botPeer.SendMessageToGroup(groupID, timeout.String())
  61. if err != nil {
  62. fmt.Printf("Sent to group on server %v failed at interval %v of total %v with: %v\n", serverAddr, timeout, timeElapsed, err)
  63. os.Exit(1)
  64. } else {
  65. fmt.Printf("Successfully sent message to %v at interval %v of total %v\n", serverAddr, timeout, timeElapsed)
  66. }
  67. time.Sleep(timeout)
  68. timeElapsed += timeout
  69. if timeout < 2*time.Minute {
  70. timeout = timeout * 2
  71. }
  72. }
  73. }