initial something
This commit is contained in:
commit
19f943832f
|
@ -0,0 +1,3 @@
|
||||||
|
*.swp
|
||||||
|
go.mod
|
||||||
|
go.sum
|
|
@ -0,0 +1,30 @@
|
||||||
|
VERSION = $(shell git describe --tags)
|
||||||
|
|
||||||
|
# create the go.mod and go.sum if this is a brand new repo
|
||||||
|
# REDOMOD = $(shell if [ -e go.mod ]; then echo go.mod; else echo no go mod; fi)
|
||||||
|
REDOMOD = $(shell if [ -e go.sum ]; then echo go.sum exists; else GO111MODULE= go mod init; GO111MODULE= go mod tidy; fi)
|
||||||
|
|
||||||
|
all:
|
||||||
|
GO111MODULE=off go vet
|
||||||
|
|
||||||
|
# this is for release builds using the go.mod files
|
||||||
|
release-build:
|
||||||
|
@echo ${REDOMOD}
|
||||||
|
go build -v -ldflags "-X main.Version=${VERSION} -X gui.GUIVERSION=${VERSION}"
|
||||||
|
|
||||||
|
goimports:
|
||||||
|
goimports -w *.go
|
||||||
|
|
||||||
|
redomod:
|
||||||
|
rm -f go.*
|
||||||
|
GO111MODULE= go mod init
|
||||||
|
GO111MODULE= go mod tidy
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f go.*
|
||||||
|
|
||||||
|
# git clone the sources and all the golang dependancies into ~/go/src
|
||||||
|
# if you don't have go-clone, you can get it from http://go.wit.com/
|
||||||
|
git-clone:
|
||||||
|
# go-clone --recursive --go-src --no-work go.wit.com/apps/go-clone
|
||||||
|
go-clone --recursive --go-src --no-work go.wit.com/lib/daemons/virtigod
|
|
@ -0,0 +1,150 @@
|
||||||
|
package libme
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
|
@ -0,0 +1,75 @@
|
||||||
|
package libme
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"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 route string
|
||||||
|
|
||||||
|
log.Info("Got URL Path: ", r.URL.Path)
|
||||||
|
log.Info("Got URL Query:", r.URL.Query().Get("start"))
|
||||||
|
|
||||||
|
route = cleanURL(r.URL.Path)
|
||||||
|
|
||||||
|
msg, err := ioutil.ReadAll(r.Body) // Read the body as []byte
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintln(w, "ReadAll() error =", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Info("Got URL msg:", string(msg))
|
||||||
|
|
||||||
|
if route == "/" {
|
||||||
|
fmt.Fprintln(w, "OK")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if route == "/me" {
|
||||||
|
j, err := dumpJsonClient(r)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintln(w, "json parse error")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Fprintln(w, j)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if route == "/favicon.ico" {
|
||||||
|
w.Header().Set("Content-Type", "image/png")
|
||||||
|
}
|
||||||
|
|
||||||
|
if route == "/kill" {
|
||||||
|
log.Warn("KILLED")
|
||||||
|
fmt.Fprintln(w, "KILLED")
|
||||||
|
os.Exit(-1)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Warn("BAD URL =", route)
|
||||||
|
fmt.Fprintln(w, "BAD URL =", route)
|
||||||
|
}
|
||||||
|
|
||||||
|
// starts and sits waiting for HTTP requests
|
||||||
|
func StartMe(port int) {
|
||||||
|
http.HandleFunc("/", okHandler)
|
||||||
|
|
||||||
|
p := fmt.Sprintf(":%d", port)
|
||||||
|
log.Println("Running on port", p)
|
||||||
|
|
||||||
|
err := http.ListenAndServe(p, nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error starting server:", err)
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,108 @@
|
||||||
|
package libme
|
||||||
|
|
||||||
|
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)
|
||||||
|
*/
|
Loading…
Reference in New Issue