Esempio n. 1
0
def main(args):
    tun = Tun(args.device, args.ip, args.peer_ip)
    tun.open()

    if args.mode == 'server':
        server_main(args, tun)
    else:
        client_main(args, tun)
Esempio n. 2
0
File: server.py Progetto: xtao/myvpn
def main(args):
    tun = Tun(device=args.device, ip=args.ip, peer_ip=args.peer_ip)
    tun.open()

    netseg = '.'.join(args.ip.split('.')[:3] + ['0/24'])
    call(['iptables', '-t', 'nat', '-D', 'POSTROUTING', '-s', netseg, '-j', 'MASQUERADE'])
    check_call(['iptables', '-t', 'nat', '-A', 'POSTROUTING', '-s', netseg, '-j', 'MASQUERADE'])

    logger.info("listen at port %d", args.port)
    server = TCPServer(('0.0.0.0', args.port), MyHandlerFactory(tun))
    server.serve_forever()
Esempio n. 3
0
def main(args):
    tun = Tun(device=args.device, ip=args.ip, peer_ip=args.peer_ip)
    tun.open()

    netseg = '.'.join(args.ip.split('.')[:3] + ['0/24'])
    call([
        'iptables', '-t', 'nat', '-D', 'POSTROUTING', '-s', netseg, '-j',
        'MASQUERADE'
    ])
    check_call([
        'iptables', '-t', 'nat', '-A', 'POSTROUTING', '-s', netseg, '-j',
        'MASQUERADE'
    ])

    logger.info("listen at port %d", args.port)
    server = TCPServer(('0.0.0.0', args.port), MyHandlerFactory(tun))
    server.serve_forever()
Esempio n. 4
0
def main(args):
    tun = Tun(device=args.device, ip=args.ip, peer_ip=args.peer_ip)
    tun.open()

    sock = socket()
    server_ip = gethostbyname(args.server)
    logger.info("%s resolved to %s", args.server, server_ip)

    try:
        sock.connect((server_ip, args.port))
        logger.info("connected to %s:%i" % (server_ip, args.port))
        sock.send(MAGIC_WORD)
        data = sock.recv(len(MAGIC_WORD))
        if data != MAGIC_WORD:
            logger.warning("Handshake failed")
            sys.exit(2)

        logger.info("Connection with %s:%i established" %
                    (server_ip, args.port))

        gateway = get_default_gateway()

        if args.down:
            atexit.register(
                on_down,
                args.down,
                server_ip=server_ip,
                restore_gateway=gateway if args.default_gateway else None)

        call(['route', 'delete', server_ip + '/32'])
        check_call(['route', 'add', server_ip + '/32', gateway])

        if args.default_gateway:
            logger.info("set default gateway")
            call(['route', 'delete', 'default'])
            check_call(['route', 'add', 'default', args.peer_ip])

        if args.up:
            logger.info("Run up script")
            check_call(args.up)

        proxy(tun.fd, sock)

    except KeyboardInterrupt:
        logger.warning("Stopped by user")
Esempio n. 5
0
File: client.py Progetto: xtao/myvpn
def main(args):
    tun = Tun(device=args.device, ip=args.ip, peer_ip=args.peer_ip)
    tun.open()

    sock = socket()
    server_ip = gethostbyname(args.server)
    logger.info("%s resolved to %s", args.server, server_ip)

    try:
        sock.connect((server_ip, args.port))
        logger.info("connected to %s:%i" % (server_ip, args.port))
        sock.send(MAGIC_WORD)
        data = sock.recv(len(MAGIC_WORD))
        if data != MAGIC_WORD:
            logger.warning("Handshake failed")
            sys.exit(2)

        logger.info("Connection with %s:%i established" % (server_ip, args.port))

        gateway = get_default_gateway()

        if args.down:
            atexit.register(on_down, args.down,
                            server_ip=server_ip,
                            restore_gateway=gateway if args.default_gateway else None)

        call(['route', 'delete', server_ip+'/32'])
        check_call(['route', 'add', server_ip+'/32', gateway])

        if args.default_gateway:
            logger.info("set default gateway")
            call(['route', 'delete', 'default'])
            check_call(['route', 'add', 'default', args.peer_ip])

        if args.up:
            logger.info("Run up script")
            check_call(args.up)

        proxy(tun.fd, sock)

    except KeyboardInterrupt:
        logger.warning("Stopped by user")