2022-11-23 11:51:53 -08:00
|
|
|
import { JSONRPCClient, JSONRPCErrorException } from "json-rpc-2.0";
|
|
|
|
|
|
|
|
import { formatApiCall } from "utils/proxy/api-helpers";
|
|
|
|
import { httpProxy } from "utils/proxy/http";
|
|
|
|
import getServiceWidget from "utils/config/service-helpers";
|
|
|
|
import createLogger from "utils/logger";
|
|
|
|
import widgets from "widgets/widgets";
|
|
|
|
|
|
|
|
const logger = createLogger("jsonrpcProxyHandler");
|
|
|
|
|
|
|
|
export async function sendJsonRpcRequest(url, method, params, username, password) {
|
|
|
|
const headers = {
|
|
|
|
"content-type": "application/json",
|
|
|
|
"accept": "application/json"
|
|
|
|
}
|
|
|
|
|
|
|
|
if (username && password) {
|
2022-11-23 20:07:34 -08:00
|
|
|
headers.authorization = `Basic ${Buffer.from(`${username}:${password}`).toString("base64")}`;
|
2022-11-23 11:51:53 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
const client = new JSONRPCClient(async (rpcRequest) => {
|
2022-11-23 20:07:34 -08:00
|
|
|
const body = JSON.stringify(rpcRequest);
|
|
|
|
headers['content-length'] = Buffer.byteLength(body);
|
2022-11-23 11:51:53 -08:00
|
|
|
const httpRequestParams = {
|
|
|
|
method: "POST",
|
|
|
|
headers,
|
2022-11-23 20:07:34 -08:00
|
|
|
body
|
2022-11-23 11:51:53 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
const [status, contentType, data] = await httpProxy(url, httpRequestParams);
|
|
|
|
if (status === 200) {
|
2022-11-23 20:07:34 -08:00
|
|
|
const json = JSON.parse(data.toString());
|
2022-11-23 11:51:53 -08:00
|
|
|
|
|
|
|
// in order to get access to the underlying error object in the JSON response
|
|
|
|
// you must set `result` equal to undefined
|
|
|
|
if (json.error && (json.result === null)) {
|
|
|
|
json.result = undefined;
|
|
|
|
}
|
|
|
|
return client.receive(json);
|
|
|
|
}
|
|
|
|
|
2022-11-23 20:07:34 -08:00
|
|
|
return Promise.reject(data?.error ? data : new Error(data.toString()));
|
2022-11-23 11:51:53 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await client.request(method, params);
|
|
|
|
return [200, "application/json", JSON.stringify(response)];
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
if (e instanceof JSONRPCErrorException) {
|
2022-11-23 20:07:34 -08:00
|
|
|
logger.warn("Error calling JSONPRC endpoint: %s. %s", url, e.message);
|
2022-11-23 11:51:53 -08:00
|
|
|
return [200, "application/json", JSON.stringify({result: null, error: {code: e.code, message: e.message}})];
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.warn("Error calling JSONPRC endpoint: %s. %s", url, e);
|
|
|
|
return [500, "application/json", JSON.stringify({result: null, error: {code: 2, message: e.toString()}})];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default async function jsonrpcProxyHandler(req, res) {
|
|
|
|
const { group, service, endpoint: method } = req.query;
|
|
|
|
|
|
|
|
if (group && service) {
|
|
|
|
const widget = await getServiceWidget(group, service);
|
|
|
|
const api = widgets?.[widget.type]?.api;
|
|
|
|
|
|
|
|
if (!api) {
|
|
|
|
return res.status(403).json({ error: "Service does not support API calls" });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (widget) {
|
|
|
|
const url = formatApiCall(api, { ...widget });
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
const [status, contentType, data] = await sendJsonRpcRequest(url, method, null, widget.username, widget.password);
|
2022-11-23 20:07:34 -08:00
|
|
|
return res.status(status).end(data);
|
2022-11-23 11:51:53 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.debug("Invalid or missing proxy service type '%s' in group '%s'", service, group);
|
|
|
|
return res.status(400).json({ error: "Invalid proxy service type" });
|
|
|
|
}
|