diff --git a/package.json b/package.json index 2af6625..fc13425 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "compromise-dates": "^2.2.1", "compromise-numbers": "^1.4.0", "compromise-sentences": "^0.3.0", - "cors": "^2.8.5", + "cors-anywhere": "^0.4.4", "dayjs": "^1.10.6", "ellipsize": "^0.1.0", "express": "^4.17.1", diff --git a/src/client/components/AirDCPPSettings/AirDCPPHubsForm.tsx b/src/client/components/AirDCPPSettings/AirDCPPHubsForm.tsx index 230106d..cfc239f 100644 --- a/src/client/components/AirDCPPSettings/AirDCPPHubsForm.tsx +++ b/src/client/components/AirDCPPSettings/AirDCPPHubsForm.tsx @@ -5,6 +5,7 @@ import { useDispatch } from "react-redux"; import { isEmpty, isUndefined } from "lodash"; import Select from "react-select"; import { saveSettings } from "../../actions/settings.actions"; +import { CORS_PROXY_SERVER_URI } from "../../constants/endpoints"; export const AirDCPPHubsForm = (airDCPPClientUserSettings): ReactElement => { const { settings } = airDCPPClientUserSettings; @@ -14,7 +15,7 @@ export const AirDCPPHubsForm = (airDCPPClientUserSettings): ReactElement => { useEffect(() => { if (!isEmpty(settings)) { axios({ - url: `${settings.directConnect.client.host.protocol}://${settings.directConnect.client.host.hostname}/api/v1/hubs`, + url: `${CORS_PROXY_SERVER_URI}${settings.directConnect.client.host.protocol}://${settings.directConnect.client.host.hostname}/api/v1/hubs`, method: "GET", headers: { Authorization: `${settings.directConnect.client.airDCPPUserSettings.auth_token}`, diff --git a/src/client/components/AirDCPPSettings/AirDCPPSettingsForm.tsx b/src/client/components/AirDCPPSettings/AirDCPPSettingsForm.tsx index 7e04e27..22d8635 100644 --- a/src/client/components/AirDCPPSettings/AirDCPPSettingsForm.tsx +++ b/src/client/components/AirDCPPSettings/AirDCPPSettingsForm.tsx @@ -7,6 +7,7 @@ import axios from "axios"; import { AirDCPPSocketContext } from "../../context/AirDCPPSocket"; import AirDCPPSocket from "../../services/DcppSearchService"; import { isUndefined, isEmpty } from "lodash"; +import { CORS_PROXY_SERVER_URI } from "../../constants/endpoints"; export const AirDCPPSettingsForm = (airDCPPClientSettings): ReactElement => { const { settings } = airDCPPClientSettings; @@ -16,7 +17,7 @@ export const AirDCPPSettingsForm = (airDCPPClientSettings): ReactElement => { const onSubmit = async (values) => { try { const airDCPPResponse = await axios({ - url: `${values.protocol}://${values.hostname}/api/v1/sessions/authorize`, + url: `${CORS_PROXY_SERVER_URI}${values.protocol}://${values.hostname}/api/v1/sessions/authorize`, method: "POST", data: { username: values.username, diff --git a/src/client/constants/endpoints.ts b/src/client/constants/endpoints.ts index bc6d97b..82d7527 100644 --- a/src/client/constants/endpoints.ts +++ b/src/client/constants/endpoints.ts @@ -9,6 +9,13 @@ export const hostURIBuilder = (options: Record): string => { ); }; +export const CORS_PROXY_SERVER_URI = hostURIBuilder({ + protocol: "http", + host: process.env.UNDERLYING_HOSTNAME || "localhost", + port: "8050", + apiPath: "/", +}); + export const COMICBOOKINFO_SERVICE_URI = hostURIBuilder({ protocol: "http", host: process.env.UNDERLYING_HOSTNAME || "localhost", diff --git a/src/server/index.ts b/src/server/index.ts index 4204ed7..2d87859 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -2,9 +2,11 @@ import express, { Request, Response, Router, Express } from "express"; import bodyParser from "body-parser"; import router from "./route"; import path from "path"; +import cors_proxy from "cors-anywhere"; // call express const app: Express = express(); // define our app using express +const host = process.env.HOST || "0.0.0.0"; // configure app to use bodyParser for // Getting data from body of requests @@ -12,6 +14,7 @@ app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); const port: number = Number(process.env.PORT) || 8050; // set our port +const proxyPort = 8050; // set our port app.get("/", (req: Request, res: Response) => { console.log("sending index.html"); @@ -27,6 +30,20 @@ app.use("/api", routes); app.use(express.static("dist")); app.use(express.static("public")); -app.listen(port); +// app.listen(port); +// console.log(`Server is listening on ${port}`); -console.log(`Server is listening on ${port}`); +cors_proxy + .createServer({ + originWhitelist: [], // Allow all origins + requireHeader: ["origin", "x-requested-with"], + removeHeaders: ["cookie", "cookie2"], + }) + .listen(port, host, function () { + console.log( + "ThreeTwo! Express server with CORS Anywhere running on " + + host + + ":" + + proxyPort, + ); + }); diff --git a/yarn.lock b/yarn.lock index 75c70da..c59a40b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4291,7 +4291,15 @@ core-util-is@~1.0.0: resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.3.tgz#a6042d3634c2b27e9328f837b965fac83808db85" integrity sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ== -cors@^2.8.5, cors@~2.8.5: +cors-anywhere@^0.4.4: + version "0.4.4" + resolved "https://registry.yarnpkg.com/cors-anywhere/-/cors-anywhere-0.4.4.tgz#98892fcab55f408fff13a63e125135c18dc22ca8" + integrity sha512-8OBFwnzMgR4mNrAeAyOLB2EruS2z7u02of2bOu7i9kKYlZG+niS7CTHLPgEXKWW2NAOJWRry9RRCaL9lJRjNqg== + dependencies: + http-proxy "1.11.1" + proxy-from-env "0.0.1" + +cors@~2.8.5: version "2.8.5" resolved "https://registry.yarnpkg.com/cors/-/cors-2.8.5.tgz#eac11da51592dd86b9f06f6e7ac293b3df875d29" integrity sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g== @@ -5533,6 +5541,11 @@ etag@~1.8.1: resolved "https://registry.yarnpkg.com/etag/-/etag-1.8.1.tgz#41ae2eeb65efa62268aebfea83ac7d79299b0887" integrity sha1-Qa4u62XvpiJorr/qg6x9eSmbCIc= +eventemitter3@1.x.x: + version "1.2.0" + resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-1.2.0.tgz#1c86991d816ad1e504750e73874224ecf3bec508" + integrity sha1-HIaZHYFq0eUEdQ5zh0Ik7PO+xQg= + eventemitter3@^3.0.0: version "3.1.2" resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-3.1.2.tgz#2d3d48f9c346698fce83a85d7d664e98535df6e7" @@ -6857,6 +6870,14 @@ http-proxy-middleware@0.19.1: lodash "^4.17.11" micromatch "^3.1.10" +http-proxy@1.11.1: + version "1.11.1" + resolved "https://registry.yarnpkg.com/http-proxy/-/http-proxy-1.11.1.tgz#71df55757e802d58ea810df2244019dda05ae85d" + integrity sha1-cd9VdX6ALVjqgQ3yJEAZ3aBa6F0= + dependencies: + eventemitter3 "1.x.x" + requires-port "0.x.x" + http-proxy@^1.17.0: version "1.18.1" resolved "https://registry.yarnpkg.com/http-proxy/-/http-proxy-1.18.1.tgz#401541f0534884bbf95260334e72f88ee3976549" @@ -10673,6 +10694,11 @@ proxy-addr@~2.0.5: forwarded "0.2.0" ipaddr.js "1.9.1" +proxy-from-env@0.0.1: + version "0.0.1" + resolved "https://registry.yarnpkg.com/proxy-from-env/-/proxy-from-env-0.0.1.tgz#b27c4946e9e6d5dbadb7598a6435d3014c4cfd49" + integrity sha1-snxJRunm1dutt1mKZDXTAUxM/Uk= + prr@~1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/prr/-/prr-1.0.1.tgz#d3fc114ba06995a45ec6893f484ceb1d78f5f476" @@ -11673,6 +11699,11 @@ require-main-filename@^2.0.0: resolved "https://registry.yarnpkg.com/require-main-filename/-/require-main-filename-2.0.0.tgz#d0b329ecc7cc0f61649f62215be69af54aa8989b" integrity sha512-NKN5kMDylKuldxYLSUfrbo5Tuzh4hd+2E8NPPX02mZtn1VuREQToYe/ZdlJy+J3uCpfaiGF05e7B8W0iXbQHmg== +requires-port@0.x.x: + version "0.0.1" + resolved "https://registry.yarnpkg.com/requires-port/-/requires-port-0.0.1.tgz#4b4414411d9df7c855995dd899a8c78a2951c16d" + integrity sha1-S0QUQR2d98hVmV3YmajHiilRwW0= + requires-port@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/requires-port/-/requires-port-1.0.0.tgz#925d2601d39ac485e091cf0da5c6e694dc3dcaff"