Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加uos请求头支持 #129

Merged
merged 9 commits into from
Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wechaty-puppet-wechat",
"version": "0.27.0",
"version": "0.27.1",
"description": "Puppet WeChat for Wechaty",
"main": "dist/src/mod.js",
"typings": "dist/src/mod.d.ts",
Expand Down
60 changes: 27 additions & 33 deletions src/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,6 @@ export class Bridge extends EventEmitter {
launchOptions.executablePath = this.options.endpoint
}

const extensionOptionsArgs = [
...this.getWeChromeExtensionOptionsArgs(),
]

const options = {
...launchOptions,
args: [
Expand All @@ -149,7 +145,6 @@ export class Bridge extends EventEmitter {
'--mute-audio',
'--no-sandbox',
...launchOptionsArgs,
...extensionOptionsArgs,
],
headless,
}
Expand Down Expand Up @@ -230,6 +225,12 @@ export class Bridge extends EventEmitter {
// might be called before initPage() return.
const page = this.page = await browser.newPage()

/**
* Can we support UOS with puppeteer? #127
* https://github.com/wechaty/wechaty-puppet-wechat/issues/127
*/
await this.uosPatch(page)

page.on('error', e => this.emit('error', e))
page.on('dialog', this.onDialog.bind(this))

Expand Down Expand Up @@ -268,36 +269,26 @@ export class Bridge extends EventEmitter {
const UOS_PATCH_CLIENT_VERSION = '2.0.0'
const UOS_PATCH_EXTSPAM = 'Gp8ICJkIEpkICggwMDAwMDAwMRAGGoAI1GiJSIpeO1RZTq9QBKsRbPJdi84ropi16EYI10WB6g74sGmRwSNXjPQnYUKYotKkvLGpshucCaeWZMOylnc6o2AgD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Fj6qQvQ1a/I+MkTx5UO+yNHl718JWcR3AXGmv/aa9rD1eNP8ioTGlOZwPgmr2sor2iBpKTOrB83QgZXP+xRYkb4zVC+LoAXEoIa1+zArywlgREer7DLePukkU6wHTkuSaF+ge5Of1bXuU4i938WJHj0t3D8uQxkJvoFi/EYN/7u2P1zGRLV4dHVUsZMGCCtnO6BBigFMAA='

const extraHeaders = {
const uosHeaders = {
'client-version' : UOS_PATCH_CLIENT_VERSION,
extspam : UOS_PATCH_EXTSPAM,
referer : 'https://wx.qq.com/?&lang=zh_CN&target=t',
}
await page.setExtraHTTPHeaders(extraHeaders)
}

private getWeChromeExtensionOptionsArgs (): string[] {
/**
* Issue #127: Can we support UOS with puppeteer?
* https://github.com/wechaty/wechaty-puppet-wechat/issues/127
* https://github.com/adamyi/wechrome - Chrome extension to unblock web wechat
* https://stackoverflow.com/a/51434261/1123955
*/
const weChromeExt = path.join(
__dirname,
'..',
'wechrome'
)
if (!fs.existsSync(weChromeExt)) {
throw new Error('WeChrome extension not found.')
}

const extensionOptionsArgs = [
`--load-extension=${weChromeExt}`,
`--disable-extensions-except=${weChromeExt}`,
]

return extensionOptionsArgs
// add RequestInterception
await page.setRequestInterception(true)
page.on('request', async req => {
const url = new URL(req.url())
if (url.pathname === '/cgi-bin/mmwebwx-bin/webwxnewloginpage') {
const override = {
headers: {
...req.headers(),
...uosHeaders,
},
}
await req.continue(override)
return
}
await req.continue()
})
}

public async readyAngular (page: Page): Promise<void> {
Expand Down Expand Up @@ -1050,7 +1041,10 @@ export class Bridge extends EventEmitter {
public entryUrl (cookieList?: Protocol.Network.Cookie[]): string {
log.verbose('PuppetWeChatBridge', 'cookieDomain(%s)', cookieList)

const DEFAULT_URL = 'https://wx.qq.com'
/**
* `?target=t` is from https://github.com/wechaty/wechaty-puppet-wechat/pull/129
*/
const DEFAULT_URL = 'https://wx.qq.com?target=t'

if (!cookieList || cookieList.length === 0) {
log.silly('PuppetWeChatBridge', 'cookieDomain() no cookie, return default %s', DEFAULT_URL)
Expand Down