feat: VPN utils
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
{ config, pkgs, lib, disko, sydnix-cli, ... }:
|
{ config, pkgs, lib, disko, sydnix-cli, ... }:
|
||||||
|
|
||||||
{
|
{
|
||||||
imports = [
|
imports = [
|
||||||
./hardware.nix
|
./hardware.nix
|
||||||
@@ -103,6 +104,7 @@
|
|||||||
waypipe
|
waypipe
|
||||||
sydnix-cli.packages.x86_64-linux.default
|
sydnix-cli.packages.x86_64-linux.default
|
||||||
(import ../../scripts/port-tools { inherit pkgs; })
|
(import ../../scripts/port-tools { inherit pkgs; })
|
||||||
|
(import ../../scripts/vpn-tools { inherit pkgs; })
|
||||||
];
|
];
|
||||||
|
|
||||||
services.openssh = {
|
services.openssh = {
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
/nix/store/vnfvgwnkx6jf2cmla3lsmj3kpnxmyv7k-ports
|
|
||||||
1
scripts/vpn-tools/.envrc
Normal file
1
scripts/vpn-tools/.envrc
Normal file
@@ -0,0 +1 @@
|
|||||||
|
use nix
|
||||||
0
scripts/vpn-tools/bb.edn
Normal file
0
scripts/vpn-tools/bb.edn
Normal file
7
scripts/vpn-tools/default.nix
Normal file
7
scripts/vpn-tools/default.nix
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
{ pkgs ? import <nixpkgs> {} }:
|
||||||
|
|
||||||
|
pkgs.writeShellScriptBin "vpn" ''
|
||||||
|
exit=0
|
||||||
|
${pkgs.babashka}/bin/bb --init "${./vpn}" -m vpn-tools -- "$@" || exit=$?
|
||||||
|
exit $exit
|
||||||
|
''
|
||||||
7
scripts/vpn-tools/shell.nix
Normal file
7
scripts/vpn-tools/shell.nix
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
{ pkgs ? import <nixpkgs> {} }:
|
||||||
|
|
||||||
|
pkgs.mkShell {
|
||||||
|
packages = [
|
||||||
|
pkgs.babashka
|
||||||
|
];
|
||||||
|
}
|
||||||
58
scripts/vpn-tools/vpn
Executable file
58
scripts/vpn-tools/vpn
Executable file
@@ -0,0 +1,58 @@
|
|||||||
|
#!/usr/bin/env sh
|
||||||
|
:$(); # -*- mode: clojure -*-
|
||||||
|
:$(); exit=0
|
||||||
|
:$(); bb --init "$0" -- "$@" || exit=$?
|
||||||
|
:$(); exit $exit
|
||||||
|
(ns vpn-tools
|
||||||
|
(:require [babashka.cli :as cli]
|
||||||
|
[babashka.process :as p]
|
||||||
|
[babashka.http-client :as http])
|
||||||
|
(:import [clojure.lang ExceptionInfo]))
|
||||||
|
|
||||||
|
(defn adorn-with-help-option [spec]
|
||||||
|
(letfn [(fn-with-help [opts]
|
||||||
|
;; TODO: Implement
|
||||||
|
((:fn spec) opts))]
|
||||||
|
(-> spec
|
||||||
|
(assoc-in [:spec :help]
|
||||||
|
{:help {:coerce :bool}})
|
||||||
|
(assoc :fn fn-with-help))))
|
||||||
|
|
||||||
|
(defn help [opts]
|
||||||
|
(prn 'help))
|
||||||
|
|
||||||
|
(defn vpn-shell [opts]
|
||||||
|
(let [cmd (or (:args opts) ["bash"])
|
||||||
|
user (System/getenv "USER")
|
||||||
|
namespace (-> opts :opts :namespace)
|
||||||
|
ip-cmd (concat ["sudo" "ip" "netns" "exec"
|
||||||
|
namespace "sudo" "-u" user]
|
||||||
|
cmd)]
|
||||||
|
(apply println "$" ip-cmd)
|
||||||
|
(-> (apply p/shell ip-cmd)
|
||||||
|
:exit System/exit)))
|
||||||
|
|
||||||
|
(defn test-mullvad [_opts]
|
||||||
|
(p/shell "curl https://am.i.mullvad.net/connected"))
|
||||||
|
|
||||||
|
(def namespace-option
|
||||||
|
{:namespace {:coerce :string
|
||||||
|
:alias :n
|
||||||
|
:default "wg"
|
||||||
|
:ref "NAMESPACE"}})
|
||||||
|
|
||||||
|
(def cli-table
|
||||||
|
(map adorn-with-help-option
|
||||||
|
[{:cmds ["shell"]
|
||||||
|
:fn vpn-shell
|
||||||
|
:spec namespace-option}
|
||||||
|
{:cmds ["test-mullvad"]
|
||||||
|
:fn test-mullvad}
|
||||||
|
{:cmds []
|
||||||
|
:fn help}]))
|
||||||
|
|
||||||
|
(defn -main [& args]
|
||||||
|
(cli/dispatch cli-table args))
|
||||||
|
|
||||||
|
#_
|
||||||
|
(apply -main *command-line-args*)
|
||||||
Reference in New Issue
Block a user