-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathreqHandler.js
112 lines (87 loc) · 2.85 KB
/
reqHandler.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
'use strict';
const utils = require("./utils");
const g_constants = require("./constants");
let isKnownCoin = {};
exports.handle = function(app)
{
app.post('/', OnRequest);
};
let g_reqHandlers = {};
async function OnRequest(req, res)
{
try
{
const coin = JSON.parse(new Buffer(req.headers['coin-info'], 'base64').toString('ascii'));
const auth = req.headers['authorization'];
let post_data = await processPost(req, res);
//console.log('auth: '+auth+'\ncoin:'+JSON.stringify(coin)+'\nPOST:'+post_data+"\n");
const headers = {
'Content-Type': 'text/plain',
'Authorization': auth
}
if (!isKnownCoin[coin.name])
FillData(coin, headers);
try {
const strHandle = "try "+coin.name+" "+JSON.parse(post_data).method;
if (g_reqHandlers[strHandle] && JSON.parse(post_data).method == "getbalance" && Date.now()-g_reqHandlers[strHandle]*1 < 3000)
{
res.end("");
return;
}
if (JSON.parse(post_data).method == "getbalance")
g_reqHandlers[strHandle] = Date.now();
utils.log2(strHandle);
require("./RPC/"+JSON.parse(post_data).method).Run(coin, headers, post_data, res);
}
catch(e) {
utils.log2("OnRequest catch 2: " + e.message);
utils.postString(coin.hostname, {'nPort' : coin.port, 'name' : "http"}, "/", headers, post_data, result => {
//console.log(result.data);
res.end(result.data || "");
});
}
}
catch(e) {
utils.log2("OnRequest catch 1: " + e.message);
res.end("");
}
}
function processPost(request, response)
{
let queryData = "";
return new Promise(ok => {
request.on('data', data => {
queryData += data;
if(queryData.length > 1e6) {
queryData = "";
response.writeHead(413, {'Content-Type': 'text/plain'}).end();
request.connection.destroy();
}
});
request.on('end', () => {
ok(queryData);
});
});
}
async function OneInit()
{
}
async function FillData (coin, headers)
{
OneInit();
isKnownCoin[coin.name] = true;
//FillAll(coin, headers);
FillLast(coin, headers, 100);
async function FillLast(coin, headers, count = 100)
{
try {
await utils.SaveLastTransactions(coin, headers, count);
}
catch(e) {
utils.log2('FillLast catch error for '+coin.name+" "+(e.message || ""));
utils.Offline(coin.name, true);
}
console.log('WAIT 60 sec for '+coin.name);
setTimeout(FillLast, 60000, coin, headers, 100);
}
}