Replacing express-http-proxy by http-proxy + code refactoring.

This commit is contained in:
John Livingston 2022-08-23 20:34:03 +02:00
parent 2b7174eb96
commit 1bb202d9d3
No known key found for this signature in database
GPG Key ID: B17B5640CE66CDBC
4 changed files with 1147 additions and 367 deletions

1388
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -33,10 +33,9 @@
],
"dependencies": {
"async": "^3.2.2",
"body-parser": "^1.19.0",
"decache": "^4.6.0",
"express-http-proxy": "^1.6.3",
"got": "^11.8.2",
"http-proxy": "^1.18.1",
"log-rotate": "^0.2.8",
"validate-color": "^2.2.1"
},
@ -46,8 +45,8 @@
"@tsconfig/node12": "^1.0.9",
"@types/async": "^3.2.9",
"@types/express": "^4.17.13",
"@types/express-http-proxy": "^1.6.3",
"@types/got": "^9.6.12",
"@types/http-proxy": "^1.17.9",
"@types/node": "^16.11.6",
"@types/winston": "^2.4.4",
"@typescript-eslint/eslint-plugin": "^4.29.0",

View File

@ -1,7 +1,7 @@
import type { RegisterServerOptions } from '@peertube/peertube-types'
import { getProsodyConfig, getProsodyFilePaths, writeProsodyConfig } from './config'
import { startProsodyLogRotate, stopProsodyLogRotate } from './logrotate'
import { changeHttpBindRoute } from '../routers/webchat'
import { disableProxyRoute, enableProxyRoute } from '../routers/webchat'
import * as fs from 'fs'
import * as child_process from 'child_process'
@ -188,7 +188,7 @@ async function ensureProsodyRunning (options: RegisterServerOptions): Promise<vo
})
// Set the http-bind route.
changeHttpBindRoute(options, {
enableProxyRoute(options, {
host: config.host,
port: config.port
})
@ -240,8 +240,8 @@ async function ensureProsodyNotRunning (options: RegisterServerOptions): Promise
const status = await prosodyCtl(options, 'stop')
logger.info(`ProsodyCtl command returned: ${status.message}`)
logger.debug('Removing http-bind route')
changeHttpBindRoute(options, null)
logger.debug('Removing proxy route')
disableProxyRoute(options)
}
export {

View File

@ -1,9 +1,9 @@
import type { RegisterServerOptions, MVideoThumbnail } from '@peertube/peertube-types'
import type { Router, RequestHandler, Request, Response, NextFunction } from 'express'
import type { ProxyOptions } from 'express-http-proxy'
import type { Router, Request, Response, NextFunction } from 'express'
import type {
ProsodyListRoomsResult, ProsodyListRoomsResultRoom
} from '../../../shared/lib/types'
import { createProxyServer } from 'http-proxy'
import { getBaseRouterRoute, getBaseRouterCanonicalRoute, getBaseStaticRoute, isUserAdmin } from '../helpers'
import { asyncMiddleware } from '../middlewares/async'
import { getProsodyDomain } from '../prosody/config/domain'
@ -11,18 +11,17 @@ import { getAPIKey } from '../apikey'
import { getChannelInfosById, getChannelNameById } from '../database/channel'
import { isAutoColorsAvailable, areAutoColorsValid, AutoColors } from '../../../shared/lib/autocolors'
import * as path from 'path'
const bodyParser = require('body-parser')
const got = require('got')
const fs = require('fs').promises
const proxy = require('express-http-proxy')
// const proxy = require('express-http-proxy')
let httpBindRoute: RequestHandler
interface ProsodyHttpBindInfo {
interface ProsodyProxyInfo {
host: string
port: string
}
let currentProsodyHttpBindInfo: ProsodyHttpBindInfo | null = null
let currentProsodyProxyInfo: ProsodyProxyInfo | null = null
let currentHttpBindProxy: ReturnType<typeof createProxyServer> | null = null
async function initWebchatRouter (options: RegisterServerOptions): Promise<Router> {
const {
@ -207,11 +206,20 @@ async function initWebchatRouter (options: RegisterServerOptions): Promise<Route
}
))
changeHttpBindRoute(options, null)
disableProxyRoute(options)
router.all('/http-bind',
bodyParser.raw({ type: 'text/xml' }),
(req: Request, res: Response, next: NextFunction) => {
httpBindRoute(req, res, next)
try {
if (!currentHttpBindProxy) {
res.status(404)
res.send('Not found')
return
}
req.url = 'http-bind'
currentHttpBindProxy.web(req, res)
} catch (err) {
next(err)
}
}
)
@ -226,16 +234,16 @@ async function initWebchatRouter (options: RegisterServerOptions): Promise<Route
return
}
if (!currentProsodyHttpBindInfo) {
if (!currentProsodyProxyInfo) {
throw new Error('It seems that prosody is not binded... Cant list rooms.')
}
const apiUrl = `http://localhost:${currentProsodyHttpBindInfo.port}/peertubelivechat_list_rooms/list-rooms`
const apiUrl = `http://localhost:${currentProsodyProxyInfo.port}/peertubelivechat_list_rooms/list-rooms`
peertubeHelpers.logger.debug('Calling list rooms API on url: ' + apiUrl)
const rooms = await got(apiUrl, {
method: 'GET',
headers: {
authorization: 'Bearer ' + await getAPIKey(options),
host: currentProsodyHttpBindInfo.host
host: currentProsodyProxyInfo.host
},
responseType: 'json',
resolveBodyOnly: true
@ -271,40 +279,63 @@ async function initWebchatRouter (options: RegisterServerOptions): Promise<Route
return router
}
function changeHttpBindRoute (
{ peertubeHelpers }: RegisterServerOptions,
prosodyHttpBindInfo: ProsodyHttpBindInfo | null
): void {
const logger = peertubeHelpers.logger
if (prosodyHttpBindInfo && !/^\d+$/.test(prosodyHttpBindInfo.port)) {
logger.error(`Port '${prosodyHttpBindInfo.port}' is not valid. Replacing by null`)
prosodyHttpBindInfo = null
// function changeHttpBindRoute (
// { peertubeHelpers }: RegisterServerOptions,
// prosodyHttpBindInfo: ProsodyHttpBindInfo | null
// ): void {
// const logger = peertubeHelpers.logger
// if (prosodyHttpBindInfo && !/^\d+$/.test(prosodyHttpBindInfo.port)) {
// logger.error(`Port '${prosodyHttpBindInfo.port}' is not valid. Replacing by null`)
// prosodyHttpBindInfo = null
// }
// if (!prosodyHttpBindInfo) {
// logger.info('Changing http-bind port for null')
// currentProsodyHttpBindInfo = null
// httpBindRoute = (_req: Request, res: Response, _next: NextFunction) => {
// res.status(404)
// res.send('Not found')
// }
// } else {
// logger.info('Changing http-bind port for ' + prosodyHttpBindInfo.port + ', on host ' + prosodyHttpBindInfo.host)
// const options: ProxyOptions = {
// https: false,
// proxyReqPathResolver: async (_req: Request): Promise<string> => {
// return '/http-bind' // should not be able to access anything else
// },
// // preserveHostHdr: true,
// parseReqBody: true // Note that setting this to false overrides reqAsBuffer and reqBodyEncoding below.
// // FIXME: should we remove cookies?
// }
// currentProsodyHttpBindInfo = prosodyHttpBindInfo
// httpBindRoute = proxy('http://localhost:' + prosodyHttpBindInfo.port, options)
// }
// }
function disableProxyRoute (_options: RegisterServerOptions): void {
currentHttpBindProxy?.close()
currentHttpBindProxy = null
currentProsodyProxyInfo = null
}
if (!prosodyHttpBindInfo) {
logger.info('Changing http-bind port for null')
currentProsodyHttpBindInfo = null
httpBindRoute = (_req: Request, res: Response, _next: NextFunction) => {
res.status(404)
res.send('Not found')
}
} else {
logger.info('Changing http-bind port for ' + prosodyHttpBindInfo.port + ', on host ' + prosodyHttpBindInfo.host)
const options: ProxyOptions = {
https: false,
proxyReqPathResolver: async (_req: Request): Promise<string> => {
return '/http-bind' // should not be able to access anything else
},
// preserveHostHdr: true,
parseReqBody: true // Note that setting this to false overrides reqAsBuffer and reqBodyEncoding below.
// FIXME: should we remove cookies?
}
currentProsodyHttpBindInfo = prosodyHttpBindInfo
httpBindRoute = proxy('http://localhost:' + prosodyHttpBindInfo.port, options)
function enableProxyRoute (
{ peertubeHelpers }: RegisterServerOptions,
prosodyProxyInfo: ProsodyProxyInfo
): void {
const logger = peertubeHelpers.logger
if (!/^\d+$/.test(prosodyProxyInfo.port)) {
logger.error(`Port '${prosodyProxyInfo.port}' is not valid. Aborting.`)
return
}
currentProsodyProxyInfo = prosodyProxyInfo
currentHttpBindProxy = createProxyServer({
target: 'http://localhost:' + prosodyProxyInfo.port + '/http-bind',
ignorePath: true
})
}
export {
initWebchatRouter,
changeHttpBindRoute
disableProxyRoute,
enableProxyRoute
}