Skip to content

Commit

Permalink
add proxy support
Browse files Browse the repository at this point in the history
  • Loading branch information
bachue committed Sep 1, 2023
1 parent 6375a7b commit 886b058
Show file tree
Hide file tree
Showing 20 changed files with 270 additions and 91 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/test-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,20 @@ jobs:
with:
php-version: '8.2'

- name: Install Go
uses: actions/setup-go@v2
with:
go-version: '1.21.x'

- name: Setup build-in server
run: |
nohup php -S localhost:9000 -t ./tests/mock-server/ > phpd.log 2>&1 &
echo $! > mock-server.pid
cd tests/socks5-server/
nohup go run main.go > ../../socks5.log 2>&1 &
echo $! > ../../socks-server.pid
- name: Setup php
uses: shivammathur/setup-php@v2
with:
Expand All @@ -40,6 +49,7 @@ jobs:
./vendor/bin/phpcs --standard=PSR2 tests
./vendor/bin/phpunit --coverage-clover=coverage.xml
cat mock-server.pid | xargs kill
cat socks-server.pid | xargs kill
env:
QINIU_ACCESS_KEY: ${{ secrets.QINIU_ACCESS_KEY }}
Expand All @@ -52,5 +62,10 @@ jobs:
run: |
cat phpd.log
- name: Print socks5 server log
if: ${{ failure() }}
run: |
cat socks5.log
- name: After_success
run: bash <(curl -s https://codecov.io/bash)
8 changes: 5 additions & 3 deletions src/Qiniu/Cdn/CdnManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
use Qiniu\Auth;
use Qiniu\Http\Error;
use Qiniu\Http\Client;
use Qiniu\Http\Proxyable;

final class CdnManager
final class CdnManager extends Proxyable
{

private $auth;
private $server;

public function __construct(Auth $auth)
public function __construct(Auth $auth, $proxy = null, $proxy_auth = null, $proxy_user_password = null)
{
parent::__construct($proxy, $proxy_auth, $proxy_user_password);
$this->auth = $auth;
$this->server = 'http://fusion.qiniuapi.com';
}
Expand Down Expand Up @@ -225,7 +227,7 @@ private function post($url, $body)
{
$headers = $this->auth->authorization($url, $body, 'application/json');
$headers['Content-Type'] = 'application/json';
$ret = Client::post($url, $body, $headers);
$ret = Client::post($url, $body, $headers, $this->makeReqOpt());
if (!$ret->ok()) {
return array(null, new Error($url, $ret));
}
Expand Down
66 changes: 34 additions & 32 deletions src/Qiniu/Config.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Qiniu;

final class Config
Expand All @@ -15,9 +16,9 @@ final class Config
const RTCAPI_HOST = 'http://rtc.qiniuapi.com';
const ARGUS_HOST = 'ai.qiniuapi.com';
const CASTER_HOST = 'pili-caster.qiniuapi.com';
const SMS_HOST="https://sms.qiniuapi.com";
const SMS_HOST = "https://sms.qiniuapi.com";
const RTCAPI_VERSION = 'v3';
const SMS_VERSION='v1';
const SMS_VERSION = 'v1';

// Zone 空间对应的存储区域
public $region;
Expand Down Expand Up @@ -99,9 +100,9 @@ public function getBackupQueryRegionHosts()
return $this->backupQueryRegionHosts;
}

public function getUpHost($accessKey, $bucket)
public function getUpHost($accessKey, $bucket, $reqOpt = null)
{
$region = $this->getRegion($accessKey, $bucket);
$region = $this->getRegion($accessKey, $bucket, $reqOpt);
if ($this->useHTTPS === true) {
$scheme = "https://";
} else {
Expand All @@ -116,9 +117,9 @@ public function getUpHost($accessKey, $bucket)
return $scheme . $host;
}

public function getUpHostV2($accessKey, $bucket)
public function getUpHostV2($accessKey, $bucket, $reqOpt = null)
{
list($region, $err) = $this->getRegionV2($accessKey, $bucket);
list($region, $err) = $this->getRegionV2($accessKey, $bucket, $reqOpt);
if ($err != null) {
return array(null, $err);
}
Expand All @@ -137,9 +138,9 @@ public function getUpHostV2($accessKey, $bucket)
return array($scheme . $host, null);
}

public function getUpBackupHost($accessKey, $bucket)
public function getUpBackupHost($accessKey, $bucket, $reqOpt = null)
{
$region = $this->getRegion($accessKey, $bucket);
$region = $this->getRegion($accessKey, $bucket, $reqOpt);
if ($this->useHTTPS === true) {
$scheme = "https://";
} else {
Expand All @@ -154,9 +155,9 @@ public function getUpBackupHost($accessKey, $bucket)
return $scheme . $host;
}

public function getUpBackupHostV2($accessKey, $bucket)
public function getUpBackupHostV2($accessKey, $bucket, $reqOpt = null)
{
list($region, $err) = $this->getRegionV2($accessKey, $bucket);
list($region, $err) = $this->getRegionV2($accessKey, $bucket, $reqOpt);
if ($err != null) {
return array(null, $err);
}
Expand All @@ -175,9 +176,9 @@ public function getUpBackupHostV2($accessKey, $bucket)
return array($scheme . $host, null);
}

public function getRsHost($accessKey, $bucket)
public function getRsHost($accessKey, $bucket, $reqOpt = null)
{
$region = $this->getRegion($accessKey, $bucket);
$region = $this->getRegion($accessKey, $bucket, $reqOpt);

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

public function getRsHostV2($accessKey, $bucket)
public function getRsHostV2($accessKey, $bucket, $reqOpt = null)
{
list($region, $err) = $this->getRegionV2($accessKey, $bucket);
list($region, $err) = $this->getRegionV2($accessKey, $bucket, $reqOpt);
if ($err != null) {
return array(null, $err);
}
Expand All @@ -204,9 +205,9 @@ public function getRsHostV2($accessKey, $bucket)
return array($scheme . $region->rsHost, null);
}

public function getRsfHost($accessKey, $bucket)
public function getRsfHost($accessKey, $bucket, $reqOpt = null)
{
$region = $this->getRegion($accessKey, $bucket);
$region = $this->getRegion($accessKey, $bucket, $reqOpt);

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

public function getRsfHostV2($accessKey, $bucket)
public function getRsfHostV2($accessKey, $bucket, $reqOpt = null)
{
list($region, $err) = $this->getRegionV2($accessKey, $bucket);
list($region, $err) = $this->getRegionV2($accessKey, $bucket, $reqOpt);
if ($err != null) {
return array(null, $err);
}
Expand All @@ -233,9 +234,9 @@ public function getRsfHostV2($accessKey, $bucket)
return array($scheme . $region->rsfHost, null);
}

public function getIovipHost($accessKey, $bucket)
public function getIovipHost($accessKey, $bucket, $reqOpt = null)
{
$region = $this->getRegion($accessKey, $bucket);
$region = $this->getRegion($accessKey, $bucket, $reqOpt);

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

public function getIovipHostV2($accessKey, $bucket)
public function getIovipHostV2($accessKey, $bucket, $reqOpt = null)
{
list($region, $err) = $this->getRegionV2($accessKey, $bucket);
list($region, $err) = $this->getRegionV2($accessKey, $bucket, $reqOpt);
if ($err != null) {
return array(null, $err);
}
Expand All @@ -262,9 +263,9 @@ public function getIovipHostV2($accessKey, $bucket)
return array($scheme . $region->iovipHost, null);
}

public function getApiHost($accessKey, $bucket)
public function getApiHost($accessKey, $bucket, $reqOpt = null)
{
$region = $this->getRegion($accessKey, $bucket);
$region = $this->getRegion($accessKey, $bucket, $reqOpt);

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

public function getApiHostV2($accessKey, $bucket)
public function getApiHostV2($accessKey, $bucket, $reqOpt = null)
{
list($region, $err) = $this->getRegionV2($accessKey, $bucket);
list($region, $err) = $this->getRegionV2($accessKey, $bucket, $reqOpt);
if ($err != null) {
return array(null, $err);
}
Expand All @@ -302,8 +303,7 @@ private function getRegionCache($cacheId)
{
if (isset($this->regionCache[$cacheId]) &&
isset($this->regionCache[$cacheId]["deadline"]) &&
time() < $this->regionCache[$cacheId]["deadline"]
) {
time() < $this->regionCache[$cacheId]["deadline"]) {
return $this->regionCache[$cacheId]["region"];
}

Expand Down Expand Up @@ -336,7 +336,7 @@ private function setRegionCache($cacheId, $region)
*
* @throws \Exception
*/
private function getRegion($accessKey, $bucket)
private function getRegion($accessKey, $bucket, $reqOpt = null)
{
if (isset($this->zone)) {
return $this->zone;
Expand All @@ -353,7 +353,8 @@ private function getRegion($accessKey, $bucket)
$bucket,
$this->getQueryRegionHost(),
$this->getBackupQueryRegionHosts(),
$this->backupUcHostsRetryTimes
$this->backupUcHostsRetryTimes,
$reqOpt
);
if (is_array($region)) {
list($region, $err) = $region;
Expand All @@ -366,7 +367,7 @@ private function getRegion($accessKey, $bucket)
return $region;
}

private function getRegionV2($accessKey, $bucket)
private function getRegionV2($accessKey, $bucket, $reqOpt = null)
{
if (isset($this->zone)) {
return array($this->zone, null);
Expand All @@ -383,7 +384,8 @@ private function getRegionV2($accessKey, $bucket)
$bucket,
$this->getQueryRegionHost(),
$this->getBackupQueryRegionHosts(),
$this->backupUcHostsRetryTimes
$this->backupUcHostsRetryTimes,
$reqOpt
);
if (is_array($region)) {
list($region, $err) = $region;
Expand Down
2 changes: 0 additions & 2 deletions src/Qiniu/Http/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,6 @@ public static function sendRequest($request)
$options = array(
CURLOPT_USERAGENT => self::userAgent(),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_SSL_VERIFYHOST => true,
CURLOPT_HEADER => true,
CURLOPT_NOBODY => false,
CURLOPT_CUSTOMREQUEST => $request->method,
Expand Down
34 changes: 34 additions & 0 deletions src/Qiniu/Http/Proxyable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Qiniu\Http;

use Qiniu\Http\RequestOptions;

abstract class Proxyable
{
private $proxy;
private $proxy_auth;
private $proxy_user_password;

protected function __construct($proxy = null, $proxy_auth = null, $proxy_user_password = null)
{
$this->proxy = $proxy;
$this->proxy_auth = $proxy_auth;
$this->proxy_user_password = $proxy_user_password;
}

protected function makeReqOpt()
{
$reqOpt = new RequestOptions();
if ($this->proxy !== null) {
$reqOpt->proxy = $this->proxy;
}
if ($this->proxy_auth !== null) {
$reqOpt->proxy_auth = $this->proxy_auth;
}
if ($this->proxy_user_password !== null) {
$reqOpt->proxy_user_password = $this->proxy_user_password;
}
return $reqOpt;
}
}
35 changes: 34 additions & 1 deletion src/Qiniu/Http/RequestOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ final class RequestOptions
*/
public $timeout_ms;

/**
* @var string|null
* 代理URL,默认:空
*/
public $proxy;

/**
* @var int|null
* 代理鉴权方式,默认:空
*/
public $proxy_auth;

/**
* @var string|null
* 代理鉴权参数,默认:空
*/
public $proxy_user_password;

/**
* @var array<Middleware>
*/
Expand All @@ -42,12 +60,18 @@ public function __construct(
$connection_timeout_ms = null,
$timeout = null,
$timeout_ms = null,
$middlewares = array()
$middlewares = array(),
$proxy = null,
$proxy_auth = null,
$proxy_user_password = null
) {
$this->connection_timeout = $connection_timeout;
$this->connection_timeout_ms = $connection_timeout_ms;
$this->timeout = $timeout;
$this->timeout_ms = $timeout_ms;
$this->proxy = $proxy;
$this->proxy_auth = $proxy_auth;
$this->proxy_user_password = $proxy_user_password;
$this->middlewares = $middlewares;
}

Expand All @@ -66,6 +90,15 @@ public function getCurlOpt()
if ($this->timeout_ms != null) {
$result[CURLOPT_TIMEOUT_MS] = $this->timeout_ms;
}
if ($this->proxy != null) {
$result[CURLOPT_PROXY] = $this->proxy;
}
if ($this->proxy_auth != null) {
$result[CURLOPT_PROXYAUTH] = $this->proxy_auth;
}
if ($this->proxy_user_password != null) {
$result[CURLOPT_PROXYUSERPWD] = $this->proxy_user_password;
}
return $result;
}
}
Loading

0 comments on commit 886b058

Please sign in to comment.