Skip to content

Commit

Permalink
Some operating systems (including Linux) need to check/prepare the tu…
Browse files Browse the repository at this point in the history
…nnel device

So restore that behavior of `reason=pre-init` by creating a
new `TunnelPrepProvider` class.
  • Loading branch information
dlenski committed May 26, 2019
1 parent ca0e574 commit b6bea08
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
5 changes: 5 additions & 0 deletions vpn_slice/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ def configure_firewall(self, device):

def deconfigure_firewall(self, device):
pass


class NoTunnelCheckProvider(FirewallProvider):
def prepare_tunnel(self):
pass
8 changes: 7 additions & 1 deletion vpn_slice/linux.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import subprocess

from .provider import FirewallProvider, ProcessProvider, RouteProvider
from .provider import FirewallProvider, ProcessProvider, RouteProvider, TunnelPrepProvider
from .util import get_executable


Expand Down Expand Up @@ -84,3 +84,9 @@ def configure_firewall(self, device):
def deconfigure_firewall(self, device):
self._iptables('-D', 'INPUT', '-i', device, '-j', 'DROP')
self._iptables('-D', 'INPUT', '-i', device, '-m', 'state', '--state', 'RELATED,ESTABLISHED', '-j', 'ACCEPT')


class CheckTunDevProvider(TunnelPrepProvider):
def prepare_tunnel(self):
if not os.access('/dev/net/tun', os.R_OK | os.W_OK):
raise OSError("can't read and write /dev/net/tun")
8 changes: 5 additions & 3 deletions vpn_slice/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,27 @@

def get_default_providers():
if platform.startswith('linux'):
from .linux import ProcfsProvider, Iproute2Provider, IptablesProvider
from .linux import ProcfsProvider, Iproute2Provider, IptablesProvider, CheckTunDevProvider
from .posix import DigProvider, PosixHostsFileProvider
return {
'process': ProcfsProvider(),
'route': Iproute2Provider(),
'firewall': IptablesProvider(),
'dns': DigProvider(),
'hosts': PosixHostsFileProvider(),
'prep': CheckTunDevProvider(),
}
elif platform.startswith('darwin'):
from .mac import PsProvider, BSDRouteProvider
from .generic import NoFirewallProvider
from .generic import NoFirewallProvider, NoTunnelPrepProvider
from .posix import DigProvider, PosixHostsFileProvider
return {
'process': PsProvider(),
'route': BSDRouteProvider(),
'firewall': NoFirewallProvider(),
'dns': DigProvider(),
'hosts': PosixHostsFileProvider(),
'prep': NoTunnelPrepProvider(),
}
else:
raise OSError('Your platform, {}, is unsupported'.format(platform))
Expand Down Expand Up @@ -68,7 +70,7 @@ def names_for(host, domains, short=True, long=True):
########################################

def do_pre_init(env, args, providers):
pass
providers['prep'].prepare_tunnel()

def do_disconnect(env, args, providers):
for pidfile in args.kill:
Expand Down
8 changes: 8 additions & 0 deletions vpn_slice/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,11 @@ def write_hosts(self, host_map, name):
host_map maps IP addresses to host names, like the hosts file expects.
"""

class TunnelPrepProvider:
def prepare_tunnel(self):
"""Prepare operating system to create tunnel devices.
Base class behavior is to do nothing.
"""

0 comments on commit b6bea08

Please sign in to comment.