changelog shortlog graph tags branches changeset files revisions annotate raw help

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
2 
3 ;; CLI Access to wg* tools from lisp. Requires the wireguard package.
4 
5 ;;; Code:
6 (in-package :cli/tools/wg)
7 (deferror cc-error (simple-error error) ())
8 
9 (defun wg-error (fmt &rest args)
10  (error 'wg-error :format-arguments args :format-control fmt))
11 
12 (defparameter *wg* (find-exe "wg"))
13 
14 (defun run-wg* (args &optional (output *standard-output*) input)
15  (let ((proc (if 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)
20  while l
21  do (write-string l output)))
22  (if (eq 0 (sb-ext:process-exit-code proc))
23  nil
24  (wg-error "WG command failed: ~A ~A" *wg* (or args "")))))
25 
26 (defun run-wg (&rest args)
27  (run-wg* args))
28 
29 (defun wg-private-key ()
30  (with-output-to-string (s)
31  (run-wg* '("genkey") s)))
32 
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))))
37 
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)))
43 
44 
45 (defun wg-generate-key-files (&optional (private "private.key") (public "public.key"))
46  (multiple-value-bind (pubkey privkey) (wg-generate-keys)
47  (with-umask #o077
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)))))
53