add http listen

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-10-11 23:01:55 -05:00
parent f71da2ef59
commit 25f7aa1d22
6 changed files with 349 additions and 6 deletions

150
dumpClient.go Normal file
View File

@ -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
}

79
http.go Normal file
View File

@ -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)
}
}

108
jsonClient.go Normal file
View File

@ -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)
*/

View File

@ -15,6 +15,7 @@
package main package main
import ( import (
"embed"
"flag" "flag"
"fmt" "fmt"
"log" "log"
@ -27,6 +28,9 @@ import (
var Version string var Version string
//go:embed resources/*
var resources embed.FS
func main() { func main() {
flag.Parse() flag.Parse()
@ -79,6 +83,8 @@ func main() {
} }
} }
// fmt.Printf("\n***************************\n") // fmt.Printf("\n***************************\n")
startHTTP()
} }
func displayBlockDevices(domain *qemu.Domain) { func displayBlockDevices(domain *qemu.Domain) {
@ -110,4 +116,3 @@ func displayPCIDevices(domain *qemu.Domain) {
fmt.Printf("[%10s] [%20s]\n", pciDevice.QdevID, pciDevice.ClassInfo.Desc) fmt.Printf("[%10s] [%20s]\n", pciDevice.QdevID, pciDevice.ClassInfo.Desc)
} }
} }

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 6.3 KiB