-
Notifications
You must be signed in to change notification settings - Fork 3
/
TrtlCheckout.js
331 lines (263 loc) · 6.66 KB
/
TrtlCheckout.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/******************************************************************************
*
* MIT License
*
* Copyright (c) 2021 Benjamin Collins [email protected]
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*****************************************************************************/
"use strict";
const fs = require('fs')
const fetch = require('node-fetch')
const express = require('express')
const WebSocket = require('ws')
const sqlite3 = require('sqlite3').verbose();
const Handlebars = require('handlebars');
const http = require('http');
const app = express()
const port = 2800
const addr = 'TRTLv3bpWo5KR6ACMJ4cJeXq2CVtEwEwZhTJvSyriKjYjWEMS9Rb9Wwf4mDo3WTzDZNP1gPHYp8bJ7VYCgbHGxcvgFjXPUsUcgB';
const view = '9fcb0087252c147657af23700810cad0bceee0b4fdf2a4479406b9f2636eae0d';
const server = http.createServer(app);
app.use(express.json());
const db = new sqlite3.Database('db.sqlite');
db.serialize(function() {
db.run(`
CREATE TABLE IF NOT EXISTS dat_models (
name VARCHAR(255) UNIQUE,
views INT DEFAULT 0,
downloads INT DEFAULT 0
)
`);
db.run(`
CREATE TABLE IF NOT EXISTS dat_stats (
paymentId VARCHAR(255) UNIQUE,
startTime UNSIGNED BIG INT,
confTime UNSIGNED BIG INT,
completeTime UNSIGNED BIG INT,
details TEXT
)
`);
db.run(`
INSERT OR IGNORE INTO dat_models
( name )
VALUES
( 'dashie' )
`)
});
const mem = {}
function random (len) {
len = len || 4;
let chars = 'abcdefghijklmnopqrstuvwxyz';
// Pick characers randomly
let str = '';
for (let i = 0; i < len; i++) {
str += chars.charAt(Math.floor(Math.random() * chars.length));
}
return str;
};
app.get('/trtl/prepare', async function(req, res) {
const body = {
amount : 50000,
address : addr,
privateViewKey : view,
callback : "https://shellshop.lol/trtl/process",
name : "ShellShop_" + random(),
confirmations : 10
}
let prep = await fetch('https://api.turtlepay.io/v2/new', {
headers: {
'Accept' : 'application/json',
'Content-Type' : 'application/json'
},
method : "POST",
body: JSON.stringify(body)
});
let data = await prep.json();
res.json(data);
let sql = `
INSERT INTO dat_stats
( paymentId, startTime)
VALUES
( ?, ? )
`;
let args = [
data.paymendId || data.paymentId,
Date.now()
];
db.run(sql, args, function(err) {
if(err) {
throw err;
}
});
});
app.get('/assets/dashie.rar*', async function(req, res, next) {
console.log('An attempt was made!!!!');
console.log(req.query.paymentId);
console.log(' ----- ');
if(req.query.paymentId === 'bypass') {
// Do nothing, force allow the user to download
} else if(!mem[req.query.paymentId]) {
res.status(403);
return res.end('Not Authorized');
} else {
let sql = `
UPDATE
dat_models
SET
downloads = downloads + 1
WHERE
name = 'dashie'
`;
db.run(sql, function(err) {
if(err) {
console.log("Error 100");
throw err;
}
});
}
next();
});
app.post('/trtl/process', function(req, res) {
if(req.body.confirmationsRemaining % 10 === 0) {
console.log("%s %s %s", req.body.paymentId, req.body.status, req.body.confirmationsRemaining);
}
if(!mem[req.body.paymentId]) {
console.log('ConfIRMED PAYMENT!!!');
console.log(req.body);
wss.clients.forEach(function each(client) {
if(client.paymentId !== req.body.paymentId) {
return;
}
if (client.readyState !== WebSocket.OPEN) {
return;
}
console.log('Payment confirmed, sending!!');
client.send('Payment confirmed');
});
let sql = `
UPDATE
dat_stats
SET
confTime = ?
WHERE
paymentId = ?
`;
let args = [
Date.now(),
req.body.paymentId
];
db.run(sql, args, function(err) {
if(err) {
console.log("Error 200");
throw err;
}
});
}
if(req.body.status === 200) {
console.log('Complete!!');
console.log(req.body);
setTimeout( function() {
delete mem[req.body.paymentId];
}, 3600000);
let sql = `
UPDATE
dat_stats
SET
completeTime = ?,
details = ?
WHERE
paymentId = ?
`;
let args = [
Date.now(),
JSON.stringify(req.body),
req.body.paymentId
];
db.run(sql, args, function(err) {
if(err) {
console.log("Error 300");
throw err;
}
});
} else if(req.body.status !== 102) {
console.log(req.body);
}
mem[req.body.paymentId] = req.body.status;
res.status(200).end();
});
const source = fs.readFileSync('public/index.html').toString();
const template = Handlebars.compile(source);
app.get(['/', 'index.html'], function(req, res) {
let sql = `
SELECT
views,
downloads
FROM
dat_models
WHERE
name = 'dashie'
`;
db.get(sql, function(err, data) {
if(err) {
console.log("Error 500");
throw err;
}
data.views++;
data.views = data.views.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")
data.downloads = data.downloads.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")
let result = template(data)
res.writeHead(200, {'Content-Type' : 'text/html'});
res.end(result);
});
sql = `
UPDATE
dat_models
SET
views = views + 1
WHERE
name = 'dashie'
`;
db.run(sql, function(err) {
if(err) {
console.log("Error 600");
throw err;
}
});
});
app.use(express.static('public'))
const wss = new WebSocket.Server({ server });
wss.on('connection', (ws) => {
ws.on('message', (message) => {
if(message === 'ping') {
return;
}
console.log(message);
console.log('Setting payment id for ws client', message);
ws.paymentId = message;
if(mem[message]) {
console.log('Payment confirmed, reporting');
ws.send('Payment confirmed');
}
});
});
server.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})