/
sup.go
160 lines (132 loc) · 3.11 KB
/
sup.go
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
package now
import (
"bufio"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os/exec"
"sort"
"sync"
"time"
"github.com/frioux/leatherman/internal/lmhttp"
"github.com/frioux/leatherman/internal/notes"
)
func loadPeers() ([]string, error) {
var tsStatus struct {
Peer map[string]struct {
DNSName string
}
}
cmd := exec.Command("tailscale", "status", "-json")
b, err := cmd.Output()
if err != nil {
return nil, err
}
if err := json.Unmarshal(b, &tsStatus); err != nil {
return nil, err
}
ret := make([]string, 0, len(tsStatus.Peer))
for _, p := range tsStatus.Peer {
ret = append(ret, p.DNSName)
}
sort.Strings(ret)
return ret, nil
}
func peerVersion(ctx context.Context, hostname string) (ret option) {
defer func() {
if ret.error != nil {
ret.error = fmt.Errorf("%s: %w", hostname, ret.error)
} else {
ret.value = fmt.Sprintf("%s: %s", hostname, ret.value)
}
}()
resp, err := lmhttp.Get(ctx, "http://"+hostname+":8081/version")
if err != nil {
return option{error: err}
}
defer resp.Body.Close()
s := bufio.NewScanner(resp.Body)
for s.Scan() {
return option{value: s.Text()} // silly, return first line
}
return option{error: errors.New("???")}
}
func allVersions(ctx context.Context) []option {
peers, err := loadPeers()
if err != nil {
return []option{{error: fmt.Errorf("couldn't load peers: %w", err)}}
}
wg := &sync.WaitGroup{}
wg.Add(len(peers))
ret := make([]option, len(peers))
for i, p := range peers {
go func(i int, p string) {
defer wg.Done()
ret[i] = peerVersion(ctx, p)
}(i, p)
}
wg.Wait()
return ret
}
func handlerSup(z *notes.Zine) http.Handler {
return lmhttp.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) error {
rw.Header().Set("Cache-Control", "no-cache")
ctx, cancel := context.WithTimeout(req.Context(), 2*time.Second)
defer cancel()
v := supVars{HTMLVars: &HTMLVars{Zine: z}}
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
resp, err := lmhttp.Get(ctx, "http://retropie:8081/retropie")
if err != nil {
// whyyyyy
v.retroPie.error = err
return
}
defer resp.Body.Close()
var decodeable struct{ Game string }
dec := json.NewDecoder(resp.Body)
v.retroPie.error = dec.Decode(&decodeable)
v.retroPie.value = decodeable.Game
}()
wg.Add(1)
go func() {
defer wg.Done()
resp, err := lmhttp.Get(ctx, "http://steamos:8081/steambox")
if err != nil {
v.steamOS.error = err
return
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
v.steamOS.value, v.steamOS.error = string(b), err
}()
wg.Add(1)
go func() {
defer wg.Done()
resp, err := lmhttp.Get(ctx, "http://pi400:8081/x11title")
if err != nil {
v.pi400.error = err
return
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
v.pi400.error = err
} else {
v.pi400.value = `<a href="http://pi400:8081/x11shot">` + string(b) + `</a>`
}
}()
wg.Add(1)
go func() {
defer wg.Done()
v.Versions = allVersions(ctx)
}()
wg.Wait()
return tpl.ExecuteTemplate(rw, "sup.html", v)
})
}