From 3469935bcb5bdc235311dc4657cd1f4bea17ab6a Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Fri, 1 Jan 2016 18:08:28 -0800 Subject: [PATCH] Adding examples --- examples/echobot/main.go | 30 ++++++++++++++++++++++++++++++ ricochet.go | 13 +++++++++++-- 2 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 examples/echobot/main.go diff --git a/examples/echobot/main.go b/examples/echobot/main.go new file mode 100644 index 0000000..1aafec2 --- /dev/null +++ b/examples/echobot/main.go @@ -0,0 +1,30 @@ +package main + +import ( + "fmt" + "github.com/s-rah/go-ricochet" +) + +func main() { + ricochet := new(goricochet.Ricochet) + + // You will want to replace these values with your own test credentials + ricochet.Init("./private_key", true) + ricochet.Connect("kwke2hntvyfqm7dr", "127.0.0.1:55555|jlq67qzo6s4yp3sp") + + // Not needed passed the initial run + // TODO need to wait for contact response before sending OpenChannel + // ricochet.SendContactRequest("EchoBot", "I'm an EchoBot") + + go ricochet.ListenAndWait() + ricochet.OpenChannel("im.ricochet.chat", 5) + ricochet.SendMessage("Hi I'm an echo bot, I echo what you say! ", 5) + + for true { + message,channel,_ := ricochet.Listen() + fmt.Print(message, channel) + if message != "" { + ricochet.SendMessage(message, 5) + } + } +} diff --git a/ricochet.go b/ricochet.go index d760571..8749287 100644 --- a/ricochet.go +++ b/ricochet.go @@ -3,6 +3,7 @@ package goricochet import ( "crypto" "crypto/hmac" + "crypto/rand" "crypto/rsa" "crypto/sha256" "crypto/x509" @@ -17,6 +18,7 @@ import ( "github.com/s-rah/go-ricochet/contact" "github.com/s-rah/go-ricochet/control" "h12.me/socks" + "io" "io/ioutil" "log" "net" @@ -128,7 +130,11 @@ func (r *Ricochet) Connect(from string, to string) error { ChannelIdentifier: proto.Int32(1), ChannelType: proto.String("im.ricochet.auth.hidden-service"), } - err := proto.SetExtension(oc, Protocol_Data_AuthHiddenService.E_ClientCookie, []byte("0000000000000000")) + + var cookie [16]byte + io.ReadFull(rand.Reader, cookie[:]) + + err := proto.SetExtension(oc, Protocol_Data_AuthHiddenService.E_ClientCookie, cookie[:]) pc := &Protocol_Data_Control.Packet{ OpenChannel: oc, } @@ -157,7 +163,7 @@ func (r *Ricochet) Connect(from string, to string) error { r.logger.Print("Starting Authentication with Server Cookie: ", serverCookie) key := make([]byte, 32) - copy(key[0:16], []byte("0000000000000000")) + copy(key[0:16], cookie[:]) copy(key[16:], serverCookie) value := []byte(from + to) r.logger.Print("Got Hmac Key: ", key) @@ -392,8 +398,11 @@ func (r *Ricochet) ListenAndWait() error { r.logger.Print("Channel Opened Successfully: ", channelResult.GetChannelIdentifier()) r.channelState[int(message.ControlPacket.GetChannelResult().GetChannelIdentifier())] = 1 } + break } + } else if packet.Channel == 3 { + // Contact Request } else { // At this point the only other expected type of message // is a Chat Message