-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathkill.go
100 lines (79 loc) · 2.01 KB
/
kill.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package commands
import (
"errors"
"fmt"
"io"
"github.com/NHAS/reverse_ssh/internal/server/users"
"github.com/NHAS/reverse_ssh/internal/terminal"
"github.com/NHAS/reverse_ssh/internal/terminal/autocomplete"
"github.com/NHAS/reverse_ssh/pkg/logger"
)
type kill struct {
log logger.Logger
}
func (k *kill) ValidArgs() map[string]string {
return map[string]string{"y": "Do not prompt for confirmation before killing clients"}
}
func (k *kill) Run(user *users.User, tty io.ReadWriter, line terminal.ParsedLine) error {
if len(line.Arguments) != 1 {
return errors.New(k.Help(false))
}
connections, err := user.SearchClients(line.Arguments[0].Value())
if err != nil {
return err
}
if len(connections) == 0 {
return fmt.Errorf("No clients matched '%s'", line.Arguments[0].Value())
}
if !line.IsSet("y") {
fmt.Fprintf(tty, "Kill %d clients? [N/y] ", len(connections))
if term, ok := tty.(*terminal.Terminal); ok {
term.EnableRaw()
}
b := make([]byte, 1)
_, err := tty.Read(b)
if err != nil {
if term, ok := tty.(*terminal.Terminal); ok {
term.DisableRaw()
}
return err
}
if term, ok := tty.(*terminal.Terminal); ok {
term.DisableRaw()
}
if !(b[0] == 'y' || b[0] == 'Y') {
return fmt.Errorf("\nUser did not enter y/Y, aborting")
}
fmt.Fprint(tty, "\n")
}
killedClients := 0
for id, serverConn := range connections {
serverConn.SendRequest("kill", false, nil)
if len(connections) == 1 {
return fmt.Errorf("%s killed", id)
}
killedClients++
}
return fmt.Errorf("%d connections killed", killedClients)
}
func (k *kill) Expect(line terminal.ParsedLine) []string {
if len(line.Arguments) <= 1 {
return []string{autocomplete.RemoteId}
}
return nil
}
func (k *kill) Help(explain bool) string {
if explain {
return "Stop the execute of the rssh client."
}
return terminal.MakeHelpText(k.ValidArgs(),
"kill <remote_id>",
"kill <glob pattern>",
"Stop the execute of the rssh client.",
)
}
func Kill(log logger.Logger) *kill {
return &kill{
log: log,
}
}