117 lines
2.8 KiB
Go
117 lines
2.8 KiB
Go
package debugghttpy
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"net"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
func main() {
|
|
port := os.Getenv("PORT")
|
|
if port == "" {
|
|
port = "3000"
|
|
}
|
|
allowedOrigin := os.Getenv("DOMAIN")
|
|
if allowedOrigin == "" {
|
|
allowedOrigin = "*"
|
|
}
|
|
|
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Access-Control-Allow-Origin", allowedOrigin)
|
|
|
|
if r.Method == http.MethodOptions {
|
|
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, PATCH, OPTIONS")
|
|
w.Header().Set("Access-Control-Allow-Headers", "*")
|
|
w.WriteHeader(http.StatusNoContent)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
ip, portStr, err := net.SplitHostPort(r.RemoteAddr)
|
|
if err != nil {
|
|
ip = r.RemoteAddr
|
|
portStr = "unknown"
|
|
}
|
|
family := "IPv4"
|
|
if strings.Contains(ip, ":") {
|
|
family = "IPv6"
|
|
}
|
|
|
|
headers := make(map[string]string)
|
|
for k, v := range r.Header {
|
|
headers[strings.ToLower(k)] = strings.Join(v, ", ")
|
|
}
|
|
|
|
searchParams := make(map[string]string)
|
|
for k, v := range r.URL.Query() {
|
|
searchParams[k] = v[0]
|
|
}
|
|
|
|
var cookiesData interface{}
|
|
if r.URL.Query().Has("cookies") {
|
|
cookiesMap := make(map[string]string)
|
|
for _, c := range r.Cookies() {
|
|
cookiesMap[c.Name] = c.Value
|
|
}
|
|
cookiesData = cookiesMap
|
|
} else {
|
|
cookiesData = "disabled, add ?cookies to query to see cookies"
|
|
delete(headers, "cookie")
|
|
}
|
|
|
|
tlsInfo := "none"
|
|
if r.TLS != nil {
|
|
tlsInfo = fmt.Sprintf("TLS (Version: %x)", r.TLS.Version)
|
|
}
|
|
|
|
getValOrNil := func(val string) interface{} {
|
|
if val == "" {
|
|
return nil
|
|
}
|
|
return val
|
|
}
|
|
|
|
debugInfo := map[string]interface{}{
|
|
"app": "debugghttpy",
|
|
"rawSocket": map[string]interface{}{
|
|
"ip": ip,
|
|
"family": family,
|
|
"port": portStr,
|
|
},
|
|
"request": map[string]interface{}{
|
|
"method": r.Method,
|
|
"url": r.URL.RequestURI(),
|
|
"pathname": r.URL.Path,
|
|
"searchParams": searchParams,
|
|
"protocol": r.Proto,
|
|
"host": r.Host,
|
|
"contentLength": r.ContentLength,
|
|
"tls": tlsInfo,
|
|
},
|
|
"quickIps": map[string]interface{}{
|
|
"x-forwarded-for": getValOrNil(headers["x-forwarded-for"]),
|
|
"x-real-ip": getValOrNil(headers["x-real-ip"]),
|
|
"cf-connecting-ip": getValOrNil(headers["cf-connecting-ip"]),
|
|
"true-client-ip": getValOrNil(headers["true-client-ip"]),
|
|
},
|
|
"headers": headers,
|
|
"cookies": cookiesData,
|
|
}
|
|
|
|
enc := json.NewEncoder(w)
|
|
enc.SetIndent("", " ")
|
|
if err := enc.Encode(debugInfo); err != nil {
|
|
log.Printf("Error encoding JSON: %v", err)
|
|
}
|
|
})
|
|
|
|
log.Printf("🚀 debugghttpy listening on port %s\n", port)
|
|
log.Printf("🔒 CORS Allowed Origin: %s\n", allowedOrigin)
|
|
log.Fatal(http.ListenAndServe(":"+port, nil))
|
|
}
|