-
Notifications
You must be signed in to change notification settings - Fork 0
/
globals.go
135 lines (114 loc) · 2.9 KB
/
globals.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
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
"math/rand"
"os"
"regexp"
"strings"
"time"
"github.com/codeskyblue/go-sh"
"github.com/davecgh/go-spew/spew"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
)
const (
DEFAULT_EXEC_TIMEOUT = 10
DEFAULT_SEND_INTERVAL = 1
DEFAULT_SEND_PAGE_SIZE = 4000
DEFAULT_SEND_MAX_PAGES = 3
RECONNECT_INTERVAL = 15 * time.Second
CONNKEY_SIZE = 9
USE_SPEW = true
)
const (
ACL_ANY = "" // pseudo rule means 'anyone can'. not added to allowlist
ACL_ALL = "*" // pseudo rule means "All rules for this user
ACL_INFORM = "inform"
ACL_UTIL = "util"
ACL_SUPERVISE = "supervise"
ACL_EXEC = "exec"
ACL_SCROT = "scrot"
ACL_ADMIN = "admin"
ACL_FILES = "files"
ACL_HALT = "halt"
ACL_LOCK = "lock"
)
type ACLEntry struct {
Id int `json:"id"`
Name string `json:"name"`
Allow []string `json:"allow"`
}
func (e *ACLEntry) String() string {
if e == nil {
return "<nil>"
}
return fmt.Sprintf("[%v]%v", e.Id, e.Name)
}
type ExecConfig struct {
Timeout int `json:"timeout" yaml:"timeout"`
Interval int `json:"interval" yaml:"interval"`
PageSize int `json:"page_size" yaml:"page_size"`
MaxPages int `json:"max_pages" yaml:"max_pages"`
}
type Config struct {
Token string `json:"token" yaml:"token"`
Master int `json:"master" yaml:"master"`
Users []ACLEntry `json:"users" yaml:"users"`
Shell string `json:"shell" yaml:"shell"`
Data_Dir string `json:"datadir" yaml:"datadir"`
Display string `jdon:"display" yaml:"display"`
Exec ExecConfig `json:"exec" yaml:"exec"`
Host string `json:"-" yaml:"-"`
}
type HandlerProc func(m *tgbotapi.Message)
type Handler struct {
proc HandlerProc
info string
perm string
}
var handlers map[string]Handler = make(map[string]Handler)
var config Config
var bot *tgbotapi.BotAPI
var job_counter int
var shell *sh.Session
var connect_key string
var exitcode int
var sigchan chan os.Signal
func ppj(v interface{}) string {
if USE_SPEW {
return spew.Sdump(v)
} else {
b, _ := json.MarshalIndent(v, "", " ")
return string(b)
}
}
func random_string(n int) string {
count := (n + 3) * 3 / 4
key := make([]byte, n)
for i := 0; i < count; i++ {
key[i] = byte(rand.Int() & 0xFF)
}
return base64.StdEncoding.EncodeToString(key)[:n]
}
func generate_key() {
connect_key = random_string(CONNKEY_SIZE)
}
var splitRx *regexp.Regexp = regexp.MustCompile(`(?sm)\A\s*([\S]+)\s*(.*)\z`)
func Split2(text string) (token, rest string) {
v := splitRx.FindStringSubmatch(text)
if v == nil {
return "", ""
}
return v[1], v[2]
}
func GetFirstToken(text string) string {
token, _ := Split2(text)
return token
}
func isMaster(id int) bool {
return config.Master == id
}
func addHandler(name string, proc HandlerProc, info string, perm string) {
handlers[strings.ToUpper(name)] = Handler{proc, info, perm}
}