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
|
|
|
"log"
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
func serve(cCtx *cli.Context) error {
|
|
|
|
rawLogger, err := zap.NewDevelopment()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error initializing logger: %v", err)
|
|
|
|
}
|
|
|
|
logger = rawLogger.Sugar()
|
|
|
|
|
|
|
|
logger.Info("Running serve command, flags passed:")
|
|
|
|
for _, flag := range ServeFlags {
|
|
|
|
logger.Infof("-%s %v", flag.Names()[0], cCtx.Value(flag.Names()[0]))
|
|
|
|
}
|
|
|
|
|
|
|
|
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-03-19 00:31:35 +00:00
|
|
|
|
2024-06-10 07:10:09 +00:00
|
|
|
cli, err := start(name, port, apiModules, telemetryUrl)
|
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) {
|
|
|
|
var ev MobileSignalEvent
|
|
|
|
if err := json.Unmarshal(s, &ev); err != nil {
|
|
|
|
logger.Errorf("unmarshaling signal event: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2024-03-19 00:31:35 +00:00
|
|
|
|
2024-06-10 07:10:09 +00:00
|
|
|
if ev.Type == msignal.EventNewMessages {
|
|
|
|
for _, message := range ev.Event.Messages {
|
|
|
|
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-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
|
|
|
|
|
|
|
type MobileSignalEvent struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
Event struct {
|
|
|
|
Messages []*common.Message `json:"messages"`
|
|
|
|
} `json:"event"`
|
|
|
|
}
|
2024-06-10 10:21:09 +00:00
|
|
|
|
|
|
|
func waitForSigExit() {
|
|
|
|
sig := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
<-sig
|
|
|
|
}
|