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"
|
2020-06-25 01:20:58 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
2019-11-16 16:06:34 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
2020-06-25 14:54:04 +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"`
|
2021-01-08 18:32:08 +01:00
|
|
|
ManifestHash string `json:"manifest"`
|
2020-01-22 16:54:01 +01:00
|
|
|
Version string `json:"version"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var wsAuth = struct {
|
2020-10-03 13:50:30 +02:00
|
|
|
user map[string]entity.User
|
2020-06-25 01:20:58 +02:00
|
|
|
mutex sync.RWMutex
|
2020-10-03 13:50:30 +02:00
|
|
|
}{user: make(map[string]entity.User)}
|
2020-01-22 16:54:01 +01:00
|
|
|
|
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
|
|
|
|
|
|
|
var info clientInfo
|
|
|
|
|
|
|
|
if err := json.Unmarshal(m, &info); err != nil {
|
2020-06-29 11:10:24 +02:00
|
|
|
// Do nothing.
|
2020-01-22 16:54:01 +01:00
|
|
|
} else {
|
2020-06-25 14:54:04 +02:00
|
|
|
if sess := Session(info.SessionToken); sess.Valid() {
|
2020-01-22 16:54:01 +01:00
|
|
|
wsAuth.mutex.Lock()
|
2020-06-25 01:20:58 +02:00
|
|
|
wsAuth.user[connId] = sess.User
|
2020-01-22 16:54:01 +01:00
|
|
|
wsAuth.mutex.Unlock()
|
2020-01-23 07:39:04 +01:00
|
|
|
|
2020-06-29 11:10:24 +02:00
|
|
|
var clientConfig config.ClientConfig
|
2020-01-23 07:39:04 +01:00
|
|
|
|
2020-06-25 01:20:58 +02:00
|
|
|
if sess.User.Guest() {
|
2020-06-29 11:10:24 +02:00
|
|
|
clientConfig = conf.GuestConfig()
|
2020-06-25 14:54:04 +02:00
|
|
|
} else if sess.User.Registered() {
|
2020-06-29 11:10:24 +02:00
|
|
|
clientConfig = conf.UserConfig()
|
2020-06-25 01:20:58 +02:00
|
|
|
} else {
|
2020-06-29 11:10:24 +02:00
|
|
|
clientConfig = conf.PublicConfig()
|
|
|
|
}
|
|
|
|
|
|
|
|
writeMutex.Lock()
|
|
|
|
ws.SetWriteDeadline(time.Now().Add(30 * time.Second))
|
|
|
|
|
|
|
|
if err := ws.WriteJSON(gin.H{"event": "config.updated", "data": event.Data{"config": clientConfig}}); err != nil {
|
|
|
|
// Do nothing.
|
2020-01-23 07:39:04 +01:00
|
|
|
}
|
2020-06-25 01:20:58 +02:00
|
|
|
|
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()
|
2020-10-03 13:50:30 +02:00
|
|
|
wsAuth.user[connId] = entity.UnknownUser
|
2020-01-22 16:54:01 +01:00
|
|
|
wsAuth.mutex.Unlock()
|
2019-11-16 16:06:34 +01:00
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-pingTicker.C:
|
2020-06-29 11:10:24 +02:00
|
|
|
writeMutex.Lock()
|
2020-01-23 07:39:04 +01:00
|
|
|
ws.SetWriteDeadline(time.Now().Add(30 * time.Second))
|
2020-06-29 11:10:24 +02:00
|
|
|
|
2019-11-16 16:06:34 +01:00
|
|
|
if err := ws.WriteMessage(websocket.PingMessage, []byte{}); err != nil {
|
2020-06-29 11:10:24 +02:00
|
|
|
writeMutex.Unlock()
|
2019-11-16 16:06:34 +01:00
|
|
|
return
|
|
|
|
}
|
2020-06-29 11:10:24 +02:00
|
|
|
|
|
|
|
writeMutex.Unlock()
|
2019-11-16 16:06:34 +01:00
|
|
|
case msg := <-s.Receiver:
|
2020-01-22 16:54:01 +01:00
|
|
|
wsAuth.mutex.RLock()
|
2020-06-29 11:10:24 +02:00
|
|
|
|
2020-10-03 13:50:30 +02:00
|
|
|
user := entity.UnknownUser
|
2020-06-29 11:10:24 +02:00
|
|
|
|
|
|
|
if hit, ok := wsAuth.user[connId]; ok {
|
|
|
|
user = hit
|
|
|
|
}
|
|
|
|
|
2020-01-22 16:54:01 +01:00
|
|
|
wsAuth.mutex.RUnlock()
|
2019-11-16 16:06:34 +01:00
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
if user.Registered() {
|
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
|
|
|
return
|
|
|
|
}
|
2020-06-29 11:10:24 +02:00
|
|
|
|
2020-02-21 01:58:40 +01:00
|
|
|
writeMutex.Unlock()
|
2019-11-16 16:06:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GET /api/v1/ws
|
2020-06-25 14:54:04 +02:00
|
|
|
func Websocket(router *gin.RouterGroup) {
|
2020-01-22 16:54:01 +01:00
|
|
|
if router == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
conf := service.Config()
|
|
|
|
|
2020-01-22 16:54:01 +01:00
|
|
|
if conf == 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)
|
2020-06-29 11:10:24 +02:00
|
|
|
|
2019-11-16 16:06:34 +01:00
|
|
|
if err != nil {
|
|
|
|
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()
|
|
|
|
|
2020-06-25 01:20:58 +02:00
|
|
|
// Init connection.
|
|
|
|
wsAuth.mutex.Lock()
|
2020-01-22 16:54:01 +01:00
|
|
|
if conf.Public() {
|
2020-06-25 01:20:58 +02:00
|
|
|
wsAuth.user[connId] = entity.Admin
|
|
|
|
} else {
|
2020-10-03 13:50:30 +02:00
|
|
|
wsAuth.user[connId] = entity.UnknownUser
|
2020-01-22 16:54:01 +01:00
|
|
|
}
|
2020-06-25 01:20:58 +02:00
|
|
|
wsAuth.mutex.Unlock()
|
2020-01-22 16:54:01 +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
|
|
|
})
|
|
|
|
}
|