Skip to content

Commit

Permalink
Full support for socks5 proxies
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeykomlev committed Mar 30, 2023
1 parent a88449c commit 2c8bd0b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ public function setProxy($proxy)
{
$proxy = str_replace("https://", "", $proxy);
$proxy = str_replace("http://", "", $proxy);
if (strpos("socks5://", $proxy) !== false) {
if (strpos($proxy, "socks5://") !== false) {
$proxy = str_replace("socks5://", "", $proxy);
$this->proxyType = "socks5";
}
Expand Down
31 changes: 20 additions & 11 deletions src/Speedtest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,21 +112,25 @@ protected function getRemoteConfig() {
],
"headers" => [
"accept-language" => "en-US"
]
],
// "debug" => true
];

if (!empty($this->config->getProxy())) {
$proxy_parts = explode('://', $this->config->getProxy());
$options["proxy"] = $proxy_parts[0] == "https" ? $proxy_parts[1] : $this->config->getProxy();
if ($this->config->getProxyType() == "socks5") {
$options["proxy"] = "socks5://" . $this->config->getProxy();
} else {
$options["proxy"] = "http://" . $this->config->getProxy();
}

$res = $client->request('GET', 'https://www.speedtest.net/speedtest-config.php', $options);

$data = $res->getBody()->getContents();
$xml = simplexml_load_string($data);

if (empty($xml)) {
throw new SpeedtestException("Couldn't get remote client config. Reason: empty xml data.");
}

$server_config = $xml->{'server-config'};
$download = $xml->download;
$upload = $xml->upload;
Expand Down Expand Up @@ -189,9 +193,10 @@ public function getServers() {
]
];

if (!empty($this->config->getProxy())) {
$proxy_parts = explode('://', $this->config->getProxy());
$options["proxy"] = $proxy_parts[0] == "https" ? $proxy_parts[1] : $this->config->getProxy();
if ($this->config->getProxyType() == "socks5") {
$options["proxy"] = "socks5://" . $this->config->getProxy();
} else {
$options["proxy"] = "http://" . $this->config->getProxy();
}

$res = $client->request('GET', 'https://c.speedtest.net/speedtest-servers-static.php', $options);
Expand Down Expand Up @@ -299,10 +304,11 @@ public function getBestServer($checkCount = 100, $skipLatencyRetries = false) {
$ping_time = $stats->getHandlerStat('starttransfer_time') - $stats->getHandlerStat('pretransfer_time');
}
];

if (!empty($this->config->getProxy())) {
$proxy_parts = explode('://', $this->config->getProxy());
$options["proxy"] = $proxy_parts[0] == "https" ? $proxy_parts[1] : $this->config->getProxy();

if ($this->config->getProxyType() == "socks5") {
$options["proxy"] = "socks5://" . $this->config->getProxy();
} else {
$options["proxy"] = "http://" . $this->config->getProxy();
}

$res = $client->request('GET', $url, $options);
Expand Down Expand Up @@ -375,6 +381,7 @@ public function download($threads = null) {
}
if (!empty($this->config->getProxy())) {
if ($this->config->getProxyType() == "socks5") {
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
$urlParts = parse_url("socks5://" . $this->config->getProxy());
} else {
$urlParts = parse_url("http://" . $this->config->getProxy());
Expand Down Expand Up @@ -457,6 +464,7 @@ public function upload($threads = null) {
}
if (!empty($this->config->getProxy())) {
if ($this->config->getProxyType() == "socks5") {
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
$urlParts = parse_url("socks5://" . $this->config->getProxy());
} else {
$urlParts = parse_url("http://" . $this->config->getProxy());
Expand Down Expand Up @@ -548,6 +556,7 @@ public function share() {
}
if (!empty($this->config->getProxy())) {
if ($this->config->getProxyType() == "socks5") {
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
$urlParts = parse_url("socks5://" . $this->config->getProxy());
} else {
$urlParts = parse_url("http://" . $this->config->getProxy());
Expand Down

0 comments on commit 2c8bd0b

Please sign in to comment.