-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteractive.go
209 lines (165 loc) · 5.25 KB
/
interactive.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package cmd
import (
"fmt"
"os"
"os/exec"
"sort"
"strings"
"time"
"github.com/inpher/sb/internal/commands"
"github.com/inpher/sb/internal/config"
"github.com/inpher/sb/internal/helpers"
"github.com/inpher/sb/internal/models"
"github.com/inpher/sb/internal/types"
prompt "github.com/c-bata/go-prompt"
)
// Interactive describes the interactive command
type Interactive struct {
Context *commands.Context
}
func init() {
commands.RegisterCommand("interactive", func() (c commands.Command, r models.Right, helper helpers.Helper, args map[string]commands.Argument) {
return new(Interactive), models.Public, helpers.Helper{
Header: "launch sb in interactive mode",
Usage: "interactive",
Description: "launch sb in interactive mode",
}, map[string]commands.Argument{
"client": {
Required: true,
Description: "The client to use SSH or MOSH",
},
"client-arguments": {
Required: false,
},
}
})
}
// Checks checks whether or not the user can execute this method
func (c *Interactive) Checks(ct *commands.Context) error {
// No specific rights needed but a sb account
return nil
}
// Execute executes the command
func (c *Interactive) Execute(ct *commands.Context) (repl models.ReplicationData, cmdError error, err error) {
c.Context = ct
// Special case, we need to launch a mosh-server that will be calling ourselves
if ct.FormattedArguments["client"] == "mosh" {
fmt.Printf("Launched interactive command with mosh client...\n")
moshCommand, errMosh := c.buildMOSHCommand(ct)
if errMosh != nil {
err = errMosh
return
}
moshCommand = append(moshCommand, config.GetBinaryPath(), "-i")
cmd := exec.Command(moshCommand[0], moshCommand[1:]...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Start()
if err != nil {
return
}
err = cmd.Wait()
if err != nil {
var ok bool
cmdError, ok = err.(*exec.ExitError)
if !ok {
return
}
err = nil
}
return
}
// Launch our prompt
p := prompt.New(c.promptExecutor, c.promptCompleter,
prompt.OptionTitle("sb prompt"),
prompt.OptionPrefix(c.promptPrefix()),
prompt.OptionPrefixTextColor(prompt.DarkBlue),
prompt.OptionMaxSuggestion(20),
)
p.Run()
return
}
func (c *Interactive) PostExecute(repl models.ReplicationData) (err error) {
return
}
func (c *Interactive) Replicate(repl models.ReplicationData) (err error) {
return
}
func (c *Interactive) buildMOSHCommand(ct *commands.Context) (cmd []string, err error) {
moshPath, err := exec.LookPath("mosh-server")
if err != nil {
fmt.Printf("Unable to find ssh on system: %s\n", err)
return
}
moshArguments := strings.Split(ct.FormattedArguments["client-arguments"], ",")
moshArguments = append(moshArguments, "-p", config.GetMOSHPortsRange(), "--")
cmd = []string{moshPath, "new"}
cmd = append(cmd, moshArguments...)
return
}
func (c *Interactive) promptPrefix() string {
return fmt.Sprintf("%s@%s$ ", c.Context.User.User.Username, config.GetSBName())
}
func (c *Interactive) promptExecutor(command string) {
// Check that user entered somthing
if command == "" {
return
}
// We read from input
commandLine, err := helpers.ParseCommandLine(command)
if err != nil {
fmt.Printf("error: %s", err)
return
}
commandLine = helpers.RegroupCommandArguments(commandLine)
if commandLine[0] == "exit" {
os.Exit(0)
}
if !commands.IsAPublicCommand(commandLine[0]) {
fmt.Println("command not found")
return
}
log := models.NewLog(c.Context.User.User.Username, []string{config.GetGlobalDatabasePath(), c.Context.User.GetLocalLogDatabasePath()}, commandLine)
err = commands.BuildAndExecuteSBCommand(log, c.Context.User, commandLine...)
if err != nil && err != types.ErrMissingArguments {
fmt.Printf("Error while executing command: %s\n", err)
}
log.SessionEndDate = time.Now()
log.Save()
}
func (c *Interactive) promptCompleter(d prompt.Document) (s []prompt.Suggest) {
// No completion if no characters
if d.TextBeforeCursor() == "" {
return s
}
// Do we have a command typed ?
// We check if line begins with a known command and a space
for commandName, commandFactory := range commands.GetCommands() {
if commands.IsAPublicCommand(commandName) && strings.HasPrefix(d.CurrentLine(), fmt.Sprintf("%s ", commandName)) {
// We have a known command
// We have to give the argument list to user
_, _, _, args := commandFactory()
for argName, arg := range args {
if !strings.Contains(d.CurrentLine(), fmt.Sprintf("--%s", argName)) {
argDesc := arg.Description
argDesc = strings.ReplaceAll(argDesc, "\t", " ")
argDesc = strings.ReplaceAll(argDesc, "\n", " ")
s = append(s, prompt.Suggest{Text: fmt.Sprintf("--%s", argName), Description: argDesc})
}
}
return prompt.FilterHasPrefix(s, d.GetWordBeforeCursor(), true)
}
}
// Let's give the completion tu user
for commandName, commandFactory := range commands.GetCommands() {
if commands.IsAPublicCommand(commandName) && strings.HasPrefix(commandName, d.TextBeforeCursor()) {
_, _, helper, _ := commandFactory()
s = append(s, prompt.Suggest{Text: commandName, Description: helper.Description})
}
}
sort.Slice(s, func(i, j int) bool {
return s[i].Text < s[j].Text
})
return prompt.FilterHasPrefix(s, d.TextBeforeCursor(), true)
}