-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
65 lines (53 loc) · 1.96 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env node
const argv = require('optimist')
.boolean('verbose')
.argv
const fs = require('fs')
const path = require('path')
const untildify = require('untildify')
const configFile = untildify(argv._[0] || '~/dlna-adapter.config.json')
if (!fs.existsSync(configFile)) {
console.info(`Config file ${configFile} is not found.`)
console.info('Please create one like this:')
console.info(fs.readFileSync(path.join(__dirname, 'dlna-adapter.config.json.example')).toString())
process.exit(1)
}
const config = JSON.parse(fs.readFileSync(configFile))
const express = require('express')
const bodyParser = require('body-parser')
config.deviceDescriptionPath = config.deviceDescriptionPath || '/DeviceDescription.xml'
config.dlnaHost = `${config.dlnaHostname}:${config.dlnaPort}`
config.externalHost = `${config.externalHostname}:${config.port}`
config.replaceRegExp = new RegExp(config.dlnaHost, 'g')
const proxy = require('express-http-proxy')
const app = express()
app.use(bodyParser.text({type: 'text/xml'}))
if (argv.verbose) {
app.use(function (req, res, next) {
console.info(req.method, req.originalUrl)
if (req.body && typeof req.body === 'string') {
console.info(req.body)
}
next()
})
}
function userResDecorator(proxyRes, proxyResData) {
const contentType = proxyRes.headers['content-type']
if (!contentType || !contentType.includes('text/xml')) {
return proxyResData
}
const xml = proxyResData.toString('utf8')
return xml.replace(config.replaceRegExp, config.externalHost)
}
app.use('/DeviceDescription.xml', proxy(config.dlnaHost, {
proxyReqPathResolver: () => config.deviceDescriptionPath,
userResDecorator,
}))
app.use('/', proxy(config.dlnaHost, {
userResDecorator,
}))
app.listen(config.port, config.listenHostname, () => {
console.info(`Use config file ${configFile}.`)
console.info(`Listening on ${config.listenHostname}:${config.port}.`)
console.info(`Proxying ${config.externalHost} to ${config.dlnaHost}.`)
})