2017-08-13 15:30:08 +00:00
|
|
|
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"
|
2017-08-13 15:30:08 +00:00
|
|
|
"time"
|
2017-09-07 03:33:14 +00:00
|
|
|
"github.com/yawning/bulb/utils/pkcs1"
|
|
|
|
"crypto/rsa"
|
2017-09-07 15:11:23 +00:00
|
|
|
"github.com/dballard/goRicochetMobile/ODClient"
|
2017-08-13 15:30:08 +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 {
|
2017-09-07 03:33:14 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-08-20 20:42:55 +00:00
|
|
|
func TestNet() (ok bool, ex error) {
|
|
|
|
_, err := http.Get("http://golang.org/")
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2017-09-07 15:11:23 +00:00
|
|
|
func ODClientConnect(privateKey string, serverAddr string) error {
|
|
|
|
odClient := new(ODClient.ODClient)
|
|
|
|
err := odClient.Connect(privateKey, serverAddr)
|
|
|
|
return err
|
2017-09-07 03:33:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/******** Testing by standing up an echobot ******/
|
|
|
|
|
2017-08-20 20:42:55 +00:00
|
|
|
func EchoBot(privateKeyData string) {
|
2017-08-13 15:30:08 +00:00
|
|
|
privateKey, err := utils.ParsePrivateKey([]byte(privateKeyData))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("error parsing private key: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-09-07 03:33:14 +00:00
|
|
|
log.Println("Setup onion hidden service via tor control...")
|
2017-08-16 14:44:38 +00:00
|
|
|
l, err := application.SetupOnion("127.0.0.1:9051", "tcp4","", privateKey, 9878)
|
2017-08-13 15:30:08 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error setting up onion service: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-09-07 03:33:14 +00:00
|
|
|
echobot := new(application.RicochetApplication)
|
|
|
|
echobot.Init(privateKey, new(application.RejectAllContactManager))
|
|
|
|
|
2017-08-13 15:30:08 +00:00
|
|
|
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)
|
|
|
|
})
|
2017-09-07 03:33:14 +00:00
|
|
|
log.Printf("echobot started on %s", l.Addr().String())
|
2017-08-13 15:30:08 +00:00
|
|
|
echobot.Run(l)
|
|
|
|
}
|