Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import { InfluxDB, Point } from '@influxdata/influxdb-client'

const debug = createDebug('app:api')

// const vtClient = new VTApi(process.env.VT_API_KEY)
const vtClient = process.env.VT_API_KEY ? new VTApi(process.env.VT_API_KEY) : null

const redisUrl = process.env.REDIS_URL || 'redis://redis:6379'
console.log(`REDIS_URL: ${redisUrl}`)
// NOTE: Increase connectTimeout for Render, consider using reconnectStrategy...
Expand Down Expand Up @@ -210,12 +213,11 @@ export async function getVTStats(hash) {
return cached
}
debug(`-- CACHE MISS: ${key}`)
const vt = new VTApi(process.env.VT_API_KEY)
let stats, epoch, data
if (hash.endsWith('==')) {
debug('DEPRECATED - getAnalysis') // TODO: Deprecated
try {
data = await vt.getAnalysis(hash)
data = await vtClient.getAnalysis(hash)
} catch (error) {
await cacheError(key, `Error ${error.status}`)
}
Expand All @@ -224,7 +226,7 @@ export async function getVTStats(hash) {
epoch = data?.data?.attributes?.date
} else {
try {
data = await vt.getReport(hash)
data = await vtClient.getReport(hash)
} catch (error) {
await cacheError(key, `Error ${error.status}`)
}
Expand Down
23 changes: 19 additions & 4 deletions src/virustotal.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,28 @@ const debug = createDebug('app:api')

export class VTApi {
/**
* GitHub Api
* @param {string} token
* VirusTotal API
* @param {string} tokens - CSV of API tokens
*/
constructor(token) {
constructor(tokens) {
this.tokens = tokens
.split(',')
.map((t) => t.trim())
.filter(Boolean)
// debug('this.tokens:', this.tokens)
console.log(`Loaded ${this.tokens.length} VT API Keys`)

this.idx = 0

this.client = axios.create({
baseURL: 'https://www.virustotal.com/api/v3/',
headers: { 'X-APIKey': token },
})

this.client.interceptors.request.use((config) => {
config.headers['X-APIKey'] = this.tokens[this.idx]
this.idx = (this.idx + 1) % this.tokens.length
debug('Using token index %d/%d', this.idx, this.tokens.length)
return config
})
}

Expand Down
Loading