mirror of
https://github.com/DI0IK/homepage-plus.git
synced 2025-07-07 14:18:47 +00:00
Add the Jackett widget
- add the follow-redirect package - add the tough-cookie package Jackett API uses a redirect mechanism to set a CSRF token. This CSRF token is stored in a cookie that is required to be present or the API won't work.
This commit is contained in:
parent
945ed854a4
commit
f750876425
8 changed files with 135 additions and 5 deletions
|
@ -1,9 +1,44 @@
|
|||
/* eslint-disable prefer-promise-reject-errors */
|
||||
import https from "https";
|
||||
import http from "http";
|
||||
/* eslint-disable no-param-reassign */
|
||||
import { http, https } from "follow-redirects";
|
||||
import { Cookie, CookieJar } from 'tough-cookie';
|
||||
|
||||
const cookieJar = new CookieJar();
|
||||
|
||||
function addCookieHandler(url, params) {
|
||||
// add cookie header, if we have one in the jar
|
||||
const existingCookie = cookieJar.getCookieStringSync(url.toString());
|
||||
if (existingCookie) {
|
||||
params.headers = params.headers ?? {};
|
||||
params.headers.Cookie = existingCookie;
|
||||
}
|
||||
|
||||
// handle cookies during redirects
|
||||
params.beforeRedirect = (options, responseInfo) => {
|
||||
const cookieHeader = responseInfo.headers['set-cookie'];
|
||||
if (!cookieHeader || cookieHeader.length === 0) return;
|
||||
|
||||
let cookies = null;
|
||||
if (cookieHeader instanceof Array) {
|
||||
cookies = cookieHeader.map(Cookie.parse);
|
||||
}
|
||||
else {
|
||||
cookies = [Cookie.parse(cookieHeader)];
|
||||
}
|
||||
|
||||
for (let i = 0; i < cookies.length; i += 1) {
|
||||
cookieJar.setCookieSync(cookies[i], options.href);
|
||||
}
|
||||
|
||||
const cookie = cookieJar.getCookieStringSync(options.href);
|
||||
options.headers = options.headers ?? {};
|
||||
options.headers.Cookie = cookie;
|
||||
};
|
||||
}
|
||||
|
||||
export function httpsRequest(url, params) {
|
||||
return new Promise((resolve, reject) => {
|
||||
addCookieHandler(url, params);
|
||||
const request = https.request(url, params, (response) => {
|
||||
const data = [];
|
||||
|
||||
|
@ -30,6 +65,7 @@ export function httpsRequest(url, params) {
|
|||
|
||||
export function httpRequest(url, params) {
|
||||
return new Promise((resolve, reject) => {
|
||||
addCookieHandler(url, params);
|
||||
const request = http.request(url, params, (response) => {
|
||||
const data = [];
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue