-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpxeserver.go
119 lines (101 loc) · 2.69 KB
/
pxeserver.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
package main
import (
"net/http"
"path/filepath"
"strings"
"sync"
"time"
tftp "github.com/pin/tftp/v3"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
var ipHostMap sync.Map
type serveCfg struct {
Host string
Http string
Https string
Tftp string
Broadcast string
Datadir string
}
type acmeCfg struct {
Root string
CAurl string
}
func main() {
viper.SetConfigName("pxeserver")
viper.SetConfigType("yaml")
viper.AddConfigPath("/etc/")
viper.AddConfigPath(".")
viper.AutomaticEnv()
viper.SetEnvPrefix("pxeserver")
viper.EnvKeyReplacer(strings.NewReplacer("_", "."))
err := viper.ReadInConfig()
if err != nil {
log.Fatalf("fatal error config file: %w", err)
}
var hostCfg serveCfg
viper.UnmarshalKey("serve", &hostCfg)
dirname, err := filepath.Abs(hostCfg.Datadir)
if err != nil {
log.Fatalf("Could not get absolute path to directory: %s: %s", dirname, err)
}
inventory := viper.GetStringMapString("inventory")
var acmeCfg acmeCfg
viper.UnmarshalKey("acme", &acmeCfg)
tlsConfig, m, err := setupACME(hostCfg.Host, &acmeCfg)
if err != nil {
log.Fatalf("Could not setup ACME for certificates: %s", err)
}
var wg sync.WaitGroup
wg.Add(3)
fs := logStaticRequest(http.FileServer(http.Dir(dirname)))
go func() {
defer wg.Done()
httpHandler := http.NewServeMux()
httpHandler.Handle("/", fs)
httpHandler.HandleFunc("/cloudlog/", cloudLogHandle())
httpServe := &http.Server{
Addr: hostCfg.Http,
Handler: m.HTTPHandler(httpHandler),
}
err = httpServe.ListenAndServe()
if err != nil {
log.Fatalf("Could not serve directory: %s: %s", dirname, err.Error())
}
}()
go func() {
defer wg.Done()
mux := http.NewServeMux()
tlsServer := &http.Server{
Addr: hostCfg.Https,
Handler: mux,
TLSConfig: tlsConfig,
}
mux.Handle("/", fs)
mux.HandleFunc("/magic", handleWOL(inventory, hostCfg.Broadcast))
mux.HandleFunc("/status", statusPage(hostCfg.Host, hostCfg.Https, inventory))
mux.HandleFunc("/status/ws", wsEndpoint)
mux.HandleFunc("/favicon.ico", favicon)
err = tlsServer.ListenAndServeTLS("", "")
if err != nil {
log.Fatalf("Could not serve pxe status: %s", err.Error())
}
}()
go func() {
s := tftp.NewServer(tftpReadHandler(dirname, inventory), tftpWriteHandler)
s.SetBlockSize(5000)
s.SetBackoff(func(attempts int) time.Duration {
return time.Duration(attempts) * time.Second
})
defer wg.Done()
err := s.ListenAndServe(hostCfg.Tftp)
if err != nil {
log.Fatalf("TFTP error: %v\n", err)
}
}()
log.Infof("Serving /status on HTTPS %d", hostCfg.Https)
log.Infof("Serving %s on HTTP %d", dirname, hostCfg.Http)
log.Infof("Serving %s on TFTP %d", dirname, hostCfg.Tftp)
wg.Wait()
}