Skip to content

Commit

Permalink
Merge pull request #1029 from roiwk/fix_async_tcp_proxy
Browse files Browse the repository at this point in the history
fix(async tcp): http proxy setting
  • Loading branch information
walkor committed Apr 12, 2024
2 parents 7043c1a + 1eda02e commit eb8149f
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/Connection/AsyncTcpConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,19 +261,10 @@ public function connect(): void
$this->socketContext['ssl']['peer_name'] = $this->remoteHost;
$context = stream_context_create($this->socketContext);
$this->socket = stream_socket_client("tcp://$this->proxySocks5", $errno, $err_str, 0, STREAM_CLIENT_ASYNC_CONNECT, $context);
fwrite($this->socket, chr(5) . chr(1) . chr(0));
fread($this->socket, 512);
fwrite($this->socket, chr(5) . chr(1) . chr(0) . chr(3) . chr(strlen($this->remoteHost)) . $this->remoteHost . pack("n", $this->remotePort));
fread($this->socket, 512);
} else if ($this->proxyHttp) {
$this->socketContext['ssl']['peer_name'] = $this->remoteHost;
$context = stream_context_create($this->socketContext);
$this->socket = stream_socket_client("tcp://$this->proxyHttp", $errno, $err_str, 0, STREAM_CLIENT_ASYNC_CONNECT, $context);
$str = "CONNECT $this->remoteHost:$this->remotePort HTTP/1.1\n";
$str .= "Host: $this->remoteHost:$this->remotePort\n";
$str .= "Proxy-Connection: keep-alive\n";
fwrite($this->socket, $str);
fread($this->socket, 512);
} else if ($this->socketContext) {
$context = stream_context_create($this->socketContext);
$this->socket = stream_socket_client("tcp://$this->remoteHost:$this->remotePort",
Expand Down Expand Up @@ -377,6 +368,20 @@ public function checkConnection(): void

// Check socket state.
if ($address = stream_socket_get_name($this->socket, true)) {
// Proxy
if ($this->proxySocks5 && $address === $this->proxySocks5) {
fwrite($this->socket, chr(5) . chr(1) . chr(0));
fread($this->socket, 512);
fwrite($this->socket, chr(5) . chr(1) . chr(0) . chr(3) . chr(strlen($this->remoteHost)) . $this->remoteHost . pack("n", $this->remotePort));
fread($this->socket, 512);
}
if ($this->proxyHttp && $address === $this->proxyHttp) {
$str = "CONNECT $this->remoteHost:$this->remotePort HTTP/1.1\r\n";
$str .= "Host: $this->remoteHost:$this->remotePort\r\n";
$str .= "Proxy-Connection: keep-alive\r\n\r\n";
fwrite($this->socket, $str);
fread($this->socket, 512);
}
// Nonblocking.
stream_set_blocking($this->socket, false);
// Compatible with hhvm
Expand Down

0 comments on commit eb8149f

Please sign in to comment.