mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
p2p https and login fix, removed static domain from ejs files
This commit is contained in:
parent
ae3bd7c55a
commit
e858d7f23e
13 changed files with 210 additions and 79 deletions
|
@ -3,8 +3,8 @@ import {
|
|||
TestUsersSchema,
|
||||
isJsonValidAndLogError,
|
||||
PeersInfoSchema,
|
||||
PeerInfoSchema,
|
||||
ModulesSchema,
|
||||
SelfInfoSchema,
|
||||
} from '../types/typeSchemas'
|
||||
import logger from './logger'
|
||||
import utils from './utils'
|
||||
|
@ -230,7 +230,7 @@ export const files = {
|
|||
path: 'data/p2p/selfInfo.json',
|
||||
description: 'json of info of this servers peer functionality',
|
||||
defaultValue: JSON.stringify({}),
|
||||
schema: PeerInfoSchema,
|
||||
schema: SelfInfoSchema,
|
||||
},
|
||||
thirdPartyPeersFile: {
|
||||
path: 'data/p2p/thirdPartyPeers.json',
|
||||
|
|
102
src/utils/networkUtils.ts
Normal file
102
src/utils/networkUtils.ts
Normal file
|
@ -0,0 +1,102 @@
|
|||
import http, { request as httpRequest } from 'http'
|
||||
import https, { request as httpsRequest } from 'https'
|
||||
|
||||
export interface GetResult<T> {
|
||||
data?: T
|
||||
error?: Error
|
||||
options?: http.RequestOptions
|
||||
}
|
||||
|
||||
export function get<T = any>(
|
||||
options: http.RequestOptions,
|
||||
useHttp?: boolean
|
||||
): Promise<GetResult<T>> {
|
||||
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) {
|
||||
console.log(body)
|
||||
resolve({ error: e, options: options })
|
||||
}
|
||||
})
|
||||
})
|
||||
req.on('error', function (e) {
|
||||
resolve({ error: e, options: options })
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export interface PostResult<T> {
|
||||
data?: T
|
||||
error?: Error
|
||||
cookies?: string[]
|
||||
}
|
||||
|
||||
interface PostParams {
|
||||
hostname: string
|
||||
path: string
|
||||
port: number
|
||||
bodyObject: any
|
||||
http?: boolean
|
||||
}
|
||||
|
||||
// https://nodejs.org/api/http.html#httprequesturl-options-callback
|
||||
export function post<T = any>({
|
||||
hostname,
|
||||
path,
|
||||
port,
|
||||
bodyObject,
|
||||
http,
|
||||
}: PostParams): Promise<PostResult<T>> {
|
||||
const provider = http ? httpRequest : httpsRequest
|
||||
const body = JSON.stringify(bodyObject)
|
||||
|
||||
return new Promise((resolve) => {
|
||||
const req = provider(
|
||||
{
|
||||
hostname: hostname,
|
||||
port: port,
|
||||
path: path,
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Content-Length': Buffer.byteLength(body),
|
||||
},
|
||||
},
|
||||
(res) => {
|
||||
const bodyChunks: string[] = []
|
||||
res.setEncoding('utf8')
|
||||
res.on('data', (chunk) => {
|
||||
bodyChunks.push(chunk)
|
||||
})
|
||||
res.on('end', () => {
|
||||
const body = bodyChunks.join()
|
||||
try {
|
||||
resolve({
|
||||
data: JSON.parse(body),
|
||||
cookies: res.headers['set-cookie'],
|
||||
})
|
||||
} catch (e) {
|
||||
console.log(body)
|
||||
resolve({ error: e })
|
||||
}
|
||||
})
|
||||
}
|
||||
)
|
||||
|
||||
req.on('error', (e) => {
|
||||
resolve({ error: e })
|
||||
})
|
||||
|
||||
req.write(body)
|
||||
req.end()
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue