-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
114 lines (91 loc) · 2.97 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const fetch = require('node-fetch');
const distDir = __dirname + '/www/';
const { createProxyMiddleware } = require('http-proxy-middleware');
var cron = require('node-cron');
const fs = require('fs');
app.use(express.static(distDir));
async function getEnabledNodes() {
let date = new Date();
let fileName = 'servers' + date.getFullYear() + date.getMonth() + date.getDate() + '.txt';
try {
if(!fs.existsSync(fileName)){
let nodes = await fetch('https://sapi.smartcash.cc/v1/smartnode/check/ENABLED');
nodes = await nodes.json();
const servers = nodes.map((node) => 'http://' + node.ip.replace(':9678', ':8080'));
fs.writeFileSync(fileName, JSON.stringify(servers));
}
return JSON.parse(fs.readFileSync(fileName));
} catch (err) {
console.error(err);
}
}
async function electedSapi(){
const sapis = await getEnabledNodes();
return sapis[Math.floor(Math.random() * sapis.length)];
}
async function getRandomSapiUrl() {
let prefix = `http://${await electedSapi()}:8080`;
return prefix;
}
const options = {
target: `https://sapi.smartcash.cc`,
changeOrigin: true,
onProxyReq: async (proxyReq, req, res) => {
try {
proxyReq.setHeader('host', await electedSapi());
} catch (error) {
}
},
onProxyRes: (onProxyRes, req, res) => {
},
};
const sapiProxy = createProxyMiddleware(options);
app.use('/v1', sapiProxy);
app.use(bodyParser.json());
app.get('/', function(req, res) {
res.sendFile(distDir + 'index.html');
});
app.get('/home', function(req, res) {
res.sendFile(distDir + 'index.html');
});
app.get('/tx/*', function(req, res) {
res.sendFile(distDir + 'index.html');
});
app.get('/block/*', function(req, res) {
res.sendFile(distDir + 'index.html');
});
app.get('/address/*', function(req, res) {
res.sendFile(distDir + 'index.html');
});
app.get('/ext/getmoneysupply', function(req, res) {
res.setHeader('Content-Type', 'application/json');
fetch('https://sapi.smartcash.cc/v1/blockchain/supply')
.then(res => res.json())
.then(json => {
res.send(json.CurrentSupply.toString());
});
});
app.get('/api/getTransactions/:address', async function(req, res) {
res.setHeader('Content-Type', 'application/json');
fetch(await getRandomSapiUrl() + "/v1/address/transactions", {
// Adding method type
method: "POST",
// Adding body or contents to send
body: JSON.stringify({
"pageNumber": 1,
"pageSize": 10,
"ascending": false,
"address": req.params.address
}),
// Adding headers to the request
headers: {
"Content-type": "application/json; charset=UTF-8"
}
}).then(response => response.json())
// Displaying results to console
.then(json => res.send(json));
});
module.exports = app;