cleanup
This commit is contained in:
parent
382b6d2559
commit
4a77737d8b
@ -3,10 +3,8 @@ package main
|
|||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"novit.nc/direktil/pkg/localconfig"
|
"novit.nc/direktil/pkg/localconfig"
|
||||||
)
|
)
|
||||||
@ -17,49 +15,6 @@ var (
|
|||||||
trustXFF = flag.Bool("trust-xff", true, "Trust the X-Forwarded-For header")
|
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) {
|
func renderHost(w http.ResponseWriter, r *http.Request, what string, host *localconfig.Host, cfg *localconfig.Config) {
|
||||||
ctx, err := newRenderContext(host, cfg)
|
ctx, err := newRenderContext(host, cfg)
|
||||||
if err != nil {
|
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)))
|
|
||||||
}
|
|
||||||
|
@ -4,7 +4,6 @@ import (
|
|||||||
"compress/gzip"
|
"compress/gzip"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
@ -12,31 +11,27 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func wsUploadConfig(req *restful.Request, resp *restful.Response) {
|
func wsUploadConfig(req *restful.Request, resp *restful.Response) {
|
||||||
r := req.Request
|
body := req.Request.Body
|
||||||
w := resp.ResponseWriter
|
|
||||||
|
|
||||||
if !authorizeAdmin(r) {
|
err := writeNewConfig(body)
|
||||||
forbidden(w, r)
|
body.Close()
|
||||||
return
|
|
||||||
}
|
if err != nil {
|
||||||
|
wsError(resp, err)
|
||||||
if r.Method != "POST" {
|
|
||||||
http.NotFound(w, r)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeNewConfig(reader io.Reader) (err error) {
|
||||||
out, err := ioutil.TempFile(*dataDir, ".config-upload")
|
out, err := ioutil.TempFile(*dataDir, ".config-upload")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeError(w, err)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
defer os.Remove(out.Name())
|
defer os.Remove(out.Name())
|
||||||
|
|
||||||
_, err = io.Copy(out, r.Body)
|
_, err = io.Copy(out, req.Request.Body)
|
||||||
out.Close()
|
out.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeError(w, err)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,11 +40,9 @@ func wsUploadConfig(req *restful.Request, resp *restful.Response) {
|
|||||||
|
|
||||||
err = os.MkdirAll(archivesPath, 0700)
|
err = os.MkdirAll(archivesPath, 0700)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeError(w, err)
|
|
||||||
return
|
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)
|
bck, err := os.Create(backupPath)
|
||||||
@ -72,17 +65,11 @@ func wsUploadConfig(req *restful.Request, resp *restful.Response) {
|
|||||||
_, err = io.Copy(gz, in)
|
_, err = io.Copy(gz, in)
|
||||||
gz.Close()
|
gz.Close()
|
||||||
in.Close()
|
in.Close()
|
||||||
return
|
|
||||||
}()
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeError(w, err)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = os.Rename(out.Name(), cfgPath)
|
err = os.Rename(out.Name(), cfgPath)
|
||||||
if err != nil {
|
|
||||||
writeError(w, err)
|
|
||||||
return
|
return
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
6
go.mod
6
go.mod
@ -11,7 +11,7 @@ require (
|
|||||||
github.com/go-openapi/spec v0.18.0 // indirect
|
github.com/go-openapi/spec v0.18.0 // indirect
|
||||||
github.com/go-openapi/swag v0.18.0 // indirect
|
github.com/go-openapi/swag v0.18.0 // indirect
|
||||||
github.com/gobuffalo/buffalo-plugins v1.12.1 // 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/json-iterator/go v1.1.5 // indirect
|
||||||
github.com/markbates/going v1.0.3 // indirect
|
github.com/markbates/going v1.0.3 // indirect
|
||||||
github.com/mcluseau/go-swagger-ui v0.0.0-20190204031235-fc4ac9154422
|
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/pierrec/lz4 v2.0.5+incompatible
|
||||||
github.com/spf13/afero v1.2.1 // indirect
|
github.com/spf13/afero v1.2.1 // indirect
|
||||||
github.com/ugorji/go/codec v0.0.0-20190128213124-ee1426cffec0 // 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/crypto v0.0.0-20190131182504-b8fe1690c613
|
||||||
golang.org/x/net v0.0.0-20190125091013-d26f9f9a57f3 // indirect
|
golang.org/x/net v0.0.0-20190125091013-d26f9f9a57f3
|
||||||
golang.org/x/sys v0.0.0-20190203050204-7ae0202eb74c // indirect
|
golang.org/x/sys v0.0.0-20190203050204-7ae0202eb74c // indirect
|
||||||
golang.org/x/tools v0.0.0-20190202235157-7414d4c1f71c // indirect
|
golang.org/x/tools v0.0.0-20190202235157-7414d4c1f71c // indirect
|
||||||
gopkg.in/yaml.v2 v2.2.2
|
gopkg.in/yaml.v2 v2.2.2
|
||||||
|
Loading…
Reference in New Issue
Block a user