Skip to content

Commit fc3b665

Browse files
committed
chore(ssh,pkg): move ssh event structures to models
1 parent d42c479 commit fc3b665

File tree

6 files changed

+49
-97
lines changed

6 files changed

+49
-97
lines changed

pkg/models/session.go

-5
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,6 @@ type Status struct {
5151
Authenticated bool `json:"authenticated"`
5252
}
5353

54-
type SessionRecorded struct {
55-
UID string `json:"uid"`
56-
Output string `json:"output" bson:"output"`
57-
}
58-
5954
type SessionUpdate struct {
6055
Authenticated *bool `json:"authenticated"`
6156
Type *string `json:"type"`

pkg/models/ssh.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package models
2+
3+
type SSHCommand struct {
4+
Command string `json:"command"`
5+
}
6+
7+
type SSHSubsystem struct {
8+
Subsystem string `json:"subsystem"`
9+
}
10+
11+
type SSHExitStatus struct {
12+
Status uint32 `json:"status"`
13+
}
14+
15+
type SSHSignal struct {
16+
Name uint32 `json:"status"`
17+
Dumped bool `json:"dumped"`
18+
Message string `json:"message"`
19+
Lang string `json:"lang"`
20+
}
21+
22+
type SSHWindowChange struct {
23+
Columns uint32 `json:"columns"`
24+
Rows uint32 `json:"rows"`
25+
Width uint32 `json:"width"`
26+
Height uint32 `json:"height"`
27+
}
28+
29+
// NOTE: [SSHPty] cannot use [SSHWindowChange] inside itself due [ssh.Unmarshal] issues.
30+
type SSHPty struct {
31+
Term string `json:"term"`
32+
Columns uint32 `json:"columns" `
33+
Rows uint32 `json:"rows"`
34+
Width uint32 `json:"width"`
35+
Height uint32 `json:"height"`
36+
Modelist []byte `json:"modelist"`
37+
}
38+
39+
type SSHPtyOutput struct {
40+
Output string `json:"output"`
41+
}

ssh/server/channels/session.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"strings"
55

66
gliderssh "github.com/gliderlabs/ssh"
7+
"github.com/shellhub-io/shellhub/pkg/models"
78
"github.com/shellhub-io/shellhub/ssh/session"
89
log "github.com/sirupsen/logrus"
910
gossh "golang.org/x/crypto/ssh"
@@ -188,9 +189,9 @@ func DefaultSessionHandler() gliderssh.ChannelHandler {
188189

189190
switch req.Type {
190191
case ExitStatusRequest:
191-
session.Event[session.Status](sess, req.Type, req.Payload, seat)
192+
session.Event[models.SSHExitStatus](sess, req.Type, req.Payload, seat)
192193
case ExitSignalRequest:
193-
session.Event[session.Signal](sess, req.Type, req.Payload, seat)
194+
session.Event[models.SSHSignal](sess, req.Type, req.Payload, seat)
194195
default:
195196
sess.Event(req.Type, req.Payload, seat)
196197
}
@@ -226,11 +227,11 @@ func DefaultSessionHandler() gliderssh.ChannelHandler {
226227

227228
sess.Event(req.Type, req.Payload, seat)
228229
case ExecRequestType, SubsystemRequestType:
229-
session.Event[session.Command](sess, req.Type, req.Payload, seat)
230+
session.Event[models.SSHCommand](sess, req.Type, req.Payload, seat)
230231

231232
sess.Type = ExecRequestType
232233
case PtyRequestType:
233-
var pty session.Pty
234+
var pty models.SSHPty
234235

235236
if err := gossh.Unmarshal(req.Payload, &pty); err != nil {
236237
reject(nil, "failed to recover the session dimensions")
@@ -240,7 +241,7 @@ func DefaultSessionHandler() gliderssh.ChannelHandler {
240241

241242
sess.Event(req.Type, pty, seat) //nolint:errcheck
242243
case WindowChangeRequestType:
243-
var dimensions session.Dimensions
244+
var dimensions models.SSHWindowChange
244245

245246
if err := gossh.Unmarshal(req.Payload, &dimensions); err != nil {
246247
reject(nil, "failed to recover the session dimensions")

ssh/server/channels/utils.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ func (c *Recorder) Write(output []byte) (int, error) {
3939
}
4040

4141
// NOTE: Writes the event into the event stream to be processed and send to target endpoint.
42-
c.session.Event(PtyOutputEventType, &models.SessionRecorded{
43-
UID: c.session.UID,
42+
c.session.Event(PtyOutputEventType, &models.SSHPtyOutput{
4443
Output: string(output),
4544
}, c.seat)
4645

ssh/session/camera.go

-37
This file was deleted.

ssh/session/session.go

+1-48
Original file line numberDiff line numberDiff line change
@@ -25,42 +25,6 @@ import (
2525
gossh "golang.org/x/crypto/ssh"
2626
)
2727

28-
type Command struct {
29-
Command string `json:"command"`
30-
}
31-
32-
type Subsystem struct {
33-
Subsystem string `json:"subsystem"`
34-
}
35-
36-
type Status struct {
37-
Status uint32 `json:"status"`
38-
}
39-
40-
type Signal struct {
41-
Name uint32 `json:"status"`
42-
Dumped bool `json:"dumped"`
43-
Message string `json:"message"`
44-
Lang string `json:"lang"`
45-
}
46-
47-
type Dimensions struct {
48-
Columns uint32 `json:"columns"`
49-
Rows uint32 `json:"rows"`
50-
Width uint32 `json:"width"`
51-
Height uint32 `json:"height"`
52-
}
53-
54-
// NOTICE: [Pty] cannot use [Dimensions] inside itself due [ssh.Unmarshal] issues.
55-
type Pty struct {
56-
Term string `json:"term"`
57-
Columns uint32 `json:"columns" `
58-
Rows uint32 `json:"rows"`
59-
Width uint32 `json:"width"`
60-
Height uint32 `json:"height"`
61-
Modelist []byte `json:"modelist"`
62-
}
63-
6428
type Data struct {
6529
Target *target.Target
6630
// SSHID is the combination of device's name and namespace name.
@@ -75,7 +39,7 @@ type Data struct {
7539
// TODO:
7640
Lookup map[string]string
7741
// Pty is the PTY dimension.
78-
Pty Pty
42+
Pty models.SSHPty
7943
// Handled check if the session is already handling a "shell", "exec" or a "subsystem".
8044
Handled bool
8145
}
@@ -550,17 +514,6 @@ func (s *Session) Auth(ctx gliderssh.Context, auth Auth) error {
550514
return nil
551515
}
552516

553-
func (s *Session) Record(ctx context.Context, url string, seat int) (*Camera, error) {
554-
conn, err := s.api.RecordSession(ctx, s.UID, seat, url)
555-
if err != nil {
556-
log.WithError(err).Error("failed to start the record session process")
557-
558-
return nil, err
559-
}
560-
561-
return NewCamera(conn), nil
562-
}
563-
564517
func (s *Session) NewSeat() (int, error) {
565518
seat := int(s.Seat.Load())
566519
defer s.Seat.Add(1)

0 commit comments

Comments
 (0)