From 44ab3d16796df32ea72bf2cb5fdfeed666a1e2e1 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Wed, 17 Jan 2024 16:05:28 -0600 Subject: [PATCH] dump /me as JSON and /list for the autotypist Signed-off-by: Jeff Carr --- files/repomap | 10 +- files/{refresh.html => test-refresh.html} | 0 files/{repo.html => test.html} | 0 jsonClient.go | 107 ++++++++++++++++++++++ main.go | 23 +++-- 5 files changed, 128 insertions(+), 12 deletions(-) rename files/{refresh.html => test-refresh.html} (100%) rename files/{repo.html => test.html} (100%) create mode 100644 jsonClient.go diff --git a/files/repomap b/files/repomap index cb84f05..1263bf0 100644 --- a/files/repomap +++ b/files/repomap @@ -4,7 +4,8 @@ go.wit.com/log git.wit.org/wit/log # gui/ packages -go.wit.com/gui/gui git.wit.org/gui/gui +go.wit.com/gui git.wit.org/jcarr/old-gui/ +go.wit.com/gui/gui git.wit.org/jcarr/new-gui go.wit.com/gui/widget git.wit.org/gui/widget go.wit.com/gui/toolkits git.wit.org/gui/toolkits go.wit.com/gui/debugger git.wit.org/gui/debugger @@ -13,17 +14,20 @@ go.wit.com/gui/gadgets git.wit.org/gui/gadgets # gui libraries go.wit.com/gui/lib/repostatus git.wit.org/jcarr/repostatus go.wit.com/gui/lib/hostname git.wit.org/jcarr/hostname +go.wit.com/gui/lib/linuxstatus git.wit.org/jcarr/hostname go.wit.com/gui/lib/logsettings git.wit.org/jcarr/logsettings go.wit.com/gui/lib/digitalocean git.wit.org/gui/digitalocean go.wit.com/gui/lib/cloudflare git.wit.org/gui/cloudflare +go.wit.com/gui/lib/protobuf/wit git.wit.org/wit/witProtobuf # Applications +go.wit.com/apps/helloworld git.wit.org/gui/helloworld +go.wit.com/apps/autotypist git.wit.org/jcarr/myrepos go.wit.com/apps/control-panel-dns git.wit.org/jcarr/control-panel-dns go.wit.com/apps/control-panel-digitalocean git.wit.org/wit/control-panel-digitalocean go.wit.com/apps/control-panel-cloudflare git.wit.org/wit/control-panel-cloudflare -go.wit.com/apps/control-panel-vpn git.wit.org/wit/control-panel-vpn -go.wit.com/apps/autotypist git.wit.org/jcarr/myrepos +go.wit.com/apps/control-panel-vpn git.wit.org/jcarr/control-panel-vpn go.wit.com/apps/myrepos git.wit.org/jcarr/myrepos go.wit.com/apps/go.wit.com git.wit.org/jcarr/go.wit.com diff --git a/files/refresh.html b/files/test-refresh.html similarity index 100% rename from files/refresh.html rename to files/test-refresh.html diff --git a/files/repo.html b/files/test.html similarity index 100% rename from files/repo.html rename to files/test.html diff --git a/jsonClient.go b/jsonClient.go new file mode 100644 index 0000000..756fae7 --- /dev/null +++ b/jsonClient.go @@ -0,0 +1,107 @@ +package main + +import ( + "io/ioutil" + "bytes" + "encoding/json" + "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 3d3e676..c8d3dda 100644 --- a/main.go +++ b/main.go @@ -48,8 +48,21 @@ func okHandler(w http.ResponseWriter, r *http.Request) { findFile(w, "files/index.html") return } + if tmp == "/me" { + j, err := dumpJsonClient(r) + if err != nil { + fmt.Fprintln(w, "BAD ZOOT") + return + } + fmt.Fprintln(w, j) + return + } + if tmp == "/list" { + findFile(w, "files/repomap") + return + } if tmp == "/test" { - findFile(w, "files/repo.html") + findFile(w, "files/test.html") return } if tmp == "/skeleton.v2.css" { @@ -86,14 +99,6 @@ func findFile(w http.ResponseWriter, filename string) { func main() { accessf, _ = os.OpenFile("/home/jcarr/accessclient.log", os.O_RDWR | os.O_CREATE | os.O_APPEND, 0666) clientf, _ = os.OpenFile("/home/jcarr/httpclient.log", os.O_RDWR | os.O_CREATE | os.O_APPEND, 0666) - /* - pfile, err := htmlFiles.ReadFile("html/repo.html") - if (err != nil) { - log.Println("ERROR:", err) - // w.Write(pfile) - return - } - */ readconfigfile() http.HandleFunc("/", okHandler) https()