2021-04-04 17:06:17 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
mrand "math/rand"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2021-04-13 19:17:21 +00:00
|
|
|
logging "github.com/ipfs/go-log"
|
2021-04-04 17:06:17 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
|
|
|
"github.com/status-im/go-waku/waku/v2/node"
|
2021-04-15 02:22:18 +00:00
|
|
|
store "github.com/status-im/go-waku/waku/v2/protocol/waku_store"
|
2021-04-04 17:06:17 +00:00
|
|
|
)
|
|
|
|
|
2021-04-08 23:01:23 +00:00
|
|
|
var DefaultContentTopic string = "dingpu"
|
2021-04-06 23:27:54 +00:00
|
|
|
|
2021-04-04 17:06:17 +00:00
|
|
|
func main() {
|
|
|
|
mrand.Seed(time.Now().UTC().UnixNano())
|
|
|
|
|
|
|
|
nickFlag := flag.String("nick", "", "nickname to use in chat. will be generated if empty")
|
|
|
|
nodeKeyFlag := flag.String("nodekey", "", "private key for this node. will be generated if empty")
|
|
|
|
staticNodeFlag := flag.String("staticnode", "", "connects to a node. will get a random node from fleets.status.im if empty")
|
2021-04-06 23:27:54 +00:00
|
|
|
storeNodeFlag := flag.String("storenode", "", "connects to a store node to retrieve messages. will get a random node from fleets.status.im if empty")
|
2021-04-04 17:06:17 +00:00
|
|
|
port := flag.Int("port", 0, "port. Will be random if 0")
|
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
hostAddr, _ := net.ResolveTCPAddr("tcp", fmt.Sprintf("0.0.0.0:%d", *port))
|
|
|
|
|
|
|
|
// use the nickname from the cli flag, or a default if blank
|
|
|
|
nodekey := *nodeKeyFlag
|
|
|
|
if len(nodekey) == 0 {
|
|
|
|
var err error
|
|
|
|
nodekey, err = randomHex(32)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Could not generate random key")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
prvKey, err := crypto.HexToECDSA(nodekey)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
wakuNode, err := node.New(ctx, prvKey, []net.Addr{hostAddr})
|
|
|
|
if err != nil {
|
|
|
|
fmt.Print(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
wakuNode.MountRelay()
|
2021-04-15 02:22:18 +00:00
|
|
|
wakuNode.MountStore(false, nil)
|
2021-04-04 17:06:17 +00:00
|
|
|
|
|
|
|
// use the nickname from the cli flag, or a default if blank
|
|
|
|
nick := *nickFlag
|
|
|
|
if len(nick) == 0 {
|
|
|
|
nick = defaultNick(wakuNode.Host().ID())
|
|
|
|
}
|
|
|
|
|
|
|
|
// join the chat
|
|
|
|
chat, err := NewChat(ctx, wakuNode, wakuNode.Host().ID(), nick)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-04-13 19:17:21 +00:00
|
|
|
// Display panic level to reduce log noise
|
|
|
|
lvl, err := logging.LevelFromString("panic")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
logging.SetAllLoggers(lvl)
|
|
|
|
|
2021-04-04 17:06:17 +00:00
|
|
|
ui := NewChatUI(chat)
|
|
|
|
|
|
|
|
// Connect to a static node or use random node from fleets.status.im
|
|
|
|
go func() {
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
|
|
|
|
staticnode := *staticNodeFlag
|
2021-04-06 23:27:54 +00:00
|
|
|
storenode := *storeNodeFlag
|
2021-04-15 02:22:18 +00:00
|
|
|
|
2021-04-06 23:27:54 +00:00
|
|
|
var fleetData []byte
|
|
|
|
if len(staticnode) == 0 || len(storenode) == 0 {
|
|
|
|
fleetData = getFleetData()
|
|
|
|
}
|
|
|
|
|
2021-04-04 17:06:17 +00:00
|
|
|
if len(staticnode) == 0 {
|
|
|
|
ui.displayMessage("No static peers configured. Choosing one at random from test fleet...")
|
2021-04-06 23:27:54 +00:00
|
|
|
staticnode = getRandomFleetNode(fleetData)
|
2021-04-04 17:06:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = wakuNode.DialPeer(staticnode)
|
|
|
|
if err != nil {
|
|
|
|
ui.displayMessage("Could not connect to peer: " + err.Error())
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
ui.displayMessage("Connected to peer: " + staticnode)
|
|
|
|
|
|
|
|
}
|
2021-04-06 23:27:54 +00:00
|
|
|
|
|
|
|
if len(storenode) == 0 {
|
|
|
|
ui.displayMessage("No store node configured. Choosing one at random from test fleet...")
|
|
|
|
storenode = getRandomFleetNode(fleetData)
|
|
|
|
}
|
|
|
|
|
2021-04-15 02:22:18 +00:00
|
|
|
storeNodeId, err := wakuNode.AddStorePeer(storenode)
|
2021-04-06 23:27:54 +00:00
|
|
|
if err != nil {
|
|
|
|
ui.displayMessage("Could not connect to storenode: " + err.Error())
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
ui.displayMessage("Connected to storenode: " + storenode)
|
|
|
|
}
|
|
|
|
|
2021-04-12 18:03:58 +00:00
|
|
|
time.Sleep(300 * time.Millisecond)
|
2021-04-06 23:27:54 +00:00
|
|
|
ui.displayMessage("Querying historic messages")
|
2021-04-15 02:22:18 +00:00
|
|
|
|
|
|
|
response, err := wakuNode.Query([]string{DefaultContentTopic}, 0, 0,
|
|
|
|
store.WithAutomaticRequestId(),
|
|
|
|
store.WithPeer(string(*storeNodeId)),
|
|
|
|
store.WithPaging(true, 0))
|
|
|
|
|
2021-04-06 23:27:54 +00:00
|
|
|
if err != nil {
|
2021-04-12 18:03:58 +00:00
|
|
|
ui.displayMessage("Could not query storenode: " + err.Error())
|
|
|
|
} else {
|
|
|
|
chat.displayMessages(response.Messages)
|
|
|
|
}
|
2021-04-04 17:06:17 +00:00
|
|
|
}()
|
|
|
|
|
2021-04-06 23:27:54 +00:00
|
|
|
//draw the UI
|
2021-04-04 17:06:17 +00:00
|
|
|
if err = ui.Run(); err != nil {
|
|
|
|
printErr("error running text UI: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generates a random hex string with a length of n
|
|
|
|
func randomHex(n int) (string, error) {
|
|
|
|
bytes := make([]byte, n)
|
|
|
|
if _, err := rand.Read(bytes); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return hex.EncodeToString(bytes), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// printErr is like fmt.Printf, but writes to stderr.
|
|
|
|
func printErr(m string, args ...interface{}) {
|
|
|
|
fmt.Fprintf(os.Stderr, m, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// defaultNick generates a nickname based on the $USER environment variable and
|
|
|
|
// the last 8 chars of a peer ID.
|
|
|
|
func defaultNick(p peer.ID) string {
|
|
|
|
return fmt.Sprintf("%s-%s", os.Getenv("USER"), shortID(p))
|
|
|
|
}
|
|
|
|
|
|
|
|
// shortID returns the last 8 chars of a base58-encoded peer id.
|
|
|
|
func shortID(p peer.ID) string {
|
|
|
|
pretty := p.Pretty()
|
|
|
|
return pretty[len(pretty)-8:]
|
|
|
|
}
|
|
|
|
|
2021-04-06 23:27:54 +00:00
|
|
|
func getFleetData() []byte {
|
2021-04-04 17:06:17 +00:00
|
|
|
url := "https://fleets.status.im"
|
|
|
|
httpClient := http.Client{
|
|
|
|
Timeout: time.Second * 2,
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodGet, url, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
res, getErr := httpClient.Do(req)
|
|
|
|
if getErr != nil {
|
|
|
|
log.Fatal(getErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.Body != nil {
|
|
|
|
defer res.Body.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
body, readErr := ioutil.ReadAll(res.Body)
|
|
|
|
if readErr != nil {
|
|
|
|
log.Fatal(readErr)
|
|
|
|
}
|
|
|
|
|
2021-04-06 23:27:54 +00:00
|
|
|
return body
|
|
|
|
}
|
|
|
|
|
|
|
|
func getRandomFleetNode(data []byte) string {
|
2021-04-04 17:06:17 +00:00
|
|
|
var result map[string]interface{}
|
2021-04-06 23:27:54 +00:00
|
|
|
json.Unmarshal(data, &result)
|
2021-04-04 17:06:17 +00:00
|
|
|
|
|
|
|
fleets := result["fleets"].(map[string]interface{})
|
|
|
|
wakuv2Test := fleets["wakuv2.test"].(map[string]interface{})
|
|
|
|
waku := wakuv2Test["waku"].(map[string]interface{})
|
|
|
|
|
|
|
|
var wakunodes []string
|
|
|
|
for v := range waku {
|
|
|
|
wakunodes = append(wakunodes, v)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
randKey := wakunodes[mrand.Intn(len(wakunodes))]
|
|
|
|
|
|
|
|
return waku[randKey].(string)
|
|
|
|
}
|