local-server/cmd/dkl-local-server/http.go

128 lines
2.6 KiB
Go
Raw Normal View History

2018-06-12 10:09:47 +00:00
package main
import (
"flag"
"log"
"net"
"net/http"
"regexp"
"strings"
2018-12-10 10:59:24 +00:00
"novit.nc/direktil/pkg/localconfig"
2018-06-12 10:09:47 +00:00
)
var (
reHost = regexp.MustCompile("^/hosts/([^/]+)/([^/]+)$")
trustXFF = flag.Bool("trust-xff", true, "Trust the X-Forwarded-For header")
)
func serveHostByIP(w http.ResponseWriter, r *http.Request) {
host, cfg := hostByIP(w, r)
if host == nil {
return
}
2018-11-16 03:33:30 +00:00
what := strings.TrimLeft(r.URL.Path, "/")
2018-06-12 10:09:47 +00:00
renderHost(w, r, what, host, cfg)
}
2018-12-10 10:59:24 +00:00
func hostByIP(w http.ResponseWriter, r *http.Request) (*localconfig.Host, *localconfig.Config) {
2018-06-12 10:09:47 +00:00
remoteAddr := r.RemoteAddr
if *trustXFF {
if xff := r.Header.Get("X-Forwarded-For"); xff != "" {
2018-06-12 10:09:47 +00:00
remoteAddr = strings.Split(xff, ",")[0]
}
}
hostIP, _, err := net.SplitHostPort(remoteAddr)
if err != nil {
hostIP = remoteAddr
}
cfg, err := readConfig()
if err != nil {
http.Error(w, "", http.StatusServiceUnavailable)
return nil, nil
}
host := cfg.HostByIP(hostIP)
if host == nil {
log.Print("no host found for IP ", hostIP)
http.NotFound(w, r)
return nil, nil
}
return host, cfg
}
2018-12-10 10:59:24 +00:00
func renderHost(w http.ResponseWriter, r *http.Request, what string, host *localconfig.Host, cfg *localconfig.Config) {
2018-07-03 07:35:52 +00:00
ctx, err := newRenderContext(host, cfg)
if err != nil {
log.Printf("host %s: %s: failed to render: %v", what, host.Name, err)
http.Error(w, "", http.StatusServiceUnavailable)
return
}
2018-06-12 10:09:47 +00:00
switch what {
case "ipxe":
w.Header().Set("Content-Type", "text/x-ipxe")
case "config":
w.Header().Set("Content-Type", "text/vnd.yaml")
default:
w.Header().Set("Content-Type", "application/octet-stream")
}
switch what {
case "ipxe":
err = renderIPXE(w, ctx)
case "kernel":
err = renderKernel(w, r, ctx)
case "initrd":
2018-12-10 10:59:24 +00:00
err = renderCtx(w, r, ctx, what, buildInitrd)
2018-06-12 10:09:47 +00:00
case "boot.iso":
2018-12-10 10:59:24 +00:00
err = renderCtx(w, r, ctx, what, buildBootISO)
2018-06-12 10:09:47 +00:00
2018-07-03 07:35:52 +00:00
case "boot.tar":
2018-12-10 10:59:24 +00:00
err = renderCtx(w, r, ctx, what, buildBootTar)
2018-07-03 07:35:52 +00:00
2018-11-15 07:07:10 +00:00
case "boot.img":
2018-11-13 03:44:15 +00:00
err = renderCtx(w, r, ctx, what, buildBootImg)
2018-11-15 07:07:10 +00:00
case "boot.img.gz":
err = renderCtx(w, r, ctx, what, buildBootImgGZ)
case "boot.img.lz4":
err = renderCtx(w, r, ctx, what, buildBootImgLZ4)
2018-06-12 10:09:47 +00:00
case "config":
err = renderConfig(w, r, ctx)
default:
http.NotFound(w, r)
}
if err != nil {
if isNotFound(err) {
log.Printf("host %s: %s: %v", what, host.Name, err)
http.NotFound(w, r)
} else {
log.Printf("host %s: %s: failed to render: %v", what, host.Name, err)
http.Error(w, "", http.StatusServiceUnavailable)
}
}
}
2019-01-22 20:51:05 +00:00
func writeError(w http.ResponseWriter, err error) {
log.Print("request failed: ", err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(http.StatusText(http.StatusInternalServerError)))
}