Skip to content

Commit

Permalink
feat: Revert multiple plex server urls setup due to plex cors
Browse files Browse the repository at this point in the history
  • Loading branch information
Ipmake committed Jan 15, 2025
1 parent 134b644 commit 6d9cde0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 107 deletions.
40 changes: 12 additions & 28 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import { randomBytes } from 'crypto';
/*
* ENVIRONMENT VARIABLES
*
* PLEX_SERVERS: A comma separated list of Plex servers the frontend can connect to
* PROXY_PLEX_SERVER: The URL of the Plex server to proxy requests to
* FRONTEND_SERVER_CHECK_TIMEOUT?: The timeout in milliseconds for the proxy to check if the frontend server is reachable (default: 2000)
* PLEX_SERVER: The URL of the Plex server that the frontend will connect to
* PROXY_PLEX_SERVER?: The URL of the Plex server to proxy requests to
* DISABLE_PROXY?: If set to true, the proxy will be disabled and all requests go directly to the Plex server from the frontend (NOT RECOMMENDED)
* DISABLE_TLS_VERIFY?: If set to true, the proxy will not check any https ssl certificates
**/
Expand All @@ -26,37 +25,23 @@ const app = express();
app.use(express.json());

(async () => {
if (process.env.PLEX_SERVER) {
status.error = true;
status.message = 'PLEX_SERVER has changed to PLEX_SERVERS. Please view the upgrade guide in the 1.0.0 release notes on github. https://github.com/Ipmake/PerPlexed/releases/tag/v1.0.0';
return;
}

if (!process.env.PLEX_SERVERS) {
if (!process.env.PLEX_SERVER) {
status.error = true;
status.message = 'PLEX_SERVERS environment variable not set';
status.message = 'PLEX_SERVER environment variable not set';
console.error('PLEX_SERVER environment variable not set');
return;
}

if (!process.env.PROXY_PLEX_SERVER && process.env.DISABLE_PROXY !== 'true') {
status.error = true;
status.message = 'PROXY_PLEX_SERVER environment variable not set. Please view the upgrade guide in the 1.0.0 release notes on github. https://github.com/Ipmake/PerPlexed/releases/tag/v1.0.0';
console.error('PROXY_PLEX_SERVER environment variable not set');
return;
}

if (process.env.PLEX_SERVERS) {
// check if the PLEX_SERVERS environment variable is a comma separated list and whether each server is a valid URL, the URL must not end with a /
const servers = process.env.PLEX_SERVERS.split(',');
const invalidServers = servers.filter((server) => !server.trim().match(/^https?:\/\/[^\/]+$/));
if (!process.env.PROXY_PLEX_SERVER && process.env.DISABLE_PROXY !== 'true') process.env.PROXY_PLEX_SERVER = process.env.PLEX_SERVER

if (invalidServers.length > 0) {
if (process.env.PLEX_SERVER) {
// check if the PLEX_SERVER environment variable is a valid URL, the URL must not end with a /
if (!process.env.PLEX_SERVER.match(/^https?:\/\/[^\/]+$/)) {
status.error = true;
status.message = 'Invalid PLEX_SERVERS environment variable. The URL must start with http:// or https:// and must not end with a /';
console.error('Invalid PLEX_SERVERS environment variable. The URL must start with http:// or https:// and must not end with a /');
status.message = 'Invalid PLEX_SERVER environment variable. The URL must start with http:// or https:// and must not end with a /';
console.error('Invalid PLEX_SERVER environment variable. The URL must start with http:// or https:// and must not end with a /');
return;
}
}
}

if (process.env.PROXY_PLEX_SERVER && process.env.DISABLE_PROXY !== 'true') {
Expand Down Expand Up @@ -99,11 +84,10 @@ app.get('/status', (req, res) => {

app.get('/config', (req, res) => {
res.send({
PLEX_SERVERS: (process.env.PLEX_SERVERS as string).split(",").map((server) => server.trim()),
PLEX_SERVER: process.env.PLEX_SERVER,
DEPLOYMENTID: deploymentID,
CONFIG: {
DISABLE_PROXY: process.env.DISABLE_PROXY === 'true',
FRONTEND_SERVER_CHECK_TIMEOUT: parseInt(process.env.FRONTEND_SERVER_CHECK_TIMEOUT || '2000'),
}
});
});
Expand Down
79 changes: 2 additions & 77 deletions frontend/src/pages/Startup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,87 +107,12 @@ function Startup() {
reload = true;
}

const getBestServer: () => Promise<string | null> = async () => {
// if the current page is http then filter all https servers
const servers = config.PLEX_SERVERS.filter((server) => {
if (window.location.protocol === "http:") {
if (server.startsWith("https:")) return false;
}
if (window.location.protocol === "https:") {
if (server.startsWith("http:")) return false;
}
return true;
});

if (servers.length === 0) {
setFrontEndStatus({
ready: false,
error: true,
message:
"No servers available for the current protocol. Due to the same-origin policy, the Plex server must be accessible over the same protocol as the PerPlexed frontend.",
});
return null;
}

const reachableServers = await Promise.all(
servers.map((server) =>
axios
.get(`${server}/identity`, {
timeout: config.CONFIG.FRONTEND_SERVER_CHECK_TIMEOUT,
})
.then((res) => {
if (res.data?.MediaContainer?.machineIdentifier) return server;
return null;
})
.catch((error) => {
if (error.response && error.response.status === 0) return server; // CORS error
return null;
})
)
);

// server priority goes from left to right from highest to lowest
const bestServer = reachableServers.find((server) => server !== null);
if (!bestServer) {
setFrontEndStatus({
ready: false,
error: true,
message:
"No servers reachable. Please check the backend logs for more information.",
});
return null;
}

return bestServer;
};

const currServer = localStorage.getItem("server");
const lastDeployId = localStorage.getItem("deploymentId");

let bestServer: string | null = null;

// if a server is defined in localStorage and the deploymentId is the same as the last one, only check the current server for reachability, otherwise get a new best server
if (currServer && lastDeployId === config.DEPLOYMENTID) {
// check if the current server is reachable, if not get a new best server
const res = await axios
.get(`${currServer}/identity`, {
timeout: config.CONFIG.FRONTEND_SERVER_CHECK_TIMEOUT,
})
.then(() => currServer)
.catch(() => null);

if (res) bestServer = currServer;
else bestServer = await getBestServer();
} else {
// get a new best server
bestServer = await getBestServer();
}

if(bestServer !== currServer) reload = true;
if(config.PLEX_SERVER !== currServer) reload = true;
skipUpdates.current = false;

if(bestServer) localStorage.setItem("server", bestServer);
else return localStorage.removeItem("server");
localStorage.setItem("server", config.PLEX_SERVER);
localStorage.setItem("deploymentId", config.DEPLOYMENTID);


Expand Down
3 changes: 1 addition & 2 deletions frontend/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ declare namespace PerPlexed {
}

interface Config {
PLEX_SERVERS: string[];
PLEX_SERVER: string;
DEPLOYMENTID: string;
CONFIG: {
DISABLE_PROXY: boolean;
FRONTEND_SERVER_CHECK_TIMEOUT: number;
}
}
}

0 comments on commit 6d9cde0

Please sign in to comment.