2017-02-18 22:10:22 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2023-03-11 21:34:27 +00:00
|
|
|
"encoding/base64"
|
2017-12-07 22:48:44 +00:00
|
|
|
"encoding/json"
|
2018-06-09 10:47:40 +00:00
|
|
|
"net/http"
|
2023-03-11 22:10:41 +00:00
|
|
|
"strings"
|
2018-06-09 10:47:40 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2023-03-11 17:14:49 +00:00
|
|
|
"github.com/olahol/melody"
|
2020-08-26 20:27:00 +00:00
|
|
|
|
2018-02-26 23:33:21 +00:00
|
|
|
"github.com/42wim/matterbridge/bridge"
|
2017-02-18 22:10:22 +00:00
|
|
|
"github.com/42wim/matterbridge/bridge/config"
|
2019-01-31 16:06:36 +00:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"github.com/labstack/echo/v4/middleware"
|
2023-03-11 21:34:27 +00:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2020-06-24 22:25:10 +00:00
|
|
|
ring "github.com/zfjagann/golang-ring"
|
2017-02-18 22:10:22 +00:00
|
|
|
)
|
|
|
|
|
2018-11-15 19:43:43 +00:00
|
|
|
type API struct {
|
2017-02-18 22:10:22 +00:00
|
|
|
Messages ring.Ring
|
|
|
|
sync.RWMutex
|
2018-03-04 22:52:14 +00:00
|
|
|
*bridge.Config
|
2020-08-26 20:27:00 +00:00
|
|
|
mrouter *melody.Melody
|
2017-02-18 22:10:22 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 19:43:43 +00:00
|
|
|
type Message struct {
|
2017-02-18 22:10:22 +00:00
|
|
|
Text string `json:"text"`
|
|
|
|
Username string `json:"username"`
|
2017-06-18 13:44:54 +00:00
|
|
|
UserID string `json:"userid"`
|
2017-02-18 22:10:22 +00:00
|
|
|
Avatar string `json:"avatar"`
|
2017-06-07 21:54:50 +00:00
|
|
|
Gateway string `json:"gateway"`
|
2017-02-18 22:10:22 +00:00
|
|
|
}
|
|
|
|
|
2018-03-04 22:52:14 +00:00
|
|
|
func New(cfg *bridge.Config) bridge.Bridger {
|
2018-11-15 19:43:43 +00:00
|
|
|
b := &API{Config: cfg}
|
2017-02-18 22:10:22 +00:00
|
|
|
e := echo.New()
|
2018-02-20 23:49:10 +00:00
|
|
|
e.HideBanner = true
|
|
|
|
e.HidePort = true
|
2020-08-26 20:27:00 +00:00
|
|
|
|
|
|
|
b.mrouter = melody.New()
|
|
|
|
b.mrouter.HandleMessage(func(s *melody.Session, msg []byte) {
|
|
|
|
message := config.Message{}
|
|
|
|
err := json.Unmarshal(msg, &message)
|
|
|
|
if err != nil {
|
|
|
|
b.Log.Errorf("failed to decode message from byte[] '%s'", string(msg))
|
|
|
|
return
|
|
|
|
}
|
2020-08-27 20:28:03 +00:00
|
|
|
b.handleWebsocketMessage(message, s)
|
2020-08-26 20:27:00 +00:00
|
|
|
})
|
|
|
|
b.mrouter.HandleConnect(func(session *melody.Session) {
|
|
|
|
greet := b.getGreeting()
|
|
|
|
data, err := json.Marshal(greet)
|
|
|
|
if err != nil {
|
|
|
|
b.Log.Errorf("failed to encode message '%v'", greet)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = session.Write(data)
|
|
|
|
if err != nil {
|
|
|
|
b.Log.Errorf("failed to write message '%s'", string(data))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// TODO: send message history buffer from `b.Messages` here
|
|
|
|
})
|
|
|
|
|
2017-02-18 22:10:22 +00:00
|
|
|
b.Messages = ring.Ring{}
|
2018-11-05 20:53:51 +00:00
|
|
|
if b.GetInt("Buffer") != 0 {
|
|
|
|
b.Messages.SetCapacity(b.GetInt("Buffer"))
|
|
|
|
}
|
2018-03-04 22:52:14 +00:00
|
|
|
if b.GetString("Token") != "" {
|
2017-06-05 22:05:32 +00:00
|
|
|
e.Use(middleware.KeyAuth(func(key string, c echo.Context) (bool, error) {
|
2018-03-04 22:52:14 +00:00
|
|
|
return key == b.GetString("Token"), nil
|
2017-06-05 22:05:32 +00:00
|
|
|
}))
|
|
|
|
}
|
2020-06-24 22:25:10 +00:00
|
|
|
|
|
|
|
// Set RemoteNickFormat to a sane default
|
|
|
|
if !b.IsKeySet("RemoteNickFormat") {
|
|
|
|
b.Log.Debugln("RemoteNickFormat is unset, defaulting to \"{NICK}\"")
|
|
|
|
b.Config.Config.Viper().Set(b.GetConfigKey("RemoteNickFormat"), "{NICK}")
|
|
|
|
}
|
|
|
|
|
2018-11-07 08:11:59 +00:00
|
|
|
e.GET("/api/health", b.handleHealthcheck)
|
2017-02-18 22:10:22 +00:00
|
|
|
e.GET("/api/messages", b.handleMessages)
|
2017-12-07 22:48:44 +00:00
|
|
|
e.GET("/api/stream", b.handleStream)
|
2020-07-12 19:13:28 +00:00
|
|
|
e.GET("/api/websocket", b.handleWebsocket)
|
2017-02-18 22:10:22 +00:00
|
|
|
e.POST("/api/message", b.handlePostMessage)
|
|
|
|
go func() {
|
2018-03-04 22:52:14 +00:00
|
|
|
if b.GetString("BindAddress") == "" {
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Fatalf("No BindAddress configured.")
|
2018-02-20 23:49:10 +00:00
|
|
|
}
|
2018-03-04 22:52:14 +00:00
|
|
|
b.Log.Infof("Listening on %s", b.GetString("BindAddress"))
|
|
|
|
b.Log.Fatal(e.Start(b.GetString("BindAddress")))
|
2017-02-18 22:10:22 +00:00
|
|
|
}()
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2018-11-15 19:43:43 +00:00
|
|
|
func (b *API) Connect() error {
|
2017-02-18 22:10:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-08-26 20:27:00 +00:00
|
|
|
|
2018-11-15 19:43:43 +00:00
|
|
|
func (b *API) Disconnect() error {
|
2017-02-18 22:10:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-08-26 20:27:00 +00:00
|
|
|
|
2018-11-15 19:43:43 +00:00
|
|
|
func (b *API) JoinChannel(channel config.ChannelInfo) error {
|
2017-02-18 22:10:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-15 19:43:43 +00:00
|
|
|
func (b *API) Send(msg config.Message) (string, error) {
|
2017-02-18 22:10:22 +00:00
|
|
|
b.Lock()
|
|
|
|
defer b.Unlock()
|
2017-09-11 20:45:15 +00:00
|
|
|
// ignore delete messages
|
2018-11-15 19:43:43 +00:00
|
|
|
if msg.Event == config.EventMsgDelete {
|
2017-09-11 20:45:15 +00:00
|
|
|
return "", nil
|
|
|
|
}
|
2020-08-26 20:27:00 +00:00
|
|
|
b.Log.Debugf("enqueueing message from %s on ring buffer", msg.Username)
|
|
|
|
b.Messages.Enqueue(msg)
|
|
|
|
|
|
|
|
data, err := json.Marshal(msg)
|
|
|
|
if err != nil {
|
|
|
|
b.Log.Errorf("failed to encode message '%s'", msg)
|
|
|
|
}
|
|
|
|
_ = b.mrouter.Broadcast(data)
|
2017-08-27 20:59:37 +00:00
|
|
|
return "", nil
|
2017-02-18 22:10:22 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 19:43:43 +00:00
|
|
|
func (b *API) handleHealthcheck(c echo.Context) error {
|
2018-11-07 08:11:59 +00:00
|
|
|
return c.String(http.StatusOK, "OK")
|
|
|
|
}
|
|
|
|
|
2018-11-15 19:43:43 +00:00
|
|
|
func (b *API) handlePostMessage(c echo.Context) error {
|
2018-02-20 17:36:44 +00:00
|
|
|
message := config.Message{}
|
|
|
|
if err := c.Bind(&message); err != nil {
|
2017-02-18 22:10:22 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-02-20 17:36:44 +00:00
|
|
|
// these values are fixed
|
|
|
|
message.Channel = "api"
|
|
|
|
message.Protocol = "api"
|
|
|
|
message.Account = b.Account
|
|
|
|
message.ID = ""
|
|
|
|
message.Timestamp = time.Now()
|
2023-03-11 22:10:41 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
fm map[string]interface{}
|
|
|
|
ds string
|
|
|
|
ok bool
|
|
|
|
)
|
|
|
|
|
2023-03-11 21:34:27 +00:00
|
|
|
for i, f := range message.Extra["file"] {
|
|
|
|
fi := config.FileInfo{}
|
2023-03-11 22:10:41 +00:00
|
|
|
if fm, ok = f.(map[string]interface{}); !ok {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "invalid format for extra")
|
|
|
|
}
|
|
|
|
err := mapstructure.Decode(fm, &fi)
|
|
|
|
if err != nil {
|
|
|
|
if !strings.Contains(err.Error(), "got string") {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-03-11 21:34:27 +00:00
|
|
|
// mapstructure doesn't decode base64 into []byte, so it must be done manually for fi.Data
|
2023-03-11 22:10:41 +00:00
|
|
|
if ds, ok = fm["Data"].(string); !ok {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "invalid format for data")
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := base64.StdEncoding.DecodeString(ds)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-11 21:34:27 +00:00
|
|
|
fi.Data = &data
|
|
|
|
message.Extra["file"][i] = fi
|
|
|
|
}
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Debugf("Sending message from %s on %s to gateway", message.Username, "api")
|
2018-02-20 17:36:44 +00:00
|
|
|
b.Remote <- message
|
2017-02-18 22:10:22 +00:00
|
|
|
return c.JSON(http.StatusOK, message)
|
|
|
|
}
|
|
|
|
|
2018-11-15 19:43:43 +00:00
|
|
|
func (b *API) handleMessages(c echo.Context) error {
|
2017-02-18 22:10:22 +00:00
|
|
|
b.Lock()
|
|
|
|
defer b.Unlock()
|
2017-06-05 21:08:36 +00:00
|
|
|
c.JSONPretty(http.StatusOK, b.Messages.Values(), " ")
|
2017-02-18 22:10:22 +00:00
|
|
|
b.Messages = ring.Ring{}
|
|
|
|
return nil
|
|
|
|
}
|
2017-12-07 22:48:44 +00:00
|
|
|
|
2020-07-12 19:13:28 +00:00
|
|
|
func (b *API) getGreeting() config.Message {
|
|
|
|
return config.Message{
|
2018-11-15 19:43:43 +00:00
|
|
|
Event: config.EventAPIConnected,
|
2018-11-07 19:36:50 +00:00
|
|
|
Timestamp: time.Now(),
|
2018-11-02 15:35:13 +00:00
|
|
|
}
|
2020-07-12 19:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *API) handleStream(c echo.Context) error {
|
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
|
|
|
|
c.Response().WriteHeader(http.StatusOK)
|
|
|
|
greet := b.getGreeting()
|
2018-11-02 15:35:13 +00:00
|
|
|
if err := json.NewEncoder(c.Response()).Encode(greet); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.Response().Flush()
|
2017-12-07 22:48:44 +00:00
|
|
|
for {
|
2023-03-11 17:14:49 +00:00
|
|
|
select {
|
2020-08-26 20:27:00 +00:00
|
|
|
// TODO: this causes issues, messages should be broadcasted to all connected clients
|
2023-03-11 17:14:49 +00:00
|
|
|
default:
|
|
|
|
msg := b.Messages.Dequeue()
|
|
|
|
if msg != nil {
|
|
|
|
if err := json.NewEncoder(c.Response()).Encode(msg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.Response().Flush()
|
2017-12-07 22:48:44 +00:00
|
|
|
}
|
2023-03-11 17:14:49 +00:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
case <-c.Request().Context().Done():
|
|
|
|
return nil
|
2017-12-07 22:48:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-12 19:13:28 +00:00
|
|
|
|
2020-08-27 20:28:03 +00:00
|
|
|
func (b *API) handleWebsocketMessage(message config.Message, s *melody.Session) {
|
2020-07-12 19:13:28 +00:00
|
|
|
message.Channel = "api"
|
|
|
|
message.Protocol = "api"
|
|
|
|
message.Account = b.Account
|
|
|
|
message.ID = ""
|
|
|
|
message.Timestamp = time.Now()
|
|
|
|
|
2020-08-27 20:28:03 +00:00
|
|
|
data, err := json.Marshal(message)
|
|
|
|
if err != nil {
|
|
|
|
b.Log.Errorf("failed to encode message for loopback '%v'", message)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = b.mrouter.BroadcastOthers(data, s)
|
|
|
|
|
2020-07-12 19:13:28 +00:00
|
|
|
b.Log.Debugf("Sending websocket message from %s on %s to gateway", message.Username, "api")
|
|
|
|
b.Remote <- message
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *API) handleWebsocket(c echo.Context) error {
|
2020-08-26 20:27:00 +00:00
|
|
|
err := b.mrouter.HandleRequest(c.Response(), c.Request())
|
2020-07-12 19:13:28 +00:00
|
|
|
if err != nil {
|
2020-08-26 20:27:00 +00:00
|
|
|
b.Log.Errorf("error in websocket handling '%v'", err)
|
2020-07-12 19:13:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|