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 ( eac3b8...08138e )
by François
02:37
created

Firewall::clientToClient()   A

Complexity

Conditions 1
Paths 1

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 1
eloc 2
nc 1
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
    private $ranges;
27
    private $enableForward;
28
    private $clientToClient;
29
30
    public function __construct($ipVersion = 4, $externalIf = 'eth0', $useNat = true, $enableForward = true)
31
    {
32
        $this->ipVersion = $ipVersion;
33
        $this->externalIf = $externalIf;
34
        $this->useNat = $useNat;
35
        $this->inputPorts = [];
36
        $this->ranges = [];
37
        $this->enableForward = $enableForward;
38
        $this->clientToClient = false;
39
    }
40
41
    public function clientToClient($clientToClient)
42
    {
43
        $this->clientToClient = (bool) $clientToClient;
44
    }
45
46
    private function getNat()
47
    {
48
        return [
49
            '*nat',
50
            ':PREROUTING ACCEPT [0:0]',
51
            ':OUTPUT ACCEPT [0:0]',
52
            ':POSTROUTING ACCEPT [0:0]',
53
            sprintf('-A POSTROUTING -o %s -j MASQUERADE', $this->externalIf),
54
            'COMMIT',
55
        ];
56
    }
57
58
    private function getFilter()
59
    {
60
        $filter = [
61
            '*filter',
62
            ':INPUT ACCEPT [0:0]',
63
            ':FORWARD ACCEPT [0:0]',
64
            ':OUTPUT ACCEPT [0:0]',
65
            '-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT',
66
            sprintf('-A INPUT -p %s -j ACCEPT', 4 === $this->ipVersion ? 'icmp' : 'ipv6-icmp'),
67
            '-A INPUT -i lo -j ACCEPT',
68
        ];
69
70
        foreach ($this->inputPorts as $inputPort) {
71
            list($proto, $port) = explode('/', $inputPort);
72
            $proto = strtolower($proto);
73
            $filter[] = sprintf('-A INPUT -m state --state NEW -m %s -p %s --dport %d -j ACCEPT', $proto, $proto, $port);
74
        }
75
76
        $filter[] = sprintf('-A INPUT -j REJECT --reject-with %s', 4 === $this->ipVersion ? 'icmp-host-prohibited' : 'icmp6-adm-prohibited');
77
78
        $filter = array_merge($filter, $this->getForward());
79
80
        $filter[] = sprintf('-A FORWARD -j REJECT --reject-with %s', 4 === $this->ipVersion ? 'icmp-host-prohibited' : 'icmp6-adm-prohibited');
81
        $filter[] = 'COMMIT';
82
83
        return $filter;
84
    }
85
86
    private function getForward()
87
    {
88
        $forward = [
89
            '-A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT',
90
        ];
91
92
        if ($this->clientToClient) {
93
            // allow communication between the various tun interfaces
94
            // XXX mention actually only the explicit interface(s) for which 
95
            // forwarding should be enabled
96
            $forward[] = '-A FORWARD -i tun+ -o tun+ -j ACCEPT';
97
        }
98
99
        if (!$this->enableForward) {
100
            // do not allow forwarding to Internet
101
            return $forward;
102
        }
103
104
        $forward[] = '-N vpn';
105
        // XXX mention the explicit tun interface(s)
106
        $forward[] = sprintf('-A FORWARD -i tun+ -o %s -j vpn', $this->externalIf);
107
108
        $forward = array_merge($forward, $this->ranges);
109
110
        return $forward;
111
    }
112
113
    public function addInputPorts(array $inputPorts)
114
    {
115
        $this->inputPorts = $inputPorts;
116
    }
117
118
    public function addRange($srcNet, $dstNets = [])
119
    {
120
        if (0 === count($dstNets)) {
121
            $this->ranges[] = sprintf('-A vpn -s %s -j ACCEPT', $srcNet);
122
        } else {
123
            foreach ($dstNets as $dstNet) {
124
                $this->ranges[] = sprintf('-A vpn -s %s -d %s -j ACCEPT', $srcNet, $dstNet);
125
            }
126
        }
127
    }
128
129
    public function getFirewall()
130
    {
131
        $firewall = [];
132
133
        if ($this->useNat) {
134
            $firewall = array_merge($firewall, $this->getNat());
135
        }
136
        $firewall = array_merge($firewall, $this->getFilter());
137
138
        return implode(PHP_EOL, $firewall).PHP_EOL;
139
    }
140
}
141