-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathterminal.go
544 lines (519 loc) · 13.7 KB
/
terminal.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
// terminal
package main
import (
"errors"
"fmt"
"io"
"log"
"os"
"regexp"
"runtime"
"strings"
"time"
"bytes"
"flag"
"net"
"net/http"
"os/exec"
"path/filepath"
"encoding/base64"
"crypto/md5"
"encoding/hex"
"github.com/gorilla/websocket"
"github.com/axgle/mahonia"
)
var (
portPointer *int
shellPointer *string
codePointer *string
passPointer *string
staticPointer *bool
)
const TAG_ERR = "swt_error:"
const TAG_LOGIN = "swt_login:"
var isConnected bool = false
var isLogged bool = false
var isStatic bool = true
var serverPort int = 7777
var passwordHash string = ""
var userPassword string = ""
var userCoding string = ""
var userShell string = ""
var userDecoder mahonia.Decoder = nil
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
var FORBIDDEN_COMMANDS = []string{
"reboot",
"poweroff",
"ssh",
"bash",
"powershell",
"sh",
}
var AVAILABLE_SHELLS = []string{
"bash",
"sh",
"powershell",
}
func readId(r *http.Request) string {
IPAddress := r.Header.Get("X-Real-Ip")
if IPAddress == "" {
IPAddress = r.Header.Get("X-Forwarded-For")
}
if IPAddress == "" {
IPAddress = r.RemoteAddr
}
return IPAddress
}
func getMd5(text string) string {
hash := md5.Sum([]byte(text))
return hex.EncodeToString(hash[:])
}
const TERMIAL_CONTENT = `%s` // [M[ FILE_PLAIN | ./terminal.html ]M]
func getHelp() string {
help := "\ncommands (%s):\n" // [M[ LINE_CALLBACK | VERSION_NAME ]M]
help += "💁🏻 [ help ] print this help info again\n"
help += "🙅♂️ [ stop ] stop current terminal program\n"
help += "💾 [ save ] save current terminal content\n"
help += "✂️ [ exit ] exit current websocket connection\n"
help += "🔨 [ shell ] get/set current shell name\n"
help += "👁️ [ coding ] get/set current encoding name\n"
help += "📁 [ static ] open tab for static server\n"
help += "🚃 [ scp ./ ] upload file to current directory\n"
help += "🚋 [ scp ./test.txt ] download file from target path\n"
help += "📝 [ vim ./test.txt ] edit file with simple editor\n"
return help
}
func setShell(name string) bool {
command := exec.Command(name, "-c", "ls")
_, err := command.Output()
if err == nil {
userShell = name
return true
} else {
return false
}
}
func setCoding(name string) bool {
decoder := mahonia.NewDecoder(name)
if decoder != nil {
userCoding = name
userDecoder = decoder
return true
} else {
return false
}
}
func getPath(name string) string {
path, err := os.Getwd()
length := len(name)
if err != nil {
return ""
}
if (length == 1 && string(name[0]) == "~") || (length > 1 && string(name[1]) == ":") || string(name[0]) == "/" {
path = name
} else {
path = filepath.Join(path, name)
}
return path
}
func changeDirectory(name string) string {
dir := getPath(name)
err := os.Chdir(dir)
if err != nil {
return err.Error()
}
dir, err = os.Getwd()
if err != nil {
return err.Error()
}
return dir
}
func executeCommand(input string, conn *websocket.Conn) string {
log.Println("executing:" + input)
args := strings.Fields(input)
cmd := args[0]
//
if cmd == "login" {
if len(args) != 2 {
return TAG_ERR + "invalid argument count for " + cmd
} else if isLogged {
return TAG_ERR + "invalid state, try later!"
} else if (passwordHash != args[1]) {
return TAG_ERR + "invalid password, try again!"
}
isLogged = true
return TAG_LOGIN + "finish"
}
//
for _, name := range FORBIDDEN_COMMANDS {
if cmd == name {
return TAG_ERR + "command not supported!"
}
}
//
if cmd == "exit" {
conn.Close()
return "exit!"
}
//
if cmd == "help" {
return getHelp()
}
//
if cmd == "stop" {
conn.WriteMessage(1, []byte("program stopping ..."))
os.Exit(0)
return "exit!"
}
//
if cmd == "save" {
return "swt_save:"
}
//
if cmd == "shell" {
if len(args) == 1 {
return userShell
} else if len(args) != 2 {
return TAG_ERR + "invalid argument count for " + cmd
}
isOk := setShell(args[1])
if isOk {
return "current shell changed ..."
} else {
return TAG_ERR + "invalid encoding name ..."
}
}
//
if cmd == "coding" {
if len(args) == 1 {
return userCoding
} else if len(args) != 2 {
return TAG_ERR + "invalid argument count for " + cmd
}
isOk := setCoding(args[1])
if isOk {
return "current encoding changed ..."
} else {
return TAG_ERR + "invalid encoding name ..."
}
}
//
if cmd == "static" {
if !isStatic {
return TAG_ERR + "static server not started ..."
}
return "swt_static:"
}
//
if cmd == "scp" {
if len(args) != 2 {
return TAG_ERR + "invalid argument count for " + cmd
}
path := args[1]
path = getPath(path)
info, err := os.Stat(path)
if err != nil && errors.Is(err, os.ErrNotExist) {
return TAG_ERR + "path not exist"
}
flag := base64.StdEncoding.EncodeToString([]byte(path))
if info.IsDir() {
return "swt_upload:" + flag
} else {
return "swt_download:" + flag
}
}
//
if cmd == "vim" || cmd == "vi" {
if len(args) != 2 {
return TAG_ERR + "invalid argument count for " + cmd
}
path := args[1]
path = getPath(path)
info, err := os.Stat(path)
if err != nil && !errors.Is(err, os.ErrNotExist) {
log.Println(err)
return err.Error()
}
if info != nil && info.IsDir() {
return TAG_ERR + "cannot edit a directory!"
}
if info != nil && info.Size() > 1 * 1024 * 1024 {
return TAG_ERR + "file too large, cannot edit!"
}
flag := base64.StdEncoding.EncodeToString([]byte(path))
return "swt_edit:" + flag
}
//
if cmd == "cd" || cmd == "chdir" {
if len(args) != 2 {
return TAG_ERR + "invalid argument count for " + cmd
}
return changeDirectory(args[1])
}
//
command := exec.Command(userShell, "-c", input)
var error bytes.Buffer
command.Stderr = &error
stdout, err := command.Output()
if err != nil {
return TAG_ERR + err.Error() + "," + userDecoder.ConvertString(error.String())
}
decoded := userDecoder.ConvertString(string(stdout))
return strings.TrimSpace(decoded)
}
func handleTerminal(w http.ResponseWriter, r *http.Request) {
log.Println("connecting:" + readId(r))
time.Sleep(100 * time.Millisecond)
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
return
}
if err != nil {
log.Println(err)
return
}
if isConnected {
log.Println("refused!")
conn.WriteMessage(1, []byte(TAG_ERR + "connecting with another terminal, please wait or close previous terminals!"))
conn.Close()
return
} else {
log.Println("connected!")
isConnected = true
isLogged = len(userPassword) == 0
}
if isLogged {
conn.WriteMessage(1, []byte(TAG_LOGIN + "ignore"))
} else {
conn.WriteMessage(1, []byte(TAG_LOGIN + "start"))
}
for {
messageType, bytes, err := conn.ReadMessage()
if err != nil {
log.Println(err)
isConnected = false
isLogged = false
return
}
input := string(bytes[:])
output := executeCommand(input, conn)
err = conn.WriteMessage(messageType, []byte(output))
if err != nil {
log.Println(err)
isConnected = false
isLogged = false
return
}
}
}
func handleDownload(w http.ResponseWriter, r *http.Request) {
flag := r.URL.Query().Get("flag")
arr,err := base64.StdEncoding.DecodeString(flag)
if err != nil {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
fmt.Fprint(w, "err: flag not found ...")
return
}
path := string(arr)
info, err := os.Stat(path);
if err != nil && errors.Is(err, os.ErrNotExist) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
fmt.Fprint(w, "err: file not found ...")
} else if err != nil && !errors.Is(err, os.ErrNotExist) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
fmt.Fprint(w, err.Error())
} else if info.IsDir() {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
fmt.Fprint(w, "err: cannot download a directory!")
} else {
log.Println("downloading:" + path)
w.Header().Set("Content-Disposition", "attachment; filename="+filepath.Base(path))
w.Header().Set("Content-Type", "application/octet-stream")
http.ServeFile(w, r, path)
}
}
func handleUpload(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
fmt.Fprint(w, "upload...")
} else if r.Method == "POST" {
flag := r.URL.Query().Get("flag")
arr,err := base64.StdEncoding.DecodeString(flag)
if err != nil {
fmt.Fprint(w, "err: flag not found ...")
return
}
path := string(arr)
info, _ := os.Stat(path);
if err != nil && errors.Is(err, os.ErrNotExist) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
fmt.Fprint(w, "err: directory not found ...")
return
} else if err != nil && !errors.Is(err, os.ErrNotExist) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
fmt.Fprint(w, err.Error())
return
} else if !info.IsDir() {
fmt.Fprint(w, "err: cannot upload to file!")
return
}
r.Body = http.MaxBytesReader(w, r.Body, 10 * 1024 * 1024)
file, handler, err := r.FormFile("fileUpload")
if err != nil {
log.Println(err)
fmt.Fprint(w, err.Error())
return
}
defer file.Close()
log.Println("uploading:" + handler.Filename)
target, err := os.OpenFile(path + "/" + handler.Filename, os.O_WRONLY | os.O_CREATE, 0777)
if err != nil {
log.Println(err)
fmt.Fprint(w, err.Error())
return
}
defer target.Close()
_, err = io.Copy(target, file)
if err != nil {
log.Println(err)
fmt.Fprint(w, err.Error())
return
}
log.Println("uploaded:" + handler.Filename)
fmt.Fprint(w, "successful...")
}
}
func handleEdit(w http.ResponseWriter, r *http.Request) {
flag := r.URL.Query().Get("flag")
arr,err := base64.StdEncoding.DecodeString(flag)
if err != nil {
fmt.Fprint(w, "err: flag not found ...")
return
}
path := string(arr)
info, err := os.Stat(path)
if err != nil && !errors.Is(err, os.ErrNotExist) {
log.Println(err)
fmt.Fprint(w, err.Error())
return
}
if info != nil && info.IsDir() {
fmt.Fprint(w, "err: cannot edit a directory!")
return
}
if r.Method == "GET" {
if errors.Is(err, os.ErrNotExist) {
fmt.Fprint(w, "")
} else {
http.ServeFile(w, r, path)
}
} else if r.Method == "POST" {
if err := r.ParseMultipartForm(0); err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, err.Error())
return
}
log.Println("editing:" + path)
text := r.FormValue("textEdit")
target, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0777)
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, err.Error())
return
}
defer target.Close()
if _, err = target.WriteString(text); err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, err.Error())
return
}
log.Println("edited:" + path)
fmt.Fprint(w, "saved!")
}
}
func handleRequest(w http.ResponseWriter, r *http.Request) {
log.Println("responsing:" + readId(r))
time.Sleep(100 * time.Microsecond)
w.Header().Set("Content-Type", "text/html; charset=utf-8")
if r.URL.Path == "/" {
http.ServeFile(w, r, "terminal.html") // [M[ LINE_REFPLACE | fmt.Fprint(w, TERMIAL_CONTENT) ]M]
} else {
w.WriteHeader(http.StatusNotFound)
}
}
func parseArgs() {
flag.Parse()
serverPort = *portPointer;
shellName := *shellPointer;
codingName := *codePointer;
passValue := *passPointer;
isStatic = *staticPointer;
//
if len(userShell) == 0 {
setShell(shellName)
}
for _, name := range AVAILABLE_SHELLS {
if len(userShell) == 0 { setShell(name) }
}
//
if len(codingName) > 0 {
setCoding(codingName)
}
if len(userCoding) == 0 {
setCoding("utf8")
}
//
if len(passValue) > 0 {
userPassword = passValue
}
regexPattern, _ := regexp.Compile(`\s+`)
userPassword = regexPattern.ReplaceAllString(strings.TrimSpace(userPassword), "")
passwordHash = getMd5(userPassword)
}
func init() {
portPointer = flag.Int("port", serverPort, "terminal serving port")
shellPointer = flag.String("shell", userShell, "set default shell")
codePointer = flag.String("coding", userCoding, "set output encoding")
passPointer = flag.String("pass", userPassword, "set terminal password")
staticPointer = flag.Bool("static", isStatic, "start static server")
}
func main() {
fmt.Println("")
log.Println("♚ Welcome To Simple Web Terminal!")
parseArgs()
log.Println(fmt.Sprintf("☛ pass: [%s]", userPassword))
log.Println("☛ date:", time.Now())
log.Println("☛ plat:", runtime.GOOS)
log.Println("☛ arch:", runtime.GOARCH)
log.Println("☛ port:", serverPort)
log.Println("☛ shell:", userShell)
log.Println("☛ coding:", userCoding)
//
if isStatic {
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./"))))
}
//
http.HandleFunc("/terminal", handleTerminal)
http.HandleFunc("/download", handleDownload)
http.HandleFunc("/upload", handleUpload)
http.HandleFunc("/edit", handleEdit)
http.HandleFunc("/", handleRequest)
//
ip := "127.0.0.1"
conn, err := net.Dial("udp", "8.8.8.8:80")
if err == nil {
ip = conn.LocalAddr().(*net.UDPAddr).IP.String()
}
conn.Close()
//
log.Println("☛ addr:", fmt.Sprintf("http:%s:%d", ip, serverPort))
log.Println("☛ addr:", fmt.Sprintf("http:127.0.0.1:%d", serverPort))
log.Println("running...")
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", serverPort), nil))
}