This repository was archived by the owner on Dec 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtailscale.go
More file actions
72 lines (62 loc) · 1.48 KB
/
tailscale.go
File metadata and controls
72 lines (62 loc) · 1.48 KB
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
package main
import (
"context"
"fmt"
"os/exec"
"regexp"
"strings"
"github.com/charmbracelet/bubbles/table"
)
func changeExitNode(ctx context.Context, exitNode string) error {
cmd := exec.CommandContext(ctx, "tailscale", "set", "--exit-node="+exitNode)
return cmd.Run()
}
func getCurrentExitNode() string {
cmd := exec.Command("tailscale", "exit-node", "list")
output, err := cmd.Output()
if err != nil {
return ""
}
lines := strings.Split(string(output), "\n")
for _, line := range lines {
if strings.Contains(line, "selected") {
fields := strings.Fields(line)
if len(fields) >= 4 {
country := fields[2]
city := fields[3]
if city == "Any" {
continue
}
return country + ", " + city
}
}
}
return ""
}
func generateMullvadServers() ([]table.Row, error) {
cmd := exec.Command("tailscale", "exit-node", "list")
output, err := cmd.Output()
if err != nil {
return nil, fmt.Errorf("failed to execute tailscale command: %v", err)
}
var servers []table.Row
lines := strings.Split(string(output), "\n")
re := regexp.MustCompile(`\s{2,}`)
for _, line := range lines[2:] { // Skip the header line
if strings.HasPrefix(line, "#") {
continue
}
fields := re.Split(strings.TrimSpace(line), -1)
if len(fields) >= 5 {
ip := fields[0]
hostname := fields[1]
country := fields[2]
city := fields[3]
if city == "Any" {
continue
}
servers = append(servers, table.Row{ip, hostname, country, city})
}
}
return servers, nil
}