Skip to content

Commit

Permalink
wip: x-scripts-rules 改为使用独立仓库维护
Browse files Browse the repository at this point in the history
  • Loading branch information
renxia committed Jan 23, 2024
1 parent a846727 commit 2e220b6
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 277 deletions.
Empty file modified .husky/commit-msg
100644 → 100755
Empty file.
Empty file modified .husky/pre-commit
100644 → 100755
Empty file.
25 changes: 13 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

常见的流行代理工具软件如 WireShark、fiddler、charles、whistle、burpsuite、mitmproxy 等本身自带的能力已相当强大,但是在实现较为复杂的自定义逻辑目的时,要么配置规则相当复杂,拥有较高的规则编写学习成本,要么需要开发对应的插件实现。

本插件基于代理工具 `whistle` 的插件开发能力,提供了一套简易的脚本编写规范。基于该规范,对于拥有简单的 JavaScript 开发能力的同学来说,只需针对常用网站或软件以自由编码的形式编写少量的代码规则脚本,即可实现绝大多数场景需求。如实现自动保存登录认证 cookie、拦截、模拟、修改、和保存接口数据等功能,从而可以以较低的成本实现:认证信息同步、广告过滤、数据修改、数据缓存、文件替换调试等目的。
本插件基于代理工具 `whistle` 的插件开发能力,提供了一套简易的脚本编写规范。基于该规范,对于拥有简单的 JavaScript 开发能力的同学来说,只需针对常用网站或软件以自由编码的形式编写少量的代码规则,即可实现绝大多数场景需求。如实现自动保存登录认证 cookie、拦截、模拟、修改、和保存接口数据等功能,从而可以以较低的成本实现:认证信息同步、广告过滤、数据修改、数据缓存、文件替换调试等目的。

**功能特性:**

Expand Down Expand Up @@ -49,7 +49,7 @@ w2 install @lzwme/whistle.x-scripts

首先,在当前工作目录下新建配置文件 `w2.x-scripts.config.js`。内容参考:[w2.x-scripts.config.sample.js](./w2.x-scripts.config.sample.js)

然后在配置文件中的 `rules``ruleDirs` 配置项中,添加配置自定义的规则脚本
然后在配置文件中的 `rules``ruleDirs` 配置项中,添加配置自定义的规则

最后启动 `whistle`

Expand Down Expand Up @@ -116,11 +116,11 @@ w2 start -M capture

各配置项及详细说明请参考类型定义[W2CookiesConfig]('./index.d.ts')

## 规则脚本的编写
## 脚本规则的编写

你可以在配置文件 `w2.x-scripts.config.js` 中的 `rules` 字段中直接编写规则,也可以在其他目录下新建 `.js` 文件并编写具体的规则脚本,然后在 `ruleDirs` 字段中引入。
你可以在配置文件 `w2.x-scripts.config.js` 中的 `rules` 字段中直接编写规则,也可以在其他目录下新建 `.js` 文件并编写具体的脚本规则,然后在 `ruleDirs` 字段中引入。

一个极其简单的规则脚本示例
一个极其简单的脚本规则示例

