init work

This commit is contained in:
zizifn
2022-12-05 05:02:47 +08:00
committed by zizifn
parent 5475daa162
commit 26db660035
17 changed files with 376 additions and 133 deletions

View File

@@ -0,0 +1,58 @@
import { Command } from 'commander';
import { writeFileSync, existsSync, readFileSync } from 'fs';
import { exit } from 'node:process';
let config: {
port: string;
address: string;
uuid: string;
config: string;
} = null;
const program = new Command();
program.option('-v').action((options) => {
console.log(options);
exit();
});
program
.command('run')
.description('launch local http proxy for edge pass')
.option(
'--config <config>',
'address of remote proxy, etc https://***.deno.dev/'
)
.option(
'--address <address>',
'address of remote proxy, etc https://***.deno.dev/'
)
.option('--port <port>', 'local port of http proxy proxy')
.option('--uuid <uuid>', 'uuid')
.option('--save', 'if this is pass, will save to config.json')
.action((options) => {
console.log(__dirname);
console.log(process.cwd());
if (options.config) {
if (existsSync(options.config)) {
const content = readFileSync(options.config, {
encoding: 'utf-8',
});
config = JSON.parse(content);
return;
} else {
console.error('config not exsit!');
exit();
}
}
config = options;
if (config.address && config.port && config.uuid) {
if (options.save) {
writeFileSync('./config.json', JSON.stringify(options), {
encoding: 'utf-8',
});
}
} else {
console.error('need pass all args!');
exit();
}
});
program.parse();
export { config };

View File

@@ -0,0 +1,46 @@
import { IncomingMessage } from 'http';
import * as os from 'os';
import * as url from 'node:url';
async function* concatStreams(readables: any[]) {
for (const readable of readables) {
for await (const chunk of readable) {
yield chunk;
}
}
}
// POST http://zizi.press/test11.ttt?test1=66 HTTP/1.1
// Host: zizi.press
// User-Agent: curl/7.83.1
// Connection: Keep-Alive
// Content-Type: application/json
// Accept: application/json
// Content-Length: 16
// {"tool": "curl"}
//-------------------------------------
// GET http://zizi.press/test11.ttt?test1=66 HTTP/1.1
// Host: zizi.press
// User-Agent: curl/7.83.1
// Accept: */*
// Connection: Keep-Alive
function rawHTTPHeader(req: IncomingMessage) {
const reqUrl = url.parse(req.url);
const headers = Object.entries(req.headers)
.map(([key, value]) => {
return `${key}:${value}`;
})
.join(os.EOL);
const raw = `${req.method} ${reqUrl.path} HTTP/${req.httpVersion}${os.EOL}${headers}${os.EOL}${os.EOL}`;
return raw;
}
function rawHTTPPackage(req: IncomingMessage) {
const rawHttpHeader = rawHTTPHeader(req);
return concatStreams([[rawHttpHeader], req]);
}
export { concatStreams, rawHTTPPackage, rawHTTPHeader };