mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
detailed tests on received data plus testmode for socket handshake on
sending data
This commit is contained in:
parent
c1c4862880
commit
3fa4aa9f36
@ -1,8 +1,6 @@
|
|||||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||||
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
||||||
|
|
||||||
import DHT from '@hyperswarm/dht'
|
import DHT from '@hyperswarm/dht'
|
||||||
// import { Connection } from '@dbTools/typeorm'
|
|
||||||
import { backendLogger as logger } from '@/server/logger'
|
import { backendLogger as logger } from '@/server/logger'
|
||||||
import CONFIG from '@/config'
|
import CONFIG from '@/config'
|
||||||
import { Community as DbCommunity } from '@entity/Community'
|
import { Community as DbCommunity } from '@entity/Community'
|
||||||
@ -25,30 +23,65 @@ type CommunityApi = {
|
|||||||
api: string
|
api: string
|
||||||
url: string
|
url: string
|
||||||
}
|
}
|
||||||
type CommunityApiList = {
|
|
||||||
apiVersions: CommunityApi[]
|
|
||||||
}
|
|
||||||
|
|
||||||
export const startDHT = async (
|
export const startDHT = async (topic: string): Promise<void> => {
|
||||||
// connection: Connection,
|
|
||||||
topic: string,
|
|
||||||
): Promise<void> => {
|
|
||||||
try {
|
try {
|
||||||
|
let testModeCtrl = 0
|
||||||
|
const testModeData = [
|
||||||
|
`hello here is a new community and i don't know how to communicate with you`,
|
||||||
|
[`string1`, `api`, `url3`],
|
||||||
|
[
|
||||||
|
[`api`, `url`, `wrong`],
|
||||||
|
[`wrong`, `api`, `url`],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
{ wrong: 'wrong property name test', api: 'api1', url: 'url1' },
|
||||||
|
{ api: 'api2', url: 'url2', wrong: 'wrong property name test' },
|
||||||
|
],
|
||||||
|
[
|
||||||
|
{ test1: 'api proterty name test', url: 'any url definition as string' },
|
||||||
|
{ api: 'some api', test2: 'url property name test' },
|
||||||
|
],
|
||||||
|
[
|
||||||
|
{ api: 1, url: 'api number type test' },
|
||||||
|
{ api: 'urltyptest', url: 2 },
|
||||||
|
],
|
||||||
|
[
|
||||||
|
{
|
||||||
|
api: ApiVersionType.V1_0,
|
||||||
|
url: CONFIG.FEDERATION_COMMUNITY_URL
|
||||||
|
? (CONFIG.FEDERATION_COMMUNITY_URL.endsWith('/')
|
||||||
|
? CONFIG.FEDERATION_COMMUNITY_URL
|
||||||
|
: CONFIG.FEDERATION_COMMUNITY_URL + '/') + ApiVersionType.V1_0
|
||||||
|
: 'not configured',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
api: ApiVersionType.V2_0,
|
||||||
|
url: CONFIG.FEDERATION_COMMUNITY_URL
|
||||||
|
? (CONFIG.FEDERATION_COMMUNITY_URL.endsWith('/')
|
||||||
|
? CONFIG.FEDERATION_COMMUNITY_URL
|
||||||
|
: CONFIG.FEDERATION_COMMUNITY_URL + '/') + ApiVersionType.V2_0
|
||||||
|
: 'not configured',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
]
|
||||||
const TOPIC = DHT.hash(Buffer.from(topic))
|
const TOPIC = DHT.hash(Buffer.from(topic))
|
||||||
const keyPair = DHT.keyPair(getSeed())
|
const keyPair = DHT.keyPair(getSeed())
|
||||||
logger.info(`keyPairDHT: publicKey=${keyPair.publicKey.toString('hex')}`)
|
logger.info(`keyPairDHT: publicKey=${keyPair.publicKey.toString('hex')}`)
|
||||||
logger.debug(`keyPairDHT: secretKey=${keyPair.secretKey.toString('hex')}`)
|
logger.debug(`keyPairDHT: secretKey=${keyPair.secretKey.toString('hex')}`)
|
||||||
|
|
||||||
const apiList: CommunityApiList = {
|
const ownApiVersions = Object.values(ApiVersionType).map(function (apiEnum) {
|
||||||
apiVersions: Object.values(ApiVersionType).map(function (apiEnum) {
|
|
||||||
const comApi: CommunityApi = {
|
const comApi: CommunityApi = {
|
||||||
api: apiEnum,
|
api: apiEnum,
|
||||||
url: CONFIG.FEDERATION_COMMUNITY_URL || 'not configured',
|
url: CONFIG.FEDERATION_COMMUNITY_URL
|
||||||
|
? (CONFIG.FEDERATION_COMMUNITY_URL.endsWith('/')
|
||||||
|
? CONFIG.FEDERATION_COMMUNITY_URL
|
||||||
|
: CONFIG.FEDERATION_COMMUNITY_URL + '/') + apiEnum
|
||||||
|
: 'not configured',
|
||||||
}
|
}
|
||||||
return comApi
|
return comApi
|
||||||
}),
|
})
|
||||||
}
|
logger.debug(`ApiList: ${JSON.stringify(ownApiVersions)}`)
|
||||||
logger.debug(`ApiList: ${JSON.stringify(apiList)}`)
|
|
||||||
|
|
||||||
const node = new DHT({ keyPair })
|
const node = new DHT({ keyPair })
|
||||||
|
|
||||||
@ -63,14 +96,28 @@ export const startDHT = async (
|
|||||||
socket.on('data', async (data: Buffer) => {
|
socket.on('data', async (data: Buffer) => {
|
||||||
try {
|
try {
|
||||||
logger.info(`data: ${data.toString('ascii')}`)
|
logger.info(`data: ${data.toString('ascii')}`)
|
||||||
const apiVersionList: CommunityApiList = JSON.parse(data.toString('ascii'))
|
const recApiVersions: CommunityApi[] = JSON.parse(data.toString('ascii'))
|
||||||
if (apiVersionList && apiVersionList.apiVersions) {
|
if (recApiVersions && Array.isArray(recApiVersions)) {
|
||||||
for (let i = 0; i < apiVersionList.apiVersions.length; i++) {
|
recApiVersions.forEach(async (recApiVersion) => {
|
||||||
const apiVersion = apiVersionList.apiVersions[i]
|
if (
|
||||||
|
Object.keys(recApiVersion).some((key) => {
|
||||||
|
return key !== 'api' && key !== 'url'
|
||||||
|
})
|
||||||
|
) {
|
||||||
|
logger.warn(
|
||||||
|
`received apiVersion-Definition with unexpected properties:${JSON.stringify(
|
||||||
|
Object.keys(recApiVersion),
|
||||||
|
)}`,
|
||||||
|
)
|
||||||
|
} else if (
|
||||||
|
recApiVersion.api &&
|
||||||
|
typeof recApiVersion.api === 'string' &&
|
||||||
|
recApiVersion.url &&
|
||||||
|
typeof recApiVersion.url === 'string'
|
||||||
|
) {
|
||||||
const variables = {
|
const variables = {
|
||||||
apiVersion: apiVersion.api,
|
apiVersion: recApiVersion.api,
|
||||||
endPoint: apiVersion.url,
|
endPoint: recApiVersion.url,
|
||||||
publicKey: socket.remotePublicKey.toString('hex'),
|
publicKey: socket.remotePublicKey.toString('hex'),
|
||||||
lastAnnouncedAt: new Date(),
|
lastAnnouncedAt: new Date(),
|
||||||
}
|
}
|
||||||
@ -85,8 +132,17 @@ export const startDHT = async (
|
|||||||
overwrite: ['end_point', 'last_announced_at'],
|
overwrite: ['end_point', 'last_announced_at'],
|
||||||
})
|
})
|
||||||
.execute()
|
.execute()
|
||||||
|
logger.info(`federation community upserted successfully...`)
|
||||||
|
} else {
|
||||||
|
logger.warn(
|
||||||
|
`received invalid apiVersion-Definition:${JSON.stringify(recApiVersion)}`,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
logger.info(`federation community apiVersions stored...`)
|
})
|
||||||
|
} else {
|
||||||
|
logger.warn(
|
||||||
|
`received wrong apiVersions-Definition JSON-String:${JSON.stringify(recApiVersions)}`,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.error(`Error on receiving data from socket: ${JSON.stringify(e)}`)
|
logger.error(`Error on receiving data from socket: ${JSON.stringify(e)}`)
|
||||||
@ -154,7 +210,19 @@ export const startDHT = async (
|
|||||||
|
|
||||||
socket.on('open', function () {
|
socket.on('open', function () {
|
||||||
// noiseSocket fully open with the other peer
|
// noiseSocket fully open with the other peer
|
||||||
socket.write(Buffer.from(JSON.stringify(apiList)))
|
if (CONFIG.FEDERATION_DHT_TEST_SOCKET === true) {
|
||||||
|
logger.info(
|
||||||
|
`test-mode for socket handshake is activated...Test:(${testModeCtrl + 1}/${
|
||||||
|
testModeData.length
|
||||||
|
})`,
|
||||||
|
)
|
||||||
|
socket.write(Buffer.from(JSON.stringify(testModeData[testModeCtrl++])))
|
||||||
|
if (testModeCtrl >= testModeData.length) {
|
||||||
|
testModeCtrl = 0
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
socket.write(Buffer.from(JSON.stringify(ownApiVersions)))
|
||||||
|
}
|
||||||
successfulRequests.push(remotePubKey)
|
successfulRequests.push(remotePubKey)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user