rrd108 / nuxt-api-shield

Nuxt API Rate Limiter / Brute Force Protection
16 stars 4 forks source link

Using redis as driver instead memory #47

Open zmwildan opened 3 hours ago

zmwildan commented 3 hours ago

Hello, I am trying using redis as driver and got error like this

Screenshot 2024-10-23 at 15 34 28

thankyou.

rrd108 commented 2 hours ago

plase share the contents of your nuxt.config.ts file

zmwildan commented 1 hour ago

nuxt.config.ts

export default defineNuxtConfig({
  modules: [
    'nuxt-api-shield',
  ],
  pinia     : { autoImports: ['defineStore', 'storeToRefs'] },
  typescript: {
    tsConfig: {
      compilerOptions: {
        strict          : false,
        strictNullChecks: true,
      },
    },
  },
  nuxtApiShield: {
    limit: {
      max     : 13, // maximum requests per duration time, default is 12/duration
      duration: 108, // duration time in seconds, default is 108 seconds
      ban     : 3600, // ban time in seconds, default is 3600 seconds = 1 hour
    },
    delayOnBan      : false, // delay every response with +1sec when the user is banned, default is true
    errorMessage    : 'Too Many Requests', // error message when the user is banned, default is "Too Many Requests"
    retryAfterHeader: true, // when the user is banned add the Retry-After header to the response, default is false
    log             : {
      path    : '', // path to the log file, every day a new log file will be created, use "" to disable logging
      attempts: 0, // if an IP reach 100 requests, all the requests will be logged, can be used for further analysis or blocking for example with fail2ban, use 0 to disable logging
    },
    routes: [], // specify routes to apply rate limiting to, default is an empty array meaning all routes are protected.
    // Example:
    // routes: ["/api/v2/", "/api/v3/"], // /api/v1 will not be protected, /api/v2/ and /api/v3/ will be protected
  },
  nitro: {
    experimental: { tasks: true },
    storage     : {
      shield: {
        // storage name, you **must** use "shield" as the name
        driver: 'redis',
      },
    },
    scheduledTasks: { '*/15 * * * *': ['shield:clean'] },
  },
})

/server/plugins/storage.ts

import redis from 'unstorage/drivers/redis'

export default defineNitroPlugin(() => {
  const storage = useStorage()
  storage.mount(
    'redis',
    redis({
      port    : xxx, // Redis port
      host    : '0.0.0.0', // Redis host
      password: 'redis',
      db      : 0, // Defaults to 0
    }),
  )
})