install flumm-fetch-cookies with npm

This commit is contained in:
Flummi 2019-08-15 23:31:06 +02:00
parent 4aea18099f
commit 9ee9f87f58
21 changed files with 23 additions and 253 deletions

View File

@ -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",

View File

@ -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 {

View File

@ -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));
}
};

View File

@ -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;
}
};

View File

@ -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);
});

View File

@ -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;
};

View File

@ -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"));
});
};
};

View File

@ -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

View File

@ -1,4 +1,4 @@
import fetch from "../fetch";
import fetch from "flumm-fetch-cookies";
export default bot => {
bot._trigger.set("irpg", new bot.trigger({

View File

@ -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));
}
}));
}
}

View File

@ -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));
}
}));
};
};

View File

@ -1,4 +1,4 @@
import fetch from "../../fetch";
import fetch from "flumm-fetch-cookies";
import { cfg } from "../../../inc/cfg";
class cleverbot {

View File

@ -1,4 +1,4 @@
import fetch from "../../fetch";
import fetch from "flumm-fetch-cookies";
const maxoutput = 500;
const hsimports = [

View File

@ -1,4 +1,4 @@
import fetch from "../fetch";
import fetch from "flumm-fetch-cookies";
import url from "url";
const apis = {

View File

@ -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 (

View File

@ -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();

View File

@ -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));
}
}));
};
};

View File

@ -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));
}
}));
};
};

View File

@ -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.`);
}
}));
};
};

View File

@ -1,5 +1,5 @@
import sql from "../sql";
import fetch from "../fetch";
import fetch from "flumm-fetch-cookies";
const data = {
abschieben: [],

View File

@ -1,4 +1,4 @@
import fetch from "../fetch";
import fetch from "flumm-fetch-cookies";
import { cfg } from "../../inc/cfg";
import { conds } from "./lib/wttr";