blob: edc36ffd4d29e9546496262b477a313d91215250 [file] [log] [blame]
Giorgi Lekveishvili52814d92023-06-15 19:30:32 +04001package main
2
3import (
4 "fmt"
5 "os/exec"
Giorgi Lekveishvili027ef432023-06-16 12:31:25 +04006 "strings"
Giorgi Lekveishvili52814d92023-06-15 19:30:32 +04007)
8
9type client struct {
10 config string
11}
12
13func newClient(config string) *client {
14 return &client{
15 config: fmt.Sprintf("--config=%s", config),
16 }
17}
18
19func (c *client) createUser(name string) error {
20 cmd := exec.Command("headscale", c.config, "users", "create", name)
21 out, err := cmd.Output()
22 fmt.Println(string(out))
23 return err
24}
25
26func (c *client) createPreAuthKey(user string) (string, error) {
27 // TODO(giolekva): make expiration configurable, and auto-refresh
28 cmd := exec.Command("headscale", c.config, "--user", user, "preauthkeys", "create", "--reusable", "--expiration", "365d")
29 out, err := cmd.Output()
Giorgi Lekveishvili027ef432023-06-16 12:31:25 +040030 if err != nil {
31 return "", err
32 }
Giorgi Lekveishvili52814d92023-06-15 19:30:32 +040033 fmt.Println(string(out))
Giorgi Lekveishvili027ef432023-06-16 12:31:25 +040034 return extractLastLine(string(out))
Giorgi Lekveishvili52814d92023-06-15 19:30:32 +040035}
36
37func (c *client) enableRoute(id string) error {
38 // TODO(giolekva): make expiration configurable, and auto-refresh
39 cmd := exec.Command("headscale", c.config, "routes", "enable", "-r", id)
40 out, err := cmd.Output()
41 fmt.Println(string(out))
42 return err
43}
Giorgi Lekveishvili027ef432023-06-16 12:31:25 +040044
45func extractLastLine(s string) (string, error) {
46 items := strings.Split(s, "\n")
47 for i := len(items) - 1; i >= 0; i-- {
48 t := strings.TrimSpace(items[i])
49 if t != "" {
50 return t, nil
51 }
52 }
53 return "", fmt.Errorf("All lines are empty")
54}