2024-03-19 00:31:35 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-06-10 07:10:09 +00:00
|
|
|
"encoding/json"
|
2024-03-19 00:31:35 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
|
2024-06-10 07:10:09 +00:00
|
|
|
"github.com/status-im/status-go/protocol/common"
|
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
|
|
|
msignal "github.com/status-im/status-go/signal"
|
|
|
|
|
2024-03-19 00:31:35 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2024-06-14 13:13:31 +00:00
|
|
|
func serve(cCtx *cli.Context) error {
|
2024-03-19 00:31:35 +00:00
|
|
|
name := cCtx.String(NameFlag)
|
|
|
|
port := cCtx.Int(PortFlag)
|
2024-04-09 15:44:43 +00:00
|
|
|
apiModules := cCtx.String(APIModulesFlag)
|
2024-05-22 22:01:54 +00:00
|
|
|
telemetryUrl := cCtx.String(TelemetryServerURLFlag)
|
2024-06-10 10:21:09 +00:00
|
|
|
interactive := cCtx.Bool(InteractiveFlag)
|
|
|
|
dest := cCtx.String(AddFlag)
|
2024-06-12 08:00:51 +00:00
|
|
|
keyUID := cCtx.String(KeyUIDFlag)
|
2024-06-14 13:13:31 +00:00
|
|
|
isDebugLevel := cCtx.Bool(DebugLevel)
|
|
|
|
cmdName := cCtx.Command.Name
|
|
|
|
|
|
|
|
logger, err := getSLogger(isDebugLevel)
|
|
|
|
if err != nil {
|
|
|
|
zap.S().Fatalf("Error initializing logger: %v", err)
|
|
|
|
}
|
|
|
|
logger.Infof("Running %v command, with:\n%v", cmdName, flagsUsed(cCtx))
|
|
|
|
|
|
|
|
logger = logger.Named(name)
|
2024-03-19 00:31:35 +00:00
|
|
|
|
2024-06-14 13:13:31 +00:00
|
|
|
cli, err := start(name, port, apiModules, telemetryUrl, keyUID, logger)
|
2024-03-19 00:31:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-09 15:44:43 +00:00
|
|
|
defer cli.stop()
|
2024-03-19 00:31:35 +00:00
|
|
|
|
2024-06-10 07:10:09 +00:00
|
|
|
// Using the mobile signal handler to listen for received messages
|
|
|
|
// because if we call messenger.RetrieveAll() from different routines we will miss messages in one of them
|
|
|
|
// and the retrieve messages loop is started when starting a node, so we needed a different appproach,
|
|
|
|
// alternatively we could have implemented another notification mechanism in the messenger, but this signal is already in place
|
|
|
|
msignal.SetMobileSignalHandler(msignal.MobileSignalHandler(func(s []byte) {
|
2024-06-14 13:13:31 +00:00
|
|
|
var evt EventType
|
|
|
|
if err := json.Unmarshal(s, &evt); err != nil {
|
|
|
|
logger.Error("unmarshaling event type", zap.Error(err), zap.String("event", string(s)))
|
2024-06-10 07:10:09 +00:00
|
|
|
return
|
|
|
|
}
|
2024-03-19 00:31:35 +00:00
|
|
|
|
2024-06-14 13:13:31 +00:00
|
|
|
switch evt.Type {
|
|
|
|
case msignal.EventNewMessages:
|
|
|
|
var ev EventNewMessages
|
|
|
|
if err := json.Unmarshal(evt.Event, &ev); err != nil {
|
|
|
|
logger.Error("unmarshaling new message event", zap.Error(err), zap.Any("event", evt.Event))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, message := range ev.Messages {
|
2024-06-10 07:10:09 +00:00
|
|
|
logger.Infof("message received: %v (ID=%v)", message.Text, message.ID)
|
|
|
|
// if request contact, accept it
|
|
|
|
if message.ContentType == protobuf.ChatMessage_SYSTEM_MESSAGE_MUTUAL_EVENT_SENT {
|
2024-06-10 10:21:09 +00:00
|
|
|
if err := cli.sendContactRequestAcceptance(cCtx.Context, message.ID); err != nil {
|
2024-06-10 07:10:09 +00:00
|
|
|
logger.Errorf("accepting contact request: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-06-14 13:13:31 +00:00
|
|
|
default:
|
|
|
|
logger.Debugf("received event type '%v'\t%v", evt.Type, string(evt.Event))
|
2024-06-10 07:10:09 +00:00
|
|
|
}
|
|
|
|
}))
|
2024-03-19 00:31:35 +00:00
|
|
|
|
2024-06-10 07:10:09 +00:00
|
|
|
// Send contact request
|
2024-03-19 00:31:35 +00:00
|
|
|
if dest != "" {
|
2024-06-10 07:10:09 +00:00
|
|
|
err := cli.sendContactRequest(cCtx.Context, dest)
|
2024-03-19 00:31:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-10 07:10:09 +00:00
|
|
|
// nightly testrunner looks for this log to consider node as started
|
|
|
|
logger.Info("retrieve messages...")
|
|
|
|
|
2024-06-10 10:21:09 +00:00
|
|
|
if interactive {
|
|
|
|
ctx, cancel := context.WithCancel(cCtx.Context)
|
|
|
|
go func() {
|
|
|
|
waitForSigExit()
|
|
|
|
cancel()
|
|
|
|
}()
|
|
|
|
interactiveSendMessageLoop(ctx, cli)
|
|
|
|
} else {
|
|
|
|
waitForSigExit()
|
|
|
|
}
|
2024-03-19 00:31:35 +00:00
|
|
|
|
|
|
|
logger.Info("Exiting")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-06-10 07:10:09 +00:00
|
|
|
|
2024-06-14 13:13:31 +00:00
|
|
|
type EventType struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
Event json.RawMessage `json:"event"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type EventNewMessages struct {
|
|
|
|
Messages []*common.Message `json:"messages"`
|
2024-06-10 07:10:09 +00:00
|
|
|
}
|
2024-06-10 10:21:09 +00:00
|
|
|
|
|
|
|
func waitForSigExit() {
|
|
|
|
sig := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
<-sig
|
|
|
|
}
|