-
Notifications
You must be signed in to change notification settings - Fork 0
/
tftphandle.go
66 lines (57 loc) · 1.58 KB
/
tftphandle.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
package main
import (
"fmt"
"io"
"os"
"strings"
tftp "github.com/pin/tftp/v3"
log "github.com/sirupsen/logrus"
)
func tftpWriteHandler(filename string, wt io.WriterTo) error {
return fmt.Errorf("read-only")
}
func tftpReadHandler(dirname string, inventory map[string]string) func(string, io.ReaderFrom) error {
return func(filename string, rf io.ReaderFrom) error {
efiConfig := strings.HasPrefix(filename, "efi64/pxelinux.cfg/01-")
if efiConfig {
macAddr := filename
macAddr = strings.TrimPrefix(macAddr, "efi64/pxelinux.cfg/01-")
macAddr = strings.ReplaceAll(macAddr, "-", ":")
macAddr = strings.ToLower(macAddr)
hostname, ok := inventory[macAddr]
if !ok {
log.Warnf("MAC %s not in inventory", macAddr)
}
raddr := rf.(tftp.OutgoingTransfer).RemoteAddr()
ipHostMap.Store(raddr.IP.String(), hostname)
}
reader, err := os.Open(dirname + "/" + filename)
if err != nil {
log.Warnf("%v\n", err)
return err
}
n, err := rf.ReadFrom(reader)
if err != nil {
log.Warnf("%v\n", err)
return err
}
raddr := rf.(tftp.OutgoingTransfer).RemoteAddr()
hostname, ok := ipHostMap.Load(raddr.IP.String())
if !ok {
hostname = "unknown"
}
log.Infof("%s: %d bytes sent to %s\n", filename, n, hostname.(string))
if !ok {
log.Warnf("Uknown host, cant set progress\n")
return nil
}
if efiConfig {
setMenuProgress(hostname.(string))
} else if strings.HasSuffix(filename, "vmlinuz") {
setVmlinuzProgress(hostname.(string))
} else if strings.HasSuffix(filename, "initrd") {
setInitrdProgress(hostname.(string))
}
return nil
}
}