|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright 2015 François Kooman <[email protected]>. |
|
4
|
|
|
* |
|
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
6
|
|
|
* you may not use this file except in compliance with the License. |
|
7
|
|
|
* You may obtain a copy of the License at |
|
8
|
|
|
* |
|
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
10
|
|
|
* |
|
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14
|
|
|
* See the License for the specific language governing permissions and |
|
15
|
|
|
* limitations under the License. |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace fkooman\VPN\Server; |
|
19
|
|
|
|
|
20
|
|
|
class Firewall |
|
21
|
|
|
{ |
|
22
|
|
|
private $ipVersion; |
|
23
|
|
|
private $externalIf; |
|
24
|
|
|
private $useNat; |
|
25
|
|
|
private $inputPorts; |
|
26
|
|
|
private $ranges; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct($ipVersion = 4, $externalIf = 'eth0', $useNat = true) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->ipVersion = $ipVersion; |
|
31
|
|
|
$this->externalIf = $externalIf; |
|
32
|
|
|
$this->useNat = $useNat; |
|
33
|
|
|
$this->inputPorts = []; |
|
34
|
|
|
$this->ranges = []; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
private function getNat() |
|
38
|
|
|
{ |
|
39
|
|
|
return [ |
|
40
|
|
|
'*nat', |
|
41
|
|
|
':PREROUTING ACCEPT [0:0]', |
|
42
|
|
|
':OUTPUT ACCEPT [0:0]', |
|
43
|
|
|
':POSTROUTING ACCEPT [0:0]', |
|
44
|
|
|
sprintf('-A POSTROUTING -o %s -j MASQUERADE', $this->externalIf), |
|
45
|
|
|
'COMMIT', |
|
46
|
|
|
]; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
private function getFilter() |
|
50
|
|
|
{ |
|
51
|
|
|
$filter = [ |
|
52
|
|
|
'*filter', |
|
53
|
|
|
':INPUT ACCEPT [0:0]', |
|
54
|
|
|
':FORWARD ACCEPT [0:0]', |
|
55
|
|
|
':OUTPUT ACCEPT [0:0]', |
|
56
|
|
|
'-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT', |
|
57
|
|
|
sprintf('-A INPUT -p %s -j ACCEPT', 4 === $this->ipVersion ? 'icmp' : 'ipv6-icmp'), |
|
58
|
|
|
'-A INPUT -i lo -j ACCEPT', |
|
59
|
|
|
]; |
|
60
|
|
|
|
|
61
|
|
|
foreach ($this->inputPorts as $inputPort) { |
|
62
|
|
|
list($proto, $port) = explode('/', $inputPort); |
|
63
|
|
|
$proto = strtolower($proto); |
|
64
|
|
|
$filter[] = sprintf('-A INPUT -m state --state NEW -m %s -p %s --dport %d -j ACCEPT', $proto, $proto, $port); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$filter[] = sprintf('-A INPUT -j REJECT --reject-with %s', 4 === $this->ipVersion ? 'icmp-host-prohibited' : 'icmp6-adm-prohibited'); |
|
68
|
|
|
|
|
69
|
|
|
$filter = array_merge($filter, $this->getForward()); |
|
70
|
|
|
|
|
71
|
|
|
$filter[] = sprintf('-A FORWARD -j REJECT --reject-with %s', 4 === $this->ipVersion ? 'icmp-host-prohibited' : 'icmp6-adm-prohibited'); |
|
72
|
|
|
$filter[] = 'COMMIT'; |
|
73
|
|
|
|
|
74
|
|
|
return $filter; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
private function getForward() |
|
78
|
|
|
{ |
|
79
|
|
|
$forward = [ |
|
80
|
|
|
'-N vpn', |
|
81
|
|
|
'-A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT', |
|
82
|
|
|
sprintf('-A FORWARD -i tun+ -o %s -j vpn', $this->externalIf), |
|
83
|
|
|
]; |
|
84
|
|
|
|
|
85
|
|
|
foreach ($this->ranges as $r) { |
|
86
|
|
|
$forward = array_merge($forward, $r); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $forward; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function addInputPorts(array $inputPorts) |
|
93
|
|
|
{ |
|
94
|
|
|
$this->inputPorts = $inputPorts; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function addRange($srcNet) |
|
98
|
|
|
{ |
|
99
|
|
|
$range = []; |
|
100
|
|
|
$range[] = sprintf('-A vpn -s %s -j ACCEPT', $srcNet); |
|
101
|
|
|
|
|
102
|
|
|
$this->ranges[] = $range; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function getFirewall() |
|
106
|
|
|
{ |
|
107
|
|
|
$firewall = []; |
|
108
|
|
|
|
|
109
|
|
|
if ($this->useNat) { |
|
110
|
|
|
$firewall = array_merge($firewall, $this->getNat()); |
|
111
|
|
|
} |
|
112
|
|
|
$firewall = array_merge($firewall, $this->getFilter()); |
|
113
|
|
|
|
|
114
|
|
|
return implode(PHP_EOL, $firewall).PHP_EOL; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|