Initial captcha image generation
This commit is contained in:
		
							
								
								
									
										2
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								go.mod
									
									
									
									
									
								
							| @ -4,7 +4,7 @@ go 1.22.5 | ||||
|  | ||||
| require ( | ||||
| 	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/go-telegram/bot v1.5.0 // indirect | ||||
| 	github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect | ||||
|  | ||||
							
								
								
									
										29
									
								
								src/captcha.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								src/captcha.go
									
									
									
									
									
										Normal 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") | ||||
| } | ||||
| @ -148,7 +148,6 @@ func handlePrivateStartCaptcha(ctx context.Context, b *bot.Bot, u *models.Update | ||||
| 			return | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	if captcha.CorrectAnswer == 0 { | ||||
| 		captcha.CorrectAnswer = 42 | ||||
| 		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 { | ||||
| 		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()) | ||||
| 		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()) | ||||
| 		if err != nil { | ||||
| 			log.Println("Can't block user from captcha: ", err) | ||||
| 		} | ||||
|  | ||||
| @ -14,6 +14,7 @@ import ( | ||||
| ) | ||||
|  | ||||
| func main() { | ||||
| 	GenCaptcha() | ||||
| 	ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt) | ||||
| 	defer cancel() | ||||
|  | ||||
|  | ||||
		Reference in New Issue
	
	Block a user