feat: add RemoteAddr to /api

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

25
app.go
View File

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