Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
150 lines (136 loc) · 6.14 KB
/
Copy pathserver.js
File metadata and controls
150 lines (136 loc) · 6.14 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
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
const https = require('https');
const fs = require('fs');
const express = require('express');
const path = require('path');
const app = express();
const sslOptions = {
cert: fs.readFileSync(process.env.WEB_SSL_CRT || "./ssl/localssl.crt"),
key: fs.readFileSync(process.env.WEB_SSL_KEY || "./ssl/localssl.key"),
};
const PORT = process.env.WEB_PORT || process.env.PORT || 3000;
https.createServer(sslOptions, app).listen(PORT, () => {
console.log(`HTTPS server running on port ${PORT}`);
});
app.use(express.static(path.join(__dirname, 'build')));
// ─── OpenSky proxy ───────────────────────────────────────────────────────────
// OpenSky's API does not send CORS headers for third-party origins, so browser
// fetch() is blocked. This proxy forwards requests server-side (no CORS in
// Node.js) and keeps OAuth2 credentials server-side (out of the browser bundle).
//
// GET /opensky/api/states/all?... -> https://opensky-network.org/api/states/all?...
// POST /opensky/token -> https://auth.opensky-network.org/.../token
//
// Credentials are read from public/config.local.json (gitignored) at startup.
const OPENSKY_API = 'https://opensky-network.org/api';
const OPENSKY_TOKEN_URL = 'https://auth.opensky-network.org/auth/realms/opensky-network/protocol/openid-connect/token';
// ─── Config loading ──────────────────────────────────────────────────────────
// Loads public/config.json and public/config.local.json (the latter overrides the
// former). A *missing* file is fine (soft notice). A file that EXISTS but cannot
// be parsed is a real error: we highlight it LOUDLY and abort startup so a broken
// config is never silently ignored.
function fn_loadConfigFile(fileRelPath) {
const absPath = path.join(__dirname, fileRelPath);
let raw;
try {
raw = fs.readFileSync(absPath, 'utf8');
} catch (e) {
if (e.code === 'ENOENT') {
console.log(`[config] ${fileRelPath} not found (skipping)`);
return {};
}
// Any other read error (permissions, I/O...) is loud.
console.error('\n' + '='.repeat(79));
console.error(' CONFIG ERROR :: could not read ' + fileRelPath);
console.error(' path: ' + absPath);
console.error(' error: ' + e.message);
console.error('='.repeat(79) + '\n');
throw e;
}
let parsed;
try {
parsed = JSON.parse(raw);
} catch (e) {
console.error('\n' + '#'.repeat(79));
console.error('######## CONFIG PARSE ERROR ########################################################');
console.error('##');
console.error('## file: ' + fileRelPath);
console.error('## path: ' + absPath);
console.error('## error: ' + e.message);
console.error('##');
console.error('## Fix the JSON syntax in this file and restart the server.');
console.error('## The server will NOT start while a config file is malformed.');
console.error('#'.repeat(79) + '\n');
throw e;
}
console.log(`[config] loaded ${fileRelPath} OK (${Object.keys(parsed).length} top-level key(s))`);
return parsed;
}
let mergedConfig = {};
try {
mergedConfig = Object.assign(
{},
fn_loadConfigFile('public/config.json'),
fn_loadConfigFile('public/config.local.json') // local overrides base
);
} catch (e) {
console.error('FATAL: aborting startup due to config error above.');
process.exit(1);
}
let openskyCreds = { clientId: '', clientSecret: '' };
{
const osc = (mergedConfig.CONST_ADSB_CONFIG && mergedConfig.CONST_ADSB_CONFIG.OPENSKY) || {};
openskyCreds.clientId = osc.CLIENT_ID || '';
openskyCreds.clientSecret = osc.CLIENT_SECRET || '';
if (!openskyCreds.clientId || !openskyCreds.clientSecret) {
console.log('[config] No OpenSky credentials found (anonymous mode)');
} else {
console.log('[config] OpenSky credentials loaded (authenticated mode)');
}
}
function fn_forwardJson(proxyRes, res) {
let body = '';
proxyRes.on('data', (chunk) => { body += chunk; });
proxyRes.on('end', () => {
res.status(proxyRes.statusCode || 502)
.set('Content-Type', proxyRes.headers['content-type'] || 'application/json')
.send(body);
});
}
// Proxy: OpenSky states API (GET)
// Forwards the browser-supplied Authorization header (bearer token from
// /opensky/token) so OpenSky sees an authenticated request and applies the
// higher rate limit. Without this the request goes anonymous and hits 429.
app.get('/opensky/api/*', (req, res) => {
const targetUrl = OPENSKY_API + req.url.replace('/opensky/api', '');
const fwdHeaders = {};
if (req.headers['authorization']) fwdHeaders['Authorization'] = req.headers['authorization'];
const proxyReq = https.get(targetUrl, { headers: fwdHeaders }, (proxyRes) => fn_forwardJson(proxyRes, res));
proxyReq.on('error', (e) => res.status(502).json({ error: e.message }));
});
// Proxy: OpenSky OAuth2 token (POST, server-side only)
app.post('/opensky/token', express.urlencoded({ extended: false }), (req, res) => {
const clientId = req.body.client_id || openskyCreds.clientId;
const clientSecret = req.body.client_secret || openskyCreds.clientSecret;
if (!clientId || !clientSecret) {
return res.status(400).json({ error: 'missing OpenSky credentials' });
}
const payload = new URLSearchParams({
grant_type: 'client_credentials',
client_id: clientId,
client_secret: clientSecret,
}).toString();
const proxyReq = https.request(OPENSKY_TOKEN_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(payload),
},
}, (proxyRes) => fn_forwardJson(proxyRes, res));
proxyReq.on('error', (e) => res.status(502).json({ error: e.message }));
proxyReq.write(payload);
proxyReq.end();
});
// ─── end OpenSky proxy ───────────────────────────────────────────────────────
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});