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
}

6
go.mod
View File

@ -11,7 +11,7 @@ require (
github.com/go-openapi/spec v0.18.0 // indirect
github.com/go-openapi/swag v0.18.0 // indirect
github.com/gobuffalo/buffalo-plugins v1.12.1 // indirect
github.com/google/certificate-transparency-go v1.0.21 // indirect
github.com/google/certificate-transparency-go v1.0.21
github.com/json-iterator/go v1.1.5 // indirect
github.com/markbates/going v1.0.3 // indirect
github.com/mcluseau/go-swagger-ui v0.0.0-20190204031235-fc4ac9154422
@ -21,8 +21,8 @@ require (
github.com/pierrec/lz4 v2.0.5+incompatible
github.com/spf13/afero v1.2.1 // indirect
github.com/ugorji/go/codec v0.0.0-20190128213124-ee1426cffec0 // indirect
golang.org/x/crypto v0.0.0-20190131182504-b8fe1690c613 // indirect
golang.org/x/net v0.0.0-20190125091013-d26f9f9a57f3 // indirect
golang.org/x/crypto v0.0.0-20190131182504-b8fe1690c613
golang.org/x/net v0.0.0-20190125091013-d26f9f9a57f3
golang.org/x/sys v0.0.0-20190203050204-7ae0202eb74c // indirect
golang.org/x/tools v0.0.0-20190202235157-7414d4c1f71c // indirect
gopkg.in/yaml.v2 v2.2.2