-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
110 lines (97 loc) · 2.77 KB
/
server.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
require("dotenv").config();
const express = require("express");
const axios = require("axios");
const cors = require('cors');
axios.defaults.headers.post["accept-encoding"] = "";
const bodyParser = require("body-parser");
const PORT = process.env.SERVER_PORT || 3000;
const SHIELD_FORWARD_URLS = process.env.SHIELD_FORWARD_URLS;
const FEE_PK = process.env.FEE_PRIVATE_KEY;
const PARALLEL_CALL = process.env.PARALLEL_CALL=="true";
const { confirmResponse, muonFeeSignature } = require("./utils/muon-helpers");
global.MuonAppUtils = require("./muonapp-utils");
const originalForwardUrls = SHIELD_FORWARD_URLS.split(",");
let forwardUrls = originalForwardUrls;
const router = express();
router.use(bodyParser.json());
router.use(bodyParser.urlencoded({ extended: true }));
router.use(cors({
origin: '*'
}));
router.get("/", (req, res) => {
res.json({ message: "Muon Light Shield Node" });
});
router.use("*", async (req, res, next) => {
let mixed = {
...req.query,
...req.body,
};
let {
app,
method,
fee = {},
params = {},
nSign,
mode = "sign",
gwSign,
} = mixed;
if (!["sign", "view"].includes(mode)) {
return res.json({
success: false,
error: { message: "Request mode is invalid" },
});
}
gwSign = false; // do not allow gwSign for shiled nodes
// TODO: Implement user authentication and rate limiting for each project.
// This will prevent attackers from sending multiple requests and depleting the fees
if (FEE_PK) {
fee = muonFeeSignature(FEE_PK, process.env.FEE_APP_ID);
}
let requestData = { app, method, params, nSign, mode, gwSign };
if (fee && fee.spender) {
requestData["fee"] = fee;
}
if (forwardUrls.length == 0) forwardUrls = originalForwardUrls;
let result;
if(PARALLEL_CALL && forwardUrls.length > 1){
result = await Promise.any(
forwardUrls.map(api => {
return axios.post(api, requestData)
.then(({data}) => {
//TODO: filter invalid responses
return data;
})
})
).catch(e => {
console.log(e);
})
}else{
let forwardUrl = forwardUrls[0];
result = await axios
.post(forwardUrl, requestData)
.then(({ data }) => data)
.catch((error) => {
if (error.code === "ECONNREFUSED" || error.code === "ECONNABORTED")
forwardUrls.shift();
return res.json({
success: false,
error,
});
});
}
if (result.success) {
try {
await confirmResponse(requestData, result.result);
} catch (ex) {
console.log(ex);
return res.json({
success: false,
error: ex,
});
}
}
return res.json(result);
});
router.listen(PORT, () => {
console.log(`Server is running on port ${PORT}.`);
});