GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 558a0e...4914c9 )
by François
02:13
created

Firewall::getFamily()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 1
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
27
    public function __construct($ipVersion = 4, $externalIf = 'eth0', $useNat = true)
28
    {
29
        $this->ipVersion = $ipVersion;
30
        $this->externalIf = $externalIf;
31
        $this->useNat = $useNat;
32
        $this->inputPorts = [];
33
        $this->pool = [];
0 ignored issues
show
Bug introduced by
The property pool does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
34
    }
35
36
    private function getNat()
37
    {
38
        return [
39
            '*nat',
40
            ':PREROUTING ACCEPT [0:0]',
41
            ':OUTPUT ACCEPT [0:0]',
42
            ':POSTROUTING ACCEPT [0:0]',
43
            sprintf('-A POSTROUTING -i tun+ -o %s -j MASQUERADE', $this->externalIf),
44
            'COMMIT',
45
        ];
46
    }
47
48
    private function getFilter()
49
    {
50
        $filter = [
51
            '*filter',
52
            ':INPUT ACCEPT [0:0]',
53
            ':FORWARD ACCEPT [0:0]',
54
            ':OUTPUT ACCEPT [0:0]',
55
            '-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT',
56
            sprintf('-A INPUT -p %s -j ACCEPT', 4 === $this->ipVersion ? 'icmp' : 'ipv6-icmp'),
57
            '-A INPUT -i lo -j ACCEPT',
58
        ];
59
60
        foreach ($this->inputPorts as $inputPort) {
61
            list($proto, $port) = explode('/', $inputPort);
62
            $proto = strtolower($proto);
63
            $filter[] = sprintf('-A INPUT -m state --state NEW -m %s -p %s --dport %d -j ACCEPT', $proto, $proto, $port);
64
        }
65
66
        $filter[] = sprintf('-A INPUT -j REJECT --reject-with %s', 4 === $this->ipVersion ? 'icmp-host-prohibited' : 'icmp6-adm-prohibited');
67
68
        $filter = array_merge($filter, $this->getForward());
69
70
        $filter[] = sprintf('-A FORWARD -j REJECT --reject-with %s', 4 === $this->ipVersion ? 'icmp-host-prohibited' : 'icmp6-adm-prohibited');
71
        $filter[] = 'COMMIT';
72
73
        return $filter;
74
    }
75
76
    private function getForward()
77
    {
78
        $forward = [
79
            '-N vpn',
80
            sprintf('-A FORWARD -i tun+ -o %s -j vpn', $this->externalIf),
81
            sprintf('-A vpn -p %s -j ACCEPT', 4 === $this->ipVersion ? 'icmp' : 'ipv6-icmp'),
82
            '-A vpn -m state --state ESTABLISHED,RELATED -j ACCEPT',
83
            '-A vpn -m udp -p udp --dport 53 -j ACCEPT',
84
            '-A vpn -m tcp -p tcp --dport 53 -j ACCEPT',
85
        ];
86
87
        foreach ($this->pool as $p) {
88
            $forward = array_merge($forward, $p);
89
        }
90
91
        return $forward;
92
    }
93
94
    public function addInputPorts(array $inputPorts)
95
    {
96
        $this->inputPorts = $inputPorts;
97
    }
98
99
    public function addPool($poolId, $srcNet, array $dstNets = [], array $dstPorts = [])
100
    {
101
        $pool = [];
102
        $pool[] = sprintf('-N %s', $poolId);
103
        $pool[] = sprintf('-A vpn -s %s -j %s', $srcNet, $poolId);
104
105
        // add rules
106
        foreach ($dstNets as $dstNet) {
107
            if ($this->ipVersion === self::getFamily($dstNet)) {
108
                // only include dstNet if it matches FW type 
109
                continue;
110
            }
111
112
            if (0 !== count($dstPorts)) {
113
                foreach ($dstPorts as $dstPort) {
114
                    list($protocol, $port) = explode('/', $dstPort);
115
                    $pool[] = sprintf('-A %s -d %s -m %s -p %s --dport %d -j ACCEPT', $poolId, $dstNet, strtolower($protocol), strtolower($protocol), $port);
116
                }
117
            } else {
118
                $pool[] = sprintf('-A %s -d %s -j ACCEPT', $poolId, $dstNet);
119
            }
120
        }
121
122
        $this->pool[] = $pool;
123
    }
124
125
    private static function getFamily($dstNet)
126
    {
127
        return false !== strpos($dstNet, ':') ? 4 : 6;
128
    }
129
130
    public function getFirewall()
131
    {
132
        $firewall = [];
133
134
        if ($this->useNat) {
135
            $firewall = array_merge($firewall, $this->getNat());
136
        }
137
        $firewall = array_merge($firewall, $this->getFilter());
138
139
        return implode(PHP_EOL, $firewall);
140
    }
141
}
142