[refactor] proxy2
This commit is contained in:
@@ -44,6 +44,8 @@ async function fetchProxy(request) {
|
|||||||
newResponseHeaders.delete('content-security-policy');
|
newResponseHeaders.delete('content-security-policy');
|
||||||
newResponseHeaders.delete('content-security-policy-report-only');
|
newResponseHeaders.delete('content-security-policy-report-only');
|
||||||
newResponseHeaders.delete('clear-site-data');
|
newResponseHeaders.delete('clear-site-data');
|
||||||
|
newResponseHeaders.delete('Cache-Control');
|
||||||
|
|
||||||
|
|
||||||
const originalBody = originalResponse.body
|
const originalBody = originalResponse.body
|
||||||
|
|
||||||
|
|||||||
58
functions/proxy2.js
Normal file
58
functions/proxy2.js
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
export function onRequest(context) {
|
||||||
|
const request = context.request
|
||||||
|
return fetchProxy(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
const blocked_region = []
|
||||||
|
|
||||||
|
async function fetchProxy(request) {
|
||||||
|
|
||||||
|
const region = (request.headers.get('cf-ipcountry') ?? '').toUpperCase();
|
||||||
|
|
||||||
|
let response = null;
|
||||||
|
let rurl = new URL(request.url);
|
||||||
|
|
||||||
|
const urlParam = rurl.searchParams.get('url')
|
||||||
|
|
||||||
|
if(!urlParam){
|
||||||
|
return new Response('Access denied', {
|
||||||
|
status: 403
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (blocked_region.includes(region)) {
|
||||||
|
response = new Response('Access denied', {
|
||||||
|
status: 403
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
let method = request.method;
|
||||||
|
const header = JSON.parse(decodeURIComponent(request.headers.get('risu-header') ?? 'null')) ?? request.headers
|
||||||
|
let requestHeaders = new Headers(header);
|
||||||
|
|
||||||
|
let originalResponse = await fetch(urlParam, {
|
||||||
|
method: method,
|
||||||
|
headers: requestHeaders,
|
||||||
|
body: request.body
|
||||||
|
})
|
||||||
|
|
||||||
|
const responseHeaders = originalResponse.headers;
|
||||||
|
const status = originalResponse.status;
|
||||||
|
let newResponseHeaders = new Headers(responseHeaders);
|
||||||
|
|
||||||
|
newResponseHeaders.set('access-control-allow-origin', 'https://risuai.xyz/');
|
||||||
|
newResponseHeaders.set('access-control-allow-credentials', "true");
|
||||||
|
newResponseHeaders.delete('content-security-policy');
|
||||||
|
newResponseHeaders.delete('content-security-policy-report-only');
|
||||||
|
newResponseHeaders.delete('clear-site-data');
|
||||||
|
newResponseHeaders.delete('Cache-Control');
|
||||||
|
|
||||||
|
|
||||||
|
const originalBody = originalResponse.body
|
||||||
|
|
||||||
|
response = new Response(originalBody, {
|
||||||
|
status,
|
||||||
|
headers: newResponseHeaders
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return response;
|
||||||
|
}
|
||||||
@@ -40,7 +40,7 @@ app.get('/', async (req, res, next) => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
app.post('/proxy', async (req, res, next) => {
|
const proxyFunc = async (req, res, next) => {
|
||||||
const urlParam = req.query.url;
|
const urlParam = req.query.url;
|
||||||
|
|
||||||
if (!urlParam) {
|
if (!urlParam) {
|
||||||
@@ -67,12 +67,20 @@ app.post('/proxy', async (req, res, next) => {
|
|||||||
const status = originalResponse.status;
|
const status = originalResponse.status;
|
||||||
|
|
||||||
const originalBody = await originalResponse.text();
|
const originalBody = await originalResponse.text();
|
||||||
|
const head = originalResponse.headers
|
||||||
|
head.delete('content-security-policy');
|
||||||
|
head.delete('content-security-policy-report-only');
|
||||||
|
head.delete('clear-site-data');
|
||||||
|
head.delete('Cache-Control');
|
||||||
if(status < 200 || status >= 300){
|
if(status < 200 || status >= 300){
|
||||||
res.status(status)
|
res.status(status)
|
||||||
}
|
}
|
||||||
|
res.header(head)
|
||||||
res.send(originalBody);
|
res.send(originalBody);
|
||||||
});
|
}
|
||||||
|
|
||||||
|
app.post('/proxy', proxyFunc);
|
||||||
|
app.post('/proxy2', proxyFunc);
|
||||||
|
|
||||||
app.get('/api/password', async(req, res)=> {
|
app.get('/api/password', async(req, res)=> {
|
||||||
if(password === ''){
|
if(password === ''){
|
||||||
|
|||||||
@@ -1,5 +0,0 @@
|
|||||||
{
|
|
||||||
"dependencies": {
|
|
||||||
"cors-anywhere": "^0.4.4"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
68
server/proxy/pnpm-lock.yaml
generated
68
server/proxy/pnpm-lock.yaml
generated
@@ -1,68 +0,0 @@
|
|||||||
lockfileVersion: 5.4
|
|
||||||
|
|
||||||
specifiers:
|
|
||||||
cors-anywhere: ^0.4.4
|
|
||||||
http-proxy: ^1.18.1
|
|
||||||
|
|
||||||
dependencies:
|
|
||||||
cors-anywhere: 0.4.4
|
|
||||||
http-proxy: 1.18.1
|
|
||||||
|
|
||||||
packages:
|
|
||||||
|
|
||||||
/cors-anywhere/0.4.4:
|
|
||||||
resolution: {integrity: sha512-8OBFwnzMgR4mNrAeAyOLB2EruS2z7u02of2bOu7i9kKYlZG+niS7CTHLPgEXKWW2NAOJWRry9RRCaL9lJRjNqg==}
|
|
||||||
engines: {node: '>=0.10.0'}
|
|
||||||
dependencies:
|
|
||||||
http-proxy: 1.11.1
|
|
||||||
proxy-from-env: 0.0.1
|
|
||||||
dev: false
|
|
||||||
|
|
||||||
/eventemitter3/1.2.0:
|
|
||||||
resolution: {integrity: sha512-DOFqA1MF46fmZl2xtzXR3MPCRsXqgoFqdXcrCVYM3JNnfUeHTm/fh/v/iU7gBFpwkuBmoJPAm5GuhdDfSEJMJA==}
|
|
||||||
dev: false
|
|
||||||
|
|
||||||
/eventemitter3/4.0.7:
|
|
||||||
resolution: {integrity: sha512-8guHBZCwKnFhYdHr2ysuRWErTwhoN2X8XELRlrRwpmfeY2jjuUN4taQMsULKUVo1K4DvZl+0pgfyoysHxvmvEw==}
|
|
||||||
dev: false
|
|
||||||
|
|
||||||
/follow-redirects/1.15.2:
|
|
||||||
resolution: {integrity: sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==}
|
|
||||||
engines: {node: '>=4.0'}
|
|
||||||
peerDependencies:
|
|
||||||
debug: '*'
|
|
||||||
peerDependenciesMeta:
|
|
||||||
debug:
|
|
||||||
optional: true
|
|
||||||
dev: false
|
|
||||||
|
|
||||||
/http-proxy/1.11.1:
|
|
||||||
resolution: {integrity: sha512-qz7jZarkVG3G6GMq+4VRJPSN4NkIjL4VMTNhKGd8jc25BumeJjWWvnY3A7OkCGa8W1TTxbaK3dcE0ijFalITVA==}
|
|
||||||
engines: {node: '>=0.10.0'}
|
|
||||||
dependencies:
|
|
||||||
eventemitter3: 1.2.0
|
|
||||||
requires-port: 0.0.1
|
|
||||||
dev: false
|
|
||||||
|
|
||||||
/http-proxy/1.18.1:
|
|
||||||
resolution: {integrity: sha512-7mz/721AbnJwIVbnaSv1Cz3Am0ZLT/UBwkC92VlxhXv/k/BBQfM2fXElQNC27BVGr0uwUpplYPQM9LnaBMR5NQ==}
|
|
||||||
engines: {node: '>=8.0.0'}
|
|
||||||
dependencies:
|
|
||||||
eventemitter3: 4.0.7
|
|
||||||
follow-redirects: 1.15.2
|
|
||||||
requires-port: 1.0.0
|
|
||||||
transitivePeerDependencies:
|
|
||||||
- debug
|
|
||||||
dev: false
|
|
||||||
|
|
||||||
/proxy-from-env/0.0.1:
|
|
||||||
resolution: {integrity: sha512-B9Hnta3CATuMS0q6kt5hEezOPM+V3dgaRewkFtFoaRQYTVNsHqUvFXmndH06z3QO1ZdDnRELv5vfY6zAj/gG7A==}
|
|
||||||
dev: false
|
|
||||||
|
|
||||||
/requires-port/0.0.1:
|
|
||||||
resolution: {integrity: sha512-AzPDCliPoWDSvEVYRQmpzuPhGGEnPrQz9YiOEvn+UdB9ixBpw+4IOZWtwctmpzySLZTy7ynpn47V14H4yaowtA==}
|
|
||||||
dev: false
|
|
||||||
|
|
||||||
/requires-port/1.0.0:
|
|
||||||
resolution: {integrity: sha512-KigOCHcocU3XODJxsu8i/j8T9tzT4adHiecwORRQ0ZZFcp7ahwXuRU1m+yuO90C5ZUyGeGfocHDI14M3L3yDAQ==}
|
|
||||||
dev: false
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
const cors_proxy = require('cors-anywhere');
|
|
||||||
|
|
||||||
const host = process.env.HOST || 'localhost';
|
|
||||||
const port = process.env.PORT || 8080;
|
|
||||||
|
|
||||||
cors_proxy.createServer({originWhitelist: []}).listen(port, host, function() {console.log('Running Proxy on ' + host + ':' + port);});
|
|
||||||
@@ -181,7 +181,7 @@ export async function requestChatDataMain(arg:requestDataArgument, model:'model'
|
|||||||
if(db.useStreaming && arg.useStreaming){
|
if(db.useStreaming && arg.useStreaming){
|
||||||
body.stream = true
|
body.stream = true
|
||||||
const da = ((!isTauri) && (!isNodeServer))
|
const da = ((!isTauri) && (!isNodeServer))
|
||||||
? await fetch(`/proxy?url=${encodeURIComponent(replacerURL)}`, {
|
? await fetch(`/proxy2?url=${encodeURIComponent(replacerURL)}`, {
|
||||||
body: JSON.stringify(body),
|
body: JSON.stringify(body),
|
||||||
headers: {
|
headers: {
|
||||||
"risu-header": encodeURIComponent(JSON.stringify({
|
"risu-header": encodeURIComponent(JSON.stringify({
|
||||||
|
|||||||
@@ -617,7 +617,7 @@ export async function globalFetch(url:string, arg:{plainFetchForce?:boolean,body
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(arg.rawResponse){
|
if(arg.rawResponse){
|
||||||
const furl = `/proxy?url=${encodeURIComponent(url)}`
|
const furl = `/proxy2?url=${encodeURIComponent(url)}`
|
||||||
|
|
||||||
const da = await fetch(furl, {
|
const da = await fetch(furl, {
|
||||||
body: body,
|
body: body,
|
||||||
@@ -637,7 +637,7 @@ export async function globalFetch(url:string, arg:{plainFetchForce?:boolean,body
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
const furl = hubURL + `/proxy?url=${encodeURIComponent(url)}`
|
const furl = `/proxy2?url=${encodeURIComponent(url)}`
|
||||||
|
|
||||||
const da = await fetch(furl, {
|
const da = await fetch(furl, {
|
||||||
body: body,
|
body: body,
|
||||||
|
|||||||
Reference in New Issue
Block a user