From f047701106464ef56f24738193e3103bd27c94eb Mon Sep 17 00:00:00 2001 From: mrfry Date: Wed, 26 Apr 2023 15:02:50 +0200 Subject: [PATCH] p2p instant question sending fixes --- README.md | 4 +- src/middlewares/auth.middleware.ts | 2 +- src/modules/api/submodules/p2p.ts | 40 +++++------ src/modules/api/submodules/qminingapi.ts | 3 +- src/sharedViews/login.ejs | 4 +- src/utils/networkUtils.ts | 67 ++++++++++++------- src/utils/p2putils.ts | 6 +- src/worker/handlers/handleQuestionsToPeers.ts | 3 +- submodules/moodle-test-userscript | 2 +- 9 files changed, 75 insertions(+), 56 deletions(-) diff --git a/README.md b/README.md index a75c41b..4b9825d 100755 --- a/README.md +++ b/README.md @@ -93,11 +93,11 @@ To setup P2P functionality you have to create a few files in `./data/p2p`: } ``` - * `peers.json` : an array, with objects same as above, and: + * `peers.json`: List of registered peers. An array, with objects same as above, and: ```json { "pw": "password to the host, so the server can log in there. Please use a dedicated password, that only the server uses!", - "publicKey": "public key of the server" + "publicKey": "public key of the server for encryption" } ``` Public key is optional, but needed to encrypt and add the users database in the response, so they diff --git a/src/middlewares/auth.middleware.ts b/src/middlewares/auth.middleware.ts index 3199195..a4c2eb6 100644 --- a/src/middlewares/auth.middleware.ts +++ b/src/middlewares/auth.middleware.ts @@ -56,7 +56,7 @@ function renderLogin(req: Request, res: Response) { if (req.headers['content-type'] === 'application/json') { res.json({ result: 'nouser', - msg: 'You are not logged in', + message: 'You are not logged in', }) return } else { diff --git a/src/modules/api/submodules/p2p.ts b/src/modules/api/submodules/p2p.ts index c1cfaf9..5a36939 100644 --- a/src/modules/api/submodules/p2p.ts +++ b/src/modules/api/submodules/p2p.ts @@ -325,13 +325,11 @@ async function authAndGetNewData({ peer, selfInfo, lastSyncWithPeer, - lastSync, }: { peers: PeerInfo[] peer: PeerInfo selfInfo: PeerInfo lastSyncWithPeer: number - lastSync: number }): Promise> { let sessionCookie = peer.sessionCookie @@ -354,13 +352,13 @@ async function authAndGetNewData({ return get( { headers: { - cookies: `sessionID=${sessionCookie}`, + cookie: `sessionID=${sessionCookie}`, }, host: peer.host, port: peer.port, path: `/api/getnewdatasince?host=${encodeURIComponent( peerToString(selfInfo) - )}${lastSync ? `&since=${lastSyncWithPeer}` : ''}`, + )}${lastSyncWithPeer ? `&since=${lastSyncWithPeer}` : ''}`, }, peer.http ) @@ -637,7 +635,6 @@ function setup(data: SubmoduleData): Submodule { peer: peer, selfInfo: selfInfo, lastSyncWithPeer: lastSyncWithPeer, - lastSync: lastSync, }) }) @@ -671,23 +668,28 @@ function setup(data: SubmoduleData): Submodule { } const recievedDataCounts: (number | string)[][] = [] - const resultDataWithoutEmptyDbs = resultDataWithoutErrors.filter( - (res) => { - const qdbCount = res.questionDbs.length - const { subjCount, questionCount } = countOfQdbs( - res.questionDbs - ) + const resultDataWithoutEmptyDbs: (SyncDataRes & { peer: PeerInfo })[] = + [] + resultDataWithoutErrors.forEach((res) => { + const qdbCount = res.questionDbs.length + const { subjCount, questionCount } = countOfQdbs(res.questionDbs) - recievedDataCounts.push([ - peerToString(res.peer), - qdbCount, - subjCount, - questionCount, - ]) + recievedDataCounts.push([ + peerToString(res.peer), + qdbCount, + subjCount, + questionCount, + ]) - return questionCount > 0 + if (questionCount > 0) { + resultDataWithoutEmptyDbs.push(res) + } else { + updatePeersFile(peers, { + ...res.peer, + lastSync: syncStart, + }) } - ) + }) logger.Log(`\tRecieved data from peers:`) logger.logTable( diff --git a/src/modules/api/submodules/qminingapi.ts b/src/modules/api/submodules/qminingapi.ts index 4b83f8f..9ecba81 100644 --- a/src/modules/api/submodules/qminingapi.ts +++ b/src/modules/api/submodules/qminingapi.ts @@ -600,8 +600,7 @@ function setup(data: SubmoduleData): Submodule { ) res.json({ - success: resultArray.length > 0, - newQuestions: resultArray, // FIXME: this is useless? + success: true, totalNewQuestions: totalNewQuestions, }) diff --git a/src/sharedViews/login.ejs b/src/sharedViews/login.ejs index 7bed923..c46019e 100644 --- a/src/sharedViews/login.ejs +++ b/src/sharedViews/login.ejs @@ -111,9 +111,9 @@ const textNode = document.getElementById('text') if (resp.result === 'success') { location.reload() - textNode.innerText = resp.msg + textNode.innerText = resp.message } else { - textNode.innerText = resp.msg + textNode.innerText = resp.message button.disabled = false button.classList.remove('disabledButton') } diff --git a/src/utils/networkUtils.ts b/src/utils/networkUtils.ts index 9fa2a82..1daf002 100644 --- a/src/utils/networkUtils.ts +++ b/src/utils/networkUtils.ts @@ -14,19 +14,37 @@ export function get( const provider = useHttp ? http : https return new Promise((resolve) => { - const req = provider.get(options, function (res) { - const bodyChunks: Uint8Array[] = [] - res.on('data', (chunk) => { - bodyChunks.push(chunk) - }).on('end', () => { - const body = Buffer.concat(bodyChunks).toString() - try { - resolve({ data: JSON.parse(body) }) - } catch (e) { - resolve({ error: e, options: options }) - } - }) - }) + const req = provider.get( + { + ...options, + headers: { + ...options?.headers, + 'Content-Type': 'application/json', + }, + }, + function (res) { + const bodyChunks: Uint8Array[] = [] + res.on('data', (chunk) => { + bodyChunks.push(chunk) + }).on('end', () => { + const body = Buffer.concat(bodyChunks).toString() + try { + if (res.statusCode === 200) { + resolve({ data: JSON.parse(body) }) + } else { + resolve({ + data: JSON.parse(body), + error: new Error( + `HTTP response code: ${res.statusCode}` + ), + }) + } + } catch (e) { + resolve({ error: e, options: options }) + } + }) + } + ) req.on('error', function (e) { resolve({ error: e, options: options }) }) @@ -36,7 +54,7 @@ export function get( export interface PostResult { data?: T error?: Error - cookies?: string[] + cookie?: string[] } interface PostParams { @@ -45,7 +63,7 @@ interface PostParams { port: number bodyObject: any http?: boolean - cookies?: string + cookie?: string } // https://nodejs.org/api/http.html#httprequesturl-options-callback @@ -55,7 +73,7 @@ export function post({ port, bodyObject, http, - cookies, + cookie, }: PostParams): Promise> { const provider = http ? httpRequest : httpsRequest const body = JSON.stringify(bodyObject) @@ -70,9 +88,9 @@ export function post({ headers: { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(body), - ...(cookies + ...(cookie ? { - cookie: cookies, + cookie: cookie, } : {}), }, @@ -88,7 +106,7 @@ export function post({ try { resolve({ data: JSON.parse(body), - cookies: res.headers['set-cookie'], + cookie: res.headers['set-cookie'], }) } catch (e) { resolve({ error: e }) @@ -101,16 +119,15 @@ export function post({ resolve({ error: e }) }) - req.write(body) - req.end() + req.end(body) }) } -export function parseCookies(responseCookies: string[]): { +export function parseCookie(responseCookie: string[]): { [key: string]: string } { - const cookiesArray = responseCookies.join('; ').split('; ') - const parsedCookies: { [key: string]: string } = cookiesArray.reduce( + const cookieArray = responseCookie.join('; ').split('; ') + const parsedCookie: { [key: string]: string } = cookieArray.reduce( (acc, cookieString) => { const [key, val] = cookieString.split('=') return { @@ -120,5 +137,5 @@ export function parseCookies(responseCookies: string[]): { }, {} ) - return parsedCookies + return parsedCookie } diff --git a/src/utils/p2putils.ts b/src/utils/p2putils.ts index 46216ab..2366d1a 100644 --- a/src/utils/p2putils.ts +++ b/src/utils/p2putils.ts @@ -1,6 +1,6 @@ import { PeerInfo } from '../types/basicTypes' import { paths } from './files' -import { parseCookies, post } from './networkUtils' +import { parseCookie, post } from './networkUtils' import utils from './utils' export function peerToString(peer: { @@ -33,7 +33,7 @@ export function updatePeersFile( } export async function loginToPeer(peer: PeerInfo): Promise { - const { data, error, cookies } = await post<{ + const { data, error, cookie } = await post<{ result: string msg: string }>({ @@ -48,6 +48,6 @@ export async function loginToPeer(peer: PeerInfo): Promise { return data ? new Error(data.msg) : error } - const parsedCookies = parseCookies(cookies) + const parsedCookies = parseCookie(cookie) return parsedCookies.sessionID } diff --git a/src/worker/handlers/handleQuestionsToPeers.ts b/src/worker/handlers/handleQuestionsToPeers.ts index 74e1678..c4e9242 100644 --- a/src/worker/handlers/handleQuestionsToPeers.ts +++ b/src/worker/handlers/handleQuestionsToPeers.ts @@ -85,7 +85,7 @@ export const handleQuestionsToPeers = async ( http: peer.http, path: '/api/isAdding', bodyObject: dataToSend, - cookies: `sessionID=${sessionCookie}`, + cookie: `sessionID=${sessionCookie}`, }) } @@ -115,6 +115,7 @@ export const handleQuestionsToPeers = async ( if (res.error || !res.data?.success) { results.errors.push(peer) + console.error('AAAAAAAAAAAAAAAAAAAAAAA') console.error(res.error || JSON.stringify(res.data)) } else if (res.data?.totalNewQuestions > 0) { results.hasNew.push(peer) diff --git a/submodules/moodle-test-userscript b/submodules/moodle-test-userscript index 8e192d3..aad2822 160000 --- a/submodules/moodle-test-userscript +++ b/submodules/moodle-test-userscript @@ -1 +1 @@ -Subproject commit 8e192d3b23736851423cd469b950a2e97139f357 +Subproject commit aad2822226c6fc039ee190581b0eaa55f2859dc9