Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

19.9 KB
Raw Blame
// SiYuan - https://github.com/siyuan-note/siyuan
// Copyright (c) 2020-present, b3log.org
// SPDX-License-Identifier: AGPL-3.0-or-later

package util

import (
	"errors"
	"net/http"
	"strings"
	"sync"
	"sync/atomic"
	"time"

	"github.com/88250/gulu"
	"github.com/gorilla/websocket"
	"github.com/olahol/melody"
	"github.com/siyuan-note/eventbus"
	"github.com/siyuan-note/logging"
)

// LogWebSocketError 忽略正常关闭,记录消息丢弃警告,其他连接错误保留在调试日志中。
func LogWebSocketError(session *melody.Session, channel string, err error) {
	var closeErr *websocket.CloseError
	if errors.As(err, &closeErr) && (closeErr.Code == websocket.CloseNormalClosure || closeErr.Code == websocket.CloseGoingAway) {
		return
	}
	app, _ := session.Get("app")
	id, _ := session.Get("id")
	typ, _ := session.Get("type")
	if errors.Is(err, melody.ErrMessageBufferFull) {
		logging.LogWarnf("websocket message dropped [channel=%q, app=%v, id=%v, type=%v]: %s", channel, app, id, typ, err)
		return
	}
	logging.LogDebugf("websocket failed [channel=%q, app=%v, id=%v, type=%v]: %s", channel, app, id, typ, err)
}

var (
	WebSocketServer *melody.Melody

	// map[string]map[string]*melody.Session{}
	sessions     = sync.Map{} // {appId, {sessionId, session}}
	authSessions = sync.Map{}
	// 注册与清理必须串行,避免最后一个旧连接断开时删掉并发注册的新连接。
	pushChanLock sync.Mutex

	// ReloadDocInfoGuard 由 model 层注入,在广播 docInfo 前检查 box 是否仍处于可广播状态。
	// 加密笔记本锁定后返回 false,防止 500ms 延迟任务在锁定后泄漏明文元数据。
	ReloadDocInfoGuard func(boxID string) bool
)

func BroadcastByTypeAndExcludeApp(excludeApp, typ, cmd string, code int, msg string, data any) {
	payload := broadcastPayload(cmd, code, msg, data)
	sessions.Range(func(key, value any) bool {
		appSessions := value.(*sync.Map)
		if key == excludeApp {
			return true
		}

		appSessions.Range(func(key, value any) bool {
			session := value.(*melody.Session)
			if isPublishSession(session) {
				return true
			}
			if t, ok := session.Get("type"); ok && typ == t {
				session.Write(payload())
			}
			return true
		})
		return true
	})
}

func BroadcastByTypeAndApp(typ, app, cmd string, code int, msg string, data any) {
	appSessions, ok := sessions.Load(app)
	if !ok {
		return
	}
	payload := broadcastPayload(cmd, code, msg, data)

	appSessions.(*sync.Map).Range(func(key, value any) bool {
		session := value.(*melody.Session)
		if isPublishSession(session) {
			return true
		}
		if t, ok := session.Get("type"); ok && typ == t {
			session.Write(payload())
		}
		return true
	})
}

// BroadcastByType 广播所有实例上 typ 类型的会话。
func BroadcastByType(typ, cmd string, code int, msg string, data any) {
	typeSessions := SessionsByType(typ)
	payload := broadcastPayload(cmd, code, msg, data)
	for _, sess := range typeSessions {
		sess.Write(payload())
	}
}

// broadcastPayload 仅在找到目标会话时序列化,同一次广播共享结果。
func broadcastPayload(cmd string, code int, msg string, data any) func() []byte {
	return sync.OnceValue(func() []byte {
		event := NewResult()
		event.Cmd, event.Code, event.Msg, event.Data = cmd, code, msg, data
		return event.Bytes()
	})
}

func SessionsByType(typ string) (ret []*melody.Session) {
	return sessionsByType(typ, false)
}

