plugin/vhosting: support masks for domain

Massimo Melina committed Jan 8, 2023 at 01:03 UTC b2f2b27a190c2c8399c433b5bac75d8db65e707a
1 file changed +23 -20
plugins/vhosting/plugin.js
+23 -20
@@ -1,5 +1,5 @@
1 exports.description = "If you want to have different home folders, based on domain"
2 -exports.version = 2 // added config.mandatory
2 +exports.version = 3 // support masks for host
3 exports.apiRequired = 2 // 2 is for the config 'array'
4
5 exports.config = {
@@ -7,7 +7,7 @@ exports.config = {
7 label: '',
8 type: 'array',
9 fields: {
10 - host: { label: "Domain" },
10 + host: { label: "Domain", helperText: "Masks supported: domain.*|other.*" },
11 root: { helperText: "Root path in VFS" },
12 }
13 },
@@ -17,24 +17,27 @@ exports.config = {
17 }
18 }
19
20 -exports.init = api => ({
21 - middleware(ctx) {
22 - let toModify = ctx
23 - if (ctx.path.startsWith(api.const.SPECIAL_URI)) { // special uris should be excluded...
24 - toModify = ctx.params
25 - if (toModify.path === undefined) // ...unless they carry a path in the query. In that case we'll work that.
26 - return
27 - }
28 - const hosts = api.getConfig('hosts')
29 - if (!hosts?.length) return
30 - for (const row of hosts)
31 - if (ctx.host === row.host) {
32 - toModify.path = row.root + toModify.path
33 - return
20 +exports.init = api => {
21 + const { isMatch } = api.require('micromatch')
22 + return {
23 + middleware(ctx) {
24 + let toModify = ctx
25 + if (ctx.path.startsWith(api.const.SPECIAL_URI)) { // special uris should be excluded...
26 + toModify = ctx.params
27 + if (toModify.path === undefined) // ...unless they carry a path in the query. In that case we'll work that.
28 + return
29 + }
30 + const hosts = api.getConfig('hosts')
31 + if (!hosts?.length) return
32 + for (const row of hosts)
33 + if (isMatch(ctx.host, row.host)) {
34 + toModify.path = row.root + toModify.path
35 + return
36 + }
37 + if (api.getConfig('mandatory')) {
38 + ctx.socket.destroy()
39 + return true
40 }
35 - if (api.getConfig('mandatory')) {
36 - ctx.socket.destroy()
37 - return true
41 }
42 }
40 -})
43 +}