Skip to content

Commit

Permalink
Merge branch 'main' into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
HughParry committed Nov 4, 2024
2 parents 089ecbc + ed6d1a7 commit 486a6e9
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 3 deletions.
12 changes: 12 additions & 0 deletions packages/cli/src/prosopo.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ function getMongoURI(): string {
return mongoURI;
}

const getLRules = () => {
if (!process.env.L_RULES) {
return {};
}
try {
return JSON.parse(process.env.L_RULES);
} catch (e) {
return {};
}
};

export default function getConfig(
captchaSolutionsConfig?: typeof ProsopoCaptchaSolutionConfigSchema,
captchaServeConfig?: ProsopoCaptchaCountConfigSchemaInput,
Expand Down Expand Up @@ -96,5 +107,6 @@ export default function getConfig(
schedule: process.env.CLIENT_LIST_SCHEDULE,
},
},
lRules: getLRules(),
} as ProsopoConfigInput);
}
7 changes: 6 additions & 1 deletion packages/provider/src/api/captcha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,12 @@ export function prosopoRouter(env: ProviderEnvironment): Router {
try {
const { token, dapp, user } =
GetFrictionlessCaptchaChallengeRequestBody.parse(req.body);
const botScore = await getBotScore(token);

const lScore = tasks.frictionlessManager.checkLangRules(
req.headers["accept-language"] || "",
);

const botScore = (await getBotScore(token)) + lScore;
const clientConfig = await tasks.db.getClientRecord(dapp);
const botThreshold =
clientConfig?.settings?.frictionlessThreshold ||
Expand Down
26 changes: 25 additions & 1 deletion packages/provider/src/tasks/frictionless/frictionlessTasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
POW_SEPARATOR,
type PoWCaptcha,
type PoWChallengeId,
type ProsopoConfigOutput,
type RequestHeaders,
} from "@prosopo/types";
import type { IProviderDatabase, Session } from "@prosopo/types-database";
Expand All @@ -33,10 +34,16 @@ const logger = getLoggerDefault();
const DEFAULT_POW_DIFFICULTY = 4;

export class FrictionlessManager {
config: ProsopoConfigOutput;
pair: KeyringPair;
db: IProviderDatabase;

constructor(pair: KeyringPair, db: IProviderDatabase) {
constructor(
config: ProsopoConfigOutput,
pair: KeyringPair,
db: IProviderDatabase,
) {
this.config = config;
this.pair = pair;
this.db = db;
}
Expand Down Expand Up @@ -81,6 +88,23 @@ export class FrictionlessManager {
return false;
}

checkLangRules(acceptLanguage: string): number {
const lConfig = this.config.lRules;
let lScore = 0;
if (lConfig) {
const languages = acceptLanguage
.split(",")
.map((lang) => lang.trim().split(";")[0]);

for (const lang of languages) {
if (lang && lConfig[lang]) {
lScore += lConfig[lang];
}
}
}
return lScore;
}

sendImageCaptcha(): GetFrictionlessCaptchaResponse {
return {
[ApiParams.captchaType]: "image",
Expand Down
6 changes: 5 additions & 1 deletion packages/provider/src/tasks/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ export class Tasks {
this.logger,
this.db,
);
this.frictionlessManager = new FrictionlessManager(this.pair, this.db);
this.frictionlessManager = new FrictionlessManager(
this.config,
this.pair,
this.db,
);
}
}
1 change: 1 addition & 0 deletions packages/types/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ export const ProsopoConfigSchema = ProsopoBasicConfigSchema.merge(
mongoClientUri: string().optional(),
rateLimits: ApiPathRateLimits.default(ProviderDefaultRateLimits),
proxyCount: number().optional().default(0),
lRules: record(string(), number()).optional(),
}),
);

Expand Down

0 comments on commit 486a6e9

Please sign in to comment.