forked from ping-pub/faucet
-
Notifications
You must be signed in to change notification settings - Fork 5
/
checker.js
87 lines (77 loc) · 2.54 KB
/
checker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { Level } from "level";
const WINDOW = 86400 * 1000 // milliseconds in a day
// const WINDOW = 20 * 1000 // 20s for test
export class FrequencyChecker {
constructor(conf) {
this.conf = conf
this.db = new Level(conf.db.path, { valueEncoding: 'json' });
}
async check(key, limit) {
return new Promise((resolve) => {
this.db.get(key, function (err, value) {
const now = Date.now()
if (err || value && value.filter(x => now - x < WINDOW).length < limit) {
resolve(true)
// console.log(key, limit, value, true)
} else {
resolve(false)
// console.log(key, limit, false)
}
});
})
}
async checkIp(ip, chain) {
const chainLimit = this.conf.blockchains.find(x => x.name === chain)
return chainLimit ? this.check(ip, chainLimit.limit.ip ) : Promise.resolve(false)
}
async checkAddress(address, chain) {
const chainLimit = this.conf.blockchains.find(x => x.name === chain)
return chainLimit ? this.check(address, chainLimit.limit.address ) : Promise.resolve(false)
}
async update(key) {
const db = this.db
db.get(key, function (err, history) {
if (err) {
db.put(key, [Date.now()])
} else {
history.push(Date.now())
db.put(key, history)
}
});
}
async updateRequestStatus(requestId, status, message, data=null) {
const db = this.db
let request
try {
request = await db.get(requestId)
request.statuses.push(status)
} catch (error) {
if (error.status == 404) {
request = {statuses: [status]}
} else {
console.log(error, 'error')
}
}
await db.put(requestId, {data, statuses: request.statuses, messsage: message ? message : ''});
}
async getRequestStatus(requestId) {
const db = this.db
const request = await db.get(requestId);
return request
}
async put(key, value) {
await this.db.put(key, value)
}
async get (address) {
try {
const status = await this.db.get(address);
return status
} catch (err) {
if (err.notFound) {
console.log('Address not found')
} else {
console.log('Database error')
}
}
}
}