-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix : fix constructor params & improve documentation
- Loading branch information
Showing
12 changed files
with
150 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -132,5 +132,5 @@ dist | |
#hook | ||
test/cjs/node_modules/ | ||
test/mjs/node_modules/ | ||
public/ | ||
#public/ | ||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const opencors_1 = require("./src/opencors"); | ||
const server = new opencors_1.OpenCORS({ | ||
//port:4000 | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export declare class OpenCORS { | ||
constructor({ port }: { | ||
port?: number; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.OpenCORS = void 0; | ||
const http_1 = __importDefault(require("http")); | ||
const https_1 = __importDefault(require("https")); | ||
class OpenCORS { | ||
constructor({ port }) { | ||
const serverport = port || process.env.PORT || 8080; | ||
const server = http_1.default.createServer((req, res) => __awaiter(this, void 0, void 0, function* () { | ||
var _a; | ||
function isvalidurl(url) { | ||
try { | ||
return !!(new URL(url)); | ||
} | ||
catch (e) { | ||
return false; | ||
} | ||
} | ||
function get(targeturl, resolve) { | ||
https_1.default.get(targeturl, res => { | ||
if ((res.statusCode === 301 || res.statusCode === 302) && (res.headers.location != undefined)) { | ||
return get(res.headers.location, resolve); | ||
} | ||
let data = []; | ||
res.on('data', chunk => { data.push(chunk); }); | ||
res.on('end', () => { | ||
try { | ||
const raw = Buffer.concat(data).toString(); | ||
resolve(raw); | ||
} | ||
catch (err) { | ||
let msg = JSON.stringify({ status: 'error', msg: err, targeturl }); | ||
resolve(msg); | ||
} | ||
}); | ||
}); | ||
} | ||
function get_page(targeturl) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return new Promise((resolve) => { | ||
get(targeturl, resolve); | ||
}); | ||
}); | ||
} | ||
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 requrl = req.url; | ||
requrl = requrl.startsWith('/') ? 'http://' + req.headers.host + req.url : req.url; | ||
const targeturl = (new URL(requrl)).searchParams.get("url"); | ||
if (isvalidurl(targeturl)) { | ||
const rawdata = yield get_page(targeturl); | ||
body = rawdata; | ||
} | ||
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', '*'); | ||
res.end(body); | ||
})); | ||
server.listen(serverport, () => { | ||
console.log(`Server running at ${serverport}`); | ||
}); | ||
} | ||
} | ||
exports.OpenCORS = OpenCORS; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import {OpenCORS} from './src/opencors' | ||
|
||
const server = new OpenCORS({ | ||
//port:4000 | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"version": 2, | ||
"builds": [ | ||
{ | ||
"src": "public/server.js", | ||
"use": "@vercel/node", | ||
"config": { "includeFiles": ["public/**"] } | ||
} | ||
], | ||
"routes": [ | ||
{ | ||
"src": "/(.*)", | ||
"dest": "public/server.js", | ||
"methods": ["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"], | ||
"headers": { | ||
"Access-Control-Allow-Credentials": "true", | ||
"Access-Control-Allow-Origin": "*", | ||
"Access-Control-Allow-Methods": "GET,OPTIONS,PATCH,DELETE,POST,PUT", | ||
"Access-Control-Allow-Headers": "X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version" | ||
} | ||
} | ||
] | ||
} |