From ececfb5a6b18f37cebab74ec8c9940c293f08286 Mon Sep 17 00:00:00 2001 From: Mitra Ardron Date: Mon, 22 Oct 2018 17:46:25 -0700 Subject: [PATCH] Catch magnet links in canonical --- Transports.js | 5 +++-- test.js | 26 +++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/Transports.js b/Transports.js index 1d352d5..8a0bb85 100644 --- a/Transports.js +++ b/Transports.js @@ -699,7 +699,7 @@ class Transports { const patts = [ // No overlap between patts & arcpatts, so order unimportant /^dweb:[/]+(\w+)[/]+(.*)/i, // dweb://(proto)/(internal) /^\w+:[/]+(\w+)[/](.*)/i, // proto1://proto2//(internal) - maybe only match if proto1=proto2 (must be before proto:/internal) - /^(\w+):[/]+(.*)/i, // (proto)://(internal) # must be after proto1://proto2 + /^(\w+):[/]*(.*)/i, // (proto)://(internal) # must be after proto1://proto2 /^[/]*(\w+)[/](.*)/i, // /(proto)//(internal) - maybe only match if proto1=proto2 /^[/]*dweb[/]*(\w+)[/](.*)/i, // /dweb/(proto)//(internal) ] @@ -738,8 +738,9 @@ class Transports { return o.protocol + ":/" + o.internal; } static gatewayUrl(url) { + // Convert url to gateway url, if not canonicalizable then just pass the url along let o = Transports.canonicalName(url); - return [this.mirror, o.proto, o.internal].join('/'); + return o ? [this.mirror, o.proto, o.internal].join('/') : url; } } Transports._transports = []; // Array of transport instances connected diff --git a/test.js b/test.js index fbfe0a8..524e81a 100644 --- a/test.js +++ b/test.js @@ -66,6 +66,8 @@ function canonicalNameTests() { ["gun://ipfs/internal", "ipfs", "internal"], ["/ipfs/internal", "ipfs", "internal"], ["/dweb/ipfs/internal", "ipfs", "internal"], + [ "magnet:?xt=urn:btih:465HQWPEN374LABVHUBUPBUX4WZU6HDS&tr=http%3A%2F%2Fbt1.archive.org%3A6969%2Fannounce&tr=http%3A%2F%2Fbt2.archive.org%3A6969%2Fannounce&tr=wss%3A%2F%2Fdweb.archive.org%3A6969&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&tr=wss%3A%2F%2Ftracker.fastcast.nz&ws=https%3A%2F%2Fdweb.me%2Farc%2Farchive.org%2Fdownload%2F&xs=https%3A%2F%2Fdweb.me%2Farc%2Farchive.org%2Ftorrent%2Ffav-mitra/fav-mitra_members.json", + "magnet","?xt=urn:btih:465HQWPEN374LABVHUBUPBUX4WZU6HDS&tr=http%3A%2F%2Fbt1.archive.org%3A6969%2Fannounce&tr=http%3A%2F%2Fbt2.archive.org%3A6969%2Fannounce&tr=wss%3A%2F%2Fdweb.archive.org%3A6969&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&tr=wss%3A%2F%2Ftracker.fastcast.nz&ws=https%3A%2F%2Fdweb.me%2Farc%2Farchive.org%2Fdownload%2F&xs=https%3A%2F%2Fdweb.me%2Farc%2Farchive.org%2Ftorrent%2Ffav-mitra/fav-mitra_members.json"], ["http://dweb.dom.ain/internal", "arc", "dom.ain/internal"], ["http://localhost:123/archive.org/internal", "arc", "archive.org/internal"], ["https://dweb.arc.dom.ain/internal", "arc", "dom.ain/internal"], @@ -80,6 +82,28 @@ function canonicalNameTests() { } /* -p_test() +p_test({transport: ["GUN"]) .then(() => test_transports); */ + + +// Intentionally testing this with no connection +const sampleMagnetURL = "magnet:?xt=urn:btih:465HQWPEN374LABVHUBUPBUX4WZU6HDS&tr=http%3A%2F%2Fbt1.archive.org%3A6969%2Fannounce&tr=http%3A%2F%2Fbt2.archive.org%3A6969%2Fannounce&tr=wss%3A%2F%2Fdweb.archive.org%3A6969&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&tr=wss%3A%2F%2Ftracker.fastcast.nz&ws=https%3A%2F%2Fdweb.me%2Farc%2Farchive.org%2Fdownload%2F&xs=https%3A%2F%2Fdweb.me%2Farc%2Farchive.org%2Ftorrent%2Ffav-mitra/fav-mitra_members.json"; +const sampleMagnetURLMirrorresolve = "http://localhost:4244/magnet/?xt=urn:btih:465HQWPEN374LABVHUBUPBUX4WZU6HDS&tr=http%3A%2F%2Fbt1.archive.org%3A6969%2Fannounce&tr=http%3A%2F%2Fbt2.archive.org%3A6969%2Fannounce&tr=wss%3A%2F%2Fdweb.archive.org%3A6969&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&tr=wss%3A%2F%2Ftracker.fastcast.nz&ws=https%3A%2F%2Fdweb.me%2Farc%2Farchive.org%2Fdownload%2F&xs=https%3A%2F%2Fdweb.me%2Farc%2Farchive.org%2Ftorrent%2Ffav-mitra/fav-mitra_members.json" +const sampleGatewayURL = "http://dweb.me/arc/archive.org/download/foo/bar"; +const sampleHttpURL = "http://somewhere.com/zzz/xxx/download/foo/bar"; // XXX Looks like this is handled wrong +const sampleGatewayURLMirrorResolve = "http://localhost:4244/arc/archive.org/download/foo/bar"; +let tests = [ + {u: sampleMagnetURL, cn_proto:"magnet", gw: sampleMagnetURLMirrorresolve, resolve:sampleMagnetURL, resolveM: sampleMagnetURLMirrorresolve}, + {u: sampleGatewayURL, cn_proto: "arc", gw: sampleGatewayURLMirrorResolve, resolve: sampleGatewayURL, resolveM: sampleGatewayURLMirrorResolve}, + //FAILS ! {u: sampleHttpURL, cn_proto: "http", gw: sampleHttpURL, resolve: sampleHttpURL, resolveM: sampleHttpURL} + ] +tests.forEach(t => { + let url = t.u; + let res = DwebTransports.canonicalName(url).proto; console.assert( res=== t.cn_proto, "Canonical fail",url, t.cn_proto, "!==", res); + DwebTransports.mirror = undefined; + res = DwebTransports.p_resolveNames([url]).then(res => console.assert(res[0] === t.resolve, "Resolve", url, t.resolve, "!==", res )) + DwebTransports.mirror = "http://localhost:4244"; + res = DwebTransports.gatewayUrl(url); console.assert( res === t.gw, "GatewayURL:", url, t.gw,"!==", res); + res = DwebTransports.p_resolveNames([url]).then(res => console.assert(res[0] === t.resolveM, "Resolve with Mirror", url, t.resolveM, "!==", res )) +})