1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the slince/spike package. |
5
|
|
|
* |
6
|
|
|
* (c) Slince <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Spike\Common\Tunnel; |
13
|
|
|
|
14
|
|
|
class HttpTunnel extends Tunnel |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $proxyHosts; |
20
|
|
|
|
21
|
|
|
protected $proxyHost; |
22
|
|
|
|
23
|
|
|
public function __construct($serverPort, $proxyHosts) |
24
|
|
|
{ |
25
|
|
|
$this->proxyHosts = $proxyHosts; |
26
|
|
|
parent::__construct($serverPort); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Gets all proxy hosts. |
31
|
|
|
* |
32
|
|
|
* @return array |
33
|
|
|
*/ |
34
|
|
|
public function getProxyHosts() |
35
|
|
|
{ |
36
|
|
|
return $this->proxyHosts; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param mixed $proxyHost |
41
|
|
|
*/ |
42
|
|
|
public function setProxyHost($proxyHost) |
43
|
|
|
{ |
44
|
|
|
$this->proxyHost = $proxyHost; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
|
|
public function getProxyHost() |
51
|
|
|
{ |
52
|
|
|
return $this->proxyHost; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Checks whether the tunnel supports the host. |
57
|
|
|
* |
58
|
|
|
* @param string $proxyHost |
59
|
|
|
* |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
|
|
public function supportProxyHost($proxyHost) |
63
|
|
|
{ |
64
|
|
|
return isset($this->proxyHosts[$proxyHost]); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Gets the forward host of the proxy host. |
69
|
|
|
* |
70
|
|
|
* @param string $proxyHost |
71
|
|
|
* |
72
|
|
|
* @return string|null |
73
|
|
|
*/ |
74
|
|
|
public function getForwardHost($proxyHost) |
75
|
|
|
{ |
76
|
|
|
return isset($this->proxyHosts[$proxyHost]) ? |
77
|
|
|
$this->proxyHosts[$proxyHost] : null; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
|
|
public function match($info) |
84
|
|
|
{ |
85
|
|
|
return parent::match($info) |
86
|
|
|
&& (!isset($info['proxyHost']) |
87
|
|
|
|| $this->supportProxyHost($info['proxyHost'])); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
|
|
public function toArray() |
94
|
|
|
{ |
95
|
|
|
return [ |
96
|
|
|
'protocol' => $this->getProtocol(), |
97
|
|
|
'proxyHosts' => $this->proxyHosts, |
98
|
|
|
'serverPort' => $this->serverPort, |
99
|
|
|
'proxyHost' => $this->proxyHost, |
100
|
|
|
]; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
|
|
*/ |
106
|
|
|
public function getProtocol() |
107
|
|
|
{ |
108
|
|
|
return 'http'; |
109
|
|
|
} |
110
|
|
|
} |