Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

working on the captcha APi

Blue Mousead60b6782d38
diff --git a/.env.example b/.env.example
index 9672851e4..adfc0a865 100644
--- a/.env.example
+++ b/.env.example
@@ -50,3 +50,6 @@ TG_BOT_TOKEN=
# Swetrix CDN for storing files
CDN_URL=http://localhost:5006
CDN_ACCESS_TOKEN=SOME_SECRET_TOKEN
+
+# Captcha API
+CAPTCHA_SALT=
diff --git a/package.json b/package.json
index 25ff209d5..cce820b5c 100644
--- a/package.json
+++ b/package.json
@@ -69,6 +69,7 @@
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^7.8.0",
+ "svg-captcha": "^1.4.0",
"telegraf": "^4.11.2",
"typeorm": "^0.2.45",
"ua-parser-js": "^1.0.32",
diff --git a/src/captcha/captcha.controller.ts b/src/captcha/captcha.controller.ts
index 145a946df..797c2ed9c 100644
--- a/src/captcha/captcha.controller.ts
+++ b/src/captcha/captcha.controller.ts
@@ -1,5 +1,9 @@
-import { Controller } from '@nestjs/common'
+import {
+ Controller, Post, Body, UseGuards, ForbiddenException, InternalServerErrorException, Headers, Ip, Request, Req,
+} from '@nestjs/common'
import { CaptchaService } from './captcha.service'
+import { BotDetectionGuard } from '../common/guards/bot-detection.guard'
+import { BotDetection } from '../common/decorators/bot-detection.decorator'
@Controller({
version: '1',
@@ -9,4 +13,22 @@ export class CaptchaController {
constructor(
private readonly captchaService: CaptchaService,
) { }
+
+
+ @Post('/auto-verify')
+ @UseGuards(BotDetectionGuard)
+ @BotDetection()
+ async logCustom(
+ @Body() eventsDTO: EventsDTO,
+ @Headers() headers,
+ @Req() request: Request
+ @Ip() reqIP,
+ ): Promise<any> {
+ // request.cookies['cookieKey']
+ const { 'user-agent': userAgent, origin } = headers
+
+ const ip = headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''
+
+ // await this.analyticsService.validate(eventsDTO, origin, 'custom', ip)
+ }
}
diff --git a/src/captcha/captcha.service.ts b/src/captcha/captcha.service.ts
index 227234a70..e4382bd19 100644
--- a/src/captcha/captcha.service.ts
+++ b/src/captcha/captcha.service.ts
@@ -1,10 +1,43 @@
import {
Injectable,
} from '@nestjs/common'
+import * as svgCaptcha from 'svg-captcha'
+import { hash } from 'blake3'
+
+import { CAPTCHA_SALT } from '../common/constants'
+
+interface GeneratedCaptcha {
+ data: string
+ hash: string
+}
@Injectable()
export class CaptchaService {
constructor(
) { }
+ hashCaptcha(text: string): string {
+ return hash(`${text}${CAPTCHA_SALT}`).toString('hex')
+ }
+
+ async generateCaptcha(): Promise<GeneratedCaptcha> {
+ const captcha = svgCaptcha.create()
+ const hash = this.hashCaptcha(captcha.text)
+
+ return {
+ data: captcha.data,
+ hash,
+ }
+ }
+
+ verifyCaptcha(text: string, hash: string): boolean {
+ const hashedText = this.hashCaptcha(text)
+ return hashedText === hash
+ }
+
+ async autoVerify(jwtCookie: string | undefined): Promise<boolean> {
+ // TODO
+
+ return false
+ }
}
diff --git a/src/common/constants.ts b/src/common/constants.ts
index c67444c89..ddeee21d3 100644
--- a/src/common/constants.ts
+++ b/src/common/constants.ts
@@ -173,6 +173,8 @@ const REDIS_PROJECTS_COUNT_KEY = 'stats:projects_count'
const REDIS_PAGEVIEWS_COUNT_KEY = 'stats:pageviews'
const REDIS_PERFORMANCE_COUNT_KEY = 'stats:performance'
+const CAPTCHA_SALT = process.env.CAPTCHA_SALT
+
// 3600 sec -> 1 hour
const redisProjectCacheTimeout = 3600
@@ -226,4 +228,5 @@ export {
ORIGINS_REGEX,
REDIS_LOG_PERF_CACHE_KEY,
REDIS_PERFORMANCE_COUNT_KEY,
+ CAPTCHA_SALT,
}