Skip to content

Commit f8625fe

Browse files
authored
支持 proxy (#419)
1 parent dfa6893 commit f8625fe

22 files changed

+442
-92
lines changed

.github/workflows/test-ci.yml

+15
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,20 @@ jobs:
1818
with:
1919
php-version: '8.2'
2020

21+
- name: Install Go
22+
uses: actions/setup-go@v2
23+
with:
24+
go-version: '1.21.x'
25+
2126
- name: Setup build-in server
2227
run: |
2328
nohup php -S localhost:9000 -t ./tests/mock-server/ > phpd.log 2>&1 &
2429
echo $! > mock-server.pid
2530
31+
cd tests/socks5-server/
32+
nohup go run main.go > ../../socks5.log 2>&1 &
33+
echo $! > ../../socks-server.pid
34+
2635
- name: Setup php
2736
uses: shivammathur/setup-php@v2
2837
with:
@@ -40,6 +49,7 @@ jobs:
4049
./vendor/bin/phpcs --standard=PSR2 tests
4150
./vendor/bin/phpunit --coverage-clover=coverage.xml
4251
cat mock-server.pid | xargs kill
52+
cat socks-server.pid | xargs kill
4353
4454
env:
4555
QINIU_ACCESS_KEY: ${{ secrets.QINIU_ACCESS_KEY }}
@@ -52,5 +62,10 @@ jobs:
5262
run: |
5363
cat phpd.log
5464
65+
- name: Print socks5 server log
66+
if: ${{ failure() }}
67+
run: |
68+
cat socks5.log
69+
5570
- name: After_success
5671
run: bash <(curl -s https://codecov.io/bash)

src/Qiniu/Cdn/CdnManager.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,20 @@
55
use Qiniu\Auth;
66
use Qiniu\Http\Error;
77
use Qiniu\Http\Client;
8+
use Qiniu\Http\Proxy;
89

910
final class CdnManager
1011
{
1112

1213
private $auth;
1314
private $server;
15+
private $proxy;
1416

15-
public function __construct(Auth $auth)
17+
public function __construct(Auth $auth, $proxy = null, $proxy_auth = null, $proxy_user_password = null)
1618
{
1719
$this->auth = $auth;
1820
$this->server = 'http://fusion.qiniuapi.com';
21+
$this->proxy = new Proxy($proxy, $proxy_auth, $proxy_user_password);
1922
}
2023

2124
/**
@@ -225,7 +228,7 @@ private function post($url, $body)
225228
{
226229
$headers = $this->auth->authorization($url, $body, 'application/json');
227230
$headers['Content-Type'] = 'application/json';
228-
$ret = Client::post($url, $body, $headers);
231+
$ret = Client::post($url, $body, $headers, $this->proxy->makeReqOpt());
229232
if (!$ret->ok()) {
230233
return array(null, new Error($url, $ret));
231234
}

src/Qiniu/Config.php

+34-32
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace Qiniu;
34

45
final class Config
@@ -15,9 +16,9 @@ final class Config
1516
const RTCAPI_HOST = 'http://rtc.qiniuapi.com';
1617
const ARGUS_HOST = 'ai.qiniuapi.com';
1718
const CASTER_HOST = 'pili-caster.qiniuapi.com';
18-
const SMS_HOST="https://sms.qiniuapi.com";
19+
const SMS_HOST = "https://sms.qiniuapi.com";
1920
const RTCAPI_VERSION = 'v3';
20-
const SMS_VERSION='v1';
21+
const SMS_VERSION = 'v1';
2122

2223
// Zone 空间对应的存储区域
2324
public $region;
@@ -99,9 +100,9 @@ public function getBackupQueryRegionHosts()
99100
return $this->backupQueryRegionHosts;
100101
}
101102

102-
public function getUpHost($accessKey, $bucket)
103+
public function getUpHost($accessKey, $bucket, $reqOpt = null)
103104
{
104-
$region = $this->getRegion($accessKey, $bucket);
105+
$region = $this->getRegion($accessKey, $bucket, $reqOpt);
105106
if ($this->useHTTPS === true) {
106107
$scheme = "https://";
107108
} else {
@@ -116,9 +117,9 @@ public function getUpHost($accessKey, $bucket)
116117
return $scheme . $host;
117118
}
118119

119-
public function getUpHostV2($accessKey, $bucket)
120+
public function getUpHostV2($accessKey, $bucket, $reqOpt = null)
120121
{
121-
list($region, $err) = $this->getRegionV2($accessKey, $bucket);
122+
list($region, $err) = $this->getRegionV2($accessKey, $bucket, $reqOpt);
122123
if ($err != null) {
123124
return array(null, $err);
124125
}
@@ -137,9 +138,9 @@ public function getUpHostV2($accessKey, $bucket)
137138
return array($scheme . $host, null);
138139
}
139140

140-
public function getUpBackupHost($accessKey, $bucket)
141+
public function getUpBackupHost($accessKey, $bucket, $reqOpt = null)
141142
{
142-
$region = $this->getRegion($accessKey, $bucket);
143+
$region = $this->getRegion($accessKey, $bucket, $reqOpt);
143144
if ($this->useHTTPS === true) {
144145
$scheme = "https://";
145146
} else {
@@ -154,9 +155,9 @@ public function getUpBackupHost($accessKey, $bucket)
154155
return $scheme . $host;
155156
}
156157

157-
public function getUpBackupHostV2($accessKey, $bucket)
158+
public function getUpBackupHostV2($accessKey, $bucket, $reqOpt = null)
158159
{
159-
list($region, $err) = $this->getRegionV2($accessKey, $bucket);
160+
list($region, $err) = $this->getRegionV2($accessKey, $bucket, $reqOpt);
160161
if ($err != null) {
161162
return array(null, $err);
162163
}
@@ -175,9 +176,9 @@ public function getUpBackupHostV2($accessKey, $bucket)
175176
return array($scheme . $host, null);
176177
}
177178

178-
public function getRsHost($accessKey, $bucket)
179+
public function getRsHost($accessKey, $bucket, $reqOpt = null)
179180
{
180-
$region = $this->getRegion($accessKey, $bucket);
181+
$region = $this->getRegion($accessKey, $bucket, $reqOpt);
181182

182183
if ($this->useHTTPS === true) {
183184
$scheme = "https://";
@@ -188,9 +189,9 @@ public function getRsHost($accessKey, $bucket)
188189
return $scheme . $region->rsHost;
189190
}
190191

191-
public function getRsHostV2($accessKey, $bucket)
192+
public function getRsHostV2($accessKey, $bucket, $reqOpt = null)
192193
{
193-
list($region, $err) = $this->getRegionV2($accessKey, $bucket);
194+
list($region, $err) = $this->getRegionV2($accessKey, $bucket, $reqOpt);
194195
if ($err != null) {
195196
return array(null, $err);
196197
}
@@ -204,9 +205,9 @@ public function getRsHostV2($accessKey, $bucket)
204205
return array($scheme . $region->rsHost, null);
205206
}
206207

207-
public function getRsfHost($accessKey, $bucket)
208+
public function getRsfHost($accessKey, $bucket, $reqOpt = null)
208209
{
209-
$region = $this->getRegion($accessKey, $bucket);
210+
$region = $this->getRegion($accessKey, $bucket, $reqOpt);
210211

211212
if ($this->useHTTPS === true) {
212213
$scheme = "https://";
@@ -217,9 +218,9 @@ public function getRsfHost($accessKey, $bucket)
217218
return $scheme . $region->rsfHost;
218219
}
219220

220-
public function getRsfHostV2($accessKey, $bucket)
221+
public function getRsfHostV2($accessKey, $bucket, $reqOpt = null)
221222
{
222-
list($region, $err) = $this->getRegionV2($accessKey, $bucket);
223+
list($region, $err) = $this->getRegionV2($accessKey, $bucket, $reqOpt);
223224
if ($err != null) {
224225
return array(null, $err);
225226
}
@@ -233,9 +234,9 @@ public function getRsfHostV2($accessKey, $bucket)
233234
return array($scheme . $region->rsfHost, null);
234235
}
235236

236-
public function getIovipHost($accessKey, $bucket)
237+
public function getIovipHost($accessKey, $bucket, $reqOpt = null)
237238
{
238-
$region = $this->getRegion($accessKey, $bucket);
239+
$region = $this->getRegion($accessKey, $bucket, $reqOpt);
239240

240241
if ($this->useHTTPS === true) {
241242
$scheme = "https://";
@@ -246,9 +247,9 @@ public function getIovipHost($accessKey, $bucket)
246247
return $scheme . $region->iovipHost;
247248
}
248249

249-
public function getIovipHostV2($accessKey, $bucket)
250+
public function getIovipHostV2($accessKey, $bucket, $reqOpt = null)
250251
{
251-
list($region, $err) = $this->getRegionV2($accessKey, $bucket);
252+
list($region, $err) = $this->getRegionV2($accessKey, $bucket, $reqOpt);
252253
if ($err != null) {
253254
return array(null, $err);
254255
}
@@ -262,9 +263,9 @@ public function getIovipHostV2($accessKey, $bucket)
262263
return array($scheme . $region->iovipHost, null);
263264
}
264265

265-
public function getApiHost($accessKey, $bucket)
266+
public function getApiHost($accessKey, $bucket, $reqOpt = null)
266267
{
267-
$region = $this->getRegion($accessKey, $bucket);
268+
$region = $this->getRegion($accessKey, $bucket, $reqOpt);
268269

269270
if ($this->useHTTPS === true) {
270271
$scheme = "https://";
@@ -275,9 +276,9 @@ public function getApiHost($accessKey, $bucket)
275276
return $scheme . $region->apiHost;
276277
}
277278

278-
public function getApiHostV2($accessKey, $bucket)
279+
public function getApiHostV2($accessKey, $bucket, $reqOpt = null)
279280
{
280-
list($region, $err) = $this->getRegionV2($accessKey, $bucket);
281+
list($region, $err) = $this->getRegionV2($accessKey, $bucket, $reqOpt);
281282
if ($err != null) {
282283
return array(null, $err);
283284
}
@@ -302,8 +303,7 @@ private function getRegionCache($cacheId)
302303
{
303304
if (isset($this->regionCache[$cacheId]) &&
304305
isset($this->regionCache[$cacheId]["deadline"]) &&
305-
time() < $this->regionCache[$cacheId]["deadline"]
306-
) {
306+
time() < $this->regionCache[$cacheId]["deadline"]) {
307307
return $this->regionCache[$cacheId]["region"];
308308
}
309309

@@ -336,7 +336,7 @@ private function setRegionCache($cacheId, $region)
336336
*
337337
* @throws \Exception
338338
*/
339-
private function getRegion($accessKey, $bucket)
339+
private function getRegion($accessKey, $bucket, $reqOpt = null)
340340
{
341341
if (isset($this->zone)) {
342342
return $this->zone;
@@ -353,7 +353,8 @@ private function getRegion($accessKey, $bucket)
353353
$bucket,
354354
$this->getQueryRegionHost(),
355355
$this->getBackupQueryRegionHosts(),
356-
$this->backupUcHostsRetryTimes
356+
$this->backupUcHostsRetryTimes,
357+
$reqOpt
357358
);
358359
if (is_array($region)) {
359360
list($region, $err) = $region;
@@ -366,7 +367,7 @@ private function getRegion($accessKey, $bucket)
366367
return $region;
367368
}
368369

369-
private function getRegionV2($accessKey, $bucket)
370+
private function getRegionV2($accessKey, $bucket, $reqOpt = null)
370371
{
371372
if (isset($this->zone)) {
372373
return array($this->zone, null);
@@ -383,7 +384,8 @@ private function getRegionV2($accessKey, $bucket)
383384
$bucket,
384385
$this->getQueryRegionHost(),
385386
$this->getBackupQueryRegionHosts(),
386-
$this->backupUcHostsRetryTimes
387+
$this->backupUcHostsRetryTimes,
388+
$reqOpt
387389
);
388390
if (is_array($region)) {
389391
list($region, $err) = $region;

src/Qiniu/Http/Client.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace Qiniu\Http;
34

45
use Qiniu\Config;
@@ -144,8 +145,6 @@ public static function sendRequest($request)
144145
$options = array(
145146
CURLOPT_USERAGENT => self::userAgent(),
146147
CURLOPT_RETURNTRANSFER => true,
147-
CURLOPT_SSL_VERIFYPEER => false,
148-
CURLOPT_SSL_VERIFYHOST => false,
149148
CURLOPT_HEADER => true,
150149
CURLOPT_NOBODY => false,
151150
CURLOPT_CUSTOMREQUEST => $request->method,

src/Qiniu/Http/Proxy.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Qiniu\Http;
4+
5+
use Qiniu\Http\RequestOptions;
6+
7+
final class Proxy
8+
{
9+
private $proxy;
10+
private $proxy_auth;
11+
private $proxy_user_password;
12+
13+
public function __construct($proxy = null, $proxy_auth = null, $proxy_user_password = null)
14+
{
15+
$this->proxy = $proxy;
16+
$this->proxy_auth = $proxy_auth;
17+
$this->proxy_user_password = $proxy_user_password;
18+
}
19+
20+
public function makeReqOpt()
21+
{
22+
$reqOpt = new RequestOptions();
23+
if ($this->proxy !== null) {
24+
$reqOpt->proxy = $this->proxy;
25+
}
26+
if ($this->proxy_auth !== null) {
27+
$reqOpt->proxy_auth = $this->proxy_auth;
28+
}
29+
if ($this->proxy_user_password !== null) {
30+
$reqOpt->proxy_user_password = $this->proxy_user_password;
31+
}
32+
return $reqOpt;
33+
}
34+
}

src/Qiniu/Http/RequestOptions.php

+34-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,24 @@ final class RequestOptions
3232
*/
3333
public $timeout_ms;
3434

35+
/**
36+
* @var string|null
37+
* 代理URL,默认:空
38+
*/
39+
public $proxy;
40+
41+
/**
42+
* @var int|null
43+
* 代理鉴权方式,默认:空
44+
*/
45+
public $proxy_auth;
46+
47+
/**
48+
* @var string|null
49+
* 代理鉴权参数,默认:空
50+
*/
51+
public $proxy_user_password;
52+
3553
/**
3654
* @var array<Middleware>
3755
*/
@@ -42,12 +60,18 @@ public function __construct(
4260
$connection_timeout_ms = null,
4361
$timeout = null,
4462
$timeout_ms = null,
45-
$middlewares = array()
63+
$middlewares = array(),
64+
$proxy = null,
65+
$proxy_auth = null,
66+
$proxy_user_password = null
4667
) {
4768
$this->connection_timeout = $connection_timeout;
4869
$this->connection_timeout_ms = $connection_timeout_ms;
4970
$this->timeout = $timeout;
5071
$this->timeout_ms = $timeout_ms;
72+
$this->proxy = $proxy;
73+
$this->proxy_auth = $proxy_auth;
74+
$this->proxy_user_password = $proxy_user_password;
5175
$this->middlewares = $middlewares;
5276
}
5377

@@ -66,6 +90,15 @@ public function getCurlOpt()
6690
if ($this->timeout_ms != null) {
6791
$result[CURLOPT_TIMEOUT_MS] = $this->timeout_ms;
6892
}
93+
if ($this->proxy != null) {
94+
$result[CURLOPT_PROXY] = $this->proxy;
95+
}
96+
if ($this->proxy_auth != null) {
97+
$result[CURLOPT_PROXYAUTH] = $this->proxy_auth;
98+
}
99+
if ($this->proxy_user_password != null) {
100+
$result[CURLOPT_PROXYUSERPWD] = $this->proxy_user_password;
101+
}
69102
return $result;
70103
}
71104
}

0 commit comments

Comments
 (0)