summaryrefslogtreecommitdiffhomepage
path: root/lib/net/jao-mullvad.el
blob: c76af33de5183001c58b53cd78cc14bcc066b96a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
;;; jao-mullvad.el --- Calling the mullvad cli       -*- lexical-binding: t; -*-

;; Copyright (C) 2021  jao

;; Author: jao <mail@jao.io>
;; Keywords: processes

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; Simple helpers to call mullvad and parse its output

;;; Code:

(defvar jao-mullvad-mode-map
  (let ((map (make-keymap)))
    (suppress-keymap map)
    (define-key map [?q] 'bury-buffer)
    (define-key map [?n] 'next-line)
    (define-key map [?p] 'previous-line)
    (define-key map [?g] 'jao-mullvad-status)
    (define-key map [?l] 'jao-mullvad-list)
    (define-key map [?u] 'jao-mullvad-update-list)
    (define-key map [?r] 'jao-mullvad-reconnect)
    (define-key map [?R] 'jao-mullvad-set-relay)
    (define-key map [?d] 'jao-mullvad-disconnect)
    (define-key map [?c] 'jao-mullvad-connect)
    map))

(defvar jao-mullvad--buffer "*mullvad*")

;;;###autoload
(defun jao-mullvad-mode ()
  "A very simple mode to show the output of mulvad commands."
  (interactive)
  (kill-all-local-variables)
  (buffer-disable-undo)
  (use-local-map jao-mullvad-mode-map)
  ;; (setq-local font-lock-defaults '(jao-jao-mullvad-font-lock-keywords))
  (setq-local truncate-lines t)
  (setq-local next-line-add-newlines nil)
  (setq major-mode 'jao-mullvad-mode)
  (setq mode-name "mullvad")
  (read-only-mode 1))

(defun jao-mullvad--do (things &optional buffer)
  "Execute a mullvad command THINGS in the given BUFFER."
  (let ((b (or buffer (pop-to-buffer (get-buffer-create jao-mullvad--buffer)))))
    (let ((inhibit-read-only t)
          (cmd (format "mullvad %s" things)))
      (delete-region (point-min) (point-max))
      (message "Running: %s ...." cmd)
      (shell-command cmd b)
      (message ""))
    (jao-mullvad-mode)))

(defconst jao-mullvad--country-rx "^\\([^\t\n]+ (...?)\\)$")
(defconst jao-mullvad--city-rx "	\\([^\t(]+ (...)\\)")

(defun jao-mullvad--list ()
  "Compute an alist of available relay countries with their cities."
  (with-temp-buffer
    (jao-mullvad--do "relay list" (current-buffer))
    (goto-char (point-min))
    (let ((countries) (country))
      (while (not (eobp))
        (cond ((looking-at jao-mullvad--country-rx)
               (when country
                 (setq countries (cons (reverse country) countries)))
               (setq country (list (match-string 1))))
              ((looking-at jao-mullvad--city-rx)
               (setq country (cons (match-string 1) country))))
        (forward-line 1))
      (reverse (cons (reverse country) countries)))))

;;;###autoload
(defun jao-mullvad-status ()
  "Display mullvad connection status, including country, city and server."
  (interactive)
  (jao-mullvad--do "status")
  (when (re-search-forward "[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+" nil t)
    (let ((ip (match-string 0)) (country "") (city "") (server ""))
      (with-temp-buffer
        (jao-mullvad--do "relay list" (current-buffer))
        (goto-char (point-min))
        (when (search-forward ip nil t)
          (beginning-of-line)
          (when (looking-at "\t\t\\([^( ]+\\) (")
            (setq server (match-string 1)))
          (when (re-search-backward jao-mullvad--city-rx nil t)
            (setq city (string-trim (match-string 0))))
          (when (re-search-backward jao-mullvad--country-rx nil t)
            (setq country (match-string 0)))))
      (let ((inhibit-read-only t))
        (forward-line 1)
        (insert "Connected to " country " " city  " - " server "\n")
        (goto-char (point-min))))))

;;;###autoload
(defun jao-mullvad-set-relay ()
  "Choose a country and city and set them to the default relay location."
  (interactive)
  (let* ((cc (jao-mullvad--list))
         (countries (cons "any" (mapcar #'car cc)))
         (country (completing-read "Country: " countries nil t))
         (cities (unless (string= "any" country)
                   (cons "any" (cdr (assoc country cc #'string=)))))
         (city (if cities (completing-read "City: " cities nil t) "any"))
         (cntr (if (string-match "(\\(...?\\))" country)
                   (match-string 1 country)
                 country))
         (code (if (string-match "(\\(...\\))" city) (match-string 1 city) "")))
    (when (y-or-n-p (format "Set location to %s / %s?" country city))
      (jao-mullvad--do (format "relay set location %s %s" cntr code)))))

;;;###autoload
(defun jao-mullvad-connect ()
  "Connect to mullvad VPM, with the current configuration."
  (interactive)
  (jao-mullvad--do "connect"))

;;;###autoload
(defun jao-mullvad-reconnect ()
  "Ask mullvad to reconnect."
  (interactive)
  (jao-mullvad--do "reconnect"))

;;;###autoload
(defun jao-mullvad-disconnect ()
  "Ask mullvad to disconnect, after confirmation."
  (interactive)
  (when (y-or-n-p "Disconnect?") (jao-mullvad--do "disconnect")))

;;;###autoload
(defun jao-mullvad-list ()
  "List all available relay locations."
  (interactive)
  (jao-mullvad--do "relay list"))

;;;###autoload
(defun jao-mullvad-update-list ()
  "Update list of available relay locations (background operation)."
  (interactive)
  (jao-mullvad--do "relay update"))


(provide 'jao-mullvad)
;;; jao-mullvad.el ends here