plexsonic 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -33,7 +33,9 @@ cp .env.example .env
33
33
  Install globally from this repo:
34
34
 
35
35
  ```bash
36
- pnpm add -g .
36
+ pnpm add -g plexsonic
37
+ # or
38
+ npm install -g plexsonic
37
39
  ```
38
40
 
39
41
  Then run:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "plexsonic",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "PlexMusic to OpenSubsonic bridge",
5
5
  "main": "./src/index.js",
6
6
  "bin": {
package/src/plex.js CHANGED
@@ -18,6 +18,8 @@
18
18
  * under the License.
19
19
  */
20
20
 
21
+ import { APP_VERSION } from './version.js';
22
+
21
23
  const PLEX_TV_BASE = 'https://plex.tv';
22
24
  const PLEX_CLIENTS_BASE = 'https://clients.plex.tv';
23
25
 
@@ -25,7 +27,7 @@ function makePlexHeaders(config, token = null) {
25
27
  const headers = {
26
28
  Accept: 'application/json',
27
29
  'X-Plex-Product': config.plexProduct,
28
- 'X-Plex-Version': '0.1.0',
30
+ 'X-Plex-Version': APP_VERSION,
29
31
  'X-Plex-Client-Identifier': config.plexClientIdentifier,
30
32
  'X-Plex-Platform': process.platform,
31
33
  'X-Plex-Device': 'Plexsonic Bridge',
package/src/server.js CHANGED
@@ -73,6 +73,39 @@ import { createTokenCipher } from './token-crypto.js';
73
73
  import { emptyNode, failedResponse, failedResponseJson, node, okResponse, okResponseJson } from './subsonic-xml.js';
74
74
 
75
75
  const USERNAME_PATTERN = /^[A-Za-z0-9_.-]{3,32}$/;
76
+ const DEFAULT_CORS_ALLOW_HEADERS = [
77
+ 'Accept',
78
+ 'Authorization',
79
+ 'Content-Type',
80
+ 'X-Requested-With',
81
+ 'X-Plex-Token',
82
+ 'X-Plex-Client-Identifier',
83
+ 'X-Plex-Product',
84
+ 'X-Plex-Version',
85
+ 'X-Plex-Platform',
86
+ 'X-Plex-Device',
87
+ 'X-Plex-Device-Name',
88
+ 'X-Plex-Model',
89
+ ].join(', ');
90
+ const DEFAULT_CORS_ALLOW_METHODS = 'GET, POST, PUT, PATCH, DELETE, OPTIONS';
91
+ const DEFAULT_CORS_EXPOSE_HEADERS = 'content-type, content-length, content-range, accept-ranges, etag, last-modified';
92
+
93
+ function applyCorsHeaders(request, reply) {
94
+ const origin = firstForwardedValue(request.headers?.origin);
95
+ if (origin) {
96
+ reply.header('access-control-allow-origin', origin);
97
+ reply.header('vary', 'Origin');
98
+ } else {
99
+ reply.header('access-control-allow-origin', '*');
100
+ }
101
+
102
+ reply.header('access-control-allow-methods', DEFAULT_CORS_ALLOW_METHODS);
103
+
104
+ const requestedHeaders = firstForwardedValue(request.headers?.['access-control-request-headers']);
105
+ reply.header('access-control-allow-headers', requestedHeaders || DEFAULT_CORS_ALLOW_HEADERS);
106
+ reply.header('access-control-expose-headers', DEFAULT_CORS_EXPOSE_HEADERS);
107
+ reply.header('access-control-max-age', '86400');
108
+ }
76
109
 
77
110
  function normalizeUsername(value) {
78
111
  return String(value || '').trim();
@@ -351,6 +384,31 @@ function decodePasswordParam(rawPassword) {
351
384
  }
352
385
  }
353
386
 
387
+ function syncStoredSubsonicPassword(repo, tokenCipher, account, clearPassword) {
388
+ const normalizedPassword = String(clearPassword || '');
389
+ if (!account || !account.id || !normalizedPassword) {
390
+ return;
391
+ }
392
+
393
+ let shouldUpdate = false;
394
+ if (!account.subsonic_password_enc) {
395
+ shouldUpdate = true;
396
+ } else {
397
+ try {
398
+ const decrypted = tokenCipher.decrypt(account.subsonic_password_enc);
399
+ if (decrypted !== normalizedPassword) {
400
+ shouldUpdate = true;
401
+ }
402
+ } catch {
403
+ shouldUpdate = true;
404
+ }
405
+ }
406
+
407
+ if (shouldUpdate) {
408
+ repo.updateSubsonicPasswordEnc(account.id, tokenCipher.encrypt(normalizedPassword));
409
+ }
410
+ }
411
+
354
412
  async function authenticateSubsonicRequest(request, reply, repo, tokenCipher) {
355
413
  const username = normalizeUsername(getRequestParam(request, 'u'));
356
414
  const passwordRaw = normalizePassword(getRequestParam(request, 'p'));
@@ -424,9 +482,7 @@ async function authenticateSubsonicRequest(request, reply, repo, tokenCipher) {
424
482
  return null;
425
483
  }
426
484
 
427
- if (!account.subsonic_password_enc) {
428
- repo.updateSubsonicPasswordEnc(account.id, tokenCipher.encrypt(decodedPassword));
429
- }
485
+ syncStoredSubsonicPassword(repo, tokenCipher, account, decodedPassword);
430
486
 
431
487
  return account;
432
488
  }
@@ -1981,6 +2037,13 @@ export async function buildServer(config = loadConfig()) {
1981
2037
  },
1982
2038
  });
1983
2039
 
2040
+ app.addHook('onRequest', async (request, reply) => {
2041
+ applyCorsHeaders(request, reply);
2042
+ if (request.method === 'OPTIONS') {
2043
+ return reply.code(204).send();
2044
+ }
2045
+ });
2046
+
1984
2047
  app.addHook('onClose', async () => {
1985
2048
  db.close();
1986
2049
  });
@@ -2357,9 +2420,7 @@ export async function buildServer(config = loadConfig()) {
2357
2420
  return reply.code(401).type('text/html; charset=utf-8').send(loginPage('Invalid username or password.'));
2358
2421
  }
2359
2422
 
2360
- if (!account.subsonic_password_enc) {
2361
- repo.updateSubsonicPasswordEnc(account.id, tokenCipher.encrypt(password));
2362
- }
2423
+ syncStoredSubsonicPassword(repo, tokenCipher, account, password);
2363
2424
 
2364
2425
  request.session.accountId = account.id;
2365
2426
  request.session.username = account.username;
@@ -2417,9 +2478,7 @@ export async function buildServer(config = loadConfig()) {
2417
2478
  });
