This repository has been archived by the owner on Oct 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
404 lines (337 loc) ยท 11.4 KB
/
main.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package main
import (
"bytes"
"crypto/sha512"
"crypto/tls"
"crypto/x509"
"encoding/hex"
"encoding/json"
"io/ioutil"
"net/http"
"os"
"os/exec"
"os/signal"
"regexp"
"strings"
"syscall"
"time"
"github.com/pkg/err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9w0BAQsF
AAOCAQEA3TPXEfNjWDjdGBX7CVW+dla5cEilaUcne8IkCJLxWh9KEik3JHRRHGJo
uM2VcGfl96S8TihRzZvoroed6ti6WqEBmtzw3Wodatg+VyOeph4EYpr/1wXKtx8/
wApIvJSwtmVi4MFU5aMqrSDE6ea73Mj2tcMyo5jMd6jmeWUHK8so/joWUoHOUgwu
X4Po1QYz+3dszkDqMp4fklxBwXRsW10KXzPMTZ+sOPAveyxindmjkW8lGy+QsRlG
PfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+X+Q7UNKEkROb3N6
KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg==
-----END CERTIFICATE-----`
var cachedMachineList []byte
func getBase() string {
if os.Getenv("DEBUG") == "true" {
return "https://mapp-dev.betterinformatics.com"
}
return "https://mapp.betterinformatics.com"
}
func getMachines() (machines []string, err error) {
var listStruct struct{ Machines []string }
var listBytes []byte
machineListPath := os.Getenv("MACHINE_LIST")
if machineListPath == "" {
log.Infoln("Could not find machine list. Downloading and using machine list...")
client, err := getHTTPClient()
if err != nil {
return nil, errors.Wrap(err, "could not get http client")
}
resp, err := client.Get(getBase() + "/api/rooms/all")
if err != nil {
return nil, errors.Wrap(err, "could not download machines list")
}
listBytes, err = ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "could not read API response for machines list")
}
defer resp.Body.Close()
} else {
if cachedMachineList == nil {
listBytes, err = ioutil.ReadFile(machineListPath)
if err != nil {
return nil, errors.Wrap(err, "could not open machines file")
}
cachedMachineList = listBytes
} else {
listBytes = cachedMachineList
}
}
if err := json.Unmarshal(listBytes, &listStruct); err != nil {
return nil, errors.Wrap(err, "could not read machines file json contents")
}
machines = listStruct.Machines
return
}
func checkAuthentication() error {
_, err := exec.Command(
"ssh",
"-o", "ForwardX11=no",
"-o", "GSSAPIAuthentication=yes",
"-o", "GSSAPIDelegateCredentials=no",
"-o", "BatchMode=yes",
"student.login.inf.ed.ac.uk",
"exit",
).Output()
if err != nil {
return err
}
return nil
}
type MachineResult struct {
Hostname string `json:"hostname"`
User string `json:"user"`
Timestamp string `json:"timestamp"`
Status string `json:"status"`
Error error `json:"-"`
}
var reo = regexp.MustCompile(`^\s+(\w+)\s+\d+\s+(\w+)\s+seat0\s+$`)
func searchWorker(id int, jobs <-chan string, results chan<- MachineResult) {
for machine := range jobs {
// fmt.Println("worker", id, "started job", machine)
result := MachineResult{Hostname: machine}
cmd := exec.Command(
"ssh",
"-o", "ForwardX11=no",
"-o", "GSSAPIAuthentication=yes",
"-o", "GSSAPIDelegateCredentials=no",
"-o", "BatchMode=yes",
"-o", "ServerAliveInterval=5",
"-o", "ServerAliveCountMax=3",
"-o", "ConnectTimeout=5s",
"-o", "StrictHostKeyChecking=no",
machine+".inf.ed.ac.uk",
`/usr/bin/printf '%s' "$(PIDS=$(pidof lightdm); for pid in $PIDS; do ps -ho user,lstart --ppid $pid | fgrep -v -e 'root' -e lightdm; done)"`,
)
var errbuf bytes.Buffer
cmd.Stderr = &errbuf
out, err := cmd.Output()
if err != nil {
result.Status = "offline"
result.Error = errors.Wrap(err, errbuf.String())
if strings.Contains(errbuf.String(), "Name or service not known") {
result.Status = "unknown"
}
} else {
strOut := strings.TrimSpace(string(out))
result.Status = "online"
if strOut == "" {
result.User = ""
} else {
parts := strings.SplitN(strOut, " ", 2)
result.User = parts[0]
loginTime, err := time.Parse("Mon Jan 2 15:04:05 2006", parts[1])
if err != nil {
log.
WithField("host", machine).
WithField("error", err).
Errorln("TIME PARSE FAILED")
} else {
location, _ := time.LoadLocation("Europe/London")
result.Timestamp = loginTime.In(location).Format(time.RFC3339)
}
}
}
results <- result
// fmt.Println("worker", id, "finished job", result)
}
}
func getHTTPClient() (*http.Client, error) {
pool := x509.NewCertPool()
if !pool.AppendCertsFromPEM([]byte(letsencryptPem)) {
return nil, errors.New("failed to append cert to pool")
}
tlsConf := &tls.Config{RootCAs: pool}
tlsConf.BuildNameToCertificate()
client := &http.Client{Transport: &http.Transport{
TLSClientConfig: tlsConf,
}}
return client, nil
}
func sendUpdate(payload interface{}) error {
data, err := json.Marshal(payload)
if err != nil {
return err
}
client, err := getHTTPClient()
if err != nil {
return errors.Wrap(err, "could not get http client")
}
_, err = client.Post(
getBase()+"/api/update",
"application/json",
bytes.NewBuffer(data),
)
return err
}
func performSearch(machines []string, secret, callbackKey string) {
start := time.Now()
count := len(machines)
jobs := make(chan string, count)
resultsChan := make(chan MachineResult, count)
workers := 100
if count < workers {
workers = count
}
for w := 0; w < workers; w++ {
go searchWorker(w, jobs, resultsChan)
}
for _, machine := range machines {
jobs <- machine
}
log.Println("JOBS SENT")
close(jobs)
var payload struct {
Machines []MachineResult `json:"machines"`
CallbackKey string `json:"callback-key"`
}
payload.Machines = make([]MachineResult, count)
payload.CallbackKey = callbackKey
hash := sha512.New()
for i := 0; i < count; i++ {
result := <-resultsChan
if result.Error != nil {
log.
WithField("host", result.Hostname).
WithField("status", result.Status).
WithField("error", result.Error).
Errorln("NO-GO")
} else if result.User != "" {
hash.Reset()
if _, err := hash.Write([]byte(result.User + secret)); err != nil {
log.WithField("error", err).Errorln("could not hash user")
result.User = ""
} else {
result.User = hex.EncodeToString(hash.Sum(nil))
log.
WithField("status", result.Status).
WithField("host", result.Hostname).
WithField("user", result.User[:15]+"...").
Infoln("SUCCESS WITH USER")
}
} else {
log.
WithField("status", result.Status).
WithField("host", result.Hostname).
Infoln("SUCCESS")
}
result.Error = nil
payload.Machines[i] = result
}
log.WithField("servers", count).WithField("timestamp", time.Now()).Printf("DONE ITERATION")
if err := sendUpdate(payload); err != nil {
log.WithField("error", err).Errorln("ERROR could not reach callback")
} else {
duration := time.Now().Sub(start)
log.WithField("duration", duration).Infoln("CALLBACK ok for all machines")
}
// Massive payload. Lets not show it.
// Payload also shows callback key..
// log.Printf("RESULTS %+v\n", payload)
}
func main() {
machines, err := getMachines()
if err != nil {
log.WithField("error", err).Fatalln("could not get machines")
return
}
callbackKey := os.Getenv("CALLBACK_KEY")
secret := os.Getenv("MAPP_SECRET")
if callbackKey == "" || secret == "" {
log.Fatalln("missing CALLBACK_KEY or MAPP_SECRET")
return
}
log.Info("CHECKING authentication...")
err = checkAuthentication()
if err != nil {
log.WithField("error", err).Fatalln("AUTH FAIL!")
return
}
log.Info("AUTH OK, starting initial run...")
performSearch(machines, secret, callbackKey)
cronSearch := func(msg string) func() {
return func() {
if newMachines, err := getMachines(); err != nil {
log.WithField("error", err).Fatalln("could not get machines")
} else {
machines = newMachines
}
log.WithField("msg", msg).Println("CRON TRIGGERED")
performSearch(machines, secret, callbackKey)
}
}
c := cron.New()
// weekdays, between 9am and 6pm,
// at 8, 15, 45, 52 past the hour
// See https://crontab.guru/ (remove first number [seconds])
c.AddFunc("0 8,15,45,52 09-18 * * 1-5", cronSearch("near the hour, from 9am to 6pm, weekly"))
// Always do it on the hour and at the half hour.
c.AddFunc("0 0,30 * * * *", cronSearch("every thirty minutes"))
// Test
// c.AddFunc("0 */1 * * * *", cronSearch("every minute"))
log.Println("CRON LOADED.")
c.Start()
// Create new signal receiver
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
log.Println("Shutting down...")
c.Stop()
}