Skip to content

Commit

Permalink
Allow adding multiple types of rules at the same time
Browse files Browse the repository at this point in the history
  • Loading branch information
forgetso committed Dec 23, 2024
1 parent cf2cf7f commit 0c51dcb
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 50 deletions.
32 changes: 18 additions & 14 deletions packages/cli/src/commands/addBlockRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,25 +95,29 @@ export default (

if (argv.ips) {
await tasks.clientTaskManager.addIPBlockRules(
AddBlockRulesIPSpec.parse({
ips: argv.ips,
global: argv.global,
hardBlock: argv.hardBlock,
dapp: argv.dapp,
captchaConfig,
}),
AddBlockRulesIPSpec.parse([
{
ips: argv.ips,
global: argv.global,
hardBlock: argv.hardBlock,
dapp: argv.dapp,
captchaConfig,
},
]),
);
logger.info("IP Block rules added");
}
if (argv.users) {
await tasks.clientTaskManager.addUserBlockRules(
AddBlockRulesUserSpec.parse({
users: argv.users,
global: argv.global,
hardBlock: argv.hardBlock,
dapp: argv.dapp,
captchaConfig,
}),
AddBlockRulesUserSpec.parse([
{
users: argv.users,
global: argv.global,
hardBlock: argv.hardBlock,
dapp: argv.dapp,
captchaConfig,
},
]),
);
logger.info("User Block rules added");
}
Expand Down
64 changes: 36 additions & 28 deletions packages/provider/src/tasks/client/clientTasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,20 +223,24 @@ export class ClientTaskManager {

/**
* @description Add IP block rules to the database. Allows specifying mutiple IPs for a single configuration
* @param {AddBlockRulesIP} opts
* @param {AddBlockRulesIP} rulesets
*/
async addIPBlockRules(opts: AddBlockRulesIP): Promise<void> {
const rules: IPAddressBlockRule[] = opts.ips.map((ip) => {
return {
ip: Number(getIPAddress(ip).bigInt()),
global: opts.global,
type: BlockRuleType.ipAddress,
dappAccount: opts.dappAccount,
hardBlock: opts.hardBlock,
...(opts.captchaConfig && { captchaConfig: opts.captchaConfig }),
};
});
await this.providerDB.storeIPBlockRuleRecords(rules);
async addIPBlockRules(rulesets: AddBlockRulesIP): Promise<void> {
for (const ruleset of rulesets) {
const rules: IPAddressBlockRule[] = ruleset.ips.map((ip) => {
return {
ip: Number(getIPAddress(ip).bigInt()),
global: ruleset.global,
type: BlockRuleType.ipAddress,
dappAccount: ruleset.dappAccount,
hardBlock: ruleset.hardBlock,
...(ruleset.captchaConfig && {
captchaConfig: ruleset.captchaConfig,
}),
};
});
await this.providerDB.storeIPBlockRuleRecords(rules);
}
}

/**
Expand All @@ -252,22 +256,26 @@ export class ClientTaskManager {

/**
* @description Add user block rules to the database. Allows specifying multiple users for a single configuration
* @param {AddBlockRulesUser} opts
* @param {AddBlockRulesUser} rulesets
*/
async addUserBlockRules(opts: AddBlockRulesUser): Promise<void> {
validateAddress(opts.dappAccount, false, 42);
const rules: UserAccountBlockRule[] = opts.users.map((userAccount) => {
validateAddress(userAccount, false, 42);
return {
dappAccount: opts.dappAccount,
userAccount,
type: BlockRuleType.userAccount,
global: opts.global,
hardBlock: opts.hardBlock,
...(opts.captchaConfig && { captchaConfig: opts.captchaConfig }),
};
});
await this.providerDB.storeUserBlockRuleRecords(rules);
async addUserBlockRules(rulesets: AddBlockRulesUser): Promise<void> {
for (const ruleset of rulesets) {
validateAddress(ruleset.dappAccount, false, 42);
const rules: UserAccountBlockRule[] = ruleset.users.map((userAccount) => {
validateAddress(userAccount, false, 42);
return {
dappAccount: ruleset.dappAccount,
userAccount,
type: BlockRuleType.userAccount,
global: ruleset.global,
hardBlock: ruleset.hardBlock,
...(ruleset.captchaConfig && {
captchaConfig: ruleset.captchaConfig,
}),
};
});
await this.providerDB.storeUserBlockRuleRecords(rules);
}
}

/**
Expand Down
20 changes: 12 additions & 8 deletions packages/types/src/provider/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,10 +387,12 @@ export const BlockRuleSpec = object({

export type BlockRule = zInfer<typeof BlockRuleSpec>;

export const AddBlockRulesIPSpec = BlockRuleSpec.merge(
object({
ips: array(string()),
}),
export const AddBlockRulesIPSpec = array(
BlockRuleSpec.merge(
object({
ips: array(string()),
}),
),
);

export type AddBlockRulesIP = zInfer<typeof AddBlockRulesIPSpec>;
Expand All @@ -404,10 +406,12 @@ export type RemoveBlockRulesIP = zInfer<typeof RemoveBlockRulesIPSpec>;

export const BlockRuleIPAddBody = array(AddBlockRulesIPSpec);

export const AddBlockRulesUserSpec = BlockRuleSpec.merge(
object({
users: array(string()),
}),
export const AddBlockRulesUserSpec = array(
BlockRuleSpec.merge(
object({
users: array(string()),
}),
),
);

export type AddBlockRulesUser = zInfer<typeof AddBlockRulesUserSpec>;
Expand Down

0 comments on commit 0c51dcb

Please sign in to comment.