2018-11-27 03:37:46 +00:00
package main
import (
2019-01-04 21:44:21 +00:00
"cwtch.im/cwtch/event"
2018-11-27 03:37:46 +00:00
"cwtch.im/cwtch/peer"
2019-01-04 21:44:21 +00:00
"cwtch.im/cwtch/protocol/connections"
2018-11-27 03:37:46 +00:00
"errors"
"fmt"
"git.openprivacy.ca/openprivacy/libricochet-go/connectivity"
"os"
"time"
)
2019-05-15 20:12:11 +00:00
func waitForPeerGroupConnection ( peer peer . CwtchPeer , groupID string ) error {
2018-11-27 03:37:46 +00:00
for {
2019-05-15 20:12:11 +00:00
_ , ok := peer . GetProfile ( ) . Groups [ groupID ]
2018-11-27 03:37:46 +00:00
if ok {
2019-05-15 20:12:11 +00:00
state := peer . GetGroupState ( groupID )
2018-11-27 03:37:46 +00:00
if state == connections . FAILED {
2019-05-15 20:12:11 +00:00
return errors . New ( "Connection to group " + groupID + " failed!" )
2018-11-27 03:37:46 +00:00
}
if state != connections . AUTHENTICATED {
2019-05-15 20:12:11 +00:00
fmt . Printf ( "peer %v waiting to authenticate with group %v 's server, current state: %v\n" , peer . GetProfile ( ) . Onion , groupID , connections . ConnectionStateName [ state ] )
2018-11-27 03:37:46 +00:00
time . Sleep ( time . Second * 10 )
continue
}
} else {
return errors . New ( "peer server connections should have entry for server but do not" )
}
break
}
return nil
}
func main ( ) {
if len ( os . Args ) != 2 {
fmt . Printf ( "Usage: ./servermon SERVER_ADDRESS\n" )
os . Exit ( 1 )
}
serverAddr := os . Args [ 1 ]
acn , err := connectivity . StartTor ( "." , "" )
if err != nil {
fmt . Printf ( "Could not start tor: %v\n" , err )
os . Exit ( 1 )
}
botPeer := peer . NewCwtchPeer ( "servermon" )
2019-01-04 21:44:21 +00:00
botPeer . Init ( acn , new ( event . Manager ) )
2018-11-27 03:37:46 +00:00
fmt . Printf ( "Connecting to %v...\n" , serverAddr )
botPeer . JoinServer ( serverAddr )
groupID , _ , err := botPeer . StartGroup ( serverAddr )
if err != nil {
fmt . Printf ( "Error creating group on server %v: %v\n" , serverAddr , err )
os . Exit ( 1 )
}
2019-05-15 20:12:11 +00:00
err = waitForPeerGroupConnection ( botPeer , groupID )
2018-11-27 03:37:46 +00:00
if err != nil {
fmt . Printf ( "Could not connect to server %v: %v\n" , serverAddr , err )
os . Exit ( 1 )
}
timeout := 1 * time . Second
timeElapsed := 0 * time . Second
for {
err := botPeer . SendMessageToGroup ( groupID , timeout . String ( ) )
if err != nil {
fmt . Printf ( "Sent to group on server %v failed at interval %v of total %v with: %v\n" , serverAddr , timeout , timeElapsed , err )
os . Exit ( 1 )
} else {
fmt . Printf ( "Successfully sent message to %v at interval %v of total %v\n" , serverAddr , timeout , timeElapsed )
}
time . Sleep ( timeout )
timeElapsed += timeout
if timeout < 2 * time . Minute {
timeout = timeout * 2
}
}
}