Commit f5329467 authored by Michał "rysiek" Woźniak's avatar Michał "rysiek" Woźniak 🔒

Gateway IPNS plugin implemented (naively); ref: #2

parent 0bfa6099
Pipeline #46065 canceled with stage
in 6 minutes and 57 seconds
......@@ -36,9 +36,14 @@ samizdat-is-deploy:
# this requires the ipfs container to be running and reachable from the gitlab runner container
# we also need to save the output for later use with Gun
- cd "/output/$CI_PROJECT_NAME/"
- $CI_PROJECT_DIR/node_modules/go-ipfs/bin/ipfs --api=/ip4/$( getent hosts ipfs | cut -d ' ' -f 1 )/tcp/5001 add --progress=false -r * | tee "$SAMIZDAT_TEMP"
# make sure we have a favicon
- grep favicon.ico "$SAMIZDAT_TEMP" | cut -d ' ' -f 1,2 | sed -r -e 's/$/ favicon.ico/' | tee -a "$SAMIZDAT_TEMP"
# make the favicon available in the main directory
- cp img/favicon.ico ./
# add everything to ipfs
- $CI_PROJECT_DIR/node_modules/go-ipfs/bin/ipfs --api=/ip4/$( getent hosts ipfs | cut -d ' ' -f 1 )/tcp/5001 add --progress=false -r ./ | sed -r -e "s/ $(basename $PWD)/ /" | tee "$SAMIZDAT_TEMP"
# publish stuff to IPNS
- $CI_PROJECT_DIR/node_modules/go-ipfs/bin/ipfs --api=/ip4/$( getent hosts ipfs | cut -d ' ' -f 1 )/tcp/5001 name publish "$( tail -n 1 $SAMIZDAT_TEMP | cut -f 2 -d ' ' )"
# remove the favicon
- rm ./favicon.ico
# get the IPFS output and use it to update Gun
- cd "$CI_PROJECT_DIR/samizdat-cli/"
- cat "$SAMIZDAT_TEMP" | sed -r -e 's/added //' -e "s%^(.+) (.+)$%$PROJECT_PATH/\2 /ipfs/\1%" | node . gun-update -u "$GUN_USER" -p "$GUN_PASSWORD" -m 1 "$GUN_DOMAIN" '-'
......
......@@ -60,5 +60,5 @@ self.SamizdatPlugins.push({
name: 'fetch',
description: 'Just a regular HTTP(S) fetch()',
version: 'COMMIT_UNKNOWN',
fetch: fetchContent,
fetch: fetchContent
})
/* ========================================================================= *\
|* === HTTP(S) fetch() from IPNS via known public gateways === *|
\* ========================================================================= */
/**
* this plugin does not implement any push method
*/
// the pubkey of the preconfigured IPNS node
const ipnsPubKey = 'QmYGVgGGfD5N4Xcc78CcMJ99dKcH6K6myhd4Uenv5yJwiJ'
// this will become useful later
//const gatewaysJSONUrl = "https://ipfs.github.io/public-gateway-checker/gateways.json";
// important:
// we cannot use gateways that use hash directly in the (sub)domain:
// https://github.com/node-fetch/node-fetch/issues/260
var gateways = [
'https://ninetailed.ninja/ipns/', // Russia
'https://10.via0.com/ipns/', // USA
'https://ipfs.sloppyta.co/ipns/', // UK
'https://gateway.temporal.cloud/ipns/', // Germany
'https://ipfs.best-practice.se/ipns/' // Sweden
]
/**
* getting content using regular HTTP(S) fetch()
*/
let fetchContentFromGatewayIPNS = (url) => {
// we're going to try a random gateway and building an URL of the form:
// https://<gateway_address>/<ipnsPubkey>/<rest_of_URL>
var ipnsUrl = gateways[Math.floor(Math.random() * gateways.length)] + ipnsPubKey + url.replace(/https?:\/\/[^/]+/, '')
console.log(`Samizdat: gateway IPNS:\n+-- fetching: ${ipnsUrl}`)
return fetch(ipnsUrl, {cache: "reload"})
.then((response) => {
// 4xx? 5xx? that's a paddlin'
if (response.status >= 400) {
// throw an Error to fall back to other plugins:
throw new Error('HTTP Error: ' + response.status + ' ' + response.statusText);
}
// all good, it seems
console.log("(COMMIT_UNKNOWN) Fetched:", response.url);
// we need to create a new Response object
// with all the headers added explicitly,
// since response.headers is immutable
var init = {
status: response.status,
statusText: response.statusText,
headers: {}
};
response.headers.forEach(function(val, header){
init.headers[header] = val;
});
// add the X-Samizdat-* headers to the mix
init.headers['X-Samizdat-Method'] = 'gateway-ipns'
init.headers['X-Samizdat-ETag'] = response.headers.get('ETag')
// return the new response, using the Blob from the original one
return response
.blob()
.then((blob) => {
return new Response(
blob,
init
)
})
})
}
// initialize the SamizdatPlugins array
if (!Array.isArray(self.SamizdatPlugins)) {
self.SamizdatPlugins = new Array()
}
// and add ourselves to it
// with some additional metadata
self.SamizdatPlugins.push({
name: 'gateway-ipns',
description: 'HTTP(S) fetch() from IPNS via known public gateways',
version: 'COMMIT_UNKNOWN',
fetch: fetchContentFromGatewayIPNS
})
......@@ -21,6 +21,7 @@ if (!Array.isArray(self.SamizdatPlugins)) {
self.importScripts(
"./plugins/fetch.js",
"./plugins/cache.js",
"./plugins/gateway-ipns.js",
"./plugins/gun-ipfs.js");
console.log('(COMMIT_UNKNOWN) SamizdatPlugins.length:', self.SamizdatPlugins.length)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment