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-04-20 13:50:28 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/service"
|
2020-01-22 16:54:01 +01:00
|
|
|
"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-02-21 01:58:40 +01:00
|
|
|
func wsReader(ws *websocket.Conn, writeMutex *sync.Mutex, 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 {
|
2020-04-20 13:50:28 +02:00
|
|
|
if service.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
|
|
|
|
2020-02-21 01:58:40 +01:00
|
|
|
writeMutex.Lock()
|
2020-01-23 07:39:04 +01:00
|
|
|
ws.SetWriteDeadline(time.Now().Add(30 * time.Second))
|
|
|
|
|
2020-05-27 19:38:40 +02:00
|
|
|
if err := ws.WriteJSON(gin.H{"event": "config.updated", "data": event.Data{"config": conf.ClientConfig()}}); err != nil {
|
2020-01-23 07:39:04 +01:00
|
|
|
log.Error(err)
|
|
|
|
}
|
2020-02-21 01:58:40 +01:00
|
|
|
writeMutex.Unlock()
|
2020-01-22 16:54:01 +01:00
|
|
|
}
|
|
|
|
}
|
2019-11-16 16:06:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:58:40 +01:00
|
|
|
func wsWriter(ws *websocket.Conn, writeMutex *sync.Mutex, connId string) {
|
2020-01-23 07:39:04 +01:00
|
|
|
pingTicker := time.NewTicker(15 * time.Second)
|
2020-05-29 18:04:30 +02:00
|
|
|
s := event.Subscribe(
|
|
|
|
"log.*",
|
|
|
|
"notify.*",
|
|
|
|
"index.*",
|
|
|
|
"upload.*",
|
|
|
|
"import.*",
|
|
|
|
"config.*",
|
|
|
|
"count.*",
|
|
|
|
"photos.*",
|
|
|
|
"cameras.*",
|
|
|
|
"lenses.*",
|
|
|
|
"countries.*",
|
|
|
|
"albums.*",
|
|
|
|
"labels.*",
|
|
|
|
"sync.*",
|
|
|
|
)
|
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-02-21 01:58:40 +01:00
|
|
|
writeMutex.Lock()
|
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 {
|
2020-02-21 01:58:40 +01:00
|
|
|
writeMutex.Unlock()
|
2020-01-22 16:54:01 +01:00
|
|
|
log.Debug(err)
|
|
|
|
return
|
|
|
|
}
|
2020-02-21 01:58:40 +01:00
|
|
|
writeMutex.Unlock()
|
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
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:58:40 +01:00
|
|
|
var writeMutex sync.Mutex
|
|
|
|
|
2019-11-16 16:06:34 +01:00
|
|
|
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-02-21 01:58:40 +01:00
|
|
|
go wsWriter(ws, &writeMutex, connId)
|
2019-11-16 16:06:34 +01:00
|
|
|
|
2020-02-21 01:58:40 +01:00
|
|
|
wsReader(ws, &writeMutex, connId, conf)
|
2019-11-16 16:06:34 +01:00
|
|
|
})
|
|
|
|
}
|