forked from sponnet/locals-faucetserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
187 lines (165 loc) · 4.98 KB
/
index.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
const async = require("async");
const cors = require("cors");
const ethers = require('ethers');
const express = require("express");
const NodeCache = require("node-cache");
const app = express();
const config = require("./config.json");
const greylist = new NodeCache({ stdTTL: 60 * 60 * 24 });
const accesskey = require("./static/src/accesskey.json").key;
// set up wallet
console.log(`connecting to ${config.web3.host}`);
var customHttpProvider = new ethers.providers.JsonRpcProvider(config.web3.host);
let wallet = new ethers.Wallet(config.walletpk, customHttpProvider);
console.log(`wallet address = ${wallet.address}`);
let donations = [];
// create payout queue
const q = async.queue((task, callback) => {
console.log(`donating to ${task.address}`);
donate(task.address, (err, txHash) => {
if (err) {
}
if (txHash) {
donations.push({
address: task.address,
txhash: txHash
});
while (donations.length > 5) {
donations.shift();
}
}
console.log(`waiting ${config.payoutfrequencyinsec} sec`);
setTimeout(callback, config.payoutfrequencyinsec * 1000);
})
}, 1);
q.drain(() => {
console.log(`Queue empty.`)
})
// Get faucet balance in ether
const getFaucetBalance = async () => {
const balance = await wallet.getBalance()
return parseFloat(ethers.utils.formatUnits(balance))
};
app.use(cors());
// frontend app is served from here
app.use(express.static("static/build"));
// get current faucet info
app.get("/faucetinfo", async (req, res) => {
var ip = req.headers["x-forwarded-for"] || req.remoteAddress;
console.log("client IP=", ip);
var etherbalance = -1;
try {
etherbalance = await getFaucetBalance();
} catch (e) {
console.log(e);
}
res.status(200).json({
account: wallet.address,
balance: Math.floor(etherbalance),
etherscanroot: config.etherscanroot,
payoutfrequencyinsec: config.payoutfrequencyinsec,
payoutamountinether: config.payoutamountinether,
queuesize: config.queuesize,
queuename: "queue"
});
});
// lookup if there is an exception made for this address
function getException(key) {
const val = greylist.get(key);
if (!val) return;
return ({
...val,
key
})
}
// set an exception for this address ( greylist / blacklist )
function setException(key, reason) {
if (key && key !== undefined) {
greylist.set(key, { reason });
} else {
console.log(`no key given to setException`)
}
}
app.get("/q", function (req, res) {
return res.status(200).json(
{
last: [...donations],
current: [...q],
exceptions: greylist.getStats().keys
});
});
app.get(`/donate/:address`, function (req, res) {
console.log(`request on old endpoint.. keeping them busy`);
setTimeout(()=>{
res.status(200).json({ message: "ok" });
},60*1000)
});
// try to add an address to the donation queue
app.get(`/donate/${accesskey}/:address`, function (req, res) {
// Strip all spaces
var address = req.params.address.replace(" ", "");
try {
// check for valid Eth address
// then convert it to checksum address with 0x prefix
address = ethers.utils.getAddress(address);
//console.log("Fix address", address);
} catch (error) {
return res.status(400).json({
message: "the address is invalid"
});
}
var ip = req.headers["x-forwarded-for"] || req.socket.remoteAddress;
ip = ip.replace(/\./g, "_");
// check if address/ip is not greylisted or blacklisted
const exception = getException(address) || getException(ip);
if (exception) {
if (exception.reason === "greylist") {
console.log(exception.key, "is on the greylist");
// extend greylist
setException(ip, "greylist");
setException(address, "greylist");
return res.status(403).json({
address: exception.address,
message: "you are greylisted - greylist period is now reset on this ip and address",
});
}
if (exception.reason === "blacklist") {
console.log(exception.address, "is on the blacklist");
return res.status(403).json({
address: address,
message: "you are blacklisted"
});
}
}
// check if queue is not full
if (q.length() >= config.queuesize) {
return res.status(403).json({
message: "queue is full. Try again later."
});
}
q.push({ address });
setException(ip, "greylist");
setException(address, "greylist");
return res.status(200).json({ message: "request added to the queue" });
});
function donate(to, cb) {
return donateAmount(to, config.payoutamountinether, cb);
}
function donateAmount(to, amount, cb) {
console.log(`donating ${amount} ETH to: ${to}`);
const tx = {
to,
value: ethers.utils.parseEther(amount.toString())
}
wallet.sendTransaction(tx)
.then((txObj) => {
// console.log('txHash', txObj.hash)
return cb(null, txObj.hash);
}).catch((e) => {
return cb(e.message);
});
}
// start API
app.listen(config.httpport, function () {
console.log(`faucet listening on port ${config.httpport}`);
});