Initial captcha image generation

This commit is contained in:
Nefrace 2024-08-04 18:24:48 +03:00
parent 104fd180ab
commit e411553db4
4 changed files with 59 additions and 30 deletions

2
go.mod
View File

@ -4,7 +4,7 @@ go 1.22.5
require ( require (
github.com/dustin/go-humanize v1.0.1 // indirect github.com/dustin/go-humanize v1.0.1 // indirect
github.com/fogleman/gg v1.3.0 // indirect github.com/fogleman/gg v1.3.0
github.com/glebarez/go-sqlite v1.22.0 // indirect github.com/glebarez/go-sqlite v1.22.0 // indirect
github.com/go-telegram/bot v1.5.0 // indirect github.com/go-telegram/bot v1.5.0 // indirect
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect

29
src/captcha.go Normal file
View File

@ -0,0 +1,29 @@
package main
import (
"embed"
"image/color"
"github.com/fogleman/gg"
)
var (
res embed.FS
)
func GenCaptcha() {
dc := gg.NewContext(400, 300)
gd := gg.NewLinearGradient(0, 0, 400, 300)
gd.AddColorStop(0, color.RGBA{189, 24, 229, 255})
gd.AddColorStop(1, color.RGBA{27, 21, 123, 255})
dc.SetFillStyle(gd)
dc.DrawRectangle(0, 0, 400, 300)
dc.Fill()
dc.SetRGBA(0, 0, 0, 0.05)
dc.SetLineWidth(27)
for i := 0.0; i < 20; i += 1 {
dc.DrawLine(i * 70, -100, i * 70 - 500, 400)
dc.Stroke()
}
dc.SavePNG("grad.png")
}

View File

@ -89,20 +89,20 @@ func handleNewJoined(ctx context.Context, b *bot.Bot, u *models.Update) {
ChatID: u.Message.Chat.ID, ChatID: u.Message.Chat.ID,
UserID: user.ID, UserID: user.ID,
Permissions: &models.ChatPermissions{ Permissions: &models.ChatPermissions{
CanSendMessages: false, CanSendMessages: false,
CanSendAudios: false, CanSendAudios: false,
CanSendDocuments: false, CanSendDocuments: false,
CanSendPhotos: false, CanSendPhotos: false,
CanSendVideos: false, CanSendVideos: false,
CanSendVideoNotes: false, CanSendVideoNotes: false,
CanSendVoiceNotes: false, CanSendVoiceNotes: false,
CanSendPolls: false, CanSendPolls: false,
CanSendOtherMessages: false, CanSendOtherMessages: false,
CanAddWebPagePreviews: false, CanAddWebPagePreviews: false,
CanChangeInfo: false, CanChangeInfo: false,
CanInviteUsers: false, CanInviteUsers: false,
CanPinMessages: false, CanPinMessages: false,
CanManageTopics: false, CanManageTopics: false,
}, },
}) })
if err != nil { if err != nil {
@ -148,7 +148,6 @@ func handlePrivateStartCaptcha(ctx context.Context, b *bot.Bot, u *models.Update
return return
} }
} }
if captcha.CorrectAnswer == 0 { if captcha.CorrectAnswer == 0 {
captcha.CorrectAnswer = 42 captcha.CorrectAnswer = 42
msg, err := b.SendMessage(ctx, &bot.SendMessageParams{ msg, err := b.SendMessage(ctx, &bot.SendMessageParams{
@ -197,8 +196,8 @@ func handlePrivateCaptcha(ctx context.Context, b *bot.Bot, u *models.Update) {
} }
if ban_minutes > 0 { if ban_minutes > 0 {
b.SendMessage(ctx, &bot.SendMessageParams{ChatID: msg.From.ID, Text:text}) b.SendMessage(ctx, &bot.SendMessageParams{ChatID: msg.From.ID, Text: text})
_, err := db.Exec("update captchas set blocked_until = $2 where id = $1", captcha.Id, time.Now().Add(time.Minute * time.Duration(ban_minutes)).Unix()) _, err := db.Exec("update captchas set blocked_until = $2 where id = $1", captcha.Id, time.Now().Add(time.Minute*time.Duration(ban_minutes)).Unix())
if err != nil { if err != nil {
log.Println("Can't block user from captcha: ", err) log.Println("Can't block user from captcha: ", err)
} }
@ -206,20 +205,20 @@ func handlePrivateCaptcha(ctx context.Context, b *bot.Bot, u *models.Update) {
} }
_, err = b.RestrictChatMember(ctx, &bot.RestrictChatMemberParams{ChatID: captcha.ChatID, UserID: captcha.UserID, Permissions: &models.ChatPermissions{ _, err = b.RestrictChatMember(ctx, &bot.RestrictChatMemberParams{ChatID: captcha.ChatID, UserID: captcha.UserID, Permissions: &models.ChatPermissions{
CanSendMessages: true, CanSendMessages: true,
CanSendAudios: true, CanSendAudios: true,
CanSendDocuments: true, CanSendDocuments: true,
CanSendPhotos: true, CanSendPhotos: true,
CanSendVideos: true, CanSendVideos: true,
CanSendVideoNotes: true, CanSendVideoNotes: true,
CanSendVoiceNotes: true, CanSendVoiceNotes: true,
CanSendPolls: true, CanSendPolls: true,
CanSendOtherMessages: true, CanSendOtherMessages: true,
CanAddWebPagePreviews: true, CanAddWebPagePreviews: true,
CanChangeInfo: true, CanChangeInfo: true,
CanInviteUsers: true, CanInviteUsers: true,
CanPinMessages: true, CanPinMessages: true,
CanManageTopics: true, CanManageTopics: true,
}}) }})
if err != nil { if err != nil {
log.Println("Can't unrestrict user: ", err) log.Println("Can't unrestrict user: ", err)

View File

@ -14,6 +14,7 @@ import (
) )
func main() { func main() {
GenCaptcha()
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt) ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel() defer cancel()