Mercurial > core / lisp/lib/cli/tools/wg.lisp
changeset 698: |
96958d3eb5b0 |
parent: |
16a3cdc06cbc
|
author: |
Richard Westhaver <ellis@rwest.io> |
date: |
Fri, 04 Oct 2024 22:04:59 -0400 |
permissions: |
-rw-r--r-- |
description: |
fixes |
1 ;;; wg.lisp --- WireGuard Tools 3 ;; CLI Access to wg* tools from lisp. Requires the wireguard package. 6 (in-package :cli/tools/wg) 7 (deferror cc-error (simple-error error) ()) 9 (defun wg-error (fmt &rest args) 10 (error 'wg-error :format-arguments args :format-control fmt)) 12 (defparameter *wg* (find-exe "wg")) 14 (defun run-wg* (args &optional (output *standard-output*) input) 16 (sb-ext:run-program *wg* (or args nil) :output :stream :input input) 17 (sb-ext:run-program *wg* (or args nil) :output :stream)))) 18 (with-open-stream (s (sb-ext:process-output proc)) 19 (loop for l = (read-line s nil nil) 21 do (write-string l output))) 22 (if (eq 0 (sb-ext:process-exit-code proc)) 24 (wg-error "WG command failed: ~A ~A" *wg* (or args ""))))) 26 (defun run-wg (&rest args) 29 (defun wg-private-key () 30 (with-output-to-string (s) 31 (run-wg* '("genkey") s))) 33 (defun wg-public-key (private-key) 34 (with-output-to-string (public-key) 35 (with-input-from-string (s private-key) 36 (run-wg* '("pubkey") public-key s)))) 38 (defun wg-generate-keys () 39 "Generate a wireguard keypair, returning (values PUBLIC-KEY PRIVATE-KEY)." 40 (let* ((privkey (wg-private-key)) 41 (pubkey (wg-public-key privkey))) 42 (values pubkey privkey))) 45 (defun wg-generate-key-files (&optional (private "private.key") (public "public.key")) 46 (multiple-value-bind (pubkey privkey) (wg-generate-keys) 48 (log:trace! "setting umask to 077") 49 (with-open-file (f public :direction :output) 50 (write-line pubkey f)) 51 (with-open-file (f private :direction :output) 52 (write-line privkey f)))))