Skip to content

Commit

Permalink
feat: add option to change front page
Browse files Browse the repository at this point in the history
  • Loading branch information
nuzulul committed Feb 3, 2024
1 parent 85d51b2 commit cf23f81
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 37 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,16 @@ npm install opencors
import {OpenCORS} from 'opencors'

const server = new OpenCORS({
//port:4000
//port:4000,
//front: '<h1>opencors</h1>'
})
```
```javascript
const {OpenCORS} = require('opencors')

const server = new OpenCORS({
//port:4000
//port:4000,
//front: '<h1>opencors</h1>'
})
```
Request examples:
Expand Down
24 changes: 13 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "opencors",
"version": "1.0.4",
"version": "1.0.5",
"description": "OpenCORS is a simple NodeJS based CORS Proxy",
"main": "dist/cjs/opencors.js",
"module": "dist/mjs/opencors.js",
Expand All @@ -11,16 +11,18 @@
}
},
"scripts": {
"start": "tsc && node public/server.js",
"prepare-build-win-1": "if not exist .\\dist (mkdir dist) else (rmdir /S /Q .\\dist\\)",
"prepare-build-win-2": "if not exist .\\public (mkdir public) else (rmdir /S /Q .\\public\\)",
"build-win": "npm run prepare-build-win-1 && npm run prepare-build-win-2 && tsc && tsc -p tsconfig-mjs.json && tsc -p tsconfig-cjs.json && echo {\"type\": \"commonjs\"}>dist\\cjs\\package.json && echo {\"type\": \"module\"}>dist\\mjs\\package.json",
"dev":"nodemon -e js,ts --watch src --watch server.ts --exec \"npm start\"",
"clean-win":"del /S /Q .\\public && del /S /Q .\\dist && rmdir /S /Q .\\node_modules && rmdir /S /Q .\\test\\cjs\\node_modules && rmdir /S /Q .\\test\\mjs\\node_modules",
"test-win":"npm link && cd .\\test\\cjs && npm install && npm link opencors && cd .\\..\\mjs && npm install && npm link opencors",
"test-cjs":"cd .\\test\\cjs && npm start",
"test-mjs":"cd .\\test\\mjs && npm start",
"prepare-win":"npm install && npm run build-win && npm run dev"
"start": "npm run dev-node",

"removedir": "node -e \"var fs = require('fs'); try{process.argv.slice(1).map((fpath) => fs.rmdirSync(fpath, { recursive: true }))}catch(err){console.log(`Dist not found`)}; process.exit(0);\"",
"build-all": "tsc -p tsconfig.json && tsc -p tsconfig-mjs.json && tsc -p tsconfig-cjs.json && echo {\"type\": \"commonjs\"}>dist\\cjs\\package.json && echo {\"type\": \"module\"}>dist\\mjs\\package.json",
"build": "npm run removedir dist && npm run removedir public && npm run build-all",

"build-dev":"npm run removedir public && tsc -p tsconfig.json",
"dev-node":"nodemon -e js,ts --watch src --watch server.ts --exec \"npm run build-dev && node public/server.js\"",
"dev":"npm run dev-node",

"test-win":"npm link && cd .\\test\\cjs && npm install && npm link opencors && cd .\\..\\mjs && npm install && npm link opencors",
"test": "npm run dev-node"
},
"files": [
"dist/",
Expand Down
3 changes: 2 additions & 1 deletion public/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
Object.defineProperty(exports, "__esModule", { value: true });
const opencors_1 = require("./src/opencors");
const server = new opencors_1.OpenCORS({
//port:4000
//port:4000,
//front: '<h1>opencors</h1>'
});
3 changes: 2 additions & 1 deletion public/src/opencors.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export declare class OpenCORS {
constructor({ port }: {
constructor({ port, front }: {
port?: number;
front?: string;
});
}
25 changes: 16 additions & 9 deletions public/src/opencors.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ exports.OpenCORS = void 0;
const http_1 = __importDefault(require("http"));
const https_1 = __importDefault(require("https"));
class OpenCORS {
constructor({ port }) {
constructor({ port, front }) {
const serverport = port || process.env.PORT || 8080;
const server = http_1.default.createServer((req, res) => __awaiter(this, void 0, void 0, function* () {
var _a;
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.setHeader('Access-Control-Allow-Origin', '*');
function isvalidurl(url) {
try {
return !!(new URL(url));
Expand Down Expand Up @@ -54,11 +57,15 @@ class OpenCORS {
});
});
}
let body = (_a = 'OpenCors\n' +
'OpenCORS is a simple NodeJS based CORS Proxy\n' +
'https://github.com/nuzulul/opencors\n\n' +
'Usage :\n' + req.headers.host + '/?url=\n\n' +
'Agent:\n' + req.headers['user-agent']) !== null && _a !== void 0 ? _a : "Unknown";
let body = 'OpenCors<br>' +
'OpenCORS is a simple NodeJS based CORS Proxy<br>' +
'https://github.com/nuzulul/opencors<br><br>' +
'Usage :<br>' + req.headers.host + '/?url=<br><br>' +
'Agent:<br>' + ((_a = req.headers['user-agent']) !== null && _a !== void 0 ? _a : "Unknown") + '<br><br>' +
'Demo:<br>' +
'Input Url <input type="text" id="input"/><button id="button" onclick="location.href=\'/?url=\'+document.getElementById(\'input\').value">Submit</button>';
if (front != undefined)
body = front;
let requrl = req.url;
requrl = requrl.startsWith('/') ? 'http://' + req.headers.host + req.url : req.url;
const targeturl = (new URL(requrl)).searchParams.get("url");
Expand All @@ -69,9 +76,9 @@ class OpenCORS {
else if (targeturl != null) {
body = JSON.stringify({ status: 'error', msg: 'Invalid target url', targeturl });
}
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.setHeader('Access-Control-Allow-Origin', '*');
else {
res.setHeader('Content-Type', 'text/html');
}
res.end(body);
}));
server.listen(serverport, () => {
Expand Down
3 changes: 2 additions & 1 deletion server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {OpenCORS} from './src/opencors'

const server = new OpenCORS({
//port:4000
//port:4000,
//front: '<h1>opencors</h1>'
})

30 changes: 18 additions & 12 deletions src/opencors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@ import https from 'https'

export class OpenCORS{

public constructor({port}:{port?:number}){
public constructor({port,front}:{port?:number,front?:string}){

const serverport:string|number = port || process.env.PORT || 8080

const server = http.createServer(async(req,res)=>{

res.statusCode = 200

res.setHeader('Content-Type','text/plain')

res.setHeader('Access-Control-Allow-Origin','*')

function isvalidurl(url:string){

try{
Expand Down Expand Up @@ -65,11 +71,15 @@ export class OpenCORS{
})
}

let body = 'OpenCors\n'+
'OpenCORS is a simple NodeJS based CORS Proxy\n'+
'https://github.com/nuzulul/opencors\n\n'+
'Usage :\n'+req.headers.host+'/?url=\n\n'+
'Agent:\n'+req.headers['user-agent'] ?? "Unknown"
let body = 'OpenCors<br>'+
'OpenCORS is a simple NodeJS based CORS Proxy<br>'+
'https://github.com/nuzulul/opencors<br><br>'+
'Usage :<br>'+req.headers.host+'/?url=<br><br>'+
'Agent:<br>'+(req.headers['user-agent'] ?? "Unknown")+'<br><br>'+
'Demo:<br>'+
'Input Url <input type="text" id="input"/><button id="button" onclick="location.href=\'/?url=\'+document.getElementById(\'input\').value">Submit</button>'

if(front != undefined) body = front

let requrl = req.url as string

Expand All @@ -87,13 +97,9 @@ export class OpenCORS{

body = JSON.stringify({status:'error',msg:'Invalid target url',targeturl})

}else{
res.setHeader('Content-Type','text/html')
}

res.statusCode = 200

res.setHeader('Content-Type','text/plain')

res.setHeader('Access-Control-Allow-Origin','*')

res.end(body)
})
Expand Down

0 comments on commit cf23f81

Please sign in to comment.