mirror of
https://github.com/hzrd149/nostrudel.git
synced 2025-04-08 20:08:02 +02:00
add default cors proxy
This commit is contained in:
parent
b4d7d0c642
commit
70bada5e96
5
.changeset/unlucky-melons-divide.md
Normal file
5
.changeset/unlucky-melons-divide.md
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"nostrudel": minor
|
||||||
|
---
|
||||||
|
|
||||||
|
Add <url> and <encoded_url> options to CORS proxy url
|
5
.changeset/violet-walls-dance.md
Normal file
5
.changeset/violet-walls-dance.md
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"nostrudel": minor
|
||||||
|
---
|
||||||
|
|
||||||
|
Use corsproxy.io as default service for CORS proxy
|
@ -3,15 +3,27 @@ import { convertToUrl } from "./url";
|
|||||||
|
|
||||||
const corsFailedHosts = new Set();
|
const corsFailedHosts = new Set();
|
||||||
|
|
||||||
|
export function createCorsUrl(url: URL | string, corsProxy = appSettings.value.corsProxy) {
|
||||||
|
if (!corsProxy) return url;
|
||||||
|
|
||||||
|
if (corsProxy.includes("<url>")) {
|
||||||
|
return corsProxy.replace("<url>", "" + url);
|
||||||
|
} else if (corsProxy.includes("<encoded_url>")) {
|
||||||
|
return corsProxy.replace("<encoded_url>", encodeURIComponent("" + url));
|
||||||
|
} else {
|
||||||
|
return corsProxy.endsWith("/") ? corsProxy + url : corsProxy + "/" + url;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function fetchWithCorsFallback(url: URL | string, opts?: RequestInit) {
|
export function fetchWithCorsFallback(url: URL | string, opts?: RequestInit) {
|
||||||
if (!appSettings.value.corsProxy) return fetch(url, opts);
|
if (!appSettings.value.corsProxy) return fetch(url, opts);
|
||||||
|
|
||||||
if (corsFailedHosts.has(convertToUrl(url).host)) {
|
if (corsFailedHosts.has(convertToUrl(url).host)) {
|
||||||
return fetch(appSettings.value.corsProxy + url, opts);
|
return fetch(createCorsUrl(url), opts);
|
||||||
}
|
}
|
||||||
|
|
||||||
return fetch(url, opts).catch((e) => {
|
return fetch(url, opts).catch((e) => {
|
||||||
corsFailedHosts.add(convertToUrl(url).host);
|
corsFailedHosts.add(convertToUrl(url).host);
|
||||||
return fetch(appSettings.value.corsProxy + url, opts);
|
return fetch(createCorsUrl(url), opts);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -42,7 +42,7 @@ export const defaultSettings: AppSettings = {
|
|||||||
|
|
||||||
primaryColor: "#8DB600",
|
primaryColor: "#8DB600",
|
||||||
imageProxy: "",
|
imageProxy: "",
|
||||||
corsProxy: "",
|
corsProxy: "https://corsproxy.io/?<encoded_url>",
|
||||||
showContentWarning: true,
|
showContentWarning: true,
|
||||||
twitterRedirect: undefined,
|
twitterRedirect: undefined,
|
||||||
redditRedirect: undefined,
|
redditRedirect: undefined,
|
||||||
|
@ -11,10 +11,12 @@ import {
|
|||||||
Input,
|
Input,
|
||||||
Link,
|
Link,
|
||||||
FormErrorMessage,
|
FormErrorMessage,
|
||||||
|
Code,
|
||||||
} from "@chakra-ui/react";
|
} from "@chakra-ui/react";
|
||||||
import { useFormContext } from "react-hook-form";
|
import { useFormContext } from "react-hook-form";
|
||||||
import { safeUrl } from "../../helpers/parse";
|
import { safeUrl } from "../../helpers/parse";
|
||||||
import { AppSettings } from "../../services/settings/migrations";
|
import { AppSettings } from "../../services/settings/migrations";
|
||||||
|
import { createCorsUrl } from "../../helpers/cors";
|
||||||
|
|
||||||
async function validateInvidiousUrl(url?: string) {
|
async function validateInvidiousUrl(url?: string) {
|
||||||
if (!url) return true;
|
if (!url) return true;
|
||||||
@ -31,7 +33,7 @@ async function validateCorsProxy(url?: string) {
|
|||||||
try {
|
try {
|
||||||
const controller = new AbortController();
|
const controller = new AbortController();
|
||||||
const timeoutId = setTimeout(() => controller.abort(), 2000);
|
const timeoutId = setTimeout(() => controller.abort(), 2000);
|
||||||
const res = await fetch(new URL("/https://example.com", url), { signal: controller.signal });
|
const res = await fetch(createCorsUrl("https://example.com", url), { signal: controller.signal });
|
||||||
return res.ok || "Cant reach instance";
|
return res.ok || "Cant reach instance";
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return "Cant reach instance";
|
return "Cant reach instance";
|
||||||
@ -122,16 +124,26 @@ export default function PrivacySettings() {
|
|||||||
<FormLabel>CORS Proxy</FormLabel>
|
<FormLabel>CORS Proxy</FormLabel>
|
||||||
<Input
|
<Input
|
||||||
type="url"
|
type="url"
|
||||||
placeholder="https://cors.example.com/"
|
placeholder="https://corsproxy.io/?<encoded_url>"
|
||||||
{...register("corsProxy", { setValueAs: safeUrl, validate: validateCorsProxy })}
|
{...register("corsProxy", { validate: validateCorsProxy })}
|
||||||
/>
|
/>
|
||||||
{formState.errors.corsProxy && <FormErrorMessage>{formState.errors.corsProxy.message}</FormErrorMessage>}
|
{formState.errors.corsProxy && <FormErrorMessage>{formState.errors.corsProxy.message}</FormErrorMessage>}
|
||||||
<FormHelperText>
|
<FormHelperText>
|
||||||
This is used as a fallback when verifying NIP-05 ids and fetching open-graph metadata. URL to an instance
|
This is used as a fallback ( to bypass CORS restrictions ) when verifying NIP-05 ids and fetching
|
||||||
of{" "}
|
open-graph metadata.
|
||||||
|
<br />
|
||||||
|
This can either point to an instance of{" "}
|
||||||
<Link href="https://github.com/Rob--W/cors-anywhere" isExternal color="blue.500">
|
<Link href="https://github.com/Rob--W/cors-anywhere" isExternal color="blue.500">
|
||||||
cors-anywhere
|
cors-anywhere
|
||||||
</Link>
|
</Link>{" "}
|
||||||
|
or{" "}
|
||||||
|
<Link href="https://corsproxy.io/" isExternal color="blue.500">
|
||||||
|
corsproxy.io
|
||||||
|
</Link>{" "}
|
||||||
|
<br />
|
||||||
|
<Code fontSize="0.9em">{`<url>`}</Code> or <Code fontSize="0.9em">{`<encoded_url>`}</Code> can be used to
|
||||||
|
inject the raw or the encoded url into the proxy url ( example:{" "}
|
||||||
|
<Code fontSize="0.9em">{`https://corsproxy.io/?<encoded_url>`}</Code> )
|
||||||
</FormHelperText>
|
</FormHelperText>
|
||||||
</FormControl>
|
</FormControl>
|
||||||
</Flex>
|
</Flex>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user