-
Notifications
You must be signed in to change notification settings - Fork 48
feat: known peers integration #528
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
base: main
Are you sure you want to change the base?
Changes from 6 commits
c5577b7
fa4a194
9e68d74
1f6e34e
edcabe8
c21462f
3ff3b48
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"); | ||
|
@@ -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; | ||
|
@@ -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; | ||
} | ||
|
||
|
@@ -297,6 +307,31 @@ async function getRequestedBlockFromCar(streamIn, streamOut, cidObj, filename) { | |
} | ||
} | ||
|
||
function generatePeersLassieUrl(knownPeers) { | ||
const peerUrls = []; | ||
if (knownPeers) { | ||
const knownPeerList = []; | ||
Object.values(knownPeers).forEach((peerList) => { | ||
knownPeerList.push(...peerList); | ||
}); | ||
|
||
knownPeerList.forEach((peer, idx) => { | ||
const { peerID, multiaddr, protocol } = peer; | ||
|
||
if (multiaddr && multiaddr.startsWith("http")) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be checking The peer structure i'm working with is
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The final url with the current logic will result in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to @hannahhoward's description, we pass the raw multiaddr only if the actual multiaddr starts with |
||
peerUrls.push(multiaddr); | ||
return; | ||
} | ||
if (multiaddr && peerID && protocol) { | ||
const peerUrl = `${multiaddr}/p2p/${peerID}+${protocol}`; | ||
peerUrls.push(peerUrl); | ||
} | ||
}); | ||
} | ||
const urlString = peerUrls.join(","); | ||
return urlString; | ||
} | ||
|
||
async function queueMetricsReport(newMetric) { | ||
metrics.push(newMetric); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import jwt from "jsonwebtoken"; | ||
|
||
function findJWT(req) { | ||
const jwtQuery = req.query.jwt; | ||
|
||
let jwtHeader = ""; | ||
const authHeader = req.headers.authorization; | ||
if (authHeader && authHeader.startsWith("Bearer ")) { | ||
jwtHeader = authHeader.replace("Bearer ", ""); | ||
} | ||
|
||
return jwtQuery || jwtHeader; | ||
} | ||
|
||
export function getKnownPeers(req) { | ||
const reqJwt = findJWT(req); | ||
if (reqJwt) { | ||
const jwtObject = jwt.decode(reqJwt); | ||
const knownPeers = jwtObject.knownPeers; | ||
return knownPeers; | ||
} | ||
return null; | ||
} |
Uh oh!
There was an error while loading. Please reload this page.