-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
252 lines (223 loc) · 7.47 KB
/
Copy pathserver.js
File metadata and controls
252 lines (223 loc) · 7.47 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
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
import http from 'http';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { networkInterfaces } from 'os';
import express from 'express';
import crypto from 'crypto';
import { createProxyMiddleware } from 'http-proxy-middleware';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
function getLanIP() {
for (const iface of Object.values(networkInterfaces()))
for (const addr of iface) if (addr.family === 'IPv4' && !addr.internal) return addr.address;
return 'localhost';
}
const HTTP_PORT = 3001;
const SCOPES = 'user-read-playback-state user-modify-playback-state';
const CONFIG_PATH = path.join(__dirname, 'config.json');
function loadConfig() {
try {
return JSON.parse(fs.readFileSync(CONFIG_PATH, 'utf8'));
} catch {
return {
obs: { host: 'localhost', port: '4455', password: '' },
obs_runway: { host: 'localhost', port: '4455', password: '' },
trackaudio: { host: 'localhost', port: '49080' },
spotify: { clientId: '' },
youtube: { chatUrl: '' },
};
}
}
function saveConfig(cfg) {
fs.writeFileSync(CONFIG_PATH, JSON.stringify(cfg, null, 2));
}
let appConfig = loadConfig();
const spotify = {
verifier: null,
clientId: null,
redirectUri: null,
accessToken: null,
refreshToken: null,
pending: false,
};
const mkVerifier = () => crypto.randomBytes(64).toString('base64url');
const mkChallenge = (v) => crypto.createHash('sha256').update(v).digest('base64url');
function getOrigin(req) {
const proto = req.headers['x-forwarded-proto'] || (req.socket?.encrypted ? 'https' : 'http');
const host = req.headers['x-forwarded-host'] || req.headers.host;
return `${proto}://${host}`;
}
const app = express();
app.use(express.json());
app.set('trust proxy', true);
const isProd = process.env.NODE_ENV === 'production';
app.get('/api/config', (_req, res) => {
res.json({
obs: appConfig.obs,
obs_runway: appConfig.obs_runway,
trackaudio: appConfig.trackaudio,
spotify: { clientId: appConfig.spotify?.clientId || '' },
youtube: appConfig.youtube || { chatUrl: '' },
});
});
app.post('/api/config', (req, res) => {
const { obs, obs_runway, trackaudio, spotify, youtube } = req.body;
if (obs) appConfig.obs = { ...appConfig.obs, ...obs };
if (obs_runway) appConfig.obs_runway = { ...appConfig.obs_runway, ...obs_runway };
if (trackaudio) appConfig.trackaudio = { ...appConfig.trackaudio, ...trackaudio };
if (spotify) appConfig.spotify = { ...appConfig.spotify, ...spotify };
if (youtube) appConfig.youtube = { ...appConfig.youtube, ...youtube };
saveConfig(appConfig);
res.json({ ok: true });
});
app.get('/manifest.json', (_req, res) => {
res.sendFile(path.join(__dirname, isProd ? 'dist' : 'public', 'manifest.json'));
});
app.get('/sw.js', (_req, res) => {
res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
res.setHeader('Service-Worker-Allowed', '/');
res.sendFile(path.join(__dirname, isProd ? 'dist' : 'public', 'sw.js'));
});
app.get('/icons/:file', (req, res) => {
const iconDir = path.join(__dirname, isProd ? 'dist' : 'public', 'icons');
res.sendFile(path.join(iconDir, req.params.file));
});
app.get('/auth/spotify/start', (req, res) => {
const { clientId } = req.query;
if (!clientId) return res.status(400).json({ error: 'clientId required' });
const verifier = mkVerifier();
const challenge = mkChallenge(verifier);
const redirectUri = `${getOrigin(req)}/auth/spotify/callback`;
Object.assign(spotify, {
verifier,
clientId,
redirectUri,
accessToken: null,
refreshToken: null,
pending: true,
});
const params = new URLSearchParams({
client_id: clientId,
response_type: 'code',
redirect_uri: redirectUri,
scope: SCOPES,
code_challenge_method: 'S256',
code_challenge: challenge,
});
res.json({ url: `https://accounts.spotify.com/authorize?${params}` });
});
app.get('/auth/spotify/callback', async (req, res) => {
const { code, error } = req.query;
const html = (title, body, color = '#e8e4f0') =>
`<html><body style="font-family:sans-serif;padding:40px;background:#0f0e13;color:${color}">
<h2>${title}</h2><p>${body}</p>
<script>setTimeout(()=>window.close(),2500)</script>
</body></html>`;
if (error) {
spotify.pending = false;
return res.send(html('❌ Auth failed', `Spotify returned: ${error}`));
}
if (!code || !spotify.verifier || !spotify.clientId || !spotify.redirectUri) {
spotify.pending = false;
return res.status(400).send(html('❌ Missing session', 'Start the auth flow again from the app.'));
}
try {
const r = await fetch('https://accounts.spotify.com/api/token', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: new URLSearchParams({
grant_type: 'authorization_code',
code,
redirect_uri: spotify.redirectUri,
client_id: spotify.clientId,
code_verifier: spotify.verifier,
}),
});
const data = await r.json();
if (data.access_token) {
spotify.accessToken = data.access_token;
spotify.refreshToken = data.refresh_token || null;
spotify.pending = false;
spotify.verifier = null;
res.send(html('✓ Spotify authenticated!', 'You can close this tab and return to the app.', '#34d399'));
} else {
spotify.pending = false;
res.send(html('❌ Token error', data.error_description || data.error || 'Unknown error'));
}
} catch (e) {
spotify.pending = false;
res.status(500).send(html('❌ Server error', e.message));
}
});
app.get('/auth/spotify/status', (_req, res) => {
res.json(
spotify.accessToken
? {
authenticated: true,
accessToken: spotify.accessToken,
refreshToken: spotify.refreshToken,
}
: { authenticated: false, pending: spotify.pending },
);
});
app.post('/auth/spotify/refresh', async (req, res) => {
const clientId = req.body.clientId || spotify.clientId;
const refreshToken = req.body.refreshToken || spotify.refreshToken;
if (!refreshToken || !clientId) return res.status(400).json({ error: 'Missing fields' });
try {
const r = await fetch('https://accounts.spotify.com/api/token', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: new URLSearchParams({
grant_type: 'refresh_token',
refresh_token: refreshToken,
client_id: clientId,
}),
});
const data = await r.json();
if (data.access_token) {
spotify.accessToken = data.access_token;
if (data.refresh_token) spotify.refreshToken = data.refresh_token;
}
res.status(r.ok ? 200 : r.status).json(data);
} catch (e) {
res.status(500).json({ error: e.message });
}
});
app.post('/auth/spotify/logout', (_req, res) => {
Object.assign(spotify, {
accessToken: null,
refreshToken: null,
pending: false,
verifier: null,
redirectUri: null,
});
res.json({ ok: true });
});
const distDir = path.join(__dirname, 'dist');
if (isProd && fs.existsSync(distDir)) {
app.use(express.static(distDir));
app.get('*', (_req, res) => res.sendFile(path.join(distDir, 'index.html')));
} else {
app.use(
'/',
createProxyMiddleware({
target: 'http://127.0.0.1:5173',
changeOrigin: true,
ws: true,
on: {
error: (_e, _q, res) => res.status(502).send('Vite dev server not running — run: npm run dev'),
},
}),
);
}
const bindHost = isProd ? '127.0.0.1' : '0.0.0.0';
http.createServer(app).listen(HTTP_PORT, bindHost, () => {
const mode = isProd ? 'production' : 'development';
console.log(`\n CTP Controller v1.2.0 [${mode}]`);
console.log(` Listening: http://${bindHost}:${HTTP_PORT}`);
if (!isProd) {
console.log(` Network: http://${getLanIP()}:${HTTP_PORT}`);
}
console.log();
});