From fb72d2e1c24cd4660fd6611ef723c5827c47294c Mon Sep 17 00:00:00 2001
From: Chocobozzz <me@florianbigard.com>
Date: Tue, 7 Sep 2021 15:16:26 +0200
Subject: Fix infohash with object storage

---
 shared/extra-utils/requests/requests.ts |  5 ++++-
 shared/extra-utils/server/index.ts      |  1 +
 shared/extra-utils/server/tracker.ts    | 27 +++++++++++++++++++++++++++
 3 files changed, 32 insertions(+), 1 deletion(-)
 create mode 100644 shared/extra-utils/server/tracker.ts

(limited to 'shared')

diff --git a/shared/extra-utils/requests/requests.ts b/shared/extra-utils/requests/requests.ts
index 60a08b13c..5bbf7f3bf 100644
--- a/shared/extra-utils/requests/requests.ts
+++ b/shared/extra-utils/requests/requests.ts
@@ -29,9 +29,12 @@ function makeRawRequest (url: string, expectedStatus?: HttpStatusCode, range?: s
 
 function makeGetRequest (options: CommonRequestParams & {
   query?: any
+  rawQuery?: string
 }) {
   const req = request(options.url).get(options.path)
-                                  .query(options.query)
+
+  if (options.query) req.query(options.query)
+  if (options.rawQuery) req.query(options.rawQuery)
 
   return buildRequest(req, { contentType: 'application/json', expectedStatus: HttpStatusCode.BAD_REQUEST_400, ...options })
 }
diff --git a/shared/extra-utils/server/index.ts b/shared/extra-utils/server/index.ts
index 92ff7a0f9..76a2099da 100644
--- a/shared/extra-utils/server/index.ts
+++ b/shared/extra-utils/server/index.ts
@@ -14,3 +14,4 @@ export * from './server'
 export * from './servers-command'
 export * from './servers'
 export * from './stats-command'
+export * from './tracker'
diff --git a/shared/extra-utils/server/tracker.ts b/shared/extra-utils/server/tracker.ts
new file mode 100644
index 000000000..f04e8f8a1
--- /dev/null
+++ b/shared/extra-utils/server/tracker.ts
@@ -0,0 +1,27 @@
+import { expect } from 'chai'
+import { sha1 } from '@server/helpers/core-utils'
+import { makeGetRequest } from '../requests'
+
+async function hlsInfohashExist (serverUrl: string, masterPlaylistUrl: string, fileNumber: number) {
+  const path = '/tracker/announce'
+
+  const infohash = sha1(`2${masterPlaylistUrl}+V${fileNumber}`)
+
+  // From bittorrent-tracker
+  const infohashBinary = escape(Buffer.from(infohash, 'hex').toString('binary')).replace(/[@*/+]/g, function (char) {
+    return '%' + char.charCodeAt(0).toString(16).toUpperCase()
+  })
+
+  const res = await makeGetRequest({
+    url: serverUrl,
+    path,
+    rawQuery: `peer_id=-WW0105-NkvYO/egUAr4&info_hash=${infohashBinary}&port=42100`,
+    expectedStatus: 200
+  })
+
+  expect(res.text).to.not.contain('failure')
+}
+
+export {
+  hlsInfohashExist
+}
-- 
cgit v1.2.3