|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace EasyHttp\Model; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Proxy Server |
|
7
|
|
|
* |
|
8
|
|
|
* @link https://github.com/shahradelahi/easy-http |
|
9
|
|
|
* @author Shahrad Elahi (https://github.com/shahradelahi) |
|
10
|
|
|
* @license https://github.com/shahradelahi/easy-http/blob/master/LICENSE (MIT License) |
|
11
|
|
|
*/ |
|
12
|
|
|
class ProxyServer |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Proxy Server IP Address - IP or Domain |
|
17
|
|
|
* |
|
18
|
|
|
* @var ?string |
|
19
|
|
|
*/ |
|
20
|
|
|
public ?string $ip = null; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Proxy Server Port - 1-65535 |
|
24
|
|
|
* |
|
25
|
|
|
* @var ?int |
|
26
|
|
|
*/ |
|
27
|
|
|
public ?int $port = null; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Proxy Server Username |
|
31
|
|
|
* |
|
32
|
|
|
* @var ?string |
|
33
|
|
|
*/ |
|
34
|
|
|
public ?string $username = null; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Proxy Server Password |
|
38
|
|
|
* |
|
39
|
|
|
* @var ?string |
|
40
|
|
|
*/ |
|
41
|
|
|
public ?string $password = null; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Proxy Server Type |
|
45
|
|
|
* |
|
46
|
|
|
* @var ?int [CURLPROXY_SOCKS5|CURLPROXY_SOCKS4|CURLPROXY_HTTP] |
|
47
|
|
|
*/ |
|
48
|
|
|
public ?int $type = null; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Setup Proxy Server |
|
52
|
|
|
* |
|
53
|
|
|
* @param array $proxy ["ip", "port", "user", "pass"] |
|
54
|
|
|
* @return ProxyServer |
|
55
|
|
|
*/ |
|
56
|
|
|
public function setProxy(array $proxy): self |
|
57
|
|
|
{ |
|
58
|
|
|
$this->ip = $proxy['ip']; |
|
59
|
|
|
$this->port = $proxy['port']; |
|
60
|
|
|
$this->username = $proxy['user']; |
|
61
|
|
|
$this->password = $proxy['pass']; |
|
62
|
|
|
|
|
63
|
|
|
return $this; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Set Proxy Server Type |
|
68
|
|
|
* |
|
69
|
|
|
* @param int $type [CURLPROXY_SOCKS5|CURLPROXY_SOCKS4|CURLPROXY_HTTP] |
|
70
|
|
|
* @return ProxyServer |
|
71
|
|
|
*/ |
|
72
|
|
|
public function setType(int $type): self |
|
73
|
|
|
{ |
|
74
|
|
|
if (!in_array($type, [CURLPROXY_SOCKS5, CURLPROXY_SOCKS4, CURLPROXY_HTTP])) { |
|
75
|
|
|
throw new \InvalidArgumentException('Invalid Proxy Type'); |
|
76
|
|
|
} |
|
77
|
|
|
$this->type = $type; |
|
78
|
|
|
|
|
79
|
|
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Get the IP:Port string |
|
84
|
|
|
* |
|
85
|
|
|
* @return ?string |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getHost(): ?string |
|
88
|
|
|
{ |
|
89
|
|
|
return !empty($this->ip) && !empty($this->port) ? "$this->ip:$this->port" : null; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Get auth data |
|
94
|
|
|
* |
|
95
|
|
|
* @return ?string |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getAuth(): ?string |
|
98
|
|
|
{ |
|
99
|
|
|
return !empty($this->username) && !empty($this->password) ? "$this->username:$this->password" : null; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
} |