From 7634edc3381bbea5a2035331e70a091bdeccf822 Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Wed, 21 Feb 2018 07:31:13 +0100 Subject: [PATCH 1/2] Parse parameters and flags with lua_cliargs --- README.md | 4 ++-- frivpn_client.lua | 22 ++++++++++++++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 2a13f2f..1d432aa 100644 --- a/README.md +++ b/README.md @@ -23,13 +23,13 @@ in the best possible VPN bandwidth and throughput. ### Debian (stretch) ``` -# apt install build-essential lua5.2 lua5.2-dev lua-posix lua-luaossl lua-cqueues libssl-dev liblzo2-dev +# apt install build-essential lua5.2 lua5.2-dev lua-cliargs lua-posix lua-luaossl lua-cqueues libssl-dev liblzo2-dev ``` ### Ubuntu 17.10 ``` -# apt install build-essential lua liblua5.2-dev lua-posix lua-luaossl lua-cqueues libssl-dev liblzo2-dev +# apt install build-essential lua liblua5.2-dev lua-cliargs lua-posix lua-luaossl lua-cqueues libssl-dev liblzo2-dev ``` ## Build it diff --git a/frivpn_client.lua b/frivpn_client.lua index adac912..d6f843b 100755 --- a/frivpn_client.lua +++ b/frivpn_client.lua @@ -12,6 +12,7 @@ local x509 = require "openssl.x509" local castore = require "openssl.x509.store" local pkey = require "openssl.pkey" local posix = require "posix" +local cli = require "cliargs" require "libfrivpn" require "utils" @@ -464,6 +465,11 @@ function vpn:connect(host, port) end end +local function print_version() + print("frivpn_client: version 0.0.1") + os.exit(0) +end + function ssl_preload() local a, b = csock.pair() -- do a dummy checktls so cqueues socket loads & keeps the ossl module @@ -475,13 +481,25 @@ end ssl_preload() +-- cliargs handling +cli:set_name("frivpn_client") +cli:argument("CONFIG", "config module to load (e.g. configs/ipredator)") +cli:flag("-v, --version", "print frivpn's version", print_version) +cli:flag("--notun", "creates no tun device") + +local args, err = cli:parse(arg) +if not args and err then + print(string.format('%s: %s', cli.name, err)) + os.exit(1) +end + -- load config -local config = require(arg[1]) +local config = require(args["CONFIG"]) config.port = config.port or 1195 local tun = nil -if arg[2] ~= "notun" then +if not args["notun"] then local status, tun_fd, tun_dev = pcall(tun_create) if status then local tun_pipe, tun_pid = tun_fork(tun_dev, config.netmask, config.on_connected) From e22a8b94dbb0da1b73d87a92aaccaf1b0fd22b6d Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Wed, 21 Feb 2018 08:04:57 +0100 Subject: [PATCH 2/2] Handle invalid config module parameter a bit more gracefully --- frivpn_client.lua | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/frivpn_client.lua b/frivpn_client.lua index d6f843b..5822be9 100755 --- a/frivpn_client.lua +++ b/frivpn_client.lua @@ -66,6 +66,14 @@ local syscalls = { } +function prequire(...) + local status, lib = pcall(require, ...) + if status then + return lib + end + return nil +end + function lookup_ip(host) local res = posix.getaddrinfo(host, 1195) return res[1].addr @@ -494,9 +502,13 @@ if not args and err then end -- load config -local config = require(args["CONFIG"]) -config.port = config.port or 1195 +local config = prequire(args["CONFIG"]) +if not config then + print(string.format("Could not load config module %s", arg[1])) + os.exit(1) +end +config.port = config.port or 1195 local tun = nil if not args["notun"] then