UNPKG

@fxtop/proxy

Version:

a lightweight web proxy tool based on Nodejs

268 lines (267 loc) 10.5 kB
/// <reference types="node" /> import net from 'net'; import http from 'http'; import https from 'https'; declare class Proxy { /** * Status - 代理服务状态 */ status: string; /** * Options - 初始化配置信息 */ private options; /** * Lifecycle - 请求代理声明周期 */ private lifecycle; /** * httpProxy - HTTP代理服务实例 */ private httpProxy; /** * httpsProxy - HTTPS代理服务实例(解析密文) */ private httpsProxy; /** * Sockets - 收集代理服务过程中产生的套接字(主动断开链接) */ private httpSockets; private httpsSockets; /** * Creates an instance of proxy. * * @param {Proxy.Options} options - 代理初始化配置信息 * @param {Proxy.Lifecycle} lifecycle - 代理的生命周期钩子 */ constructor(options: Proxy.Options, lifecycle?: Proxy.Lifecycle); /** * Start Proxy - 启动代理服务 */ start(): void; /** * Gateway - 开启Http(s)代理网关 */ protected gateway(): void; /** * Handles httpRequest - Http请求代理处理 * * @param {http.IncomingMessage} req - 请求信息 * @param {http.ServerResponse} res - 响应信息 */ protected handleHttpRequest(req: http.IncomingMessage, res: http.ServerResponse): void; /** * Handles httpRequest - Https请求代理处理 * * @param {http.IncomingMessage} req - 请求信息 * @param {http.ServerResponse} res - 响应信息 */ protected handleHttpsRequest(req: http.IncomingMessage, res: http.ServerResponse): void; /** * Handles Tunnel connect - 为HTTPS请求建立tunnel隧道 * * @param {http.IncomingMessage} req - 请求头信息 * @param {net.Socket} client - socket信息 */ protected handleTunnelConnect(req: http.IncomingMessage, client: net.Socket): void; /** * Handles RedundantEvent - 处理Http(s)服务实例中非代理相关的事件 * * @param {http.Server | https.Server} server - 服务实例 * @param {'http' | 'https'} type - 服务实例类型 */ protected handleRedundantEvent(server: http.Server | https.Server, type: 'http' | 'https'): void; /** * Reset Proxy - 重置代理配置 * * @param {Proxy.Options} options - 更改代理配置信息 * @param {Proxy.Lifecycle} lifecycle - 代理生命周期钩子 */ reset(options: Proxy.Options, lifecycle?: Proxy.Lifecycle): void; /** * Close Proxy - 销毁代理服务实例 */ close(): void; static createHttpsProxy(ca?: Proxy.CA): https.Server; /** * Creates CA - 创建根证书 * * @param {InfoCA} info - 根证书信息 * @param {string} [info.organization='FXTOp'] - 组织 * @param {string} [info.countryCode='CN'] - 国家 * @param {string} [info.state='GuangDong'] - 省份 * @param {string} [info.locality='ShenZhen'] - 城市 * @param {number} [info.validityDays=365] - 有效期 * * @returns ca 根证书 */ static createCA: (info?: import("./helper/mkcert").InfoCA | undefined) => Promise<import("mkcert").Certificate>; /** * Gets Default CA - 获取默认根证书 * * @returns {CA} 默认根证书 */ static getDefaultCA: () => import("./helper/mkcert").CA; /** * Create Cert - 创建授权证书 * * @param {CA} ca - 根证书(授权) * @param {string} domain - 证书域名 * @param {number} [days=365] - 证书有效期 */ static createCert: (domain: string, ca?: import("./helper/mkcert").CA | undefined, validDays?: number) => Promise<import("mkcert").Certificate>; /** * Trust Root Cert - 导入根证书到系统中 * * @param {string} path - 存放根证书的路径 * * @return {boolean} - 是否导入成功 */ static trustRootCert: (path?: string | undefined) => Promise<import("./helper/sys").Result>; /** * Start System Proxy - 启动系统代理 * * @param {number} [port=8888] 代理服务监听端口 * * @return {Promise} 启动系统代理结果 * * @description 返回结果Promise中,error不为零,代表启动失败,可通过message查看失败原因。 */ static startSysProxy: (port?: number, isProxyHttps?: boolean) => Promise<import("./helper/sys").Result>; /** * Stop System Proxy - 关闭系统代理 * * @return {Promise} 关闭系统代理结果 * * @description 返回结果Promise中,error不为零,代表关闭失败,可通过message查看失败原因。 */ static stopSysProxy: () => Promise<import("./helper/sys").Result>; } export default Proxy; export { Proxy }; /** * Proxy - HTTP(S)代理 */ declare namespace Proxy { /** * Options - 代理初始化配置 * * @param {number} [httpPort=8888] - http代理服务监听端口 * @param {number} [httpsPort=8889] - https代理服务监听端口 * @param {CA} [ca] - CA签名证书和私钥,启动https服务代理必须设置该参数 * @param {boolean} [autoStart=false] - 是否在实例化后立即启动代理 */ interface Options { ca?: CA; httpPort?: number; httpsPort?: number; autoStart?: boolean; } /** * CA - 权威机构数字证书和私钥 * * @param {string} caCert - 签名证书 * @param {string} caKey - CA私钥 */ interface CA { caCert: string; caKey: string; } /** * Lifecycle - 代理服务声明周期 * * @param {Function} beforeStart - 代理服务启动前 * @param {Function} afterStart - 代理服务启动后 * @param {Function} beforeConnect - 代理为HTTPS通讯建立Tunnel前 * @param {Function} beforeRequest - 代理服务向目标服务发起请求前 * @param {Function} afterRequest - 代理服务向目标服务发起请求后(建立链接) * @param {Function} beforeResponse - 代理透传响应数据至源服务器前 * @param {Function} afterResponse - 代理透传响应数据至源服务器后 * @param {Function} beforeClose - 关闭代理服务前 * @param {Function} afterClose - 关闭代理服务后 */ interface Lifecycle { /** * Before Proxy Start - 代理服务启动前 * * @param {Proxy.Options} options - 代理服务初始化配置信息 * * @description 修改初始化配置信息,改变代理服务启动的行为,如监听端口等等 */ beforeStart?(options: Proxy.Options): void; /** * After Proxy Start - 代理服务启动后 */ afterStart?(): void; /** * Before Https Tunnel Connect - 建立通讯隧道前 * * @param {URL} urlObj - 请求URL信息 * * @return {boolean} isNeedProxy - 是否需要进行代理 */ beforeConnect?(urlObj: URL): boolean; /** * Before Proxy Request - 代理服务向目标服务发起请求前 * * @param {http.IncomingMessage} request - 来源服务的请求信息实例 * @param {http.ServerResponse} response - 来源服务的响应控制实例 * @param {http.RequestOptions} proxyOption - 代理请求配置信息 * @return {boolean} isBreakOff - 是否中断,自行处理后续流程 * * @description 通过更改proxyOption配置,改变请求的目标服务器、路径、方法等等 */ beforeRequest?(request: http.IncomingMessage, response: http.ServerResponse, proxyOption: http.RequestOptions): boolean; /** * After Proxy Request - 代理服务向目标服务发起请求后 * * @param {http.IncomingMessage} request - 来源服务的请求信息实例 * @param {http.ServerResponse} response - 来源服务的响应控制实例 * @param {http.IncomingMessage} proxyResponse - 代理服务的响应控制实例 * @return {boolean} isBreakOff - 是否中断,自行处理后续流程 * * @description 通过proxyResponse可获取目标服务发送的响应信息和传输数据,并通过pipe管道透传至来源服务端 */ afterRequest?(request: http.IncomingMessage, response: http.ServerResponse, proxyResponse: http.IncomingMessage): boolean; /** * Before Proxy Response - 代理服务透传响应数据给来源服务器前 * * @param {http.IncomingMessage} request - 来源服务的请求信息实例 * @param {http.ServerResponse} response - 来源服务的响应控制实例 * @param {http.IncomingMessage} proxyResponse - 代理服务的响应控制实例 * @return {boolean} isBreakOff - 是否中断,自行处理后续流程 * * @description proxyResponse中指定了chunks的加密方式,需要使用相应的解密方式解析chunks内容 */ beforeResponse?(req: http.IncomingMessage, res: http.ServerResponse, proxyResponse: http.IncomingMessage): boolean; /** * After Proxy Response - 代理服务透传响应数据给来源服务器后 * * @param {http.IncomingMessage} request - 来源服务的请求信息实例 * @param {http.ServerResponse} response - 来源服务的响应控制实例 * @param {ResponseBody} responseData - 目标服务传输过来的响应体数据 * @return {boolean} isBreakOff - 是否中断,自行处理后续流程 * * @description proxyResponse中指定了chunks的加密方式,需要使用相应的解密方式解析chunks内容 */ afterResponse?(request: http.IncomingMessage, response: http.ServerResponse, responseData: ResponseBody): boolean; /** * Before Proxy Close - 代理服务关闭前 */ beforeClose?(): void; /** * After Proxy Close - 代理服务关闭后 */ afterClose?(): void; } /** * Response Body - 响应数据内容 * * @member {Buffer} chunks - 压缩数据 * @member {string} encoding = 压缩方式 */ interface ResponseBody { chunks: Buffer; encoding: string; } }