diff --git a/package.json b/package.json index a4413a0..c88c4eb 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "author": "Flummi & jkhsjdhjs", "license": "WTFPL", "dependencies": { + "flumm-fetch-cookies": "git+https://gitfap.de/keinBot/flumm-fetch-cookies.git", "long-timeout": "^0.1.1", "pg-promise": "^7.3.2", "stringify-object": "^3.2.1", diff --git a/src/clients/tg.mjs b/src/clients/tg.mjs index 055e3d7..905e384 100644 --- a/src/clients/tg.mjs +++ b/src/clients/tg.mjs @@ -4,7 +4,7 @@ import { spurdo } from "../inc/spurdo"; import { schmuser } from "../inc/schmuser"; import { schwitzer } from "../inc/schwitzer"; -import fetch from "../inc/fetch"; +import fetch from "flumm-fetch-cookies"; import EventEmitter from "events"; export class tg extends EventEmitter { diff --git a/src/inc/fetch/cookie-jar.mjs b/src/inc/fetch/cookie-jar.mjs deleted file mode 100644 index 82e1751..0000000 --- a/src/inc/fetch/cookie-jar.mjs +++ /dev/null @@ -1,39 +0,0 @@ -import Cookie from "./cookie"; - -export default class CookieJar { - constructor(flags, file, cookies) { - this.cookies = new Map(); - } - addCookie(c, fromURL) { - if(typeof c === "string") - c = new Cookie(c, fromURL); - if(!(c instanceof Cookie)) - throw new TypeError("First parameter is neither a string nor a cookie!"); - if(!this.cookies.get(c.domain)) - this.cookies.set(c.domain, new Map()); - this.cookies.get(c.domain).set(c.name, c); - } - domains() { - return [...this.cookies.keys()]; - } - *iterValidForRequest(domain, url) { - for(const cookie of this.iter(domain)) - if(cookie.isValidForRequest(url)) - yield cookie; - } - *iterValid() { - for(const cookie of this.iterAll()) - if(!cookie.hasExpired()) - yield cookie; - } - *iter(domain) { - for(const cookie of (this.cookies.get(domain) || []).values()) - yield cookie; - } - deleteExpired() { - const filteredCookies = [...this.iterValid()]; - this.cookies = new Map(); - filteredCookies.forEach(c => this.addCookie(c)); - } -}; - diff --git a/src/inc/fetch/cookie.mjs b/src/inc/fetch/cookie.mjs deleted file mode 100644 index 743db09..0000000 --- a/src/inc/fetch/cookie.mjs +++ /dev/null @@ -1,119 +0,0 @@ -import urlParser from "url"; - -const validateHostname = (cookieHostname, requestHostname, subdomains) => { - cookieHostname = cookieHostname.toLowerCase(); - requestHostname = requestHostname.toLowerCase(); - if(requestHostname === cookieHostname || (subdomains && requestHostname.endsWith("." + cookieHostname))) - return true; - return false; -}; - -const validatePath = (cookiePath, requestPath) => { - cookiePath = cookiePath.toLowerCase(); - requestPath = requestPath.toLowerCase(); - if(cookiePath.endsWith("/")) - cookiePath = cookiePath.slice(0, -1); - if(requestPath.endsWith("/")) - requestPath = requestPath.slice(0, -1); - return (requestPath + "/").startsWith(cookiePath + "/"); -}; - -const splitN = (str, sep, n) => { - const splitted = str.split(sep); - if(n < splitted.length - 1) { - splitted[n] = splitted.slice(n).join(sep); - splitted.splice(n + 1); - } - return splitted; -}; - -export default class Cookie { - constructor(str, url) { - if(typeof str !== "string") - throw new TypeError("Input not a string"); - - const splitted = str.split("; "); - [this.name, this.value] = splitN(splitted[0], "=", 1); - if(this.value.startsWith("\"") && this.value.endsWith("\"")) - this.value = this.value.slice(1, -1); - - for(let i = 1; i < splitted.length; i++) { - let [k, v] = splitN(splitted[i], "=", 1); - k = k.toLowerCase(); - if(v) { - if(k === "expires") { - if(this.expiry) // max-age has precedence over expires - continue; - if(!/^(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun), \d{2}[ -](?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[ -]\d{2,4} \d{2}:\d{2}:\d{2} GMT$/.test(v) - || (this.expiry = new Date(v)) === "Invalid Date") - throw new TypeError("Invalid value for Expires \"" + v + "\"!"); - } - else if(k === "max-age") { - const seconds = ~~+v; - if(seconds.toString() !== v) - throw new TypeError("Invalid value for Max-Age \"" + v + "\"!"); - this.expiry = new Date(); - this.expiry.setSeconds(this.expiry.getSeconds() + seconds); - } - else if(k === "domain") { - if(v.startsWith(".")) - v = v.substring(1); - this.domain = v; - this.subdomains = true; - } - else if(k === "path") { - this.path = v; - } - else if(k === "samesite") // only relevant for cross site requests, so not for us - continue; - else - throw new TypeError("Invalid key \"" + k + "\" specified!"); - } - else { - if(k === "secure") - this.secure = true; - else if(k === "httponly") // only relevant for browsers - continue; - else - throw new TypeError("Invalid key \"" + k + "\" specified!"); - } - } - if(!this.domain) { - this.domain = urlParser.parse(url).hostname; - this.subdomains = false; - } - if(!this.path) - this.path = "/"; - if(this.name.toLowerCase().startsWith("__secure-") && (!this.secure || !url.toLowerCase().startsWith("https:"))) - throw new TypeError("Cookie has \"__Secure-\" prefix but \"Secure\" isn't set or the cookie is not set via https!"); - if(this.name.toLowerCase().startsWith("__host-") && (!this.secure || !url.toLowerCase().startsWith("https:") || this.domain || this.path !== "/")) - throw new TypeError("Cookie has \"__Host-\" prefix but \"Secure\" isn't set, the cookie is not set via https, \"Domain\" is set or \"Path\" is not equal to \"/\"!"); - } - static fromObject(obj) { - let c = Object.assign(Object.create(this.prototype), obj); - if(c.expiry && typeof c.expiry === "string") - c.expiry = new Date(c.expiry); - return c; - } - serialize() { - return this.name + "=" + this.value; - } - hasExpired() { - return this.expiry && this.expiry < new Date(); - } - isValidForRequest(url) { - if(this.hasExpired()) - return false; - const parsedURL = urlParser.parse(url); - if(parsedURL.protocol !== "http:" && parsedURL.protocol !== "https:") - return false; - if(this.secure && parsedURL.protocol !== "https:") - return false; - if(!validateHostname(this.domain, parsedURL.hostname, this.subdomains)) - return false; - if(!validatePath(this.path, parsedURL.pathname)) - return false; - return true; - } -}; - diff --git a/src/inc/fetch/fetch.mjs b/src/inc/fetch/fetch.mjs deleted file mode 100644 index e14f6f1..0000000 --- a/src/inc/fetch/fetch.mjs +++ /dev/null @@ -1,35 +0,0 @@ -import http from "http"; -import https from "https"; -import url from "url"; -import querystring from "querystring"; - -const readdata = (res, mode, data = "") => new Promise((resolve, reject) => res - .setEncoding("utf8") - .on("data", chunk => data += chunk) - .on("end", () => { - switch(mode) { - case "text": resolve(data); break; - case "json": try { resolve(JSON.parse(data)); } catch(err) { reject(data); } break; - case "buffer": resolve(new Buffer.from(data)); break; - default: reject("lol no D:"); break; - } - })); - -export default (a, options = {}, link = url.parse(a), body = "") => new Promise((resolve, reject) => { - options = {...{ hostname: link.hostname, path: link.path, method: "GET" }, ...options}; - if(options.method === "POST") { - body = querystring.stringify(options.body); - delete options.body; - options.headers = {...options.headers, ...{ - "Content-Type": "application/x-www-form-urlencoded", - "Content-Length": Buffer.byteLength(body) - }}; - } - (link.protocol === "https:"?https:http).request(options, res => resolve({ - body: res, - headers: res.headers, - text: () => readdata(res, "text"), - json: () => readdata(res, "json"), - buffer: () => readdata(res, "buffer") - })).on("error", err => reject(err)).end(body); -}); diff --git a/src/inc/fetch/index.mjs b/src/inc/fetch/index.mjs deleted file mode 100644 index 0d89f2a..0000000 --- a/src/inc/fetch/index.mjs +++ /dev/null @@ -1,36 +0,0 @@ -import _fetch from "./fetch"; -import CookieJar from "./cookie-jar"; -import urlParser from "url"; - -export const cookieJar = new CookieJar(); - -export default async (url, options) => { - let cookies = ""; - urlParser - .parse(url) - .hostname - .split(".") - .map((_, i, a) => a.slice(i).join(".")) - .slice(0, -1) - .map(d => [...cookieJar.iterValidForRequest(d, url)]) - .reduce((a, b) => [...a, ...b]) - .filter((v, i, a) => a.slice(0, i).every(c => c.name !== v.name)) //unique - .forEach(c => cookies += c.serialize() + "; "); - if(cookies.length !== 0) { - if(!options) { - options = { - headers: {} - }; - } - else if(!options.headers) - options.headers = {}; - options.headers.cookie = cookies.slice(0, -2); - } - const result = await _fetch(url, options); - cookies = result.headers["set-cookie"]; - if(cookies) { - cookies.forEach(c => cookieJar.addCookie(c, url)); - console.log(cookieJar); - } - return result; -}; diff --git a/src/inc/trigger/coins.mjs b/src/inc/trigger/coins.mjs index 065bbc7..e109c41 100644 --- a/src/inc/trigger/coins.mjs +++ b/src/inc/trigger/coins.mjs @@ -1,4 +1,4 @@ -import fetch from "../fetch"; +import fetch from "flumm-fetch-cookies"; const api_url = ({ market, crypto, currency }) => `https://api.cryptowat.ch/markets/${market}/${crypto}${currency}/summary`; const currencies = { @@ -53,4 +53,4 @@ const cryptowat_summary = (crypto, market, currency) => { resolve(`Current: [b]${data.last}[/b] - High: [b]${data.high}[/b] - Low: [b]${data.low}[/b] - Change: [b]${data.change}[/b]% - Volume: [b]${data.volume}[/b]`); }).catch(err => reject("lol cryptowatch ist down")); }); -}; \ No newline at end of file +}; diff --git a/src/inc/trigger/debug.mjs b/src/inc/trigger/debug.mjs index 3cbb08f..a88ea68 100644 --- a/src/inc/trigger/debug.mjs +++ b/src/inc/trigger/debug.mjs @@ -1,6 +1,5 @@ import { admins, getLevel } from "../admin"; -import fetch from "../fetch"; -import { cookieJar } from "../fetch"; +import fetch from "flumm-fetch-cookies"; import { wrapper, clients } from "../wrapper"; import vm from "vm"; @@ -11,7 +10,6 @@ let context = vm.createContext({ bot: null, admins: null, fetch: fetch, - cookieJar: cookieJar, console: console, wrapper: { wrapper, clients diff --git a/src/inc/trigger/irpg.mjs b/src/inc/trigger/irpg.mjs index 771c565..7557a20 100644 --- a/src/inc/trigger/irpg.mjs +++ b/src/inc/trigger/irpg.mjs @@ -1,4 +1,4 @@ -import fetch from "../fetch"; +import fetch from "flumm-fetch-cookies"; export default bot => { bot._trigger.set("irpg", new bot.trigger({ diff --git a/src/inc/trigger/kernel.mjs b/src/inc/trigger/kernel.mjs index add40c1..7df10d9 100644 --- a/src/inc/trigger/kernel.mjs +++ b/src/inc/trigger/kernel.mjs @@ -1,4 +1,4 @@ -import fetch from "../fetch"; +import fetch from "flumm-fetch-cookies"; const feed = "https://www.kernel.org/releases.json"; @@ -15,4 +15,4 @@ export default bot => { }).catch(err => console.log(err)); } })); -} \ No newline at end of file +} diff --git a/src/inc/trigger/lastfm.mjs b/src/inc/trigger/lastfm.mjs index a556e43..9f40a7a 100644 --- a/src/inc/trigger/lastfm.mjs +++ b/src/inc/trigger/lastfm.mjs @@ -1,5 +1,5 @@ import { cfg } from "../../inc/cfg"; -import fetch from "../fetch"; +import fetch from "flumm-fetch-cookies"; export default bot => { bot._trigger.set("lastfm", new bot.trigger({ @@ -32,4 +32,4 @@ export default bot => { }).catch(err => console.log(err)); } })); -}; \ No newline at end of file +}; diff --git a/src/inc/trigger/lib/cleverbot.mjs b/src/inc/trigger/lib/cleverbot.mjs index 8b57183..a396e9a 100644 --- a/src/inc/trigger/lib/cleverbot.mjs +++ b/src/inc/trigger/lib/cleverbot.mjs @@ -1,4 +1,4 @@ -import fetch from "../../fetch"; +import fetch from "flumm-fetch-cookies"; import { cfg } from "../../../inc/cfg"; class cleverbot { diff --git a/src/inc/trigger/lib/sandbox.mjs b/src/inc/trigger/lib/sandbox.mjs index 9e52c3e..9d9921a 100644 --- a/src/inc/trigger/lib/sandbox.mjs +++ b/src/inc/trigger/lib/sandbox.mjs @@ -1,4 +1,4 @@ -import fetch from "../../fetch"; +import fetch from "flumm-fetch-cookies"; const maxoutput = 500; const hsimports = [ diff --git a/src/inc/trigger/pr0gag.mjs b/src/inc/trigger/pr0gag.mjs index 8b7df53..d01e204 100644 --- a/src/inc/trigger/pr0gag.mjs +++ b/src/inc/trigger/pr0gag.mjs @@ -1,4 +1,4 @@ -import fetch from "../fetch"; +import fetch from "flumm-fetch-cookies"; import url from "url"; const apis = { diff --git a/src/inc/trigger/quotes.mjs b/src/inc/trigger/quotes.mjs index e1334a4..36e43bd 100644 --- a/src/inc/trigger/quotes.mjs +++ b/src/inc/trigger/quotes.mjs @@ -1,5 +1,5 @@ import sql from "../sql"; -import fetch from "../fetch"; +import fetch from "flumm-fetch-cookies"; let _query_get = ` with ranked_quotes as ( diff --git a/src/inc/trigger/sandbox.mjs b/src/inc/trigger/sandbox.mjs index 2ab3358..38e7d6a 100644 --- a/src/inc/trigger/sandbox.mjs +++ b/src/inc/trigger/sandbox.mjs @@ -2,7 +2,7 @@ import sql from "../sql"; import { maxoutput, sandbox, bfgen } from "./lib/sandbox"; import vm from "vm"; -import fetch from "../fetch"; +import fetch from "flumm-fetch-cookies"; import stringify from "stringify-object"; let _contexts = new Map(); diff --git a/src/inc/trigger/soundcloud.mjs b/src/inc/trigger/soundcloud.mjs index a8ecfdb..9f4fad9 100644 --- a/src/inc/trigger/soundcloud.mjs +++ b/src/inc/trigger/soundcloud.mjs @@ -1,4 +1,4 @@ -import fetch from "../fetch"; +import fetch from "flumm-fetch-cookies"; import { cfg } from "../../inc/cfg"; export default bot => { @@ -18,4 +18,4 @@ export default bot => { }).catch(err => console.log(err)); } })); -}; \ No newline at end of file +}; diff --git a/src/inc/trigger/urban.mjs b/src/inc/trigger/urban.mjs index 63b1d2d..2b8dad1 100644 --- a/src/inc/trigger/urban.mjs +++ b/src/inc/trigger/urban.mjs @@ -1,4 +1,4 @@ -import fetch from "../fetch"; +import fetch from "flumm-fetch-cookies"; const url = "https://api.urbandictionary.com/v0/define" @@ -28,4 +28,4 @@ export default bot => { }).catch(err => console.log(err)); } })); -}; \ No newline at end of file +}; diff --git a/src/inc/trigger/useless_nxy.mjs b/src/inc/trigger/useless_nxy.mjs index 7576352..76d38e3 100644 --- a/src/inc/trigger/useless_nxy.mjs +++ b/src/inc/trigger/useless_nxy.mjs @@ -1,5 +1,5 @@ import sql from "../sql"; -import fetch from "../fetch"; +import fetch from "flumm-fetch-cookies"; const data = { yiff: [], @@ -256,4 +256,4 @@ export default bot => { e.replyAction(`schläfert [b]${e.args[0] || e.user.nick}[/b] mit einer Spritze Ketamin ein.`); } })); -}; \ No newline at end of file +}; diff --git a/src/inc/trigger/useless_uwe.mjs b/src/inc/trigger/useless_uwe.mjs index 039d724..83acf14 100644 --- a/src/inc/trigger/useless_uwe.mjs +++ b/src/inc/trigger/useless_uwe.mjs @@ -1,5 +1,5 @@ import sql from "../sql"; -import fetch from "../fetch"; +import fetch from "flumm-fetch-cookies"; const data = { abschieben: [], diff --git a/src/inc/trigger/wttr.mjs b/src/inc/trigger/wttr.mjs index 5154403..7ec5e83 100644 --- a/src/inc/trigger/wttr.mjs +++ b/src/inc/trigger/wttr.mjs @@ -1,4 +1,4 @@ -import fetch from "../fetch"; +import fetch from "flumm-fetch-cookies"; import { cfg } from "../../inc/cfg"; import { conds } from "./lib/wttr";