2024-07-20 05:10:27 +12:00
|
|
|
package legacy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
2024-07-21 01:45:01 +12:00
|
|
|
oldEvent "github.com/demodesk/neko/internal/http/legacy/event"
|
|
|
|
oldMessage "github.com/demodesk/neko/internal/http/legacy/message"
|
|
|
|
"github.com/pion/webrtc/v3"
|
|
|
|
|
|
|
|
"github.com/demodesk/neko/pkg/types"
|
|
|
|
"github.com/demodesk/neko/pkg/types/event"
|
|
|
|
"github.com/demodesk/neko/pkg/types/message"
|
|
|
|
|
|
|
|
chat "github.com/demodesk/neko/internal/plugins/chat"
|
|
|
|
filetransfer "github.com/demodesk/neko/internal/plugins/filetransfer"
|
2024-07-20 05:10:27 +12:00
|
|
|
)
|
|
|
|
|
2024-07-21 01:45:01 +12:00
|
|
|
func (s *session) wsToBackend(msg []byte, sendMsg func([]byte) error) error {
|
|
|
|
header := oldMessage.Message{}
|
2024-07-20 05:10:27 +12:00
|
|
|
err := json.Unmarshal(msg, &header)
|
|
|
|
if err != nil {
|
2024-07-21 01:45:01 +12:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
send := func(event string, payload any) error {
|
|
|
|
rawPayload, err := json.Marshal(payload)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
msg, err := json.Marshal(&types.WebSocketMessage{
|
|
|
|
Event: event,
|
|
|
|
Payload: rawPayload,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return sendMsg(msg)
|
2024-07-20 05:10:27 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
switch header.Event {
|
|
|
|
// Signal Events
|
2024-07-21 01:45:01 +12:00
|
|
|
case oldEvent.SIGNAL_OFFER:
|
|
|
|
request := &oldMessage.SignalOffer{}
|
2024-07-20 05:10:27 +12:00
|
|
|
err := json.Unmarshal(msg, request)
|
|
|
|
if err != nil {
|
2024-07-21 01:45:01 +12:00
|
|
|
return err
|
2024-07-20 05:10:27 +12:00
|
|
|
}
|
|
|
|
|
2024-07-21 01:45:01 +12:00
|
|
|
return send(event.SIGNAL_OFFER, &message.SignalDescription{
|
|
|
|
SDP: request.SDP,
|
|
|
|
})
|
|
|
|
|
|
|
|
case oldEvent.SIGNAL_ANSWER:
|
|
|
|
request := &oldMessage.SignalAnswer{}
|
2024-07-20 05:10:27 +12:00
|
|
|
err := json.Unmarshal(msg, request)
|
|
|
|
if err != nil {
|
2024-07-21 01:45:01 +12:00
|
|
|
return err
|
2024-07-20 05:10:27 +12:00
|
|
|
}
|
|
|
|
|
2024-07-21 01:45:01 +12:00
|
|
|
// TODO: Set Display Name here.
|
|
|
|
|
|
|
|
return send(event.SIGNAL_ANSWER, &message.SignalDescription{
|
|
|
|
SDP: request.SDP,
|
|
|
|
})
|
|
|
|
|
|
|
|
case oldEvent.SIGNAL_CANDIDATE:
|
|
|
|
request := &oldMessage.SignalCandidate{}
|
2024-07-20 05:10:27 +12:00
|
|
|
err := json.Unmarshal(msg, request)
|
|
|
|
if err != nil {
|
2024-07-21 01:45:01 +12:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var candidate webrtc.ICECandidateInit
|
|
|
|
err = json.Unmarshal([]byte(request.Data), &candidate)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2024-07-20 05:10:27 +12:00
|
|
|
}
|
|
|
|
|
2024-07-21 01:45:01 +12:00
|
|
|
return send(event.SIGNAL_CANDIDATE, &message.SignalCandidate{
|
|
|
|
ICECandidateInit: candidate,
|
|
|
|
})
|
|
|
|
|
2024-07-20 05:10:27 +12:00
|
|
|
// Control Events
|
2024-07-21 01:45:01 +12:00
|
|
|
case oldEvent.CONTROL_RELEASE:
|
|
|
|
return send(event.CONTROL_RELEASE, nil)
|
|
|
|
|
|
|
|
case oldEvent.CONTROL_REQUEST:
|
|
|
|
return send(event.CONTROL_REQUEST, nil)
|
|
|
|
|
|
|
|
case oldEvent.CONTROL_GIVE:
|
|
|
|
request := &oldMessage.Control{}
|
2024-07-20 05:10:27 +12:00
|
|
|
err := json.Unmarshal(msg, request)
|
|
|
|
if err != nil {
|
2024-07-21 01:45:01 +12:00
|
|
|
return err
|
2024-07-20 05:10:27 +12:00
|
|
|
}
|
|
|
|
|
2024-07-21 01:45:01 +12:00
|
|
|
// TODO: No WS equivalent, call HTTP API.
|
|
|
|
return fmt.Errorf("event not implemented: %s", header.Event)
|
|
|
|
|
|
|
|
case oldEvent.CONTROL_CLIPBOARD:
|
|
|
|
request := &oldMessage.Clipboard{}
|
2024-07-20 05:10:27 +12:00
|
|
|
err := json.Unmarshal(msg, request)
|
|
|
|
if err != nil {
|
2024-07-21 01:45:01 +12:00
|
|
|
return err
|
2024-07-20 05:10:27 +12:00
|
|
|
}
|
|
|
|
|
2024-07-21 01:45:01 +12:00
|
|
|
return send(event.CLIPBOARD_SET, &message.ClipboardData{
|
|
|
|
Text: request.Text,
|
|
|
|
})
|
|
|
|
|
|
|
|
case oldEvent.CONTROL_KEYBOARD:
|
|
|
|
request := &oldMessage.Keyboard{}
|
2024-07-20 05:10:27 +12:00
|
|
|
err := json.Unmarshal(msg, request)
|
|
|
|
if err != nil {
|
2024-07-21 01:45:01 +12:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if request.Layout != nil {
|
|
|
|
err = send(event.KEYBOARD_MAP, &message.KeyboardMap{
|
|
|
|
KeyboardMap: types.KeyboardMap{
|
|
|
|
Layout: *request.Layout,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if request.CapsLock != nil || request.NumLock != nil || request.ScrollLock != nil {
|
|
|
|
err = send(event.KEYBOARD_MODIFIERS, &message.KeyboardModifiers{
|
|
|
|
KeyboardModifiers: types.KeyboardModifiers{
|
|
|
|
CapsLock: request.CapsLock,
|
|
|
|
NumLock: request.NumLock,
|
|
|
|
// ScrollLock: request.ScrollLock, // ScrollLock is deprecated.
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-07-20 05:10:27 +12:00
|
|
|
}
|
|
|
|
|
2024-07-21 01:45:01 +12:00
|
|
|
return nil
|
|
|
|
|
2024-07-20 05:10:27 +12:00
|
|
|
// Chat Events
|
2024-07-21 01:45:01 +12:00
|
|
|
case oldEvent.CHAT_MESSAGE:
|
|
|
|
request := &oldMessage.ChatReceive{}
|
2024-07-20 05:10:27 +12:00
|
|
|
err := json.Unmarshal(msg, request)
|
|
|
|
if err != nil {
|
2024-07-21 01:45:01 +12:00
|
|
|
return err
|
2024-07-20 05:10:27 +12:00
|
|
|
}
|
|
|
|
|
2024-07-21 01:45:01 +12:00
|
|
|
return send(chat.CHAT_MESSAGE, &chat.Content{
|
|
|
|
Text: request.Content,
|
|
|
|
})
|
|
|
|
|
|
|
|
case oldEvent.CHAT_EMOTE:
|
|
|
|
request := &oldMessage.EmoteReceive{}
|
2024-07-20 05:10:27 +12:00
|
|
|
err := json.Unmarshal(msg, request)
|
|
|
|
if err != nil {
|
2024-07-21 01:45:01 +12:00
|
|
|
return err
|
2024-07-20 05:10:27 +12:00
|
|
|
}
|
|
|
|
|
2024-07-21 01:45:01 +12:00
|
|
|
// TODO: Emote plugin not implemented.
|
|
|
|
return fmt.Errorf("event not implemented: %s", header.Event)
|
|
|
|
|
2024-07-20 05:10:27 +12:00
|
|
|
// File Transfer Events
|
2024-07-21 01:45:01 +12:00
|
|
|
case oldEvent.FILETRANSFER_REFRESH:
|
|
|
|
return send(filetransfer.FILETRANSFER_UPDATE, nil)
|
2024-07-20 05:10:27 +12:00
|
|
|
|
|
|
|
// Screen Events
|
2024-07-21 01:45:01 +12:00
|
|
|
case oldEvent.SCREEN_RESOLUTION:
|
|
|
|
// No WS equivalent, call HTTP API and return screen resolution.
|
|
|
|
return fmt.Errorf("event not implemented: %s", header.Event)
|
|
|
|
|
|
|
|
case oldEvent.SCREEN_CONFIGURATIONS:
|
|
|
|
// No WS equivalent, call HTTP API and return screen configurations.
|
|
|
|
return fmt.Errorf("event not implemented: %s", header.Event)
|
|
|
|
|
|
|
|
case oldEvent.SCREEN_SET:
|
|
|
|
request := &oldMessage.ScreenResolution{}
|
2024-07-20 05:10:27 +12:00
|
|
|
err := json.Unmarshal(msg, request)
|
|
|
|
if err != nil {
|
2024-07-21 01:45:01 +12:00
|
|
|
return err
|
2024-07-20 05:10:27 +12:00
|
|
|
}
|
|
|
|
|
2024-07-21 01:45:01 +12:00
|
|
|
return send(event.SCREEN_SET, &message.ScreenSize{
|
|
|
|
ScreenSize: types.ScreenSize{
|
|
|
|
Width: request.Width,
|
|
|
|
Height: request.Height,
|
|
|
|
Rate: request.Rate,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2024-07-20 05:10:27 +12:00
|
|
|
// Broadcast Events
|
2024-07-21 01:45:01 +12:00
|
|
|
case oldEvent.BROADCAST_CREATE:
|
|
|
|
request := &oldMessage.BroadcastCreate{}
|
2024-07-20 05:10:27 +12:00
|
|
|
err := json.Unmarshal(msg, request)
|
|
|
|
if err != nil {
|
2024-07-21 01:45:01 +12:00
|
|
|
return err
|
2024-07-20 05:10:27 +12:00
|
|
|
}
|
|
|
|
|
2024-07-21 01:45:01 +12:00
|
|
|
// TODO: No WS equivalent, call HTTP API.
|
|
|
|
return fmt.Errorf("event not implemented: %s", header.Event)
|
|
|
|
|
|
|
|
case oldEvent.BROADCAST_DESTROY:
|
|
|
|
// TODO: No WS equivalent, call HTTP API.
|
|
|
|
return fmt.Errorf("event not implemented: %s", header.Event)
|
2024-07-20 05:10:27 +12:00
|
|
|
|
|
|
|
// Admin Events
|
2024-07-21 01:45:01 +12:00
|
|
|
case oldEvent.ADMIN_LOCK:
|
|
|
|
request := &oldMessage.AdminLock{}
|
2024-07-20 05:10:27 +12:00
|
|
|
err := json.Unmarshal(msg, request)
|
|
|
|
if err != nil {
|
2024-07-21 01:45:01 +12:00
|
|
|
return err
|
2024-07-20 05:10:27 +12:00
|
|
|
}
|
|
|
|
|
2024-07-21 01:45:01 +12:00
|
|
|
// TODO: No WS equivalent, call HTTP API.
|
|
|
|
return fmt.Errorf("event not implemented: %s", header.Event)
|
|
|
|
|
|
|
|
case oldEvent.ADMIN_UNLOCK:
|
|
|
|
request := &oldMessage.AdminLock{}
|
2024-07-20 05:10:27 +12:00
|
|
|
err := json.Unmarshal(msg, request)
|
|
|
|
if err != nil {
|
2024-07-21 01:45:01 +12:00
|
|
|
return err
|
2024-07-20 05:10:27 +12:00
|
|
|
}
|
|
|
|
|
2024-07-21 01:45:01 +12:00
|
|
|
// TODO: No WS equivalent, call HTTP API.
|
|
|
|
return fmt.Errorf("event not implemented: %s", header.Event)
|
|
|
|
|
|
|
|
case oldEvent.ADMIN_CONTROL:
|
|
|
|
// TODO: No WS equivalent, call HTTP API.
|
|
|
|
return fmt.Errorf("event not implemented: %s", header.Event)
|
|
|
|
|
|
|
|
case oldEvent.ADMIN_RELEASE:
|
|
|
|
// TODO: No WS equivalent, call HTTP API.
|
|
|
|
return fmt.Errorf("event not implemented: %s", header.Event)
|
|
|
|
|
|
|
|
case oldEvent.ADMIN_GIVE:
|
|
|
|
request := &oldMessage.Admin{}
|
2024-07-20 05:10:27 +12:00
|
|
|
err := json.Unmarshal(msg, request)
|
|
|
|
if err != nil {
|
2024-07-21 01:45:01 +12:00
|
|
|
return err
|
2024-07-20 05:10:27 +12:00
|
|
|
}
|
|
|
|
|
2024-07-21 01:45:01 +12:00
|
|
|
// TODO: No WS equivalent, call HTTP API.
|
|
|
|
return fmt.Errorf("event not implemented: %s", header.Event)
|
|
|
|
|
|
|
|
case oldEvent.ADMIN_BAN:
|
|
|
|
request := &oldMessage.Admin{}
|
2024-07-20 05:10:27 +12:00
|
|
|
err := json.Unmarshal(msg, request)
|
|
|
|
if err != nil {
|
2024-07-21 01:45:01 +12:00
|
|
|
return err
|
2024-07-20 05:10:27 +12:00
|
|
|
}
|
|
|
|
|
2024-07-21 01:45:01 +12:00
|
|
|
// TODO: No WS equivalent, call HTTP API.
|
|
|
|
return fmt.Errorf("event not implemented: %s", header.Event)
|
|
|
|
|
|
|
|
case oldEvent.ADMIN_KICK:
|
|
|
|
request := &oldMessage.Admin{}
|
2024-07-20 05:10:27 +12:00
|
|
|
err := json.Unmarshal(msg, request)
|
|
|
|
if err != nil {
|
2024-07-21 01:45:01 +12:00
|
|
|
return err
|
2024-07-20 05:10:27 +12:00
|
|
|
}
|
|
|
|
|
2024-07-21 01:45:01 +12:00
|
|
|
// TODO: No WS equivalent, call HTTP API.
|
|
|
|
return fmt.Errorf("event not implemented: %s", header.Event)
|
|
|
|
|
|
|
|
case oldEvent.ADMIN_MUTE:
|
|
|
|
request := &oldMessage.Admin{}
|
2024-07-20 05:10:27 +12:00
|
|
|
err := json.Unmarshal(msg, request)
|
|
|
|
if err != nil {
|
2024-07-21 01:45:01 +12:00
|
|
|
return err
|
2024-07-20 05:10:27 +12:00
|
|
|
}
|
|
|
|
|
2024-07-21 01:45:01 +12:00
|
|
|
// TODO: No WS equivalent, call HTTP API.
|
|
|
|
return fmt.Errorf("event not implemented: %s", header.Event)
|
|
|
|
|
|
|
|
case oldEvent.ADMIN_UNMUTE:
|
|
|
|
request := &oldMessage.Admin{}
|
2024-07-20 05:10:27 +12:00
|
|
|
err := json.Unmarshal(msg, request)
|
|
|
|
if err != nil {
|
2024-07-21 01:45:01 +12:00
|
|
|
return err
|
2024-07-20 05:10:27 +12:00
|
|
|
}
|
|
|
|
|
2024-07-21 01:45:01 +12:00
|
|
|
// TODO: No WS equivalent, call HTTP API.
|
|
|
|
return fmt.Errorf("event not implemented: %s", header.Event)
|
|
|
|
|
2024-07-20 05:10:27 +12:00
|
|
|
default:
|
2024-07-21 01:45:01 +12:00
|
|
|
return fmt.Errorf("unknown event type: %s", header.Event)
|
2024-07-20 05:10:27 +12:00
|
|
|
}
|
|
|
|
}
|