Skip to content

Commit 9022267

Browse files
committed
Set trmnl ip cache to 24 hours, re-enable rate limiting, allow trmnl worker ips max auth rate limit as authenticated
1 parent a58914b commit 9022267

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

src/auth/trmnlAuth.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ let cachedIPs: Set<string> | null = null
88
let cachedAtMs = 0
99
let inFlight: Promise<Set<string>> | null = null
1010

11-
const TRMNL_TTL_IPS_MS = 10 * 60 * 1000 // 10 minute cache
11+
const TRMNL_TTL_IPS_MS = 24 * 60 * 60 * 1000 // 24hr cache
1212

1313
// Disallow TRMNL worker IP bypass by default
1414
const TRMNL_IP_ALLOW_BYPASS: boolean = (process.env.TRMNL_IP_AUTH_ALLOW_PRIVATE === 'true')
@@ -98,6 +98,7 @@ export const trmnlAuthByIP: RequestHandler = async (req: Request, res: Response,
9898
if (TRMNL_IP_ALLOW_BYPASS) {
9999
logger.warn('[AUTH] Bypassing TRMNL worker IP check')
100100
next()
101+
return
101102
}
102103

103104
// NOTE: Some worker IPs can be ipv6 based. No normalization is done here.
@@ -118,7 +119,7 @@ export const trmnlAuthByIP: RequestHandler = async (req: Request, res: Response,
118119
}
119120
}
120121

121-
async function getTRMNLIPs(): Promise<Set<string>> {
122+
export async function getTRMNLIPs(): Promise<Set<string>> {
122123
const now = Date.now()
123124

124125
if (cachedIPs && now - cachedAtMs < TRMNL_TTL_IPS_MS) {

src/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ server.set('trust proxy', 1)
6060

6161
logger.info('Setting up middleware...')
6262
server.use(helmet())
63-
//server.use(rateLimiter)
63+
server.use(rateLimiter)
6464
server.use(express.json())
6565
// Declare regular REST API routing
6666
logger.info('Initializing routes...')

src/utils/rateLimiter.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
11
import rateLimit, { RateLimitRequestHandler } from 'express-rate-limit'
22
import { logger } from './logger.js'
33
import { Request, Response } from 'express'
4+
import { getTRMNLIPs } from '../auth/trmnlAuth.js'
45

6+
let ips: Set<string> = new Set()
7+
8+
async function refreshTRMNLIPs() {
9+
ips = await getTRMNLIPs()
10+
}
11+
12+
function isTrmnlWorkerIp(ip: string) {
13+
return ips.has(ip)
14+
}
15+
16+
// warm up trmnl worker ips and refresh every 24 hours
17+
refreshTRMNLIPs()
18+
setInterval(() => void refreshTRMNLIPs(), 24 * 60 * 60 * 1000).unref()
519

620
function getClientIp(req: Request): string {
721
const h = req.headers['cf-connecting-ip']
@@ -20,6 +34,10 @@ export const rateLimiter: RateLimitRequestHandler = rateLimit({
2034
limit: (req: Request): number => {
2135
const ip = getClientIp(req)
2236
const sub = getAuthSub(req)
37+
if (isTrmnlWorkerIp(ip)) {
38+
logger.info(`Rate limit check for TRMNL IP: ${ip}`)
39+
return 60
40+
}
2341
logger.info(`Rate limit check for ${sub ? 'authenticated' : 'anon'} - ${ip}`)
2442
return sub ? 60 : 5
2543
},

0 commit comments

Comments
 (0)