-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
168 lines (149 loc) · 4.22 KB
/
server.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
package preseeder
import (
"encoding/json"
"fmt"
// htmlTemplate "html/template"
"log"
"net"
"net/http"
"text/template"
"time"
)
/*
* Types
*/
type PreseedServer struct {
preseedTemplate *template.Template
preseedContext *PreseedContext
lateCommandTemplate *template.Template
authorizedKeys string
lateCommand string
staticFileRoot string
tracker *ClientTracker
serveMux *http.ServeMux
}
type preseedInput struct {
PreseedHost string
}
type serverSummary struct {
Name string `json:"name"`
Description string `json:"description"`
Urls map[string]string `json:"urls"`
Clients TrackerState `json:"clients"`
}
/*
* Constants
*/
/*
* Functions
*/
func (s *PreseedServer) track(name string, r *http.Request) {
if r.RemoteAddr != "" {
host, _, err := net.SplitHostPort(r.RemoteAddr)
if err == nil {
s.tracker.Track(name, host, getRemoteMacAddress(host))
return
} else {
log.Printf("track: %v", err)
}
}
log.Printf("track: No remote addr found for %v", r)
}
func (s *PreseedServer) handleGetRoot(w http.ResponseWriter, r *http.Request) {
summary := &serverSummary{
Name: "preseeder",
Description: "Debian/Ubuntu preseed.cfg server. See https://github.com/hblanks/preseeder",
Clients: s.tracker.ReadState(),
Urls: map[string]string{
"preseed": fmt.Sprintf("http://%s/preseed", r.Host),
"late_command": fmt.Sprintf("http://%s/preseed/late_command", r.Host),
},
}
summaryJSON, err := json.MarshalIndent(summary, "", " ")
if err == nil {
fmt.Fprintf(w, string(summaryJSON))
} else {
fmt.Fprintf(w, "render failure")
}
}
func (s *PreseedServer) handleGetPreseed(w http.ResponseWriter, r *http.Request) {
preseedInput := s.preseedContext
preseedInput.PreseedHost = r.Host
err := s.preseedTemplate.Execute(w, preseedInput)
if err != nil {
log.Printf("handleGetPreseed: Template expansion failed: %v", err)
http.Error(w, "Internal server error", 500)
}
s.track("preseed", r)
}
func (s *PreseedServer) handleGetLateCommand(w http.ResponseWriter, r *http.Request) {
lateCommandContext := &lateCommandContext{
s.authorizedKeys,
s.lateCommand,
s.preseedContext,
}
err := s.lateCommandTemplate.Execute(w, lateCommandContext)
if err != nil {
log.Printf("handleGetLateCommand: %v", err)
http.Error(w, "Internal server error", 500)
}
s.track("late_command", r)
}
func (s *PreseedServer) handleFunc(
prefix string, fn func(http.ResponseWriter, *http.Request)) {
wrappedFn := func(w http.ResponseWriter, r *http.Request) {
// The installer's HTTP client for fetching preseed.cfg
// fails unless we explicitly close the HTTP connection.
// So, we do it manually here. Alas.
w.Header().Set("Connection", "close")
fn(w, r)
}
s.serveMux.HandleFunc(prefix, wrappedFn)
}
func (s *PreseedServer) registerHandlers() {
s.handleFunc("/preseed", s.handleGetPreseed)
s.handleFunc("/preseed/late_command", s.handleGetLateCommand)
if len(s.staticFileRoot) != 0 {
staticServer := http.StripPrefix("/static/",
http.FileServer(http.Dir(s.staticFileRoot)))
http.Handle("/static/", staticServer)
}
s.handleFunc("/", s.handleGetRoot)
}
func NewPreseedServer(
preseed string,
preseedContext *PreseedContext,
authorizedKeys string,
lateCommand string,
staticFileRoot string,
) *PreseedServer {
if preseed == "" {
preseed = GetDefaultPreseed()
}
preseedTemplate := parseTemplateString("preseed", preseed)
lateCommandTemplate := parseTemplateString("late_command",
GetLateCommandScript())
s := &PreseedServer{
preseedTemplate: preseedTemplate,
preseedContext: preseedContext,
lateCommandTemplate: lateCommandTemplate,
authorizedKeys: authorizedKeys,
lateCommand: lateCommand,
staticFileRoot: staticFileRoot,
tracker: NewClientTracker(),
serveMux: http.NewServeMux(),
}
s.registerHandlers()
return s
}
func (s *PreseedServer) ListenAndServe(addr string, handler http.Handler) error {
s.tracker.Start()
httpServer := &http.Server{
Addr: addr,
Handler: s.serveMux,
ReadTimeout: 60 * time.Second,
WriteTimeout: 60 * time.Second,
MaxHeaderBytes: 1 << 20,
}
return httpServer.ListenAndServe()
}