Adding examples

This commit is contained in:
Sarah Jamie Lewis 2016-01-01 18:08:28 -08:00
parent 64d08bdff0
commit 3469935bcb
2 changed files with 41 additions and 2 deletions

30
examples/echobot/main.go Normal file
View File

@ -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)
}
}
}

View File

@ -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