Compare commits
5 Commits
v0.1-send-
...
master
Author | SHA1 | Date |
---|---|---|
|
d8d5db80b1 | |
|
1dd4a113a6 | |
|
81906f08f6 | |
|
4d7017402d | |
|
7265e7b7de |
|
@ -8,9 +8,10 @@ import (
|
|||
"time"
|
||||
"github.com/yawning/bulb/utils/pkcs1"
|
||||
"crypto/rsa"
|
||||
"github.com/dballard/goRicochetMobile/ODClient"
|
||||
)
|
||||
|
||||
|
||||
|
||||
func GeneratePrivateKey() (string, error) {
|
||||
privateKey, err := utils.GeneratePrivateKey()
|
||||
if err != nil {
|
||||
|
@ -29,6 +30,10 @@ func GetOnionAddress(privateKey string) string {
|
|||
return addr
|
||||
}
|
||||
|
||||
|
||||
|
||||
/******** Testing by standing up an echobot ******/
|
||||
|
||||
func TestNet() (ok bool, ex error) {
|
||||
_, err := http.Get("http://golang.org/")
|
||||
if err != nil {
|
||||
|
@ -37,15 +42,6 @@ func TestNet() (ok bool, ex error) {
|
|||
return true, nil
|
||||
}
|
||||
|
||||
func ODClientConnect(privateKey string, serverAddr string) error {
|
||||
log.Println("ODClientConnect(" + serverAddr + ")")
|
||||
odClient := new(ODClient.ODClient)
|
||||
err := odClient.Connect(privateKey, serverAddr)
|
||||
return err
|
||||
}
|
||||
|
||||
/******** Testing by standing up an echobot ******/
|
||||
|
||||
func EchoBot(privateKeyData string) {
|
||||
privateKey, err := utils.ParsePrivateKey([]byte(privateKeyData))
|
||||
if err != nil {
|
||||
|
@ -59,7 +55,7 @@ func EchoBot(privateKeyData string) {
|
|||
}
|
||||
|
||||
echobot := new(application.RicochetApplication)
|
||||
echobot.Init(privateKey, new(application.RejectAllContactManager))
|
||||
echobot.Init(privateKey, new(application.AcceptAllContactManager))
|
||||
|
||||
echobot.OnChatMessage(func(rai *application.RicochetApplicationInstance, id uint32, timestamp time.Time, message string) {
|
||||
log.Printf("message from %v - %v", rai.RemoteHostname, message)
|
||||
|
|
|
@ -0,0 +1,53 @@
|
|||
package od
|
||||
|
||||
import (
|
||||
"github.com/dballard/goRicochetMobile/od/odClient"
|
||||
"log"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
var (
|
||||
// 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}
|
||||
client *odClient.ODClient = nil
|
||||
)
|
||||
|
||||
func ODClientConnect(privateKey string, serverAddr string) error {
|
||||
log.Println("ODClientConnect()")
|
||||
client = new(odClient.ODClient)
|
||||
err := client.Connect(privateKey, serverAddr)
|
||||
return err
|
||||
}
|
||||
|
||||
func ODClientDisconnect() {
|
||||
log.Println("ODClientDisconnect()")
|
||||
client.Disconnect()
|
||||
client = nil
|
||||
}
|
||||
|
||||
func GetDeviceName() string {
|
||||
client.SendMessage("/name")
|
||||
name := client.GetMessage()
|
||||
return name
|
||||
}
|
||||
|
||||
func GetBatteryLevel() string {
|
||||
client.SendMessage("/battery")
|
||||
batteryLevel := client.GetMessage()
|
||||
return batteryLevel
|
||||
}
|
||||
|
||||
func GetVibeLevel() int {
|
||||
client.SendMessage("/level")
|
||||
level, err := strconv.Atoi(client.GetMessage())
|
||||
if err != nil {
|
||||
// TODO: don't swallow errors
|
||||
return 0
|
||||
}
|
||||
return levelArr[level] // not bounds checking...
|
||||
}
|
||||
|
||||
func SetVibeLevel(newVibeLevel int) {
|
||||
client.SendMessage("/level " + strconv.Itoa(newVibeLevel))
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package ODClient
|
||||
package odClient
|
||||
|
||||
import (
|
||||
"github.com/s-rah/go-ricochet/connection"
|
||||
|
@ -13,7 +13,6 @@ type ODClient struct {
|
|||
connection.AutoConnectionHandler
|
||||
connection *connection.Connection
|
||||
recvMessages chan string
|
||||
sendMessages chan string
|
||||
deviceName string
|
||||
deviceLevel int
|
||||
batteryLevel string
|
||||
|
@ -43,7 +42,6 @@ func (odClient *ODClient) Connect(privateKeyData string, serverAddr string) erro
|
|||
})
|
||||
|
||||
odClient.recvMessages = make(chan string)
|
||||
odClient.sendMessages = make(chan string)
|
||||
|
||||
log.Println("ODClient connecting...")
|
||||
odClient.connection, err = goricochet.Open(serverAddr)
|
||||
|
@ -52,7 +50,7 @@ func (odClient *ODClient) Connect(privateKeyData string, serverAddr string) erro
|
|||
return err
|
||||
}
|
||||
log.Println("ODCleint connected!")
|
||||
log.Println("starting auth...")
|
||||
log.Println("Starting auth...")
|
||||
known, err := connection.HandleOutboundConnection(odClient.connection).ProcessAuthAsClient(privateKey)
|
||||
if err != nil {
|
||||
log.Println("Error handling auth: %v", err)
|
||||
|
@ -65,14 +63,11 @@ func (odClient *ODClient) Connect(privateKeyData string, serverAddr string) erro
|
|||
if !known {
|
||||
err := odClient.connection.RequestOpenChannel("im.ricochet.contact.request", odClient)
|
||||
if err != nil {
|
||||
log.Printf("could not contact %s", err)
|
||||
log.Printf("Could not auth with server", err)
|
||||
}
|
||||
}
|
||||
|
||||
log.Println("ODClient: Authenticated")
|
||||
//odClient.connection.RequestOpenChannel("im.ricochet.contact.request", odClient)
|
||||
|
||||
log.Println("go")
|
||||
|
||||
log.Println("RequestOpenChanel chat")
|
||||
err = odClient.connection.RequestOpenChannel("im.ricochet.chat", odClient)
|
||||
|
@ -80,14 +75,17 @@ func (odClient *ODClient) Connect(privateKeyData string, serverAddr string) erro
|
|||
log.Println("Error: " + err.Error())
|
||||
}
|
||||
|
||||
log.Println("sending greeting message")
|
||||
odClient.SendMessage("hello from the client")
|
||||
|
||||
|
||||
|
||||
log.Println("Connection fully initiated, chat channel open!")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (odClient *ODClient) Disconnect() {
|
||||
log.Println("Break()...")
|
||||
odClient.connection.Break()
|
||||
log.Println("Conn.Close()...")
|
||||
odClient.connection.Conn.Close()
|
||||
}
|
||||
|
||||
/*func (odClient *ODClient) RequestContact() {
|
||||
odClient.connection.Do(func() error {
|
||||
channel := odClient.connection.Channel("im.ricochet.contact.request", channels.Outbound)
|
||||
|
@ -118,11 +116,16 @@ func (odClient *ODClient) SendMessage(message string) {
|
|||
})
|
||||
}
|
||||
|
||||
func (odClient *ODClient) GetMessage() string {
|
||||
message := <-odClient.recvMessages
|
||||
return message
|
||||
}
|
||||
|
||||
/************* Chat Channel Handler ********/
|
||||
|
||||
// ChatMessage passes the response to recvMessages.
|
||||
func (odc *ODClient) ChatMessage(messageID uint32, when time.Time, message string) bool {
|
||||
log.Printf("Received Message: %s", message)
|
||||
//log.Printf("Received Message: %s", message)
|
||||
odc.recvMessages <- message
|
||||
return true
|
||||
}
|
Loading…
Reference in New Issue