2019-10-10 11:07:00 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-10-29 13:06:02 +00:00
|
|
|
"bytes"
|
2019-10-10 11:07:00 +00:00
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2019-11-10 16:26:39 +00:00
|
|
|
"time"
|
2019-10-10 11:07:00 +00:00
|
|
|
|
2019-10-11 06:11:57 +00:00
|
|
|
rice "github.com/GeertJohan/go.rice"
|
2019-10-10 11:07:00 +00:00
|
|
|
"github.com/gorilla/websocket"
|
2020-05-05 01:31:56 +00:00
|
|
|
"github.com/ipld/go-car"
|
2019-10-10 11:07:00 +00:00
|
|
|
"github.com/libp2p/go-libp2p"
|
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
|
|
|
|
2019-11-10 16:26:39 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
2020-07-23 02:05:11 +00:00
|
|
|
"github.com/filecoin-project/lotus/lib/blockstore"
|
2019-10-10 11:07:00 +00:00
|
|
|
)
|
|
|
|
|
2019-10-29 13:06:02 +00:00
|
|
|
var topic = "/fil/headnotifs/"
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
genBytes := build.MaybeGenesis()
|
2020-01-13 20:51:34 +00:00
|
|
|
if len(genBytes) == 0 {
|
|
|
|
topic = ""
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-23 02:05:11 +00:00
|
|
|
bs := blockstore.NewTemporary()
|
2019-10-29 13:06:02 +00:00
|
|
|
|
|
|
|
c, err := car.LoadCar(bs, bytes.NewReader(genBytes))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if len(c.Roots) != 1 {
|
|
|
|
panic("expected genesis file to have one root")
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Genesis CID: %s\n", c.Roots[0])
|
|
|
|
topic = topic + c.Roots[0].String()
|
|
|
|
}
|
2019-10-10 11:07:00 +00:00
|
|
|
|
|
|
|
var upgrader = websocket.Upgrader{
|
|
|
|
WriteBufferSize: 1024,
|
|
|
|
CheckOrigin: func(r *http.Request) bool {
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2020-01-13 20:51:34 +00:00
|
|
|
if topic == "" {
|
|
|
|
fmt.Println("FATAL: No genesis found")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-10 11:07:00 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
host, err := libp2p.New(
|
|
|
|
ctx,
|
|
|
|
libp2p.Defaults,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
ps, err := pubsub.NewGossipSub(ctx, host)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-10-29 13:06:02 +00:00
|
|
|
pi, err := build.BuiltinBootstrap()
|
2019-10-10 11:07:00 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := host.Connect(ctx, pi[0]); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
http.HandleFunc("/sub", handler(ps))
|
2019-10-11 06:11:57 +00:00
|
|
|
http.Handle("/", http.FileServer(rice.MustFindBox("townhall/build").HTTPBox()))
|
2019-10-10 11:07:00 +00:00
|
|
|
|
2019-10-11 06:11:57 +00:00
|
|
|
fmt.Println("listening on http://localhost:2975")
|
2019-10-10 11:07:00 +00:00
|
|
|
|
|
|
|
if err := http.ListenAndServe("0.0.0.0:2975", nil); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type update struct {
|
|
|
|
From peer.ID
|
|
|
|
Update json.RawMessage
|
2019-11-10 16:26:39 +00:00
|
|
|
Time uint64
|
2019-10-10 11:07:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func handler(ps *pubsub.PubSub) func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
|
|
if r.Header.Get("Sec-WebSocket-Protocol") != "" {
|
|
|
|
w.Header().Set("Sec-WebSocket-Protocol", r.Header.Get("Sec-WebSocket-Protocol"))
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := upgrader.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-20 04:49:10 +00:00
|
|
|
sub, err := ps.Subscribe(topic) //nolint
|
2019-10-10 11:07:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-08-20 04:49:10 +00:00
|
|
|
defer sub.Cancel() //nolint:errcheck
|
2019-10-10 11:07:00 +00:00
|
|
|
|
2019-11-10 16:26:39 +00:00
|
|
|
fmt.Println("new conn")
|
|
|
|
|
2019-10-10 11:07:00 +00:00
|
|
|
for {
|
|
|
|
msg, err := sub.Next(r.Context())
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-10 16:26:39 +00:00
|
|
|
//fmt.Println(msg)
|
2019-10-10 11:07:00 +00:00
|
|
|
|
|
|
|
if err := conn.WriteJSON(update{
|
|
|
|
From: peer.ID(msg.From),
|
|
|
|
Update: msg.Data,
|
2019-11-10 16:26:39 +00:00
|
|
|
Time: uint64(time.Now().UnixNano() / 1000_000),
|
2019-10-10 11:07:00 +00:00
|
|
|
}); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|