feat: add RemoteAddr to /api

This commit is contained in:
Fernandez Ludovic
2023-07-08 15:24:56 +02:00
parent 5a6136c5e7
commit 09b74d7912

45
app.go
View File

@ -10,7 +10,6 @@ import (
"log" "log"
"net" "net"
"net/http" "net/http"
"net/url"
"os" "os"
"strconv" "strconv"
"strings" "strings"
@ -56,13 +55,14 @@ func init() {
} }
type Data struct { type Data struct {
Hostname string `json:"hostname,omitempty"` Hostname string `json:"hostname,omitempty"`
IP []string `json:"ip,omitempty"` IP []string `json:"ip,omitempty"`
Headers http.Header `json:"headers,omitempty"` Headers http.Header `json:"headers,omitempty"`
URL string `json:"url,omitempty"` URL string `json:"url,omitempty"`
Host string `json:"host,omitempty"` Host string `json:"host,omitempty"`
Method string `json:"method,omitempty"` Method string `json:"method,omitempty"`
Name string `json:"name,omitempty"` Name string `json:"name,omitempty"`
RemoteAddr string `json:"remoteAddr,omitempty"`
} }
func main() { func main() {
@ -164,8 +164,7 @@ func printBinary(s []byte) {
} }
func dataHandler(w http.ResponseWriter, r *http.Request) { func dataHandler(w http.ResponseWriter, r *http.Request) {
u, _ := url.Parse(r.URL.String()) queryParams := r.URL.Query()
queryParams := u.Query()
size, err := strconv.ParseInt(queryParams.Get("size"), 10, 64) size, err := strconv.ParseInt(queryParams.Get("size"), 10, 64)
if err != nil { if err != nil {
@ -206,9 +205,8 @@ func dataHandler(w http.ResponseWriter, r *http.Request) {
} }
} }
func whoamiHandler(w http.ResponseWriter, req *http.Request) { func whoamiHandler(w http.ResponseWriter, r *http.Request) {
u, _ := url.Parse(req.URL.String()) wait := r.URL.Query().Get("wait")
wait := u.Query().Get("wait")
if len(wait) > 0 { if len(wait) > 0 {
duration, err := time.ParseDuration(wait) duration, err := time.ParseDuration(wait)
if err == nil { if err == nil {
@ -227,24 +225,25 @@ func whoamiHandler(w http.ResponseWriter, req *http.Request) {
_, _ = fmt.Fprintln(w, "IP:", ip) _, _ = fmt.Fprintln(w, "IP:", ip)
} }
_, _ = fmt.Fprintln(w, "RemoteAddr:", req.RemoteAddr) _, _ = fmt.Fprintln(w, "RemoteAddr:", r.RemoteAddr)
if err := req.Write(w); err != nil { if err := r.Write(w); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} }
} }
func apiHandler(w http.ResponseWriter, req *http.Request) { func apiHandler(w http.ResponseWriter, r *http.Request) {
hostname, _ := os.Hostname() hostname, _ := os.Hostname()
data := Data{ data := Data{
Hostname: hostname, Hostname: hostname,
IP: getIPs(), IP: getIPs(),
Headers: req.Header, Headers: r.Header,
URL: req.URL.RequestURI(), URL: r.URL.RequestURI(),
Host: req.Host, Host: r.Host,
Method: req.Method, Method: r.Method,
Name: name, Name: name,
RemoteAddr: r.RemoteAddr,
} }
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")