2418
2479
  }
2419
2480
 
2420
- if (!account.subsonic_password_enc) {
2421
- repo.updateSubsonicPasswordEnc(account.id, tokenCipher.encrypt(password));
2422
- }
2481
+ syncStoredSubsonicPassword(repo, tokenCipher, account, password);
2423
2482
 
2424
2483
  request.session.accountId = account.id;
2425
2484
  request.session.username = account.username;
@@ -18,11 +18,13 @@
18
18
  * under the License.
19
19
  */
20
20
 
21
+ import { APP_VERSION } from './version.js';
22
+
21
23
  const XML_HEADER = '<?xml version="1.0" encoding="UTF-8"?>';
22
24
  const XMLNS = 'http://subsonic.org/restapi';
23
25
  const API_VERSION = '1.16.1';
24
26
  const SERVER_TYPE = 'Plexsonic';
25
- const SERVER_VERSION = '0.1.0';
27
+ const SERVER_VERSION = APP_VERSION;
26
28
  const OPEN_SUBSONIC = true;
27
29
 
28
30
  const TOKEN_START = '\u0001';
package/src/version.js ADDED
@@ -0,0 +1,37 @@
1
+ /* Copyright Yukino Song, SudoMaker Ltd.
2
+ *
3
+ * Licensed to the Apache Software Foundation (ASF) under one
4
+ * or more contributor license agreements. See the NOTICE file
5
+ * distributed with this work for additional information
6
+ * regarding copyright ownership. The ASF licenses this file
7
+ * to you under the Apache License, Version 2.0 (the
8
+ * "License"); you may not use this file except in compliance
9
+ * with the License. You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing,
14
+ * software distributed under the License is distributed on an
15
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
+ * KIND, either express or implied. See the License for the
17
+ * specific language governing permissions and limitations
18
+ * under the License.
19
+ */
20
+
21
+ import { createRequire } from 'node:module';
22
+
23
+ const require = createRequire(import.meta.url);
24
+
25
+ function loadPackageVersion() {
26
+ try {
27
+ const packageJson = require('../package.json');
28
+ const rawVersion = String(packageJson?.version || '').trim();
29
+ if (rawVersion) {
30
+ return rawVersion;
31
+ }
32
+ } catch {}
33
+
34
+ return '0.0.0';
35
+ }
36
+
37
+ export const APP_VERSION = loadPackageVersion();