2022-07-24 20:51:42 +00:00
|
|
|
package rest
|
|
|
|
|
|
|
|
import (
|
2022-12-10 16:21:22 +00:00
|
|
|
"context"
|
2022-07-24 20:51:42 +00:00
|
|
|
"encoding/json"
|
2023-09-07 18:00:59 +00:00
|
|
|
"errors"
|
2022-07-24 20:51:42 +00:00
|
|
|
"net/http"
|
2023-10-23 22:59:02 +00:00
|
|
|
"net/url"
|
2022-07-24 20:51:42 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
2023-02-16 20:05:24 +00:00
|
|
|
"github.com/go-chi/chi/v5"
|
2023-09-07 18:00:59 +00:00
|
|
|
"github.com/waku-org/go-waku/cmd/waku/server"
|
2022-11-09 19:53:01 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/node"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/pb"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/relay"
|
2022-07-24 20:51:42 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2023-09-07 18:00:59 +00:00
|
|
|
const routeRelayV1Subscriptions = "/relay/v1/subscriptions"
|
|
|
|
const routeRelayV1Messages = "/relay/v1/messages/{topic}"
|
2022-07-24 20:51:42 +00:00
|
|
|
|
2023-10-23 22:59:02 +00:00
|
|
|
const routeRelayV1AutoSubscriptions = "/relay/v1/auto/subscriptions"
|
|
|
|
const routeRelayV1AutoMessages = "/relay/v1/auto/messages"
|
|
|
|
|
2023-09-07 18:00:59 +00:00
|
|
|
// RelayService represents the REST service for WakuRelay
|
2022-07-24 20:51:42 +00:00
|
|
|
type RelayService struct {
|
2022-12-10 16:21:22 +00:00
|
|
|
node *node.WakuNode
|
|
|
|
cancel context.CancelFunc
|
2022-07-24 20:51:42 +00:00
|
|
|
|
|
|
|
log *zap.Logger
|
|
|
|
|
|
|
|
messages map[string][]*pb.WakuMessage
|
|
|
|
cacheCapacity int
|
|
|
|
messagesMutex sync.RWMutex
|
|
|
|
|
|
|
|
runner *runnerService
|
|
|
|
}
|
|
|
|
|
2023-09-07 18:00:59 +00:00
|
|
|
// NewRelayService returns an instance of RelayService
|
2023-02-16 20:05:24 +00:00
|
|
|
func NewRelayService(node *node.WakuNode, m *chi.Mux, cacheCapacity int, log *zap.Logger) *RelayService {
|
2022-07-24 20:51:42 +00:00
|
|
|
s := &RelayService{
|
|
|
|
node: node,
|
|
|
|
log: log.Named("relay"),
|
|
|
|
cacheCapacity: cacheCapacity,
|
|
|
|
messages: make(map[string][]*pb.WakuMessage),
|
|
|
|
}
|
|
|
|
|
|
|
|
s.runner = newRunnerService(node.Broadcaster(), s.addEnvelope)
|
|
|
|
|
2023-09-07 18:00:59 +00:00
|
|
|
m.Post(routeRelayV1Subscriptions, s.postV1Subscriptions)
|
|
|
|
m.Delete(routeRelayV1Subscriptions, s.deleteV1Subscriptions)
|
|
|
|
m.Get(routeRelayV1Messages, s.getV1Messages)
|
|
|
|
m.Post(routeRelayV1Messages, s.postV1Message)
|
2022-07-24 20:51:42 +00:00
|
|
|
|
2023-10-23 22:59:02 +00:00
|
|
|
m.Post(routeRelayV1AutoSubscriptions, s.postV1AutoSubscriptions)
|
|
|
|
m.Delete(routeRelayV1AutoSubscriptions, s.deleteV1AutoSubscriptions)
|
|
|
|
|
|
|
|
m.Route(routeRelayV1AutoMessages, func(r chi.Router) {
|
|
|
|
r.Get("/{contentTopic}", s.getV1AutoMessages)
|
|
|
|
r.Post("/", s.postV1AutoMessage)
|
|
|
|
})
|
|
|
|
|
2022-07-24 20:51:42 +00:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RelayService) addEnvelope(envelope *protocol.Envelope) {
|
|
|
|
r.messagesMutex.Lock()
|
|
|
|
defer r.messagesMutex.Unlock()
|
|
|
|
|
|
|
|
if _, ok := r.messages[envelope.PubsubTopic()]; !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep a specific max number of messages per topic
|
|
|
|
if len(r.messages[envelope.PubsubTopic()]) >= r.cacheCapacity {
|
|
|
|
r.messages[envelope.PubsubTopic()] = r.messages[envelope.PubsubTopic()][1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
r.messages[envelope.PubsubTopic()] = append(r.messages[envelope.PubsubTopic()], envelope.Message())
|
|
|
|
}
|
|
|
|
|
2023-09-07 18:00:59 +00:00
|
|
|
// Start starts the RelayService
|
2022-12-10 16:21:22 +00:00
|
|
|
func (r *RelayService) Start(ctx context.Context) {
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
r.cancel = cancel
|
|
|
|
|
2023-02-07 22:26:07 +00:00
|
|
|
r.messagesMutex.Lock()
|
2022-07-24 20:51:42 +00:00
|
|
|
// Node may already be subscribed to some topics when Relay API handlers are installed. Let's add these
|
|
|
|
for _, topic := range r.node.Relay().Topics() {
|
|
|
|
r.log.Info("adding topic handler for existing subscription", zap.String("topic", topic))
|
2022-09-12 14:13:38 +00:00
|
|
|
r.messages[topic] = []*pb.WakuMessage{}
|
2022-07-24 20:51:42 +00:00
|
|
|
}
|
2023-02-07 22:26:07 +00:00
|
|
|
r.messagesMutex.Unlock()
|
2022-07-24 20:51:42 +00:00
|
|
|
|
2022-12-10 16:21:22 +00:00
|
|
|
r.runner.Start(ctx)
|
2022-07-24 20:51:42 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 18:00:59 +00:00
|
|
|
// Stop stops the RelayService
|
2022-07-24 20:51:42 +00:00
|
|
|
func (r *RelayService) Stop() {
|
2023-01-06 22:37:57 +00:00
|
|
|
if r.cancel == nil {
|
|
|
|
return
|
|
|
|
}
|
2022-12-10 16:21:22 +00:00
|
|
|
r.cancel()
|
2022-07-24 20:51:42 +00:00
|
|
|
}
|
|
|
|
|
2023-09-11 14:24:05 +00:00
|
|
|
func (r *RelayService) deleteV1Subscriptions(w http.ResponseWriter, req *http.Request) {
|
2022-07-24 20:51:42 +00:00
|
|
|
var topics []string
|
2023-09-11 14:24:05 +00:00
|
|
|
decoder := json.NewDecoder(req.Body)
|
2022-07-24 20:51:42 +00:00
|
|
|
if err := decoder.Decode(&topics); err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2023-09-11 14:24:05 +00:00
|
|
|
defer req.Body.Close()
|
2022-07-24 20:51:42 +00:00
|
|
|
|
2023-09-11 14:24:05 +00:00
|
|
|
r.messagesMutex.Lock()
|
|
|
|
defer r.messagesMutex.Unlock()
|
2022-07-24 20:51:42 +00:00
|
|
|
|
|
|
|
var err error
|
|
|
|
for _, topic := range topics {
|
2023-10-20 19:56:18 +00:00
|
|
|
err = r.node.Relay().Unsubscribe(req.Context(), protocol.NewContentFilter(topic))
|
2022-07-24 20:51:42 +00:00
|
|
|
if err != nil {
|
2023-09-11 14:24:05 +00:00
|
|
|
r.log.Error("unsubscribing from topic", zap.String("topic", strings.Replace(strings.Replace(topic, "\n", "", -1), "\r", "", -1)), zap.Error(err))
|
2022-07-24 20:51:42 +00:00
|
|
|
} else {
|
2023-09-11 14:24:05 +00:00
|
|
|
delete(r.messages, topic)
|
2022-07-24 20:51:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
writeErrOrResponse(w, err, true)
|
|
|
|
}
|
|
|
|
|
2023-09-11 14:24:05 +00:00
|
|
|
func (r *RelayService) postV1Subscriptions(w http.ResponseWriter, req *http.Request) {
|
2022-07-24 20:51:42 +00:00
|
|
|
var topics []string
|
2023-09-11 14:24:05 +00:00
|
|
|
decoder := json.NewDecoder(req.Body)
|
2022-07-24 20:51:42 +00:00
|
|
|
if err := decoder.Decode(&topics); err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2023-09-11 14:24:05 +00:00
|
|
|
defer req.Body.Close()
|
2022-07-24 20:51:42 +00:00
|
|
|
|
|
|
|
var err error
|
|
|
|
var sub *relay.Subscription
|
2023-10-20 19:56:18 +00:00
|
|
|
var subs []*relay.Subscription
|
2022-07-24 20:51:42 +00:00
|
|
|
var topicToSubscribe string
|
|
|
|
for _, topic := range topics {
|
|
|
|
if topic == "" {
|
2023-10-20 19:56:18 +00:00
|
|
|
subs, err = r.node.Relay().Subscribe(req.Context(), protocol.NewContentFilter(relay.DefaultWakuTopic))
|
2022-07-24 20:51:42 +00:00
|
|
|
topicToSubscribe = relay.DefaultWakuTopic
|
|
|
|
} else {
|
2023-10-20 19:56:18 +00:00
|
|
|
subs, err = r.node.Relay().Subscribe(req.Context(), protocol.NewContentFilter(topic))
|
2022-07-24 20:51:42 +00:00
|
|
|
topicToSubscribe = topic
|
|
|
|
}
|
|
|
|
if err != nil {
|
2023-09-11 14:24:05 +00:00
|
|
|
r.log.Error("subscribing to topic", zap.String("topic", strings.Replace(topicToSubscribe, "\n", "", -1)), zap.Error(err))
|
2022-07-24 20:51:42 +00:00
|
|
|
} else {
|
2023-10-20 19:56:18 +00:00
|
|
|
sub = subs[0]
|
2023-05-05 09:49:15 +00:00
|
|
|
sub.Unsubscribe()
|
2023-09-11 14:24:05 +00:00
|
|
|
r.messagesMutex.Lock()
|
|
|
|
r.messages[topic] = []*pb.WakuMessage{}
|
|
|
|
r.messagesMutex.Unlock()
|
2022-07-24 20:51:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
writeErrOrResponse(w, err, true)
|
|
|
|
}
|
|
|
|
|
2023-09-11 14:24:05 +00:00
|
|
|
func (r *RelayService) getV1Messages(w http.ResponseWriter, req *http.Request) {
|
|
|
|
topic := chi.URLParam(req, "topic")
|
2023-02-16 20:18:15 +00:00
|
|
|
if topic == "" {
|
2022-07-24 20:51:42 +00:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
2023-09-11 14:24:05 +00:00
|
|
|
r.messagesMutex.Lock()
|
|
|
|
defer r.messagesMutex.Unlock()
|
2022-07-24 20:51:42 +00:00
|
|
|
|
2023-09-11 14:24:05 +00:00
|
|
|
if _, ok := r.messages[topic]; !ok {
|
2022-07-24 20:51:42 +00:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
2022-08-15 23:29:19 +00:00
|
|
|
_, err = w.Write([]byte("not subscribed to topic"))
|
2023-09-11 14:24:05 +00:00
|
|
|
r.log.Error("writing response", zap.Error(err))
|
2022-07-24 20:51:42 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-11 14:24:05 +00:00
|
|
|
response := r.messages[topic]
|
2023-03-04 17:26:47 +00:00
|
|
|
|
2023-09-11 14:24:05 +00:00
|
|
|
r.messages[topic] = []*pb.WakuMessage{}
|
2022-09-08 12:54:12 +00:00
|
|
|
writeErrOrResponse(w, nil, response)
|
2022-07-24 20:51:42 +00:00
|
|
|
}
|
|
|
|
|
2023-09-11 14:24:05 +00:00
|
|
|
func (r *RelayService) postV1Message(w http.ResponseWriter, req *http.Request) {
|
|
|
|
topic := chi.URLParam(req, "topic")
|
2023-02-16 20:18:15 +00:00
|
|
|
if topic == "" {
|
2022-07-24 20:51:42 +00:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var message *pb.WakuMessage
|
2023-09-11 14:24:05 +00:00
|
|
|
decoder := json.NewDecoder(req.Body)
|
2022-07-24 20:51:42 +00:00
|
|
|
if err := decoder.Decode(&message); err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2023-09-11 14:24:05 +00:00
|
|
|
defer req.Body.Close()
|
2022-07-24 20:51:42 +00:00
|
|
|
|
|
|
|
var err error
|
|
|
|
if topic == "" {
|
2023-09-07 18:00:59 +00:00
|
|
|
topic = relay.DefaultWakuTopic
|
2022-07-24 20:51:42 +00:00
|
|
|
}
|
|
|
|
|
2023-09-11 14:24:05 +00:00
|
|
|
if !r.node.Relay().IsSubscribed(topic) {
|
2023-09-07 18:00:59 +00:00
|
|
|
writeErrOrResponse(w, errors.New("not subscribed to pubsubTopic"), nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-11 14:24:05 +00:00
|
|
|
if err = server.AppendRLNProof(r.node, message); err != nil {
|
2023-09-07 18:00:59 +00:00
|
|
|
writeErrOrResponse(w, err, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-30 16:30:25 +00:00
|
|
|
_, err = r.node.Relay().Publish(req.Context(), message, relay.WithPubSubTopic(strings.Replace(topic, "\n", "", -1)))
|
2022-07-24 20:51:42 +00:00
|
|
|
if err != nil {
|
2023-09-11 14:24:05 +00:00
|
|
|
r.log.Error("publishing message", zap.Error(err))
|
2022-07-24 20:51:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
writeErrOrResponse(w, err, true)
|
|
|
|
}
|
2023-10-23 22:59:02 +00:00
|
|
|
|
|
|
|
func (r *RelayService) deleteV1AutoSubscriptions(w http.ResponseWriter, req *http.Request) {
|
|
|
|
var cTopics []string
|
|
|
|
decoder := json.NewDecoder(req.Body)
|
|
|
|
if err := decoder.Decode(&cTopics); err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer req.Body.Close()
|
|
|
|
|
|
|
|
err := r.node.Relay().Unsubscribe(req.Context(), protocol.NewContentFilter("", cTopics...))
|
|
|
|
if err != nil {
|
|
|
|
r.log.Error("unsubscribing from topics", zap.Strings("contentTopics", cTopics), zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
writeErrOrResponse(w, err, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RelayService) postV1AutoSubscriptions(w http.ResponseWriter, req *http.Request) {
|
|
|
|
var cTopics []string
|
|
|
|
decoder := json.NewDecoder(req.Body)
|
|
|
|
if err := decoder.Decode(&cTopics); err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer req.Body.Close()
|
|
|
|
|
|
|
|
var err error
|
|
|
|
_, err = r.node.Relay().Subscribe(r.node.Relay().Context(), protocol.NewContentFilter("", cTopics...))
|
|
|
|
if err != nil {
|
|
|
|
r.log.Error("subscribing to topics", zap.Strings("contentTopics", cTopics), zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
_, err := w.Write([]byte(err.Error()))
|
|
|
|
r.log.Error("writing response", zap.Error(err))
|
|
|
|
} else {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RelayService) getV1AutoMessages(w http.ResponseWriter, req *http.Request) {
|
|
|
|
cTopic := chi.URLParam(req, "contentTopic")
|
|
|
|
if cTopic == "" {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
_, err := w.Write([]byte("contentTopic is required"))
|
|
|
|
r.log.Error("writing response", zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cTopic, err := url.QueryUnescape(cTopic)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
_, err = w.Write([]byte("invalid contentTopic format"))
|
|
|
|
r.log.Error("writing response", zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sub, err := r.node.Relay().GetSubscription(cTopic)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
_, err = w.Write([]byte("not subscribed to topic"))
|
|
|
|
r.log.Error("writing response", zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var response []*pb.WakuMessage
|
|
|
|
select {
|
|
|
|
case msg := <-sub.Ch:
|
|
|
|
response = append(response, msg.Message())
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
writeErrOrResponse(w, nil, response)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RelayService) postV1AutoMessage(w http.ResponseWriter, req *http.Request) {
|
|
|
|
|
|
|
|
var message *pb.WakuMessage
|
|
|
|
decoder := json.NewDecoder(req.Body)
|
|
|
|
if err := decoder.Decode(&message); err != nil {
|
|
|
|
r.log.Error("decoding message failure", zap.Error(err))
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer req.Body.Close()
|
|
|
|
var err error
|
|
|
|
if err = server.AppendRLNProof(r.node, message); err != nil {
|
|
|
|
writeErrOrResponse(w, err, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = r.node.Relay().Publish(req.Context(), message)
|
|
|
|
if err != nil {
|
|
|
|
r.log.Error("publishing message", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
_, err := w.Write([]byte(err.Error()))
|
|
|
|
r.log.Error("writing response", zap.Error(err))
|
|
|
|
} else {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|