-
Notifications
You must be signed in to change notification settings - Fork 22
/
shell2telegram.go
404 lines (346 loc) · 12.6 KB
/
shell2telegram.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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"net/url"
"os"
"os/signal"
"strings"
"sync"
"time"
"github.com/msoap/raphanus"
tgbotapi "gopkg.in/telegram-bot-api.v2"
)
const (
// version - current version
version = "1.10"
// DefaultBotTimeout - bot default timeout
DefaultBotTimeout = 60
// MessagesQueueSize - size of channel for bot messages
MessagesQueueSize = 10
// MaxMessageLength - max length of one bot message
MaxMessageLength = 4096
// SecondsForAutoSaveUsersToDB - save users to file every 1 min (if need)
SecondsForAutoSaveUsersToDB = 60
// DBFileName - DB json name
DBFileName = "shell2telegram.json"
// shell2telegram command name for get plain text without /command
cmdPlainText = "/:plain_text"
)
// Command - one user command
type Command struct {
shellCmd string // shell command
description string // command description for list in /help (/cmd:desc="Command name")
vars []string // environment vars for user text, split by `/s+` to vars (/cmd:vars=SUBCOMMAND,ARGS)
isMarkdown bool // send message in markdown format
}
// Commands - list of all commands
type Commands map[string]Command
// Config - config struct
type Config struct {
token string // bot token
botTimeout int // bot timeout
predefinedAllowedUsers []string // telegram users who are allowed to chat with the bot
predefinedRootUsers []string // telegram users, who confirms new users in their private chat
description string // description of bot
bindAddr string // bind address to listen webhook requests
webhookURL url.URL // url for the webhook
usersDB string // file for store users
shell string // custom shell
cache int // caching command out (in seconds)
shTimeout int // timeout for execute shell command (in seconds)
addExit bool // adding /shell2telegram exit command
allowAll bool // allow all user (DANGEROUS!)
logCommands bool // logging all commands
persistentUsers bool // load/save users from file
isPublicBot bool // bot is public (don't add /auth* commands)
oneThread bool // run each shell commands in one thread
}
// message types
const (
msgIsText int8 = iota
msgIsPhoto
)
// BotMessage - record for send via channel for send message to telegram chat
type BotMessage struct {
message string
fileName string
photo []byte
chatID int
messageType int8
isMarkdown bool
}
// ----------------------------------------------------------------------------
// get config
func getConfig() (commands Commands, appConfig Config, err error) {
flag.StringVar(&appConfig.token, "tb-token", "", "setting bot `token` (or set TB_TOKEN variable)")
flag.BoolVar(&appConfig.addExit, "add-exit", false, "adding \"/shell2telegram exit\" command for terminate bot (for roots only)")
flag.IntVar(&appConfig.botTimeout, "timeout", DefaultBotTimeout, "setting timeout for bot (in `seconds`)")
flag.StringVar(&appConfig.bindAddr, "bind-addr", "", "bind address to listen webhook requests, like: `0.0.0.0:8080`")
flag.Var(&urlValue{&appConfig.webhookURL}, "webhook", "`url` of bot's webhook")
flag.BoolVar(&appConfig.allowAll, "allow-all", false, "allow all users (DANGEROUS!)")
flag.BoolVar(&appConfig.logCommands, "log-commands", false, "logging all commands")
flag.StringVar(&appConfig.description, "description", "", "setting description of bot")
flag.BoolVar(&appConfig.persistentUsers, "persistent-users", false, "load/save users from file (default ~/.config/shell2telegram.json)")
flag.StringVar(&appConfig.usersDB, "users-db", "", "`file` for store users")
flag.IntVar(&appConfig.cache, "cache", 0, "caching command out (in `seconds`)")
flag.BoolVar(&appConfig.isPublicBot, "public", false, "bot is public (don't add /auth* commands)")
flag.IntVar(&appConfig.shTimeout, "sh-timeout", 0, "set timeout for execute shell command (in `seconds`)")
flag.StringVar(&appConfig.shell, "shell", "sh", "custom shell or \"\" for execute without shell")
flag.BoolVar(&appConfig.oneThread, "one-thread", false, "run each shell command in one thread")
logFilename := flag.String("log", "", "log `filename`, default - STDOUT")
predefinedAllowedUsers := flag.String("allow-users", "", "telegram users who are allowed to chat with the bot (\"user1,user2\")")
predefinedRootUsers := flag.String("root-users", "", "telegram users, who confirms new users in their private chat (\"user1,user2\")")
showVersion := flag.Bool("version", false, "get version")
flag.Usage = func() {
fmt.Printf("usage: %s [options] %s\n%s\n%s\n\noptions:\n",
os.Args[0],
`/chat_command "shell command" /chat_command2 "shell command2"`,
"All text after /chat_command will be sent to STDIN of shell command.",
"If chat command is /:plain_text - get user message without any /command (for private chats only)",
)
flag.PrintDefaults()
os.Exit(0)
}
flag.Parse()
if *showVersion {
fmt.Println(version)
os.Exit(0)
}
// setup log file
if len(*logFilename) > 0 {
fhLog, err := os.OpenFile(*logFilename, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
if err != nil {
log.Fatalf("error opening log file: %v", err)
}
log.SetOutput(fhLog)
}
// setup users and roots
if *predefinedAllowedUsers != "" {
appConfig.predefinedAllowedUsers = strings.Split(*predefinedAllowedUsers, ",")
}
if *predefinedRootUsers != "" {
appConfig.predefinedRootUsers = strings.Split(*predefinedRootUsers, ",")
}
commands = Commands{}
// need >= 2 arguments and count of it must be even
args := flag.Args()
if len(args) < 2 || len(args)%2 == 1 {
return commands, appConfig, fmt.Errorf("error: need pairs of /chat-command and shell-command")
}
for i := 0; i < len(args); i += 2 {
path, command, err := parseBotCommand(args[i], args[i+1]) // (/path, shell_command)
if err != nil {
return commands, appConfig, err
}
commands[path] = command
}
if appConfig.token == "" {
if appConfig.token = os.Getenv("TB_TOKEN"); appConfig.token == "" {
return commands, appConfig, fmt.Errorf("TB_TOKEN environment var not found. See https://core.telegram.org/bots#botfather for more information")
}
}
return commands, appConfig, nil
}
// ----------------------------------------------------------------------------
func sendMessage(messageSignal chan<- BotMessage, chatID int, message []byte, isMarkdown bool) {
go func() {
var fileName string
fileType := http.DetectContentType(message)
switch fileType {
case "image/png":
fileName = "file.png"
case "image/jpeg":
fileName = "file.jpeg"
case "image/gif":
fileName = "file.gif"
case "image/bmp":
fileName = "file.bmp"
case "video/mp4":
// TODO: nedded migrate to new telegram-bot-api library
log.Printf("not supported")
return
default:
fileName = "message"
}
if fileName == "message" {
// is text message
messageString := string(message)
var messagesList []string
if len(messageString) <= MaxMessageLength {
messagesList = []string{messageString}
} else {
messagesList = splitStringLinesBySize(messageString, MaxMessageLength)
}
for _, messageChunk := range messagesList {
messageSignal <- BotMessage{
chatID: chatID,
messageType: msgIsText,
message: messageChunk,
isMarkdown: isMarkdown,
}
}
} else {
// is image
messageSignal <- BotMessage{
chatID: chatID,
messageType: msgIsPhoto,
fileName: fileName,
photo: message,
}
}
}()
}
// ----------------------------------------------------------------------------
func main() {
commands, appConfig, err := getConfig()
if err != nil {
log.Fatal(err)
}
bot, err := tgbotapi.NewBotAPI(appConfig.token)
if err != nil {
log.Fatal(err)
}
log.Printf("Authorized on bot account: @%s", bot.Self.UserName)
tgbotConfig := tgbotapi.NewUpdate(0)
tgbotConfig.Timeout = appConfig.botTimeout
var botUpdatesChan <-chan tgbotapi.Update
var server *http.Server
if appConfig.bindAddr != "" {
_, err = bot.SetWebhook(tgbotapi.WebhookConfig{URL: &appConfig.webhookURL})
if err != nil {
log.Fatal(err)
}
botUpdatesChan = bot.ListenForWebhook(appConfig.webhookURL.Path)
server = &http.Server{Addr: appConfig.bindAddr}
go func() {
log.Println("Listening incoming requests at ", appConfig.bindAddr)
log.Fatal(server.ListenAndServe())
}()
} else {
botUpdatesChan, err = bot.GetUpdatesChan(tgbotConfig)
if err != nil {
log.Fatal(err)
}
}
users := NewUsers(appConfig)
messageSignal := make(chan BotMessage, MessagesQueueSize)
vacuumTicker := time.Tick(SecondsForOldUsersBeforeVacuum * time.Second)
saveToBDTicker := make(<-chan time.Time)
oneThreadMutex := sync.Mutex{}
exitSignal := make(chan struct{})
systemExitSignal := make(chan os.Signal, 1)
signal.Notify(systemExitSignal, os.Interrupt)
if appConfig.persistentUsers {
saveToBDTicker = time.Tick(SecondsForAutoSaveUsersToDB * time.Second)
}
var cache raphanus.DB
if appConfig.cache > 0 {
cache = raphanus.New()
}
// all /shell2telegram sub-commands handlers
internalCommands := map[string]func(Ctx) string{
"stat": cmdShell2telegramStat,
"ban": cmdShell2telegramBan,
"search": cmdShell2telegramSearch,
"desc": cmdShell2telegramDesc,
"rm": cmdShell2telegramRm,
"exit": cmdShell2telegramExit,
"version": cmdShell2telegramVersion,
"broadcast_to_root": cmdShell2telegramBroadcastToRoot,
"message_to_user": cmdShell2telegramMessageToUser,
}
doExit := false
for !doExit {
select {
case telegramUpdate := <-botUpdatesChan:
var messageCmd, messageArgs string
allUserMessage := telegramUpdate.Message.Text
if len(allUserMessage) > 0 && allUserMessage[0] == '/' {
messageCmd, messageArgs = splitStringHalfBySpace(allUserMessage)
} else {
messageCmd, messageArgs = cmdPlainText, allUserMessage
}
allowPlainText := false
if _, ok := commands[cmdPlainText]; ok {
allowPlainText = true
}
replayMsg := ""
if len(messageCmd) > 0 && (messageCmd != cmdPlainText || allowPlainText) {
users.AddNew(telegramUpdate.Message)
userID := telegramUpdate.Message.From.ID
allowExec := appConfig.allowAll || users.IsAuthorized(userID)
ctx := Ctx{
appConfig: &appConfig,
users: &users,
commands: commands,
userID: userID,
allowExec: allowExec,
messageCmd: messageCmd,
messageArgs: messageArgs,
messageSignal: messageSignal,
chatID: telegramUpdate.Message.Chat.ID,
exitSignal: exitSignal,
cache: &cache,
oneThreadMutex: &oneThreadMutex,
}
switch {
// commands .................................
case !appConfig.isPublicBot && (messageCmd == "/auth" || messageCmd == "/authroot"):
replayMsg = cmdAuth(ctx)
case messageCmd == "/help":
replayMsg = cmdHelp(ctx)
case messageCmd == "/shell2telegram" && users.IsRoot(userID):
var messageSubCmd string
messageSubCmd, messageArgs = splitStringHalfBySpace(messageArgs)
ctx.messageArgs = messageArgs
if cmdHandler, ok := internalCommands[messageSubCmd]; ok {
replayMsg = cmdHandler(ctx)
} else {
replayMsg = "Sub-command not found"
}
case allowExec && (allowPlainText && messageCmd == cmdPlainText || messageCmd[0] == '/'):
cmdUser(ctx)
} // switch for commands
if appConfig.logCommands {
log.Printf("%s: %s", users.String(userID), allUserMessage)
}
sendMessage(messageSignal, telegramUpdate.Message.Chat.ID, []byte(replayMsg), false)
}
case botMessage := <-messageSignal:
switch {
case botMessage.messageType == msgIsText && !stringIsEmpty(botMessage.message):
messageConfig := tgbotapi.NewMessage(botMessage.chatID, botMessage.message)
if botMessage.isMarkdown {
messageConfig.ParseMode = tgbotapi.ModeMarkdown
}
_, err = bot.Send(messageConfig)
case botMessage.messageType == msgIsPhoto && len(botMessage.photo) > 0:
bytesPhoto := tgbotapi.FileBytes{Name: botMessage.fileName, Bytes: botMessage.photo}
_, err = bot.Send(tgbotapi.NewPhotoUpload(botMessage.chatID, bytesPhoto))
}
if err != nil {
log.Printf("failed to send message: %s", err)
}
case <-saveToBDTicker:
users.SaveToDB(appConfig.usersDB)
case <-vacuumTicker:
users.ClearOldUsers()
case <-systemExitSignal:
go func() {
exitSignal <- struct{}{}
}()
case <-exitSignal:
if appConfig.persistentUsers {
users.needSaveDB = true
users.SaveToDB(appConfig.usersDB)
}
if server != nil {
log.Println(server.Close())
}
doExit = true
}
}
}