jo hau rein die dscheise

This commit is contained in:
Flummi 2019-08-14 01:30:58 +02:00
parent 771b3af362
commit 4aea18099f
6 changed files with 72 additions and 23 deletions

View File

@ -1,15 +1,39 @@
import Cookie from "./cookie"; import Cookie from "./cookie";
export default class CookieJar { export default class CookieJar {
constructor() { constructor(flags, file, cookies) {
this.cookies = new Map(); this.cookies = new Map();
} }
addCookie(c, fromURL) { addCookie(c, fromURL) {
if(typeof c === "string") if(typeof c === "string")
c = new Cookie(c, fromURL); 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) { domains() {
this.cookies.forEach(callback); 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));
} }
}; };

View File

@ -18,18 +18,27 @@ const validatePath = (cookiePath, requestPath) => {
return (requestPath + "/").startsWith(cookiePath + "/"); 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 { export default class Cookie {
constructor(str, url) { constructor(str, url) {
if(typeof str !== "string") if(typeof str !== "string")
throw new TypeError("Input not a string"); throw new TypeError("Input not a string");
const splitted = str.split("; "); 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("\"")) if(this.value.startsWith("\"") && this.value.endsWith("\""))
this.value = this.value.slice(1, -1); this.value = this.value.slice(1, -1);
for(let i = 1; i < splitted.length; i++) { for(let i = 1; i < splitted.length; i++) {
let [k, v] = splitted[i].split("="); let [k, v] = splitN(splitted[i], "=", 1);
k = k.toLowerCase(); k = k.toLowerCase();
if(v) { if(v) {
if(k === "expires") { if(k === "expires") {
@ -40,7 +49,7 @@ export default class Cookie {
throw new TypeError("Invalid value for Expires \"" + v + "\"!"); throw new TypeError("Invalid value for Expires \"" + v + "\"!");
} }
else if(k === "max-age") { else if(k === "max-age") {
const seconds = parseInt(v); const seconds = ~~+v;
if(seconds.toString() !== v) if(seconds.toString() !== v)
throw new TypeError("Invalid value for Max-Age \"" + v + "\"!"); throw new TypeError("Invalid value for Max-Age \"" + v + "\"!");
this.expiry = new Date(); this.expiry = new Date();
@ -107,3 +116,4 @@ export default class Cookie {
return true; return true;
} }
}; };

View File

@ -1,15 +1,21 @@
import fetch from "./fetch"; import _fetch from "./fetch";
import CookieJar from "./cookie-jar"; 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 = ""; let cookies = "";
cookieJar.forEach(c => { urlParser
if(c.isValidForRequest(url)) .parse(url)
cookies += c.serialize() + "; "; .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(cookies.length !== 0) {
if(!options) { if(!options) {
options = { options = {
@ -20,9 +26,11 @@ export default async function cookieFetch(url, options) {
options.headers = {}; options.headers = {};
options.headers.cookie = cookies.slice(0, -2); options.headers.cookie = cookies.slice(0, -2);
} }
const result = await fetch(url, options); const result = await _fetch(url, options);
cookies = result.headers["set-cookie"]; cookies = result.headers["set-cookie"];
if(cookies) if(cookies) {
cookies.forEach(c => cookieJar.addCookie(c, url)); cookies.forEach(c => cookieJar.addCookie(c, url));
console.log(cookieJar);
}
return result; return result;
} };

View File

@ -2,11 +2,11 @@ import cleverbot from "./lib/cleverbot";
export default bot => { export default bot => {
bot._trigger.set("chatbot", new bot.trigger({ bot._trigger.set("chatbot", new bot.trigger({
call: /^(?![!./[])(.*uwe.*)/i, call: /^(?![!./[])(.*uw(e|i).*)/i,
set: "uwe", set: "uwe",
f: e => { f: e => {
const chat = e.message const chat = e.message
.replace(/uwe/gi, "") .replace(/uw(e|i)/gi, "")
.split("?") .split("?")
.join(""); .join("");
cleverbot.ask(chat) cleverbot.ask(chat)

View File

@ -1,5 +1,7 @@
import { admins, getLevel } from "../admin"; import { admins, getLevel } from "../admin";
import fetch from "../fetch"; import fetch from "../fetch";
import { cookieJar } from "../fetch";
import { wrapper, clients } from "../wrapper";
import vm from "vm"; import vm from "vm";
@ -8,7 +10,12 @@ let context = vm.createContext({
e: null, e: null,
bot: null, bot: null,
admins: null, admins: null,
fetch: fetch fetch: fetch,
cookieJar: cookieJar,
console: console,
wrapper: {
wrapper, clients
}
}); });
export default bot => { export default bot => {
bot._trigger.set("sandbox_debug", new bot.trigger({ bot._trigger.set("sandbox_debug", new bot.trigger({

View File

@ -25,7 +25,7 @@ insert into nxy_quotes
export default bot => { export default bot => {
bot._trigger.set("qrnd", new bot.trigger({ bot._trigger.set("qrnd", new bot.trigger({
call: /^(\.|\/)qrnd$/i, call: /^(\.|\/)q(rnd)?$/i,
set: "all", set: "all",
f: e => { f: e => {
fetch("https://nxy.totally.rip/api/quotes.php?c=nick,item") fetch("https://nxy.totally.rip/api/quotes.php?c=nick,item")