// IsSessionReady 未声明就绪协议的旧客户端沿用连接后即可接收控制消息的行为。
func IsSessionReady(session *melody.Session) bool {
	ready, exists := session.Get("uiReady")
	return !exists || ready == true
}

func publishSessionsByType(typ string) (ret []*melody.Session) {
	return sessionsByType(typ, true)
}

func sessionsByType(typ string, publish bool) (ret []*melody.Session) {
	ret = []*melody.Session{}

	sessions.Range(func(key, value any) bool {
		appSessions := value.(*sync.Map)
		appSessions.Range(func(key, value any) bool {
			session := value.(*melody.Session)
			if isPublishSession(session) != publish {
				return true
			}
			if t, ok := session.Get("type"); ok && typ == t {
				ret = append(ret, session)
			}
			return true
		})
		return true
	})
	return
}

func isPublishSession(session *melody.Session) bool {
	isPublish, ok := session.Get("isPublish")
	return ok && isPublish == true
}

func AddPushChan(session *melody.Session) {
	appID := strings.TrimSpace(session.Request.URL.Query().Get("app"))
	if "" == appID {
		logging.LogErrorf("app id is required")
		return
	}
	session.Set("app", appID)

	id := strings.TrimSpace(session.Request.URL.Query().Get("id"))
	if "" == id {
		logging.LogErrorf("id is required")
		return
	}
	session.Set("id", id)

	typ := strings.TrimSpace(session.Request.URL.Query().Get("type"))
	if "" == typ {
		logging.LogErrorf("type is required")
		return
	}
	session.Set("type", typ)
	if typ == "main" && session.Request.URL.Query().Get("ready") == "false" {
		session.Set("uiReady", false)
	}

	pushChanLock.Lock()
	defer pushChanLock.Unlock()
	if IsAuthSession(session) {
		if appSessions, ok := authSessions.Load(appID); !ok {
			appSess := &sync.Map{}
			appSess.Store(id, session)
			authSessions.Store(appID, appSess)
		} else {
			(appSessions.(*sync.Map)).Store(id, session)
		}
	} else {
		if appSessions, ok := sessions.Load(appID); !ok {
			appSess := &sync.Map{}
			appSess.Store(id, session)
			sessions.Store(appID, appSess)
		} else {
			(appSessions.(*sync.Map)).Store(id, session)
		}
	}
}

// IsAuthPageKeepaliveRequest 判断是否为授权页保持连接请求,避免非常驻内存内核自动退出。
// 该请求无需认证,但连接必须被隔离在广播池之外。
// https://github.com/siyuan-note/insider/issues/1099
func IsAuthPageKeepaliveRequest(r *http.Request) bool {
	if "/ws" != r.URL.Path {
		return false
	}

	query := r.URL.Query()
	return strings.HasPrefix(query.Get("app"), "siyuan") && "auth" == query.Get("id") && "auth" == query.Get("type")
}

// HasDuplicateQueryValues 判断请求查询参数中是否存在重复键。
func HasDuplicateQueryValues(r *http.Request) bool {
	query := r.URL.Query()
	for _, values := range query {
		if 1 < len(values) {
			return true
		}
	}
	return false
}

func IsAuthSession(session *melody.Session) bool {
	// 授权页保持连接会话在接入时打上标记,禁止重解析请求查询参数判定会话身份
	if isAuth, ok := session.Get("authSession"); ok {
		return isAuth.(bool)
	}

	id, _ := session.Get("id")
	return "auth" == id
}

func RemovePushChan(session *melody.Session) {
	app, _ := session.Get("app")
	id, _ := session.Get("id")

	if nil == app || nil == id {
		return
	}

	pushChanLock.Lock()
	defer pushChanLock.Unlock()
	if IsAuthSession(session) {
		appSess, _ := authSessions.Load(app)
		if nil != appSess {
			appSessions := appSess.(*sync.Map)
			if !appSessions.CompareAndDelete(id, session) {
				return
			}
			if 1 > lenOfSyncMap(appSessions) {
				authSessions.Delete(app)
			}
		}
	} else {
		appSess, _ := sessions.Load(app)
		if nil != appSess {
			appSessions := appSess.(*sync.Map)
			if !appSessions.CompareAndDelete(id, session) {
				return
			}
			if 1 > lenOfSyncMap(appSessions) {
				sessions.Delete(app)
			}
		}
	}
}

