-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.go
161 lines (137 loc) · 3.82 KB
/
commands.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
package server
import (
"bytes"
"io"
"io/ioutil"
"log"
"math/rand"
"os"
"strconv"
"strings"
"github.com/spf13/viper"
"github.com/zyxar/image2ascii/ascii"
)
func randSeq(n int) string {
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
// UploadFile uploads a file to the server
func UploadFile(c Client, filename string) error {
f, err := os.Create(MakePathFromStringStack(c.Stack()) + filename)
if err != nil {
return err
}
defer f.Close()
io.Copy(f, c.Connection())
return nil
}
// SendASCIIPic sends an image as ascii text to the client.
func SendASCIIPic(c Client, path string) error {
f, err := os.Open(MakePathFromStringStack(c.Stack()) + path)
if err != nil {
log.Println(err)
return err
}
defer f.Close()
opt := ascii.Options{
Width: viper.GetInt("pic.x"),
Height: viper.GetInt("pic.y"),
Color: viper.GetBool("pic.color"),
Invert: false,
Flipx: false,
Flipy: false}
a, err := ascii.Decode(f, opt)
if err != nil {
log.Println(err)
return err
}
_, err = a.WriteTo(c.Connection())
return err
}
// GetFile sends the file to the client and returns true if it succeeds and false otherwise.
// it also returns the total number of send bytes.
func GetFile(c Client, path string) (int64, error) {
fileName, sanitized := sanitizeFilePath(path)
if sanitized {
return 0, ErrSlashNotAllowed
}
file, err := os.Open(MakePathFromStringStack(c.Stack()) + fileName)
if err != nil {
log.Println(err.Error())
return 0, err
}
defer file.Close()
totalSend, err := io.Copy(c.Connection(), file)
return totalSend, err
}
func sanitizeFilePath(path string) (string, bool) {
var fileName string
var sanitized bool
// Make sure the user can't request any files on the system.
lastForwardSlash := strings.LastIndex(path, "/")
if lastForwardSlash != -1 {
// Eliminate the last forward slash i.e ../../asdas will become asdas
fileName = path[lastForwardSlash+1:]
sanitized = true
} else {
fileName = path
sanitized = false
}
return fileName, sanitized
}
// ListFiles list the files from path and sends them to the connection
func ListFiles(c Client) error {
files, err := ioutil.ReadDir(MakePathFromStringStack(c.Stack()))
if err != nil {
return err
}
buffer := bytes.NewBufferString("Directory Mode Size LastModified Name\n")
for _, f := range files {
buffer.WriteString(strconv.FormatBool(f.IsDir()) + " " + string(f.Mode().String()) + " " +
strconv.FormatInt(f.Size(), 10) + " " + f.ModTime().String() + " " + string(f.Name()) + " " + "\n")
}
_, err = c.Connection().Write(buffer.Bytes())
return err
}
// ClearScreen cleans the client's screen by sending clear to the terminal.
func ClearScreen(c Client) error {
// Ansi clear: 1b 5b 48 1b 5b 4a
// clear | hexdump -C
var b = []byte{0x1b, 0x5b, 0x48, 0x1b, 0x5b, 0x4a}
_, err := c.Connection().Write(b)
return err
}
// ChangeDirectoryCommand changes the directory to the given directory
func ChangeDirectoryCommand(c Client, directory string) error {
var err error
path, sanitized := sanitizeFilePath(directory)
if sanitized {
return ErrSlashNotAllowed
}
if path == "." {
err = nil
} else if path == ".." {
err = ChangeDirectoryToPrevious(c.Stack())
} else {
err = ChangeDirectory(c.Stack(), path)
}
return err
}
// ShowHelp writes the help text to the client.
func ShowHelp(c Client) error {
var helpText = `
The available commands are:
get <filename> - Download the requested filename.
ls - List the files in the current directory.
cd - Changes the directory.
clear - Clear the screen.
exit - Close the connection with the server.c
pic - Returns the ascii art of an image. :-)
`
_, err := c.Connection().Write([]byte(helpText))
return err
}