This repository has been archived by the owner on Jun 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
server.js
191 lines (143 loc) · 4.81 KB
/
server.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
188
189
190
191
const fs = require('fs');
const http = require('http');
const crypto = require('crypto');
const ejs = require('ejs');
const Koa = require('koa');
const bodyParser = require('koa-bodyparser');
const Router = require('koa-router');
const _static = require('koa-static');
const json = require('koa-json');
const socketIo = require('socket.io');
const log = require('debug')('zfaucet:server');
const config = require('./config');
const redis = require('./lib/redis');
const db = require('./lib/db');
const utils = require('./lib/utils');
const coinhive = require('./lib/coinhive');
const reports = require('./lib/reports');
const indexTemplate = ejs.compile(fs.readFileSync(`${__dirname}/views/index.ejs`, 'utf8'));
// create app and config vars
const app = new Koa();
const router = new Router();
// make the public folder viewable
app.use(async (ctx, next) => {
try {
await next();
} catch (error) {
ctx.response.status = 500;
log(`error: ${error.message}`);
/* istanbul ignore next */
ctx.body = process.env.NODE_ENV === 'production' ?
'Internal Server Error' :
error.toString();
}
});
app.use(_static('public'));
app.use(bodyParser());
app.use(json());
// set the view engine to ejs
// app.set('view engine', 'ejs');
router.get('/', async ctx => {
ctx.body = indexTemplate({withdrawThreshold: config.withdrawThreshold});
});
router.get('/api/recent', async ctx => {
const rows = await db.searchDrips({});
ctx.body = utils.readableTime(rows);
});
router.get('/api/recent/:address', async ctx => {
const payoutAddress = ctx.params.address;
if (!utils.isAddress(payoutAddress))
throw new Error('Please enter a valid address');
// find the drips for the user and return
const rows = await db.searchDrips({payoutAddress});
ctx.body = utils.readableTime(rows);
});
router.get('/api/balance/:address', async ctx => {
if (!utils.isAddress(ctx.params.address))
throw new Error('Please enter a valid address');
// check the balance from coinhive and return
ctx.body = await coinhive.getBalance(ctx.params.address);
});
router.get('/api/withdraw/:address', async ctx => {
if (!utils.isAddress(ctx.params.address))
throw new Error('Please enter a valid address');
// make sure the user has enough balance
const balResponse = await coinhive.getBalance(ctx.params.address);
if (balResponse.balance < config.withdrawThreshold)
throw new Error('Not enough coins');
// make sure the withdrawal was successful
const withReponse = await coinhive.withdraw(ctx.params.address,
config.withdrawThreshold);
if (withReponse.success !== true)
throw new Error(`Withdraw Failed: ${withReponse.error}`);
// add the withdrawal to the queue and return true
await db.createDrip(ctx.params.address);
ctx.body = true;
});
router.post('/api/report/:algorithm/:address/:hashRate', async ctx => {
let {algorithm, address, hashRate} = ctx.params;
const supportedAlgorithms = [
'xmr',
'zec'
];
hashRate = Number(hashRate);
if (supportedAlgorithms.indexOf(algorithm) === -1)
throw new Error('Please enter a supported algorithm');
if (!utils.isAddress(address))
throw new Error('Please enter a valid address');
if (isNaN(hashRate))
throw new Error('Please enter a valid hash rate');
await reports.insert({
algorithm,
address,
hashRate
});
ctx.body = true;
});
router.get('/api/reports', async ctx => {
ctx.body = await reports.find(25);
});
router.get('/api/reports/algorithm/:algorithm', async ctx => {
ctx.body = await reports.find(25, {
algorithm: ctx.params.algorithm
});
});
router.get('/api/reports/address/:address', async ctx => {
ctx.body = await reports.find(25, {
address: ctx.params.address
});
});
router.get('/api/external/withdraw', async ctx => {
const privateKey = ctx.request.query.key;
const address = ctx.request.query.address;
const amount = Number(ctx.request.query.amount);
log('privateKey', privateKey);
log('address', address);
log('amount', amount);
const hash = crypto.createHash('sha256');
hash.update(privateKey, 'hex');
const publicKey = hash.digest().toString('hex');
log('publicKey', publicKey);
if ((await redis.sismember('external-keys', publicKey)) !== 1)
throw new Error('Bad key');
if (typeof amount !== 'number' || !isFinite(amount))
throw new Error('Bad amount');
if (!utils.isAddress(address))
throw new Error('Please enter a valid address');
await db.createDrip(address, amount);
ctx.body = "";
});
app
.use(router.routes())
.use(router.allowedMethods());
const server = http.createServer(app.callback());
const io = socketIo(server);
io.on('connection', require('./lib/io/report'));
io.on('connection', require('./lib/io/chat'));
/* istanbul ignore next */
// start the server, if running this script alone
if (require.main === module)
server.listen(config.port, () => {
console.log('Server started! At http://localhost:' + config.port);
});
module.exports = app;