func lenOfSyncMap(m *sync.Map) (ret int) {
	m.Range(func(key, value any) bool {
		ret++
		return true
	})
	return
}

func ClosePushChan(id string) {
	sessions.Range(func(key, value any) bool {
		appSessions := value.(*sync.Map)
		appSessions.Range(func(key, value any) bool {
			session := value.(*melody.Session)
			if sid, _ := session.Get("id"); sid == id {
				session.CloseWithMsg([]byte("  close websocket"))
				RemovePushChan(session)
			}
			return true
		})
		return true
	})
}

func ReloadUIResetScroll() {
	BroadcastByType("main", "reloadui", 0, "", map[string]any{"resetScroll": true})
}

func ReloadUI() {
	BroadcastByType("main", "reloadui", 0, "", nil)
}

// ReloadPublishServiceSessions 通知所有已打开的发布服务页面刷新,使发布插件设置立即生效。
func ReloadPublishServiceSessions() {
	for _, session := range publishSessionsByType("main") {
		event := NewResult()
		event.Cmd = "reloadpublishpage"
		session.Write(event.Bytes())
	}
}

func PushTxErr(msg string, code int, data any) {
	BroadcastByType("main", "txerr", code, msg, data)
}

func PushUpdateMsg(msgId string, msg string, timeout int) {
	PushUpdateMsgWithApp("", msgId, msg, timeout)
}

// PushUpdateMsgWithApp 在指定实例更新提示;未指定实例时广播,实例离线时丢弃。
func PushUpdateMsgWithApp(app, msgId, msg string, timeout int) {
	pushNotificationWithApp(app, "msg", 0, msg, map[string]any{"id": msgId, "closeTimeout": timeout})
}

func PushMsg(msg string, timeout int) (msgId string) {
	return PushMsgWithApp("", msg, timeout)
}

func PushMsgWithApp(app, msg string, timeout int) (msgId string) {
	msgId = gulu.Rand.String(7)
	PushUpdateMsgWithApp(app, msgId, msg, timeout)
	return
}

func PushErrMsg(msg string, timeout int) (msgId string) {
	return PushErrMsgWithApp("", msg, timeout)
}

// PushErrMsgWithApp 在指定实例显示错误;未指定实例时广播,实例离线时丢弃。
func PushErrMsgWithApp(app, msg string, timeout int) (msgId string) {
	msgId = gulu.Rand.String(7)
	pushNotificationWithApp(app, "msg", -1, msg, map[string]any{"id": msgId, "closeTimeout": timeout})
	return
}

func pushNotificationWithApp(app, cmd string, code int, msg string, data any) {
	if "" == app {
		BroadcastByType("main", cmd, code, msg, data)
		return
	}
	BroadcastByTypeAndApp("main", app, cmd, code, msg, data)
}

func PushStatusBar(msg string) {
	msg += " (" + time.Now().Format("2006-01-02 15:04:05") + ")"
	BroadcastByType("main", "statusbar", 0, msg, nil)
}

func PushBackgroundTask(data map[string]any) {
	BroadcastByType("main", "backgroundtask", 0, "", data)
}

func PushReloadFiletree() {
	BroadcastByType("filetree", "reloadFiletree", 0, "", nil)
}

func PushBoxDocFeatureChanged() {
	BroadcastByType("filetree", "boxDocFeatureChanged", 0, "", nil)
}

func PushReloadTag() {
	BroadcastByType("main", "reloadTag", 0, "", nil)
}

