-
Notifications
You must be signed in to change notification settings - Fork 3
/
testserver.ts
341 lines (331 loc) · 12.1 KB
/
testserver.ts
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/**
* This file requires the --experimental-specifier-resolution=node option.
*
* env NODE_OPTIONS='--experimental-specifier-resolution=node' npx tsx testserver.ts testroot
*/
import { hostname } from 'node:os';
import { dirname, resolve, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import type { Request } from 'express';
import express from 'express';
import createDebug from 'debug';
import type { AuthResponse, Plugin } from './packages/nephele/dist/index.js';
import server from './packages/nephele/dist/index.js';
import FileSystemAdapter from './packages/adapter-file-system/dist/index.js';
import VirtualAdapter from './packages/adapter-virtual/dist/index.js';
import S3Adapter from './packages/adapter-s3/dist/index.js';
import PamAuthenticator from './packages/authenticator-pam/dist/index.js';
import HtpasswdAuthenticator from './packages/authenticator-htpasswd/dist/index.js';
import CustomAuthenticator, {
User as CustomUser,
} from './packages/authenticator-custom/dist/index.js';
import InsecureAuthenticator from './packages/authenticator-none/dist/index.js';
import IndexPlugin from './packages/plugin-index/dist/index.js';
import ReadOnlyPlugin from './packages/plugin-read-only/dist/index.js';
import EncryptionPlugin from './packages/plugin-encryption/dist/index.js';
const __dirname = dirname(fileURLToPath(import.meta.url));
const debug = createDebug('nephele:testserver');
const app = express();
const host = hostname();
const port = 8080;
const root = process.argv.length > 2 ? resolve(process.argv[2]) : __dirname;
const readonly = !!process.env.READONLY;
const htpasswd = !!process.env.HTPASSWD;
const pam = !process.env.NOPAM;
const unauthorized = !!process.env.UNAUTHORIZED;
const virtual = !!process.env.VIRTUALFS;
const s3Endpoint = process.env.S3ENDPOINT;
const s3Region = process.env.S3REGION ?? 'us-east-1';
const s3AccessKey = process.env.S3ACCESSKEY;
const s3SecretKey = process.env.S3SECRETKEY;
const s3Bucket = process.env.S3BUCKET;
const envuser = process.env.USERNAME || process.env.USER;
const envpass = process.env.PASSWORD;
const userpassdefined = !!(envuser && envpass);
const encryption = process.env.ENCRYPTION;
const userDirs = !!process.env.USERDIRS;
const WEBROOT = process.env.WEBROOT || '/';
app.use(
WEBROOT,
server({
adapter: async (_request, response) =>
virtual
? new VirtualAdapter({
files: {
properties: {
creationdate: new Date(),
getlastmodified: new Date(),
},
locks: {},
children: [],
},
})
: s3Endpoint && s3AccessKey && s3SecretKey && s3Bucket
? new S3Adapter({
s3Config: {
endpoint: s3Endpoint,
region: s3Region,
credentials: {
accessKeyId: s3AccessKey,
secretAccessKey: s3SecretKey,
},
},
bucket: s3Bucket,
root:
userDirs && response.locals.user
? response.locals.user.username
: '',
})
: new FileSystemAdapter({
root:
userDirs && response.locals.user
? join(root, response.locals.user.username)
: root,
}),
authenticator: userpassdefined
? new CustomAuthenticator({
getUser: async (username) => {
if (username === envuser) {
return new CustomUser({ username });
}
return null;
},
authBasic: async (user, password) => {
if (user.username === envuser && password === envpass) {
return true;
}
return false;
},
})
: htpasswd
? new HtpasswdAuthenticator({
unauthorizedAccess: unauthorized,
})
: pam
? new PamAuthenticator({
unauthorizedAccess: unauthorized,
})
: new InsecureAuthenticator(),
plugins: async (_request, response) => {
const isReadonly =
readonly ||
(unauthorized &&
(response.locals.user == null ||
response.locals.user.username == 'nobody'));
const plugins: Plugin[] = [
new IndexPlugin({ showForms: !isReadonly }),
simpleLogPlugin(),
];
if (isReadonly) {
plugins.push(new ReadOnlyPlugin());
}
if (encryption) {
plugins.push(
new EncryptionPlugin({
salt: '5de338e9a6c8465591821c4f5e1c5acf',
filenameSalt: '3ac159a27a3342c0bb106affac46812f',
filenameIVSalt: '7f3bf86e561d46bcbba06702eb0d7718',
exclude: ['/.htpasswd'],
...(htpasswd || pam ? {} : { globalPassword: encryption }),
}),
);
}
return plugins;
},
}),
);
app.listen(port, () => {
debug(`Listening on ${host}:${port}.`);
debug(`Serving files from ${virtual ? 'RAM' : `"${root}"`}.`);
console.log(`Example Nephele WebDAV server listening on port ${port}`);
});
function simpleLogPlugin() {
return {
async prepare(request: Request, _res: AuthResponse) {
console.log('prepare', request.url);
},
async beforeAuth(request: Request, _res: AuthResponse) {
console.log('beforeAuth', request.url);
},
async afterAuth(request: Request, _res: AuthResponse) {
console.log('afterAuth', request.url);
},
async begin(request: Request, _res: AuthResponse) {
console.log('begin', request.url);
},
async close(request: Request, _res: AuthResponse) {
console.log('close', request.url);
},
async beforeCheckAuthorization(request: Request, _res: AuthResponse) {
console.log('beforeCheckAuthorization', request.url);
},
async afterCheckAuthorization(request: Request, _res: AuthResponse) {
console.log('afterCheckAuthorization', request.url);
},
async beginGet(request: Request, _res: AuthResponse) {
console.log('beginGet', request.url);
},
async preGet(request: Request, _res: AuthResponse) {
console.log('preGet', request.url);
},
async beforeGet(request: Request, _res: AuthResponse) {
console.log('beforeGet', request.url);
},
async afterGet(request: Request, _res: AuthResponse) {
console.log('afterGet', request.url);
},
async beginHead(request: Request, _res: AuthResponse) {
console.log('beginHead', request.url);
},
async preHead(request: Request, _res: AuthResponse) {
console.log('preHead', request.url);
},
async beforeHead(request: Request, _res: AuthResponse) {
console.log('beforeHead', request.url);
},
async afterHead(request: Request, _res: AuthResponse) {
console.log('afterHead', request.url);
},
async beginCopy(request: Request, _res: AuthResponse) {
console.log('beginCopy', request.url);
},
async preCopy(request: Request, _res: AuthResponse) {
console.log('preCopy', request.url);
},
async beforeCopy(request: Request, _res: AuthResponse) {
console.log('beforeCopy', request.url);
},
async afterCopy(request: Request, _res: AuthResponse) {
console.log('afterCopy', request.url);
},
async beginDelete(request: Request, _res: AuthResponse) {
console.log('beginDelete', request.url);
},
async preDelete(request: Request, _res: AuthResponse) {
console.log('preDelete', request.url);
},
async beforeDelete(request: Request, _res: AuthResponse) {
console.log('beforeDelete', request.url);
},
async afterDelete(request: Request, _res: AuthResponse) {
console.log('afterDelete', request.url);
},
async beginLock(request: Request, _res: AuthResponse) {
console.log('beginLock', request.url);
},
async preLock(request: Request, _res: AuthResponse) {
console.log('preLock', request.url);
},
async preLockRefresh(request: Request, _res: AuthResponse) {
console.log('preLockRefresh', request.url);
},
async beforeLockRefresh(request: Request, _res: AuthResponse) {
console.log('beforeLockRefresh', request.url);
},
async beforeLockProvisional(request: Request, _res: AuthResponse) {
console.log('beforeLockProvisional', request.url);
},
async beforeLock(request: Request, _res: AuthResponse) {
console.log('beforeLock', request.url);
},
async afterLock(request: Request, _res: AuthResponse) {
console.log('afterLock', request.url);
},
async beginMkcol(request: Request, _res: AuthResponse) {
console.log('beginMkcol', request.url);
},
async preMkcol(request: Request, _res: AuthResponse) {
console.log('preMkcol', request.url);
},
async beforeMkcol(request: Request, _res: AuthResponse) {
console.log('beforeMkcol', request.url);
},
async afterMkcol(request: Request, _res: AuthResponse) {
console.log('afterMkcol', request.url);
},
async beginMove(request: Request, _res: AuthResponse) {
console.log('beginMove', request.url);
},
async preMove(request: Request, _res: AuthResponse) {
console.log('preMove', request.url);
},
async beforeMove(request: Request, _res: AuthResponse) {
console.log('beforeMove', request.url);
},
async afterMove(request: Request, _res: AuthResponse) {
console.log('afterMove', request.url);
},
async beginOptions(request: Request, _res: AuthResponse) {
console.log('beginOptions', request.url);
},
async preOptions(request: Request, _res: AuthResponse) {
console.log('preOptions', request.url);
},
async beforeOptions(request: Request, _res: AuthResponse) {
console.log('beforeOptions', request.url);
},
async afterOptions(request: Request, _res: AuthResponse) {
console.log('afterOptions', request.url);
},
async beginPropfind(request: Request, _res: AuthResponse) {
console.log('beginPropfind', request.url);
},
async prePropfind(request: Request, _res: AuthResponse) {
console.log('prePropfind', request.url);
},
async beforePropfind(request: Request, _res: AuthResponse) {
console.log('beforePropfind', request.url);
},
async afterPropfind(request: Request, _res: AuthResponse) {
console.log('afterPropfind', request.url);
},
async beginProppatch(request: Request, _res: AuthResponse) {
console.log('beginProppatch', request.url);
},
async preProppatch(request: Request, _res: AuthResponse) {
console.log('preProppatch', request.url);
},
async beforeProppatch(request: Request, _res: AuthResponse) {
console.log('beforeProppatch', request.url);
},
async afterProppatch(request: Request, _res: AuthResponse) {
console.log('afterProppatch', request.url);
},
async beginPut(request: Request, _res: AuthResponse) {
console.log('beginPut', request.url);
},
async prePut(request: Request, _res: AuthResponse) {
console.log('prePut', request.url);
},
async beforePut(request: Request, _res: AuthResponse) {
console.log('beforePut', request.url);
},
async afterPut(request: Request, _res: AuthResponse) {
console.log('afterPut', request.url);
},
async beginUnlock(request: Request, _res: AuthResponse) {
console.log('beginUnlock', request.url);
},
async preUnlock(request: Request, _res: AuthResponse) {
console.log('preUnlock', request.url);
},
async beforeUnlock(request: Request, _res: AuthResponse) {
console.log('beforeUnlock', request.url);
},
async afterUnlock(request: Request, _res: AuthResponse) {
console.log('afterUnlock', request.url);
},
async beginMethod(request: Request, _res: AuthResponse) {
console.log('beginMethod', request.url);
},
async preMethod(request: Request, _res: AuthResponse) {
console.log('preMethod', request.url);
},
async beforeMethod(request: Request, _res: AuthResponse) {
console.log('beforeMethod', request.url);
},
async afterMethod(request: Request, _res: AuthResponse) {
console.log('afterMethod', request.url);
},
};
}