mirror of
https://github.com/m1k1o/neko.git
synced 2026-11-03 00:11:48 -06:00
implement client heartbeat #460.
This commit is contained in:
@@ -18,6 +18,7 @@ type Session struct {
|
||||
ImplicitHosting bool
|
||||
InactiveCursors bool
|
||||
MercifulReconnect bool
|
||||
HeartbeatInterval int
|
||||
APIToken string
|
||||
|
||||
CookieEnabled bool
|
||||
@@ -67,6 +68,11 @@ func (Session) Init(cmd *cobra.Command) error {
|
||||
return err
|
||||
}
|
||||
|
||||
cmd.PersistentFlags().Int("session.heartbeat_interval", 120, "interval in seconds for sending heartbeat messages")
|
||||
if err := viper.BindPFlag("session.heartbeat_interval", cmd.PersistentFlags().Lookup("session.heartbeat_interval")); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cmd.PersistentFlags().String("session.api_token", "", "API token for interacting with external services")
|
||||
if err := viper.BindPFlag("session.api_token", cmd.PersistentFlags().Lookup("session.api_token")); err != nil {
|
||||
return err
|
||||
@@ -112,6 +118,11 @@ func (Session) InitV2(cmd *cobra.Command) error {
|
||||
return err
|
||||
}
|
||||
|
||||
cmd.PersistentFlags().Int("heartbeat_interval", 120, "heartbeat interval in seconds")
|
||||
if err := viper.BindPFlag("heartbeat_interval", cmd.PersistentFlags().Lookup("heartbeat_interval")); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -125,6 +136,7 @@ func (s *Session) Set() {
|
||||
s.ImplicitHosting = viper.GetBool("session.implicit_hosting")
|
||||
s.InactiveCursors = viper.GetBool("session.inactive_cursors")
|
||||
s.MercifulReconnect = viper.GetBool("session.merciful_reconnect")
|
||||
s.HeartbeatInterval = viper.GetInt("session.heartbeat_interval")
|
||||
s.APIToken = viper.GetString("session.api_token")
|
||||
|
||||
s.CookieEnabled = viper.GetBool("session.cookie.enabled")
|
||||
@@ -156,4 +168,8 @@ func (s *Session) SetV2() {
|
||||
s.ControlProtection = viper.GetBool("control_protection")
|
||||
log.Warn().Msg("you are using v2 configuration 'NEKO_CONTROL_PROTECTION' which is deprecated, please use 'NEKO_SESSION_CONTROL_PROTECTION' instead")
|
||||
}
|
||||
if viper.IsSet("heartbeat_interval") {
|
||||
s.HeartbeatInterval = viper.GetInt("heartbeat_interval")
|
||||
log.Warn().Msg("you are using v2 configuration 'NEKO_HEARTBEAT_INTERVAL' which is deprecated, please use 'NEKO_SESSION_HEARTBEAT_INTERVAL' instead")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,10 @@ const (
|
||||
SYSTEM_ERROR = "system/error"
|
||||
)
|
||||
|
||||
const (
|
||||
CLIENT_HEARTBEAT = "client/heartbeat"
|
||||
)
|
||||
|
||||
const (
|
||||
SIGNAL_OFFER = "signal/offer"
|
||||
SIGNAL_ANSWER = "signal/answer"
|
||||
|
||||
@@ -11,10 +11,11 @@ type Message struct {
|
||||
}
|
||||
|
||||
type SystemInit struct {
|
||||
Event string `json:"event"`
|
||||
Locks map[string]string `json:"locks"`
|
||||
ImplicitHosting bool `json:"implicit_hosting"`
|
||||
FileTransfer bool `json:"file_transfer"`
|
||||
Event string `json:"event"`
|
||||
Locks map[string]string `json:"locks"`
|
||||
ImplicitHosting bool `json:"implicit_hosting"`
|
||||
FileTransfer bool `json:"file_transfer"`
|
||||
HeartbeatInterval int `json:"heartbeat_interval"`
|
||||
}
|
||||
|
||||
type SystemMessage struct {
|
||||
|
||||
@@ -26,6 +26,11 @@ func (s *session) wsToBackend(msg []byte) error {
|
||||
}
|
||||
|
||||
switch header.Event {
|
||||
// Client Events
|
||||
case oldEvent.CLIENT_HEARTBEAT:
|
||||
// do nothing
|
||||
return nil
|
||||
|
||||
// Signal Events
|
||||
case oldEvent.SIGNAL_OFFER:
|
||||
request := &oldMessage.SignalOffer{}
|
||||
|
||||
@@ -259,7 +259,8 @@ func (s *session) wsToClient(msg []byte) error {
|
||||
ImplicitHosting: request.Settings.ImplicitHosting,
|
||||
Locks: locks,
|
||||
// TODO: hack - we don't know if file transfer is enabled, we would need to check the global config.
|
||||
FileTransfer: viper.GetBool("filetransfer.enabled") || (viper.GetBool("legacy") && viper.GetBool("file_transfer_enabled")),
|
||||
FileTransfer: viper.GetBool("filetransfer.enabled") || (viper.GetBool("legacy") && viper.GetBool("file_transfer_enabled")),
|
||||
HeartbeatInterval: request.Settings.HeartbeatInterval,
|
||||
})
|
||||
|
||||
case event.SYSTEM_ADMIN:
|
||||
|
||||
@@ -27,6 +27,7 @@ func New(config *config.Session) *SessionManagerCtx {
|
||||
ImplicitHosting: config.ImplicitHosting,
|
||||
InactiveCursors: config.InactiveCursors,
|
||||
MercifulReconnect: config.MercifulReconnect,
|
||||
HeartbeatInterval: config.HeartbeatInterval,
|
||||
},
|
||||
tokens: make(map[string]string),
|
||||
sessions: make(map[string]*SessionCtx),
|
||||
|
||||
@@ -36,6 +36,10 @@ type MessageHandlerCtx struct {
|
||||
func (h *MessageHandlerCtx) Message(session types.Session, data types.WebSocketMessage) bool {
|
||||
var err error
|
||||
switch data.Event {
|
||||
// Client Events
|
||||
case event.CLIENT_HEARTBEAT:
|
||||
// do nothing
|
||||
|
||||
// System Events
|
||||
case event.SYSTEM_LOGS:
|
||||
payload := &message.SystemLogs{}
|
||||
|
||||
@@ -31,8 +31,9 @@ const maxPayloadLogLength = 10_000
|
||||
var nologEvents = []string{
|
||||
// don't log twice
|
||||
event.SYSTEM_LOGS,
|
||||
// don't log heartbeat
|
||||
// don't log heartbeats
|
||||
event.SYSTEM_HEARTBEAT,
|
||||
event.CLIENT_HEARTBEAT,
|
||||
// don't log every cursor update
|
||||
event.SESSION_CURSORS,
|
||||
}
|
||||
|
||||
@@ -9,6 +9,10 @@ const (
|
||||
SYSTEM_HEARTBEAT = "system/heartbeat"
|
||||
)
|
||||
|
||||
const (
|
||||
CLIENT_HEARTBEAT = "client/heartbeat"
|
||||
)
|
||||
|
||||
const (
|
||||
SIGNAL_REQUEST = "signal/request"
|
||||
SIGNAL_RESTART = "signal/restart"
|
||||
|
||||
@@ -47,6 +47,7 @@ type Settings struct {
|
||||
ImplicitHosting bool `json:"implicit_hosting"`
|
||||
InactiveCursors bool `json:"inactive_cursors"`
|
||||
MercifulReconnect bool `json:"merciful_reconnect"`
|
||||
HeartbeatInterval int `json:"heartbeat_interval"`
|
||||
|
||||
// plugin scope
|
||||
Plugins PluginSettings `json:"plugins"`
|
||||
|
||||
Reference in New Issue
Block a user