From 4aea18099fb238a58b35cdf662c6a3a5ecc599de Mon Sep 17 00:00:00 2001 From: Flummi Date: Wed, 14 Aug 2019 01:30:58 +0200 Subject: [PATCH] jo hau rein die dscheise --- src/inc/fetch/cookie-jar.mjs | 32 ++++++++++++++++++++++++++++---- src/inc/fetch/cookie.mjs | 16 +++++++++++++--- src/inc/fetch/index.mjs | 30 +++++++++++++++++++----------- src/inc/trigger/chatbot.mjs | 4 ++-- src/inc/trigger/debug.mjs | 11 +++++++++-- src/inc/trigger/quotes.mjs | 2 +- 6 files changed, 72 insertions(+), 23 deletions(-) diff --git a/src/inc/fetch/cookie-jar.mjs b/src/inc/fetch/cookie-jar.mjs index 34bf275..82e1751 100644 --- a/src/inc/fetch/cookie-jar.mjs +++ b/src/inc/fetch/cookie-jar.mjs @@ -1,15 +1,39 @@ import Cookie from "./cookie"; export default class CookieJar { - constructor() { + constructor(flags, file, cookies) { this.cookies = new Map(); } addCookie(c, fromURL) { if(typeof c === "string") c = new Cookie(c, fromURL); - this.cookies.set(c.name, c); + 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); } - forEach(callback) { - this.cookies.forEach(callback); + 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 index 8f3b83d..743db09 100644 --- a/src/inc/fetch/cookie.mjs +++ b/src/inc/fetch/cookie.mjs @@ -18,18 +18,27 @@ const validatePath = (cookiePath, requestPath) => { 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] = splitted[0].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] = splitted[i].split("="); + let [k, v] = splitN(splitted[i], "=", 1); k = k.toLowerCase(); if(v) { if(k === "expires") { @@ -40,7 +49,7 @@ export default class Cookie { throw new TypeError("Invalid value for Expires \"" + v + "\"!"); } else if(k === "max-age") { - const seconds = parseInt(v); + const seconds = ~~+v; if(seconds.toString() !== v) throw new TypeError("Invalid value for Max-Age \"" + v + "\"!"); this.expiry = new Date(); @@ -107,3 +116,4 @@ export default class Cookie { return true; } }; + diff --git a/src/inc/fetch/index.mjs b/src/inc/fetch/index.mjs index 54b3397..0d89f2a 100644 --- a/src/inc/fetch/index.mjs +++ b/src/inc/fetch/index.mjs @@ -1,15 +1,21 @@ -import fetch from "./fetch"; +import _fetch from "./fetch"; import CookieJar from "./cookie-jar"; -import Cookie from "./cookie"; +import urlParser from "url"; -const cookieJar = new CookieJar(); +export const cookieJar = new CookieJar(); -export default async function cookieFetch(url, options) { +export default async (url, options) => { let cookies = ""; - cookieJar.forEach(c => { - if(c.isValidForRequest(url)) - cookies += c.serialize() + "; "; - }); + 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 = { @@ -20,9 +26,11 @@ export default async function cookieFetch(url, options) { options.headers = {}; options.headers.cookie = cookies.slice(0, -2); } - const result = await fetch(url, options); + const result = await _fetch(url, options); cookies = result.headers["set-cookie"]; - if(cookies) + if(cookies) { cookies.forEach(c => cookieJar.addCookie(c, url)); + console.log(cookieJar); + } return result; -} +}; diff --git a/src/inc/trigger/chatbot.mjs b/src/inc/trigger/chatbot.mjs index b3399b0..ad49a0c 100644 --- a/src/inc/trigger/chatbot.mjs +++ b/src/inc/trigger/chatbot.mjs @@ -2,11 +2,11 @@ import cleverbot from "./lib/cleverbot"; export default bot => { bot._trigger.set("chatbot", new bot.trigger({ - call: /^(?![!./[])(.*uwe.*)/i, + call: /^(?![!./[])(.*uw(e|i).*)/i, set: "uwe", f: e => { const chat = e.message - .replace(/uwe/gi, "") + .replace(/uw(e|i)/gi, "") .split("?") .join(""); cleverbot.ask(chat) diff --git a/src/inc/trigger/debug.mjs b/src/inc/trigger/debug.mjs index cf3083d..3cbb08f 100644 --- a/src/inc/trigger/debug.mjs +++ b/src/inc/trigger/debug.mjs @@ -1,5 +1,7 @@ import { admins, getLevel } from "../admin"; import fetch from "../fetch"; +import { cookieJar } from "../fetch"; +import { wrapper, clients } from "../wrapper"; import vm from "vm"; @@ -8,7 +10,12 @@ let context = vm.createContext({ e: null, bot: null, admins: null, - fetch: fetch + fetch: fetch, + cookieJar: cookieJar, + console: console, + wrapper: { + wrapper, clients + } }); export default bot => { bot._trigger.set("sandbox_debug", new bot.trigger({ @@ -36,4 +43,4 @@ export default bot => { } } })); -}; \ No newline at end of file +}; diff --git a/src/inc/trigger/quotes.mjs b/src/inc/trigger/quotes.mjs index 889247a..e1334a4 100644 --- a/src/inc/trigger/quotes.mjs +++ b/src/inc/trigger/quotes.mjs @@ -25,7 +25,7 @@ insert into nxy_quotes export default bot => { bot._trigger.set("qrnd", new bot.trigger({ - call: /^(\.|\/)qrnd$/i, + call: /^(\.|\/)q(rnd)?$/i, set: "all", f: e => { fetch("https://nxy.totally.rip/api/quotes.php?c=nick,item")