Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: known peers integration #528

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
188 changes: 188 additions & 0 deletions container/shim/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions container/shim/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"express": "^4.18.2",
"express-async-handler": "^1.2.0",
"fast-glob": "^3.3.2",
"jsonwebtoken": "^8.5.1",
"logfmt": "^1.4.0",
"lru-cache": "^10.1.0",
"mime-types": "^2.1.35",
Expand Down
31 changes: 31 additions & 0 deletions container/shim/src/fetchers/lassie.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import fetch from "node-fetch";
import { LASSIE_ORIGIN, LASSIE_SP_ELIGIBLE_PORTION, hasNodeToken } from "../config.js";
import { submitLassieLogs } from "../modules/log_ingestor.js";
import { proxyAllResponseHeaders, proxyCARResponseHeaders, toUtf8 } from "../utils/http.js";
import { getKnownPeers } from "../utils/jwt.js";
import { debug as Debug } from "../utils/logging.js";

const debug = Debug.extend("lassie");
Expand Down Expand Up @@ -171,6 +172,7 @@ export async function respondFromLassie(req, res, { cidObj, format }) {

function createLassieURL(req, isRawFormat) {
const lassieUrl = new URL(LASSIE_ORIGIN + toUtf8(req.path));

for (const [key, val] of Object.entries(req.query)) {
if (key === "filename") {
continue;
Expand Down Expand Up @@ -223,6 +225,14 @@ function createLassieURL(req, isRawFormat) {
lassieUrl.searchParams.set("protocols", "bitswap,http");
}
}

const knownPeers = getKnownPeers(req);
const knownPeersLassieUrl = generatePeersLassieUrl(knownPeers);

if (knownPeersLassieUrl) {
lassieUrl.searchParams.set("providers", knownPeersLassieUrl);
}

return lassieUrl;
}

Expand Down Expand Up @@ -297,6 +307,27 @@ async function getRequestedBlockFromCar(streamIn, streamOut, cidObj, filename) {
}
}

function generatePeersLassieUrl(knownPeers) {
const peerUrls = [];
if (knownPeers) {
const knownPeerList = [];
Object.values(knownPeers).forEach((peerList) => {
knownPeerList.push(...peerList);
});
AmeanAsad marked this conversation as resolved.
Show resolved Hide resolved

knownPeerList.forEach((peer, idx) => {
const { peerID, multiaddr, protocol } = peer;
if (!peerID || !multiaddr || !protocol) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if multiaddr starts with http, peerID and protocol do not need to be set

return;
}
const peerUrl = multiaddr.startsWith("http") ? multiaddr : `${multiaddr}/p2p/${peerID}+${protocol}`;
peerUrls.push(peerUrl);
});
}
const urlString = peerUrls.join(",");
return urlString;
}

async function queueMetricsReport(newMetric) {
metrics.push(newMetric);

Expand Down
23 changes: 23 additions & 0 deletions container/shim/src/utils/jwt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import jwt from "jsonwebtoken";

export function findJWT(req) {
const jwtQuery = req.variables.arg_jwt;

let jwtHeader = "";
const authHeader = req.variables.http_authorization;
if (authHeader) {
jwtHeader = authHeader.replace("Bearer ", "");
}

return jwtQuery || jwtHeader;
}
guanzo marked this conversation as resolved.
Show resolved Hide resolved

export function getKnownPeers(req) {
const reqJwt = findJWT(req);
if (reqJwt) {
const jwtObject = jwt.decode(reqJwt);
const knownPeers = jwtObject.knownPeers;
return knownPeers;
}
return null;
}
Loading