yaGeey commited on
Commit
e5619c2
·
1 Parent(s): e24a52f

f health check endpoint protection

Browse files
Files changed (1) hide show
  1. src/index.ts +20 -16
src/index.ts CHANGED
@@ -14,6 +14,11 @@ const PORT = process.env.PORT || 3000
14
  chromium.use(StealthPlugin())
15
  const queue = new PQueue({ concurrency: 1 })
16
 
 
 
 
 
 
17
  app.use((req, res, next) => {
18
  if (req.headers['authorization'] !== process.env.API_SECRET) {
19
  return res.status(403).json({ error: 'Wrong Secret Key' })
@@ -26,10 +31,6 @@ if (!process.env.API_SECRET || !process.env.SP_DC || !process.env.SP_KEY) {
26
  process.exit(1)
27
  }
28
 
29
- app.get('/', (req, res) => {
30
- res.send('alive')
31
- })
32
-
33
  // TODO give userId
34
  // TODO give sha codes on 401 / !412! error on client. separate route
35
 
@@ -105,8 +106,18 @@ app.get('/token', async (req, res) => {
105
  }
106
  })
107
 
108
- // update hashes
109
- app.get('/update-hashes', async (req, res) => {
 
 
 
 
 
 
 
 
 
 
110
  const raw = req.query.names as string
111
  let hashes: { name: string; hash: string | null }[] = []
112
 
@@ -118,20 +129,13 @@ app.get('/update-hashes', async (req, res) => {
118
  const names = raw.split(',')
119
  for (const name of names) {
120
  const op = operations.find((o) => o.name === name)
121
- if (!op) {
122
- if (store.hashes[name]) hashes.push({ name, hash: store.hashes[name]! })
123
- continue
124
- }
125
- if (Boolean(store.hashes[op.name])) {
126
- hashes.push({ name: op.name, hash: store.hashes[op.name]! })
127
- continue
128
- }
129
  const hash = await queue.add(() => updateHash(op))
130
  hashes.push({ name: op.name, hash })
131
  await delay(800)
132
  }
133
  if (hashes.length === 0) {
134
- return res.status(400).json({ error: 'No valid operation names provided or do not exists in storage' })
135
  }
136
  if (hashes.length !== names.length) {
137
  return res.status(404).json({ error: 'Some operation names were invalid', details: { raw, hashes } })
@@ -152,7 +156,7 @@ cron.schedule('0 3 * * *', () => {
152
  await queue.add(async () => {
153
  const hashes = await updateAllHashes()
154
  if (hashes.map((h) => h.hash).some((h) => h === null)) {
155
- console.error('cron: Failed to update all hashes', hashes)
156
  }
157
  })
158
  }, randomDelay)
 
14
  chromium.use(StealthPlugin())
15
  const queue = new PQueue({ concurrency: 1 })
16
 
17
+ // Health check - must be before auth middleware
18
+ app.get('/', (req, res) => {
19
+ res.send('alive')
20
+ })
21
+
22
  app.use((req, res, next) => {
23
  if (req.headers['authorization'] !== process.env.API_SECRET) {
24
  return res.status(403).json({ error: 'Wrong Secret Key' })
 
31
  process.exit(1)
32
  }
33
 
 
 
 
 
34
  // TODO give userId
35
  // TODO give sha codes on 401 / !412! error on client. separate route
36
 
 
106
  }
107
  })
108
 
109
+ app.get('/hashes', (req, res) => {
110
+ const raw = req.query.names as string | undefined
111
+ if (raw) {
112
+ const names = raw.split(',')
113
+ const filtered = Object.fromEntries(Object.entries(store.hashes).filter(([key]) => names.includes(key)))
114
+ res.json(filtered)
115
+ } else {
116
+ res.json(store.hashes)
117
+ }
118
+ })
119
+
120
+ app.put('/hashes', async (req, res) => {
121
  const raw = req.query.names as string
122
  let hashes: { name: string; hash: string | null }[] = []
123
 
 
129
  const names = raw.split(',')
130
  for (const name of names) {
131
  const op = operations.find((o) => o.name === name)
132
+ if (!op) continue
 
 
 
 
 
 
 
133
  const hash = await queue.add(() => updateHash(op))
134
  hashes.push({ name: op.name, hash })
135
  await delay(800)
136
  }
137
  if (hashes.length === 0) {
138
+ return res.status(400).json({ error: 'No valid operation names provided' })
139
  }
140
  if (hashes.length !== names.length) {
141
  return res.status(404).json({ error: 'Some operation names were invalid', details: { raw, hashes } })
 
156
  await queue.add(async () => {
157
  const hashes = await updateAllHashes()
158
  if (hashes.map((h) => h.hash).some((h) => h === null)) {
159
+ console.error('cron: Failed to update some hashes', hashes)
160
  }
161
  })
162
  }, randomDelay)