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
|
privateKey *rsa.PrivateKey
|
||||||
chatMessageHandler func(*RicochetApplicationInstance, uint32, time.Time, string)
|
chatMessageHandler func(*RicochetApplicationInstance, uint32, time.Time, string)
|
||||||
chatMessageAckHandler func(*RicochetApplicationInstance, uint32)
|
chatMessageAckHandler func(*RicochetApplicationInstance, uint32)
|
||||||
l net.Listener
|
l net.Listener
|
||||||
}
|
}
|
||||||
|
|
||||||
type RicochetApplicationInstance struct {
|
type RicochetApplicationInstance struct {
|
||||||
|
@ -42,9 +42,9 @@ func (rai *RicochetApplicationInstance) ContactRequestError() {
|
||||||
func (rai *RicochetApplicationInstance) SendChatMessage(message string) {
|
func (rai *RicochetApplicationInstance) SendChatMessage(message string) {
|
||||||
rai.connection.Do(func() error {
|
rai.connection.Do(func() error {
|
||||||
// Technically this errors afte the second time but we can ignore it.
|
// Technically this errors afte the second time but we can ignore it.
|
||||||
rai.connection.RequestOpenChannel("im.ricochet.chat",
|
rai.connection.RequestOpenChannel("im.ricochet.chat",
|
||||||
&channels.ChatChannel{
|
&channels.ChatChannel{
|
||||||
Handler: rai,
|
Handler: rai,
|
||||||
})
|
})
|
||||||
|
|
||||||
channel := rai.connection.Channel("im.ricochet.chat", channels.Outbound)
|
channel := rai.connection.Channel("im.ricochet.chat", channels.Outbound)
|
||||||
|
@ -119,10 +119,10 @@ func (ra *RicochetApplication) handleConnection(conn net.Conn) {
|
||||||
rc.Process(rai)
|
rc.Process(rai)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ra *RicochetApplication) Shutdown () {
|
func (ra *RicochetApplication) Shutdown() {
|
||||||
log.Printf("Closing")
|
log.Printf("Closing")
|
||||||
ra.l.Close()
|
ra.l.Close()
|
||||||
log.Printf("Closed")
|
log.Printf("Closed")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ra *RicochetApplication) Run(l net.Listener) {
|
func (ra *RicochetApplication) Run(l net.Listener) {
|
||||||
|
@ -130,14 +130,14 @@ func (ra *RicochetApplication) Run(l net.Listener) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ra.l = l
|
ra.l = l
|
||||||
var err error
|
var err error
|
||||||
for err == nil {
|
for err == nil {
|
||||||
conn, err := ra.l.Accept()
|
conn, err := ra.l.Accept()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
go ra.handleConnection(conn)
|
go ra.handleConnection(conn)
|
||||||
} else {
|
} else {
|
||||||
log.Printf("Closing")
|
log.Printf("Closing")
|
||||||
return
|
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
|
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)
|
known, err := connection.HandleOutboundConnection(rc).ProcessAuthAsClient(privateKey)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
|
||||||
|
@ -61,12 +66,12 @@ func (echobot *RicochetEchoBot) Connect(privateKeyFile string, hostname string)
|
||||||
|
|
||||||
if !known {
|
if !known {
|
||||||
err := rc.Do(func() error {
|
err := rc.Do(func() error {
|
||||||
_,err := rc.RequestOpenChannel("im.ricochet.contact.request",
|
_, err := rc.RequestOpenChannel("im.ricochet.contact.request",
|
||||||
&channels.ContactRequestChannel{
|
&channels.ContactRequestChannel{
|
||||||
Handler: echobot,
|
Handler: echobot,
|
||||||
Name: "EchoBot",
|
Name: "EchoBot",
|
||||||
Message: "I LIVE 😈😈!!!!",
|
Message: "I LIVE 😈😈!!!!",
|
||||||
})
|
})
|
||||||
return err
|
return err
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -101,5 +106,5 @@ func (echobot *RicochetEchoBot) Connect(privateKeyFile string, hostname string)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
echoBot := new(RicochetEchoBot)
|
echoBot := new(RicochetEchoBot)
|
||||||
echoBot.Connect("private_key", "oqf7z4ot6kuejgam")
|
echoBot.Connect("private_key", "flkjmgvjloyyzlpe")
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,17 +1,17 @@
|
||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/rand"
|
||||||
"crypto/rsa"
|
"crypto/rsa"
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"encoding/pem"
|
"encoding/pem"
|
||||||
"io/ioutil"
|
|
||||||
"errors"
|
"errors"
|
||||||
"crypto/rand"
|
"io/ioutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
InvalidPrivateKeyFileError = Error("InvalidPrivateKeyFileError")
|
InvalidPrivateKeyFileError = Error("InvalidPrivateKeyFileError")
|
||||||
RICOCHET_KEY_SIZE = 1024
|
RICOCHET_KEY_SIZE = 1024
|
||||||
)
|
)
|
||||||
|
|
||||||
// Generate a private key for use
|
// Generate a private key for use
|
||||||
|
@ -47,9 +47,9 @@ func ParsePrivateKey(pemData []byte) (*rsa.PrivateKey, error) {
|
||||||
// turn a private key into storable string
|
// turn a private key into storable string
|
||||||
func PrivateKeyToString(privateKey *rsa.PrivateKey) string {
|
func PrivateKeyToString(privateKey *rsa.PrivateKey) string {
|
||||||
privateKeyBlock := pem.Block{
|
privateKeyBlock := pem.Block{
|
||||||
Type: "RSA PRIVATE KEY",
|
Type: "RSA PRIVATE KEY",
|
||||||
Headers: nil,
|
Headers: nil,
|
||||||
Bytes: x509.MarshalPKCS1PrivateKey(privateKey),
|
Bytes: x509.MarshalPKCS1PrivateKey(privateKey),
|
||||||
}
|
}
|
||||||
|
|
||||||
return string(pem.EncodeToMemory(&privateKeyBlock))
|
return string(pem.EncodeToMemory(&privateKeyBlock))
|
||||||
|
|
Reference in New Issue