mirror of
				https://github.com/karl0ss/homepage.git
				synced 2025-11-04 00:10:57 +00:00 
			
		
		
		
	Enhancement: forward cookies from request (#1804)
This commit is contained in:
		
							parent
							
								
									63f952509e
								
							
						
					
					
						commit
						d4edd432d8
					
				@ -3,6 +3,7 @@ import { performance } from "perf_hooks";
 | 
			
		||||
import { getServiceItem } from "utils/config/service-helpers";
 | 
			
		||||
import createLogger from "utils/logger";
 | 
			
		||||
import { httpProxy } from "utils/proxy/http";
 | 
			
		||||
import {importCookieHeader} from "utils/proxy/cookie-jar";
 | 
			
		||||
 | 
			
		||||
const logger = createLogger("ping");
 | 
			
		||||
 | 
			
		||||
@ -25,6 +26,10 @@ export default async function handler(req, res) {
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (req.headers.cookie) {
 | 
			
		||||
        importCookieHeader(pingURL, req.headers.cookie)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    try {
 | 
			
		||||
      let startTime = performance.now();
 | 
			
		||||
      let [status] = await httpProxy(pingURL, {
 | 
			
		||||
 | 
			
		||||
@ -37,3 +37,27 @@ export function addCookieToJar(url, headers) {
 | 
			
		||||
    cookieJar.setCookieSync(cookies[i], url.toString(), { ignoreError: true });
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function importCookieHeader(url, cookieHeader) {
 | 
			
		||||
    const cookies = cookieHeader.split(';');
 | 
			
		||||
    for (let i = 0; i < cookies.length; i += 1) {
 | 
			
		||||
        const [key, value] = cookies[i].trim().split('=');
 | 
			
		||||
 | 
			
		||||
        // If there's an existing cookie with a matching key for this url,
 | 
			
		||||
        // we want to update it. Otherwise, we add a new cookie
 | 
			
		||||
        let existingCookie;
 | 
			
		||||
        try {
 | 
			
		||||
            existingCookie = cookieJar.getCookiesSync(url).find(existing => existing.key === key);
 | 
			
		||||
        } catch (e) {
 | 
			
		||||
            console.debug(`Failed to get cookies for ${url}: ${e}`)
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (existingCookie) {
 | 
			
		||||
            existingCookie.value = value;
 | 
			
		||||
        } else {
 | 
			
		||||
            cookieJar.setCookieSync(new Cookie({
 | 
			
		||||
                key, value
 | 
			
		||||
            }), url.toString(), { ignoreError: true });
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -4,6 +4,7 @@ import validateWidgetData from "utils/proxy/validate-widget-data";
 | 
			
		||||
import { httpProxy } from "utils/proxy/http";
 | 
			
		||||
import createLogger from "utils/logger";
 | 
			
		||||
import widgets from "widgets/widgets";
 | 
			
		||||
import {importCookieHeader} from "utils/proxy/cookie-jar";
 | 
			
		||||
 | 
			
		||||
const logger = createLogger("genericProxyHandler");
 | 
			
		||||
 | 
			
		||||
@ -34,6 +35,10 @@ export default async function genericProxyHandler(req, res, map) {
 | 
			
		||||
        params.body = req.body;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      if (req.headers.cookie) {
 | 
			
		||||
          importCookieHeader(url, req.headers.cookie)
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      const [status, contentType, data] = await httpProxy(url, params);
 | 
			
		||||
 | 
			
		||||
      let resultData = data;
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user