This repository was archived by the owner on Oct 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhttp.go
More file actions
657 lines (576 loc) · 15.8 KB
/
Copy pathhttp.go
File metadata and controls
657 lines (576 loc) · 15.8 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
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
package main
import (
"avaron/llama"
network "avaron/net"
"avaron/vertex"
"avaron/health"
wg "avaron/wireguard"
"bufio"
"bytes"
"context"
"crypto/tls"
"encoding/base64"
"encoding/json"
"fmt"
systemd "github.com/coreos/go-systemd/v22/dbus"
"io"
"log"
"mime"
"net"
"net/http"
"os"
"os/exec"
filepath "path"
"sort"
"strconv"
"strings"
"time"
)
func Listen(ctx context.Context, ch chan net.Conn, listener net.Listener) {
defer close(ch)
defer listener.Close()
for {
conn, err := listener.Accept()
if err != nil {
log.Println("error accepting connection:", err)
continue
}
select {
case ch <- conn:
case <-ctx.Done():
return
}
}
}
var (
ServeDirectory string
)
func ServeHTTP(ctx context.Context) {
var (
err error
listener net.Listener
conns chan net.Conn = make(chan net.Conn)
config = &tls.Config{
Certificates: make([]tls.Certificate, 1),
}
)
if s := os.Getenv("SERVE_DIR"); s != "" {
ServeDirectory = s
} else {
ServeDirectory = "public"
}
config.Certificates[0], err = tls.LoadX509KeyPair("/etc/letsencrypt/live/isreal.estate/fullchain.pem",
"/etc/letsencrypt/live/isreal.estate/privkey.pem")
if err != nil {
log.Println("failed to load certificates:", err)
} else if listener, err := tls.Listen("tcp", ":8443", config); err != nil {
log.Println("error starting HTTPS listener:", err)
} else {
go Listen(ctx, conns, listener)
log.Printf("listening on %s\n", listener.Addr().String())
}
if listener, err = net.Listen("tcp", ":8080"); err != nil {
log.Fatalln("error starting HTTP listener:", err)
} else {
go Listen(ctx, conns, listener)
log.Printf("listening on %s\n", listener.Addr().String())
}
// load balancer - connection times out quicker the more connections there are
const (
total = 256
timeout = 0 * time.Second
)
var (
tokens = make(chan struct{})
durations = make(chan time.Duration)
)
go func() {
n := total
for {
// timeout*(total/n)
// or
// (timeout*total) / (timeout*n)
d := (time.Duration(n+1) * timeout) / (time.Duration(total + 1))
if n > 0 {
select {
case tokens <- struct{}{}:
n--
case <-tokens:
n++
case durations <- d:
case <-ctx.Done():
return
}
} else {
select {
case <-tokens:
n++
case durations <- d:
case <-ctx.Done():
return
}
}
}
}()
for conn := range conns {
select {
case <-tokens:
// borrow token
case <-ctx.Done():
return
}
go func(conn net.Conn) {
defer conn.Close()
var (
d time.Duration
)
t := time.Now()
if d := <-durations; d != 0 {
conn.SetReadDeadline(t.Add(d))
}
reader := bufio.NewReader(conn)
if req, err := http.ReadRequest(reader); err != nil {
log.Println("error reading request:", err)
} else {
if d != 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithDeadline(ctx, t)
defer cancel()
}
res := http.Response{
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
}
res.StatusCode, res.Header, res.Body = handle(ctx, req, conn)
if err != nil {
log.Println("error processing request:", err)
}
res.Status = http.StatusText(res.StatusCode)
log.Printf("%-24s %7s %-24s - %d(%s)\n", conn.RemoteAddr().String(), req.Method, req.URL.Path, res.StatusCode, res.Status)
if err = res.Write(conn); err != nil {
log.Println("error writing request:", err)
return
}
}
select {
case tokens <- struct{}{}:
// return token
case <-ctx.Done():
return
}
}(conn)
}
}
func handle(ctx context.Context, req *http.Request, conn net.Conn) (code int, header http.Header, r io.ReadCloser) {
var err error
code = http.StatusOK
i := func() int {
var i, j int
for i, j = 0, 0; i < len(req.URL.Path); i++ {
if req.URL.Path[i] == '/' {
j++
}
if j == 3 {
break
}
}
return i
}()
switch req.URL.Path[:i] {
case "/api/keys":
if req.Method != "GET" {
return http.StatusMethodNotAllowed, nil, nil
}
switch req.URL.Path[i:] {
case "/ssh":
r = io.NopCloser(strings.NewReader(PublicSSHKeys))
case "/wireguard":
buf, _ := PublicWireguardKey.MarshalText()
r = io.NopCloser(bytes.NewReader(buf))
}
case "/api/link":
if req.Method != "POST" {
return http.StatusMethodNotAllowed, nil, nil
}
log.Printf("pairing with %s\n", conn.RemoteAddr().String())
// check content-length
if l := req.ContentLength; l < 44 || l > 44+1 {
log.Printf("Request Content-Length (%d) != %d +/- 1/0\n", l, 44)
return http.StatusBadRequest, nil, nil
}
// read body
r := base64.NewDecoder(base64.StdEncoding, req.Body)
var key vertex.Key
_, err := io.ReadFull(r, key[:])
if err != nil && err != io.EOF {
log.Println("failed to public key:", err)
return http.StatusBadRequest, nil, nil
}
log.Printf("got buffer! %s\n", key.String())
files, err := os.ReadDir("pending")
if err == nil {
// fine
} else if os.IsNotExist(err) {
// fine
err := os.Mkdir("pending", 0700)
if err != nil {
log.Println("failed to make 'pending' dir:", err)
return http.StatusInternalServerError, nil, nil
}
} else if len(files) == 0 {
// still fine
} else {
var i int
var match bool
for i = range files {
if strings.EqualFold(files[i].Name(), key.String()) {
match = true
break
}
}
if match {
log.Printf("case insensitive, matching pending link: %s & %s - rejecting & deleting\n", key.String(), files[i].Name())
err := os.Remove(filepath.Join("pending", files[i].Name()))
if err != nil {
// something nasty is going on
panic(err)
}
return http.StatusUnauthorized, nil, nil
}
}
err = os.MkdirAll(fmt.Sprintf("pending/%s", key.String()), 0700)
if err != nil {
log.Println("failed to make pending link dir:", err)
return http.StatusInternalServerError, nil, nil
}
case "/api/nodes":
if req.Method != "GET" {
return http.StatusMethodNotAllowed, nil, nil
}
var w io.WriteCloser
r, w = io.Pipe()
select {
case RequestNodes <- w:
case <-ctx.Done():
w.Close()
}
header = http.Header{
"Content-Type": []string{"application/json"},
}
case "/api/wireguard":
switch req.Method {
case "GET":
info, err := wg.Interfaces(ctx)
if err != nil {
return http.StatusInternalServerError, nil, nil
}
log.Println("peer info", info)
var w io.WriteCloser
r, w = io.Pipe()
enc := json.NewEncoder(w)
go func() {
defer w.Close()
err := enc.Encode(info)
if err != nil {
log.Println("error encoding peers:", err)
}
}()
case "POST":
buf, err := io.ReadAll(req.Body)
if err != nil {
log.Println("failed reading body:", err)
return http.StatusInternalServerError, nil, nil
}
var ip net.IP
if len(buf) == 0 {
// ok
var routes map[string]*network.Route
if routes, err = network.Routes(ctx); err != nil {
log.Println("failed reading routes:", err)
return http.StatusInternalServerError, nil, nil
}
var names []string
for i := range routes {
names = append(names, i)
}
log.Println("routes pre-sort", names)
sort.Sort(&network.RouteMask{names, routes})
log.Println("routes post-sort", names)
if len(routes) < 1 {
return http.StatusBadRequest, nil, nil
}
list, err := network.List(ctx)
if err != nil {
log.Println("failed to probe network links:", err)
os.Exit(1)
}
route := routes[names[len(names)-1]]
ip = func() net.IP {
for _, link := range list {
for _, info := range link.AddrInfo {
if addr := net.ParseIP(info.Local); addr == nil {
continue
} else if route.Destination.Contains(addr) {
return addr
}
}
}
return nil
}()
if ip == nil {
log.Println("failed to to find IP matching first route:", route)
os.Exit(1)
}
} else if ip = net.ParseIP(string(buf)); ip == nil {
log.Println("failed parsing IP")
return http.StatusBadRequest, nil, nil
}
public, private, err := wg.GenerateKeyPair()
if err != nil {
log.Println("error generating wireguard key pair:", err)
return http.StatusInternalServerError, nil, nil
}
var pw io.WriteCloser
qr := exec.Command("sh", "-c", "qrencode -t SVG | grep -v '<?' | grep -v '<!'")
if pw, err = qr.StdinPipe(); err != nil {
log.Println("failed spawning qrencode pipe:", err)
return http.StatusInternalServerError, nil, nil
}
if r, err = qr.StdoutPipe(); err != nil {
log.Println("failed spawning qrencode pipe:", err)
return http.StatusInternalServerError, nil, nil
}
if err = qr.Start(); err != nil {
log.Println("error generating wireguard key pair:", err)
return http.StatusInternalServerError, nil, nil
}
dir := filepath.Join("peers", public.Path())
if err := os.Mkdir(dir, 0700); err != nil {
log.Println("error creating peer directory:", err)
return http.StatusInternalServerError, nil, nil
}
fmt.Fprintf(pw, "[Interface]\n")
fmt.Fprintf(pw, "Address = %s/32\n", public.GlobalAddress().IP.String())
fmt.Fprintf(pw, "PrivateKey = %s\n", private.String())
fmt.Fprintf(pw, "DNS = %s\n", public.GlobalAddress().IP.String())
fmt.Fprintf(pw, "\n")
fmt.Fprintf(pw, "[Peer]\n")
fmt.Fprintf(pw, "PublicKey = %s\n", PublicWireguardKey.String())
fmt.Fprintf(pw, "AllowedIPs = fc00:a7a0::/32\n")
fmt.Fprintf(pw, "Endpoint = %s:%d\n", ip.String(), 51820)
fmt.Fprintf(pw, "PersistentKeepalive = %d\n", 5)
fmt.Fprintf(pw, "\n")
pw.Close()
cmd := exec.Command("sudo", "wg", "set", "avaron", "peer", public.String(), "allowed-ips", "fc00:a7a0::/32")
buf, err = cmd.CombinedOutput()
if err != nil {
log.Println("failed adding peer", string(buf), err)
return http.StatusInternalServerError, nil, nil
}
case "DELETE":
buf, err := io.ReadAll(req.Body)
if err != nil {
log.Println("failed reading body:", err)
return http.StatusInternalServerError, nil, nil
}
var key vertex.Key
_, err = key.UnmarshalText(buf)
if err != nil {
log.Println("failed unmarshalling key:", err)
return http.StatusInternalServerError, nil, nil
}
err = os.RemoveAll(filepath.Join("peers", key.Path()))
if err != nil {
log.Println("failed deleting peer:", err)
return http.StatusNotFound, nil, nil
}
log.Println("deleted", key.Path())
cmd := exec.Command("sudo", "wg", "set", "avaron", "peer", key.String(), "remove")
buf, err = cmd.CombinedOutput()
if err != nil {
log.Println("failed removing peer", string(buf), err)
return http.StatusInternalServerError, nil, nil
}
default:
return http.StatusMethodNotAllowed, nil, nil
}
header = http.Header{
"Content-Type": []string{"application/json"},
}
case "/api/shell":
buf, err := io.ReadAll(req.Body)
if err != nil {
log.Println("failed reading body:", err)
return http.StatusInternalServerError, nil, nil
}
log.Println("SHELL", string(buf))
sh := exec.Command("sh", "-c", string(buf))
var w io.WriteCloser
r, w = io.Pipe()
r = io.NopCloser(io.TeeReader(r, os.Stderr))
sh.Stdout = w
sh.Stderr = w
if err := sh.Start(); err != nil {
log.Println("failed starting shell", string(buf), err)
return http.StatusInternalServerError, nil, nil
}
go func() {
log.Println("command completed:", sh.Wait())
w.Close()
}()
case "/api/health":
if req.Method != "GET" {
return http.StatusMethodNotAllowed, nil, nil
}
header = http.Header{
"Content-Type": []string{"application/json"},
}
var w io.WriteCloser
r, w = io.Pipe()
switch req.URL.Path[i:] {
case "/", "":
enc := json.NewEncoder(w)
go func() {
defer w.Close()
times := <-health.List
if err := enc.Encode(times); err != nil {
log.Println("error encoding:", err)
}
}()
default:
n, err := strconv.ParseInt(req.URL.Path[i+1:], 10, 64)
if err != nil {
log.Println("failed parsing integer", err)
return http.StatusBadRequest, nil, nil
}
times := <-health.List
if _, ok := times[n]; !ok {
return http.StatusNotFound, nil, nil
}
health.Get<-health.Request{
Time: n,
WriteCloser: w,
}
}
case "/api/completions":
if req.Method != "POST" {
return http.StatusMethodNotAllowed, nil, nil
}
req.RequestURI = ""
req.URL.Scheme = "http"
req.URL.Host = "localhost"
req.URL.Path = "/completions"
res, err := llama.Client.Do(req)
if err != nil {
log.Println("error forwarding request to llama:", err)
return http.StatusInternalServerError, nil, nil
} else if res.StatusCode < 200 || res.StatusCode >= 300 {
log.Println("error forwarding request to llama:", err)
return http.StatusInternalServerError, nil, nil
}
r = res.Body
header = res.Header
case "/api/services":
if req.Method != "GET" {
return http.StatusMethodNotAllowed, nil, nil
}
m, err := ListServices(ctx)
if err != nil {
panic(err)
}
buf, err := json.Marshal(m)
if err != nil {
log.Println("error marshalling systemd services", err)
return http.StatusInternalServerError, nil, nil
}
r = io.NopCloser(bytes.NewReader(buf))
header = http.Header{
"Content-Type": []string{"application/json"},
}
return
case "/api/services/start", "/api/services/stop", "/api/services/restart":
if req.Method != "POST" {
return http.StatusMethodNotAllowed, nil, nil
}
var conn *systemd.Conn
conn, err = systemd.NewSystemConnectionContext(ctx)
if err != nil {
log.Println("failed connecting to systemd:", err)
return http.StatusInternalServerError, nil, nil
}
defer conn.Close()
dec := json.NewDecoder(req.Body)
if t, _ := dec.Token(); t != json.Delim('[') {
log.Println("error reading services '[' for restart:", err)
return http.StatusInternalServerError, nil, nil
}
var service string
for dec.More() {
err = dec.Decode(&service)
if err != nil {
break
}
switch req.URL.Path {
case "/api/services/start":
_, err = conn.StartUnitContext(ctx, service, "replace", nil)
case "/api/services/stop":
_, err = conn.StopUnitContext(ctx, service, "replace", nil)
case "/api/services/restart":
_, err = conn.RestartUnitContext(ctx, service, "replace", nil)
}
if err != nil {
break
}
}
if err != nil {
log.Println("error reading services for restart:", err)
return http.StatusInternalServerError, nil, nil
}
if t, _ := dec.Token(); t != json.Delim(']') {
log.Println("error reading services ']' for restart:", err)
return http.StatusInternalServerError, nil, nil
}
case "", "/":
code = http.StatusMovedPermanently
header = http.Header{
"Location": []string{"/dashboard/"},
}
return
default:
if req.Method != "GET" {
return http.StatusNotFound, nil, nil
}
path := filepath.Join(ServeDirectory, filepath.Clean(req.URL.Path))
if strings.HasSuffix(req.URL.Path, "/") {
path = filepath.Join(ServeDirectory, filepath.Clean(req.URL.Path), "index.html")
}
info, err := os.Stat(path)
if err != nil {
return http.StatusNotFound, nil, nil
} else if info.IsDir() {
code = http.StatusMovedPermanently
header = http.Header{
"Location": []string{req.URL.Path + "/"},
}
return
} else if ts := req.Header.Get("If-Modified-Since"); ts == "" {
// fine
} else if t, err := time.Parse(http.TimeFormat, ts); err != nil {
// fine
log.Printf("If-Modified-Since time parse failure: %v\n", err)
} else if !info.ModTime().Truncate(time.Second).After(t) {
return http.StatusNotModified, nil, nil
}
if r, err = os.Open(path); err != nil {
log.Println("error openning:", err)
return http.StatusInternalServerError, nil, nil
}
header = http.Header{
"Content-Type": []string{mime.TypeByExtension(filepath.Ext(path))},
"Last-Modified": []string{info.ModTime().UTC().Format(http.TimeFormat)},
"Content-Length": []string{strconv.FormatInt(info.Size(), 10)},
}
}
return
}