Skip to content

Commit

Permalink
uos make puppet-wechat great again (#206)
Browse files Browse the repository at this point in the history
* make puppet-wechat great again

* fix

* fix smoke test

* 1.18.4
  • Loading branch information
leochen-g authored Jul 14, 2022
1 parent 78f2d4a commit 09fedb1
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Logs
.idea
logs
*.log
npm-debug.log*
Expand Down
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": "1.18.2",
"version": "1.18.4",
"description": "Puppet WeChat for Wechaty",
"type": "module",
"exports": {
Expand Down
25 changes: 9 additions & 16 deletions src/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,17 +237,20 @@ export class Bridge extends EventEmitter {
if (this.options.uos) {
await this.uosPatch(page)
}

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

const cookieList = (
await this.options.memory.get(MEMORY_SLOT)
) || [] as puppeteer.Protocol.Network.Cookie[]

const url = this.entryUrl(cookieList)
let url = this.entryUrl(cookieList)
if (this.options.uos) {
url = url + '?lang=zh_CN&target=t'
}
log.verbose('PuppetWeChatBridge', 'initPage() before page.goto(url)')

// set timeout 60000 ms,30000ms always timeout
page.setDefaultTimeout(60000)
// Does this related to(?) the CI Error: exception: Navigation Timeout Exceeded: 30000ms exceeded
await page.goto(url)
log.verbose('PuppetWeChatBridge', 'initPage() after page.goto(url)')
Expand All @@ -274,34 +277,24 @@ export class Bridge extends EventEmitter {
* Credit: @luvletter2333 https://github.com/luvletter2333
*/
const UOS_PATCH_CLIENT_VERSION = '2.0.0'
const UOS_PATCH_EXTSPAM = this.options.uosExtSpam ?? 'Gp8ICJkIEpkICggwMDAwMDAwMRAGGoAI1GiJSIpeO1RZTq9QBKsRbPJdi84ropi16EYI10WB6g74sGmRwSNXjPQ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kBKculWpdCMadx0usMomsSS/74QgpYqcPkmamB4nVv1JxczYITIqItIKjD35IGKAUwAA=='

const uosHeaders = {
'client-version' : UOS_PATCH_CLIENT_VERSION,
extspam : UOS_PATCH_EXTSPAM,
}
// add RequestInterception
await page.setRequestInterception(true)
page.on('request', req => {
page.on('request', (req) => {
const url = new URL(req.url())
if (url.pathname === '/' && url.search.indexOf('target=t') === -1) {
if (url.search === '' || url.search === '?') {
url.search = '?'
} else {
url.search += '&'
}
url.search += 'target=t'
this.wrapAsync(req.continue({ url: url.toString() }))

} else if (url.pathname === '/cgi-bin/mmwebwx-bin/webwxnewloginpage') {
if (url.pathname === '/cgi-bin/mmwebwx-bin/webwxnewloginpage') {
const override = {
headers: {
...req.headers(),
...uosHeaders,
},
}
this.wrapAsync(req.continue(override))

} else {
this.wrapAsync(req.continue())
}
Expand Down
10 changes: 4 additions & 6 deletions src/puppet-wechat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@ import {
} from 'watchdog'
import * as PUPPET from 'wechaty-puppet'
import { log } from 'wechaty-puppet'
import {
FileBox,
type FileBoxInterface,
} from 'file-box'
import type { FileBoxInterface } from 'file-box'
import { FileBox } from 'file-box'

import {
MEMORY_SLOT,
Expand Down Expand Up @@ -1484,9 +1482,9 @@ export class PuppetWeChat extends PUPPET.Puppet {
)

let mediaData: WebMessageMediaPayload
let rawPayload = {} as undefined | WebMessageRawPayload
let rawPayload = {} as WebMessageRawPayload

if (!rawPayload || !rawPayload.MediaId) {
if (!rawPayload.MediaId) {
try {
mediaData = await this.uploadMedia(file, conversationId)
rawPayload = Object.assign(rawPayload, mediaData)
Expand Down
3 changes: 2 additions & 1 deletion tests/puppeteer-room.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const ROOM_EXPECTED = {
}

test('Room smoke testing', async t => {
const MOCK_USER_ID = 'TEST-USER-ID'

// Mock
const mockContactRoomRawPayload = (id: string) => {
Expand All @@ -80,7 +81,7 @@ test('Room smoke testing', async t => {
sandbox.stub(puppet, 'roomRawPayload').callsFake(mockContactRoomRawPayload)

sandbox.stub(puppet, 'id').value('pretend-to-be-logined')

await puppet.login(MOCK_USER_ID)
const roomPayload = await puppet.roomPayload(ROOM_EXPECTED.id)

t.equal(roomPayload.id, ROOM_EXPECTED.id, 'should set id/UserName right')
Expand Down

0 comments on commit 09fedb1

Please sign in to comment.