Compare commits
No commits in common. "master" and "v0.0-echobot" have entirely different histories.
master
...
v0.0-echob
|
@ -6,12 +6,8 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
"github.com/yawning/bulb/utils/pkcs1"
|
|
||||||
"crypto/rsa"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
func GeneratePrivateKey() (string, error) {
|
func GeneratePrivateKey() (string, error) {
|
||||||
privateKey, err := utils.GeneratePrivateKey()
|
privateKey, err := utils.GeneratePrivateKey()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -20,20 +16,6 @@ func GeneratePrivateKey() (string, error) {
|
||||||
return utils.PrivateKeyToString(privateKey), nil
|
return utils.PrivateKeyToString(privateKey), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/******** Testing by standing up an echobot ******/
|
|
||||||
|
|
||||||
func TestNet() (ok bool, ex error) {
|
func TestNet() (ok bool, ex error) {
|
||||||
_, err := http.Get("http://golang.org/")
|
_, err := http.Get("http://golang.org/")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -44,23 +26,26 @@ func TestNet() (ok bool, ex error) {
|
||||||
|
|
||||||
func EchoBot(privateKeyData string) {
|
func EchoBot(privateKeyData string) {
|
||||||
privateKey, err := utils.ParsePrivateKey([]byte(privateKeyData))
|
privateKey, err := utils.ParsePrivateKey([]byte(privateKeyData))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("error parsing private key: %v", err)
|
log.Fatal("error parsing private key: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("Setup onion hidden service via tor control...")
|
echobot := new(application.RicochetApplication)
|
||||||
|
|
||||||
|
log.Println("SetupOnion()...")
|
||||||
l, err := application.SetupOnion("127.0.0.1:9051", "tcp4","", privateKey, 9878)
|
l, err := application.SetupOnion("127.0.0.1:9051", "tcp4","", privateKey, 9878)
|
||||||
|
//l, err := application.SetupOnion("/data/data/org.torproject.android/app_bin/control.txt", "unix","", privateKey, 9878)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("error setting up onion service: %v", err)
|
log.Fatalf("error setting up onion service: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
echobot := new(application.RicochetApplication)
|
|
||||||
echobot.Init(privateKey, new(application.AcceptAllContactManager))
|
echobot.Init(privateKey, new(application.AcceptAllContactManager))
|
||||||
|
|
||||||
echobot.OnChatMessage(func(rai *application.RicochetApplicationInstance, id uint32, timestamp time.Time, message string) {
|
echobot.OnChatMessage(func(rai *application.RicochetApplicationInstance, id uint32, timestamp time.Time, message string) {
|
||||||
log.Printf("message from %v - %v", rai.RemoteHostname, message)
|
log.Printf("message from %v - %v", rai.RemoteHostname, message)
|
||||||
rai.SendChatMessage(message)
|
rai.SendChatMessage(message)
|
||||||
})
|
})
|
||||||
log.Printf("echobot started on %s", l.Addr().String())
|
log.Printf("echobot listening on %s", l.Addr().String())
|
||||||
echobot.Run(l)
|
echobot.Run(l)
|
||||||
}
|
}
|
53
od/od.go
53
od/od.go
|
@ -1,53 +0,0 @@
|
||||||
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,159 +0,0 @@
|
||||||
package odClient
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/s-rah/go-ricochet/connection"
|
|
||||||
"time"
|
|
||||||
"log"
|
|
||||||
"github.com/s-rah/go-ricochet/channels"
|
|
||||||
"github.com/s-rah/go-ricochet/utils"
|
|
||||||
"github.com/s-rah/go-ricochet"
|
|
||||||
)
|
|
||||||
|
|
||||||
type ODClient struct {
|
|
||||||
connection.AutoConnectionHandler
|
|
||||||
connection *connection.Connection
|
|
||||||
recvMessages chan string
|
|
||||||
deviceName string
|
|
||||||
deviceLevel int
|
|
||||||
batteryLevel string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (odClient *ODClient) Connect(privateKeyData string, serverAddr string) error {
|
|
||||||
log.Println("ODCLient.Connect()")
|
|
||||||
privateKey, err := utils.ParsePrivateKey([]byte(privateKeyData))
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal("error parsing private key: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
odClient.Init(privateKey, serverAddr)
|
|
||||||
|
|
||||||
odClient.RegisterChannelHandler("im.ricochet.contact.request", func() channels.Handler {
|
|
||||||
log.Println("handler for contact.request")
|
|
||||||
contact := new(channels.ContactRequestChannel)
|
|
||||||
contact.Handler = odClient
|
|
||||||
return contact
|
|
||||||
})
|
|
||||||
|
|
||||||
odClient.RegisterChannelHandler("im.ricochet.chat", func() channels.Handler {
|
|
||||||
log.Println("handler for chat")
|
|
||||||
chat := new(channels.ChatChannel)
|
|
||||||
chat.Handler = odClient
|
|
||||||
return chat
|
|
||||||
})
|
|
||||||
|
|
||||||
odClient.recvMessages = make(chan string)
|
|
||||||
|
|
||||||
log.Println("ODClient connecting...")
|
|
||||||
odClient.connection, err = goricochet.Open(serverAddr)
|
|
||||||
if err != nil {
|
|
||||||
log.Println("Error connecting %v", err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
log.Println("ODCleint connected!")
|
|
||||||
log.Println("Starting auth...")
|
|
||||||
known, err := connection.HandleOutboundConnection(odClient.connection).ProcessAuthAsClient(privateKey)
|
|
||||||
if err != nil {
|
|
||||||
log.Println("Error handling auth: %v", err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Println("go Process")
|
|
||||||
go odClient.connection.Process(odClient)
|
|
||||||
|
|
||||||
if !known {
|
|
||||||
err := odClient.connection.RequestOpenChannel("im.ricochet.contact.request", odClient)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Could not auth with server", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Println("ODClient: Authenticated")
|
|
||||||
|
|
||||||
log.Println("RequestOpenChanel chat")
|
|
||||||
err = odClient.connection.RequestOpenChannel("im.ricochet.chat", odClient)
|
|
||||||
if err != nil {
|
|
||||||
log.Println("Error: " + err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
if channel != nil {
|
|
||||||
contactRequestChannel, ok := (*channel.Handler).(*channels.ContactRequestChannel)
|
|
||||||
if ok {
|
|
||||||
//contactRequestChannel.Handler
|
|
||||||
}
|
|
||||||
}else {
|
|
||||||
log.Println("ERROR: failed to find chat channel")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
}*/
|
|
||||||
|
|
||||||
func (odClient *ODClient) SendMessage(message string) {
|
|
||||||
odClient.connection.Do(func() error {
|
|
||||||
channel := odClient.connection.Channel("im.ricochet.chat", channels.Outbound)
|
|
||||||
if channel != nil {
|
|
||||||
chatchannel, ok := (*channel.Handler).(*channels.ChatChannel)
|
|
||||||
if ok {
|
|
||||||
chatchannel.SendMessage(message)
|
|
||||||
}
|
|
||||||
}else {
|
|
||||||
log.Println("ERROR: failed to find chat channel")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
odc.recvMessages <- message
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// ChatMessageAck does nothing.
|
|
||||||
func (odc *ODClient) ChatMessageAck(messageID uint32) {
|
|
||||||
}
|
|
||||||
|
|
||||||
/************* Contact Channel Handler ********/
|
|
||||||
|
|
||||||
// GetContactDetails is purposely empty
|
|
||||||
func (odc *ODClient) GetContactDetails() (string, string) {
|
|
||||||
return "AndroidOD Client", ""
|
|
||||||
}
|
|
||||||
|
|
||||||
// ContactRequest denies any contact request.
|
|
||||||
func (odc *ODClient) ContactRequest(name string, message string) string {
|
|
||||||
return "Rejected"
|
|
||||||
}
|
|
||||||
|
|
||||||
// ContactRequestRejected purposly does nothing.
|
|
||||||
func (odc *ODClient) ContactRequestRejected() {
|
|
||||||
}
|
|
||||||
|
|
||||||
// ContactRequestAccepted purposly does nothing.
|
|
||||||
func (odc *ODClient) ContactRequestAccepted() {
|
|
||||||
}
|
|
||||||
|
|
||||||
// ContactRequestError purposly does nothing.
|
|
||||||
func (odc *ODClient) ContactRequestError() {
|
|
||||||
}
|
|
Loading…
Reference in New Issue