Server.php
8.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<?php
/**
* Created by PhpStorm.
* User: Hanson
* Date: 2016/12/9
* Time: 21:10
*/
namespace Hanson\Vbot\Core;
use Endroid\QrCode\QrCode;
use Hanson\Vbot\Collections\ContactFactory;
use Hanson\Vbot\Collections\Group;
use Hanson\Vbot\Collections\Special;
use Hanson\Vbot\Support\Console;
use Hanson\Vbot\Support\FileManager;
use Hanson\Vbot\Support\System;
class Server
{
static $instance;
protected $uuid;
protected $redirectUri;
public $skey;
public $sid;
public $uin;
public $passTicket;
public $deviceId;
public $baseRequest;
public $syncKey;
public $syncKeyStr;
public $config;
public $messageHandler;
protected $debug = false;
public $baseUri = 'https://wx2.qq.com/cgi-bin/mmwebwx-bin';
public $fileUri;
public $pushUri;
public $domain = 'wx2.qq.com';
public function __construct($config = [])
{
$this->config = $config;
$this->config['debug'] = isset($this->config['debug']) ? $this->config['debug'] : false;
}
/**
* @param array $config
* @return Server
*/
public static function getInstance($config = [])
{
if(!static::$instance){
static::$instance = new Server($config);
}
return static::$instance;
}
/**
* start a wechat trip
*/
public function run()
{
$this->prepare();
$this->init();
Console::log('初始化成功');
$this->statusNotify();
Console::log('开始初始化联系人');
$this->initContact();
Console::log(sprintf("初始化联系人成功\n群数量: %d\n联系人数量: %d\n公众号数量: %d\n特殊号数量: %d", group()->count(), contact()->count(), official()->count(), Special::getInstance()->count()));
MessageHandler::getInstance()->listen();
}
public function prepare()
{
$this->getUuid();
$this->generateQrCode();
Console::showQrCode('https://login.weixin.qq.com/l/' . $this->uuid);
Console::log('请扫描二维码登录');
$this->waitForLogin();
$this->login();
Console::log('登录成功');
}
/**
* get uuid
*
* @throws \Exception
*/
protected function getUuid()
{
$content = http()->get('https://login.weixin.qq.com/jslogin', [
'appid' => 'wx782c26e4c19acffb',
'fun' => 'new',
'lang' => 'zh_CN',
// '_' => time() * 1000 . random_int(1, 999)
'_' => time()
]);
preg_match('/window.QRLogin.code = (\d+); window.QRLogin.uuid = \"(\S+?)\"/', $content, $matches);
if(!$matches){
Console::log('获取UUID失败', Console::ERROR);
exit;
}
$this->uuid = $matches[2];
}
/**
* generate a login qrcode
*/
public function generateQrCode()
{
$url = 'https://login.weixin.qq.com/l/' . $this->uuid;
$qrCode = new QrCode($url);
if(!is_dir(realpath($this->config['tmp']))){
mkdir($this->config['tmp'], 0700, true);
}
$file = System::getPath() . 'qr.png';
FileManager::download('qr.png', file_get_contents($url));
$qrCode->save($file);
}
/**
* waiting user to login
*
* @throws \Exception
*/
protected function waitForLogin()
{
$retryTime = 10;
$tip = 1;
while($retryTime > 0){
$url = sprintf('https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?tip=%s&uuid=%s&_=%s', $tip, $this->uuid, time());
$content = http()->get($url);
preg_match('/window.code=(\d+);/', $content, $matches);
$code = $matches[1];
switch($code){
case '201':
Console::log('请点击确认登录微信');
$tip = 0;
break;
case '200':
preg_match('/window.redirect_uri="(\S+?)";/', $content, $matches);
$this->redirectUri = $matches[1] . '&fun=new';
$domainList = [
'wx2.qq.com' => ['file.wx2.qq.com', 'webpush.wx2.qq.com'],
'wx8.qq.com' => ['file.wx8.qq.com', 'webpush.wx8.qq.com'],
'qq.com' => ['file.wx.qq.com', 'webpush.wx.qq.com'],
'web2.wechat.com' => ['file.web2.wechat.com', 'webpushweb2.wechat.com'],
'wechat.com' => ['file.web.wechat.com', 'webpushweb.web.wechat.com'],
];
$url = 'https://%s/cgi-bin/mmwebwx-bin';
foreach ($domainList as $domain => $list) {
if(str_contains($this->redirectUri, $domain)){
$this->fileUri = sprintf($url, $list[0]);
$this->pushUri = sprintf($url, $list[1]);
$this->baseUri = sprintf($url, $domain);
$this->domain = $domain;
break;
}else{
$this->fileUri = $this->pushUri = $this->redirectUri;
}
}
return;
case '408':
Console::log('登录超时,请重试', Console::WARNING);
$tip = 1;
$retryTime -= 1;
sleep(1);
break;
default:
Console::log("登录失败,错误码:$code 。请重试", Console::ERROR);
$tip = 1;
$retryTime -= 1;
sleep(1);
break;
}
}
Console::log('登录超时,退出应用', Console::ERROR);
exit;
}
/**
* login wechat
* @return bool
* @throws \Exception
*/
public function login()
{
$content = http()->get($this->redirectUri);
$data = (array)simplexml_load_string($content, 'SimpleXMLElement', LIBXML_NOCDATA);
$this->skey = $data['skey'];
$this->sid = $data['wxsid'];
$this->uin = $data['wxuin'];
$this->passTicket = $data['pass_ticket'];
if(in_array('', [$this->skey, $this->sid, $this->uin, $this->passTicket])){
Console::log('登录失败', Console::ERROR);
exit;
}
$this->deviceId = 'e' .substr(mt_rand().mt_rand(), 1, 15);
$this->baseRequest = [
'Uin' => intval($this->uin),
'Sid' => $this->sid,
'Skey' => $this->skey,
'DeviceID' => $this->deviceId
];
return true;
}
protected function init($first = true)
{
$url = sprintf($this->baseUri . '/webwxinit?r=%d', time());
$content = http()->json($url, [
'BaseRequest' => $this->baseRequest
]);
$result = json_decode($content, true);
$this->generateSyncKey($result, $first);
myself()->init($result['User']);
$this->initContactList($result['ContactList']);
if($result['BaseResponse']['Ret'] != 0){
Console::log('初始化失败,链接:' . $url, Console::ERROR);
exit;
}
}
protected function initContactList($contactList)
{
if($contactList){
foreach ($contactList as $contact) {
if(Group::isGroup($contact['UserName'])){
group()->put($contact['UserName'], $contact);
}
}
}
}
protected function initContact()
{
new ContactFactory();
}
/**
* open wechat status notify
*/
protected function statusNotify()
{
$url = sprintf($this->baseUri . '/webwxstatusnotify?lang=zh_CN&pass_ticket=%s', $this->passTicket);
http()->json($url, [
'BaseRequest' => $this->baseRequest,
'Code' => 3,
'FromUserName' => myself()->username,
'ToUserName' => myself()->username,
'ClientMsgId' => time()
]);
}
protected function generateSyncKey($result, $first)
{
$this->syncKey = $result['SyncKey'];
$syncKey = [];
if(is_array($this->syncKey['List'])){
foreach ($this->syncKey['List'] as $item) {
$syncKey[] = $item['Key'] . '_' . $item['Val'];
}
}elseif($first){
$this->init(false);
}
$this->syncKeyStr = implode('|', $syncKey);
}
public function setMessageHandler(\Closure $closure)
{
MessageHandler::getInstance()->setMessageHandler($closure);
}
public function setCustomerHandler(\Closure $closure)
{
MessageHandler::getInstance()->setCustomHandler($closure);
}
public function setExitHandler(\Closure $closure)
{
MessageHandler::getInstance()->setExitHandler($closure);
}
public function setExceptionHandler(\Closure $closure)
{
MessageHandler::getInstance()->setExceptionHandler($closure);
}
}