diff --git a/argv.go b/argv.go index 906dd65..6984b38 100644 --- a/argv.go +++ b/argv.go @@ -13,7 +13,7 @@ var argv args type args struct { ListRepos bool `arg:"--list-repos" help:"list all repositories"` - Port int `arg:"--port" default:"2520" help:"port to run on"` + Port int `arg:"--port" default:"2520" help:"port to run on"` } func (args) Version() string { diff --git a/dumpClient.go b/dumpClient.go new file mode 100644 index 0000000..6c5c1af --- /dev/null +++ b/dumpClient.go @@ -0,0 +1,150 @@ +package main + +import ( + "fmt" + "io/ioutil" + "net/http" + "os" + "time" + + "go.wit.com/log" +) + +func dumpClient(accessf, clientf *os.File, r *http.Request) { + var host, url, proto, addr, agent string + + host = r.Host + url = r.URL.String() + proto = r.Proto + addr = r.RemoteAddr + agent = r.UserAgent() + + log.Warn(host, proto, addr, url, agent) + + fmt.Fprintln(accessf, time.Now(), host, proto, addr, url, agent) + // return + + fmt.Fprintln(clientf) + fmt.Fprintln(clientf, time.Now()) + // Basic request information + fmt.Fprintln(clientf, "Method:", r.Method) + fmt.Fprintln(clientf, "URL:", r.URL) + fmt.Fprintln(clientf, "Protocol:", r.Proto) + fmt.Fprintln(clientf, "Host:", r.Host) + fmt.Fprintln(clientf, "Remote Address:", r.RemoteAddr) + + // Headers + fmt.Fprintln(clientf, "Headers:") + for name, values := range r.Header { + for _, value := range values { + fmt.Fprintln(clientf, "Headers:", name, value) + } + } + + // Query parameters + fmt.Fprintln(clientf, "Query Parameters:") + for param, values := range r.URL.Query() { + for _, value := range values { + fmt.Fprintln(clientf, "Query:", param, value) + } + } + + // User-Agent + fmt.Fprintln(clientf, "User-Agent:", r.UserAgent()) + + // Content Length + fmt.Fprintln(clientf, "Content Length:", r.ContentLength) + + // Cookies + fmt.Fprintln(clientf, "Cookies:") + for _, cookie := range r.Cookies() { + fmt.Fprintln(clientf, cookie.Name, cookie.Value) + } + + // Request Body (if applicable) + if r.Body != nil { + body, err := ioutil.ReadAll(r.Body) + if err == nil { + fmt.Fprintln(clientf, "Body:", string(body)) + } + } +} + +func registerClient(f *os.File, r *http.Request) bool { + var host, url, proto, addr, agent string + var giturl, gopath string + + host = r.Host + url = r.URL.String() + proto = r.Proto + addr = r.RemoteAddr + agent = r.UserAgent() + + log.Warn(host, proto, addr, url, agent) + + fmt.Fprintln(f, time.Now(), host, proto, addr, url, agent) + // return + + fmt.Fprintln(f) + fmt.Fprintln(f, time.Now()) + // Basic request information + fmt.Fprintln(f, "Method:", r.Method) + fmt.Fprintln(f, "URL:", r.URL) + fmt.Fprintln(f, "Protocol:", r.Proto) + fmt.Fprintln(f, "Host:", r.Host) + fmt.Fprintln(f, "Remote Address:", r.RemoteAddr) + + // Headers + fmt.Fprintln(f, "Headers:") + for name, values := range r.Header { + for _, value := range values { + fmt.Fprintln(f, "Headers:", name, value) + if name == "Giturl" { + giturl = value + } + if name == "Gopath" { + gopath = value + } + // Giturl https://git.wit.org/gui/go-gui-toolkits.git + // Headers: Gopath + } + } + + // Query parameters + fmt.Fprintln(f, "Query Parameters:") + for param, values := range r.URL.Query() { + for _, value := range values { + fmt.Fprintln(f, "Query:", param, value) + } + } + + // User-Agent + fmt.Fprintln(f, "User-Agent:", r.UserAgent()) + + // Content Length + fmt.Fprintln(f, "Content Length:", r.ContentLength) + + // Cookies + fmt.Fprintln(f, "Cookies:") + for _, cookie := range r.Cookies() { + fmt.Fprintln(f, cookie.Name, cookie.Value) + } + + // Request Body (if applicable) + if r.Body != nil { + body, err := ioutil.ReadAll(r.Body) + if err == nil { + fmt.Fprintln(f, "Body:", string(body)) + } + } + + fmt.Fprintln(f, "gopath =", gopath, "giturl =", giturl) + if gopath == "" { + return false + } + if giturl == "" { + return false + } + fmt.Fprintln(f, "Sent back OK") + return true +} diff --git a/http.go b/http.go new file mode 100644 index 0000000..19e1bb8 --- /dev/null +++ b/http.go @@ -0,0 +1,79 @@ +package main + +import ( + "fmt" + "net/http" + "strings" + + "go.wit.com/log" +) + +// remove '?' part and trailing '/' +func cleanURL(url string) string { + url = "/" + strings.Trim(url, "/") + return url +} + +func okHandler(w http.ResponseWriter, r *http.Request) { + var tmp string + tmp = cleanURL(r.URL.Path) + + if tmp == "/" { + fmt.Fprintln(w, "OK") + return + } + if tmp == "/me" { + j, err := dumpJsonClient(r) + if err != nil { + fmt.Fprintln(w, "BAD ZOOT") + return + } + fmt.Fprintln(w, j) + return + } + if tmp == "/favicon.ico" { + writeFile(w, "ipv6.png") + return + } + // used for uptime monitor checking + if tmp == "/uptime" { + writeFile(w, "uptime.html") + return + } + log.Warn("BAD URL =", tmp) + fmt.Fprintln(w, "BAD ZOOT") + // badurl(w, r.URL.String()) + // fmt.Fprintln(w, "BAD", tmp) +} + +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) + if filename == "goReference.svg" { + w.Header().Set("Content-Type", "image/svg+xml") + } + fmt.Fprintln(w, repohtml) + log.Println("writeFile() found internal file:", filename) +} + +// starts and sits waiting for HTTP requests +func startHTTP() { + http.HandleFunc("/", okHandler) + + p := fmt.Sprintf(":%d", argv.Port) + log.Println("Running on port", p) + + err := http.ListenAndServe(p, nil) + if err != nil { + log.Println("Error starting server:", err) + } +} diff --git a/jsonClient.go b/jsonClient.go new file mode 100644 index 0000000..4dc9b10 --- /dev/null +++ b/jsonClient.go @@ -0,0 +1,108 @@ +package main + +import ( + "bytes" + "encoding/json" + "io/ioutil" + "net/http" +) + +// RequestInfo holds the extracted data from http.Request +type RequestInfo struct { + Host string `json:"host"` + URL string `json:"url"` + Proto string `json:"proto"` + Addr string `json:"addr"` + Agent string `json:"agent"` + Headers map[string][]string `json:"headers"` + Cookies map[string]string `json:"cookies"` + QueryParams map[string][]string `json:"queryParams"` + // Add other fields as needed + Body string `json:"body"` +} + +// dumpClient returns a string with JSON formatted http.Request information +func dumpJsonClient(r *http.Request) (string, error) { + // Extracting Cookies + cookieMap := make(map[string]string) + for _, cookie := range r.Cookies() { + cookieMap[cookie.Name] = cookie.Value + } + + // Read the body (Ensure to do this first) + var bodyBytes []byte + if r.Body != nil { // Read the body if it's not nil + bodyBytes, _ = ioutil.ReadAll(r.Body) + r.Body.Close() // Close the body when done reading + r.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes)) // Reset the body + } + + info := RequestInfo{ + Host: r.Host, + URL: r.URL.String(), + Proto: r.Proto, + Addr: r.RemoteAddr, + Agent: r.UserAgent(), + Headers: r.Header, + Cookies: cookieMap, + QueryParams: r.URL.Query(), + Body: string(bodyBytes), + } + + // Marshal the struct to a JSON string + jsonString, err := json.Marshal(info) + if err != nil { + return "", err // Return the error to the caller + } + + var jsonData interface{} + err = json.Unmarshal([]byte(jsonString), &jsonData) + if err != nil { + return "", err // Return the error to the caller + } + + formattedJSON, err := json.MarshalIndent(jsonData, "", " ") + if err != nil { + return "", err // Return the error to the caller + } + + return string(formattedJSON), nil +} + +/* +package main + +import ( + "bytes" + "encoding/json" + "io" + "io/ioutil" + "net/http" +) + +type RequestInfo struct { + // ... (other fields) + Body string `json:"body"` + // ... (other fields) +} + +func dumpClient(r *http.Request) (string, error) { + // ... (rest of your code to collect other request info) + + info := RequestInfo{ + // ... (other fields) + Body: string(bodyBytes), + // ... (other fields) + } + + // Marshal the struct to a JSON string + jsonString, err := json.Marshal(info) + if err != nil { + return "", err + } + + return string(jsonString), nil +} + +// ... (rest of your code) +*/ diff --git a/main.go b/main.go index 577afab..b3057fb 100644 --- a/main.go +++ b/main.go @@ -15,6 +15,7 @@ package main import ( + "embed" "flag" "fmt" "log" @@ -27,6 +28,9 @@ import ( var Version string +//go:embed resources/* +var resources embed.FS + func main() { flag.Parse() @@ -59,10 +63,10 @@ func main() { } // fmt.Printf("\nStatus: %s\n", status) /* - if status == qemu.StatusRunning { - // getBlockDevices(dom) - // fmt.Println(name, status, err) - } + if status == qemu.StatusRunning { + // getBlockDevices(dom) + // fmt.Println(name, status, err) + } */ blockDevices, err := dom.BlockDevices() for _, blockDevice := range blockDevices { @@ -79,6 +83,8 @@ func main() { } } // fmt.Printf("\n***************************\n") + + startHTTP() } func displayBlockDevices(domain *qemu.Domain) { @@ -110,4 +116,3 @@ func displayPCIDevices(domain *qemu.Domain) { fmt.Printf("[%10s] [%20s]\n", pciDevice.QdevID, pciDevice.ClassInfo.Desc) } } - diff --git a/resources/goReference.svg b/resources/goReference.svg new file mode 100644 index 0000000..99e2d73 --- /dev/null +++ b/resources/goReference.svg @@ -0,0 +1 @@ + \ No newline at end of file