made rx kidna work!!
sophuwu sophie@skisiel.com
Sun, 07 Jan 2024 02:51:40 +0100
1 files changed,
45 insertions(+),
13 deletions(-)
jump to
M
main.go
→
main.go
@@ -6,7 +6,6 @@ _ "embed"
"fmt" "net/http" "os/exec" - "path/filepath" "strings" )@@ -29,29 +28,62 @@ w.WriteHeader(http.StatusOK)
w.Write(css) }) http.Handle("/", http.RedirectHandler("/cgi-bin/man/man2html", http.StatusTemporaryRedirect)) - http.ListenAndServe("0.0.0.0:1234", nil) + http.ListenAndServe("0.0.0.0:3234", nil) +} + +func getARG(exe, pth string) string { + i := strings.Index(pth, exe) + if i == -1 { + return "" + } + return pth[i+len(exe)-1:] +} + +func getEXE(s string) string { + mandex := [4]string{"man2html", "manwhatis", "mansearch", "mansec"} + for k := range mandex { + if strings.Contains(s, mandex[k]) { + return mandex[k] + } + } + return "" } func handleWIS(w http.ResponseWriter, r *http.Request) { - exe := filepath.Base(r.URL.Path) - if !(exe == "man2html" || exe == "mansearch" || exe == "mansec" || exe == "manwhatis") { - http.Redirect(w, r, "/cgi-bin/man/man2html", http.StatusTemporaryRedirect) - return + + fmt.Println(r.Method, r.URL.Path, r.URL.Query().Encode()) + + exe := getEXE(r.URL.Path) + if exe == "" { + http.RedirectHandler("/cgi-bin/man/man2html", 404) } - q := "" - if strings.Contains(r.URL.String(), "?") { - q = strings.SplitN(r.URL.String(), "?", 2)[1] + + q := "QUERY_STRING=" + r.URL.RawQuery + + var opt []string + var rx string = fmt.Sprint(r.URL.Query()["query"]) + if strings.IndexAny(rx, `^*&|;`) > 0 { + rx = fmt.Sprintf("QUERY_STRING= -r'%s' ", rx) + opt = append(opt, rx) + q = rx + q + exe = "mansearch" } - cmd := exec.Command("/usr/lib/cgi-bin/man/" + exe) + + cmd := exec.Command("/usr/lib/cgi-bin/man/"+exe, opt...) + var buff bytes.Buffer - cmd.Env = append(cmd.Env, "QUERY_STRING="+q, "REQUEST_METHOD="+r.Method, "SERVER_NAME=localhost:1234") - // cmd.Env = append(cmd.Env, "MANPATH=/usr/man:/usr/share/man:/usr/local/man:/usr/local/share/man:/usr/X11R6/man:/opt/man:/snap/man") cmd.Stdout = &buff + + cmd.Env = append(cmd.Env, q, "REQUEST_METHOD="+r.Method, "SERVER_NAME=localhost:1234") + cmd.Env = append(cmd.Env, "MANPATH=/usr/man:/usr/share/man:/usr/local/man:/usr/local/share/man:/usr/X11R6/man:/opt/man:/snap/man") + err := cmd.Run() if err != nil { - http.Redirect(w, r, "/cgi-bin/man/man2html", http.StatusTemporaryRedirect) + fmt.Println(err) + http.Error(w, "Internal Server Error", 500) return } + w.Header().Set("Content-Type", "text/html; charset=utf-8") w.WriteHeader(http.StatusOK) page := buff.String()