Use corsanywhere on android to patch requests

This commit is contained in:
WardPearce
2024-09-20 19:30:33 +12:00
parent 22cc29f723
commit 9965edbacd
11 changed files with 75 additions and 2 deletions
@@ -12,6 +12,7 @@ dependencies {
implementation project(':capacitor-app')
implementation project(':capacitor-browser')
implementation project(':capgo-inappbrowser')
implementation project(':capacitor-nodejs')
}
@@ -5,6 +5,7 @@
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:usesCleartextTraffic="true"
android:theme="@style/AppTheme">
<activity
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|smallestScreenSize|screenLayout|uiMode"
@@ -10,3 +10,6 @@ project(':capacitor-browser').projectDir = new File('../node_modules/@capacitor/
include ':capgo-inappbrowser'
project(':capgo-inappbrowser').projectDir = new File('../node_modules/@capgo/inappbrowser/android')
include ':capacitor-nodejs'
project(':capacitor-nodejs').projectDir = new File('../node_modules/capacitor-nodejs/android')
+4 -1
View File
@@ -6,8 +6,11 @@ const config: CapacitorConfig = {
webDir: 'build',
plugins: {
CapacitorHttp: {
enabled: true
enabled: false
},
CapacitorNodeJS: {
nodeDir: 'nodejs-android'
}
},
};
+9
View File
@@ -17,6 +17,7 @@
"@capgo/inappbrowser": "^6.6.8",
"@ffmpeg/ffmpeg": "^0.12.10",
"beercss": "^3.7.7",
"capacitor-nodejs": "https://github.com/EdenwareApps/Capacitor-NodeJS/releases/download/v1.0.0-beta.7/capacitor6-nodejs.tgz",
"dashjs": "^4.7.4",
"fuse.js": "^7.0.0",
"he": "^1.2.0",
@@ -4540,6 +4541,14 @@
],
"peer": true
},
"node_modules/capacitor-nodejs": {
"version": "1.0.0-beta.7",
"resolved": "https://github.com/EdenwareApps/Capacitor-NodeJS/releases/download/v1.0.0-beta.7/capacitor6-nodejs.tgz",
"integrity": "sha512-7NwSMxE06nqpuFKyQlt3mWo+VG6Od0d6PBxPKVyPmaptE6sSoimzU/uzQXPWoHV5bwbjgcF+R0BSJAZAyy4gsg==",
"peerDependencies": {
"@capacitor/core": "^6.0.0"
}
},
"node_modules/chalk": {
"version": "4.1.2",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz",
+1
View File
@@ -53,6 +53,7 @@
"@capgo/inappbrowser": "^6.6.8",
"@ffmpeg/ffmpeg": "^0.12.10",
"beercss": "^3.7.7",
"capacitor-nodejs": "https://github.com/EdenwareApps/Capacitor-NodeJS/releases/download/v1.0.0-beta.7/capacitor6-nodejs.tgz",
"dashjs": "^4.7.4",
"fuse.js": "^7.0.0",
"he": "^1.2.0",
+1 -1
View File
@@ -126,7 +126,7 @@
on:submit|preventDefault={() => {
instanceStore.set(ensureNoTrailingSlash(invidiousInstance));
authStore.set(null);
goto('/');
goto('/', { replaceState: true });
ui('#dialog-settings');
}}
>
@@ -0,0 +1,41 @@
import { goto } from "$app/navigation";
import { Capacitor } from "@capacitor/core";
if (Capacitor.getPlatform() === 'android') {
const originalFetch = window.fetch;
// CORS-Anywhere proxy URL
const corsAnywhereProxyUrl: string = 'http://localhost:3000/';
// Overwrite fetch to use CORS-Anywhere proxy
window.fetch = async (requestInput: RequestInfo | URL, requestOptions: RequestInit = {}): Promise<Response> => {
const requestUrl: string =
typeof requestInput === 'string' ? requestInput : requestInput.toString();
// Check if the URL is already proxied, to avoid double proxying
if (!requestUrl.startsWith(corsAnywhereProxyUrl)) {
requestInput = corsAnywhereProxyUrl + requestUrl;
}
// Ensure options.method is set to a valid value
requestOptions.method = requestOptions.method ? requestOptions.method.toUpperCase() : 'GET';
// Use the original fetch with the proxied URL and options
return originalFetch(requestInput, requestOptions);
};
const currentOrigin: string = window.location.protocol + '//' + window.location.host;
const originalXhrOpen = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function (...args: any[]): void {
const targetOriginMatch = /^https?:\/\/([^\/]+)/i.exec(args[1]);
if (targetOriginMatch && targetOriginMatch[0].toLowerCase() !== currentOrigin) {
args[1] = corsAnywhereProxyUrl + args[1];
}
/* @ts-ignore */
return originalXhrOpen.apply(this, args);
};
// Must reload page after patches
goto('/', { replaceState: true });
}
@@ -11,6 +11,7 @@
import SyncParty from '$lib/SyncParty.svelte';
import Thumbnail from '$lib/Thumbnail.svelte';
import { bookmarkletLoadFromUrl, loadSettingsFromEnv } from '$lib/externalSettings';
import '$lib/patches/androidRequests';
import {
activePageStore,
authStore,
@@ -0,0 +1,5 @@
const corsAnywhere = require('cors-anywhere');
corsAnywhere.createServer({
originWhitelist: [],
}).listen(3000, 'localhost');
@@ -0,0 +1,8 @@
{
"name": "cors-patch",
"version": "0.0.1",
"main": "./index.js",
"dependencies": {
"cors-anywhere": "^0.4.4"
}
}