-
Notifications
You must be signed in to change notification settings - Fork 0
/
messages.go
244 lines (204 loc) · 7.28 KB
/
messages.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package webmgmt
import (
"fmt"
"html"
)
var (
// NormalOutputColor color definition for normal output
NormalOutputColor = "white"
// ErrorOutputColor color definition for error output
ErrorOutputColor = "red"
)
// ServerMessage is the interface that all messages from the server to client will implement.
type ServerMessage interface {
Get() interface{}
}
// ServerMessageBase is the base structure all server to client messages will use.
type ServerMessageBase struct {
Type string `json:"type"`
}
// Get will return the ServerMessageBase
func (c *ServerMessageBase) Get() interface{} {
return c
}
// TextMessage is the struct for the server message that is sent to the client to tell the client to display text in the terminal window.
type TextMessage struct {
ServerMessageBase
Text string `json:"text"`
Color string `json:"color"`
}
// Get will return self
func (c *TextMessage) Get() interface{} {
return c
}
// RawTextMessage is the struct for the server message that is sent to the client to tell the client to display text as raw text in the terminal window.
type RawTextMessage struct {
ServerMessageBase
Text string `json:"text"`
}
// Get will return self
func (c *RawTextMessage) Get() interface{} {
return c
}
// Clickable is the struct for the server message that is sent to the client to tell the client to display clickable.
type Clickable struct {
ServerMessageBase
Commands []string `json:"commands"`
}
// Get will return self
func (c *Clickable) Get() interface{} {
return c
}
// Prompt is the struct for the server message that is sent to the client to tell the client what the prompt should be
type Prompt struct {
ServerMessageBase
Prompt string `json:"prompt"`
}
// Get will return self
func (c *Prompt) Get() interface{} {
return c
}
// HistoryMode is the struct for the server message that is sent to the client to tell the client to turn history saving on and off.
type HistoryMode struct {
ServerMessageBase
Val bool `json:"val"`
}
// Get will return self
func (c *HistoryMode) Get() interface{} {
return c
}
// Authenticated is the struct for the server message that is sent to the client to tell the client that is has been authenticated or not.
type Authenticated struct {
ServerMessageBase
Val bool `json:"val"`
}
// Get will return self
func (c *Authenticated) Get() interface{} {
return c
}
// Echo is the struct for the server message that is sent to the client to tell the client to turn echo on or off.
type Echo struct {
ServerMessageBase
Val bool `json:"val"`
}
// Get will return self
func (c *Echo) Get() interface{} {
return c
}
// Status is the struct for the server message that is sent to the client to tell the client to set the status bar to the text defined in the message.
type Status struct {
ServerMessageBase
Text string `json:"text"`
}
// Get will return self
func (c *Status) Get() interface{} {
return c
}
// AppendText will return a command packet that will append the text to the bottom of the output in the web terminal. This will format the text message in the color defined.
func AppendText(text, color string) ServerMessage {
something := &TextMessage{}
something.Type = "text"
something.Text = html.EscapeString(text)
something.Color = color
return something
}
// AppendRawText will return a command packet that will append the raw text to the bottom of the output in the web terminal
func AppendRawText(text string) ServerMessage {
something := &RawTextMessage{}
something.Type = "rawtext"
something.Text = text
return something
}
// ClickableCommands will return a command packet that will append the raw text to the bottom of the output in the web terminal
func ClickableCommands(commands []string) ServerMessage {
something := &Clickable{}
something.Type = "clickable"
something.Commands = commands
return something
}
// SetPrompt will return a command packet that will set the prompt in the web terminal.
func SetPrompt(prompt string) ServerMessage {
something := &Prompt{}
something.Type = "prompt"
something.Prompt = html.EscapeString(prompt)
return something
}
// SetHistoryMode will return a command packet that will notify the web terminal that should capture/not capture commands entered into the client's history/
func SetHistoryMode(val bool) ServerMessage {
something := &HistoryMode{}
something.Type = "history"
something.Val = val
return something
}
// SetAuthenticated will return a command packet that will notify the web terminal that the client has authenticated.
func SetAuthenticated(val bool) ServerMessage {
something := &Authenticated{}
something.Type = "authenticated"
something.Val = val
return something
}
// SetEchoOn will return a command packet that will set the echo state of text that is executed. Useful for disabling the display of the password entry in the auth process.
func SetEchoOn(val bool) ServerMessage {
something := &Echo{}
something.Type = "echo"
something.Val = val
return something
}
// Cls will return a command packet that will clear the current browser.
func Cls() ServerMessage {
something := &ServerMessageBase{}
something.Type = "cls"
return something
}
// SetStatus will return a command packet that will set the status in the html window. This is yet to be implemented on the client.
func SetStatus(text string) ServerMessage {
something := &Status{}
something.Type = "status"
something.Text = html.EscapeString(text)
return something
}
// Eval will return a command packet that will be evaluated on the client browser. The text is the javascript that will be evaluated.
func Eval(text string) ServerMessage {
something := &Status{}
something.Type = "eval"
something.Text = text
return something
}
// Link is used to format a string for a href tag
func Link(url, text string) string {
return fmt.Sprintf("<a href=\"%s\" target=\"_blank\">%s</a>", url, text)
}
// Color is used to a color string and return a string containing the color for a span.
func Color(color, text string) string {
if text == "" {
return html.EscapeString(text)
}
return fmt.Sprintf("<span style=\"color:%s\">%s</span>", color, html.EscapeString(text))
}
// Image is used to take several predefined fields and return a html snippet to display an image.
func Image(width, height int, src, alt string) string {
return fmt.Sprintf("<img width=\"%d\" height=\"%d\" src=\"%s\" alt=\"%s\"/>", width, height, src, alt)
}
// ClientMessage is the struct for the client message that is sent from the client to the server. nThe contents are passed to the
// HandleCommand func(c Client, cmd string) defined in the Config.
type ClientMessage struct {
Payload string `json:"payload"`
}
// AppendNormalText create a ServerMessage for TextMessage normal, format and args are used to create the text via Sprintf
func AppendNormalText(format string, a ...interface{}) ServerMessage {
text := fmt.Sprintf(format, a...)
something := &TextMessage{}
something.Type = "text"
something.Text = html.EscapeString(text)
something.Color = NormalOutputColor
return something
}
// AppendErrorText create a ServerMessage for TextMessage error, format and args are used to create the text via Sprintf
func AppendErrorText(format string, a ...interface{}) ServerMessage {
text := fmt.Sprintf(format, a...)
something := &TextMessage{}
something.Type = "text"
something.Text = html.EscapeString(text)
something.Color = ErrorOutputColor
return something
}