Replacing express-http-proxy by http-proxy + code refactoring.
This commit is contained in:
parent
2b7174eb96
commit
1bb202d9d3
1388
package-lock.json
generated
1388
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -33,10 +33,9 @@
|
|||||||
],
|
],
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"async": "^3.2.2",
|
"async": "^3.2.2",
|
||||||
"body-parser": "^1.19.0",
|
|
||||||
"decache": "^4.6.0",
|
"decache": "^4.6.0",
|
||||||
"express-http-proxy": "^1.6.3",
|
|
||||||
"got": "^11.8.2",
|
"got": "^11.8.2",
|
||||||
|
"http-proxy": "^1.18.1",
|
||||||
"log-rotate": "^0.2.8",
|
"log-rotate": "^0.2.8",
|
||||||
"validate-color": "^2.2.1"
|
"validate-color": "^2.2.1"
|
||||||
},
|
},
|
||||||
@ -46,8 +45,8 @@
|
|||||||
"@tsconfig/node12": "^1.0.9",
|
"@tsconfig/node12": "^1.0.9",
|
||||||
"@types/async": "^3.2.9",
|
"@types/async": "^3.2.9",
|
||||||
"@types/express": "^4.17.13",
|
"@types/express": "^4.17.13",
|
||||||
"@types/express-http-proxy": "^1.6.3",
|
|
||||||
"@types/got": "^9.6.12",
|
"@types/got": "^9.6.12",
|
||||||
|
"@types/http-proxy": "^1.17.9",
|
||||||
"@types/node": "^16.11.6",
|
"@types/node": "^16.11.6",
|
||||||
"@types/winston": "^2.4.4",
|
"@types/winston": "^2.4.4",
|
||||||
"@typescript-eslint/eslint-plugin": "^4.29.0",
|
"@typescript-eslint/eslint-plugin": "^4.29.0",
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import type { RegisterServerOptions } from '@peertube/peertube-types'
|
import type { RegisterServerOptions } from '@peertube/peertube-types'
|
||||||
import { getProsodyConfig, getProsodyFilePaths, writeProsodyConfig } from './config'
|
import { getProsodyConfig, getProsodyFilePaths, writeProsodyConfig } from './config'
|
||||||
import { startProsodyLogRotate, stopProsodyLogRotate } from './logrotate'
|
import { startProsodyLogRotate, stopProsodyLogRotate } from './logrotate'
|
||||||
import { changeHttpBindRoute } from '../routers/webchat'
|
import { disableProxyRoute, enableProxyRoute } from '../routers/webchat'
|
||||||
import * as fs from 'fs'
|
import * as fs from 'fs'
|
||||||
import * as child_process from 'child_process'
|
import * as child_process from 'child_process'
|
||||||
|
|
||||||
@ -188,7 +188,7 @@ async function ensureProsodyRunning (options: RegisterServerOptions): Promise<vo
|
|||||||
})
|
})
|
||||||
|
|
||||||
// Set the http-bind route.
|
// Set the http-bind route.
|
||||||
changeHttpBindRoute(options, {
|
enableProxyRoute(options, {
|
||||||
host: config.host,
|
host: config.host,
|
||||||
port: config.port
|
port: config.port
|
||||||
})
|
})
|
||||||
@ -240,8 +240,8 @@ async function ensureProsodyNotRunning (options: RegisterServerOptions): Promise
|
|||||||
const status = await prosodyCtl(options, 'stop')
|
const status = await prosodyCtl(options, 'stop')
|
||||||
logger.info(`ProsodyCtl command returned: ${status.message}`)
|
logger.info(`ProsodyCtl command returned: ${status.message}`)
|
||||||
|
|
||||||
logger.debug('Removing http-bind route')
|
logger.debug('Removing proxy route')
|
||||||
changeHttpBindRoute(options, null)
|
disableProxyRoute(options)
|
||||||
}
|
}
|
||||||
|
|
||||||
export {
|
export {
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
import type { RegisterServerOptions, MVideoThumbnail } from '@peertube/peertube-types'
|
import type { RegisterServerOptions, MVideoThumbnail } from '@peertube/peertube-types'
|
||||||
import type { Router, RequestHandler, Request, Response, NextFunction } from 'express'
|
import type { Router, Request, Response, NextFunction } from 'express'
|
||||||
import type { ProxyOptions } from 'express-http-proxy'
|
|
||||||
import type {
|
import type {
|
||||||
ProsodyListRoomsResult, ProsodyListRoomsResultRoom
|
ProsodyListRoomsResult, ProsodyListRoomsResultRoom
|
||||||
} from '../../../shared/lib/types'
|
} from '../../../shared/lib/types'
|
||||||
|
import { createProxyServer } from 'http-proxy'
|
||||||
import { getBaseRouterRoute, getBaseRouterCanonicalRoute, getBaseStaticRoute, isUserAdmin } from '../helpers'
|
import { getBaseRouterRoute, getBaseRouterCanonicalRoute, getBaseStaticRoute, isUserAdmin } from '../helpers'
|
||||||
import { asyncMiddleware } from '../middlewares/async'
|
import { asyncMiddleware } from '../middlewares/async'
|
||||||
import { getProsodyDomain } from '../prosody/config/domain'
|
import { getProsodyDomain } from '../prosody/config/domain'
|
||||||
@ -11,18 +11,17 @@ import { getAPIKey } from '../apikey'
|
|||||||
import { getChannelInfosById, getChannelNameById } from '../database/channel'
|
import { getChannelInfosById, getChannelNameById } from '../database/channel'
|
||||||
import { isAutoColorsAvailable, areAutoColorsValid, AutoColors } from '../../../shared/lib/autocolors'
|
import { isAutoColorsAvailable, areAutoColorsValid, AutoColors } from '../../../shared/lib/autocolors'
|
||||||
import * as path from 'path'
|
import * as path from 'path'
|
||||||
const bodyParser = require('body-parser')
|
|
||||||
const got = require('got')
|
const got = require('got')
|
||||||
|
|
||||||
const fs = require('fs').promises
|
const fs = require('fs').promises
|
||||||
const proxy = require('express-http-proxy')
|
// const proxy = require('express-http-proxy')
|
||||||
|
|
||||||
let httpBindRoute: RequestHandler
|
interface ProsodyProxyInfo {
|
||||||
interface ProsodyHttpBindInfo {
|
|
||||||
host: string
|
host: string
|
||||||
port: 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> {
|
async function initWebchatRouter (options: RegisterServerOptions): Promise<Router> {
|
||||||
const {
|
const {
|
||||||
@ -207,11 +206,20 @@ async function initWebchatRouter (options: RegisterServerOptions): Promise<Route
|
|||||||
}
|
}
|
||||||
))
|
))
|
||||||
|
|
||||||
changeHttpBindRoute(options, null)
|
disableProxyRoute(options)
|
||||||
router.all('/http-bind',
|
router.all('/http-bind',
|
||||||
bodyParser.raw({ type: 'text/xml' }),
|
|
||||||
(req: Request, res: Response, next: NextFunction) => {
|
(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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!currentProsodyHttpBindInfo) {
|
if (!currentProsodyProxyInfo) {
|
||||||
throw new Error('It seems that prosody is not binded... Cant list rooms.')
|
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)
|
peertubeHelpers.logger.debug('Calling list rooms API on url: ' + apiUrl)
|
||||||
const rooms = await got(apiUrl, {
|
const rooms = await got(apiUrl, {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
headers: {
|
headers: {
|
||||||
authorization: 'Bearer ' + await getAPIKey(options),
|
authorization: 'Bearer ' + await getAPIKey(options),
|
||||||
host: currentProsodyHttpBindInfo.host
|
host: currentProsodyProxyInfo.host
|
||||||
},
|
},
|
||||||
responseType: 'json',
|
responseType: 'json',
|
||||||
resolveBodyOnly: true
|
resolveBodyOnly: true
|
||||||
@ -271,40 +279,63 @@ async function initWebchatRouter (options: RegisterServerOptions): Promise<Route
|
|||||||
return router
|
return router
|
||||||
}
|
}
|
||||||
|
|
||||||
function changeHttpBindRoute (
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
|
function enableProxyRoute (
|
||||||
{ peertubeHelpers }: RegisterServerOptions,
|
{ peertubeHelpers }: RegisterServerOptions,
|
||||||
prosodyHttpBindInfo: ProsodyHttpBindInfo | null
|
prosodyProxyInfo: ProsodyProxyInfo
|
||||||
): void {
|
): void {
|
||||||
const logger = peertubeHelpers.logger
|
const logger = peertubeHelpers.logger
|
||||||
if (prosodyHttpBindInfo && !/^\d+$/.test(prosodyHttpBindInfo.port)) {
|
if (!/^\d+$/.test(prosodyProxyInfo.port)) {
|
||||||
logger.error(`Port '${prosodyHttpBindInfo.port}' is not valid. Replacing by null`)
|
logger.error(`Port '${prosodyProxyInfo.port}' is not valid. Aborting.`)
|
||||||
prosodyHttpBindInfo = null
|
return
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
currentProsodyProxyInfo = prosodyProxyInfo
|
||||||
|
currentHttpBindProxy = createProxyServer({
|
||||||
|
target: 'http://localhost:' + prosodyProxyInfo.port + '/http-bind',
|
||||||
|
ignorePath: true
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export {
|
export {
|
||||||
initWebchatRouter,
|
initWebchatRouter,
|
||||||
changeHttpBindRoute
|
disableProxyRoute,
|
||||||
|
enableProxyRoute
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user