changelog shortlog graph tags branches changeset file revisions annotate raw help

Mercurial > core / lisp/lib/cli/tools/wg.lisp

revision 589: 16a3cdc06cbc
     1.1--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2+++ b/lisp/lib/cli/tools/wg.lisp	Mon Aug 12 21:16:14 2024 -0400
     1.3@@ -0,0 +1,53 @@
     1.4+;;; wg.lisp --- WireGuard Tools
     1.5+
     1.6+;; CLI Access to wg* tools from lisp. Requires the wireguard package.
     1.7+
     1.8+;;; Code:
     1.9+(in-package :cli/tools/wg)
    1.10+(deferror cc-error (simple-error error) ())
    1.11+
    1.12+(defun wg-error (fmt &rest args)
    1.13+  (error 'wg-error :format-arguments args :format-control fmt))
    1.14+
    1.15+(defparameter *wg* (find-exe "wg"))
    1.16+
    1.17+(defun run-wg* (args &optional (output *standard-output*) input)
    1.18+  (let ((proc (if input
    1.19+                  (sb-ext:run-program *wg* (or args nil) :output :stream :input input)
    1.20+                  (sb-ext:run-program *wg* (or args nil) :output :stream))))
    1.21+  (with-open-stream (s (sb-ext:process-output proc))
    1.22+    (loop for l = (read-line s nil nil)
    1.23+          while l
    1.24+          do (write-string l  output)))
    1.25+  (if (eq 0 (sb-ext:process-exit-code proc))
    1.26+      nil
    1.27+      (wg-error "WG command failed: ~A ~A" *wg* (or args "")))))
    1.28+
    1.29+(defun run-wg (&rest args)
    1.30+  (run-wg* args))
    1.31+
    1.32+(defun wg-private-key ()
    1.33+  (with-output-to-string (s)
    1.34+    (run-wg* '("genkey") s)))
    1.35+
    1.36+(defun wg-public-key (private-key)
    1.37+  (with-output-to-string (public-key)
    1.38+    (with-input-from-string (s private-key)
    1.39+      (run-wg* '("pubkey") public-key s))))
    1.40+
    1.41+(defun wg-generate-keys ()
    1.42+  "Generate a wireguard keypair, returning (values PUBLIC-KEY PRIVATE-KEY)."
    1.43+  (let* ((privkey (wg-private-key))
    1.44+         (pubkey (wg-public-key privkey)))
    1.45+    (values pubkey privkey)))
    1.46+
    1.47+
    1.48+(defun wg-generate-key-files (&optional (private "private.key") (public "public.key"))
    1.49+  (multiple-value-bind (pubkey privkey) (wg-generate-keys)
    1.50+    (with-umask #o077
    1.51+      (log:trace! "setting umask to 077")
    1.52+      (with-open-file (f public :direction :output)
    1.53+        (write-line pubkey f))
    1.54+      (with-open-file (f private :direction :output)
    1.55+        (write-line privkey f)))))
    1.56+