Compare commits
11 Commits
v0.0-echob
...
master
Author | SHA1 | Date |
---|---|---|
Dan Ballard | d8d5db80b1 | |
Dan Ballard | 1dd4a113a6 | |
Dan Ballard | 81906f08f6 | |
Dan Ballard | 4d7017402d | |
Dan Ballard | 7265e7b7de | |
Dan Ballard | 639dbf2ea6 | |
Dan Ballard | b88a04432b | |
Dan Ballard | 1fdbd0f7f7 | |
Dan Ballard | 5085a1d08c | |
Dan Ballard | 032f06dad1 | |
Dan Ballard | 3ffb61065f |
|
@ -6,8 +6,12 @@ import (
|
|||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
"github.com/yawning/bulb/utils/pkcs1"
|
||||
"crypto/rsa"
|
||||
)
|
||||
|
||||
|
||||
|
||||
func GeneratePrivateKey() (string, error) {
|
||||
privateKey, err := utils.GeneratePrivateKey()
|
||||
if err != nil {
|
||||
|
@ -16,6 +20,20 @@ func GeneratePrivateKey() (string, error) {
|
|||
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) {
|
||||
_, err := http.Get("http://golang.org/")
|
||||
if err != nil {
|
||||
|
@ -26,26 +44,23 @@ func TestNet() (ok bool, ex error) {
|
|||
|
||||
func EchoBot(privateKeyData string) {
|
||||
privateKey, err := utils.ParsePrivateKey([]byte(privateKeyData))
|
||||
|
||||
if err != nil {
|
||||
log.Fatal("error parsing private key: %v", err)
|
||||
}
|
||||
|
||||
echobot := new(application.RicochetApplication)
|
||||
|
||||
log.Println("SetupOnion()...")
|
||||
log.Println("Setup onion hidden service via tor control...")
|
||||
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 {
|
||||
log.Fatalf("error setting up onion service: %v", err)
|
||||
}
|
||||
|
||||
echobot := new(application.RicochetApplication)
|
||||
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)
|
||||
rai.SendChatMessage(message)
|
||||
})
|
||||
log.Printf("echobot listening on %s", l.Addr().String())
|
||||
log.Printf("echobot started on %s", l.Addr().String())
|
||||
echobot.Run(l)
|
||||
}
|
||||
}
|
|
@ -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))
|
||||
}
|
|
@ -0,0 +1,159 @@
|
|||
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