type BlockStatResult struct {
	RuneCount  int `json:"runeCount"`
	WordCount  int `json:"wordCount"`
	LinkCount  int `json:"linkCount"`
	ImageCount int `json:"imageCount"`
	RefCount   int `json:"refCount"`
	BlockCount int `json:"blockCount"`
}

func ContextPushMsg(context map[string]any, msg string) {
	pushTarget, ok := context[eventbus.CtxPushMsg].(int)
	if !ok {
		return
	}
	switch pushTarget {
	case eventbus.CtxPushMsgToNone:
		break
	case eventbus.CtxPushMsgToProgress:
		PushEndlessProgress(msg)
	case eventbus.CtxPushMsgToStatusBar:
		PushStatusBar(msg)
	case eventbus.CtxPushMsgToStatusBarAndProgress:
		PushStatusBar(msg)
		PushEndlessProgress(msg)
	}
}

const (
	PushProgressCodeProgressed = 0 // 有进度
	PushProgressCodeEndless    = 1 // 无进度
	PushProgressCodeEnd        = 2 // 关闭进度
)

func PushClearAllMsg() {
	ClearPushProgress(100)
	PushClearMsg("")
}

func ClearPushProgress(total int) {
	PushProgress(PushProgressCodeEnd, total, total, "")
}

func PushEndlessProgress(msg string) {
	PushProgress(PushProgressCodeEndless, 1, 1, msg)
}

func PushProgress(code, current, total int, msg string) {
	BroadcastByType("main", "progress", code, msg, map[string]any{
		"current": current,
		"total":   total,
	})
}

// PushClearMsg 会清空指定消息。
func PushClearMsg(msgId string) {
	BroadcastByType("main", "cmsg", 0, "", map[string]any{"id": msgId})
}

// PushClearMsgWithApp 清除指定实例的单条提示;空消息 ID 不执行清除,避免影响其他任务。
func PushClearMsgWithApp(app, msgId string) {
	if "" == msgId {
		return
	}
	pushNotificationWithApp(app, "cmsg", 0, "", map[string]any{"id": msgId})
}

// PushClearProgress 取消进度遮罩。
func PushClearProgress() {
	BroadcastByType("main", "cprogress", 0, "", nil)
}

func PushUpdateIDs(ids map[string]string) {
	BroadcastByType("main", "updateids", 0, "", ids)
}

func PushReloadDoc(rootID string) {
	BroadcastByType("main", "reloaddoc", 0, "", rootID)
}

func PushSaveDoc(rootID, typ string, sources any) {
	evt := NewCmdResult("savedoc", 0, PushModeBroadcast)
	evt.Data = map[string]any{
		"rootID":  rootID,
		"type":    typ,
		"sources": sources,
	}
	PushEvent(evt)
}

func PushReloadDocInfo(docInfo map[string]any) {
	// 加密笔记本锁定后丢弃延迟广播,避免泄漏明文元数据(title/alias/memo/bookmark)
	if ReloadDocInfoGuard != nil {
		if boxID, ok := docInfo["box"].(string); ok && boxID != "" {
			if !ReloadDocInfoGuard(boxID) {
				return
			}
		}
	}
	BroadcastByType("filetree", "reloadDocInfo", 0, "", docInfo)
}

func PushReloadProtyle(rootID string) {
	BroadcastByType("protyle", "reload", 0, "", rootID)
}

func PushSetRefDynamicText(rootID, blockID, defBlockID, refText, boxID string) {
	// 加密笔记本锁定后丢弃延迟广播,避免泄漏明文 refText
	if ReloadDocInfoGuard != nil && boxID != "" {
		if !ReloadDocInfoGuard(boxID) {
			return
		}
	}
	BroadcastByType("main", "setRefDynamicText", 0, "", map[string]any{"rootID": rootID, "blockID": blockID, "defBlockID": defBlockID, "refText": refText})
}

