-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathutils.go
327 lines (285 loc) · 8.2 KB
/
utils.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
package main
import (
"context"
"crypto/rand"
"encoding/base64"
"fmt"
"io"
"log"
"net/url"
"os"
"os/exec"
"regexp"
"runtime"
"strconv"
"strings"
"time"
shellwords "github.com/mattn/go-shellwords"
"github.com/msoap/raphanus"
raphanuscommon "github.com/msoap/raphanus/common"
)
// codeBytesLength - length of random code in bytes
const codeBytesLength = 15
// exec shell commands with text to STDIN
func execShell(shellCmd, input string, varsNames []string, userID, chatID int, userName, userDisplayName string, cache *raphanus.DB, cacheTTL int, config *Config) (result []byte) {
cacheKey := shellCmd + "/" + input
if cacheTTL > 0 {
if cacheData, err := cache.GetBytes(cacheKey); err != raphanuscommon.ErrKeyNotExists && err != nil {
log.Printf("get from cache failed: %s", err)
} else if err == nil {
// cache hit
return cacheData
}
}
shell, params, err := getShellAndParams(shellCmd, config.shell, runtime.GOOS == "windows")
if err != nil {
log.Print("parse shell failed: ", err)
return nil
}
ctx := context.Background()
if config.shTimeout > 0 {
var cancelFn context.CancelFunc
ctx, cancelFn = context.WithTimeout(ctx, time.Duration(config.shTimeout)*time.Second)
defer cancelFn()
}
osExecCommand := exec.CommandContext(ctx, shell, params...) // #nosec
osExecCommand.Stderr = os.Stderr
// copy variables from parent process
osExecCommand.Env = append(osExecCommand.Env, os.Environ()...)
if input != "" {
if len(varsNames) > 0 {
// set user input to shell vars
arguments := regexp.MustCompile(`\s+`).Split(input, len(varsNames))
for i, arg := range arguments {
osExecCommand.Env = append(osExecCommand.Env, fmt.Sprintf("%s=%s", varsNames[i], arg))
}
} else {
var stdin io.WriteCloser
errExec := errChain(func() (err error) {
stdin, err = osExecCommand.StdinPipe()
return err
}, func() error {
_, err = io.WriteString(stdin, input)
return err
}, func() error {
return stdin.Close()
})
if errExec != nil {
log.Print("get STDIN error: ", err)
}
}
}
// set S2T_* env vars
s2tVariables := [...]struct{ name, value string }{
{"S2T_LOGIN", userName},
{"S2T_USERID", strconv.Itoa(userID)},
{"S2T_USERNAME", userDisplayName},
{"S2T_CHATID", strconv.Itoa(chatID)},
}
for _, row := range s2tVariables {
osExecCommand.Env = append(osExecCommand.Env, fmt.Sprintf("%s=%s", row.name, row.value))
}
shellOut, err := osExecCommand.Output()
if err != nil {
log.Print("exec error: ", err)
result = []byte(fmt.Sprintf("exec error: %s", err))
} else {
result = shellOut
}
if cacheTTL > 0 {
if err := cache.SetBytes(cacheKey, result, cacheTTL); err != nil {
log.Printf("set to cache failed: %s", err)
}
}
return result
}
// errChain - handle errors on few functions
func errChain(chainFuncs ...func() error) error {
for _, fn := range chainFuncs {
if err := fn(); err != nil {
return err
}
}
return nil
}
// return 2 strings, second="" if string don't contain space
func splitStringHalfBySpace(str string) (one, two string) {
array := regexp.MustCompile(`\s+`).Split(str, 2)
one, two = array[0], ""
if len(array) > 1 {
two = array[1]
}
return one, two
}
// cleanUserName - remove @ from telegram username
func cleanUserName(in string) string {
return regexp.MustCompile("@").ReplaceAllLiteralString(in, "")
}
// getRandomCode - generate random code for authorize user
func getRandomCode() string {
buffer := make([]byte, codeBytesLength)
_, err := rand.Read(buffer)
if err != nil {
log.Fatalf("Get code error: %s", err)
}
return base64.URLEncoding.EncodeToString(buffer)
}
// parseBotCommand - parse command-line arguments for one bot command
func parseBotCommand(pathRaw, shellCmd string) (path string, command Command, err error) {
if len(pathRaw) == 0 || pathRaw[0] != '/' {
return "", command, fmt.Errorf("error: path %s don't starts with /", pathRaw)
}
if stringIsEmpty(shellCmd) {
return "", command, fmt.Errorf("error: shell command cannot be empty")
}
parseAttrFn := func(varsParts []string) (command Command, err error) {
for _, oneVar := range varsParts {
oneVarParts := regexp.MustCompile("=").Split(oneVar, 2)
if len(oneVarParts) == 1 && oneVarParts[0] == "md" {
command.isMarkdown = true
} else if len(oneVarParts) != 2 {
err = fmt.Errorf("error: parse command modificators: %s", oneVar)
return
} else if oneVarParts[0] == "desc" {
command.description = oneVarParts[1]
if command.description == "" {
err = fmt.Errorf("error: command description cannot be empty")
return
}
} else if oneVarParts[0] == "vars" {
command.vars = regexp.MustCompile(",").Split(oneVarParts[1], -1)
for _, oneVarName := range command.vars {
if oneVarName == "" {
err = fmt.Errorf("error: var name cannot be empty")
return
}
}
} else {
err = fmt.Errorf("error: parse command modificators, not found %s", oneVarParts[0])
return
}
}
return command, nil
}
pathParts := regexp.MustCompile(":").Split(pathRaw, -1)
switch {
case len(pathParts) == 1:
// /, /cmd
path = pathParts[0]
case pathParts[0] == "/" && regexp.MustCompile("^(plain_text|image)$").MatchString(pathParts[1]):
// /:plain_text, /:image, /:plain_text:desc=name
path = "/:" + pathParts[1]
if pathParts[1] == "image" {
return "", command, fmt.Errorf("/:image not implemented")
}
if len(pathParts) > 2 {
command, err = parseAttrFn(pathParts[2:])
}
case len(pathParts) > 1:
// commands with modificators :desc, :vars
path = pathParts[0]
command, err = parseAttrFn(pathParts[1:])
}
if err != nil {
return "", command, err
}
command.shellCmd = shellCmd
return path, command, nil
}
// stringIsEmpty - check string is empty
func stringIsEmpty(str string) bool {
isEmpty, _ := regexp.MatchString(`^\s*$`, str)
return isEmpty
}
// split string by chunks less maxSize size (whole rows)
func splitStringLinesBySize(input string, maxSize int) []string {
result := []string{}
parts := regexp.MustCompile("\n").Split(input, -1)
chunks := []string{parts[0]}
chunkSize := len(parts[0])
for _, part := range parts[1:] {
// current + "\n" + next > maxSize
if chunkSize+1+len(part) > maxSize {
result = append(result, strings.Join(chunks, "\n"))
chunks = []string{part}
chunkSize = len(part)
} else {
chunks = append(chunks, part)
chunkSize += 1 + len(part)
}
}
if len(chunks) > 0 {
result = append(result, strings.Join(chunks, "\n"))
}
return result
}
// create dir if it is not exists
func createDirIfNeed(dir string) {
if _, err := os.Stat(dir); err != nil {
err = os.MkdirAll(dir, 0700)
if err != nil {
log.Fatal("create dir error:", dir)
}
}
}
// get home dir
func getOsUserHomeDir() string {
homeDir := os.Getenv("HOME")
if runtime.GOOS == "windows" {
homeDir = os.Getenv("APPDATA")
}
return homeDir
}
// read default or user db file name
func getDBFilePath(usersDBFile string, needCreateDir bool) (fileName string) {
if usersDBFile == "" {
dirName := getOsUserHomeDir() + string(os.PathSeparator) + ".config"
if needCreateDir {
createDirIfNeed(dirName)
}
fileName = dirName + string(os.PathSeparator) + DBFileName
} else {
fileName = usersDBFile
}
return fileName
}
// ------------------------------------------------------------------
// getShellAndParams - get default shell and command
func getShellAndParams(cmd string, customShell string, isWindows bool) (shell string, params []string, err error) {
shell, params = "sh", []string{"-c", cmd}
if isWindows {
shell, params = "cmd", []string{"/C", cmd}
}
// custom shell
switch {
case customShell != "sh" && customShell != "":
shell = customShell
case customShell == "":
cmdLine, err := shellwords.Parse(cmd)
if err != nil {
return shell, params, fmt.Errorf("failed to parse %q: %s", cmd, err)
}
shell, params = cmdLine[0], cmdLine[1:]
}
return shell, params, nil
}
// ------------------------------
type urlValue struct {
URL *url.URL
}
func (v urlValue) String() string {
if v.URL != nil {
return v.URL.String()
}
return ""
}
func (v urlValue) Set(s string) error {
u, err := url.Parse(s)
if err != nil {
return err
} else if u.Scheme == "" || u.Host == "" {
return fmt.Errorf("missing host or scheme in '%s'", s)
}
*v.URL = *u
return nil
}