-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetting.go
More file actions
192 lines (172 loc) · 4.52 KB
/
setting.go
File metadata and controls
192 lines (172 loc) · 4.52 KB
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
package main
import (
"fmt"
"golang.org/x/net/proxy"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
)
const HelpMsg = `
Supported command:
/help -- print this message
/on -- the bot will delete outdated messages
/off -- the bot will be disabled
/timeout -- new timeout after which the messages will be deleted
/delete -- delete all messages
/setting -- print current settings
/stop -- !!! Delete all messages, delete settings and stop the bot !!!
Timeout format:
Timeout is set in the format: <decimal><unit suffix>
unit suffix one of "s", "m", "h"
Example: 1h15m, 24h, 30m, 60s, 10h30m15s
`
const StartMsg = `
Instructions to get started:
1. Add bot to group
2. Give him admin rights to delete messages
3. In the group send a command to the bot /on to run
4. You can change the timeout setting
5. To get the list command send /help
`
// bot setting type
type botSetting struct {
botToken string
botDebug bool
gcTimeout time.Duration
dbRedisAddress string
dbRedisDB int
dbRedisPassword string
useSocksProxy bool
socksParams struct {
socksAddress string
socksUser string
socksPassword string
}
timeoutLimit int
// todo:
//useHTTPSProxy bool
//httpsParams struct{
// httpsAddress string
// httpsUser string
// httpsPassword string
//}
}
func (s botSetting) String() string {
return fmt.Sprint("botDebug:", s.botDebug,
", gcTimeout:", int(s.gcTimeout),
", dbRedisAddress:", s.dbRedisAddress,
", dbRedisDB:", s.dbRedisDB,
", useSocksProxy:", s.useSocksProxy,
", socksAddress:", s.socksParams.socksAddress,
", socksUser:", s.socksParams.socksUser,
", timeoutLimit:", s.timeoutLimit)
}
// parsing and create setting
func parseSetting(rawData map[string]string) *botSetting {
var setting botSetting
// if token not set
if _, ok := rawData["gc_token"]; !ok {
log.Fatal("Bot token not set!")
} else {
setting.botToken = rawData["gc_token"]
}
for key, value := range rawData {
switch key {
case "gc_bot_debug":
debug, err := strconv.ParseBool(value)
if err != nil {
log.Fatal("Bot debug must be boolean")
}
setting.botDebug = debug
case "gc_check_timeout":
timeoutInt, err := strconv.Atoi(value)
if err != nil {
log.Fatal("Invalid garbage collector timeout")
}
setting.gcTimeout = time.Duration(timeoutInt)
case "gc_redis_addr":
setting.dbRedisAddress = value
case "gc_redis_db":
db, err := strconv.Atoi(value)
if err != nil {
log.Fatal("Invalid redis DB number")
}
setting.dbRedisDB = db
case "gc_redis_pwd":
setting.dbRedisPassword = value
case "gc_use_socks5":
useSOCKS5Bool, err := strconv.ParseBool(value)
if err != nil {
log.Fatal("Use socks5 must be boolean")
}
setting.useSocksProxy = useSOCKS5Bool
case "gc_socks5_user":
setting.socksParams.socksUser = value
case "gc_socks5_pwd":
setting.socksParams.socksPassword = value
case "gc_socks5_addr":
setting.socksParams.socksAddress = value
case "gc_timeout_limit":
timeout, err := strconv.Atoi(value)
if err != nil {
log.Fatal("Invalid timeout limit")
}
setting.timeoutLimit = timeout
}
}
// check socks5 settings
if setting.useSocksProxy {
for _, socksParam := range []string{setting.socksParams.socksAddress,
setting.socksParams.socksPassword,
setting.socksParams.socksUser} {
if len(socksParam) == 0 {
log.Fatal("Not enough parameters to configure SOCKS5 proxy")
}
}
}
// setup default gc timeout
if setting.gcTimeout == 0 {
setting.gcTimeout = 60
}
// set default timeout limit
if setting.timeoutLimit == 0 {
setting.timeoutLimit = 604800
}
// setup default redis
if len(setting.dbRedisAddress) == 0 {
setting.dbRedisAddress = "127.0.0.1:6379"
}
return &setting
}
// loading setting from system env
func loadSettingFromEnv() map[string]string {
settingMap := make(map[string]string)
for _, item := range os.Environ() {
setting := strings.Split(item, "=")
settingMap[strings.ToLower(setting[0])] = setting[1]
}
return settingMap
}
// todo
//func loadSettingFromFile() botSetting {}
// todo:
//func httpsProxyClient() {}
// creates http.Client connection through SOCKS5 proxy
func socksProxyClient(address, user, password string) *http.Client {
socksAuth := proxy.Auth{User: user, Password: password}
dialSocksProxy, err := proxy.SOCKS5(
"tcp",
address,
&socksAuth,
proxy.Direct,
)
if err != nil {
fmt.Println("Error connecting to proxy:", err)
}
// create client
socksClient := &http.Client{Transport: &http.Transport{Dial: dialSocksProxy.Dial}}
return socksClient
}