From 5e8aadb004d6735bfea3313defd9749daa68c1b3 Mon Sep 17 00:00:00 2001 From: Raghu Saxena Date: Fri, 26 May 2023 14:50:53 +0800 Subject: [PATCH 1/3] checkpoint w/ multipart form data --- __tests__/qbittorrent/auth.test.ts | 6 +-- build/src/qbittorrent/api.js | 10 ++--- build/src/qbittorrent/auth.js | 2 +- package-lock.json | 61 ++++++++++++++++++++++++------ package.json | 2 +- src/qbittorrent/api.ts | 10 ++--- src/qbittorrent/auth.ts | 2 +- 7 files changed, 64 insertions(+), 29 deletions(-) diff --git a/__tests__/qbittorrent/auth.test.ts b/__tests__/qbittorrent/auth.test.ts index 35f2a72..9cc5ec8 100644 --- a/__tests__/qbittorrent/auth.test.ts +++ b/__tests__/qbittorrent/auth.test.ts @@ -12,9 +12,9 @@ test('loginV2', async t => { password: 'adminadmin' } - const scope = nock(fakeSettings.url).get('/api/v2/auth/login').query({ - username: 'admin', - password: 'adminadmin', + const scope = nock(fakeSettings.url).post('/api/v2/auth/login', (b) => { + console.log(b); + return true; }).reply(200, {}, { 'set-cookie': 'SID=1234' }); diff --git a/build/src/qbittorrent/api.js b/build/src/qbittorrent/api.js index 8652d72..e65794c 100644 --- a/build/src/qbittorrent/api.js +++ b/build/src/qbittorrent/api.js @@ -126,11 +126,9 @@ var ApiEndpoints; ApiEndpoints["transferInfo"] = "/api/v2/transfer/info"; })(ApiEndpoints || (ApiEndpoints = {})); export const login = (qbittorrentSettings) => { - return axios.get(`${qbittorrentSettings.url}${ApiEndpoints.login}`, { - params: { - username: qbittorrentSettings.username, - password: qbittorrentSettings.password, - } - }); + const form = new FormData(); + form.append('username', qbittorrentSettings.username); + form.append('password', qbittorrentSettings.password); + return axios.post(`${qbittorrentSettings.url}${ApiEndpoints.login}`, form); }; //# sourceMappingURL=api.js.map \ No newline at end of file diff --git a/build/src/qbittorrent/auth.js b/build/src/qbittorrent/auth.js index 8fcd0a5..fb6281c 100644 --- a/build/src/qbittorrent/auth.js +++ b/build/src/qbittorrent/auth.js @@ -4,7 +4,7 @@ export const loginV2 = async (qbittorrentSettings) => { const logger = getLoggerV3(); const response = await apiLogin(qbittorrentSettings); // TODO: Differentiate between wrong credentials vs. qbit is not listening (wrong URL / port etc.) - if (Array.isArray(response.headers['set-cookie']) === false || response.headers['set-cookie'].length === 0) { + if (response.headers['set-cookie'] === undefined || Array.isArray(response.headers['set-cookie']) === false || response.headers['set-cookie'].length === 0) { throw new Error(`Failed to authenticate`); } return new QbittorrentApi(qbittorrentSettings.url, response.headers['set-cookie'][0]); diff --git a/package-lock.json b/package-lock.json index 8d301c3..24e251b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,17 +1,17 @@ { "name": "qbit-race", - "version": "2.0.0-alpha.4", + "version": "2.0.0-alpha.6", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "qbit-race", - "version": "2.0.0-alpha.4", + "version": "2.0.0-alpha.6", "license": "ISC", "dependencies": { "@ckcr4lyf/bencode-esm": "^0.0.2", "@ckcr4lyf/logger": "^0.0.5", - "axios": "^0.21.1", + "axios": "^1.4.0", "commander": "^9.4.0", "fastify": "^3.19.0", "form-data": "^3.0.1" @@ -423,11 +423,26 @@ } }, "node_modules/axios": { - "version": "0.21.4", - "resolved": "https://registry.npmjs.org/axios/-/axios-0.21.4.tgz", - "integrity": "sha512-ut5vewkiu8jjGBdqpM44XxjuCjq9LAKeHVmoVfHVzy8eHgxxq8SbAVQNovDA8mVi05kP0Ea/n/UzcSHcTJQfNg==", + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.4.0.tgz", + "integrity": "sha512-S4XCWMEmzvo64T9GfvQDOXgYRDJ/wsSZc7Jvdgx5u1sd0JwsuPLqb3SYmusag+edF6ziyMensPVqLTSc1PiSEA==", + "dependencies": { + "follow-redirects": "^1.15.0", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" + } + }, + "node_modules/axios/node_modules/form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", "dependencies": { - "follow-redirects": "^1.14.0" + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" } }, "node_modules/balanced-match": { @@ -2094,6 +2109,11 @@ "node": ">= 0.10" } }, + "node_modules/proxy-from-env": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" + }, "node_modules/punycode": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz", @@ -3126,11 +3146,25 @@ } }, "axios": { - "version": "0.21.4", - "resolved": "https://registry.npmjs.org/axios/-/axios-0.21.4.tgz", - "integrity": "sha512-ut5vewkiu8jjGBdqpM44XxjuCjq9LAKeHVmoVfHVzy8eHgxxq8SbAVQNovDA8mVi05kP0Ea/n/UzcSHcTJQfNg==", + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.4.0.tgz", + "integrity": "sha512-S4XCWMEmzvo64T9GfvQDOXgYRDJ/wsSZc7Jvdgx5u1sd0JwsuPLqb3SYmusag+edF6ziyMensPVqLTSc1PiSEA==", "requires": { - "follow-redirects": "^1.14.0" + "follow-redirects": "^1.15.0", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" + }, + "dependencies": { + "form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "requires": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + } + } } }, "balanced-match": { @@ -4335,6 +4369,11 @@ "ipaddr.js": "1.9.1" } }, + "proxy-from-env": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" + }, "punycode": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz", diff --git a/package.json b/package.json index b0160c6..23a2422 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "dependencies": { "@ckcr4lyf/bencode-esm": "^0.0.2", "@ckcr4lyf/logger": "^0.0.5", - "axios": "^0.21.1", + "axios": "^1.4.0", "commander": "^9.4.0", "fastify": "^3.19.0", "form-data": "^3.0.1" diff --git a/src/qbittorrent/api.ts b/src/qbittorrent/api.ts index 956752b..2a773af 100644 --- a/src/qbittorrent/api.ts +++ b/src/qbittorrent/api.ts @@ -158,12 +158,10 @@ enum ApiEndpoints { } export const login = (qbittorrentSettings: QBITTORRENT_SETTINGS): Promise => { - return axios.get(`${qbittorrentSettings.url}${ApiEndpoints.login}`, { - params: { - username: qbittorrentSettings.username, - password: qbittorrentSettings.password, - } - }); + const form = new FormData(); + form.append('username', qbittorrentSettings.username); + form.append('password', qbittorrentSettings.password); + return axios.post(`${qbittorrentSettings.url}${ApiEndpoints.login}`, form); } // We just need the hash for some of the API calls diff --git a/src/qbittorrent/auth.ts b/src/qbittorrent/auth.ts index b0b68c1..cae3ce7 100644 --- a/src/qbittorrent/auth.ts +++ b/src/qbittorrent/auth.ts @@ -10,7 +10,7 @@ export const loginV2 = async (qbittorrentSettings: QBITTORRENT_SETTINGS): Promis const response = await apiLogin(qbittorrentSettings); // TODO: Differentiate between wrong credentials vs. qbit is not listening (wrong URL / port etc.) - if (Array.isArray(response.headers['set-cookie']) === false || response.headers['set-cookie'].length === 0) { + if (response.headers['set-cookie'] === undefined || Array.isArray(response.headers['set-cookie']) === false || response.headers['set-cookie'].length === 0) { throw new Error(`Failed to authenticate`); } From a9c9313c731a4d5cba53115d062c006313b01662 Mon Sep 17 00:00:00 2001 From: Raghu Saxena Date: Fri, 26 May 2023 14:53:50 +0800 Subject: [PATCH 2/3] Fix form data stuff --- __tests__/qbittorrent/auth.test.ts | 6 +++--- build/src/qbittorrent/api.js | 9 ++++++++- src/qbittorrent/api.ts | 12 ++++++++---- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/__tests__/qbittorrent/auth.test.ts b/__tests__/qbittorrent/auth.test.ts index 9cc5ec8..5aa21d6 100644 --- a/__tests__/qbittorrent/auth.test.ts +++ b/__tests__/qbittorrent/auth.test.ts @@ -12,9 +12,9 @@ test('loginV2', async t => { password: 'adminadmin' } - const scope = nock(fakeSettings.url).post('/api/v2/auth/login', (b) => { - console.log(b); - return true; + const scope = nock(fakeSettings.url).post('/api/v2/auth/login', { + username: 'admin', + password: 'adminadmin' }).reply(200, {}, { 'set-cookie': 'SID=1234' }); diff --git a/build/src/qbittorrent/api.js b/build/src/qbittorrent/api.js index e65794c..82699d1 100644 --- a/build/src/qbittorrent/api.js +++ b/build/src/qbittorrent/api.js @@ -129,6 +129,13 @@ export const login = (qbittorrentSettings) => { const form = new FormData(); form.append('username', qbittorrentSettings.username); form.append('password', qbittorrentSettings.password); - return axios.post(`${qbittorrentSettings.url}${ApiEndpoints.login}`, form); + return axios.post(`${qbittorrentSettings.url}${ApiEndpoints.login}`, { + username: qbittorrentSettings.username, + password: qbittorrentSettings.password + }, { + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + } + }); }; //# sourceMappingURL=api.js.map \ No newline at end of file diff --git a/src/qbittorrent/api.ts b/src/qbittorrent/api.ts index 2a773af..4a21417 100644 --- a/src/qbittorrent/api.ts +++ b/src/qbittorrent/api.ts @@ -158,10 +158,14 @@ enum ApiEndpoints { } export const login = (qbittorrentSettings: QBITTORRENT_SETTINGS): Promise => { - const form = new FormData(); - form.append('username', qbittorrentSettings.username); - form.append('password', qbittorrentSettings.password); - return axios.post(`${qbittorrentSettings.url}${ApiEndpoints.login}`, form); + return axios.post(`${qbittorrentSettings.url}${ApiEndpoints.login}`, { + username: qbittorrentSettings.username, + password: qbittorrentSettings.password + }, { + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + } + }); } // We just need the hash for some of the API calls From 73c866525f587af365fead60214f82066db955e2 Mon Sep 17 00:00:00 2001 From: Raghu Saxena Date: Fri, 26 May 2023 15:00:19 +0800 Subject: [PATCH 3/3] Remove version command --- bin/index.mjs | 5 ----- build/src/qbittorrent/api.js | 3 --- package.json | 2 +- 3 files changed, 1 insertion(+), 9 deletions(-) diff --git a/bin/index.mjs b/bin/index.mjs index 3bc7ecf..6bb2622 100755 --- a/bin/index.mjs +++ b/bin/index.mjs @@ -10,7 +10,6 @@ import { tagErroredTorrents } from '../build/src/racing/tag.js' import { postRaceResumeV2 } from '../build/src/racing/completed.js' import { startMetricsServer } from '../build/src/server/appFactory.js'; import { addTorrentToRace } from '../build/src/racing/add.js'; -import data from '../package.json' assert { type: 'json' }; // This should take care of having a base config makeConfigIfNotExist(); @@ -70,8 +69,4 @@ program.command('metrics').description('Start a prometheus metrics server').acti startMetricsServer(config, api); }) -program.option('-v, --version', 'Display the version').action(() => { - console.log(`\n\nqbit-race version ${data.version}\n\n`); -}) - program.parse(); diff --git a/build/src/qbittorrent/api.js b/build/src/qbittorrent/api.js index 82699d1..3056b57 100644 --- a/build/src/qbittorrent/api.js +++ b/build/src/qbittorrent/api.js @@ -126,9 +126,6 @@ var ApiEndpoints; ApiEndpoints["transferInfo"] = "/api/v2/transfer/info"; })(ApiEndpoints || (ApiEndpoints = {})); export const login = (qbittorrentSettings) => { - const form = new FormData(); - form.append('username', qbittorrentSettings.username); - form.append('password', qbittorrentSettings.password); return axios.post(`${qbittorrentSettings.url}${ApiEndpoints.login}`, { username: qbittorrentSettings.username, password: qbittorrentSettings.password diff --git a/package.json b/package.json index 23a2422..3b36f48 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "qbit-race", - "version": "2.0.0-alpha.6", + "version": "2.0.0-alpha.8", "description": "Qbit utilities for racing", "main": "./bin/index.js", "type": "module",