Example #1
0
def populate_common_argument_parser(parser):
    parser.add_argument('--port', type=int, default=DEFAULT_PORT,
                        help="TCP port [default: %(default)s]")
    platform = get_platform()
    default_device = '/dev/tun5' if platform == 'darwin' else '/dev/net/tun'
    parser.add_argument('--device', default=default_device,
                        help="TUN device [default: %(default)s]")
Example #2
0
File: client.py Project: xtao/myvpn
def get_default_gateway():
    platform = get_platform()
    if platform == 'darwin':
        output = getoutput("netstat -nr | grep default | head -n1 | awk '{ print $2 }'")
        gateway = output.strip()
    else:
        output = getoutput("netstat -nr | grep -e '^0.0.0.0' | head -n1 | awk '{ print $2 }'")
        gateway = output.strip()
    return gateway
Example #3
0
def get_default_gateway():
    platform = get_platform()
    if platform == 'darwin':
        output = getoutput(
            "netstat -nr | grep default | head -n1 | awk '{ print $2 }'")
        gateway = output.strip()
    else:
        output = getoutput(
            "netstat -nr | grep -e '^0.0.0.0' | head -n1 | awk '{ print $2 }'")
        gateway = output.strip()
    return gateway
Example #4
0
def populate_argument_parser(parser):
    parser.add_argument('--mode', choices=['server', 'client'],
                        default='client')
    platform = get_platform()
    default_device = '/dev/tun5' if platform == 'darwin' else '/dev/net/tun'
    parser.add_argument('--device', default=default_device, help="TUN device")
    parser.add_argument('--ip', type=ip)
    parser.add_argument('--peer-ip', type=ip)

    server_group = parser.add_argument_group('server mode only')
    server_group.add_argument('-b', '--bind', default='127.0.0.1:2504',
                              help="interface to listen")

    client_group = parser.add_argument_group('client mode only')
    client_group.add_argument('--url', help="server url")
    client_group.add_argument('--default-gateway', action='store_true',
                              help="use vpn as default gateway")
    client_group.add_argument('--up', help="script to run at connection")
    client_group.add_argument('--down', help="script to run at disconnection")
Example #5
0
########NEW FILE########
__FILENAME__ = tun
import os
from fcntl import ioctl
import struct
import logging
from subprocess import check_call

from .utils import get_platform

TUNSETIFF = 0x400454ca
IFF_TUN = 0x0001
IFF_NO_PI = 0x1000

logger = logging.getLogger(__name__)
platform = get_platform()

class Tun(object):
    def __init__(self, device, ip, peer_ip):
        self.device = device
        self.ip = ip
        self.peer_ip = peer_ip

    def open(self):
        self.fd = os.open(self.device, os.O_RDWR)

        if platform == 'linux':
            iface = ioctl(self.fd, TUNSETIFF, struct.pack('16sH', 'tun%d', IFF_TUN|IFF_NO_PI))
            self.ifname = iface[:16].strip('\0')
            check_call(['ifconfig', self.ifname, self.ip, 'pointopoint',
                        self.peer_ip, 'up'])