File: killswitch.py

package info (click to toggle)
python-proton-vpn-api-core 0.39.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 892 kB
  • sloc: python: 6,582; makefile: 8
file content (99 lines) | stat: -rw-r--r-- 2,919 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""
Module that contains the base class for Kill Switch implementations to extend from.


Copyright (c) 2023 Proton AG

This file is part of Proton VPN.

Proton VPN is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Proton VPN is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with ProtonVPN.  If not, see <https://www.gnu.org/licenses/>.
"""
from __future__ import annotations

from abc import ABC, abstractmethod
from enum import IntEnum
from typing import TYPE_CHECKING, Optional

from proton.loader import Loader

from proton.vpn.killswitch.interface.exceptions import MissingKillSwitchBackendDetails

if TYPE_CHECKING:
    from proton.vpn.connection import VPNServer


class KillSwitchState(IntEnum):  # pylint: disable=missing-class-docstring
    OFF = 0
    ON = 1
    PERMANENT = 2


class KillSwitch(ABC):
    """
    The `KillSwitch` is the base class from which all other kill switch
    backends need to derive from.
    """
    @staticmethod
    def get(class_name: str = None, protocol: str = None) -> KillSwitch:
        """
        Returns the kill switch implementation.

        :param class_name: Name of the class implementing the kill switch. This
        parameter is optional. If it's not provided then the existing implementation
        with the highest priority is returned.
        :param protocol: the kill switch backend to be used based on protocol.
            This is mainly used for backend validation.
        """
        try:
            return Loader.get(
                type_name="killswitch",
                class_name=class_name,
                validate_params={"protocol": protocol}
            )
        except RuntimeError as excp:
            raise MissingKillSwitchBackendDetails(excp) from excp

    @abstractmethod
    async def enable(self, vpn_server: Optional["VPNServer"] = None, permanent: bool = False):
        """
        Enables the kill switch.
        """

    @abstractmethod
    async def disable(self):
        """
        Disables the kill switch.
        """

    @abstractmethod
    async def enable_ipv6_leak_protection(self, permanent: bool = False):
        """
        Enables IPv6 kill switch to prevent leaks.
        """

    @abstractmethod
    async def disable_ipv6_leak_protection(self):
        """
        Disables IPv6 kill switch to prevent leaks.
        """

    @staticmethod
    @abstractmethod
    def _get_priority() -> int:
        pass

    @staticmethod
    @abstractmethod
    def _validate():
        pass