func PushSetDefRefCount(rootID, blockID string, defIDs []string, refCount, rootRefCount int) {
	BroadcastByType("main", "setDefRefCount", 0, "", map[string]any{"rootID": rootID, "blockID": blockID, "refCount": refCount, "rootRefCount": rootRefCount, "defIDs": defIDs})
}

func PushProtyleLoading(rootID, msg string) {
	BroadcastByType("protyle", "addLoading", 0, msg, rootID)
}

func PushReloadEmojiConf() {
	BroadcastByType("main", "reloadEmojiConf", 0, "", nil)
}

func PushKernelPluginState(name string, state int) {
	BroadcastByType("main", "updateKernelPluginState", 0, "", map[string]any{"name": name, "state": state})
}

func PushDownloadProgress(id string, percent float32) {
	evt := NewCmdResult("downloadProgress", 0, PushModeBroadcast)
	evt.Data = map[string]any{
		"id":      id,
		"percent": percent,
	}
	PushEvent(evt)
}

func PushEvent(event *Result) {
	msg := event.Bytes()
	mode := event.PushMode
	switch mode {
	case PushModeBroadcast:
		Broadcast(msg)
	case PushModeSingleSelf:
		single(msg, event.AppId, event.SessionId)
	case PushModeBroadcastExcludeSelf:
		broadcastOthers(msg, event.SessionId)
	case PushModeBroadcastExcludeSelfApp:
		broadcastOtherApps(msg, event.AppId)
	case PushModeBroadcastApp:
		broadcastApp(msg, event.AppId)
	case PushModeBroadcastMainExcludeSelfApp:
		broadcastOtherAppMains(msg, event.AppId)
	}
}

func single(msg []byte, appId, sid string) {
	sessions.Range(func(key, value any) bool {
		appSessions := value.(*sync.Map)
		if key != appId {
			return true
		}

		appSessions.Range(func(key, value any) bool {
			session := value.(*melody.Session)
			if id, _ := session.Get("id"); id == sid {
				session.Write(msg)
			}
			return true
		})
		return true
	})
}

func Broadcast(msg []byte) {
	sessions.Range(func(key, value any) bool {
		appSessions := value.(*sync.Map)
		appSessions.Range(func(key, value any) bool {
			session := value.(*melody.Session)
			if isPublishSession(session) {
				return true
			}
			session.Write(msg)
			return true
		})
		return true
	})
}

func broadcastOtherApps(msg []byte, excludeApp string) {
	sessions.Range(func(key, value any) bool {
		appSessions := value.(*sync.Map)
		appSessions.Range(func(key, value any) bool {
			session := value.(*melody.Session)
			if isPublishSession(session) {
				return true
			}
			if app, _ := session.Get("app"); app == excludeApp {
				return true
			}
			session.Write(msg)
			return true
		})
		return true
	})
}

func broadcastOtherAppMains(msg []byte, excludeApp string) {
	sessions.Range(func(key, value any) bool {
		appSessions := value.(*sync.Map)
		appSessions.Range(func(key, value any) bool {
			session := value.(*melody.Session)
			if isPublishSession(session) {
				return true
			}
			if app, _ := session.Get("app"); app == excludeApp {
				return true
			}

			if t, ok := session.Get("type"); ok && "main" != t {
				return true
			}

			session.Write(msg)
			return true
		})
		return true
	})
}

func broadcastApp(msg []byte, app string) {
	sessions.Range(func(key, value any) bool {
		appSessions := value.(*sync.Map)
		appSessions.Range(func(key, value any) bool {
			session := value.(*melody.Session)
			if isPublishSession(session) {
				return true
			}
			if sessionApp, _ := session.Get("app"); sessionApp != app {
				return true
			}
			session.Write(msg)
			return true
		})
		return true
	})
}

func broadcastOthers(msg []byte, excludeSID string) {
	sessions.Range(func(key, value any) bool {
		appSessions := value.(*sync.Map)
		appSessions.Range(func(key, value any) bool {
			session := value.(*melody.Session)
			if isPublishSession(session) {
				return true
			}
			if id, _ := session.Get("id"); id == excludeSID {
				return true
			}
			session.Write(msg)
			return true
		})
		return true
	})
}

