This repository has been archived by the owner on Sep 19, 2022. It is now read-only.
forked from endokai/docker-wechatPc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
WorkerStart.php
154 lines (143 loc) · 5.3 KB
/
WorkerStart.php
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
<?php
namespace Wechat\App\Service\Listener;
use Wechat\App\Library\Package;
use Wechat\App\Library\Tools;
use Wechat\App\Service\Transit;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http;
use Workerman\WebServer;
use Workerman\Worker;
/**
* 进程启动完成
*/
class WorkerStart extends AbstractListener
{
/** @var null|Worker */
private $worker = null;
/**
* WorkerStart constructor.
*/
public function __construct()
{
$config = Tools::config('web_listen');
Transit::$webListenAddress = $config['ipaddress'] ?? null;
Transit::$webListenPort = $config['port'] ?? null;
parent::__construct();
}
/**
* 处理事件
*/
public function handle()
{
// 打印日记
Tools::log('Worker Started.');
// 开启浏览器端的websocket监听,浏览器端发送过来的数据做转发给微信客户端
$this->worker = new Worker("websocket://" . Transit::$webListenAddress . ":" . Transit::$webListenPort);
$this->worker->reusePort = true; // 允许不同进程共同监听
$this->worker->onMessage = array($this, 'onMessage') ;
$this->worker->onClose = array($this, 'onClose');
$this->worker->onConnect = array($this, 'onConnect');
$this->worker->onError = array($this, 'onError');
$this->worker->listen();
// 监听WebServer
$webserver = new WebServer('http://0.0.0.0:80');
$webserver->onMessage = function($connection, $info) use ($webserver) {
// 文件访问
$webserver->onMessage($connection);
};
$webserver->addRoot('*', ROOT_PATH . '/Public/');
$webserver->initMimeTypeMap();
$webserver->listen();
}
/**
* 接收消息
* @param TcpConnection $connection
* @param $message
*/
public function onMessage($connection, $message)
{
try {
$json = json_decode($message, true);
if (
!isset($json['wechatId']) || !$json['wechatId'] ||
!isset($json['opCode']) || !is_numeric($json['opCode']) ||
!isset($json['body']) || is_null($json['body'])
) {
return Tools::log('Recv Web Error Format Message: ' . 'ConnectId=' . $connection->id . ', Message=' . $message);
}
// 构造消息包
$package = new Package();
$package->setConnection($connection);
$package->setWechatId($json['wechatId']);
$package->setOpCode($json['opCode']);
$package->setBody($json['body']);
// 把消息扔进中转站
Transit::webMessage($package);
} catch (\ErrorException $exception) {
echo $exception->getMessage()."\n";
return Tools::log('Recv Web Not Json Message: ' . 'ConnectId=' . $connection->id . ', Message=' . $message);
}
}
/**
* 连接成功
* via: https://www.workerman.net/doc/workerman/appendices/about-websocket.html
* @param TcpConnection $connection
*/
public function onConnect($connection)
{
$connection->onWebSocketConnect = function($connection , $http_header)
{
// 可以在这里判断连接来源是否合法,不合法就关掉连接
// Tools::log("new connection from ip " . $connection->getRemoteIp());
// client side data
$app_id = $_GET['app_id'];
$timestamp = $_GET['timestamp'];
$hash = $_GET['hash'];
// server side data
$local_timestamp = Tools::timestamp();
$local_app_id = Tools::config('app_id');
$local_app_key = Tools::config('app_key');
$expire = Tools::config('expire');
if (abs($local_timestamp - $timestamp) < $expire) {
if ($app_id == $local_app_id) {
// $query = "app_id=" . $local_app_id . "×tamp=" . $timestamp . "&app_key=" . $local_app_key;
$query = "app_id=" . $local_app_id . "×tamp=" . $timestamp . "&app_key" . $local_app_key;
$local_hash = hash("sha256", $query, false);
// Tools::log("hash:" . $hash);
// Tools::log("local_hash:" . $local_hash);
if ($local_hash == $hash) {
Transit::webConnect($connection);
} else {
$connection->close();
Tools::log("[Error] hash not match!");
}
} else {
$connection->close();
Tools::log("[Error] app_id not match!");
}
} else {
$connection->close();
Tools::log("[Error] timestamp expired!");
}
};
}
/**
* 关闭连接
* @param TcpConnection $connection
*/
public function onClose($connection)
{
Transit::webClose($connection);
}
/**
* 报错信息
* @param TcpConnection $connection
* @param int $code
* @param string $msg
*/
public function onError($connection, $code, $msg)
{
// 打印日记
Tools::log('Web Error: ' . 'ConnectId=' . $connection->id . ', Message=' . $code . ' - ' . $msg);
}
}