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.

87 lines
2.2KB

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