From d76cee1b2b6e69b4f30a2ea8832ed22e39b4e200 Mon Sep 17 00:00:00 2001 From: Yarmo Mackenbach Date: Thu, 1 Dec 2022 09:21:51 +0100 Subject: [PATCH] Add network module, flaresolverr options --- src/defaults.js | 4 +++ src/fetcher/activitypub.js | 6 ++-- src/network.js | 58 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 src/network.js diff --git a/src/defaults.js b/src/defaults.js index 6d5ac5f..8c5f3d6 100644 --- a/src/defaults.js +++ b/src/defaults.js @@ -49,6 +49,10 @@ const opts = { hostname: null, policy: E.ProxyPolicy.NEVER }, + flaresolverr: { + url: '', + maxTimeout: 60000 + }, claims: { activitypub: { url: null, diff --git a/src/fetcher/activitypub.js b/src/fetcher/activitypub.js index c87a7c1..2327ab8 100644 --- a/src/fetcher/activitypub.js +++ b/src/fetcher/activitypub.js @@ -13,7 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -const axios = require('axios') +const network = require('../network') const validator = require('validator') const jsEnv = require('browser-or-node') @@ -80,10 +80,10 @@ module.exports.fn = async (data, opts) => { headers.signature = `keyId="${opts.claims.activitypub.url}#main-key",headers="(request-target) host date",signature="${signatureSig}",algorithm="rsa-sha256"` } - axios.get(data.url, + network.get(data.url, { headers - }) + }, opts) .then(res => { return res.data }) diff --git a/src/network.js b/src/network.js new file mode 100644 index 0000000..8266517 --- /dev/null +++ b/src/network.js @@ -0,0 +1,58 @@ +/* +Copyright 2022 Yarmo Mackenbach + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +const axios = require('axios') + +/** + * Network related functions + * @module keys + */ + +/** + * Make a GET HTTP request + * @function + * @param {string} url - URL for the Axios request + * @param {object} requestConfig - Config for Axios request + * @param {object} opts - doip options + * @returns {object} + */ +const get = (url, requestConfig, opts) => { + return new Promise((resolve, reject) => { + switch (opts.flaresolverr.url === '') { + case false: + axios.get(url, requestConfig) + .then(res => { + resolve(res) + }) + .catch(err => { + reject(err) + }) + break + + case true: + default: + axios.get(url, requestConfig) + .then(res => { + resolve(res) + }) + .catch(err => { + reject(err) + }) + break + } + }) +} + +exports.get = get