mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
p2p instant question sending fixes
This commit is contained in:
parent
977c118da8
commit
f047701106
9 changed files with 75 additions and 56 deletions
|
@ -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
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -325,13 +325,11 @@ async function authAndGetNewData({
|
|||
peer,
|
||||
selfInfo,
|
||||
lastSyncWithPeer,
|
||||
lastSync,
|
||||
}: {
|
||||
peers: PeerInfo[]
|
||||
peer: PeerInfo
|
||||
selfInfo: PeerInfo
|
||||
lastSyncWithPeer: number
|
||||
lastSync: number
|
||||
}): Promise<GetResult<SyncDataRes & { peer: PeerInfo }>> {
|
||||
let sessionCookie = peer.sessionCookie
|
||||
|
||||
|
@ -354,13 +352,13 @@ async function authAndGetNewData({
|
|||
return get<SyncDataRes>(
|
||||
{
|
||||
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(
|
||||
|
|
|
@ -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,
|
||||
})
|
||||
|
||||
|
|
|
@ -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')
|
||||
}
|
||||
|
|
|
@ -14,19 +14,37 @@ export function get<T = any>(
|
|||
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<T = any>(
|
|||
export interface PostResult<T> {
|
||||
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<T = any>({
|
|||
port,
|
||||
bodyObject,
|
||||
http,
|
||||
cookies,
|
||||
cookie,
|
||||
}: PostParams): Promise<PostResult<T>> {
|
||||
const provider = http ? httpRequest : httpsRequest
|
||||
const body = JSON.stringify(bodyObject)
|
||||
|
@ -70,9 +88,9 @@ export function post<T = any>({
|
|||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Content-Length': Buffer.byteLength(body),
|
||||
...(cookies
|
||||
...(cookie
|
||||
? {
|
||||
cookie: cookies,
|
||||
cookie: cookie,
|
||||
}
|
||||
: {}),
|
||||
},
|
||||
|
@ -88,7 +106,7 @@ export function post<T = any>({
|
|||
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<T = any>({
|
|||
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
|
||||
}
|
||||
|
|
|
@ -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<string | Error> {
|
||||
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<string | Error> {
|
|||
return data ? new Error(data.msg) : error
|
||||
}
|
||||
|
||||
const parsedCookies = parseCookies(cookies)
|
||||
const parsedCookies = parseCookie(cookie)
|
||||
return parsedCookies.sessionID
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -1 +1 @@
|
|||
Subproject commit 8e192d3b23736851423cd469b950a2e97139f357
|
||||
Subproject commit aad2822226c6fc039ee190581b0eaa55f2859dc9
|
Loading…
Add table
Add a link
Reference in a new issue