-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
92 lines (79 loc) · 3.01 KB
/
app.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
// Retrieve
var fs = require("fs");
var http = require("http");
var walletRPC = require("bitcoin-core");
var express = require("express");
var bodyParser = require('body-parser');
var path = require("path")
var rateLimit = require('express-rate-limit');
var request = require('request');
var app = express();
var config = require("./config.json");
var sent = "none";
var message = "";
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
var limiter = new rateLimit({
windowMs: 60*60*1000, // 1 hour
max: 1,
onLimitReached: function(req, res, options){
sent = "timeout";
message = "Come back in 1 hour to claim again!";
res.redirect("/");
}
});
const walletSIGT = new walletRPC({
host: config.rpcHost,
port: config.rpcPort,
username: config.rpcUsername,
password: config.rpcPassword
});
var serverAddr = "";
walletSIGT.getAddressesByAccount(config.walletAccount, function(err, acnts){
serverAddr = acnts[0];
});
//Sets the fees for the tx
walletSIGT.setTxFee(0.001);
walletSIGT.setAccount(serverAddr, config.walletAccount);
app.use(express.static(__dirname + '/views'));
app.use("/sendAddress", limiter);
app.set('views', './views');
app.set('view engine', 'ejs');
app.get("/", function(req, res){
walletSIGT.getBalance(function(err, bal){
walletSIGT.getAccountAddress(config.walletAccount, function(err, addr){
res.render('index', {apiKey: config.googleCaptchaApiKey, faucetBalance: bal, faucetAddress: serverAddr, sentStatus: sent, statusMessage: message});
sent = "none";
message = "";
});
});
});
app.post("/sendAddress", function(req, res) {
res.setHeader('Content-Type', 'application/json');
if(req.body['g-recaptcha-response'] === undefined || req.body['g-recaptcha-response'] === '' || req.body['g-recaptcha-response'] === null) {
return res.json({"responseCode" : 1,"responseDesc" : "Please select captcha"});
}
// Put your secret key here.
var secretKey = config.googleCaptchaApiSecret;
// req.connection.remoteAddress will provide IP address of connected user.
var verificationUrl = "https://www.google.com/recaptcha/api/siteverify?secret=" + secretKey + "&response=" + req.body['g-recaptcha-response'] + "&remoteip=" + req.connection.remoteAddress;
// Hitting GET request to the URL, Google will respond with success or error scenario.
request(verificationUrl,function(error,response,body) {
body = JSON.parse(body);
// Success will be true or false depending upon captcha validation.
if(body.success !== undefined && !body.success) {
return res.json({"responseCode" : 1,"responseDesc" : "Failed captcha verification"});
}
});
walletSIGT.sendFrom(config.walletAccount, req.body.userAddress, config.faucetRate);
fs.appendFile("info.log", "Sending SIGT to address " + req.body.userAddress + "\n", function(err){
if(err) console.log(err);
console.log("Logging...")
});
if(req.rateLimit.statusCode != 429){
sent = "sent";
message = "SIGT has been sent!";
}
res.redirect("/");
});
app.listen(80);