goRicochetMobile/goRicochetMobile.go

106 lines
2.6 KiB
Go
Raw Normal View History

package goRicochetMobile
import (
"github.com/s-rah/go-ricochet/application"
"github.com/s-rah/go-ricochet/utils"
"log"
2017-08-20 20:42:55 +00:00
"net/http"
"time"
"github.com/yawning/bulb/utils/pkcs1"
"crypto/rsa"
"github.com/dballard/goRicochetMobile/ODClient"
2017-09-20 15:26:42 +00:00
"strconv"
)
2017-09-18 15:35:24 +00:00
var (
2017-09-20 15:26:42 +00:00
// Downsampling array from https://git.mascherari.press/oniondildonics/client/src/master/main.go
// moddified
levelArr = []int{1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6}
odClient *ODClient.ODClient
2017-09-18 15:35:24 +00:00
)
func GeneratePrivateKey() (string, error) {
privateKey, err := utils.GeneratePrivateKey()
if err != nil {
return "", err
}
return utils.PrivateKeyToString(privateKey), nil
}
2017-09-05 03:35:11 +00:00
func GetOnionAddress(privateKey string) string {
pk, _ := utils.ParsePrivateKey([]byte(privateKey))
pubKey := rsa.PublicKey(pk.PublicKey)
addr, err := pkcs1.OnionAddr(&pubKey)
if err != nil || addr == "" {
return ""
}
return addr
2017-09-05 03:35:11 +00:00
}
func ODClientConnect(privateKey string, serverAddr string) error {
2017-09-10 17:43:15 +00:00
log.Println("ODClientConnect(" + serverAddr + ")")
2017-09-18 15:35:24 +00:00
odClient = new(ODClient.ODClient)
err := odClient.Connect(privateKey, serverAddr)
return err
}
2017-09-18 15:35:24 +00:00
func GetDeviceName() string {
odClient.SendMessage("/name")
name := odClient.GetMessage()
return name
}
2017-09-20 15:26:42 +00:00
func GetBatteryLevel() string {
odClient.SendMessage("/battery")
batteryLevel := odClient.GetMessage()
return batteryLevel
}
func GetVibeLevel() int {
odClient.SendMessage("/level")
level, err := strconv.Atoi(odClient.GetMessage())
if err != nil {
// TODO: don't swallow errors
return 0
}
return levelArr[level] // not bounds checking...
}
func SetVibeLevel(newVibeLevel int) {
odClient.SendMessage("/level " + strconv.Itoa(newVibeLevel))
}
/******** Testing by standing up an echobot ******/
2017-09-18 15:35:24 +00:00
func TestNet() (ok bool, ex error) {
_, err := http.Get("http://golang.org/")
if err != nil {
return false, err
}
return true, nil
}
2017-08-20 20:42:55 +00:00
func EchoBot(privateKeyData string) {
privateKey, err := utils.ParsePrivateKey([]byte(privateKeyData))
if err != nil {
log.Fatal("error parsing private key: %v", err)
}
log.Println("Setup onion hidden service via tor control...")
l, err := application.SetupOnion("127.0.0.1:9051", "tcp4","", privateKey, 9878)
if err != nil {
log.Fatalf("error setting up onion service: %v", err)
}
echobot := new(application.RicochetApplication)
echobot.Init(privateKey, new(application.RejectAllContactManager))
echobot.OnChatMessage(func(rai *application.RicochetApplicationInstance, id uint32, timestamp time.Time, message string) {
log.Printf("message from %v - %v", rai.RemoteHostname, message)
rai.SendChatMessage(message)
})
log.Printf("echobot started on %s", l.Addr().String())
echobot.Run(l)
}