func CountSessions() (ret int) {
	sessions.Range(func(key, value any) bool {
		ret++
		return true
	})
	authSessions.Range(func(key, value any) bool {
		ret++
		return true
	})
	return
}

// ClosePublishServiceSessions 关闭所有发布服务的 WebSocket 连接
func ClosePublishServiceSessions() {
	if WebSocketServer == nil {
		return
	}

	// 收集所有发布服务的会话
	var publishSessions []*melody.Session
	sessions.Range(func(key, value any) bool {
		appSessions := value.(*sync.Map)
		appSessions.Range(func(key, value any) bool {
			session := value.(*melody.Session)
			if isPublishSession(session) {
				publishSessions = append(publishSessions, session)
			}
			return true
		})
		return true
	})

	// 发送消息通知客户端关闭页面
	for _, session := range publishSessions {
		event := NewResult()
		event.Cmd = "closepublishpage"
		event.Code = 0
		event.Msg = "SiYuan publish service closed"
		event.Data = map[string]any{
			"reason": "publish service closed",
		}
		session.Write(event.Bytes())
	}

	// 等待一小段时间让消息发送完成、客户端刷新页面之后显示消息
	time.Sleep(500 * time.Millisecond)

	// 关闭所有发布服务的 WebSocket 连接
	for _, session := range publishSessions {
		// 使用 "close websocket" 作为关闭消息,客户端检测到后会停止重连
		session.CloseWithMsg([]byte("  close websocket: publish service closed"))
		RemovePushChan(session)
	}
}

// CloseOIDCSessions 关闭仅通过 OIDC 认证的 WebSocket 连接。
func CloseOIDCSessions() {
	var oidcSessions []*melody.Session
	sessions.Range(func(key, value any) bool {
		appSessions := value.(*sync.Map)
		appSessions.Range(func(key, value any) bool {
			session := value.(*melody.Session)
			if _, ok := session.Get("oidcSessionVersion"); ok {
				oidcSessions = append(oidcSessions, session)
			}
			return true
		})
		return true
	})
	for _, session := range oidcSessions {
		session.CloseWithMsg([]byte("  OIDC session expired"))
		RemovePushChan(session)
	}
}

var (
	// lastActivityNs 记录最近一次用户写操作(前端发送 /api/transactions* 请求)的纳秒时间戳。
	lastActivityNs       atomic.Int64
	indexFixVersion      atomic.Uint64
	indexFixCleanVersion atomic.Uint64
)

func init() {
	// 初始化为启动时间,避免启动瞬间被判定为空闲
	lastActivityNs.Store(time.Now().UnixNano())
}

// RefreshActivity 刷新用户最近活动时间,并标记索引可能已脏(需要订正)。
// 在 model.Activity 中间件中,对 /api/transactions* 写操作请求调用。
func RefreshActivity() {
	lastActivityNs.Store(time.Now().UnixNano())
	MarkIndexFixDirty()
}

// MarkIndexFixDirty records a change without treating background writes as user activity.
func MarkIndexFixDirty() { indexFixVersion.Add(1) }

func IndexFixVersion() uint64 { return indexFixVersion.Load() }

// MarkIndexClean acknowledges only the generation captured before a successful repair.
func MarkIndexClean(version uint64) bool {
	indexFixCleanVersion.Store(version)
	return indexFixVersion.Load() == version
}

// IsIdle 自上次用户活动以来是否已超过 idleThreshold。
func IsIdle(idleThreshold time.Duration) bool {
	return time.Since(time.Unix(0, lastActivityNs.Load())) >= idleThreshold
}

// IsIndexFixDirty 返回是否存在未订正的变更(上次订正后有新用户活动)。
func IsIndexFixDirty() bool {
	return indexFixVersion.Load() != indexFixCleanVersion.Load()
}