```js
/** @type {import('@lzwme/whistle.x-scripts').RuleItem} */
Expand Down Expand Up @@ -153,7 +153,7 @@ module.exports = rule;

具体编写方法请参考配置示例和类型定义文件:

- [x-scripts-rules](./x-scripts-rules) 提供了一些内置的规则脚本,可作为规则脚本的编写参考示例
- [x-scripts-rules](https://github.com/lzwme/x-scripts-rules) 提供了一些示例脚本,可作为脚本规则的编写参考示例
- [w2.x-scripts.config.sample.js](./w2.x-scripts.config.sample.js)
- [index.d.ts](./index.d.ts)

Expand All @@ -165,15 +165,16 @@ module.exports = rule;

### 脚本规则示例

内置脚本规则
可直接下载使用的脚本规则

- [x-scripts-rules](./x-scripts-rules/) 目录下包含了内置的规则脚本可做参考。
- [jd.js](./x-scripts-rules/jd.js) **自动保存某东 cookie** 至本地环境变量文件,并上传至青龙面板
- [imaotai.js](./x-scripts-rules/imaotai.js) **自动保存i茅台 token** 并上传至青龙面板
- [https://github.com/lzwme/x-scripts-rules](https://github.com/lzwme/x-scripts-rules)
- [jd.js](https://github.com/lzwme/x-scripts-rules/blob/main/src/jd.js) **自动保存某东 cookie** 至本地环境变量文件,并上传至青龙面板
- [imaotai.js](https://github.com/lzwme/x-scripts-rules/blob/main/src/imaotai.js) **自动保存i茅台 token** 并上传至青龙面板
- 更多实用示例脚本持续添加中...

模板示例脚本规则:

- [example/rules](./example/rules/) 目录下包含了常用场景及示例规则脚本,可作为模板示例参考使用。
- [example/rules](./example/rules/) 目录下包含了常用场景及示例脚本,可作为模板示例参考使用。
- [**打印**所有接口请求与返回信息](./example/rules/print-res-body.js)
- [**保存**在访问bing首页时加载的每日背景图至本地](./example/rules/bing-save-images.js)
- [**修改**百度首页网盘入口为自定义链接地址](./example/rules/modify-baidu-index.js)
Expand Down Expand Up @@ -216,7 +217,7 @@ w2 run

- 本插件项目仅用于个人对 web 程序逆向的兴趣研究学习,请勿用于商业用途、任何恶意目的,否则后果自负。
- 由于插件引入自定义脚本会真实的在服务器上执行,使用第三方编写的脚本时请谨慎甄别安全风险,请尽可能的在虚拟化容器内使用。
- 请自行评估使用本插件及基于本插件规范开发的第三方规则脚本的安全风险。本人对使用本插件或插件涉及的任何脚本引发的问题概不负责,包括但不限于由脚本错误引起的任何损失或损害。
- 请自行评估使用本插件及基于本插件规范开发的第三方脚本的安全风险。本人对使用本插件或插件涉及的任何脚本引发的问题概不负责,包括但不限于由脚本错误引起的任何损失或损害。

## License

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"lint": "flh --prettier --tscheck",
"build": "npm run clean && tsc -p tsconfig.cjs.json",
"version": "standard-version",
"release": "npm run version && git push --follow-tags origin main",
"release": "npm run test && npm run build",
"clean": "flh rm -f dist",
"test": "npm run lint"
},
Expand Down
4 changes: 3 additions & 1 deletion src/lib/QingLong.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ export class QingLoing {
}
/** 查询任务列表 */
async getTaskList(searchValue = '') {
const { data } = await this.req.get<{ code: number; data: { data: QLTaskItem[] } }>(`${this.config.host}/api/crons?searchValue=${searchValue}&t=${Date.now()}`);
const { data } = await this.req.get<{ code: number; data: { data: QLTaskItem[] } }>(
`${this.config.host}/api/crons?searchValue=${searchValue}&t=${Date.now()}`
);
return data.data?.data || [];
}
/** 禁用任务 */
Expand Down
15 changes: 12 additions & 3 deletions src/lib/getConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
* @Description:
*/
import type { W2CookiesConfig } from '../../typings';
import { existsSync, statSync } from 'node:fs';
import { resolve } from 'node:path';
import { existsSync, readdirSync, statSync } from 'node:fs';
import { basename, resolve } from 'node:path';
import { homedir } from 'node:os';
import { assign, color } from '@lzwme/fe-utils';
import { logger } from './helper';
Expand Down Expand Up @@ -50,10 +50,19 @@ export function getConfig(useCache = true) {

if (!config.logType) config.logType = config.debug ? 'debug' : 'info';
logger.updateOptions({ levelType: config.logType, debug: config.debug });
if (!isLoadUserConfig) logger.info('请创建配置文件', color.yellow(defaultConfigFile));
if (!isLoadUserConfig) {
logger.info(
`请创建配置文件: ${color.yellow(defaultConfigFile)}`,
`\n参考: ${color.cyan(resolve(__dirname, '../../w2.x-scripts.config.sample.js'))}`
);
}

config.throttleTime = Math.max(1, +config.throttleTime || 10);

if (!Array.isArray(config.ruleDirs)) config.ruleDirs = [config.ruleDirs as never as string];
if (basename(process.cwd()).includes('x-scripts-rules')) config.ruleDirs.push(process.cwd());
readdirSync(process.cwd()).forEach(d => d.includes('x-scripts-rule') && config.ruleDirs!.push(d));

const allRules = rulesManage.loadRules(config.ruleDirs, !isLoaded);
config.rules.forEach(d => {
if (Array.isArray(d)) d.forEach(d => allRules.add(d));
Expand Down
14 changes: 4 additions & 10 deletions src/lib/rulesManage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function classifyRules(rules: RuleItem[], isInit = false) {
}

RulesCache[rule.on]!.set(rule.ruleId, rule);
logger.log(`Load Rule:[${rule.on}][${cyan(rule.ruleId)}]`, green(rule.desc));
logger.info(`Load Rule:[${rule.on}][${cyan(rule.ruleId)}]`, green(rule.desc));
});

const s = [...Object.entries(RulesCache)].filter(d => d[0] !== 'all').map(d => `${cyan(d[0])}: ${green(d[1].size)}`);
Expand All @@ -71,21 +71,15 @@ function loadRules(filepaths: string[] = [], isInit = false) {

if (statSync(filepath).isDirectory()) {
readdirSync(filepath)
.filter(d => /(rules)|(\.c?js)/.test(d))
.filter(d => /rules|src|(\.c?js)$/.test(d))
.forEach(d => findRuleFiles(resolve(filepath, d)));
} else if (/\.c?js/.test(filepath)) {
filesSet.add(resolve(process.cwd(), filepath));
}
};

// 合入当前目录下名称包含 `x-scripts-rule` 的文件或目录
new Set(
readdirSync(process.cwd())
.filter(d => d.includes('x-scripts-rule'))
.concat(filepaths)
.filter(Boolean)
.map(d => resolve(d))
).forEach(d => findRuleFiles(d));
new Set(filepaths.filter(Boolean).map(d => resolve(d))).forEach(d => findRuleFiles(d));

const rules = new Set<RuleItem>();
for (let filepath of filesSet) {
Expand All @@ -104,7 +98,7 @@ function loadRules(filepaths: string[] = [], isInit = false) {
rules.add(rule);
}
} catch (e) {
logger.log('尝试从文件加载规则失败:', filepath, e.message);
logger.debug('尝试从文件加载规则失败:', filepath, e.message);
}
}

Expand Down
43 changes: 0 additions & 43 deletions x-scripts-rules/QLScriptPublic.js

This file was deleted.

35 changes: 0 additions & 35 deletions x-scripts-rules/huluwa.js

This file was deleted.

43 changes: 0 additions & 43 deletions x-scripts-rules/imaotai.js

This file was deleted.

11 changes: 0 additions & 11 deletions x-scripts-rules/index.js

This file was deleted.

Loading

0 comments on commit 2e220b6

Please sign in to comment.