This repository was archived by the owner on Oct 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzpanel-server.js
115 lines (97 loc) · 3.16 KB
/
zpanel-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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
require('dotenv').config()
const path = require('path')
const exec = require('child_process').exec
const hapi = require('@hapi/hapi')
const { Logger, Config } = require('ranvier')
let { url, port } = Config.get('zPanel')
if (!url) url = 'localhost'
if (!port) port = 4335
const cookieUsername = process.env.ZPANEL_COOKIE_UN || 'zpanel'
const cookiePassword = process.env.ZPANEL_COOKIE_PW || 'ranvier-zpanel-hapi-authentication-cookie'
const cookieTtlDays = process.env.ZPANEL_COOKIE_TTL_DAYS || 1
module.exports = {
listeners: {
startup: state => function (commander) {
// build the Mithril app
const cmd = exec(`cd ${__dirname}/.. && npm run build`)
Logger.log('Now building zPanel...')
cmd.stdout.on('data', function (data) {
if (data.includes('ERROR')) {
console.log(data)
Logger.error('zPanel failed to build')
} else if (data.includes('hidden modules')) {
Logger.log('Finished building zPanel')
}
})
// initialize the server
const init = async () => {
const server = hapi.server({
port,
host: url,
router: {
stripTrailingSlash: true
}
})
// mount the Mithril app
await server.register(require('@hapi/inert'))
server.route({
method: 'GET',
path: '/{param*}',
handler: {
directory: {
path: path.join(__dirname, '../dist')
}
},
options: {
auth: false
}
})
// handle 404
server.ext('onPreResponse', (request, h) => {
const response = request.response
if (response.isBoom &&
response.output.statusCode === 404) {
return h.redirect('/').code(301)
}
return h.continue
})
// require authentication
await server.register(require('@hapi/cookie'))
server.auth.strategy('session', 'cookie', {
cookie: {
name: cookieUsername,
password: cookiePassword,
isSecure: false,
path: '/',
ttl: 1000 * 60 * 60 * 24 * cookieTtlDays // 1 day * cookieTtlDays
},
redirectTo: '/api/unauthenticated',
validateFunc: async (request, session) => {
const loaderRegistry = state.EntityLoaderRegistry
const accountLoader = loaderRegistry.get('accounts')
const account = Object.keys(await accountLoader.fetchAll()).find(account => account === session.id)
if (!account) {
return { valid: false }
}
return { valid: true, credentials: session.id }
}
})
server.auth.default('session')
// initialize the API
const api = require('./../api')(state, Config, Logger)
// mount the API
await server.register(api, {
routes: {
prefix: '/api'
}
})
await server.start()
Logger.log(`Serving zPanel at: http://${url}:${port}...`)
}
// start the server
init()
},
shutdown: state => function () {
},
},
}