This commit is contained in:
Mikaël Cluseau
2019-02-04 14:39:37 +11:00
parent 382b6d2559
commit 4a77737d8b
3 changed files with 33 additions and 97 deletions

View File

@ -3,10 +3,8 @@ package main
import (
"flag"
"log"
"net"
"net/http"
"regexp"
"strings"
"novit.nc/direktil/pkg/localconfig"
)
@ -17,49 +15,6 @@ var (
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
}
what := strings.TrimLeft(r.URL.Path, "/")
renderHost(w, r, what, host, cfg)
}
func hostByIP(w http.ResponseWriter, r *http.Request) (*localconfig.Host, *localconfig.Config) {
remoteAddr := r.RemoteAddr
if *trustXFF {
if xff := r.Header.Get("X-Forwarded-For"); xff != "" {
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
}
func renderHost(w http.ResponseWriter, r *http.Request, what string, host *localconfig.Host, cfg *localconfig.Config) {
ctx, err := newRenderContext(host, cfg)
if err != nil {
@ -119,9 +74,3 @@ func renderHost(w http.ResponseWriter, r *http.Request, what string, host *local
}
}
}
func writeError(w http.ResponseWriter, err error) {
log.Print("request failed: ", err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(http.StatusText(http.StatusInternalServerError)))
}

View File

@ -4,7 +4,6 @@ import (
"compress/gzip"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
@ -12,31 +11,27 @@ import (
)
func wsUploadConfig(req *restful.Request, resp *restful.Response) {
r := req.Request
w := resp.ResponseWriter
body := req.Request.Body
if !authorizeAdmin(r) {
forbidden(w, r)
return
}
if r.Method != "POST" {
http.NotFound(w, r)
return
err := writeNewConfig(body)
body.Close()
if err != nil {
wsError(resp, err)
}
}
func writeNewConfig(reader io.Reader) (err error) {
out, err := ioutil.TempFile(*dataDir, ".config-upload")
if err != nil {
writeError(w, err)
return
}
defer os.Remove(out.Name())
_, err = io.Copy(out, r.Body)
_, err = io.Copy(out, req.Request.Body)
out.Close()
if err != nil {
writeError(w, err)
return
}
@ -45,44 +40,36 @@ func wsUploadConfig(req *restful.Request, resp *restful.Response) {
err = os.MkdirAll(archivesPath, 0700)
if err != nil {
writeError(w, err)
return
}
err = func() (err error) {
backupPath := filepath.Join(archivesPath, "config."+ulid()+".yaml.gz")
backupPath := filepath.Join(archivesPath, "config."+ulid()+".yaml.gz")
bck, err := os.Create(backupPath)
if err != nil {
return
}
defer bck.Close()
in, err := os.Open(cfgPath)
if err != nil {
return
}
gz, err := gzip.NewWriterLevel(bck, 2)
if err != nil {
return
}
_, err = io.Copy(gz, in)
gz.Close()
in.Close()
bck, err := os.Create(backupPath)
if err != nil {
return
}()
}
defer bck.Close()
in, err := os.Open(cfgPath)
if err != nil {
return
}
gz, err := gzip.NewWriterLevel(bck, 2)
if err != nil {
return
}
_, err = io.Copy(gz, in)
gz.Close()
in.Close()
if err != nil {
writeError(w, err)
return
}
err = os.Rename(out.Name(), cfgPath)
if err != nil {
writeError(w, err)
return
}
return
}