initial commit
This commit is contained in:
commit
f90c9080ac
|
@ -0,0 +1 @@
|
|||
.idea*
|
|
@ -0,0 +1,289 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
libpeer "cwtch.im/cwtch/peer"
|
||||
"os"
|
||||
"fmt"
|
||||
"github.com/sethvargo/go-diceware/diceware"
|
||||
"bufio"
|
||||
"math/big"
|
||||
"time"
|
||||
"log"
|
||||
"io/ioutil"
|
||||
"cwtch.im/cwtch/model"
|
||||
)
|
||||
|
||||
func driftoff() {
|
||||
time.Sleep(time.Second * 15)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func addContactEntry(peer libpeer.CwtchPeer, name string, onion string) {
|
||||
pp := model.PublicProfile{
|
||||
name,
|
||||
nil,
|
||||
false,
|
||||
false,
|
||||
onion}
|
||||
|
||||
if peer == nil {
|
||||
fmt.Printf("peer is nil?!?!?\n")
|
||||
}
|
||||
|
||||
peer.GetProfile().Contacts[onion] = &pp
|
||||
peer.GetProfile().SetCustomAttribute(onion+"_name", name)
|
||||
peer.GetProfile().SetCustomAttribute(name+"_onion", onion)
|
||||
}
|
||||
|
||||
func printHelp() {
|
||||
fmt.Println("usage: sendafriend [command] [options]\nCommands: help, setname, info, list, add, send, receive, hide, seek")
|
||||
}
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
printHelp()
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
log.SetFlags(0)
|
||||
log.SetOutput(ioutil.Discard)
|
||||
inviteStr := "torv30I0zJgNiUz57gJlxFsp4PUq47WDoKNCL95GEadaLlEE=EsABCiA4MjViZTVjODU0ZDFhNGRhNjM3NmExNTBlYjM2ZjM2NxIgPAl3zAsMfauSZQyiGXlj7u6M6usYN/HqtPD2jafjwTAaOGloZDNlYXdhbjZ2aG1kaDdudDVicXh5aHVlM2k0aXgzb3kyZWZrcHdkaDZyNGptcTNhZXJ0dWlkIkDhF3Ko8nAtEJPvjpCjwFM4py0CeAhYCTc9l9H+Wah8traU15D/JzIHEAKhaDiIzmKqBOECk9Q+G40Y6pXPWKkI"
|
||||
|
||||
peer, err := libpeer.LoadCwtchPeer("Whoami", "be gay do crime")
|
||||
if err != nil {
|
||||
fmt.Println("couldn't load your Whoami file, attempting to create a new one now")
|
||||
|
||||
names, err := diceware.Generate(1)
|
||||
peer, err = libpeer.NewCwtchPeer(names[0], "be gay do crime", "Whoami")
|
||||
if err != nil {
|
||||
fmt.Println("couldn't create one either :( exiting")
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
switch os.Args[1] {
|
||||
default:
|
||||
printHelp()
|
||||
case "help":
|
||||
printHelp()
|
||||
case "iam":
|
||||
if len(os.Args) != 3 {
|
||||
fmt.Println("example: sendafriend iam alice")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
peer.GetProfile().Name = os.Args[2]
|
||||
fmt.Printf("hi, %v!\n", os.Args[2])
|
||||
case "setname":
|
||||
if len(os.Args) == 3 {
|
||||
fmt.Println("example: sendafriend setname alice xyz")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
_, exists := peer.GetProfile().GetCustomAttribute(os.Args[3]+"_name")
|
||||
if !exists {
|
||||
fmt.Printf("um you don't have anyone with that onion address in your list")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
peer.GetProfile().SetCustomAttribute(os.Args[3]+"_name", os.Args[2])
|
||||
peer.GetProfile().SetCustomAttribute(os.Args[2]+"_onion", os.Args[3])
|
||||
//todo: unset old one
|
||||
fmt.Printf("okay, i'll remember that %v is named %v\n", os.Args[3], os.Args[2])
|
||||
case "info":
|
||||
fmt.Printf("your name is currently %v and your address is currently %v\n", peer.GetProfile().Name, peer.GetProfile().Onion);
|
||||
case "list":
|
||||
contacts := peer.GetContacts()
|
||||
for i := range contacts {
|
||||
attr, _ := peer.GetProfile().GetCustomAttribute(contacts[i] + "_name")
|
||||
fmt.Printf("%v <%v>\n", attr, contacts[i])
|
||||
}
|
||||
case "add":
|
||||
if len(os.Args) != 4 {
|
||||
fmt.Println("example: sendafriend add bob xyz")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
_, exists := peer.GetProfile().GetCustomAttribute(os.Args[3]+"_name")
|
||||
if exists {
|
||||
fmt.Printf("woah you already have someone named %v\n", os.Args[3])
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
addContactEntry(peer, os.Args[2], os.Args[3])
|
||||
|
||||
fmt.Printf("okay, added %v <%v>\n", os.Args[2], os.Args[3])
|
||||
case "send":
|
||||
if len(os.Args) != 3 {
|
||||
fmt.Println("example: sendafriend send bob < file.txt")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
onion, exists := peer.GetProfile().GetCustomAttribute(os.Args[2]+"_onion")
|
||||
if !exists {
|
||||
fmt.Printf("you don't seem to have a contact named %v\n", os.Args[2])
|
||||
}
|
||||
|
||||
processData := func(onion string, data []byte) []byte {
|
||||
return nil
|
||||
}
|
||||
|
||||
peer.SetPeerDataHandler(processData)
|
||||
|
||||
fmt.Printf("connecting to %v...\n", os.Args[2])
|
||||
connection := peer.PeerWithOnion(onion)
|
||||
|
||||
fmt.Printf("sending data...\n")
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
packet := make([]byte, 1024)
|
||||
br, _ := reader.Read(packet)
|
||||
|
||||
connection.SendPacket(packet[:br])
|
||||
case "receive":
|
||||
if len(os.Args) != 3 {
|
||||
fmt.Fprintf(os.Stderr, "example: sendafriend receive bob > file.txt\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
_, exists := peer.GetProfile().GetCustomAttribute(os.Args[2]+"_name")
|
||||
if exists {
|
||||
fmt.Printf("woah you already have someone named %v\n", os.Args[2])
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
verifyOnion, exists := peer.GetProfile().GetCustomAttribute(os.Args[2]+"_onion")
|
||||
if !exists {
|
||||
fmt.Fprintf(os.Stderr, "hmm you don't have a contact named %v\n", os.Args[2])
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
processData := func(onion string, data []byte) []byte {
|
||||
if onion != verifyOnion {
|
||||
fmt.Fprintf(os.Stderr, "woah, got some unauthenticated data. discarding it!\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Printf("%s", data)
|
||||
os.Exit(0)
|
||||
return nil
|
||||
}
|
||||
|
||||
peer.SetPeerDataHandler(processData)
|
||||
fmt.Fprintf(os.Stderr, "waiting for %v to send...\n", os.Args[2])
|
||||
peer.Listen()
|
||||
case "hide":
|
||||
if len(os.Args) != 3 {
|
||||
fmt.Fprintf(os.Stderr, "example: sendafriend hide bob\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
suggestedSecret, _ := diceware.Generate(1)
|
||||
fmt.Printf("enter a secret that only the two of you would know [%v]: ", suggestedSecret[0])
|
||||
in := bufio.NewReader(os.Stdin)
|
||||
secret, _ := in.ReadString('\n')
|
||||
secret = secret[:len(secret) - 1]
|
||||
|
||||
if secret == "" {
|
||||
secret = suggestedSecret[0]
|
||||
}
|
||||
|
||||
ephemeralPeer, err := libpeer.NewCwtchPeer("Alice", "alicepass", "")
|
||||
if err != nil {
|
||||
fmt.Printf("couldn't create an ephemeral onion address: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
groupId, err := ephemeralPeer.ImportGroup(inviteStr)
|
||||
if err != nil {
|
||||
fmt.Printf("couldn't configure the bbs settings: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
group := ephemeralPeer.GetGroup(groupId)
|
||||
ephemeralPeer.JoinServer(group.GroupServer)
|
||||
time.Sleep(time.Second * 30) //TODO: can this be shorter?
|
||||
|
||||
err = ephemeralPeer.SendMessageToGroup(groupId, "be gay do crimes")
|
||||
if err != nil {
|
||||
fmt.Printf("the hide-n-seek server is down or something? try again i guess\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Println("okay, now have your friend type \"sendafriend seek\" and enter the secret")
|
||||
|
||||
convos := make(map[string]*Conversation)
|
||||
processData := func(onion string, data []byte) []byte {
|
||||
if _, contains := convos[onion]; !contains {
|
||||
convos[onion] = new(Conversation)
|
||||
secretInt := new(big.Int)
|
||||
secretInt.SetBytes([]byte(secret))
|
||||
convos[onion].smp.secret = secretInt
|
||||
}
|
||||
|
||||
tlvPacket := arr2tlv(data)
|
||||
if tlvPacket.typ == 42 {
|
||||
fmt.Printf("you've been found! adding %v <%s> to contact list\n", os.Args[2], tlvPacket.data)
|
||||
addContactEntry(peer, os.Args[2], string(tlvPacket.data))
|
||||
peer.Save()
|
||||
finalPacket := tlv{42, uint16(len(peer.GetProfile().Onion)), []byte(peer.GetProfile().Onion)}
|
||||
go driftoff()
|
||||
return tlv2arr(finalPacket)
|
||||
}
|
||||
|
||||
out, _, _ := convos[onion].processSMP(tlvPacket)
|
||||
return tlv2arr(out)
|
||||
}
|
||||
ephemeralPeer.SetPeerDataHandler(processData)
|
||||
ephemeralPeer.Listen()
|
||||
|
||||
fmt.Println("press enter to cancel the game and exit")
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
reader.ReadString('\n')
|
||||
case "seek":
|
||||
if len(os.Args) != 3 {
|
||||
fmt.Fprintf(os.Stderr, "example: sendafriend seek alice\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Print("enter a secret that only the two of you would know: ")
|
||||
in := bufio.NewReader(os.Stdin)
|
||||
secret, _ := in.ReadString('\n')
|
||||
secret = secret[:len(secret) - 1]
|
||||
|
||||
ephemeralPeer, err := libpeer.NewCwtchPeer("Alice", "alicepass", "")
|
||||
if err != nil {
|
||||
fmt.Printf("couldn't create an ephemeral onion address: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
groupId, err := ephemeralPeer.ImportGroup(inviteStr)
|
||||
if err != nil {
|
||||
fmt.Printf("couldn't configure the bbs settings: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
group := ephemeralPeer.GetGroup(groupId)
|
||||
ephemeralPeer.JoinServer(group.GroupServer)
|
||||
time.Sleep(time.Second * 10) //TODO: can this be shorter?
|
||||
|
||||
messages := group.GetTimeline()
|
||||
|
||||
for i := len(messages) - 1; i >= 0; i-- {
|
||||
fmt.Printf("checking advert %d/%d...\n", len(messages) - i, len(messages))
|
||||
//fmt.Printf("%v <%v> %v\n", messages[i].Timestamp, messages[i].PeerID, messages[i].Message)
|
||||
|
||||
if messages[i].Message == "be gay do crimes" {
|
||||
onion, success := doSMP(messages[i].PeerID, secret, os.Args[2], peer.GetProfile().Onion)
|
||||
if success {
|
||||
addContactEntry(peer, os.Args[2], onion)
|
||||
peer.Save()
|
||||
os.Exit(0)
|
||||
} else {
|
||||
fmt.Printf("debug:fail\n")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
peer.Save()
|
||||
}
|
Loading…
Reference in New Issue