Formatting + Checking Connection Error in Echobot
This commit is contained in:
parent
9d2e898157
commit
5057dd68ee
|
@ -17,7 +17,7 @@ type RicochetApplication struct {
|
|||
privateKey *rsa.PrivateKey
|
||||
chatMessageHandler func(*RicochetApplicationInstance, uint32, time.Time, string)
|
||||
chatMessageAckHandler func(*RicochetApplicationInstance, uint32)
|
||||
l net.Listener
|
||||
l net.Listener
|
||||
}
|
||||
|
||||
type RicochetApplicationInstance struct {
|
||||
|
@ -42,9 +42,9 @@ func (rai *RicochetApplicationInstance) ContactRequestError() {
|
|||
func (rai *RicochetApplicationInstance) SendChatMessage(message string) {
|
||||
rai.connection.Do(func() error {
|
||||
// Technically this errors afte the second time but we can ignore it.
|
||||
rai.connection.RequestOpenChannel("im.ricochet.chat",
|
||||
&channels.ChatChannel{
|
||||
Handler: rai,
|
||||
rai.connection.RequestOpenChannel("im.ricochet.chat",
|
||||
&channels.ChatChannel{
|
||||
Handler: rai,
|
||||
})
|
||||
|
||||
channel := rai.connection.Channel("im.ricochet.chat", channels.Outbound)
|
||||
|
@ -119,10 +119,10 @@ func (ra *RicochetApplication) handleConnection(conn net.Conn) {
|
|||
rc.Process(rai)
|
||||
}
|
||||
|
||||
func (ra *RicochetApplication) Shutdown () {
|
||||
log.Printf("Closing")
|
||||
ra.l.Close()
|
||||
log.Printf("Closed")
|
||||
func (ra *RicochetApplication) Shutdown() {
|
||||
log.Printf("Closing")
|
||||
ra.l.Close()
|
||||
log.Printf("Closed")
|
||||
}
|
||||
|
||||
func (ra *RicochetApplication) Run(l net.Listener) {
|
||||
|
@ -130,14 +130,14 @@ func (ra *RicochetApplication) Run(l net.Listener) {
|
|||
return
|
||||
}
|
||||
ra.l = l
|
||||
var err error
|
||||
var err error
|
||||
for err == nil {
|
||||
conn, err := ra.l.Accept()
|
||||
if err == nil {
|
||||
go ra.handleConnection(conn)
|
||||
} else {
|
||||
log.Printf("Closing")
|
||||
return
|
||||
log.Printf("Closing")
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/s-rah/go-ricochet/application"
|
||||
"github.com/s-rah/go-ricochet/utils"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
commandbot := new(application.RicochetApplication)
|
||||
pk, err := utils.LoadPrivateKeyFromFile("./testing/private_key")
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("error reading private key file: %v", err)
|
||||
}
|
||||
|
||||
l, err := application.SetupOnion("127.0.0.1:9051", "", pk, 9878)
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("error setting up onion service: %v", err)
|
||||
}
|
||||
|
||||
commandbot.Init(pk, new(application.AcceptAllContactManager))
|
||||
commandbot.OnChatMessage(func(rai *application.RicochetApplicationInstance, id uint32, timestamp time.Time, message string) {
|
||||
if message == "/" {
|
||||
rai.SendChatMessage(message)
|
||||
}
|
||||
})
|
||||
log.Printf("commandbot listening on %s", l.Addr().String())
|
||||
commandbot.Run(l)
|
||||
}
|
|
@ -53,7 +53,12 @@ func (echobot *RicochetEchoBot) Connect(privateKeyFile string, hostname string)
|
|||
return chat
|
||||
})
|
||||
|
||||
rc, _ := goricochet.Open(hostname)
|
||||
rc, err := goricochet.Open(hostname)
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("could not connect to %s: %v", hostname, err)
|
||||
}
|
||||
|
||||
known, err := connection.HandleOutboundConnection(rc).ProcessAuthAsClient(privateKey)
|
||||
if err == nil {
|
||||
|
||||
|
@ -61,12 +66,12 @@ func (echobot *RicochetEchoBot) Connect(privateKeyFile string, hostname string)
|
|||
|
||||
if !known {
|
||||
err := rc.Do(func() error {
|
||||
_,err := rc.RequestOpenChannel("im.ricochet.contact.request",
|
||||
&channels.ContactRequestChannel{
|
||||
Handler: echobot,
|
||||
Name: "EchoBot",
|
||||
Message: "I LIVE 😈😈!!!!",
|
||||
})
|
||||
_, err := rc.RequestOpenChannel("im.ricochet.contact.request",
|
||||
&channels.ContactRequestChannel{
|
||||
Handler: echobot,
|
||||
Name: "EchoBot",
|
||||
Message: "I LIVE 😈😈!!!!",
|
||||
})
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
|
@ -101,5 +106,5 @@ func (echobot *RicochetEchoBot) Connect(privateKeyFile string, hostname string)
|
|||
|
||||
func main() {
|
||||
echoBot := new(RicochetEchoBot)
|
||||
echoBot.Connect("private_key", "oqf7z4ot6kuejgam")
|
||||
echoBot.Connect("private_key", "flkjmgvjloyyzlpe")
|
||||
}
|
||||
|
|
|
@ -1,17 +1,17 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/x509"
|
||||
"encoding/pem"
|
||||
"io/ioutil"
|
||||
"errors"
|
||||
"crypto/rand"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
const (
|
||||
InvalidPrivateKeyFileError = Error("InvalidPrivateKeyFileError")
|
||||
RICOCHET_KEY_SIZE = 1024
|
||||
RICOCHET_KEY_SIZE = 1024
|
||||
)
|
||||
|
||||
// Generate a private key for use
|
||||
|
@ -47,9 +47,9 @@ func ParsePrivateKey(pemData []byte) (*rsa.PrivateKey, error) {
|
|||
// turn a private key into storable string
|
||||
func PrivateKeyToString(privateKey *rsa.PrivateKey) string {
|
||||
privateKeyBlock := pem.Block{
|
||||
Type: "RSA PRIVATE KEY",
|
||||
Type: "RSA PRIVATE KEY",
|
||||
Headers: nil,
|
||||
Bytes: x509.MarshalPKCS1PrivateKey(privateKey),
|
||||
Bytes: x509.MarshalPKCS1PrivateKey(privateKey),
|
||||
}
|
||||
|
||||
return string(pem.EncodeToMemory(&privateKeyBlock))
|
||||
|
|
Reference in New Issue