-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathlist.go
165 lines (128 loc) · 2.9 KB
/
list.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package commands
import (
"fmt"
"io"
"path/filepath"
"sort"
"github.com/NHAS/reverse_ssh/internal/server/clients"
"github.com/NHAS/reverse_ssh/internal/terminal/autocomplete"
"github.com/NHAS/reverse_ssh/pkg/table"
"golang.org/x/crypto/ssh"
)
type List struct {
}
type displayItem struct {
sc ssh.Conn
id string
}
func fancyTable(tty io.ReadWriter, applicable []displayItem) {
t, _ := table.NewTable("Targets", "ID", "Hostname", "IP Address")
for _, a := range applicable {
t.AddValues(a.id, a.sc.User(), a.sc.RemoteAddr().String())
}
t.Fprint(tty)
}
func (l *List) Run(tty io.ReadWriter, args ...string) error {
filter := ""
flags := map[byte]bool{}
for _, arg := range args {
if len(arg) > 0 && arg[0] == '-' {
for _, c := range arg[1:] {
flags[byte(c)] = true
}
continue
}
filter = arg
}
if flags['h'] {
fmt.Fprintf(tty, "%s", l.Help(false))
return nil
}
_, err := filepath.Match(filter, "")
if err != nil {
return fmt.Errorf("Filter is not well formed")
}
var toReturn []displayItem
clients := clients.GetAll()
ids := []string{}
for id := range clients {
ids = append(ids, id)
}
sort.Strings(ids)
for _, id := range ids {
conn := clients[id]
if filter == "" {
toReturn = append(toReturn, displayItem{id: id, sc: conn})
continue
}
match, _ := filepath.Match(filter, id)
if match {
toReturn = append(toReturn, displayItem{id: id, sc: conn})
continue
}
match, _ = filepath.Match(filter, conn.User())
if match {
toReturn = append(toReturn, displayItem{id: id, sc: conn})
continue
}
match, _ = filepath.Match(filter, conn.RemoteAddr().String())
if match {
toReturn = append(toReturn, displayItem{id: id, sc: conn})
continue
}
}
if flags['t'] {
fancyTable(tty, toReturn)
return nil
}
sep := ", "
if flags['l'] {
sep = "\n"
}
for i, tr := range toReturn {
if !flags['n'] && !flags['i'] && !flags['a'] {
fmt.Fprint(tty, tr.id)
if i != len(toReturn) {
fmt.Fprint(tty, sep)
}
continue
}
if flags['a'] {
fmt.Fprint(tty, tr.id)
}
if flags['n'] || flags['a'] {
fmt.Fprint(tty, " "+tr.sc.User())
}
if flags['i'] || flags['a'] {
fmt.Fprint(tty, " "+tr.sc.RemoteAddr().String())
}
if i != len(toReturn) {
fmt.Fprint(tty, sep)
}
}
if !flags['l'] {
fmt.Fprint(tty, "\n")
}
return nil
}
func (l *List) Expect(sections []string) []string {
if len(sections) == 1 {
return []string{autocomplete.RemoteId}
}
return nil
}
func (l *List) Help(explain bool) string {
if explain {
return "List connected controllable hosts."
}
return makeHelpText(
"ls [OPTION] [FILTER]",
"Filter uses glob matching against all attributes of a target (hostname, ip, id)",
"\t-a\tShow all attributes",
"\t-n\tShow only hostnames",
"\t-i\tShow only IP",
"\t-t\tPrint all attributes in pretty table",
"\t-l\tPrint with newline rather than space",
"\t-h\tPrint help",
)
}