From 98ec6f5c1caf585dcf03e9d88da8b2dee42a2506 Mon Sep 17 00:00:00 2001 From: Ivaylo Novakov Date: Wed, 5 Aug 2020 13:35:31 +0300 Subject: [PATCH 01/11] Support both bare (no leading `sia://`) and full skylinks. --- handshake-api/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/handshake-api/index.js b/handshake-api/index.js index 6c00d2f4..f92ccf05 100644 --- a/handshake-api/index.js +++ b/handshake-api/index.js @@ -19,7 +19,8 @@ const clientOptions = { }; const client = new NodeClient(clientOptions); -const startsWithSkylinkRegExp = /^[a-zA-Z0-9_-]{46}/; +// Match both `sia://HASH` and `HASH` links. +const startsWithSkylinkRegExp = /^(sia:\/\/){0,1}[a-zA-Z0-9_-]{46}/; const getDomainRecords = async (name) => { const response = await client.execute("getnameresource", [name]); From 4e37707a07120a126a8b057d43bc8fa54f2df7a9 Mon Sep 17 00:00:00 2001 From: Ivaylo Novakov Date: Wed, 5 Aug 2020 14:42:10 +0300 Subject: [PATCH 02/11] Add a hard 307 redirect. --- handshake-api/index.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/handshake-api/index.js b/handshake-api/index.js index f92ccf05..896514fb 100644 --- a/handshake-api/index.js +++ b/handshake-api/index.js @@ -69,6 +69,9 @@ server.use( proxy("nginx", { // eslint-disable-next-line no-unused-vars userResHeaderDecorator(headers, userReq, userRes, proxyReq, proxyRes) { + if (!userReq.path.endsWith("/")) { + userRes.redirect(307, `${userReq.path}/`); + } if (headers.location && headers.location.match(startsWithSkylinkRegExp)) { headers.location = headers.location.replace(startsWithSkylinkRegExp, `/hns/${userReq.params.name}`); } From 2b31f6c34a0d9f7702df2acd7ead270421af4c38 Mon Sep 17 00:00:00 2001 From: Ivaylo Novakov Date: Wed, 5 Aug 2020 14:47:15 +0300 Subject: [PATCH 03/11] Support only `sia://HASH` skylinks. --- handshake-api/index.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/handshake-api/index.js b/handshake-api/index.js index 896514fb..24f7347a 100644 --- a/handshake-api/index.js +++ b/handshake-api/index.js @@ -19,8 +19,7 @@ const clientOptions = { }; const client = new NodeClient(clientOptions); -// Match both `sia://HASH` and `HASH` links. -const startsWithSkylinkRegExp = /^(sia:\/\/){0,1}[a-zA-Z0-9_-]{46}/; +const startsWithSkylinkRegExp = /^sia:\/\/[a-zA-Z0-9_-]{46}/; const getDomainRecords = async (name) => { const response = await client.execute("getnameresource", [name]); From 90fa86b0f8e538557f204cb81cd8f0b5429bd874 Mon Sep 17 00:00:00 2001 From: Ivaylo Novakov Date: Wed, 5 Aug 2020 14:55:19 +0300 Subject: [PATCH 04/11] Bump GitHub Actions' Node version to 14.6 to match the version we're using for the services. --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 66f0cfcf..909abc86 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -11,7 +11,7 @@ jobs: - name: Use Node.js uses: actions/setup-node@v1 with: - node-version: 12.x + node-version: 14.6 - name: Install dependencies run: yarn From 9dd73cd2aac1aca3753700c80d85f02846dc45ab Mon Sep 17 00:00:00 2001 From: Ivaylo Novakov Date: Wed, 5 Aug 2020 17:02:42 +0300 Subject: [PATCH 05/11] Resolve a circular dependency between docker-compose services. --- docker-compose.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 71d0671f..e1748e85 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -55,7 +55,6 @@ services: - 80 depends_on: - docker-host - - health-check - handshake-api handshake: @@ -115,7 +114,6 @@ services: expose: - 3100 depends_on: - - docker-host + - caddy - handshake - handshake-api - - caddy From 651935ea3aa545e71dd508a58a7287d0564905b0 Mon Sep 17 00:00:00 2001 From: Ivaylo Novakov Date: Wed, 5 Aug 2020 17:20:29 +0300 Subject: [PATCH 06/11] Bring back the support for bare skylinks. --- handshake-api/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/handshake-api/index.js b/handshake-api/index.js index 24f7347a..896514fb 100644 --- a/handshake-api/index.js +++ b/handshake-api/index.js @@ -19,7 +19,8 @@ const clientOptions = { }; const client = new NodeClient(clientOptions); -const startsWithSkylinkRegExp = /^sia:\/\/[a-zA-Z0-9_-]{46}/; +// Match both `sia://HASH` and `HASH` links. +const startsWithSkylinkRegExp = /^(sia:\/\/){0,1}[a-zA-Z0-9_-]{46}/; const getDomainRecords = async (name) => { const response = await client.execute("getnameresource", [name]); From 6f408e49c93de7c4e9c6b68328997f23bee307ff Mon Sep 17 00:00:00 2001 From: Ivaylo Novakov Date: Wed, 5 Aug 2020 17:32:51 +0300 Subject: [PATCH 07/11] Get the last available skylink. --- handshake-api/index.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/handshake-api/index.js b/handshake-api/index.js index 896514fb..63cd75d7 100644 --- a/handshake-api/index.js +++ b/handshake-api/index.js @@ -32,7 +32,13 @@ const getDomainRecords = async (name) => { }; const findSkylinkRecord = (records) => { - return records?.find(({ txt }) => txt?.some((entry) => isValidSkylink(entry))); + // Find the last one, so people can update their domains in a non-destructive + // way by simply adding a new link. This will also allow keeping links to + // older versions for backwards compatibility. + return records + ?.slice() + .reverse() + .find(({ txt }) => txt?.some((entry) => isValidSkylink(entry))); }; const getSkylinkFromRecord = (record) => { From 87a3cb8aa74162131036de1ecdb7801388d1acc9 Mon Sep 17 00:00:00 2001 From: Ivaylo Novakov Date: Wed, 5 Aug 2020 17:40:16 +0300 Subject: [PATCH 08/11] Strip the `sia://` prefix. --- handshake-api/index.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/handshake-api/index.js b/handshake-api/index.js index 63cd75d7..f1cf23db 100644 --- a/handshake-api/index.js +++ b/handshake-api/index.js @@ -79,7 +79,10 @@ server.use( userRes.redirect(307, `${userReq.path}/`); } if (headers.location && headers.location.match(startsWithSkylinkRegExp)) { - headers.location = headers.location.replace(startsWithSkylinkRegExp, `/hns/${userReq.params.name}`); + headers.location = headers.location.replace( + startsWithSkylinkRegExp, + `/hns/${userReq.params.name.replace("sia://", "")}` + ); } return headers; From 71d2b3dc9140526665ce9be34a062653add43e34 Mon Sep 17 00:00:00 2001 From: Ivaylo Novakov Date: Wed, 5 Aug 2020 17:55:49 +0300 Subject: [PATCH 09/11] Fix the redirect. --- handshake-api/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/handshake-api/index.js b/handshake-api/index.js index f1cf23db..41dad687 100644 --- a/handshake-api/index.js +++ b/handshake-api/index.js @@ -75,8 +75,8 @@ server.use( proxy("nginx", { // eslint-disable-next-line no-unused-vars userResHeaderDecorator(headers, userReq, userRes, proxyReq, proxyRes) { - if (!userReq.path.endsWith("/")) { - userRes.redirect(307, `${userReq.path}/`); + if (!userReq.url.endsWith("/")) { + userRes.redirect(307, `${userReq.url}/`); } if (headers.location && headers.location.match(startsWithSkylinkRegExp)) { headers.location = headers.location.replace( From 8ee13bb96c7172879099aadf3c4cf586e1b82119 Mon Sep 17 00:00:00 2001 From: Ivaylo Novakov Date: Wed, 5 Aug 2020 18:18:36 +0300 Subject: [PATCH 10/11] Remove the problematic redirect. --- handshake-api/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/handshake-api/index.js b/handshake-api/index.js index 41dad687..452d50c7 100644 --- a/handshake-api/index.js +++ b/handshake-api/index.js @@ -75,9 +75,9 @@ server.use( proxy("nginx", { // eslint-disable-next-line no-unused-vars userResHeaderDecorator(headers, userReq, userRes, proxyReq, proxyRes) { - if (!userReq.url.endsWith("/")) { - userRes.redirect(307, `${userReq.url}/`); - } + // if (!userReq.url.endsWith('/')) { + // userRes.redirect(307, `${userReq.url}/`); + // } if (headers.location && headers.location.match(startsWithSkylinkRegExp)) { headers.location = headers.location.replace( startsWithSkylinkRegExp, From f3d918d7e299fa2e4e0608d649eaa34ae55f5f8f Mon Sep 17 00:00:00 2001 From: Ivaylo Novakov Date: Fri, 7 Aug 2020 13:03:32 +0300 Subject: [PATCH 11/11] Simplify regex. Remove dead code. --- .gitignore | 1 + handshake-api/index.js | 5 +---- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 43f25aa7..6a4f2eb0 100644 --- a/.gitignore +++ b/.gitignore @@ -78,3 +78,4 @@ docker/data # Cache files __pycache__ /.idea/ +/venv/ diff --git a/handshake-api/index.js b/handshake-api/index.js index 452d50c7..87b5a09a 100644 --- a/handshake-api/index.js +++ b/handshake-api/index.js @@ -20,7 +20,7 @@ const clientOptions = { const client = new NodeClient(clientOptions); // Match both `sia://HASH` and `HASH` links. -const startsWithSkylinkRegExp = /^(sia:\/\/){0,1}[a-zA-Z0-9_-]{46}/; +const startsWithSkylinkRegExp = /^(sia:\/\/)?[a-zA-Z0-9_-]{46}/; const getDomainRecords = async (name) => { const response = await client.execute("getnameresource", [name]); @@ -75,9 +75,6 @@ server.use( proxy("nginx", { // eslint-disable-next-line no-unused-vars userResHeaderDecorator(headers, userReq, userRes, proxyReq, proxyRes) { - // if (!userReq.url.endsWith('/')) { - // userRes.redirect(307, `${userReq.url}/`); - // } if (headers.location && headers.location.match(startsWithSkylinkRegExp)) { headers.location = headers.location.replace( startsWithSkylinkRegExp,