-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathreqHandler.js
101 lines (84 loc) · 3.13 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
'use strict';
var utils = require("./utils");
const url = require('url');
const g_constants = require('./constants');
exports.handle = function(app)
{
app.get('/', function (req, res) {res.render('index.html');});
app.get('*.blockr.io/api/v1/address/*', httpProxy);
app.post('*.blockr.io/api/v1/tx/push', httpProxy);
app.post('/api/v1/tx/push/ppc', function (req, res) {
var strJSON = '{"jsonrpc": "1.0", "id":"curltest", "method": "sendrawtransaction", "params": ["'+req.body.hex+'"] }';
utils.postString(g_constants.my_domain, 9902, "/", {'Content-Type': 'text/plain', 'Authorization': 'Basic a3p2OnEyMjEw'}, strJSON, function(result) {
if (result.data)
{
try {
if (result.success)
result.data = JSON.parse(result.data);
if (result.data.error && result.data.error.message)
result.message = result.data.error.message+"<br>";
if (result.data.result)
result.data = result.data.result
else
result.success = false;
}
catch(e) {}
}
else
{
result.success = false;
}
const ret = result.success ?
{status: result.success, message: result.message || "", data: result.data || ""} :
{status: result.success, message: 0, data: result.message || ""};
res.end(JSON.stringify(ret));
});
});
};
function httpProxy(req, res)
{
var ph = url.parse(req.url);
const host = ph.path.substr(1, ph.path.indexOf('/', 1)-1);
const path = ph.path.substr(ph.path.indexOf('/', 1));
if (req.method == 'POST')
{
const str = '{"hex" : "'+req.body.hex+'"}';
utils.postJSON(host, path, str, function(err){
res.writeHead(200, {"Content-Type": "application/json"});
res.end(err.data);
})
return;
}
var options = {
port: 80,
hostname: host,
method: req.method,
path: path,
headers: req.headers
};
options.headers.host = host;
console.log('httpProxy: '+JSON.stringify(options));
var proxyRequest = require("http").request(options);
proxyRequest.on('response', function(proxyResponse) {
console.log('proxy response proxyResponse.statusCode='+proxyResponse.statusCode);
res.writeHead(proxyResponse.statusCode, proxyResponse.headers);
proxyResponse.on('data', function(chunk) {
res.write(chunk, 'binary');
});
proxyResponse.on('end', function() {
res.end();
});
});
req.on('data', function(chunk) {
proxyRequest.write(chunk, 'binary');
});
req.on('end', function() {
proxyRequest.end()
});
proxyRequest.on('error', function(e) {
console.log('proxyRequest error' + JSON.stringify(e));
res.end(JSON.stringify(e));
});
/*if (req.body && req.body.hex)
proxyRequest.end('{"hex" : "'+req.body.hex+'"}');*/
}