2026-03-01 22:18:07 -05:00
|
|
|
module.exports = async (server) => {
|
|
|
|
|
|
2026-03-01 22:58:17 -05:00
|
|
|
const { api, env, handler, path, util } = server;
|
2026-03-01 22:18:07 -05:00
|
|
|
|
2026-03-01 22:47:48 -05:00
|
|
|
server.inject("kiri", "main.js");
|
|
|
|
|
|
2026-03-01 22:18:07 -05:00
|
|
|
if (!(env.debug || env.electron)) {
|
|
|
|
|
util.log('not a valid context for proxy');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
path.full({
|
|
|
|
|
"/printer/print/start": proxy_post,
|
|
|
|
|
"/server/files/upload": proxy_post,
|
|
|
|
|
"/api/files/local": proxy_post,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function proxy_post(req, res, next) {
|
2026-03-01 22:58:17 -05:00
|
|
|
handler.addCORS(req, res);
|
2026-03-01 22:18:07 -05:00
|
|
|
if (req.method === 'POST') {
|
|
|
|
|
let { url, headers } = req;
|
|
|
|
|
let chunks = [];
|
|
|
|
|
let host = headers['x-host'];
|
|
|
|
|
let apik = headers['x-api-key'] ?? '';
|
|
|
|
|
let cont = headers['content-type'] ?? 'application/binary';
|
|
|
|
|
req
|
|
|
|
|
.on('data', data => chunks.push(data) )
|
|
|
|
|
.on('end', () => {
|
|
|
|
|
req.app.post = Buffer.concat(chunks);
|
|
|
|
|
if (host) {
|
|
|
|
|
fetch(host + url, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': cont,
|
|
|
|
|
'X-Api-Key': apik
|
|
|
|
|
},
|
|
|
|
|
body: req.app.post
|
|
|
|
|
}).then(result => {
|
|
|
|
|
if (result.ok) {
|
|
|
|
|
res.writeHead(200, 'OK');
|
|
|
|
|
} else {
|
|
|
|
|
res.writeHead(500, 'Failed to proxy');
|
2026-03-01 23:13:45 -05:00
|
|
|
console.log({ result });
|
2026-03-01 22:18:07 -05:00
|
|
|
}
|
|
|
|
|
res.end();
|
|
|
|
|
});
|
|
|
|
|
} else {
|
2026-04-12 13:56:07 -04:00
|
|
|
console.log('drop proxy due to lack of host', { chunks: chunks.map(c => c.toString()) });
|
2026-03-01 22:18:07 -05:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
next();
|
|
|
|
|
}
|
|
|
|
|
}
|