-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.js
More file actions
70 lines (60 loc) · 2.3 KB
/
Copy pathproxy.js
File metadata and controls
70 lines (60 loc) · 2.3 KB
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
const fs = require('node:fs');
const http = require('node:http');
const https = require('node:https');
const os = require('node:os');
const path = require('node:path');
const php_port = Number(process.env.PORT || 4000);
const tls_port = Number(process.env.TLS_PORT || 4443);
const tls_data = path.join(process.env.XDG_STATE_HOME || path.join(os.homedir(), ".local/state"), 'everything/tls');
const tls_options = {
key: fs.readFileSync(path.join(tls_data, 'localhost.key')),
cert: fs.readFileSync(path.join(tls_data, 'localhost.crt')),
};
const handler = (request, response) => {
if(request.url == '/.well-known/caldav') {
response.writeHead(301, {location: `https://${request.headers.host}/caldav/`});
response.end();
return;
}
if(request.url == '/.well-known/carddav') {
response.writeHead(301, {location: `https://${request.headers.host}/carddav/`});
response.end();
return;
}
// Apple's port-less CardDAV auto-discovery probes /principals/ on the
// legacy port 8843; the CardDAV root answers the same questions.
if(request.url == '/principals/' || request.url == '/principals') {
request.url = '/carddav/';
}
const upstream = http.request({
hostname: '127.0.0.1',
port: php_port,
method: request.method,
path: request.url,
headers: {
...request.headers,
host: `localhost:${php_port}`,
'x-forwarded-proto': 'https',
},
}, (upstream_response) => {
response.writeHead(upstream_response.statusCode, upstream_response.headers)
upstream_response.pipe(response)
})
upstream.on('error', (error) => {
response.writeHead(502, {'content-type': 'text/plain'});
response.end(`Bad gateway: ${error.message}\n`);
});
request.pipe(upstream);
};
const server = https.createServer(tls_options, handler);
const legacy = https.createServer(tls_options, handler);
// No host: binds dual-stack, so localhost resolving to ::1 (as macOS
// system services do) reaches the proxy too. 8843 is the legacy CardDAV
// TLS port that Apple's auto-discovery probes when it ignores the
// configured port (macOS 26 does).
server.listen(tls_port, () => {
console.log(`Node ${process.version.slice(1)} Proxy Server (https://[::]:${tls_port}) started`)
});
legacy.listen(8843, () => {
console.log(`Node Discovery Server (https://[::]:8843) started`)
});