-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.php
241 lines (200 loc) · 6 KB
/
Client.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
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
<?php
namespace AliyunSDK\Acm;
use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\HandlerStack;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
/**
* Class Client
* @package AliyunSDK\Acm
*/
class Client
{
const GET_SERVER = "diamond-server/diamond";
const GET_CONFIG = "diamond-server/config.co";
const SET_CONFIG = "diamond-server/basestone.do?method=syncUpdateAll";
const DEL_CONFIG = "diamond-server/datum.do?method=deleteAllDatums";
/**
* 参数组
*
* @var string
*/
private $element;
/**
* 认证凭证
*
* @var AuthCreds
*/
private $authCreds;
/**
* HTTP客户端
*
* @var HttpClient
*/
private $httpClient;
/**
* 可用请求端点
*
* @var Endpoint[]
*/
private $endpoints;
/**
* Client constructor.
* @param Endpoint $endpoint
* @param Element $element
* @param AuthCreds $authCreds
* @throws \Exception
*/
public function __construct(Endpoint $endpoint, Element $element, AuthCreds $authCreds)
{
$this->element = $element;
$this->authCreds = $authCreds;
$handler = HandlerStack::create();
$handler->push($this->addHeaders());
$this->httpClient = new HttpClient(["handler" => $handler]);
// 通过Endpoint查询服务IP列表, 以便后面能够通过IP发起请求
$res = $this->httpClient->get(
$endpoint->makeURL(self::GET_SERVER)
);
$content = $this->handleResponse($res);
foreach (explode("\n", $content) as $server)
{
if (!($server = trim($server)))
{
continue;
}
$parts = explode(":", $server);
$this->endpoints[] = new Endpoint($parts[0], $parts[1] ?? 8080);
}
}
/**
* 读取配置集
*
* @param string $dataId
* @return string
* @throws \Exception
*/
public function read(string $dataId): string
{
$ele = $this->element->withDataId($dataId);
$res = $this->httpClient->get(
$this->makeURL(self::GET_CONFIG) . "?" . $ele->toQuery()
);
return $this->handleResponse($res);
}
/**
* 写入配置集
*
* @param string $dataId
* @param string $content
* @throws \Exception
*/
public function write(string $dataId, string $content): void
{
$ele = $this->element->withDataId($dataId);
$res = $this->httpClient->post(
$this->makeURL(self::SET_CONFIG),
["form_params" => $ele->toArray(["content" => $content])]
);
$this->handleResponse($res);
}
/**
* 删除配置集
*
* @param string $dataId
* @throws \Exception
*/
public function remove(string $dataId): void
{
$ele = $this->element->withDataId($dataId);
$res = $this->httpClient->post(
$this->makeURL(self::DEL_CONFIG),
["form_params" => $ele->toArray()]
);
$this->handleResponse($res);
}
/**
* 监听配置集
*
* @param string $dataId
* @param string $content
* @return bool
* @throws \Exception
*/
public function watch(string $dataId, string $content): bool
{
$wordDelimiter = chr(37) . chr(48) . chr(50);
$lineDelimiter = chr(37) . chr(48) . chr(49);
$args = [$dataId, $this->element->getGroupName(), md5($content), $this->element->getNamespace()];
$res = $this->httpClient->post(
$this->makeURL(self::GET_CONFIG),
[
"headers" => [
"longPullingTimeout" => 30 * 1000,
],
"body" => "Probe-Modify-Request=" . implode($wordDelimiter, $args) . $lineDelimiter,
]
);
return rtrim($this->handleResponse($res), $lineDelimiter) != "";
}
/**
* 生成完整URL
*
* @param string $path
* @return string
*/
private function makeURL(string $path): string
{
$key = array_rand($this->endpoints, 1);
return $this->endpoints[$key]->makeURL($path);
}
/**
* 添加请求头信息
*
* @return \Closure
*/
private function addHeaders(): \Closure
{
return function (callable $handler) {
return function (RequestInterface $request, array $options) use ($handler) {
$request = $request
->withHeader("timeStamp", time() * 1000)
->withHeader("Spas-AccessKey", $this->authCreds->getAccessKey());
$request = $request->withHeader("Spas-Signature", $this->generateSignature($request));
return $handler($request, $options);
};
};
}
/**
* 计算安全签名
*
* @param RequestInterface $request
* @return string
*/
private function generateSignature(RequestInterface $request): string
{
$string = sprintf(
"%s+%s+%s",
$this->element->getNamespace(),
$this->element->getGroupName(),
$request->getHeaderLine("timeStamp")
);
return base64_encode(hash_hmac('sha1', $string, $this->authCreds->getSecretKey(), TRUE));
}
/**
* 处理请求响应
*
* @param ResponseInterface $response
* @return string
* @throws \Exception
*/
private function handleResponse(ResponseInterface $response): string
{
$content = $response->getBody()->getContents();
if ($response->getStatusCode() != 200)
{
throw new \Exception("get server failed, status code = {$response->getStatusCode()}, content = {$content}.");
}
return $content;
}
}