Http proxy

This commit is contained in:
octospacc 2023-04-29 13:06:33 +02:00
parent 8dedcc9c50
commit 95425df2ab
8 changed files with 166 additions and 40 deletions

View File

@ -14,6 +14,9 @@ var FriendicaCredentials = 'redacted:redacted';
// Development
var Debug = true;
var UseFakeApi = false;
var HttpProxy = /*'';*/ 'http://localhost:44380/';
var ProxyStrictMode = true;
</script>
<script data-build-json="true">
var Assets = {

View File

@ -1,5 +1,5 @@
// Transform JSON tree into a new using a template schema
// DEVNOTE: Unsafe, should check for colliding "__" keys from input tree and act accordingly
function JsonTransformA(TreesOld, SchemaCurr, SchemaRoot) {
if (Array.isArray(TreesOld)) {
var ListNew = [];
@ -12,6 +12,7 @@ function JsonTransformA(TreesOld, SchemaCurr, SchemaRoot) {
};
};
// DEVNOTE: Unsafe, should check for colliding "__" keys from input tree and act accordingly
function JsonTransformCycleA(TreeOld, SchemaCurr, SchemaRoot) {
var TreeNew = {};
Object.keys(TreeOld).forEach(function(KeyOld){

View File

@ -1,18 +1,28 @@
var Tasker = {};
var Persist = {Servers: {}, Sources: {}, Identities: {},};
var Present = structuredClone(Persist);
var Tasker = {};
Persist.__Set__ = function __Set__() {
// set value to own object
// set value to Present object too
// rewrite own object into cookies
};
Present.__Set__ = function __Set__() {
// like Persist but only set value to own object
};
var ApiCache = {
__Store__(Data, Key, Where) {
ApiCache[Where][Key] = Data;
ApiCache[Where][Key].__Time__ = Date.now();
__Store__(Data, Path/*Key, Where*/) {
eval(`ApiCache.${Path} = Data;`);
},
__UrlStore__(Data) {
ApiCache.__Store__(Data, Data.Url, 'Urls');
ApiCache.__Store__(Data, `Urls['${Data.Url}']`);
},
Urls: {},
};
Assets._ = function _(Name) {
Assets.__ = function __(Name) {
if (Name in Assets) {
if (Assets[Name].startsWith('data:')) {
return Assets[Name];
@ -22,7 +32,7 @@ Assets._ = function _(Name) {
};
};
Strings._ = function _(Name) {
Strings.__ = function __(Name) {
// TODO: Handle arbitrary nestation
if (Name in Strings) {
if (Strings[Name]['en']) { // TODO{ Select this language from user config
@ -77,13 +87,14 @@ function HtmlAssign(Id, Data) {
};
function HtmlAssignPropper(El, Data) {
El.dataset.assign.trim().split(' ').forEach(function(Att){
var Toks = Att.split(':');
var Prop = Data;
Toks[1].split('.').forEach(function(Key){
Prop = Prop[Key];
});
if (Prop !== undefined) {
El[Toks[0]] = Prop;
var Toks = Att.split('=');
var Key = Toks[0].trim();
var Val = eval(`Data.${Toks[1]}`);
if (Val !== undefined) {
if (Key === 'src') {
Val = MkUrl(Val);
};
El[Key] = Val;
};
});
};
@ -171,10 +182,10 @@ function FillHome() {
var Rnd = RndHtmlId();
Window.querySelector('ul').innerHTML += `<li id="${Rnd}">
<a href="${Profile.Url}" onclick="DisplayProfile('${Profile.Url}'); return false;">
<img class="Profile Banner" data-assign="src:Banner" src="${Profile.Banner}"/>
<img class="Profile Banner" data-assign="src=Banner" src="${Profile.Banner}"/>
<div>
<img class="Profile Icon" data-assign="src:Icon" src="${Profile.Icon}"/>
<span data-assign="innerHTML:Name">${Profile.Url}</span>
<img class="Profile Icon" data-assign="src=Icon" src="${Profile.Icon}"/>
<span data-assign="innerHTML=Name">${Profile.Url}</span>
</div>
</a>
</li>`;

View File

@ -30,13 +30,15 @@ function NetCall(Data, Proc) {
// Req.open(Method, `${FriendicaUrl}/api/${Endpoint}.json`, true);
// Req.setRequestHeader('Authorization', `Basic ${btoa(FriendicaCredentials)}`);
//};
Req.open(Method, `${Data.Target}/${Endpoint}`, true);
Req.open(Method, MkUrl(`${Data.Target}/${Endpoint}`), true);
Req.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
_.forOwn(_.merge({"Content-Type": "application/json"}, Data.Headers), function(Val, Key) {
Req.setRequestHeader(Key, Val);
});
if (Method === 'POST' && !Data.Data) {
Data.Data = {};
};
Req.withCredentials = false;
Req.send(JSON.stringify(Data.Data));
};
@ -49,3 +51,29 @@ function IsHttpCodeGood(Code) {
return false;
};
};
function MkUrl(Url) {
if (!Url.toLowerCase().startsWith(HttpProxy)) {
Url = HttpProxy + Url;
};
return Url;
};
function UrlBase(Url) {
var Lower = Url.toLowerCase();
var Domain = UrlDomain(Url);
if (Lower.startsWith('https://')) {
return `https://${Domain}`;
} else {
return `http://${Domain}`;
};
};
function UrlDomain(Url) {
if (_.some(['//', 'http://', 'https://'], function(Sub){ return Url.toLowerCase().startsWith(Sub); })) {
return Url.split('//')[1].split('/')[0];
} else {
return Url.split('/')[0];
};
};

View File

@ -9,24 +9,6 @@ function RndHtmlId(Tag) {
return `Rnd-${Tag}-${RndId().replace('.', '-')}`;
};
function UrlBase(Url) {
var Lower = Url.toLowerCase();
var Domain = UrlDomain(Url);
if (Lower.startsWith('https://')) {
return `https://${Domain}`;
} else {
return `http://${Domain}`;
};
};
function UrlDomain(Url) {
if (_.some(['//', 'http://', 'https://'], function(Sub){ return Url.toLowerCase().startsWith(Sub); })) {
return Url.split('//')[1].split('/')[0];
} else {
return Url.split('/')[0];
};
};
function FuncName(Fun) {
if (Fun) {
Fun = Fun.toString();

16
HttpProxy.js Executable file
View File

@ -0,0 +1,16 @@
#!/usr/bin/env node
const Host = '0.0.0.0';
const Port = 44380;
const Blacklist = [];
const Whitelist = [];
//const cors_proxy = require('./lib/cors-anywhere');
/*cors_proxy*/
require('./node_modules/cors-anywhere/lib/cors-anywhere').createServer({
originBlacklist: Blacklist,
originWhitelist: Whitelist,
removeHeaders: ['cookie', 'cookie2'],
redirectSameOrigin: true,
}).listen(Port, Host, function() {
console.log(`Running proxy on ${Host}:${Port}`);
});

85
package-lock.json generated
View File

@ -10,7 +10,8 @@
"devDependencies": {
"@babel/cli": "^7.21.0",
"@babel/core": "^7.21.4",
"@babel/preset-env": "^7.21.4"
"@babel/preset-env": "^7.21.4",
"cors-anywhere": "^0.4.4"
}
},
"node_modules/@ampproject/remapping": {
@ -1939,6 +1940,19 @@
"url": "https://opencollective.com/core-js"
}
},
"node_modules/cors-anywhere": {
"version": "0.4.4",
"resolved": "https://registry.npmjs.org/cors-anywhere/-/cors-anywhere-0.4.4.tgz",
"integrity": "sha512-8OBFwnzMgR4mNrAeAyOLB2EruS2z7u02of2bOu7i9kKYlZG+niS7CTHLPgEXKWW2NAOJWRry9RRCaL9lJRjNqg==",
"dev": true,
"dependencies": {
"http-proxy": "1.11.1",
"proxy-from-env": "0.0.1"
},
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/debug": {
"version": "4.3.4",
"resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz",
@ -1989,6 +2003,12 @@
"node": ">=0.10.0"
}
},
"node_modules/eventemitter3": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/eventemitter3/-/eventemitter3-1.2.0.tgz",
"integrity": "sha512-DOFqA1MF46fmZl2xtzXR3MPCRsXqgoFqdXcrCVYM3JNnfUeHTm/fh/v/iU7gBFpwkuBmoJPAm5GuhdDfSEJMJA==",
"dev": true
},
"node_modules/fill-range": {
"version": "7.0.1",
"resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz",
@ -2106,6 +2126,19 @@
"node": ">=4"
}
},
"node_modules/http-proxy": {
"version": "1.11.1",
"resolved": "https://registry.npmjs.org/http-proxy/-/http-proxy-1.11.1.tgz",
"integrity": "sha512-qz7jZarkVG3G6GMq+4VRJPSN4NkIjL4VMTNhKGd8jc25BumeJjWWvnY3A7OkCGa8W1TTxbaK3dcE0ijFalITVA==",
"dev": true,
"dependencies": {
"eventemitter3": "1.x.x",
"requires-port": "0.x.x"
},
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/inflight": {
"version": "1.0.6",
"resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz",
@ -2333,6 +2366,12 @@
"node": ">=6"
}
},
"node_modules/proxy-from-env": {
"version": "0.0.1",
"resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-0.0.1.tgz",
"integrity": "sha512-B9Hnta3CATuMS0q6kt5hEezOPM+V3dgaRewkFtFoaRQYTVNsHqUvFXmndH06z3QO1ZdDnRELv5vfY6zAj/gG7A==",
"dev": true
},
"node_modules/readdirp": {
"version": "3.6.0",
"resolved": "https://registry.npmjs.org/readdirp/-/readdirp-3.6.0.tgz",
@ -2417,6 +2456,12 @@
"jsesc": "bin/jsesc"
}
},
"node_modules/requires-port": {
"version": "0.0.1",
"resolved": "https://registry.npmjs.org/requires-port/-/requires-port-0.0.1.tgz",
"integrity": "sha512-AzPDCliPoWDSvEVYRQmpzuPhGGEnPrQz9YiOEvn+UdB9ixBpw+4IOZWtwctmpzySLZTy7ynpn47V14H4yaowtA==",
"dev": true
},
"node_modules/resolve": {
"version": "1.22.2",
"resolved": "https://registry.npmjs.org/resolve/-/resolve-1.22.2.tgz",
@ -3930,6 +3975,16 @@
"browserslist": "^4.21.5"
}
},
"cors-anywhere": {
"version": "0.4.4",
"resolved": "https://registry.npmjs.org/cors-anywhere/-/cors-anywhere-0.4.4.tgz",
"integrity": "sha512-8OBFwnzMgR4mNrAeAyOLB2EruS2z7u02of2bOu7i9kKYlZG+niS7CTHLPgEXKWW2NAOJWRry9RRCaL9lJRjNqg==",
"dev": true,
"requires": {
"http-proxy": "1.11.1",
"proxy-from-env": "0.0.1"
}
},
"debug": {
"version": "4.3.4",
"resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz",
@ -3963,6 +4018,12 @@
"integrity": "sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g==",
"dev": true
},
"eventemitter3": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/eventemitter3/-/eventemitter3-1.2.0.tgz",
"integrity": "sha512-DOFqA1MF46fmZl2xtzXR3MPCRsXqgoFqdXcrCVYM3JNnfUeHTm/fh/v/iU7gBFpwkuBmoJPAm5GuhdDfSEJMJA==",
"dev": true
},
"fill-range": {
"version": "7.0.1",
"resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz",
@ -4049,6 +4110,16 @@
"integrity": "sha512-sKJf1+ceQBr4SMkvQnBDNDtf4TXpVhVGateu0t918bl30FnbE2m4vNLX+VWe/dpjlb+HugGYzW7uQXH98HPEYw==",
"dev": true
},
"http-proxy": {
"version": "1.11.1",
"resolved": "https://registry.npmjs.org/http-proxy/-/http-proxy-1.11.1.tgz",
"integrity": "sha512-qz7jZarkVG3G6GMq+4VRJPSN4NkIjL4VMTNhKGd8jc25BumeJjWWvnY3A7OkCGa8W1TTxbaK3dcE0ijFalITVA==",
"dev": true,
"requires": {
"eventemitter3": "1.x.x",
"requires-port": "0.x.x"
}
},
"inflight": {
"version": "1.0.6",
"resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz",
@ -4227,6 +4298,12 @@
"integrity": "sha512-uB80kBFb/tfd68bVleG9T5GGsGPjJrLAUpR5PZIrhBnIaRTQRjqdJSsIKkOP6OAIFbj7GOrcudc5pNjZ+geV2g==",
"dev": true
},
"proxy-from-env": {
"version": "0.0.1",
"resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-0.0.1.tgz",
"integrity": "sha512-B9Hnta3CATuMS0q6kt5hEezOPM+V3dgaRewkFtFoaRQYTVNsHqUvFXmndH06z3QO1ZdDnRELv5vfY6zAj/gG7A==",
"dev": true
},
"readdirp": {
"version": "3.6.0",
"resolved": "https://registry.npmjs.org/readdirp/-/readdirp-3.6.0.tgz",
@ -4298,6 +4375,12 @@
}
}
},
"requires-port": {
"version": "0.0.1",
"resolved": "https://registry.npmjs.org/requires-port/-/requires-port-0.0.1.tgz",
"integrity": "sha512-AzPDCliPoWDSvEVYRQmpzuPhGGEnPrQz9YiOEvn+UdB9ixBpw+4IOZWtwctmpzySLZTy7ynpn47V14H4yaowtA==",
"dev": true
},
"resolve": {
"version": "1.22.2",
"resolved": "https://registry.npmjs.org/resolve/-/resolve-1.22.2.tgz",

View File

@ -2,11 +2,13 @@
"name": "Friendiiverse",
"version": "Pre-Alpha Development",
"scripts": {
"build": "sh ./Build.sh"
"build": "sh ./Build.sh",
"http-proxy": "node ./HttpProxy.js"
},
"devDependencies": {
"@babel/cli": "^7.21.0",
"@babel/core": "^7.21.4",
"@babel/preset-env": "^7.21.4"
"@babel/preset-env": "^7.21.4",
"cors-anywhere": "^0.4.4"
}
}