Initiate repository
This commit is contained in:
parent
f2d4e523a3
commit
c5233b7088
|
@ -3,5 +3,13 @@ root = true
|
|||
[*]
|
||||
end_of_line = lf
|
||||
insert_final_newline = true
|
||||
indent_size = 2
|
||||
trim_trailing_whitespace = true
|
||||
charset = utf-8
|
||||
|
||||
[*.js]
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
|
||||
[{package.json,*.yml,*.cjson}]
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
dist
|
||||
.output
|
||||
node-modules
|
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"extends": [
|
||||
"@nuxtjs/eslint-config-typescript"
|
||||
]
|
||||
}
|
|
@ -1,18 +0,0 @@
|
|||
{
|
||||
"env": {
|
||||
"worker": true,
|
||||
"node": true,
|
||||
"es6": true
|
||||
},
|
||||
"parserOptions": {
|
||||
"ecmaVersion": "latest"
|
||||
},
|
||||
"root": true,
|
||||
"extends": ["eslint:recommended", "plugin:prettier/recommended"],
|
||||
"plugins": [],
|
||||
"ignorePatterns": ["dist"],
|
||||
"rules": {
|
||||
"prettier/prettier": "error",
|
||||
"no-undef": "off"
|
||||
}
|
||||
}
|
|
@ -1,60 +0,0 @@
|
|||
name: Build Release
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
|
||||
jobs:
|
||||
build:
|
||||
name: Build
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v2
|
||||
|
||||
- name: Install Node.js
|
||||
uses: actions/setup-node@v1
|
||||
with:
|
||||
node-version: 16
|
||||
|
||||
- name: Install NPM packages
|
||||
run: npm install
|
||||
|
||||
- name: Build project
|
||||
run: npm run build
|
||||
|
||||
- name: Upload production-ready build files
|
||||
uses: actions/upload-artifact@v3
|
||||
with:
|
||||
name: worker.js
|
||||
path: ./dist/worker.js
|
||||
|
||||
- name: Bump version and push tag
|
||||
id: tag_version
|
||||
uses: mathieudutour/github-tag-action@v6.1
|
||||
with:
|
||||
github_token: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
- name: Create Release
|
||||
id: create_release
|
||||
uses: actions/create-release@v1
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
with:
|
||||
tag_name: ${{ steps.tag_version.outputs.new_tag }}
|
||||
release_name: Simple Proxy Worker
|
||||
draft: false
|
||||
prerelease: false
|
||||
|
||||
- name: Upload Release Asset
|
||||
id: upload-release-asset
|
||||
uses: actions/upload-release-asset@v1
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
with:
|
||||
upload_url: ${{ steps.create_release.outputs.upload_url }}
|
||||
asset_path: ./dist/worker.js
|
||||
asset_name: worker.js
|
||||
asset_content_type: text/javascript
|
|
@ -1,42 +0,0 @@
|
|||
name: Linting and Testing
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
- dev
|
||||
pull_request_target:
|
||||
types: [opened, reopened, synchronize]
|
||||
|
||||
jobs:
|
||||
linting:
|
||||
name: Run Linters
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v2
|
||||
|
||||
- name: Install Node.js
|
||||
uses: actions/setup-node@v1
|
||||
with:
|
||||
node-version: 16
|
||||
|
||||
- name: Install NPM packages
|
||||
run: npm install
|
||||
|
||||
- name: Run ESLint Report
|
||||
run: npm run lint:report
|
||||
# continue on error, so it still reports it in the next step
|
||||
continue-on-error: true
|
||||
|
||||
- name: Annotate Code Linting Results
|
||||
uses: ataylorme/eslint-annotate-action@v2
|
||||
with:
|
||||
repo-token: "${{ secrets.GITHUB_TOKEN }}"
|
||||
report-json: "eslint_report.json"
|
||||
|
||||
- name: Build Project
|
||||
run: npm run build
|
||||
|
||||
|
|
@ -1,2 +1,7 @@
|
|||
node_modules
|
||||
*.log*
|
||||
.nitro
|
||||
.cache
|
||||
.output
|
||||
.env
|
||||
dist
|
|
@ -1,4 +0,0 @@
|
|||
module.exports = {
|
||||
trailingComma: 'all',
|
||||
singleQuote: true,
|
||||
};
|
|
@ -1,6 +0,0 @@
|
|||
{
|
||||
"recommendations": [
|
||||
"dbaeumer.vscode-eslint",
|
||||
"editorconfig.editorconfig"
|
||||
]
|
||||
}
|
|
@ -1,5 +0,0 @@
|
|||
{
|
||||
"editor.formatOnSave": true,
|
||||
"editor.defaultFormatter": "dbaeumer.vscode-eslint",
|
||||
"eslint.format.enable": true
|
||||
}
|
43
README.md
43
README.md
|
@ -1,3 +1,42 @@
|
|||
# Cloudflare worker proxy
|
||||
# Nitro Minimal Starter
|
||||
|
||||
Simple http proxy in a cloudflare worker.
|
||||
Look at the [Nitro documentation](https://nitro.unjs.io/) to learn more.
|
||||
|
||||
## Setup
|
||||
|
||||
Make sure to install the dependencies:
|
||||
|
||||
```bash
|
||||
# npm
|
||||
npm install
|
||||
|
||||
# yarn
|
||||
yarn install
|
||||
|
||||
# pnpm
|
||||
pnpm install
|
||||
```
|
||||
|
||||
## Development Server
|
||||
|
||||
Start the development server on <http://localhost:3000>
|
||||
|
||||
```bash
|
||||
npm run dev
|
||||
```
|
||||
|
||||
## Production
|
||||
|
||||
Build the application for production:
|
||||
|
||||
```bash
|
||||
npm run build
|
||||
```
|
||||
|
||||
Locally preview production build:
|
||||
|
||||
```bash
|
||||
npm run preview
|
||||
```
|
||||
|
||||
Check out the [deployment documentation](https://nitro.unjs.io/deploy) for more information.
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
//https://nitro.unjs.io/config
|
||||
export default defineNitroConfig({});
|
File diff suppressed because it is too large
Load Diff
18
package.json
18
package.json
|
@ -1,18 +1,12 @@
|
|||
{
|
||||
"name": "simple-proxy",
|
||||
"private": true,
|
||||
"version": "1.0.0",
|
||||
"scripts": {
|
||||
"build": "vite build",
|
||||
"lint": "eslint --ext .js src/",
|
||||
"lint:fix": "eslint --fix --ext .js src/",
|
||||
"lint:report": "eslint --ext .js --output-file eslint_report.json --format json src/"
|
||||
"prepare": "nitropack prepare",
|
||||
"dev": "nitropack dev",
|
||||
"build": "nitropack build",
|
||||
"preview": "node .output/server/index.mjs"
|
||||
},
|
||||
"devDependencies": {
|
||||
"eslint": "^8.30.0",
|
||||
"eslint-config-prettier": "^8.5.0",
|
||||
"eslint-plugin-prettier": "^4.2.1",
|
||||
"vite": "^4.0.0",
|
||||
"vite-plugin-eslint": "^1.8.1"
|
||||
"dependencies": {
|
||||
"nitropack": "latest"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
export default eventHandler(() => {
|
||||
return { nitro: 'Is Awesome!' }
|
||||
})
|
173
src/main.js
173
src/main.js
|
@ -1,173 +0,0 @@
|
|||
const corsHeaders = {
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
'Access-Control-Allow-Methods': 'GET,HEAD,POST,OPTIONS',
|
||||
'Access-Control-Max-Age': '86400',
|
||||
};
|
||||
|
||||
async function handleRequest(oRequest, destination, iteration = 0) {
|
||||
console.log(
|
||||
`PROXYING ${destination}${iteration ? ' ON ITERATION ' + iteration : ''}`,
|
||||
);
|
||||
|
||||
// Create a new mutable request object for the destination
|
||||
const request = new Request(destination, oRequest);
|
||||
request.headers.set('Origin', new URL(destination).origin);
|
||||
|
||||
// TODO - Make cookie handling better. PHPSESSID overwrites all other cookie related headers
|
||||
|
||||
// Add custom X headers from client
|
||||
// These headers are usually forbidden to be set by fetch
|
||||
if (oRequest.headers.has('X-Cookie')) {
|
||||
request.headers.set('Cookie', oRequest.headers.get('X-Cookie'));
|
||||
request.headers.delete('X-Cookie');
|
||||
}
|
||||
|
||||
if (request.headers.has('X-Referer')) {
|
||||
request.headers.set('Referer', request.headers.get('X-Referer'));
|
||||
request.headers.delete('X-Referer');
|
||||
}
|
||||
|
||||
if (request.headers.has('X-Origin')) {
|
||||
request.headers.set('Origin', request.headers.get('X-Origin'));
|
||||
request.headers.delete('X-Origin');
|
||||
}
|
||||
|
||||
// Set PHPSESSID cookie
|
||||
if (request.headers.get('PHPSESSID')) {
|
||||
request.headers.set(
|
||||
'Cookie',
|
||||
`PHPSESSID=${request.headers.get('PHPSESSID')}`,
|
||||
);
|
||||
}
|
||||
|
||||
// Set User Agent, if not exists
|
||||
const useragent = request.headers.get('User-Agent');
|
||||
if (!useragent) {
|
||||
request.headers.set(
|
||||
'User-Agent',
|
||||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0',
|
||||
);
|
||||
}
|
||||
|
||||
// Fetch the new resource
|
||||
const oResponse = await fetch(request.clone());
|
||||
|
||||
// If the server returned a redirect, follow it
|
||||
if (
|
||||
(oResponse.status === 302 || oResponse.status === 301) &&
|
||||
oResponse.headers.get('location')
|
||||
) {
|
||||
// Server tried to redirect too many times
|
||||
if (iteration > 5) {
|
||||
return new Response('418 Too many redirects', {
|
||||
status: 418,
|
||||
});
|
||||
}
|
||||
|
||||
// Handle and return the request for the redirected destination
|
||||
return await handleRequest(
|
||||
request,
|
||||
oResponse.headers.get('location'),
|
||||
iteration + 1,
|
||||
);
|
||||
}
|
||||
|
||||
// Create mutable response using the original response as init
|
||||
const response = new Response(oResponse.body, oResponse);
|
||||
|
||||
// Set CORS headers
|
||||
response.headers.set('Access-Control-Allow-Origin', '*');
|
||||
response.headers.set('Access-Control-Expose-Headers', '*');
|
||||
|
||||
const cookiesToSet = response.headers.get('Set-Cookie');
|
||||
|
||||
// Transfer Set-Cookie to X-Set-Cookie
|
||||
// Normally the Set-Cookie header is not accessible to fetch clients
|
||||
if (cookiesToSet) {
|
||||
response.headers.set('X-Set-Cookie', response.headers.get('Set-Cookie'));
|
||||
}
|
||||
|
||||
// Set PHPSESSID cookie
|
||||
if (
|
||||
cookiesToSet &&
|
||||
cookiesToSet.includes('PHPSESSID') &&
|
||||
cookiesToSet.includes(';')
|
||||
) {
|
||||
let phpsessid = cookiesToSet.slice(cookiesToSet.search('PHPSESSID') + 10);
|
||||
phpsessid = phpsessid.slice(0, phpsessid.search(';'));
|
||||
|
||||
response.headers.set('PHPSESSID', phpsessid);
|
||||
}
|
||||
|
||||
// Append to/Add Vary header so browser will cache response correctly
|
||||
response.headers.append('Vary', 'Origin');
|
||||
|
||||
// Add X-Final-Destination header to get the final url
|
||||
response.headers.set('X-Final-Destination', oResponse.url);
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
function handleOptions(request) {
|
||||
// Make sure the necessary headers are present
|
||||
// for this to be a valid pre-flight request
|
||||
const headers = request.headers;
|
||||
let response = new Response(null, {
|
||||
headers: {
|
||||
Allow: 'GET, HEAD, POST, OPTIONS',
|
||||
},
|
||||
});
|
||||
|
||||
if (
|
||||
headers.get('Origin') !== null &&
|
||||
headers.get('Access-Control-Request-Method') !== null &&
|
||||
headers.get('Access-Control-Request-Headers') !== null
|
||||
) {
|
||||
response = new Response(null, {
|
||||
headers: {
|
||||
...corsHeaders,
|
||||
// Allow all future content Request headers to go back to browser
|
||||
// such as Authorization (Bearer) or X-Client-Name-Version
|
||||
'Access-Control-Allow-Headers': request.headers.get(
|
||||
'Access-Control-Request-Headers',
|
||||
),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
addEventListener('fetch', (event) => {
|
||||
const request = event.request;
|
||||
const url = new URL(request.url);
|
||||
const destination = url.searchParams.get('destination');
|
||||
|
||||
console.log(`HTTP ${request.method} - ${request.url}`);
|
||||
|
||||
let response = new Response('404 Not Found', {
|
||||
status: 404,
|
||||
});
|
||||
|
||||
if (request.method === 'OPTIONS') {
|
||||
// Handle CORS preflight requests
|
||||
response = handleOptions(request);
|
||||
} else if (!destination) {
|
||||
response = new Response('200 OK', {
|
||||
status: 200,
|
||||
headers: {
|
||||
Allow: 'GET, HEAD, POST, OPTIONS',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
},
|
||||
});
|
||||
} else if (
|
||||
request.method === 'GET' ||
|
||||
request.method === 'HEAD' ||
|
||||
request.method === 'POST'
|
||||
) {
|
||||
// Handle request
|
||||
response = handleRequest(request, destination);
|
||||
}
|
||||
|
||||
event.respondWith(response);
|
||||
});
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"extends": "./.nitro/types/tsconfig.json"
|
||||
}
|
|
@ -1,16 +0,0 @@
|
|||
const path = require('path');
|
||||
const { defineConfig } = require('vite');
|
||||
const { default: eslint } = require('vite-plugin-eslint');
|
||||
|
||||
module.exports = defineConfig({
|
||||
plugins: [eslint()],
|
||||
build: {
|
||||
minify: false,
|
||||
lib: {
|
||||
entry: path.resolve(__dirname, 'src/main.js'),
|
||||
name: 'worker',
|
||||
formats: ['es'],
|
||||
fileName: () => `worker.js`,
|
||||
},
|
||||
},
|
||||
});
|
Loading…
Reference in New Issue