5 Commits

Author SHA1 Message Date
bel
eaa8debb73 no binary 2022-07-09 16:36:57 -06:00
bel
3727e2ee5e support for basic auth 2022-07-09 16:27:49 -06:00
bel
7fabfcd14d got md ok 2022-07-09 16:18:11 -06:00
bel
19c13c43f6 log 2022-07-09 15:56:21 -06:00
bel
fc7451ab40 add https only forwarding 2022-07-09 15:49:14 -06:00
3 changed files with 36 additions and 4 deletions

1
.gitignore vendored
View File

@@ -1,4 +1,5 @@
*.key
/exec-*
*.crt
*.pem
*.swp

38
main.go
View File

@@ -31,8 +31,10 @@ var (
func main() {
fs = args.NewArgSet()
fs.Append(args.STRING, "p", "port to serve", "8100")
fs.Append(args.STRING, "u", "user:pass for basic auth", "")
fs.Append(args.BOOL, "md", "whether to render markdown as html", true)
fs.Append(args.BOOL, "ro", "read only mode", false)
fs.Append(args.BOOL, "https", "https only", false)
fs.Append(args.STRING, "md-css", "css to load for md", "/dev/null")
fs.Append(args.STRING, "md-class", "class to wrap md", "phb")
fs.Append(args.STRING, "d", "static path to serve", "./public")
@@ -41,8 +43,10 @@ func main() {
}
d := fs.Get("d").GetString()
userPass := fs.Get("u").GetString()
md := fs.Get("md").GetBool()
ro := fs.Get("ro").GetBool()
https := fs.Get("https").GetBool()
mdCss := fs.Get("md-css").GetString()
mdClass := fs.Get("md-class").GetString()
if mdCss != "" {
@@ -91,15 +95,15 @@ func main() {
}
p := strings.TrimPrefix(fs.Get("p").GetString(), ":")
http.Handle("/", http.HandlerFunc(handler(ro, d, md, mdCss, mdClass)))
http.Handle("/", http.HandlerFunc(handler(userPass, https, ro, d, md, mdCss, mdClass)))
log.Printf("Serving %s on HTTP port: %s\n", d, p)
log.Fatal(http.ListenAndServe(":"+p, nil))
}
func handler(ro bool, d string, md bool, mdCss, mdClass string) http.HandlerFunc {
return gzip(endpoints(ro, withDel(ro, withMD(d, md, mdCss, mdClass, fserve(d)))))
func handler(userPass string, https, ro bool, d string, md bool, mdCss, mdClass string) http.HandlerFunc {
return httpsOnly(https, gzip(basicAuth(userPass, endpoints(ro, withDel(ro, withMD(d, md, mdCss, mdClass, fserve(d)))))))
}
func writeMeta(w http.ResponseWriter) {
@@ -120,6 +124,32 @@ func writeForm(w http.ResponseWriter) {
`, ENDPOINT_UPLOAD)
}
func basicAuth(userPass string, foo http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if userPass != "" {
u, p, ok := r.BasicAuth()
if !ok || u+":"+p != userPass {
w.Header().Set("WWW-Authenticate", "Basic")
w.WriteHeader(401)
return
}
}
foo(w, r)
}
}
func httpsOnly(https bool, foo http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if https && r.URL.Scheme != "https" {
log.Printf("redirecting: %+v", r.URL)
r.URL.Scheme = "https"
http.Redirect(w, r, r.URL.String(), http.StatusSeeOther)
return
}
foo(w, r)
}
}
func gzip(foo http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if gziphttp.Can(r) {
@@ -208,7 +238,7 @@ func withDel(ro bool, foo http.HandlerFunc) http.HandlerFunc {
foo(w, r)
return
}
fmt.Fprintln(w, `<a href=".."><input type="button" style="padding: .15em 4em .35em 4em" value=".."/></a>`+"\n")
fmt.Fprintln(w, `<a href=".."><input type="button" style="padding: .15em 4em .35em 4em" value=".."/></a>`)
w2 := httptest.NewRecorder()
foo(w2, r)
b := bytes.Split(w2.Body.Bytes(), []byte("\n"))

View File

@@ -28,6 +28,7 @@ func SetContentTypeIfMedia(w http.ResponseWriter, r *http.Request) {
v = "image/jpeg"
case ".md":
v = "text/markdown"
v = "text/html"
case ".gif":
v = "image/gif"
case ".png":