avwo / whistle

HTTP, HTTP2, HTTPS, Websocket debugging proxy
https://wproxy.org/
MIT License
13.99k stars 1.08k forks source link

规则求教: 正则匹配之后的自动路径拼接 #1071

Closed Fredida closed 3 weeks ago

Fredida commented 2 months ago

有n个待匹配的url例如: https://domain/path-aaa/ran02/path-bbb/file-d https://domain/path-aaa/ran67/path-ccc/file-e 其中ran后面为定长2位的随机数

本地目录X:\mock-domain\path-aaa\randxx分别有子目录path-bbb和path-ccc存放了file-d和file-e 写了一条正则rule匹配这类url让他们分别访问对应子目录里的文件: /https://domain/path-aaa/ran\d\d/path-bbb/file-d/ file://X:\mock-domain\path-aaa\randxx

但实际测试对于任何匹配上的url都会只自动访问到X:\mock-domain\path-aaa\randxx,请问有简洁的正则办法让程序在randxx之后根据url自动拼接访问吗?

另外不知道whistlek可不可以debug rule(规则)的解析与执行的过程,这样可以自行调试发现自己写的rule在哪里系统解析结果和预想的不一样,就能少提issue来麻烦作者了。

avwo commented 1 month ago

有两种写法:

# 正则
/https://domain/path-aaa/ran(\d\d)/path-bbb/file-/ file://X:\mock-domain\path-aaa\rand$1

# 通配符
^https://domain/path-aaa/ran*/path-bbb/file-* file://X:\mock-domain\path-aaa\rand$1

参考:https://wproxy.org/whistle/pattern.html

Fredida commented 1 month ago

感谢作者的帮助,抱歉之前表述的有歧义,其实是想把 /https://domain/path-aaa/ran\d\d/path-bbb/file-1 /https://domain/path-aaa/ran\d\d/path-ccc/file-2 都匹配到 file://X:\固定dir\下 (X:\固定dir下有path-bbb\file-1和path-ccc\file-2等) 请问该如何撰写规则呢?

提问之前有阅读过帮助文档pattern.html,请问这段描述

最多支持10个子匹配 $0...9,其中$0表示整个请求url,其它跟正则的子匹配一样 /[^?#]\/([^\/]+).html/ protocol://...$1...

应该理解为$1为正则第一次匹配之后剩余的后续url部分?那么$2(对应的子匹配)是什么呢?

avwo commented 1 month ago

跟正则的子匹配一样,试试:

^https://domain/path-aaa/ran*/** file://X:\固定dir\$2