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 ( 12b4db...b4b56f )
by François
02:27
created

Firewall::getNat()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 25
rs 8.5806
cc 4
eloc 15
nc 4
nop 2
1
<?php
2
/**
3
 * Copyright 2016 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
    public static function getFirewall4(Pools $p, $disableForward = false, $asArray = false)
23
    {
24
        return self::getFirewall($p, 4, $disableForward, $asArray);
25
    }
26
27
    public static function getFirewall6(Pools $p, $disableForward = false, $asArray = false)
28
    {
29
        return self::getFirewall($p, 6, $disableForward, $asArray);
30
    }
31
32
    private static function getFirewall(Pools $p, $inetFamily, $disableForward, $asArray)
33
    {
34
        $firewall = [];
35
36
        // NAT
37
        $firewall = array_merge($firewall, self::getNat($p, $inetFamily));
38
39
        // FILTER
40
        $firewall = array_merge($firewall, self::getFilter($p, $inetFamily, $disableForward));
41
42
        if ($asArray) {
43
            return $firewall;
44
        }
45
46
        return implode(PHP_EOL, $firewall).PHP_EOL;
47
    }
48
49
    private static function getNat(Pools $p, $inetFamily)
50
    {
51
        $nat = [
52
            '*nat',
53
            ':PREROUTING ACCEPT [0:0]',
54
            ':OUTPUT ACCEPT [0:0]',
55
            ':POSTROUTING ACCEPT [0:0]',
56
        ];
57
58
        foreach ($p as $pool) {
59
            if ($pool->getUseNat()) {
60
                if (4 === $inetFamily) {
61
                    // get the IPv4 range
62
                    $srcNet = $pool->getRange()->getAddressPrefix();
63
                } else {
64
                    // get the IPv6 range
65
                    $srcNet = $pool->getRange6()->getAddressPrefix();
66
                }
67
                $nat[] = sprintf('-A POSTROUTING -s %s -o %s -j MASQUERADE', $srcNet, $pool->getExtIf());
68
            }
69
        }
70
        $nat[] = 'COMMIT';
71
72
        return $nat;
73
    }
74
75
    private static function getFilter(Pools $p, $inetFamily, $disableForward)
76
    {
77
        $filter = [
78
            '*filter',
79
            ':INPUT ACCEPT [0:0]',
80
            ':FORWARD ACCEPT [0:0]',
81
            ':OUTPUT ACCEPT [0:0]',
82
        ];
83
84
        // INPUT
85
        $filter = array_merge($filter, self::getInputChain($p, $inetFamily));
86
87
        // FORWARD
88
        $filter = array_merge($filter, self::getForwardChain($p, $inetFamily, $disableForward));
89
90
        $filter[] = 'COMMIT';
91
92
        return $filter;
93
    }
94
95
    private static function getInputChain(Pools $p, $inetFamily)
96
    {
97
        $inputChain = [
98
            '-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT',
99
            sprintf('-A INPUT -p %s -j ACCEPT', 4 === $inetFamily ? 'icmp' : 'ipv6-icmp'),
100
            '-A INPUT -i lo -j ACCEPT',
101
        ];
102
103
        $inputPorts = self::getIngressPorts($p);
104
        foreach ($inputPorts as $inputPort) {
105
            list($proto, $port) = explode('/', $inputPort);
106
            $inputChain[] = sprintf('-A INPUT -m state --state NEW -m %s -p %s --dport %d -j ACCEPT', $proto, $proto, $port);
107
        }
108
109
        $inputChain[] = sprintf('-A INPUT -j REJECT --reject-with %s', 4 === $inetFamily ? 'icmp-host-prohibited' : 'icmp6-adm-prohibited');
110
111
        return $inputChain;
112
    }
113
114
    private static function getForwardChain(Pools $p, $inetFamily, $disableForward)
115
    {
116
        $forwardChain = [
117
            '-A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT',
118
        ];
119
120
        if (!$disableForward) {
121
            foreach ($p as $pool) {
122
                if (4 === $inetFamily) {
123
                    // get the IPv4 range
124
                    $srcNet = $pool->getRange()->getAddressPrefix();
125
                } else {
126
                    // get the IPv6 range
127
                    $srcNet = $pool->getRange6()->getAddressPrefix();
128
                }
129
                $forwardChain[] = sprintf('-N vpn-%s', $pool->getId());
130
                $forwardChain[] = sprintf('-A FORWARD -i tun-%s+ -s %s -j vpn-%s', $pool->getId(), $srcNet, $pool->getId());
131
                if ($pool->getClientToClient()) {
132
                    // allow client-to-client
133
                    $forwardChain[] = sprintf('-A vpn-%s -o tun-%s+ -d %s -j ACCEPT', $pool->getId(), $pool->getId(), $srcNet);
134
                }
135
                if ($pool->getDefaultGateway()) {
136
                    // allow all traffic to the external interface
137
                    $forwardChain[] = sprintf('-A vpn-%s -o %s -j ACCEPT', $pool->getId(), $pool->getExtIf(), $srcNet);
138
                } else {
139
                    // only allow certain traffic to the external interface
140
                    foreach ($pool->getRoutes() as $route) {
141
                        if ($inetFamily === $route->getFamily()) {
142
                            $forwardChain[] = sprintf('-A vpn-%s -o %s -d %s -j ACCEPT', $pool->getId(), $pool->getExtIf(), $route->getAddressPrefix());
143
                        }
144
                    }
145
                }
146
            }
147
        }
148
149
        $forwardChain[] = sprintf('-A FORWARD -j REJECT --reject-with %s', 4 === $inetFamily ? 'icmp-host-prohibited' : 'icmp6-adm-prohibited');
150
151
        return $forwardChain;
152
    }
153
154
    private static function getIngressPorts(Pools $p)
155
    {
156
        $ingressPorts = ['tcp/22', 'tcp/80', 'tcp/443'];
157
158
        // we only care about additional UDP ports, as we only want UDP and 
159
        // fallback to tcp/443
160
        foreach ($p as $pool) {
161
            foreach ($pool->getInstances() as $instance) {
162
                if ('udp' === $instance->getProto()) {
163
                    $port = sprintf('udp/%d', $instance->getPort());
164
                    if (!in_array($port, $ingressPorts)) {
165
                        $ingressPorts[] = $port;
166
                    }
167
                }
168
            }
169
        }
170
171
        return $ingressPorts;
172
    }
173
}
174