This repository was archived by the owner on Jul 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
81 lines (72 loc) · 2.1 KB
/
index.js
File metadata and controls
81 lines (72 loc) · 2.1 KB
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
const https = require('https');
const axios = require('axios').default;
const proxy = require('express-http-proxy');
const express = require('express');
const cors = require('cors');
const RPC_NODES_TXT = 'https://raw.githubusercontent.com/ovrclk/net/master/mainnet/rpc-nodes.txt';
const PORT = 8888;
function getApp(rpcEndpoints) {
const app = express();
const whitelist = ['http://localhost:3000'];
const corsOptions = {
origin: function (origin, callback) {
if (whitelist.indexOf(origin) !== -1) {
callback(null, true)
} else {
callback(null, false);
}
}
};
// Enable CORS
app.options('*', cors(corsOptions));
app.use(cors(corsOptions));
// RPC Proxy
const selectRandomRPCEndpoint = () => {
return rpcEndpoints[Math.floor(Math.random() * rpcEndpoints.length)];
};
app.use('/rpc', proxy(selectRandomRPCEndpoint, {
memoizeHost: true
}));
// Provider Proxy
app.post('/provider/*', express.json(), function(req, res) {
if (!(req.body && req.body.providerUri && req.body.cert && req.body.key && req.body.type)) {
return;
}
const path = req.originalUrl.replace('/provider', '');
const uri = req.body.providerUri + path;
const httpsAgent = new https.Agent({
cert: req.body.cert,
key: req.body.key,
rejectUnauthorized: false
});
switch(req.body.type) {
case 'LEASE_STATUS':
case 'LEASE_LOGS':
case 'LEASE_EVENTS':
axios.get(uri, { httpsAgent }).then((response) => {
res.send(response.data);
});
break;
case 'SEND_MANIFEST':
if (!req.body.manifest) {
return;
}
axios.put(uri, req.body.manifest, { httpsAgent }).then((response) => {
res.send(response.data);
});
break;
}
});
return app;
}
axios.get(RPC_NODES_TXT).then((response) => {
const endpoints = response.data.split(/\r?\n/).filter((url) => {
return url.length > 0;
});
if (endpoints.length > 1) {
const app = getApp(endpoints);
app.listen(PORT, function() {
console.log(`Listening on port ${PORT}`);
});
}
});