2019-11-16 16:06:34 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2020-01-22 16:54:01 +01:00
|
|
|
"encoding/json"
|
2020-01-20 20:47:11 +01:00
|
|
|
"net/http"
|
2020-01-22 16:54:01 +01:00
|
|
|
"sync"
|
2019-11-16 16:06:34 +01:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
2020-01-22 16:54:01 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/session"
|
|
|
|
"github.com/photoprism/photoprism/pkg/rnd"
|
2019-11-16 16:06:34 +01:00
|
|
|
)
|
|
|
|
|
2020-01-20 20:47:11 +01:00
|
|
|
var wsConnection = websocket.Upgrader{
|
|
|
|
CheckOrigin: func(r *http.Request) bool {
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
}
|
2020-01-23 07:39:04 +01:00
|
|
|
var wsTimeout = 90 * time.Second
|
2019-11-16 16:06:34 +01:00
|
|
|
|
2020-01-22 16:54:01 +01:00
|
|
|
type clientInfo struct {
|
|
|
|
SessionToken string `json:"session"`
|
|
|
|
JsHash string `json:"js"`
|
|
|
|
CssHash string `json:"css"`
|
|
|
|
Version string `json:"version"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var wsAuth = struct {
|
|
|
|
authenticated map[string]bool
|
|
|
|
mutex sync.RWMutex
|
|
|
|
}{authenticated: make(map[string]bool)}
|
|
|
|
|
2020-01-23 07:39:04 +01:00
|
|
|
func wsReader(ws *websocket.Conn, connId string, conf *config.Config) {
|
2019-11-16 16:06:34 +01:00
|
|
|
defer ws.Close()
|
|
|
|
|
|
|
|
ws.SetReadLimit(512)
|
|
|
|
ws.SetReadDeadline(time.Now().Add(wsTimeout))
|
|
|
|
ws.SetPongHandler(func(string) error { ws.SetReadDeadline(time.Now().Add(wsTimeout)); return nil })
|
|
|
|
|
|
|
|
for {
|
|
|
|
_, m, err := ws.ReadMessage()
|
2020-01-22 16:54:01 +01:00
|
|
|
|
2019-11-16 16:06:34 +01:00
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
2020-01-22 16:54:01 +01:00
|
|
|
|
2019-12-03 12:51:23 +01:00
|
|
|
log.Debugf("websocket: received %d bytes", len(m))
|
2020-01-22 16:54:01 +01:00
|
|
|
|
|
|
|
var info clientInfo
|
|
|
|
|
|
|
|
if err := json.Unmarshal(m, &info); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
} else {
|
|
|
|
if session.Exists(info.SessionToken) {
|
2020-01-23 07:39:04 +01:00
|
|
|
log.Debug("websocket: authenticated")
|
|
|
|
|
2020-01-22 16:54:01 +01:00
|
|
|
wsAuth.mutex.Lock()
|
|
|
|
wsAuth.authenticated[connId] = true
|
|
|
|
wsAuth.mutex.Unlock()
|
2020-01-23 07:39:04 +01:00
|
|
|
|
|
|
|
ws.SetWriteDeadline(time.Now().Add(30 * time.Second))
|
|
|
|
|
|
|
|
if err := ws.WriteJSON(gin.H{"event": "config.updated", "data": event.Data(conf.ClientConfig())}); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
2020-01-22 16:54:01 +01:00
|
|
|
}
|
|
|
|
}
|
2019-11-16 16:06:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-22 16:54:01 +01:00
|
|
|
func wsWriter(ws *websocket.Conn, connId string) {
|
2020-01-23 07:39:04 +01:00
|
|
|
pingTicker := time.NewTicker(15 * time.Second)
|
2020-01-30 18:19:26 +01:00
|
|
|
s := event.Subscribe("log.*", "notify.*", "index.*", "upload.*", "import.*", "config.*", "count.*", "photos.*", "albums.*", "labels.*")
|
2019-11-16 16:06:34 +01:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
pingTicker.Stop()
|
|
|
|
event.Unsubscribe(s)
|
|
|
|
ws.Close()
|
2020-01-22 16:54:01 +01:00
|
|
|
|
|
|
|
wsAuth.mutex.Lock()
|
|
|
|
wsAuth.authenticated[connId] = false
|
|
|
|
wsAuth.mutex.Unlock()
|
2019-11-16 16:06:34 +01:00
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-pingTicker.C:
|
2020-01-23 07:39:04 +01:00
|
|
|
ws.SetWriteDeadline(time.Now().Add(30 * time.Second))
|
2019-11-16 16:06:34 +01:00
|
|
|
if err := ws.WriteMessage(websocket.PingMessage, []byte{}); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case msg := <-s.Receiver:
|
2020-01-22 16:54:01 +01:00
|
|
|
wsAuth.mutex.RLock()
|
|
|
|
auth := wsAuth.authenticated[connId]
|
|
|
|
wsAuth.mutex.RUnlock()
|
2019-11-16 16:06:34 +01:00
|
|
|
|
2020-01-22 16:54:01 +01:00
|
|
|
if auth {
|
2020-01-23 07:39:04 +01:00
|
|
|
ws.SetWriteDeadline(time.Now().Add(30 * time.Second))
|
2020-01-22 16:54:01 +01:00
|
|
|
|
|
|
|
if err := ws.WriteJSON(gin.H{"event": msg.Name, "data": msg.Fields}); err != nil {
|
|
|
|
log.Debug(err)
|
|
|
|
return
|
|
|
|
}
|
2019-11-16 16:06:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GET /api/v1/ws
|
|
|
|
func Websocket(router *gin.RouterGroup, conf *config.Config) {
|
2020-01-22 16:54:01 +01:00
|
|
|
if router == nil {
|
|
|
|
log.Error("websocket: router is nil")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if conf == nil {
|
|
|
|
log.Error("websocket: conf is nil")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-16 16:06:34 +01:00
|
|
|
router.GET("/ws", func(c *gin.Context) {
|
|
|
|
w := c.Writer
|
|
|
|
r := c.Request
|
|
|
|
|
|
|
|
ws, err := wsConnection.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
2019-12-03 12:51:23 +01:00
|
|
|
log.Error(err)
|
2019-11-16 16:06:34 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer ws.Close()
|
|
|
|
|
2020-01-22 16:54:01 +01:00
|
|
|
connId := rnd.UUID()
|
|
|
|
|
|
|
|
if conf.Public() {
|
|
|
|
wsAuth.mutex.Lock()
|
|
|
|
wsAuth.authenticated[connId] = true
|
|
|
|
wsAuth.mutex.Unlock()
|
|
|
|
}
|
|
|
|
|
2019-12-03 12:51:23 +01:00
|
|
|
log.Debug("websocket: connected")
|
2019-11-16 16:06:34 +01:00
|
|
|
|
2020-01-22 16:54:01 +01:00
|
|
|
go wsWriter(ws, connId)
|
2019-11-16 16:06:34 +01:00
|
|
|
|
2020-01-23 07:39:04 +01:00
|
|
|
wsReader(ws, connId, conf)
|
2019-11-16 16:06:34 +01:00
|
|
|
})
|
|
|
|
}
|