From 25d355026b342c3a34475f37689949df44d52246 Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Thu, 4 Aug 2022 15:58:58 +0100 Subject: [PATCH] use host as authority and link type as root of path --- .../com/muwire/gui/mulinks/FileMuLink.groovy | 4 -- .../com/muwire/gui/mulinks/MuLink.groovy | 41 ++++++++++--------- 2 files changed, 21 insertions(+), 24 deletions(-) diff --git a/gui/src/main/groovy/com/muwire/gui/mulinks/FileMuLink.groovy b/gui/src/main/groovy/com/muwire/gui/mulinks/FileMuLink.groovy index cb446e28..81373030 100644 --- a/gui/src/main/groovy/com/muwire/gui/mulinks/FileMuLink.groovy +++ b/gui/src/main/groovy/com/muwire/gui/mulinks/FileMuLink.groovy @@ -33,10 +33,6 @@ class FileMuLink extends MuLink { throw new InvalidMuLinkException("invalid size $fileSize") if (pieceSizePow2 < FileHasher.MIN_PIECE_SIZE_POW2 || pieceSizePow2 > FileHasher.MAX_PIECE_SIZE_POW2) throw new InvalidMuLinkException("invalid piece size $pieceSizePow2") - - Path path = Path.of(name) - if (path.getNameCount() != 1) - throw new InvalidMuLinkException("name path count ${path.getNameCount()}") } FileMuLink(SharedFile sharedFile, Persona me, SigningPrivateKey spk) { diff --git a/gui/src/main/groovy/com/muwire/gui/mulinks/MuLink.groovy b/gui/src/main/groovy/com/muwire/gui/mulinks/MuLink.groovy index d7497b14..e0327d37 100644 --- a/gui/src/main/groovy/com/muwire/gui/mulinks/MuLink.groovy +++ b/gui/src/main/groovy/com/muwire/gui/mulinks/MuLink.groovy @@ -8,7 +8,8 @@ import net.i2p.data.Base64 import net.i2p.data.Signature import net.i2p.data.SigningPublicKey -import java.nio.charset.StandardCharsets +import java.nio.charset.StandardCharsets +import java.nio.file.Path abstract class MuLink { @@ -58,7 +59,6 @@ abstract class MuLink { def query = [:] query.name = URLEncoder.encode(name, StandardCharsets.UTF_8) query.sig = Base64.encode(sig) - query.type = linkType.name() addQueryElements(query) def kvs = [] @@ -68,12 +68,10 @@ abstract class MuLink { String queryStr = kvs.join("&") URI uri = new URI(SCHEME, - Base64.encode(infoHash.getRoot()), - host.toBase64(), - PORT, - "/", - queryStr, - null) + host.toBase64(), + "/${linkType.name()}/" + Base64.encode(infoHash.getRoot()), + queryStr, + null) uri.toASCIIString() } @@ -84,14 +82,21 @@ abstract class MuLink { URI uri = new URI(url) if (uri.getScheme() != SCHEME) throw new InvalidMuLinkException("Unsupported scheme ${uri.getScheme()}") - - if (uri.getUserInfo() == null) - throw new InvalidMuLinkException("no infohash") - InfoHash ih = new InfoHash(Base64.decode(uri.getUserInfo())) - - if (uri.getHost() == null) + + + if (uri.getAuthority() == null) throw new InvalidMuLinkException("no persona") - Persona p = new Persona(new ByteArrayInputStream(Base64.decode(uri.getHost()))) + Persona p = new Persona(new ByteArrayInputStream(Base64.decode(uri.getAuthority()))) + + if (uri.getPath() == null) + throw new InvalidMuLinkException("no path") + String [] pathElements = uri.getPath().split("/") + if (pathElements.length < 3) + throw new InvalidMuLinkException("path elements ${pathElements.length}") + + LinkType linkType = LinkType.valueOf(pathElements[1]) + + InfoHash ih = new InfoHash(Base64.decode(pathElements[2])) Map query = parseQuery(uri.getQuery()) @@ -105,10 +110,6 @@ abstract class MuLink { if (sigBytes.length != Constants.SIG_TYPE.getSigLen()) throw new InvalidMuLinkException("invalid sig key") - if (query.type == null) - throw new InvalidMuLinkException("type missing") - LinkType linkType = LinkType.valueOf(query.type) - if (linkType == LinkType.FILE) return new FileMuLink(p, ih, n, sigBytes, query) throw new InvalidMuLinkException("unknown type $linkType") @@ -132,7 +133,7 @@ abstract class MuLink { if (rv.containsKey(k)) throw new InvalidMuLinkException("duplicate key $k") - rv.k = v + rv[k] = v } rv }