package main import ( "fmt" "io/ioutil" "net" "net/http" "strings" "time" "go.wit.com/lib/gui/shell" "go.wit.com/log" ) // remove '?' part and trailing '/' func cleanURL(url string) string { url = "/" + strings.Trim(url, "/") return url } func getIpSimple(r *http.Request) string { host, _, err := net.SplitHostPort(r.RemoteAddr) if err != nil { log.Printf("could not split host port: %v", err) return r.RemoteAddr // Fallback } return host } // getClientIP inspects the request for common headers to find the true client IP. func getClientIP(r *http.Request) string { // Caddy sets the X-Forwarded-For header. if forwardedFor := r.Header.Get("X-Forwarded-For"); forwardedFor != "" { // The header can be a comma-separated list of IPs. The first one is the original client. ips := strings.Split(forwardedFor, ",") return strings.TrimSpace(ips[0]) } // Fallback to RemoteAddr if the header is not present. host, _, err := net.SplitHostPort(r.RemoteAddr) if err != nil { return r.RemoteAddr } return host } func whoSent(r *http.Request) string { return log.Sprintf("%s\t%s", getClientIP(r), r.Header.Get("hostname")) } func okHandler(w http.ResponseWriter, r *http.Request) { // something appears to be buggy. always get this first I guess msg, err := ioutil.ReadAll(r.Body) // Read the body as []byte r.Body.Close() log.Info("TRYING TO MARSHAL bytes:", len(msg), err) who := whoSent(r) var route string route = cleanURL(r.URL.Path) parts := strings.Split(route, "?") // log.Info("client sent url =", route, parts) requrl := parts[0] if route == "/" { w.Header().Set("Content-Type", "text") fmt.Fprintf(w, "go.wit.com/apps/utils/forged Version: %s\n", argv.Version()) fmt.Fprintf(w, "\n") return } // used for uptime monitor checking if route == "/uptime" { writeFile(w, "uptime.html") return } log.Warn("forged REQUEST URL =", requrl, "from =", who) if strings.HasPrefix(route, "/patches/") { handlePatches(w, r, msg) return } if route == "/patchset" { if err := savePatchset(w, msg); err != nil { log.Warn("forged /patchset error", err) return } if err := me.forge.SavePatchsets(); err != nil { log.Warn("savePatchsets() failed", err) return } return } if route == "/lookup" { log.Info("doing lookup len(msg) =", len(msg)) found, err := lookupRepos(msg) if err != nil { return } /* for repo := range repos.IterAll() { repo.Namespace += "good" log.Infof("repo:%s,%s\n", repo.Namespace, repo.FullPath) } */ found.SendPB(w) return } if route == "/update" { log.Info("doing update len(msg) =", len(msg)) found, err := updateRepos(msg) if err != nil { return } found.SendPB(w) return } if route == "/GetPatchsets" { start := time.Now() log.Info("going to w.Write(msg) with len", len(msg)) w.Write(msg) age := shell.FormatDuration(time.Since(start)) log.Printf("Done with xfer in (%s). happy hacking!\n", age) return } if route == "/patchsetget" { filename := r.URL.Query().Get("filename") getPatchset(w, filename) return } if route == "/goReference.svg" { w.Header().Set("Content-Type", "image/svg+xml") writeFile(w, "goReference.svg") return } if route == "/skeleton.v2.css" { writeFile(w, "skeleton.v2.css") return } if route == "/favicon.ico" { writeFile(w, "ipv6.png") return } log.Warn("BAD URL =", requrl, "from", who) badurl(w, r.URL.String()) } func writeFile(w http.ResponseWriter, filename string) { // fmt.Fprintln(w, "GOT TEST?") fullname := "resources/" + filename pfile, err := resources.ReadFile(fullname) if err != nil { log.Println("ERROR:", err) // w.Write(pfile) return } var repohtml string repohtml = string(pfile) fmt.Fprintln(w, repohtml) // log.Println("writeFile() found internal file:", filename) } func badurl(w http.ResponseWriter, badurl string) { fmt.Fprintln(w, "") fmt.Fprintln(w, "") fmt.Fprintln(w, " ") fmt.Fprintln(w, " ") fmt.Fprintln(w, " ") fmt.Fprintln(w, " ") fmt.Fprintln(w, " IPv4 IS NOT SUPPORTED
") fmt.Fprintln(w, " FORGE REQUIRES IPv6.
") fmt.Fprintln(w, "
") fmt.Fprintln(w, " bad url", badurl, "redirecting") fmt.Fprintln(w, " ") fmt.Fprintln(w, "") }