This repository has been archived by the owner on Jan 2, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
301 lines (263 loc) · 7.85 KB
/
main.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
// Hello!
//
// This is the source code for @TeknumCaptchaBot where you can find
// the ugly code behind @TeknumCaptchaBot's captcha feature and more.
//
// If you are learning Go for the first time and about to browse this
// repository as one of your first steps, you might want to read the
// other repository on the organization. It's far easier.
// Here: https://github.com/teknologi-umum/polarite
//
// Unless, you're stubborn and want to learn the hard way, all I can
// say is just... good luck.
//
// This source code is very ugly. Let me tell you that up front.
package main
import (
"context"
"database/sql"
"flag"
"io"
"log"
"os"
"os/signal"
"strings"
"time"
// Internals
"captcha-lite/cmd"
"captcha-lite/logger"
"captcha-lite/logger/noop"
rollbarlogger "captcha-lite/logger/rollbar"
sentrylogger "captcha-lite/logger/sentry"
zerologlogger "captcha-lite/logger/zerolog"
"captcha-lite/underattack"
"captcha-lite/underattack/datastore/memory"
"captcha-lite/underattack/datastore/mysql"
"captcha-lite/underattack/datastore/postgres"
// Database and cache
"github.com/allegro/bigcache/v3"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
"github.com/rollbar/rollbar-go"
// Others third party stuff
"github.com/getsentry/sentry-go"
_ "github.com/joho/godotenv/autoload"
"github.com/pkg/errors"
"github.com/rs/zerolog"
tb "gopkg.in/telebot.v3"
)
// This init function checks if there's any configuration
// missing from the .env file.
func init() {
env := os.Getenv("ENVIRONMENT")
if env == "" {
log.Fatal("Please provide the ENVIRONMENT value on the .env file")
}
token := os.Getenv("BOT_TOKEN")
if token == "" {
log.Fatal("Please provide the BOT_TOKEN value on the .env file")
}
if os.Getenv("TZ") == "" {
err := os.Setenv("TZ", "UTC")
if err != nil {
log.Fatalln("during setting TZ environment variable:", err)
}
}
log.Println("Passed the environment variable check")
}
func main() {
// Feature flags
experimentalUnderAttack := flag.Bool("experimental-underattack", false, "Enable the experimental under attack module")
// Setup in memory cache
cache, err := bigcache.New(context.Background(), bigcache.Config{
Shards: 1024,
LifeWindow: time.Minute * 5,
CleanWindow: time.Minute * 1,
Verbose: true,
HardMaxCacheSize: 1024 * 1024 * 1024,
MaxEntrySize: 500,
MaxEntriesInWindow: 50,
})
if err != nil {
log.Fatal("during creating a in memory cache:", errors.WithStack(err))
}
defer func(cache *bigcache.BigCache) {
err := cache.Close()
if err != nil {
log.Fatal(errors.WithStack(err))
}
}(cache)
// Setup logger client
var loggerClient logger.Logger
logProvider, ok := os.LookupEnv("LOG_PROVIDER")
if !ok {
logProvider = "noop"
}
switch strings.ToLower(logProvider) {
case "noop":
loggerClient = noop.New()
case "sentry":
// Setup Sentry for error handling.
sentryClient, err := sentry.NewClient(sentry.ClientOptions{
Dsn: os.Getenv("SENTRY_DSN"),
AttachStacktrace: true,
Debug: os.Getenv("ENVIRONMENT") == "development",
Environment: os.Getenv("ENVIRONMENT"),
})
if err != nil {
log.Fatal("during initiating a new sentry client:", errors.WithStack(err))
}
defer sentryClient.Flush(5 * time.Second)
loggerClient = sentrylogger.New(sentryClient)
case "rollbar":
loggerClient = rollbarlogger.New(
rollbar.New(
os.Getenv("ROLLBAR_TOKEN"),
os.Getenv("ENVIRONMENT"),
"1.0.0",
os.Getenv("ROLLBAR_SERVERHOST"),
os.Getenv("ROLLBAR_SERVERROOT"),
),
)
case "zerolog":
var out io.WriteCloser
switch os.Getenv("ZEROLOG_OUTPUT") {
case "STDOUT":
out = os.Stdout
case "STDERR":
fallthrough
default:
out = os.Stderr
}
zerologLogger := zerolog.New(out)
loggerClient = zerologlogger.New(zerologLogger)
default:
loggerClient = noop.New()
}
var underAttackModule *underattack.Dependency = nil
if *experimentalUnderAttack {
underAttackDatastoreProvider, ok := os.LookupEnv("UNDER_ATTACK_DATASTORE_PROVIDER")
if !ok {
underAttackDatastoreProvider = "memory"
}
underAttackDatastoreDSN, ok := os.LookupEnv("UNDER_ATTACK_DATASTORE_DSN")
if !ok {
underAttackDatastoreDSN = ""
}
var underAttackDatastore underattack.Datastore = nil
switch strings.ToLower(underAttackDatastoreProvider) {
case "pgsql":
fallthrough
case "postgresql":
fallthrough
case "postgres":
if underAttackDatastoreDSN == "" {
log.Fatalf("Empty UNDER_ATTACK_DATASTORE_DSN for provider: %s", underAttackDatastoreProvider)
}
db, err := sql.Open("postgres", underAttackDatastoreDSN)
if err != nil {
log.Fatalf("Creating connection to PostgreSQL: %s", err.Error())
}
underAttackDatastore, err = postgres.NewPostgresDatastore(db, loggerClient)
if err != nil {
log.Fatalf("Creating NewPostgresDatastore: %s", err.Error())
}
case "mysql":
if underAttackDatastoreDSN == "" {
log.Fatalf("Empty UNDER_ATTACK_DATASTORE_DSN for provider: %s", underAttackDatastoreProvider)
}
db, err := sql.Open("mysql", underAttackDatastoreDSN)
if err != nil {
log.Fatalf("Creating connection to PostgreSQL: %s", err.Error())
}
underAttackDatastore, err = mysql.NewMySQLDatastore(db, loggerClient)
if err != nil {
log.Fatalf("Creating NewPostgresDatastore: %s", err.Error())
}
case "memory":
db, err := bigcache.New(context.Background(), bigcache.DefaultConfig(time.Hour*24))
if err != nil {
log.Fatalf("Creating in memory store: %s", err.Error())
}
underAttackDatastore, err = memory.NewInMemoryDatastore(db, loggerClient)
if err != nil {
log.Fatalf("Creating NewInMemoryDatastore: %s", err.Error())
}
default:
log.Fatalf("Unknown under attack datastore provider: %s", underAttackDatastoreProvider)
}
underAttackModule = &underattack.Dependency{
Datastore: underAttackDatastore,
}
}
// Setup Telegram Bot
b, err := tb.NewBot(tb.Settings{
Token: os.Getenv("BOT_TOKEN"),
Poller: &tb.LongPoller{Timeout: 10 * time.Second},
OnError: func(err error, ctx tb.Context) {
if strings.Contains(err.Error(), "Conflict: terminated by other getUpdates request") {
// This error means the bot is currently being deployed
return
}
loggerClient.HandleError(err)
},
})
if err != nil {
log.Fatal("during init of bot client:", errors.WithStack(err))
}
defer b.Stop()
// Setup language
language, ok := os.LookupEnv("LANGUAGE")
if !ok {
language = "en"
}
// This is for recovering from panic.
defer func() {
r := recover()
if r != nil {
loggerClient.HandleError(err)
log.Println(r.(error))
}
}()
deps := cmd.New(cmd.Dependency{
Memory: cache,
Bot: b,
Logger: loggerClient,
Language: strings.ToLower(language),
UnderAttack: underAttackModule,
})
// This is basically just for health check.
b.Handle("/start", func(c tb.Context) error {
_, err := c.Bot().Send(c.Message().Chat, "ok")
if err != nil {
loggerClient.HandleBotError(err, b, c.Message())
}
return nil
})
// Captcha handlers
b.Handle(tb.OnUserJoined, deps.OnUserJoinHandler)
b.Handle(tb.OnText, deps.OnTextHandler)
b.Handle(tb.OnPhoto, deps.OnNonTextHandler)
b.Handle(tb.OnAnimation, deps.OnNonTextHandler)
b.Handle(tb.OnVideo, deps.OnNonTextHandler)
b.Handle(tb.OnDocument, deps.OnNonTextHandler)
b.Handle(tb.OnSticker, deps.OnNonTextHandler)
b.Handle(tb.OnVoice, deps.OnNonTextHandler)
b.Handle(tb.OnVideoNote, deps.OnNonTextHandler)
b.Handle(tb.OnUserLeft, deps.OnUserLeftHandler)
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt, os.Kill)
go func() {
<-signalChan
log.Println("Shutdown signal received, exiting...")
if underAttackModule != nil {
err := underAttackModule.Datastore.Close()
if err != nil {
log.Printf("Error during closing datastore connection: %s", err.Error())
}
}
}()
// Start the bot
log.Println("Bot started!")
b.Start()
}