commit 163bcfb563b32c105a6d614e1044bdbda727b217 parent 582717856293d0f97846649284220b2f1b997095 Author: Vineet Kumar <git@vineetk.net> Date: Tue, 17 Nov 2026 02:33:32 +0100 services/vpn: add amneziawg-service-type based on wireguard-service-type Diffstat:
| A | epistemia/services/vpn.scm | | | 421 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 421 insertions(+), 0 deletions(-)
diff --git a/epistemia/services/vpn.scm b/epistemia/services/vpn.scm @@ -0,0 +1,421 @@ +(define-module (epistemia services vpn) + #:use-module (srfi srfi-1) + #:use-module (gnu services) + #:use-module (gnu services shepherd) + #:use-module (guix gexp) + #:use-module (guix records) + #:use-module (gnu packages vpn) + #:use-module (epistemia packages vpn) + #:export (amneziawg-service-type + amneziawg-configuration + amneziawg-peer)) + +;;; +;;; AmneziaWG. +;;; Pretty much copied wholesale from gnu/services/vpn.scm's Wireguard +;;; service, with some Amnezia-specific additions. +;;; + +(define-record-type* <amneziawg-peer> + amneziawg-peer make-amneziawg-peer + amneziawg-peer? + (name amneziawg-peer-name) + (endpoint amneziawg-peer-endpoint + (default #f)) ;string + (public-key amneziawg-peer-public-key) ;string + (preshared-key amneziawg-peer-preshared-key + (default #f)) ;string + (allowed-ips amneziawg-peer-allowed-ips) ;list of strings + (keep-alive amneziawg-peer-keep-alive + (default #f))) ;integer + +(define-record-type* <amneziawg-configuration> + amneziawg-configuration make-amneziawg-configuration + amneziawg-configuration? + (amneziawg amneziawg-configuration-amneziawg ;file-like + (default amneziawg-tools)) + (interface amneziawg-configuration-interface ;string + (default "wg0")) + (addresses amneziawg-configuration-addresses ;string + (default '("10.0.0.1/32"))) + (port amneziawg-configuration-port ;integer + (default 51820)) + (private-key amneziawg-configuration-private-key ;maybe-string + (default "/etc/amneziawg/private.key")) + (bootstrap-private-key? amneziawg-configuration-bootstrap-private-key? ;boolean + (default #t)) + (peers amneziawg-configuration-peers ;list of <wiregard-peer> + (default '())) + (dns amneziawg-configuration-dns ;list of strings + (default '())) + (mtu amneziawg-configuration-mtu ;integer + (default #f)) + (monitor-ips? amneziawg-configuration-monitor-ips? ;boolean + (default #f)) + (monitor-ips-interval amneziawg-configuration-monitor-ips-interval + (default "*/5 * * * *")) ;string | list + (pre-up amneziawg-configuration-pre-up ;list of strings + (default '())) + (post-up amneziawg-configuration-post-up ;list of strings + (default '())) + (pre-down amneziawg-configuration-pre-down ;list of strings + (default '())) + (post-down amneziawg-configuration-post-down ;list of strings + (default '())) + (shepherd-requirement amneziawg-configuration-shepherd-requirement ; list of symbols + (default '())) + (table amneziawg-configuration-table ;string + (default "auto")) + (auto-start? amneziawg-configuration-auto-start? ;boolean + (default #t)) + ;; amneziawg-specific additions + (header-protection-key amneziawg-configuration-header-protection-key ;maybe-string + (default "/etc/amneziawg/header.key")) + + (h1 amneziawg-configuration-h1 ;integer + (default 1)) + (h2 amneziawg-configuration-h2 ;integer + (default 2)) + (h3 amneziawg-configuration-h3 ;integer + (default 3)) + (h4 amneziawg-configuration-h4 ;integer + (default 4)) + + (s1 amneziawg-configuration-s1 ;integer + (default 20)) + (s2 amneziawg-configuration-s2 ;integer + (default 20)) + (s3 amneziawg-configuration-s3 ;integer + (default 20)) + (s4 amneziawg-configuration-s4 ;integer + (default 20)) + + (content-padding-addition amneziawg-configuration-content-padding-addition ;string + (default "16-64")) + + ;; Optional pre-handshake obfuscation. + (jc amneziawg-configuration-jc ;maybe-integer + (default #f)) + (jmin amneziawg-configuration-jmin ;maybe-integer + (default #f)) + (jmax amneziawg-configuration-jmax ;maybe-integer + (default #f)) + + (i1 amneziawg-configuration-i1 ;maybe-string + (default #f)) + (i2 amneziawg-configuration-i2 ;maybe-string + (default #f)) + (i3 amneziawg-configuration-i3 ;maybe-string + (default #f)) + (i4 amneziawg-configuration-i4 ;maybe-string + (default #f)) + (i5 amneziawg-configuration-i5 ;maybe-string + (default #f))) + +(define (amneziawg-configuration-file config) + (define (peer->config peer) + (match-record peer <amneziawg-peer> + (name public-key endpoint allowed-ips keep-alive) + (let ((lines (list + (format #f "[Peer] #~a" name) + (format #f "PublicKey = ~a" public-key) + (format #f "AllowedIPs = ~{~a~^, ~}" allowed-ips) + (format #f "~@[Endpoint = ~a~]" endpoint) + (format #f "~@[PersistentKeepalive = ~a~]" keep-alive)))) + (string-join (remove string-null? lines) "\n")))) + + (define (peers->preshared-keys peer keys) + (let ((public-key (amneziawg-peer-public-key peer)) + (preshared-key (amneziawg-peer-preshared-key peer))) + (if preshared-key + (cons* public-key preshared-key keys) + keys))) + + (match-record config <amneziawg-configuration> + (amneziawg interface addresses port private-key peers dns mtu + pre-up post-up pre-down post-down table + header-protection-key + h1 h2 h3 h4 + s1 s2 s3 s4 + content-padding-addition + jc jmin jmax + i1 i2 i3 i4 i5) + (let* ((config-file (string-append interface ".conf")) + (peer-keys (fold peers->preshared-keys (list) peers)) + (peers (map peer->config peers)) + (config + (computed-file + "amneziawg-config" + #~(begin + (use-modules (ice-9 format) + (srfi srfi-1)) + + (define lines + (list + "[Interface]" + (if (null? '#$addresses) + "" + (format #f "Address = ~{~a~^, ~}" + (list #$@addresses))) + (format #f "~@[Table = ~a~]" #$table) + (if (null? '#$pre-up) + "" + (format #f "~{PreUp = ~a~%~}" (list #$@pre-up))) + (if #$private-key + (format #f "PostUp = ~a set %i private-key ~a\ +~{ peer ~a preshared-key ~a~}" + #$(file-append amneziawg "/bin/awg") + #$private-key (list #$@peer-keys)) + "") + (if #$header-protection-key + (format #f "PostUp = ~a set %i header-protection-key ~a" + #$(file-append amneziawg "/bin/awg") + #$header-protection-key) + "") + (if (null? '#$post-up) + "" + (format #f "~{PostUp = ~a~%~}" (list #$@post-up))) + (if (null? '#$pre-down) + "" + (format #f "~{PreDown = ~a~%~}" (list #$@pre-down))) + (if (null? '#$post-down) + "" + (format #f "~{PostDown = ~a~%~}" (list #$@post-down))) + (format #f "~@[ListenPort = ~a~]" #$port) + (if (null? '#$dns) + "" + (format #f "DNS = ~{~a~^, ~}" (list #$@dns))) + (if (not #$mtu) + "" + (format #f "MTU = ~a" #$mtu)) + + ;; amnezia-specific + (format #f "H1 = ~a" #$h1) + (format #f "H2 = ~a" #$h2) + (format #f "H3 = ~a" #$h3) + (format #f "H4 = ~a" #$h4) + + (format #f "S1 = ~a" #$s1) + (format #f "S2 = ~a" #$s2) + (format #f "S3 = ~a" #$s3) + (format #f "S4 = ~a" #$s4) + + (format #f "~@[ContentPaddingAddition = ~a~] " + #$content-padding-addition) + + (format #f "~@[Jc = ~a~]" #$jc) + (format #f "~@[Jmin = ~a~]" #$jmin) + (format #f "~@[Jmax = ~a~]" #$jmax) + + (format #f "~@[I1 = ~a~]" #$i1) + (format #f "~@[I2 = ~a~]" #$i2) + (format #f "~@[I3 = ~a~]" #$i3) + (format #f "~@[I4 = ~a~]" #$i4) + (format #f "~@[I5 = ~a~]" #$i5))) + + (mkdir #$output) + (chdir #$output) + (call-with-output-file #$config-file + (lambda (port) + (format port "~a~%~%~{~a~%~^~%~}" + (string-join (remove string-null? lines) "\n") + '#$peers))))))) + (file-append config "/" config-file)))) + +(define (amneziawg-activation config) + (match-record config <amneziawg-configuration> + (private-key bootstrap-private-key? amneziawg) + #~(begin + (use-modules (guix build utils) + (ice-9 popen) + (ice-9 rdelim)) + (when (and #$private-key + #$bootstrap-private-key?) + (mkdir-p (dirname #$private-key)) + (unless (file-exists? #$private-key) + (let* ((pipe + (open-input-pipe (string-append + #$(file-append amneziawg "/bin/awg") + " genkey"))) + (key (read-line pipe))) + (call-with-output-file #$private-key + (lambda (port) + (display key port))) + (chmod #$private-key #o400) + (close-pipe pipe))))))) + +;;; XXX: Copied from (guix scripts pack), changing define to define*. +(define-syntax-rule (define-with-source (variable args ...) body body* ...) + "Bind VARIABLE to a procedure accepting ARGS defined as BODY, also setting +its source property." + (begin + (define* (variable args ...) + body body* ...) + (eval-when (load eval) + (set-procedure-property! variable 'source + '(define* (variable args ...) body body* ...))))) + +(define (amneziawg-service-name interface) + "Return the Amneziawg service name (a symbol) configured to use INTERFACE." + (symbol-append 'amneziawg- (string->symbol interface))) + +(define-with-source (strip-port/maybe endpoint #:key ipv6?) + "Strip the colon and port, if present in ENDPOINT, a string." + (if ipv6? + (if (string-prefix? "[" endpoint) + (first (string-split (string-drop endpoint 1) #\])) ;ipv6 + endpoint) + (first (string-split endpoint #\:)))) ;ipv4 + +(define* (ipv4-address? address) + "Predicate to check whether ADDRESS is a valid IPv4 address." + (let ((address (strip-port/maybe address))) + (false-if-exception + (->bool (getaddrinfo address #f AI_NUMERICHOST AF_INET))))) + +(define* (ipv6-address? address) + "Predicate to check whether ADDRESS is a valid IPv6 address." + (let ((address (strip-port/maybe address #:ipv6? #t))) + (false-if-exception + (->bool (getaddrinfo address #f AI_NUMERICHOST AF_INET6))))) + +(define (host-name? name) + "Predicate to check whether NAME is a host name, i.e. not an IP address." + (not (or (ipv6-address? name) (ipv4-address? name)))) + +(define (endpoint-host-names peers) + "Return an association list of endpoint host names keyed by their peer +public key, if any." + (reverse + (fold (lambda (peer host-names) + (let ((public-key (amneziawg-peer-public-key peer)) + (endpoint (amneziawg-peer-endpoint peer))) + (if (and endpoint (host-name? endpoint)) + (cons (cons public-key endpoint) host-names) + host-names))) + '() + peers))) + +(define (amneziawg-monitoring-program config) + (match-record config <amneziawg-configuration> + (interface monitor-ips-interval peers) + (let ((host-names (endpoint-host-names peers))) + (when (null? host-names) + (warning (G_ "'monitor-ips?' is #t but no host name to monitor~%"))) + + ;; Loosely based on Amneziawg's own 'reresolve-dns.sh' shell script + ;; (see: https://raw.githubusercontent.com/Amneziawg/amneziawg-tools/ + ;; master/contrib/reresolve-dns/reresolve-dns.sh). + (program-file + (format #f "amneziawg-~a-monitoring" interface) + (with-imported-modules (source-module-closure + '((gnu services herd) + (guix build utils))) + #~(begin + (use-modules (gnu services herd) + (guix build utils) + (ice-9 popen) + (ice-9 match) + (ice-9 textual-ports) + (srfi srfi-1) + (srfi srfi-26)) + + (define (resolve-host name) + "Return the IP address resolved from NAME." + (let* ((ai (car (getaddrinfo name))) + (sa (addrinfo:addr ai))) + (inet-ntop (sockaddr:fam sa) + (sockaddr:addr sa)))) + + (define awg #$(file-append amneziawg "/bin/awg")) + + #$(procedure-source strip-port/maybe) + + (define service-name + '#$(amneziawg-service-name interface)) + + (when (live-service-running + (current-service service-name)) + (let* ((pipe (open-pipe* OPEN_READ awg "show" + #$interface "endpoints")) + (lines (string-split (get-string-all pipe) + #\newline)) + ;; IPS is an association list mapping + ;; public keys to IP addresses. + (ips (map (match-lambda + ((public-key ip) + (cons public-key + (strip-port/maybe ip)))) + (map (cut string-split <> #\tab) + (remove string-null? + lines))))) + (close-pipe pipe) + (for-each + (match-lambda + ((key . host-name) + (let ((resolved-ip (resolve-host + (strip-port/maybe + host-name))) + (current-ip (assoc-ref ips key))) + (unless (string=? resolved-ip current-ip) + (format #t "resetting `~a' peer \ +endpoint to `~a' due to stale IP (`~a' instead of `~a')~%" + key host-name + current-ip resolved-ip) + (invoke awg "set" #$interface "peer" key + "endpoint" host-name))))) + '#$host-names))))))))) + +(define (amneziawg-shepherd-services config) + (match-record config <amneziawg-configuration> + (amneziawg interface monitor-ips? monitor-ips-interval shepherd-requirement) + (let ((awg-quick (file-append amneziawg "/bin/awg-quick")) + (auto-start? (amneziawg-configuration-auto-start? config)) + (config-file (amneziawg-configuration-file config))) + (define monitoring-service + (and monitor-ips? + (shepherd-service + (provision (list (symbol-append + (amneziawg-service-name interface) + '-monitoring))) + (requirement (list 'user-processes + (amneziawg-service-name interface))) + (modules '((shepherd service timer))) + (start #~(make-timer-constructor + #$(if (string? monitor-ips-interval) + #~(cron-string->calendar-event + #$monitor-ips-interval) + monitor-ips-interval) + (command '(#$(amneziawg-monitoring-program config))) + #:wait-for-termination? #t)) + (stop #~(make-timer-destructor)) + (documentation "Monitor the Amneziawg VPN tunnel.") + (actions (list shepherd-trigger-action))))) + + (cons (shepherd-service + (requirement `(networking user-processes ,@shepherd-requirement)) + (provision (list (amneziawg-service-name interface))) + (start #~(lambda _ + (invoke #$awg-quick "up" #$config-file))) + (stop #~(lambda _ + (invoke #$awg-quick "down" #$config-file) + #f)) ;stopped! + (actions (list (shepherd-configuration-action config-file))) + (auto-start? auto-start?) + (documentation "Run the AmneziaWG VPN tunnel")) + (or (and=> monitoring-service list) + '()))))) + +(define amneziawg-service-type + (service-type + (name 'amneziawg) + (extensions + (list (service-extension shepherd-root-service-type + amneziawg-shepherd-services) + (service-extension activation-service-type + amneziawg-activation) + (service-extension profile-service-type + (compose list + amneziawg-configuration-amneziawg)))) + (description "Set up AmneziaWG @acronym{VPN, Virtual Private Network} +tunnels.")))