Skip to content

Commit

Permalink
chore: sdenv-extend更新到1.2、样例use-proxy文档
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenxianluo committed Mar 12, 2024
1 parent a1d2fa2 commit 7f424da
Show file tree
Hide file tree
Showing 17 changed files with 18,482 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ yarn.lock
*.swp
build/
*.node
*/**/*-7802
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ const { jsdomFromText, jsdomFromUrl } = require('sdenv/utils/jsdom');

sdenv-extend初始化只执行一次,初始化成功后生成的环境对象可以使用`Object.sdenv()`(vm中使用非node)获取。

sdenv-extend具体功能可参考项目内README文档
sdenv-extend具体功能可参考项目内[README文档](https://github.com/pysunday/sdenv-extend/blob/main/README.md)

## 声明

Expand Down
54 changes: 53 additions & 1 deletion example/use-proxy/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,53 @@
该example通过代理请求网站生成cookie,作者用于开发使用,用户无需关注
## 样例说明

为了模拟浏览器与sdenv运行相同瑞数vmp代码生成一摸一样的cookie的复现方法与源文件。

## 启动代理

该样例需要使用代理工具,推荐代理工具:[sd_proxy](https://github.com/pysunday/tools-proxy),该工具为自研工具,支持网站资源的收集与回放

启动代理回放:`sd_proxy -H 0.0.0.0 -p 3000 -n playback -d pysunday-proxy --log`

## 应用代理

浏览器端需要使用SwitchyOmega配置并应用3000端口转发。

sdenv需要使用jsdomFromUrl方法传入代理配置`proxy: "http://127.0.0.1:3000"`

## 获取cookie

该example对应的访问链接为:`https://wcjs.sbj.cnipa.gov.cn/sgtmi`

假设代理配置完全ok,则请求该链接时会返回html内容为文件[pysunday-proxy/wcjs.sbj.cnipa.gov.cn/sgtmi/format](./pysunday-proxy/wcjs.sbj.cnipa.gov.cn/sgtmi/format),可以看到html中存在代码:

```javascript
.getHandle('cookie')({
log: false,
setCb: (val) => {
if (val.includes('goN9uW4i0iKzT')) {
cookies.push(val);
if (cookies.length === 3) {
console.log(`第三次cookie值写入:${val}`);
debugger;
}
}
}
})
```

其中第三次cookie的值即为最终发起请求的cookie值。

* node端: node中直接执行`node-9229 example/use-proxy/index.js`,命令操作符中会打印cookie值后退出
* 浏览器端: 浏览器端需要打开开发面板后再请求网址`https://wcjs.sbj.cnipa.gov.cn/sgtmi`,在断点处即可看到cookie值

## 结论

作者电脑系统版本:macOS(13.4)

作者浏览器版本:Chrome Canary(121.0.6124.0)

作者node和浏览器分别运行后断点处打印的值都为`goN9uW4i0iKzT=0o6uz9peZ4RlIIiCpo6uz9F9f4zZghSX8vcCtuqn0xNaRncrIKsE8SR4aRoGLBGiDlCiw69spB51I78BjEbiyeIPgiMdhjjQvrJQ.7iwYLKrWMPMi0xRa.xN64KvZGbXlPfFz0OaiHM8o.IvfnXEaf0RuNFu5VKDSSevbBeiVFAqzWPD_gZzXwDmJnf6bpaet1yyg2lmCOWCzUre8W52LiD2U95ZxkI2jrAzmfpcdX.V; path=/; expires=Tue, 12 Mar 2024 09:42:17 GMT`

如果您的系统及浏览器与作者一致,那么您可以直接运行该样例复现。

当然,您也可以参考`browser/chrome`目录下的文件内容,新建一个browser并将您浏览器的值填如后参考前面方法进行试验,如果试验成功请提交您browser代码的pr,并写明您操作系统版本及浏览器版本,感谢!
9 changes: 6 additions & 3 deletions example/use-proxy/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ const browser = require('../../browser/');
const { jsdomFromUrl } = require('../../utils/jsdom');

const [jsdomer, cookieJar] = jsdomFromUrl({
proxy: "http://127.0.0.1:7759",
proxy: "http://127.0.0.1:3000",
})

const baseUrl = "https://wcjs.sbj.cnipa.gov.cn"

async function loadPages() {
const dom = await jsdomer(`${baseUrl}/first`);
const dom = await jsdomer(`${baseUrl}/sgtmi`);
browser(dom.window, 'chrome');
dom.window.onbeforeunload = async (url) => {
const cookies = cookieJar.getCookieStringSync(baseUrl);
logger.debug('cookieJar:', cookies);
if (cookies) {
logger.debug('cookieJar:', cookies);
}
process.exit();
}
}
loadPages()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
{
"url": "wcjs.sbj.cnipa.gov.cn/c5rxzYrjRT2h/cCdzB9ZjDFks.294cc83.js",
"url_full": "https://wcjs.sbj.cnipa.gov.cn/c5rxzYrjRT2h/cCdzB9ZjDFks.294cc83.js",
"params": {
"header_method": "GET"
},
"request": {
"http_version": "HTTP/1.1",
"headers": {
"fields": [
[
"Host",
"wcjs.sbj.cnipa.gov.cn"
],
[
"Connection",
"keep-alive"
],
[
"sec-ch-ua",
"\"Not A(Brand\";v=\"99\", \"Google Chrome\";v=\"121\", \"Chromium\";v=\"121\""
],
[
"sec-ch-ua-mobile",
"?0"
],
[
"User-Agent",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36"
],
[
"sec-ch-ua-platform",
"\"macOS\""
],
[
"Accept",
"*/*"
],
[
"Sec-Fetch-Site",
"same-origin"
],
[
"Sec-Fetch-Mode",
"no-cors"
],
[
"Sec-Fetch-Dest",
"script"
],
[
"Referer",
"https://wcjs.sbj.cnipa.gov.cn/sgtmi"
],
[
"Accept-Encoding",
"gzip, deflate, br, zstd"
],
[
"Accept-Language",
"zh-CN,zh;q=0.9"
],
[
"Cookie",
"goN9uW4i0iKzS=60kT8dOgmxXebqEWQEwYEO4TitVQH8CXD6HwieKtCzgFT2IN.iUVh7khPK0REAwDxhcNBIph3t8pLFf5JvsCPl4q; wcjs_cookie=27005561"
]
]
},
"content": "",
"trailers": null,
"timestamp_start": 1704176297.577336,
"timestamp_end": 1704176297.5777438,
"host": "wcjs.sbj.cnipa.gov.cn",
"port": 443,
"method": "GET",
"scheme": "https",
"authority": "",
"path": "/c5rxzYrjRT2h/cCdzB9ZjDFks.294cc83.js"
},
"response": {
"http_version": "HTTP/1.1",
"headers": {
"fields": [
[
"Date",
"Tue, 02 Jan 2024 06:18:17 GMT"
],
[
"Content-Type",
"text/javascript; charset=UTF-8"
],
[
"Content-Length",
"204872"
],
[
"Connection",
"keep-alive"
],
[
"Last-Modified",
"Fri, 31 Mar 2023 02:45:25 GMT"
],
[
"ETag",
"\"64264945-32048\""
],
[
"Cache-Control",
"max-age=2592000, public"
],
[
"Expires",
"Thu, 01 Feb 2024 06:09:07 GMT"
],
[
"Server",
"NSF"
]
]
},
"trailers": null,
"timestamp_start": 1704176297.717346,
"timestamp_end": 1704176297.858795,
"status_code": 200,
"reason": "OK"
}
}

Large diffs are not rendered by default.

Loading

0 comments on commit 7f424da

Please sign in to comment.